diff options
Diffstat (limited to 'neb/std/boolean.py')
| -rw-r--r-- | neb/std/boolean.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/neb/std/boolean.py b/neb/std/boolean.py new file mode 100644 index 0000000..b851d7d --- /dev/null +++ b/neb/std/boolean.py @@ -0,0 +1,47 @@ +from .. import TypeEnum, Environment, Arg, Builtin, evaluate +from ..structs import * + +BOOLEAN = Environment() + +def interpretOr(symbol, args, env, ns): + # or returns true for the first expression that returns true + for arg in args: + ev = evaluate(arg, env, ns) + if not isinstance(ev, Bool): + raise InterpretPanic(symbol, "requires :bool arguments") + if ev.value == True: + return ev + return Bool(False) + +or_arg = Arg("arg", TypeEnum.BOOL, lazy=True) +BOOLEAN.register("or", Builtin(interpretOr, [or_arg, or_arg], or_arg)) + +def interpretAnd(symbol, args, env, ns): + # and returns false for the first expression that returns false + for arg in args: + ev = evaluate(arg, env, ns) + if not isinstance(ev, Bool): + raise InterpretPanic(symbol, "requires :bool arguments") + if ev.value == False: + return ev + return Bool(True) + +BOOLEAN.register("and", Builtin(interpretAnd, [or_arg, or_arg], or_arg)) + +def interpretEq(symbol, args, env, ns): + # NOTE this currently only works for literals + # compare types because 0 != #false in neb + if type(args[0]) == type(args[1]) and args[0].value == args[1].value: + return Bool(True) + else: + return Bool(False) + +eq_arg = Arg("value", TypeEnum.LITERAL) +BOOLEAN.register("eq?", Builtin(interpretEq, [eq_arg, eq_arg])) + + +def interpretNot(symbol, args, env, ns): + return Bool(not args[0].value) + +not_arg = Arg("not", TypeEnum.BOOL) +BOOLEAN.register("not", Builtin(interpretNot, [not_arg])) |
