diff options
| author | mryouse | 2022-06-21 01:49:29 +0000 |
|---|---|---|
| committer | mryouse | 2022-06-21 01:49:29 +0000 |
| commit | 776fe3193b515c028b5ac69326baed51d760d32f (patch) | |
| tree | db111c3fe7a20143b2f058259f86dbbecae4cbd6 /neb/std/boolean.py | |
| parent | b1550660adaca68bb38541aed371e36b7000e124 (diff) | |
refactor: break stdlib into several files
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])) |
