add general assert builtin

This commit is contained in:
Neemek 2026-07-11 10:52:32 +02:00
parent e2a1e5ff8d
commit ddf92e1009
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
2 changed files with 23 additions and 0 deletions

View file

@ -470,6 +470,26 @@ var DefaultGlobals = map[string]Value{
nil,
true,
},
"assert": &BuiltinFunctionValue{
"assert",
&FunctionSignature{
[]TypeSignature{
&BooleanSignature{},
},
&NilSignature{},
},
func(vm *VM, this Value, params []Value) (Value, error) {
b := params[0].(*BoolValue)
if !b.Boolean {
return nil, errors.New(fmt.Sprintf("assertion failed: %s", b))
}
return &NilValue{}, nil
},
nil,
false,
},
"assertEq": &BuiltinFunctionValue{
"assertEq",
&FunctionSignature{

View file

@ -8,3 +8,6 @@ assertEq([3, 1, 4, 1], [3, 1, 4, 1])
# Inequality
assertNotEq(2, 3)
assert(1 < 2)
assert(1.0 < 2.0)