Created
March 25, 2017 09:06
-
-
Save ponyatov/2e7b59ddbabfd1986fb291fd4a9a5661 to your computer and use it in GitHub Desktop.
Using PLY with RPython (FORTH-like VM)
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
src = ''' | |
nop | |
nop | |
. | |
1 2 3 | |
. | |
bye | |
''' | |
D = [] # data stack | |
R = [] # return stack | |
# parser | |
import ply.lex as lex | |
import ply.yacc as yacc | |
tokens = ['SYM','DOT'] | |
t_ignore = ' \t\r' | |
def t_newline(t): | |
r'\n' | |
t.lexer.lineno += 1 | |
def t_DOT(t): | |
'\.' | |
return t | |
def t_SYM(t): | |
'[a-zA-Z0-9_]+' | |
return t | |
def p_REPL_none(p): ' REPL : ' | |
def p_REPL_recur(p): | |
' REPL : REPL ex ' | |
# print p[2] | |
def p_SYM(p): | |
' ex : SYM ' | |
D.append(p[1]) | |
def p_DOT(p): | |
' ex : DOT ' | |
print D | |
def t_error(t): print 'lexer/error',t | |
def p_error(p): print 'parse/error',p | |
def main(argv): | |
lex.lex() | |
yacc.yacc(debug=False,write_tables=False).parse(src) | |
return 0 | |
def target(*args): return main,None | |
import sys | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment