diff options
| author | mryouse | 2022-07-07 02:04:28 +0000 |
|---|---|---|
| committer | mryouse | 2022-07-07 02:04:28 +0000 |
| commit | f4622e734ef2be1c3793113e93219d593e3e11fb (patch) | |
| tree | 2c37586c7583d42c3e3ad98b522f23c0003089f9 | |
| parent | 276dd853729a7c71ef4805786793bbc8f651b00d (diff) | |
refactor move all functions with 'lazy' to core
| -rw-r--r-- | neb/std/boolean.py | 25 | ||||
| -rw-r--r-- | neb/std/core.py | 43 | ||||
| -rw-r--r-- | neb/std/functools.py | 10 | ||||
| -rw-r--r-- | neb/std/sys.py | 9 |
4 files changed, 43 insertions, 44 deletions
diff --git a/neb/std/boolean.py b/neb/std/boolean.py index 973fa67..e716a87 100644 --- a/neb/std/boolean.py +++ b/neb/std/boolean.py @@ -3,31 +3,6 @@ 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, Type(":bool"))) - -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, Type(":bool"))) - def interpretEq(symbol, args, env, ns): # NOTE this currently only works for literals # compare types because 0 != #false in neb diff --git a/neb/std/core.py b/neb/std/core.py index 574455d..1645699 100644 --- a/neb/std/core.py +++ b/neb/std/core.py @@ -220,3 +220,46 @@ type_parent_arg = Arg("name", TypeEnum.ANY) type_func_arg = Arg("func", TypeEnum.ANY) CORE.register("type", Builtin("type", interpretType, [type_name_arg, type_parent_arg, type_func_arg])) +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) +CORE.register("or", Builtin("or", interpretOr, [or_arg, or_arg], or_arg, Type(":bool"))) + +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) + +CORE.register("and", Builtin("and", interpretAnd, [or_arg, or_arg], or_arg, Type(":bool"))) + +def interpretApply(symbol, args, env, ns): + # TODO: to support lambdas, we can't assume the func is defined + func = args[0] + if not isinstance(func, Symbol): + raise InterpretPanic(symbol, "requires a symbol as its first argument", func) + new_expr = Expr([func] + args[1].args) + return evaluate(new_expr, env, ns) + +CORE.register("apply", Builtin("apply", interpretApply, [Arg("func", TypeEnum.ANY, lazy=True), Arg("list", TypeEnum.LIST)])) + +def interpretBench(symbol, args, env, ns): + before = datetime.now() + ret = evaluate(args[0], env, ns) + after = datetime.now() + print(f"bench [{symbol.line}]: {args[0]} => {after - before}") + return ret + +CORE.register("bench", Builtin("bench", interpretBench, [Arg("command", TypeEnum.ANY, lazy=True)], return_type=Type(":any"))) diff --git a/neb/std/functools.py b/neb/std/functools.py index 9e426b8..8a76e98 100644 --- a/neb/std/functools.py +++ b/neb/std/functools.py @@ -34,13 +34,3 @@ def interpretMap(symbol, args, env, ns): FUNCTOOLS.register("map", Builtin("map", interpretMap, [Arg("func", TypeEnum.ANY), Arg("list", TypeEnum.LIST)], return_type=Type(":list"))) -def interpretApply(symbol, args, env, ns): - # TODO: to support lambdas, we can't assume the func is defined - func = args[0] - if not isinstance(func, Symbol): - raise InterpretPanic(symbol, "requires a symbol as its first argument", func) - new_expr = Expr([func] + args[1].args) - return evaluate(new_expr, env, ns) - -FUNCTOOLS.register("apply", Builtin("apply", interpretApply, [Arg("func", TypeEnum.ANY, lazy=True), Arg("list", TypeEnum.LIST)])) - diff --git a/neb/std/sys.py b/neb/std/sys.py index 525895a..f92eb77 100644 --- a/neb/std/sys.py +++ b/neb/std/sys.py @@ -35,15 +35,6 @@ def interpretPrint(symbol, args, env, ns): SYS.register("print", Builtin("print", interpretPrint, [Arg("arg", TypeEnum.STRING)], return_type=Type(":list"))) -def interpretBench(symbol, args, env, ns): - before = datetime.now() - ret = evaluate(args[0], env, ns) - after = datetime.now() - print(f"bench [{symbol.line}]: {args[0]} => {after - before}") - return ret - -SYS.register("bench", Builtin("bench", interpretBench, [Arg("command", TypeEnum.ANY, lazy=True)], return_type=Type(":any"))) - def interpretEnv(symbol, args, env, ns): items = os.environ[args[0].value].split(":") return List([String(item) for item in items]) |
