Fix linter problems on on eval.go and some of other pkg/core
This commit is contained in:
parent
5882897375
commit
4216547dd3
|
@ -35,9 +35,9 @@ func (t *Bool) GetType() ast.NodeType {
|
||||||
|
|
||||||
func (t *Bool) String() string {
|
func (t *Bool) String() string {
|
||||||
if t.value {
|
if t.value {
|
||||||
return "true"
|
return TRUEFORM
|
||||||
}
|
}
|
||||||
return "false"
|
return FALSEFORM
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Bool) ToDebugStr() string {
|
func (t *Bool) ToDebugStr() string {
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
Serene --- Yet an other Lisp
|
||||||
|
|
||||||
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 2 of the License.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package core
|
||||||
|
|
||||||
|
const NSFORM string = "ns"
|
||||||
|
const NILFORM string = "nil"
|
||||||
|
const FALSEFORM string = "false"
|
||||||
|
const TRUEFORM string = "true"
|
|
@ -36,7 +36,7 @@ func restOfExprs(es []IExpr, i int) []IExpr {
|
||||||
// evaluation rules. For example if `form` is a list instead of the formal
|
// evaluation rules. For example if `form` is a list instead of the formal
|
||||||
// evaluation of a list it will evaluate all the elements and return the
|
// evaluation of a list it will evaluate all the elements and return the
|
||||||
// evaluated list
|
// evaluated list
|
||||||
func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
|
func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) { //nolint:gocyclo
|
||||||
switch form.GetType() {
|
switch form.GetType() {
|
||||||
case ast.Nil:
|
case ast.Nil:
|
||||||
return form, nil
|
return form, nil
|
||||||
|
@ -73,11 +73,11 @@ func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
|
||||||
symbolName := sym.GetName()
|
symbolName := sym.GetName()
|
||||||
|
|
||||||
switch symbolName {
|
switch symbolName {
|
||||||
case "true":
|
case TRUEFORM:
|
||||||
return MakeTrue(MakeNodeFromExpr(form)), nil
|
return MakeTrue(MakeNodeFromExpr(form)), nil
|
||||||
case "false":
|
case FALSEFORM:
|
||||||
return MakeFalse(MakeNodeFromExpr(form)), nil
|
return MakeFalse(MakeNodeFromExpr(form)), nil
|
||||||
case "nil":
|
case NILFORM:
|
||||||
return MakeNil(MakeNodeFromExpr(form)), nil
|
return MakeNil(MakeNodeFromExpr(form)), nil
|
||||||
default:
|
default:
|
||||||
var expr *Binding
|
var expr *Binding
|
||||||
|
@ -141,7 +141,7 @@ func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
|
||||||
|
|
||||||
// EvalForms evaluates the given expr `expressions` (it can be a list, block, symbol or anything else)
|
// EvalForms evaluates the given expr `expressions` (it can be a list, block, symbol or anything else)
|
||||||
// with the given runtime `rt` and the scope `scope`.
|
// with the given runtime `rt` and the scope `scope`.
|
||||||
func EvalForms(rt *Runtime, scope IScope, expressions IExpr) (IExpr, IError) {
|
func EvalForms(rt *Runtime, scope IScope, expressions IExpr) (IExpr, IError) { //nolint:funlen,gocyclo
|
||||||
// EvalForms is the main and the most important evaluation function on Serene.
|
// EvalForms is the main and the most important evaluation function on Serene.
|
||||||
// It's a long loooooooooooong function. Why? Well, Because we don't want to
|
// It's a long loooooooooooong function. Why? Well, Because we don't want to
|
||||||
// waste call stack spots in order to have a well organized code.
|
// waste call stack spots in order to have a well organized code.
|
||||||
|
@ -246,7 +246,7 @@ tco:
|
||||||
// Empty list evaluates to itself
|
// Empty list evaluates to itself
|
||||||
if list.Count() == 0 {
|
if list.Count() == 0 {
|
||||||
ret = list
|
ret = list
|
||||||
break tco // return &Nil, nil
|
break tco // return &Nil, nil here
|
||||||
}
|
}
|
||||||
|
|
||||||
rawFirst := list.First()
|
rawFirst := list.First()
|
||||||
|
@ -273,7 +273,8 @@ tco:
|
||||||
// name of the namespace. ( We won't evaluate the first
|
// name of the namespace. ( We won't evaluate the first
|
||||||
// element )
|
// element )
|
||||||
// TODO: decide on the syntax and complete the docs
|
// TODO: decide on the syntax and complete the docs
|
||||||
case "ns":
|
case NSFORM:
|
||||||
|
// TODO: Move this to a native function
|
||||||
ret, err = NSForm(rt, scope, list)
|
ret, err = NSForm(rt, scope, list)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -598,7 +599,8 @@ tco:
|
||||||
MakeStackPop(rt),
|
MakeStackPop(rt),
|
||||||
)
|
)
|
||||||
changeExecutionScope(body, fnScope)
|
changeExecutionScope(body, fnScope)
|
||||||
exprs = append(body, restOfExprs(exprs, i)...)
|
body = append(body, restOfExprs(exprs, i)...)
|
||||||
|
exprs = body // Just because of the stupid linters
|
||||||
goto body // rewrite
|
goto body // rewrite
|
||||||
|
|
||||||
// If the function was a native function which is represented
|
// If the function was a native function which is represented
|
||||||
|
@ -674,7 +676,7 @@ func EvalNSBody(rt *Runtime, ns *Namespace) (*Namespace, IError) {
|
||||||
|
|
||||||
if exprs[0].GetType() == ast.List {
|
if exprs[0].GetType() == ast.List {
|
||||||
firstForm := exprs[0].(*List).First()
|
firstForm := exprs[0].(*List).First()
|
||||||
if firstForm.GetType() == ast.Symbol && firstForm.(*Symbol).GetName() == "ns" {
|
if firstForm.GetType() == ast.Symbol && firstForm.(*Symbol).GetName() == NSFORM {
|
||||||
_, err := EvalForms(rt, ns.GetRootScope(), body)
|
_, err := EvalForms(rt, ns.GetRootScope(), body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -46,7 +46,7 @@ func MakeMacro(scope IScope, name string, params IColl, body *Block) *Function {
|
||||||
// isMacroCall looks up the given `form` in the given `scope` if it is a symbol.
|
// isMacroCall looks up the given `form` in the given `scope` if it is a symbol.
|
||||||
// If there is a value associated with the symbol in the scope, it will be checked
|
// If there is a value associated with the symbol in the scope, it will be checked
|
||||||
// to be a macro.
|
// to be a macro.
|
||||||
func isMacroCall(rt *Runtime, scope IScope, form IExpr) (*Function, bool) {
|
func isMacroCall(rt *Runtime, scope IScope, form IExpr) (*Function, bool) { //nolint:interfacer
|
||||||
if form.GetType() == ast.List {
|
if form.GetType() == ast.List {
|
||||||
list := form.(*List)
|
list := form.(*List)
|
||||||
if list.Count() == 0 {
|
if list.Count() == 0 {
|
||||||
|
@ -74,7 +74,7 @@ func isMacroCall(rt *Runtime, scope IScope, form IExpr) (*Function, bool) {
|
||||||
// applyMacro works very similar to how we evaluate function calls the only difference
|
// applyMacro works very similar to how we evaluate function calls the only difference
|
||||||
// is that we don't evaluate the arguments and create the bindings in the scope of the
|
// is that we don't evaluate the arguments and create the bindings in the scope of the
|
||||||
// body directly as they are. It's Lisp Macroes after all.
|
// body directly as they are. It's Lisp Macroes after all.
|
||||||
func applyMacro(rt *Runtime, macro *Function, args *List) (IExpr, IError) {
|
func applyMacro(rt *Runtime, macro *Function, args IColl) (IExpr, IError) {
|
||||||
mscope, e := MakeFnScope(rt, macro.GetScope(), macro.GetParams(), args)
|
mscope, e := MakeFnScope(rt, macro.GetScope(), macro.GetParams(), args)
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
@ -90,7 +90,6 @@ func macroexpand(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
|
||||||
var macro *Function
|
var macro *Function
|
||||||
var e IError
|
var e IError
|
||||||
ok := false
|
ok := false
|
||||||
//form := expr
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
macro, ok = isMacroCall(rt, scope, form)
|
macro, ok = isMacroCall(rt, scope, form)
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (n *Namespace) getForms() *Block {
|
||||||
// `ns` string.
|
// `ns` string.
|
||||||
func requireNS(rt *Runtime, ns *Symbol) (*Namespace, IError) {
|
func requireNS(rt *Runtime, ns *Symbol) (*Namespace, IError) {
|
||||||
// TODO: use a hashing algorithm to avoid reloading an unchanged namespace
|
// TODO: use a hashing algorithm to avoid reloading an unchanged namespace
|
||||||
loadedForms, err := rt.LoadNS(ns)
|
loadedForms, err := rt.loadNS(ns)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -30,11 +30,11 @@ func (n *Nil) GetType() ast.NodeType {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nil) String() string {
|
func (n *Nil) String() string {
|
||||||
return "nil"
|
return NILFORM
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nil) ToDebugStr() string {
|
func (n *Nil) ToDebugStr() string {
|
||||||
return "nil"
|
return NILFORM
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Nil) Hash() uint32 {
|
func (n *Nil) Hash() uint32 {
|
||||||
|
|
|
@ -186,6 +186,7 @@ func makeErrorAtPoint(p IParsable, msg string, a ...interface{}) IError {
|
||||||
|
|
||||||
// makeErrorFromError is a function which wraps a Golang error in an IError
|
// makeErrorFromError is a function which wraps a Golang error in an IError
|
||||||
func makeErrorFromError(parser IParsable, e error) IError {
|
func makeErrorFromError(parser IParsable, e error) IError {
|
||||||
|
//nolint:govet
|
||||||
return makeErrorAtPoint(parser, "%w", e)
|
return makeErrorAtPoint(parser, "%w", e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,6 +425,7 @@ func readList(parser IParsable) (IExpr, IError) {
|
||||||
return MakeList(*node, list), nil
|
return MakeList(*node, list), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:unparam
|
||||||
func readComment(parser IParsable) (IExpr, IError) {
|
func readComment(parser IParsable) (IExpr, IError) {
|
||||||
for {
|
for {
|
||||||
c := parser.next(false)
|
c := parser.next(false)
|
||||||
|
|
|
@ -27,20 +27,20 @@ import (
|
||||||
"serene-lang.org/bootstrap/pkg/errors"
|
"serene-lang.org/bootstrap/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func toRepresanbleString(ast ...IRepresentable) string {
|
func toRepresanbleString(forms ...IRepresentable) string {
|
||||||
var results []string
|
var results []string
|
||||||
|
|
||||||
for _, x := range ast {
|
for _, x := range forms {
|
||||||
results = append(results, x.String())
|
results = append(results, x.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(results, " ")
|
return strings.Join(results, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func toPrintableString(ast ...IRepresentable) string {
|
func toPrintableString(forms ...IRepresentable) string {
|
||||||
var results []string
|
var results []string
|
||||||
|
|
||||||
for _, x := range ast {
|
for _, x := range forms {
|
||||||
if printable, ok := x.(IPrintable); ok {
|
if printable, ok := x.(IPrintable); ok {
|
||||||
results = append(results, printable.PrintToString())
|
results = append(results, printable.PrintToString())
|
||||||
continue
|
continue
|
||||||
|
@ -51,30 +51,30 @@ func toPrintableString(ast ...IRepresentable) string {
|
||||||
return strings.Join(results, " ")
|
return strings.Join(results, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Pr(rt *Runtime, ast ...IRepresentable) {
|
func Pr(rt *Runtime, forms ...IRepresentable) {
|
||||||
fmt.Print(toRepresanbleString(ast...))
|
fmt.Print(toRepresanbleString(forms...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Prn(rt *Runtime, ast ...IRepresentable) {
|
func Prn(rt *Runtime, forms ...IRepresentable) {
|
||||||
fmt.Println(toRepresanbleString(ast...))
|
fmt.Println(toRepresanbleString(forms...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Print(rt *Runtime, ast ...IRepresentable) {
|
func Print(rt *Runtime, forms ...IRepresentable) {
|
||||||
fmt.Print(toPrintableString(ast...))
|
fmt.Print(toPrintableString(forms...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Println(rt *Runtime, ast ...IRepresentable) {
|
func Println(rt *Runtime, forms ...IRepresentable) {
|
||||||
fmt.Println(toPrintableString(ast...))
|
fmt.Println(toPrintableString(forms...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func printError(rt *Runtime, err IError, stage int) {
|
func printError(_ *Runtime, err IError, stage int) {
|
||||||
loc := err.GetLocation()
|
loc := err.GetLocation()
|
||||||
source := loc.GetSource()
|
source := loc.GetSource()
|
||||||
|
|
||||||
startline := source.LineNumberFor(loc.GetStart())
|
startline := source.LineNumberFor(loc.GetStart())
|
||||||
|
|
||||||
if startline > 0 {
|
if startline > 0 {
|
||||||
startline -= 1
|
startline--
|
||||||
}
|
}
|
||||||
|
|
||||||
endline := source.LineNumberFor(loc.GetEnd()) + 1
|
endline := source.LineNumberFor(loc.GetEnd()) + 1
|
||||||
|
@ -138,7 +138,7 @@ func frameSource(traces *TraceBack, frameIndex int) string {
|
||||||
startline := callerSource.LineNumberFor(callerLoc.GetStart())
|
startline := callerSource.LineNumberFor(callerLoc.GetStart())
|
||||||
|
|
||||||
if startline > 0 {
|
if startline > 0 {
|
||||||
startline -= 1
|
startline--
|
||||||
}
|
}
|
||||||
|
|
||||||
endline := callerSource.LineNumberFor(callerLoc.GetEnd()) + 1
|
endline := callerSource.LineNumberFor(callerLoc.GetEnd()) + 1
|
||||||
|
@ -160,7 +160,7 @@ func frameSource(traces *TraceBack, frameIndex int) string {
|
||||||
return lines
|
return lines
|
||||||
}
|
}
|
||||||
|
|
||||||
func printErrorWithTraceBack(rt *Runtime, err IError) {
|
func printErrorWithTraceBack(_ *Runtime, err IError) {
|
||||||
trace := err.GetStackTrace()
|
trace := err.GetStackTrace()
|
||||||
|
|
||||||
for i := range *trace {
|
for i := range *trace {
|
||||||
|
|
|
@ -147,7 +147,7 @@ func nsNameToPath(ns string) string {
|
||||||
// LoadNS looks up the namespace specified by the given name `ns`
|
// LoadNS looks up the namespace specified by the given name `ns`
|
||||||
// and reads the content as expressions (parse it) and returns the
|
// and reads the content as expressions (parse it) and returns the
|
||||||
// expressions.
|
// expressions.
|
||||||
func (r *Runtime) LoadNS(ns *Symbol) (*loadedForms, IError) {
|
func (r *Runtime) loadNS(ns *Symbol) (*loadedForms, IError) {
|
||||||
nsFile := nsNameToPath(ns.GetName())
|
nsFile := nsNameToPath(ns.GetName())
|
||||||
for _, loadPath := range r.paths {
|
for _, loadPath := range r.paths {
|
||||||
possibleFile := path.Join(loadPath, nsFile)
|
possibleFile := path.Join(loadPath, nsFile)
|
||||||
|
|
|
@ -138,10 +138,10 @@ func MakeNodeFromLocation(loc *ast.Location) Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeNodeFromExpr creates a new Node from the given `IExpr`.
|
// MakeNodeFromExpr creates a new Node from the given `ILocatable`.
|
||||||
// We use the Node to pass it to other IExpr constructors to
|
// We use the Node to pass it to other IExpr constructors to
|
||||||
// keep the reference to the original form in the input string
|
// keep the reference to the original form in the input string
|
||||||
func MakeNodeFromExpr(e IExpr) Node {
|
func MakeNodeFromExpr(e ast.ILocatable) Node {
|
||||||
return MakeNodeFromLocation(e.GetLocation())
|
return MakeNodeFromLocation(e.GetLocation())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue