diff --git a/bootstrap/pkg/core/bool.go b/bootstrap/pkg/core/bool.go index bf1b196..31dc683 100644 --- a/bootstrap/pkg/core/bool.go +++ b/bootstrap/pkg/core/bool.go @@ -35,9 +35,9 @@ func (t *Bool) GetType() ast.NodeType { func (t *Bool) String() string { if t.value { - return "true" + return TRUEFORM } - return "false" + return FALSEFORM } func (t *Bool) ToDebugStr() string { diff --git a/bootstrap/pkg/core/constants.go b/bootstrap/pkg/core/constants.go new file mode 100644 index 0000000..a15f5b5 --- /dev/null +++ b/bootstrap/pkg/core/constants.go @@ -0,0 +1,24 @@ +/* + Serene --- Yet an other Lisp + +Copyright (c) 2020 Sameer Rahmani + +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 . +*/ + +package core + +const NSFORM string = "ns" +const NILFORM string = "nil" +const FALSEFORM string = "false" +const TRUEFORM string = "true" diff --git a/bootstrap/pkg/core/eval.go b/bootstrap/pkg/core/eval.go index 09c520f..32859d1 100644 --- a/bootstrap/pkg/core/eval.go +++ b/bootstrap/pkg/core/eval.go @@ -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 of a list it will evaluate all the elements and return the // 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() { case ast.Nil: return form, nil @@ -73,11 +73,11 @@ func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) { symbolName := sym.GetName() switch symbolName { - case "true": + case TRUEFORM: return MakeTrue(MakeNodeFromExpr(form)), nil - case "false": + case FALSEFORM: return MakeFalse(MakeNodeFromExpr(form)), nil - case "nil": + case NILFORM: return MakeNil(MakeNodeFromExpr(form)), nil default: 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) // 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. // 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. @@ -246,7 +246,7 @@ tco: // Empty list evaluates to itself if list.Count() == 0 { ret = list - break tco // return &Nil, nil + break tco // return &Nil, nil here } rawFirst := list.First() @@ -273,7 +273,8 @@ tco: // name of the namespace. ( We won't evaluate the first // element ) // 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) if err != nil { return nil, err @@ -598,8 +599,9 @@ tco: MakeStackPop(rt), ) changeExecutionScope(body, fnScope) - exprs = append(body, restOfExprs(exprs, i)...) - goto body // rewrite + body = append(body, restOfExprs(exprs, i)...) + exprs = body // Just because of the stupid linters + goto body // rewrite // If the function was a native function which is represented // by the `NativeFunction` struct @@ -674,7 +676,7 @@ func EvalNSBody(rt *Runtime, ns *Namespace) (*Namespace, IError) { if exprs[0].GetType() == ast.List { 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) if err != nil { return nil, err diff --git a/bootstrap/pkg/core/macro.go b/bootstrap/pkg/core/macro.go index 5c44cc5..5b2389b 100644 --- a/bootstrap/pkg/core/macro.go +++ b/bootstrap/pkg/core/macro.go @@ -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. // If there is a value associated with the symbol in the scope, it will be checked // 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 { list := form.(*List) 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 // 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. -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) if e != nil { @@ -90,7 +90,6 @@ func macroexpand(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) { var macro *Function var e IError ok := false - //form := expr for { macro, ok = isMacroCall(rt, scope, form) diff --git a/bootstrap/pkg/core/namespace.go b/bootstrap/pkg/core/namespace.go index c85703b..c297ccc 100644 --- a/bootstrap/pkg/core/namespace.go +++ b/bootstrap/pkg/core/namespace.go @@ -143,7 +143,7 @@ func (n *Namespace) getForms() *Block { // `ns` string. func requireNS(rt *Runtime, ns *Symbol) (*Namespace, IError) { // TODO: use a hashing algorithm to avoid reloading an unchanged namespace - loadedForms, err := rt.LoadNS(ns) + loadedForms, err := rt.loadNS(ns) if err != nil { return nil, err diff --git a/bootstrap/pkg/core/nil.go b/bootstrap/pkg/core/nil.go index 9956ebd..0d12e5c 100644 --- a/bootstrap/pkg/core/nil.go +++ b/bootstrap/pkg/core/nil.go @@ -30,11 +30,11 @@ func (n *Nil) GetType() ast.NodeType { } func (n *Nil) String() string { - return "nil" + return NILFORM } func (n *Nil) ToDebugStr() string { - return "nil" + return NILFORM } func (n *Nil) Hash() uint32 { diff --git a/bootstrap/pkg/core/parser.go b/bootstrap/pkg/core/parser.go index 248bd54..5d92a4c 100644 --- a/bootstrap/pkg/core/parser.go +++ b/bootstrap/pkg/core/parser.go @@ -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 func makeErrorFromError(parser IParsable, e error) IError { + //nolint:govet return makeErrorAtPoint(parser, "%w", e) } @@ -424,6 +425,7 @@ func readList(parser IParsable) (IExpr, IError) { return MakeList(*node, list), nil } +//nolint:unparam func readComment(parser IParsable) (IExpr, IError) { for { c := parser.next(false) diff --git a/bootstrap/pkg/core/printer.go b/bootstrap/pkg/core/printer.go index d0a1c75..bfa4cc9 100644 --- a/bootstrap/pkg/core/printer.go +++ b/bootstrap/pkg/core/printer.go @@ -27,20 +27,20 @@ import ( "serene-lang.org/bootstrap/pkg/errors" ) -func toRepresanbleString(ast ...IRepresentable) string { +func toRepresanbleString(forms ...IRepresentable) string { var results []string - for _, x := range ast { + for _, x := range forms { results = append(results, x.String()) } return strings.Join(results, " ") } -func toPrintableString(ast ...IRepresentable) string { +func toPrintableString(forms ...IRepresentable) string { var results []string - for _, x := range ast { + for _, x := range forms { if printable, ok := x.(IPrintable); ok { results = append(results, printable.PrintToString()) continue @@ -51,30 +51,30 @@ func toPrintableString(ast ...IRepresentable) string { return strings.Join(results, " ") } -func Pr(rt *Runtime, ast ...IRepresentable) { - fmt.Print(toRepresanbleString(ast...)) +func Pr(rt *Runtime, forms ...IRepresentable) { + fmt.Print(toRepresanbleString(forms...)) } -func Prn(rt *Runtime, ast ...IRepresentable) { - fmt.Println(toRepresanbleString(ast...)) +func Prn(rt *Runtime, forms ...IRepresentable) { + fmt.Println(toRepresanbleString(forms...)) } -func Print(rt *Runtime, ast ...IRepresentable) { - fmt.Print(toPrintableString(ast...)) +func Print(rt *Runtime, forms ...IRepresentable) { + fmt.Print(toPrintableString(forms...)) } -func Println(rt *Runtime, ast ...IRepresentable) { - fmt.Println(toPrintableString(ast...)) +func Println(rt *Runtime, forms ...IRepresentable) { + fmt.Println(toPrintableString(forms...)) } -func printError(rt *Runtime, err IError, stage int) { +func printError(_ *Runtime, err IError, stage int) { loc := err.GetLocation() source := loc.GetSource() startline := source.LineNumberFor(loc.GetStart()) if startline > 0 { - startline -= 1 + startline-- } endline := source.LineNumberFor(loc.GetEnd()) + 1 @@ -138,7 +138,7 @@ func frameSource(traces *TraceBack, frameIndex int) string { startline := callerSource.LineNumberFor(callerLoc.GetStart()) if startline > 0 { - startline -= 1 + startline-- } endline := callerSource.LineNumberFor(callerLoc.GetEnd()) + 1 @@ -160,7 +160,7 @@ func frameSource(traces *TraceBack, frameIndex int) string { return lines } -func printErrorWithTraceBack(rt *Runtime, err IError) { +func printErrorWithTraceBack(_ *Runtime, err IError) { trace := err.GetStackTrace() for i := range *trace { diff --git a/bootstrap/pkg/core/runtime.go b/bootstrap/pkg/core/runtime.go index 8333b57..945eefa 100644 --- a/bootstrap/pkg/core/runtime.go +++ b/bootstrap/pkg/core/runtime.go @@ -147,7 +147,7 @@ func nsNameToPath(ns string) string { // LoadNS looks up the namespace specified by the given name `ns` // and reads the content as expressions (parse it) and returns the // expressions. -func (r *Runtime) LoadNS(ns *Symbol) (*loadedForms, IError) { +func (r *Runtime) loadNS(ns *Symbol) (*loadedForms, IError) { nsFile := nsNameToPath(ns.GetName()) for _, loadPath := range r.paths { possibleFile := path.Join(loadPath, nsFile) diff --git a/bootstrap/pkg/core/types.go b/bootstrap/pkg/core/types.go index 4288c9d..cbf8f39 100644 --- a/bootstrap/pkg/core/types.go +++ b/bootstrap/pkg/core/types.go @@ -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 // 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()) }