Add python.2 implementation
[jackhill/mal.git] / python.2 / step1_read_print.py
1 import readline
2
3 import reader
4 from mal_types import MalExpression, MalSyntaxException
5
6
7 def READ(x: str) -> MalExpression:
8 return reader.read(x)
9
10
11 def EVAL(x: MalExpression) -> MalExpression:
12 return x
13
14
15 def PRINT(x: MalExpression) -> str:
16 return str(x)
17
18
19 def rep(x: str) -> str:
20 try:
21 return PRINT(EVAL(READ(x)))
22 except BaseException:
23 return "Expression is invalid or unbalanced: " + x
24
25
26 if __name__ == "__main__":
27 # repl loop
28 eof: bool = False
29 while not eof:
30 try:
31 line = input("user> ")
32 readline.add_history(line)
33 try:
34 print(rep(line))
35 except MalSyntaxException as e:
36 print("ERROR: invalid syntax: " + str(e))
37 except EOFError:
38 eof = True