Permalink
Browse files

ch4

  • Loading branch information...
1 parent abbfa27 commit 5510e337231b0a16398e9ad2e26f4176e1179ed1 @flaming0 committed Sep 29, 2012
View
@@ -6,3 +6,4 @@ ch4/*.scm~
ch4/interpreter-cpp/schint-release/
ch4/interpreter-cpp/schint-debug/
ch4/interpreter-cpp/schint/schint.pro.user
+ch4/interpreter-python/__pycache__/
@@ -0,0 +1,19 @@
+class Environment:
+
+ def __init__(self, initialFrame, baseEnv = None):
+ self.frame = initialFrame
+ self.baseEnv = baseEnv
+
+ def defineVariable(self, variable, value):
+ pass
+
+ def setVariableValue(self, variable, value):
+ pass
+
+
+globalEnvironment = Environment({ "+" : lambda x, y: x + y,
+ "-" : lambda x, y: x - y,
+ "*" : lambda x, y: x * y,
+ "/" : lambda x, y: x / y })
+
+from Expression import *
@@ -0,0 +1,36 @@
+from io import StringIO
+from tokenize import generate_tokens
+from Expression import *
+
+def analyzeExpr(expr):
+ if len(expr.raw) == 1 and "(" not in expr.raw and ")" not in expr.raw:
+ if expr.raw[0].isdigit():
+ return NumberExpression(expr.raw)
+ else:
+ return VariableExpression(expr.raw)
+
+ factory = ExpressionsFactory()
+ return factory.create(expr)
+
+
+def schemeEval(expr, env):
+ exp = analyzeExpr(expr)
+ exp.eval()
+
+
+def schemeApply(proc, args):
+ pass
+
+
+def Eval(exprString, env):
+ lst = [token[1] for token
+ in generate_tokens(StringIO(exprString).readline)
+ if token[1]]
+ schemeEval(Expression(lst), env)
+
+
+# debug
+if __name__ == "__main__":
+ #schemeEval("(define (square x) (* x x))", None)
+ Eval("(define x 2)", None)
+ Eval("(set x 15)", None)
@@ -0,0 +1,143 @@
+from Environment import Environment, globalEnvironment
+
+class ExpressionException(Exception): pass
+class BadExpression(ExpressionException): pass
+class PureVirtualFunctionCall(ExpressionException): pass # lol, abstract methods
+
+class Expression:
+
+ def __init__(self, exprList):
+ self.raw = exprList
+
+ def car(self):
+ if not len(self.raw):
+ raise BadExpression
+
+ if self.raw[1] == "(":
+ return Expression(self.raw[1:self.raw.index(")") + 1])
+ else:
+ return Expression(self.raw[1])
+
+ def cdr(self):
+ if not len(self.raw) or self.raw[0] != "(":
+ raise BadExpression
+
+ if self.raw[1] == "(":
+ temp = self.raw[self.raw.index(")"):]
+ temp = temp[temp.index("("):]
+ temp.insert(0, "(")
+ return Expression(temp)
+ else:
+ temp = self.raw[2:]
+ temp.insert(0, "(")
+ return Expression(temp)
+
+ def eval(self):
+ raise PureVirtualFunctionCall
+ #pass
+
+ # def __repr__ maybe I should use this function?
+
+
+class AssignmentExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def __assignmentVariable(self):
+ return VariableExpression([self.cdr().car().raw])
+
+ def __assignmentValue(self):
+ return Expression([self.cdr().cdr().car().raw])
+
+ def eval(self):
+ print("Eval assignment:", self.raw)
+ globalEnvironment.setVariableValue(
+ self.__assignmentVariable(),
+ schemeEval(self.__assignmentValue(), globalEnvironment)
+ )
+
+
+class DefinitionExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def __definitionVariable(self):
+ var = self.cdr().car().raw
+ if isinstance(var, str):
+ # define variable
+ return VariableExpression([var])
+ else:
+ # suppose, it's `(` symbol
+ # define procedure
+ return VariableExpression([self.cdr().car().car().raw])
+
+ def __definitionValue(self):
+ var = self.cdr().car().raw
+ if isinstance(var, str):
+ return Expression([self.cdr().cdr().car().raw])
+ else:
+ raise BadExpression # make lambda
+
+ def eval(self):
+ print("Eval definition:", self.raw)
+ globalEnvironment.defineVariable(
+ self.__definitionVariable(),
+ schemeEval(self.__definitionValue(), globalEnvironment)
+ )
+
+
+class IfExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def eval(self):
+ print("Eval if:", self.raw)
+ pass
+
+
+class LambdaExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def eval(self):
+ print("Eval lambda:", self.raw)
+ pass
+
+
+class NumberExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def eval(self):
+ print("Eval number:", self.raw)
+ pass
+
+
+class VariableExpression(Expression):
+
+ def __init__(self, exprList):
+ super().__init__(exprList)
+
+ def eval(self):
+ print("Eval variable:", self.raw)
+ pass
+
+
+class ExpressionsFactory:
+
+ __objects = {
+ "set" : AssignmentExpression,
+ "define" : DefinitionExpression,
+ "if" : IfExpression,
+ "lambda" : LambdaExpression
+ }
+
+ def create(self, expr):
+ return self.__objects[expr.car().raw](expr.raw)
+
+from EvalApply import schemeEval
@@ -0,0 +1,20 @@
+from EvalApply import schemeEval
+from Environment import Environment, globalEnvironment
+
+def printResult(exp):
+ print("Xynta")
+
+def main():
+ print("Type enter (blank line) to finish")
+
+ while True:
+ line = input("> ")
+ if line:
+ output = schemeEval(line, globalEnvironment)
+ printResult(output)
+ else:
+ break
+
+
+if __name__ == "__main__":
+ main()

0 comments on commit 5510e33

Please sign in to comment.