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(
name = "shogoth",
main = "src/python/shogoth/repl/__main__.py",
main_deps = [
py_requirement("prompt_toolkit"),
py_requirement("yaspin"),
],
lib_deps = [
py_requirement("lark"),
],

View file

@ -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"])

View file

@ -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)

View file

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

View file

@ -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])
)

View file

@ -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