Created
May 30, 2015 20:12
-
-
Save mgronhol/a90cadf61a3e39f212ea to your computer and use it in GitHub Desktop.
Simple python lisp parser using shlex
This file contains hidden or 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
| #!/usr/bin/env python | |
| import shlex, pprint | |
| text = """ | |
| (define mauri (lambda (x y) | |
| (+ x y) | |
| ) | |
| ) | |
| (display (mauri 1 2) ) | |
| """ | |
| lexer = shlex.shlex( text ) | |
| stack = [] | |
| bucket = [] | |
| for token in lexer: | |
| if token == "(": | |
| stack.append( list( bucket ) ) | |
| bucket = [] | |
| elif token == ")": | |
| old_bucket = stack.pop() | |
| old_bucket.append( list( bucket ) ) | |
| bucket = old_bucket | |
| else: | |
| bucket.append( token ) | |
| pprint.pprint( bucket ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment