Datasets:
Upload mantra.py
Browse files
mantra.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# mantra.py — MANTRA CLI
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
sys.path.insert(0, os.path.dirname(__file__))
|
| 7 |
+
|
| 8 |
+
from runtime.runtime import run_file, run_source
|
| 9 |
+
from sha256.sha256_impl import sha256_hex
|
| 10 |
+
|
| 11 |
+
def cmd_run(path):
|
| 12 |
+
result = run_file(path)
|
| 13 |
+
if result is not None:
|
| 14 |
+
from runtime.evaluator import Evaluator
|
| 15 |
+
ev = Evaluator()
|
| 16 |
+
print(ev._display(result))
|
| 17 |
+
|
| 18 |
+
def cmd_hash(data: str):
|
| 19 |
+
print(sha256_hex(data))
|
| 20 |
+
|
| 21 |
+
def cmd_eval(expr: str):
|
| 22 |
+
result = run_source(expr)
|
| 23 |
+
if result is not None:
|
| 24 |
+
from runtime.evaluator import Evaluator
|
| 25 |
+
ev = Evaluator()
|
| 26 |
+
print(ev._display(result))
|
| 27 |
+
|
| 28 |
+
def cmd_test():
|
| 29 |
+
import subprocess
|
| 30 |
+
sys.exit(subprocess.call([sys.executable, '-m', 'pytest', 'tests/', '-v']))
|
| 31 |
+
|
| 32 |
+
def usage():
|
| 33 |
+
print("MANTRA language interpreter")
|
| 34 |
+
print()
|
| 35 |
+
print("Usage:")
|
| 36 |
+
print(" mantra run <file.m> run a MANTRA source file")
|
| 37 |
+
print(" mantra eval <expr> evaluate an expression")
|
| 38 |
+
print(" mantra hash <string> SHA-256 hex of a string")
|
| 39 |
+
print(" mantra test run test suite")
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
if len(sys.argv) < 2:
|
| 43 |
+
usage()
|
| 44 |
+
return
|
| 45 |
+
cmd = sys.argv[1]
|
| 46 |
+
if cmd == 'run' and len(sys.argv) >= 3: cmd_run(sys.argv[2])
|
| 47 |
+
elif cmd == 'eval' and len(sys.argv) >= 3: cmd_eval(sys.argv[2])
|
| 48 |
+
elif cmd == 'hash' and len(sys.argv) >= 3: cmd_hash(sys.argv[2])
|
| 49 |
+
elif cmd == 'test': cmd_test()
|
| 50 |
+
else: usage()
|
| 51 |
+
|
| 52 |
+
if __name__ == '__main__':
|
| 53 |
+
main()
|