Documentation and report.
This commit is contained in:
@@ -1,7 +1,26 @@
|
||||
import re
|
||||
import lisp
|
||||
from lisp import prettyprint
|
||||
|
||||
### READER.PY
|
||||
### Autumn Wolf
|
||||
### 04/26/26
|
||||
### CSE3024
|
||||
# This file contains the main Reader class, which is responsible for tokenizing, parsing, and handling user input,
|
||||
# passing it to the lisp interpreter for evaluation, then returning the results to the user.
|
||||
|
||||
# Recursively print data for human readability
|
||||
def prettyprint(expression) -> str:
|
||||
if type(expression) is list:
|
||||
retvalue = "("
|
||||
for x in expression:
|
||||
retvalue += prettyprint(x) + " "
|
||||
return retvalue.rstrip() + ")"
|
||||
elif type(expression) is bool:
|
||||
return "T" if expression else "NIL"
|
||||
elif str():
|
||||
return expression.upper()
|
||||
else:
|
||||
return str(expression)
|
||||
|
||||
class Reader:
|
||||
tokens: list[str]
|
||||
@@ -11,6 +30,7 @@ class Reader:
|
||||
self.interpreter = lisp.Lisp()
|
||||
self.outputfile = outputfile
|
||||
|
||||
# Run through tokens, passing them to the interpreter
|
||||
def run(self):
|
||||
if len(self.tokens) == 0:
|
||||
return
|
||||
@@ -26,6 +46,7 @@ class Reader:
|
||||
return
|
||||
self.flush()
|
||||
|
||||
# Capture or discard tokens based on Lisp syntax
|
||||
def tokenize(self, expression: str):
|
||||
self.tokens += re.findall(r"""(?:;.*|[\s,]*)([()']|"(?:\\.|[^\\"])*"?|[^\s()'",;]*)""", expression)
|
||||
|
||||
@@ -42,6 +63,7 @@ class Reader:
|
||||
self.tokens = self.tokens[1:]
|
||||
return token
|
||||
|
||||
# Parse an expression into a nested list of values for interpreter consumption
|
||||
def read_expression(self):
|
||||
if len(self.tokens) == 0:
|
||||
return None
|
||||
@@ -67,6 +89,7 @@ class Reader:
|
||||
else:
|
||||
return self.read_atom()
|
||||
|
||||
# Smart value conversions for some Lisp-specific syntax, such as T and NIL.
|
||||
def read_atom(self):
|
||||
token = self.consume()
|
||||
if token.upper() == "T":
|
||||
|
||||
Reference in New Issue
Block a user