From 13f3bfad0e2eb94b55c5bb424b7528e7744711ab Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Wed, 11 May 2022 23:21:22 -0700 Subject: [PATCH] Tapping towards a slightly functional reader --- projects/shogoth/BUILD | 4 ++ .../shogoth/src/python/shogoth/parser/impl.py | 38 +++++++++++++++---- .../shogoth/src/python/shogoth/reader/impl.py | 7 ++++ .../src/python/shogoth/repl/__main__.py | 2 + .../src/python/shogoth/vm/bootstrap.py | 9 +++-- .../shogoth/src/python/shogoth/vm/impl.py | 6 ++- 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/projects/shogoth/BUILD b/projects/shogoth/BUILD index 7f56c63..355fc15 100644 --- a/projects/shogoth/BUILD +++ b/projects/shogoth/BUILD @@ -1,6 +1,10 @@ py_project( name = "shogoth", main = "src/python/shogoth/repl/__main__.py", + main_deps = [ + py_requirement("prompt_toolkit"), + py_requirement("yaspin"), + ], lib_deps = [ py_requirement("lark"), ], diff --git a/projects/shogoth/src/python/shogoth/parser/impl.py b/projects/shogoth/src/python/shogoth/parser/impl.py index 693105d..a1d60c0 100644 --- a/projects/shogoth/src/python/shogoth/parser/impl.py +++ b/projects/shogoth/src/python/shogoth/parser/impl.py @@ -5,7 +5,7 @@ from typing import Any from lark import Lark -PARSER = Lark('''\ +PARSER = Lark(r''' start: expr expr: plist | blist | mapping | quote | atom @@ -15,23 +15,47 @@ mapping: "{" kv* "}" kv: expr expr quote: "'" expr -atom: KEYWORD | PATTERN | INT | FLOAT | STRING | TRUE | FALSE | NIL | SYMBOL -KEYWORD: ":" SYMBOL +atom: keyword | pattern | num | string | true | false | nil | symbol +num: hex | octal | bin | int | float + +keyword: ":" SYMBOL +symbol: SYMBOL SYMBOL: /[^(){}#'"\s]+/ // FIXME: These two don't deal with escapes correctly at all + +string: STRING STRING: /".*?"/ + +pattern: PATTERN PATTERN: /\/.*?\// -TRUE: /true/ -FALSE: /false/ -NIL: /nil/ +true: /true/ +false: /false/ +nil: /nil/ + +// NOTE: order-prescidence matters here, 0x, 0b, 0o etc. require lookahead + +int: INT INT: /[+-]?[0-9]+/ +hex: HEX +HEX: /0x[0-9a-fA-F_]+/ + +octal: OCTAL +OCTAL: /0o[0-7_]+/ + +bin: BIN +BIN: /0b[01_]+/ + +float: FLOAT + +COMMENT: /;.*?\n/ + %import common.FLOAT // float %ignore " " // Disregard spaces in text %ignore "," // Treat commas as whitespace -%ignore /;.*/ // Disregard comments +%ignore COMMENT // Disregard comments ''', start=["expr"]) diff --git a/projects/shogoth/src/python/shogoth/reader/impl.py b/projects/shogoth/src/python/shogoth/reader/impl.py index 2c118bc..33c1908 100644 --- a/projects/shogoth/src/python/shogoth/reader/impl.py +++ b/projects/shogoth/src/python/shogoth/reader/impl.py @@ -1,5 +1,9 @@ +#!/usr/bin/env python3 + """The shogoth reader.""" +import sys +assert sys.version_info > (3, 10, 0), "`match` support is required" import re from typing import Any @@ -52,6 +56,9 @@ class Reader(object): case Tree(Token("RULE", "atom"), [a]): return self._read(a) + case Tree(Token("RULE", "num"), [a]): + return self._read(a) + case Token("INT", x): return int(x) diff --git a/projects/shogoth/src/python/shogoth/repl/__main__.py b/projects/shogoth/src/python/shogoth/repl/__main__.py index c5e7305..91be4e5 100644 --- a/projects/shogoth/src/python/shogoth/repl/__main__.py +++ b/projects/shogoth/src/python/shogoth/repl/__main__.py @@ -3,6 +3,8 @@ from shogoth.reader import Reader +import prompt_toolkit +import yaspin if __name__ == "__main__": reader = Reader() diff --git a/projects/shogoth/src/python/shogoth/vm/bootstrap.py b/projects/shogoth/src/python/shogoth/vm/bootstrap.py index 563e3a1..2121af8 100644 --- a/projects/shogoth/src/python/shogoth/vm/bootstrap.py +++ b/projects/shogoth/src/python/shogoth/vm/bootstrap.py @@ -71,13 +71,16 @@ XOR = BOOTSTRAP.define_function( ) TRUE = BOOTSTRAP.define_type( - "true", ProductExpr([]), + "/lang/shogoth/v0/true", + ProductExpr([]), ) FALSE = BOOTSTRAP.define_type( - "false", ProductExpr([]), + "/lang/shogoth/v0/false", + ProductExpr([]), ) BOOL = BOOTSTRAP.define_type( - "bool", SumExpr([TRUE, FALSE]) + "/lang/shogoth/v0/bool", + SumExpr([TRUE, FALSE]) ) diff --git a/projects/shogoth/src/python/shogoth/vm/impl.py b/projects/shogoth/src/python/shogoth/vm/impl.py index ceab3ca..af5a895 100644 --- a/projects/shogoth/src/python/shogoth/vm/impl.py +++ b/projects/shogoth/src/python/shogoth/vm/impl.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3.10 +#!/usr/bin/env python3 """The Shogoth VM implementation. @@ -16,6 +16,10 @@ context (a virtual machine) which DOES have an easily introspected and serialize """ + +import sys +assert sys.version_info > (3, 10, 0), "`match` support is required" + from copy import deepcopy from .isa import FunctionRef, Opcode