Extra Features
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"monkey/lexer"
|
||||
"monkey/object"
|
||||
"monkey/parser"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -597,7 +599,7 @@ func TestBuiltinFunctions(t *testing.T) {
|
||||
},
|
||||
{`len([1, 2, 3])`, 3},
|
||||
{`len([])`, 0},
|
||||
{`puts("hello", "world!")`, Null},
|
||||
{`print("hello", "world!")`, Null},
|
||||
{`first([1, 2, 3])`, 1},
|
||||
{`first([])`, Null},
|
||||
{`first(1)`,
|
||||
@@ -620,6 +622,12 @@ func TestBuiltinFunctions(t *testing.T) {
|
||||
Message: "argument to `push` must be ARRAY, got INTEGER",
|
||||
},
|
||||
},
|
||||
{`input()`, ""},
|
||||
{`pop([])`, &object.Error{
|
||||
Message: "cannot pop from an empty array",
|
||||
},
|
||||
},
|
||||
{`pop([1])`, 1},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
@@ -779,3 +787,49 @@ func TestRecursiveFibonacci(t *testing.T) {
|
||||
|
||||
runVmTests(t, tests)
|
||||
}
|
||||
|
||||
func TestIterations(t *testing.T) {
|
||||
tests := []vmTestCase{
|
||||
{"while (false) { }", nil},
|
||||
{"let n = 0; while (n < 10) { let n = n + 1 }; n", 10},
|
||||
{"let n = 10; while (n > 0) { let n = n - 1 }; n", 0},
|
||||
// FIXME: let is an expression statement and bind new values
|
||||
// there is currently no assignment expressions :/
|
||||
{"let n = 0; while (n < 10) { let n = n + 1 }", nil},
|
||||
{"let n = 10; while (n > 0) { let n = n - 1 }", nil},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
}
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
matches, err := filepath.Glob("../examples/*.monkey")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
b, err := os.ReadFile(match)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
input := string(b)
|
||||
program := parse(input)
|
||||
|
||||
c := compiler.New()
|
||||
err = c.Compile(program)
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("compiler error: %s", err)
|
||||
}
|
||||
|
||||
vm := New(c.Bytecode())
|
||||
|
||||
err = vm.Run()
|
||||
if err != nil {
|
||||
t.Log(input)
|
||||
t.Fatalf("vm error: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user