Created
February 23, 2018 02:58
-
-
Save shaunagm/629245e01328c725610de11717d94b86 to your computer and use it in GitHub Desktop.
A simple lisp parser
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
import csv | |
input = "" | |
with open('example.lsp', 'r') as input_file: | |
for line in input_file: | |
if line[0] is not ";": | |
input += line | |
def parse(input): | |
this_level = [] | |
current_token = "" | |
index = 0 | |
while index < len(input): | |
if input[index] == "(": | |
processed_chars, result = parse(input[index+1:]) | |
this_level.append(result) | |
index += processed_chars | |
elif input[index] == ")": | |
if current_token is not "": | |
this_level.append(current_token) | |
return index+1, this_level | |
elif input[index] == " ": | |
if current_token is not "": | |
this_level.append(current_token) | |
current_token = "" | |
else: | |
current_token += input[index] | |
index += 1 | |
return this_level | |
result = parse(input) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment