from .. import TypeEnum, Environment, Arg, Builtin, evaluate, InterpretPanic 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("or", 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("and", 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("eq?", 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("not", interpretNot, [not_arg]))