Tapping towards a slightly functional reader

This commit is contained in:
Reid 'arrdem' McKenzie 2022-05-11 23:21:22 -07:00
parent dadd039a84
commit 13f3bfad0e
6 changed files with 55 additions and 11 deletions

View file

@ -1,6 +1,10 @@
py_project( py_project(
name = "shogoth", name = "shogoth",
main = "src/python/shogoth/repl/__main__.py", main = "src/python/shogoth/repl/__main__.py",
main_deps = [
py_requirement("prompt_toolkit"),
py_requirement("yaspin"),
],
lib_deps = [ lib_deps = [
py_requirement("lark"), py_requirement("lark"),
], ],

View file

@ -5,7 +5,7 @@ from typing import Any
from lark import Lark from lark import Lark
PARSER = Lark('''\ PARSER = Lark(r'''
start: expr start: expr
expr: plist | blist | mapping | quote | atom expr: plist | blist | mapping | quote | atom
@ -15,23 +15,47 @@ mapping: "{" kv* "}"
kv: expr expr kv: expr expr
quote: "'" expr quote: "'" expr
atom: KEYWORD | PATTERN | INT | FLOAT | STRING | TRUE | FALSE | NIL | SYMBOL atom: keyword | pattern | num | string | true | false | nil | symbol
KEYWORD: ":" SYMBOL num: hex | octal | bin | int | float
keyword: ":" SYMBOL
symbol: SYMBOL
SYMBOL: /[^(){}#'"\s]+/ SYMBOL: /[^(){}#'"\s]+/
// FIXME: These two don't deal with escapes correctly at all // FIXME: These two don't deal with escapes correctly at all
string: STRING
STRING: /".*?"/ STRING: /".*?"/
pattern: PATTERN
PATTERN: /\/.*?\// PATTERN: /\/.*?\//
TRUE: /true/ true: /true/
FALSE: /false/ false: /false/
NIL: /nil/ nil: /nil/
// NOTE: order-prescidence matters here, 0x, 0b, 0o etc. require lookahead
int: INT
INT: /[+-]?[0-9]+/ 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 %import common.FLOAT // float
%ignore " " // Disregard spaces in text %ignore " " // Disregard spaces in text
%ignore "," // Treat commas as whitespace %ignore "," // Treat commas as whitespace
%ignore /;.*/ // Disregard comments %ignore COMMENT // Disregard comments
''', start=["expr"]) ''', start=["expr"])

View file

@ -1,5 +1,9 @@
#!/usr/bin/env python3
"""The shogoth reader.""" """The shogoth reader."""
import sys
assert sys.version_info > (3, 10, 0), "`match` support is required"
import re import re
from typing import Any from typing import Any
@ -52,6 +56,9 @@ class Reader(object):
case Tree(Token("RULE", "atom"), [a]): case Tree(Token("RULE", "atom"), [a]):
return self._read(a) return self._read(a)
case Tree(Token("RULE", "num"), [a]):
return self._read(a)
case Token("INT", x): case Token("INT", x):
return int(x) return int(x)

View file

@ -3,6 +3,8 @@
from shogoth.reader import Reader from shogoth.reader import Reader
import prompt_toolkit
import yaspin
if __name__ == "__main__": if __name__ == "__main__":
reader = Reader() reader = Reader()

View file

@ -71,13 +71,16 @@ XOR = BOOTSTRAP.define_function(
) )
TRUE = BOOTSTRAP.define_type( TRUE = BOOTSTRAP.define_type(
"true", ProductExpr([]), "/lang/shogoth/v0/true",
ProductExpr([]),
) )
FALSE = BOOTSTRAP.define_type( FALSE = BOOTSTRAP.define_type(
"false", ProductExpr([]), "/lang/shogoth/v0/false",
ProductExpr([]),
) )
BOOL = BOOTSTRAP.define_type( BOOL = BOOTSTRAP.define_type(
"bool", SumExpr([TRUE, FALSE]) "/lang/shogoth/v0/bool",
SumExpr([TRUE, FALSE])
) )

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python3.10 #!/usr/bin/env python3
"""The Shogoth VM implementation. """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 copy import deepcopy
from .isa import FunctionRef, Opcode from .isa import FunctionRef, Opcode