-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe4b43f
commit e2ea86e
Showing
7 changed files
with
88 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- /dist | ||
- /build | ||
- /venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def atom(x): | ||
return 'atom', x | ||
|
||
def list_(x): | ||
return 'list', x | ||
|
||
def bool_(x): | ||
return 'bool', x | ||
|
||
def int_(x): | ||
return 'int', x | ||
|
||
def float_(x): | ||
return 'float', x | ||
|
||
def typ(x): | ||
return x[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import lark | ||
from smallscheme.dtypes import * | ||
|
||
grammar = ''' | ||
start : _exprs | ||
_exprs : _e* _e | ||
_e : ATOM | ||
| _num | ||
| BOOL | ||
| list | ||
TRUE : "#t" | ||
FALSE : "#f" | ||
BOOL : TRUE | FALSE | ||
list : "(" _exprs? ")" | ||
INT : /[-+]?[0-9]+/ | ||
ATOM : /[a-zA-Z]+[a-zA-Z0-9\-\?\!]*/ | ||
| /[\*\/\=\>\<]/ | ||
| /[\-\+](?![0-9])/ | ||
FLOAT : /[-+]?[0-9]+\.[0-9]*/ | ||
_num : INT | FLOAT | ||
COMMENT : ";" /(.)*/ NEWLINE? | ||
%import common.WS | ||
%import common.NEWLINE | ||
%ignore WS | ||
%ignore COMMENT | ||
''' | ||
|
||
parser = lark.Lark(grammar) | ||
|
||
def convert_ast(ast): | ||
""" | ||
Re-cast the Lark representation of the parse tree into our own. | ||
'start' is always the top of the tree and `convert_ast` returns a | ||
list of zero or more parsed expressions. | ||
All other entrypoints are recursively converted into the | ||
appropriate atom, list, int, float etc. | ||
""" | ||
if type(ast) is lark.tree.Tree: | ||
if ast.data == "start": | ||
return [convert_ast(x) for x in ast.children] | ||
if ast.data == "list": | ||
return list_([convert_ast(x) for x in ast.children]) | ||
if type(ast) is lark.lexer.Token: | ||
ty = ast.type.lower() | ||
if ty == "int": | ||
return int_(int(ast.value)) | ||
if ty == "float": | ||
return float_(float(ast.value)) | ||
elif ty == "bool": | ||
return bool_(True if ast.value == "#t" else False) | ||
elif ty == "atom": | ||
return atom(ast.value) | ||
raise Exception("Unparsed AST: '%s'" % ast) | ||
|
||
def parse_str(x): | ||
return convert_ast(parser.parse(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters