assert and test changes
Some checks failed
Build / build (push) Successful in 1m18s
Test / build (push) Failing after 11m19s

This commit is contained in:
Chuck Smith
2024-03-19 08:15:11 -04:00
parent 0b1ed43ae5
commit 60d27f09d7
7 changed files with 164 additions and 142 deletions

View File

@@ -11,15 +11,16 @@ import (
// Builtins ...
var Builtins = map[string]*Builtin{
"len": {Name: "len", Fn: Len},
"input": {Name: "input", Fn: Input},
"print": {Name: "print", Fn: Print},
"first": {Name: "first", Fn: First},
"last": {Name: "last", Fn: Last},
"rest": {Name: "rest", Fn: Rest},
"push": {Name: "push", Fn: Push},
"pop": {Name: "pop", Fn: Pop},
"exit": {Name: "exit", Fn: Exit},
"len": {Name: "len", Fn: Len},
"input": {Name: "input", Fn: Input},
"print": {Name: "print", Fn: Print},
"first": {Name: "first", Fn: First},
"last": {Name: "last", Fn: Last},
"rest": {Name: "rest", Fn: Rest},
"push": {Name: "push", Fn: Push},
"pop": {Name: "pop", Fn: Pop},
"exit": {Name: "exit", Fn: Exit},
"assert": {Name: "assert", Fn: Assert},
}
// BuiltinsIndex ...
@@ -213,3 +214,26 @@ func Exit(args ...Object) Object {
}
return nil
}
// Assert ...
func Assert(args ...Object) Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=2",
len(args))
}
if args[0].Type() != BOOLEAN_OBJ {
return newError("argument #1 to `assert` must be BOOLEAN, got %s",
args[0].Type())
}
if args[1].Type() != STRING_OBJ {
return newError("argument #2 to `assert` must be STRING, got %s",
args[0].Type())
}
if !args[0].(*Boolean).Value {
fmt.Printf("Assertion Error: %s", args[1].(*String).Value)
os.Exit(1)
}
return nil
}