Skip to content

Commit

Permalink
Correct positioning on errors, few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jan 14, 2021
1 parent 1ae2f1e commit 29eb63e
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ There are several example programs available on [the wiki](https://github.com/Vi
## Roadmap

* Full variable block scoping
* Variable argument count functions
* Multiple function return values

## Planned Optimizations
Expand Down
12 changes: 11 additions & 1 deletion tests/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func sample1(arg hello.world) int {
{
name: "CallToUnknownFunction",
input: TestMain(`foo()`),
output: `error at 89: unknown function: foo`,
output: `error at 103: unknown function: foo`,
},
{
name: "InvalidConstant",
Expand Down Expand Up @@ -141,6 +141,16 @@ func sample() (int, int) {
input: TestMain(``),
output: `empty main function`,
},
{
name: "ErrorIncorrectVarCount",
input: TestMain(`a, b := m.Read("bank1", 0)`),
output: `error at 111: function requires 1 variables, provided: 2`,
},
{
name: "ErrorMismatchedSides",
input: TestMain(`a, b, c := 1, 2`),
output: `error at 103: mismatched variable assignment sides`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions tests/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ print "A"
print _main_x
print "\n"`,
},
{
name: "float64",
input: TestMain(`x := float64(1)`),
output: `set _main_x 1`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions tests/stacked_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ read @counter bank1 @stack
op add @stack @stack 1
write 8 bank1 @stack
jump 2 always
op sub @stack @stack 1`,
},
{
name: "IgnoreEmpty",
input: `package main
func main() {
hello()
}
func hello() {
println("hello")
}
func foo() {
}`,
output: `set @stack 0
jump 5 always
print "hello"
print "\n"
read @counter bank1 @stack
op add @stack @stack 1
write 8 bank1 @stack
jump 2 always
op sub @stack @stack 1`,
},
}
Expand Down
21 changes: 21 additions & 0 deletions tests/stackless_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ print "hello"
print "\n"
set @counter @funcTramp_hello
set @funcTramp_hello 6
jump 1 always`,
},
{
name: "IgnoreEmpty",
input: `package main
func main() {
hello()
}
func hello() {
println("hello")
}
func foo() {
}`,
output: `jump 4 always
print "hello"
print "\n"
set @counter @funcTramp_hello
set @funcTramp_hello 6
jump 1 always`,
},
}
Expand Down
10 changes: 10 additions & 0 deletions tests/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ print "default"
print "\n"
jump 30 always`,
},
{
name: "IgnoredVariable",
input: TestMain(`_ := false`),
output: `set @_ false`,
},
{
name: "OperatorAssign",
input: TestMain(`x += 1`),
output: `op add _main_x _main_x 1`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions transpiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"errors"
"fmt"
"go/ast"
"go/token"
)

type ContextualError struct {
error
Context context.Context
Pos *token.Pos
}

func (e ContextualError) Error() string {
if e.Pos != nil {
return fmt.Sprintf("error at %d: %s", *e.Pos, e.error.Error())
}

if e.Context != nil {
if stmt, ok := e.Context.Value(contextStatement).(ast.Stmt); ok {
return fmt.Sprintf("error at %d: %s", stmt.Pos(), e.error.Error())
Expand All @@ -33,3 +39,11 @@ func Err(ctx context.Context, err string) ContextualError {
Context: ctx,
}
}

func ErrPos(ctx context.Context, pos token.Pos, err string) ContextualError {
return ContextualError{
error: errors.New(err),
Context: ctx,
Pos: &pos,
}
}
2 changes: 2 additions & 0 deletions transpiler/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ func callExprToMLOG(ctx context.Context, callExpr *ast.CallExpr, ident []Resolva
Function: translatedFunc,
Arguments: args,
Variables: ident,
SourcePos: callExpr.Pos(),
})
} else {
results = append(results, &MLOGCustomFunction{
Arguments: callExpr.Args,
Variables: ident,
FunctionName: funcName,
SourcePos: callExpr.Pos(),
})
}

Expand Down
1 change: 1 addition & 0 deletions transpiler/type_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (m *MLOGCustomFunction) PreProcess(ctx context.Context, global *Global, fun
},
JumpTarget: &FunctionJumpTarget{
FunctionName: m.FunctionName,
SourcePos: m.SourcePos,
},
})

Expand Down
4 changes: 3 additions & 1 deletion transpiler/type_jump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transpiler

import (
"context"
"go/token"
"strconv"
)

Expand Down Expand Up @@ -52,6 +53,7 @@ func (m *MLOGJump) GetComment(int) string {
type FunctionJumpTarget struct {
Statement WithPosition
FunctionName string
SourcePos token.Pos
}

func (m *FunctionJumpTarget) GetPosition() int {
Expand All @@ -70,7 +72,7 @@ func (m *FunctionJumpTarget) PreProcess(ctx context.Context, global *Global, _ *
return nil
}
}
return Err(ctx, "unknown function: "+m.FunctionName)
return ErrPos(ctx, m.SourcePos, "unknown function: "+m.FunctionName)
}

func (m *FunctionJumpTarget) PostProcess(context.Context, *Global, *Function) error {
Expand Down
2 changes: 1 addition & 1 deletion transpiler/type_mlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (m *MLOGFunc) SetPosition(position int) int {

func (m *MLOGFunc) PreProcess(ctx context.Context, global *Global, function *Function) error {
if len(m.Variables) != m.Function.Variables {
return Err(ctx, fmt.Sprintf("function requires %d variables, provided: %d", m.Function.Variables, len(m.Variables)))
return ErrPos(ctx, m.SourcePos, fmt.Sprintf("function requires %d variables, provided: %d", m.Function.Variables, len(m.Variables)))
}

for _, argument := range m.Arguments {
Expand Down

0 comments on commit 29eb63e

Please sign in to comment.