aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang')
-rw-r--r--pkg/lang/compiler/compiler.go54
-rw-r--r--pkg/lang/compiler/errors.go16
2 files changed, 56 insertions, 14 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index 193cd90..b08a215 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -118,6 +118,15 @@ func (comp *Compiler) compileStmt(t *code.Builder, stmt ast.Stmt) error {
panic(fmt.Errorf("unknown statement kind: %d", stmt.Kind))
}
+ // Wrap errors in a compiler error, if it's not already one.
+ if err != nil && !isCompilerError(err) {
+ err = Error{
+ Module: comp.name,
+ Loc: stmt.At,
+ Err: err,
+ }
+ }
+
return err
}
@@ -636,41 +645,53 @@ func (comp *Compiler) compileExprStmt(t *code.Builder, exprStmt ast.StmtExpr) er
}
func (comp *Compiler) compileExpr(t *code.Builder, expr ast.Expr) error {
+ var err error
switch expr.Kind {
case ast.ExprKindBinary:
- return comp.compileBinaryExpr(t, expr.Value.(ast.ExprBinary))
+ err = comp.compileBinaryExpr(t, expr.Value.(ast.ExprBinary))
case ast.ExprKindUnary:
- return comp.compileUnaryExpr(t, expr.Value.(ast.ExprUnary))
+ err = comp.compileUnaryExpr(t, expr.Value.(ast.ExprUnary))
case ast.ExprKindCall:
- return comp.compileCallExpr(t, expr.Value.(ast.ExprCall))
+ err = comp.compileCallExpr(t, expr.Value.(ast.ExprCall))
case ast.ExprKindSubscription:
- return comp.compileSubscriptionExpr(t, expr.Value.(ast.ExprSubscription))
+ err = comp.compileSubscriptionExpr(t, expr.Value.(ast.ExprSubscription))
case ast.ExprKindMember:
- return comp.compileMemberExpr(t, expr.Value.(ast.ExprMember))
+ err = comp.compileMemberExpr(t, expr.Value.(ast.ExprMember))
case ast.ExprKindGroup:
- return comp.compileGroupExpr(t, expr.Value.(ast.ExprGroup))
+ err = comp.compileGroupExpr(t, expr.Value.(ast.ExprGroup))
case ast.ExprKindFnLit:
panic("not implemented")
case ast.ExprKindArrayLit:
- return comp.compileArrayLitExpr(t, expr.Value.(ast.ExprArrayLit))
+ err = comp.compileArrayLitExpr(t, expr.Value.(ast.ExprArrayLit))
case ast.ExprKindIdent:
- return comp.compileIdentExpr(t, expr.Value.(ast.ExprIdent))
+ err = comp.compileIdentExpr(t, expr.Value.(ast.ExprIdent))
case ast.ExprKindIntLit:
- return comp.compileIntLitExpr(t, expr.Value.(ast.ExprIntLit))
+ err = comp.compileIntLitExpr(t, expr.Value.(ast.ExprIntLit))
case ast.ExprKindFloatLit:
- return comp.compileFloatLitExpr(t, expr.Value.(ast.ExprFloatLit))
+ err = comp.compileFloatLitExpr(t, expr.Value.(ast.ExprFloatLit))
case ast.ExprKindStringLit:
- return comp.compileStringLitExpr(t, expr.Value.(ast.ExprStringLit))
+ err = comp.compileStringLitExpr(t, expr.Value.(ast.ExprStringLit))
case ast.ExprKindBoolLit:
- return comp.compileBoolLitExpr(t, expr.Value.(ast.ExprBoolLit))
+ err = comp.compileBoolLitExpr(t, expr.Value.(ast.ExprBoolLit))
case ast.ExprKindNullLit:
- return comp.compileNullLitExpr(t, expr.Value.(ast.ExprNullLit))
+ err = comp.compileNullLitExpr(t, expr.Value.(ast.ExprNullLit))
case ast.ExprKindThis:
- return comp.compileThisExpr(t, expr.Value.(ast.ExprThis))
+ err = comp.compileThisExpr(t, expr.Value.(ast.ExprThis))
default:
panic("unknown expression kind")
}
+
+ // If the error was not a compiler error, we need to wrap it in one.
+ if err != nil && !isCompilerError(err) {
+ err = Error{
+ Module: comp.name,
+ Loc: expr.At,
+ Err: err,
+ }
+ }
+
+ return err
}
func (comp *Compiler) compileBinaryExpr(t *code.Builder, expr ast.ExprBinary) error {
@@ -1027,3 +1048,8 @@ func (comp *Compiler) exitScopeAndCleanStack(t *code.Builder) {
func (comp *Compiler) areWeCompilingCore() bool {
return modules.NewRef(comp.author, comp.name) == modules.CoreModuleRef
}
+
+func isCompilerError(err error) bool {
+ _, ok := err.(Error)
+ return ok
+}
diff --git a/pkg/lang/compiler/errors.go b/pkg/lang/compiler/errors.go
new file mode 100644
index 0000000..45f14cc
--- /dev/null
+++ b/pkg/lang/compiler/errors.go
@@ -0,0 +1,16 @@
+package compiler
+
+import (
+ "fmt"
+ "jinx/pkg/libs/source"
+)
+
+type Error struct {
+ Module string
+ Loc source.Loc
+ Err error
+}
+
+func (e Error) Error() string {
+ return fmt.Sprintf("compiler error at %s:%d:%d: %v", e.Module, e.Loc.Row, e.Loc.Col, e.Err)
+}