Skip to content

Instantly share code, notes, and snippets.

@shaunagm
Created February 23, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunagm/629245e01328c725610de11717d94b86 to your computer and use it in GitHub Desktop.
Save shaunagm/629245e01328c725610de11717d94b86 to your computer and use it in GitHub Desktop.
A simple lisp parser
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