Skip to content

Commit

Permalink
Cleaner order.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickardlindberg committed May 3, 2020
1 parent 3e3fc5c commit e3e7dd5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 89 deletions.
23 changes: 11 additions & 12 deletions writing/rlmeta-vm-poster/codegenerator.rlmeta
@@ -1,6 +1,4 @@
CodeGenerator {
ast = [%:x] -> x
py = .:x -> repr(x)
Grammar = .:x ast*:ys -> { "class " x "(Grammar):\n\n" >
"def assemble(self, I, LABEL):\n" >
ys
Expand Down Expand Up @@ -40,26 +38,27 @@ CodeGenerator {
MatchCallRule = -> { "I('MATCH_CALL_RULE')\n" }
Label = -> { "I('LABEL')\n" }
SemanticAction = ast:x -> { "I('ACTION', lambda scope: " x ")\n" }
MatchRule = py:x -> { "I('CALL', " x ")\n" }
MatchRange = py:x py:y -> { "I('MATCH_RANGE', " x ", " y ")\n" }
MatchString = py:x -> { "I('MATCH_STRING', " x ")\n" }
MatchCharseq = py:x -> { "I('MATCH_CHARSEQ', " x ")\n" }
MatchAny = -> { "I('MATCH_ANY')\n" }
MatchList = ast:x -> { "I('PUSH_STREAM')\n"
x
"I('POP_STREAM')\n" }
String = py
List = astList
List = astListItem*:xs -> { "(" xs "[])" }
Format = astItems:x -> { "join([" x "])" }
Indent = ast:x -> { "indent(" x ")" }
FnCall = .:x astItems:y -> { x "(" y ")" }
VarLookup = py:x -> { "scope[" x "].eval()" }
ast = [%:x] -> x
astItems =
| ast:x astItem*:xs -> { x xs }
| -> { }
astItem = ast:x -> { ", " x }
astList = astListItem*:xs -> { "(" xs "[])" }
astListItem =
| ["ListItemSplice" ast:x] -> { x "+" }
| ast:x -> { "[" x "]+" }
MatchRule = py:x -> { "I('CALL', " x ")\n" }
MatchRange = py:x py:y -> { "I('MATCH_RANGE', " x ", " y ")\n" }
MatchString = py:x -> { "I('MATCH_STRING', " x ")\n" }
MatchCharseq = py:x -> { "I('MATCH_CHARSEQ', " x ")\n" }
MatchAny = -> { "I('MATCH_ANY')\n" }
MatchList = ast:x -> { "I('PUSH_STREAM')\n"
x
"I('POP_STREAM')\n" }
py = .:x -> repr(x)
}
149 changes: 72 additions & 77 deletions writing/rlmeta-vm-poster/rlmeta.py
Expand Up @@ -774,22 +774,6 @@ def assemble(self, I, LABEL):
class CodeGenerator(Grammar):

def assemble(self, I, LABEL):
LABEL('ast')
I('PUSH_SCOPE')
I('PUSH_STREAM')
I('MATCH_CALL_RULE')
I('BIND', 'x')
I('POP_STREAM')
I('ACTION', lambda scope: scope['x'].eval())
I('POP_SCOPE')
I('RETURN')
LABEL('py')
I('PUSH_SCOPE')
I('MATCH_ANY')
I('BIND', 'x')
I('ACTION', lambda scope: repr(scope['x'].eval()))
I('POP_SCOPE')
I('RETURN')
LABEL('Grammar')
I('PUSH_SCOPE')
I('MATCH_ANY')
Expand Down Expand Up @@ -902,14 +886,65 @@ def assemble(self, I, LABEL):
I('ACTION', lambda scope: join(["I('ACTION', lambda scope: ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchRule')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('CALL', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchRange')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('CALL', 'py')
I('BIND', 'y')
I('ACTION', lambda scope: join(["I('MATCH_RANGE', ", scope['x'].eval(), ', ', scope['y'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchString')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('MATCH_STRING', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchCharseq')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('MATCH_CHARSEQ', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchAny')
I('PUSH_SCOPE')
I('ACTION', lambda scope: join(["I('MATCH_ANY')\n"]))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchList')
I('PUSH_SCOPE')
I('CALL', 'ast')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('PUSH_STREAM')\n", scope['x'].eval(), "I('POP_STREAM')\n"]))
I('POP_SCOPE')
I('RETURN')
LABEL('String')
I('PUSH_SCOPE')
I('CALL', 'py')
I('POP_SCOPE')
I('RETURN')
LABEL('List')
I('PUSH_SCOPE')
I('CALL', 'astList')
I('LIST_START')
LABEL(6)
I('BACKTRACK', 7)
I('CALL', 'astListItem')
I('LIST_APPEND')
I('COMMIT', 6)
LABEL(7)
I('LIST_END')
I('BIND', 'xs')
I('ACTION', lambda scope: join(['(', scope['xs'].eval(), '[])']))
I('POP_SCOPE')
I('RETURN')
LABEL('Format')
Expand Down Expand Up @@ -942,28 +977,37 @@ def assemble(self, I, LABEL):
I('ACTION', lambda scope: join(['scope[', scope['x'].eval(), '].eval()']))
I('POP_SCOPE')
I('RETURN')
LABEL('ast')
I('PUSH_SCOPE')
I('PUSH_STREAM')
I('MATCH_CALL_RULE')
I('BIND', 'x')
I('POP_STREAM')
I('ACTION', lambda scope: scope['x'].eval())
I('POP_SCOPE')
I('RETURN')
LABEL('astItems')
I('BACKTRACK', 8)
I('BACKTRACK', 10)
I('PUSH_SCOPE')
I('CALL', 'ast')
I('BIND', 'x')
I('LIST_START')
LABEL(6)
I('BACKTRACK', 7)
LABEL(8)
I('BACKTRACK', 9)
I('CALL', 'astItem')
I('LIST_APPEND')
I('COMMIT', 6)
LABEL(7)
I('COMMIT', 8)
LABEL(9)
I('LIST_END')
I('BIND', 'xs')
I('ACTION', lambda scope: join([scope['x'].eval(), scope['xs'].eval()]))
I('POP_SCOPE')
I('COMMIT', 9)
LABEL(8)
I('COMMIT', 11)
LABEL(10)
I('PUSH_SCOPE')
I('ACTION', lambda scope: join([]))
I('POP_SCOPE')
LABEL(9)
LABEL(11)
I('RETURN')
LABEL('astItem')
I('PUSH_SCOPE')
Expand All @@ -972,20 +1016,6 @@ def assemble(self, I, LABEL):
I('ACTION', lambda scope: join([', ', scope['x'].eval()]))
I('POP_SCOPE')
I('RETURN')
LABEL('astList')
I('PUSH_SCOPE')
I('LIST_START')
LABEL(10)
I('BACKTRACK', 11)
I('CALL', 'astListItem')
I('LIST_APPEND')
I('COMMIT', 10)
LABEL(11)
I('LIST_END')
I('BIND', 'xs')
I('ACTION', lambda scope: join(['(', scope['xs'].eval(), '[])']))
I('POP_SCOPE')
I('RETURN')
LABEL('astListItem')
I('BACKTRACK', 12)
I('PUSH_SCOPE')
Expand All @@ -1005,46 +1035,11 @@ def assemble(self, I, LABEL):
I('POP_SCOPE')
LABEL(13)
I('RETURN')
LABEL('MatchRule')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('CALL', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchRange')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('CALL', 'py')
I('BIND', 'y')
I('ACTION', lambda scope: join(["I('MATCH_RANGE', ", scope['x'].eval(), ', ', scope['y'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchString')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('MATCH_STRING', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchCharseq')
I('PUSH_SCOPE')
I('CALL', 'py')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('MATCH_CHARSEQ', ", scope['x'].eval(), ')\n']))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchAny')
I('PUSH_SCOPE')
I('ACTION', lambda scope: join(["I('MATCH_ANY')\n"]))
I('POP_SCOPE')
I('RETURN')
LABEL('MatchList')
LABEL('py')
I('PUSH_SCOPE')
I('CALL', 'ast')
I('MATCH_ANY')
I('BIND', 'x')
I('ACTION', lambda scope: join(["I('PUSH_STREAM')\n", scope['x'].eval(), "I('POP_STREAM')\n"]))
I('ACTION', lambda scope: repr(scope['x'].eval()))
I('POP_SCOPE')
I('RETURN')

Expand Down

0 comments on commit e3e7dd5

Please sign in to comment.