diff options
| author | mryouse | 2022-05-24 03:38:51 +0000 |
|---|---|---|
| committer | mryouse | 2022-05-24 03:38:51 +0000 |
| commit | 389b8e77f90f3b9586f38366aef9a7c955c4c4c3 (patch) | |
| tree | 566ce99f98cfba93a87e06e79b1bf7439e38a969 | |
| parent | fe62c5ef79cdfc470351e3a9994bc6b00f40bd76 (diff) | |
implement glob, shell, empty
| -rw-r--r-- | interpreter.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/interpreter.py b/interpreter.py index 2721a63..778c48e 100644 --- a/interpreter.py +++ b/interpreter.py @@ -1,5 +1,8 @@ from structs import Literal, Symbol, List from pathlib import Path +from glob import glob +import subprocess +import shlex class Function: @@ -545,3 +548,33 @@ def interpretApply(symbol, args, env): return evaluate(new_lst, env) GLOBALS.register("apply", Builtin(interpretApply, 2)) + +def interpretGlob(symbol, args, env): + ev = evaluate(args[0], env) + if not isinstance(ev, Literal): + raise Exception("'glob' expects a string") + items = glob(ev.value) + out = [] + for item in items: + out.append(Literal(item)) + return List(out, True) + +GLOBALS.register("glob", Builtin(interpretGlob, 1)) + +def interpretShell(symbol, args, env): + ev = evaluate(args[0], env) + if not isinstance(ev, Literal): + raise Exception("'$' expects a string") + # TODO either fail or throw exception (?) on error + ret = subprocess.run(shlex.split(ev.value), capture_output=True) + return List([Literal(r) for r in ret.stdout.decode("utf-8").split("\n")], True) + +GLOBALS.register("$", Builtin(interpretShell, 1)) + +def interpretEmpty(symbol, args, env): + ev = evaluate(args[0], env) + if not isinstance(ev, List): + raise Exception("'empty?' expects a List") + return Literal(len(ev.args) == 0) + +GLOBALS.register("empty?", Builtin(interpretEmpty, 1)) |
