Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make errors more readable #89

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions errd/sintaksia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errd

import (
"fmt"
"os"

"github.com/NuruProgramming/Nuru/token"
)

type MakosaSintaksia struct {
Ujumbe string
Muktadha string
Info token.Token
}

func (s *MakosaSintaksia) Kosa() string {
return fmt.Sprintf("Kosa la kisintaksia: %s:%d:%d\n%s\n\n%s",
s.Info.Filename, s.Info.Line.Start.Line, s.Info.Line.Start.Column, s.Ujumbe, s.Muktadha)
}

func (s *MakosaSintaksia) Onyesha() {
fmt.Fprintf(os.Stderr, "%s\n", s.Kosa())
}

func (s *MakosaSintaksia) Hatari() {
s.Onyesha()
os.Exit(1)
}
2 changes: 1 addition & 1 deletion evaluator/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func evalCall(node *ast.CallExpression, env *object.Environment) object.Object {
return args[0]
}

return applyFunction(function, args, node.Token.Line)
return applyFunction(function, args, node.Token.Line.Start.Line)
}

func evalArgsExpressions(node *ast.CallExpression, fn *object.Function, env *object.Environment) []object.Object {
Expand Down
12 changes: 6 additions & 6 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
if isError(right) {
return right
}
return evalPrefixExpression(node.Operator, right, node.Token.Line)
return evalPrefixExpression(node.Operator, right, node.Token.Line.Start.Line)

case *ast.InfixExpression:
left := Eval(node.Left, env)
Expand All @@ -48,7 +48,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
if isError(right) && right != nil {
return right
}
return evalInfixExpression(node.Operator, left, right, node.Token.Line)
return evalInfixExpression(node.Operator, left, right, node.Token.Line.Start.Line)
case *ast.PostfixExpression:
return evalPostfixExpression(env, node.Operator, node)

Expand Down Expand Up @@ -107,7 +107,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
if isError(index) {
return index
}
return evalIndexExpression(left, index, node.Token.Line)
return evalIndexExpression(left, index, node.Token.Line.Start.Line)
case *ast.DictLiteral:
return evalDictLiteral(node, env)
case *ast.WhileExpression:
Expand All @@ -123,7 +123,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
// case *ast.For:
// return evalForExpression(node, env)
case *ast.ForIn:
return evalForInExpression(node, env, node.Token.Line)
return evalForInExpression(node, env, node.Token.Line.Start.Line)
case *ast.Package:
return evalPackage(node, env)
case *ast.PropertyExpression:
Expand Down Expand Up @@ -155,7 +155,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
op := node.Token.Literal
if len(op) >= 2 {
op = op[:len(op)-1]
value = evalInfixExpression(op, left, value, node.Token.Line)
value = evalInfixExpression(op, left, value, node.Token.Line.Start.Line)
if isError(value) {
return value
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func applyFunction(fn object.Object, args []object.Object, line int) object.Obje
return newError("Hamna andaa kiendesha")
}
node.(*object.Function).Env.Set("@", obj)
applyFunction(node, args, fn.Name.Token.Line)
applyFunction(node, args, fn.Name.Token.Line.Start.Line)
node.(*object.Function).Env.Del("@")
return obj
default:
Expand Down
9 changes: 3 additions & 6 deletions evaluator/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/NuruProgramming/Nuru/ast"
"github.com/NuruProgramming/Nuru/lexer"
Expand Down Expand Up @@ -65,12 +64,10 @@ func evaluateFile(file string, env *object.Environment) (*object.Environment, ob
if err != nil {
return nil, &object.Error{Message: fmt.Sprintf("Tumeshindwa kufungua pakeji: %s", file)}
}
l := lexer.New(string(source))
p := parser.New(l)
l := lexer.New(file, string(source))
p := parser.New(string(source), l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
return nil, &object.Error{Message: fmt.Sprintf("Pakeji %s ina makosa yafuatayo:\n%s", file, strings.Join(p.Errors(), "\n"))}
}
// There was a nil return here (and errors), this change may cause nil dereference somewhere

scope := object.NewEnvironment()
result := Eval(program, scope)
Expand Down
2 changes: 1 addition & 1 deletion evaluator/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func evalMethodExpression(node *ast.MethodExpression, env *object.Environment) o
for k, v := range node.Defaults {
defs[k] = Eval(v, env)
}
return applyMethod(obj, node.Method, args, defs, node.Token.Line)
return applyMethod(obj, node.Method, args, defs, node.Token.Line.Start.Line)
}

func applyMethod(obj object.Object, method ast.Expression, args []object.Object, defs map[string]object.Object, l int) object.Object {
Expand Down
Loading