From 211cbeee9770e36d523a4f2aa8f3861235c16c8f Mon Sep 17 00:00:00 2001 From: Teddy Date: Wed, 31 Jul 2013 13:20:09 +0800 Subject: more friendly input --- sketch.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/sketch.py b/sketch.py index 039ab0f..526dca5 100755 --- a/sketch.py +++ b/sketch.py @@ -233,6 +233,8 @@ class Tokenizor(): .replace(')', ' )')\ .split() self.data = "" + if len(self.tokenized) == 0: + return None return self.tokenized.pop(0) class Node(object): @@ -285,7 +287,7 @@ class RetAddr(object): def __str__(self): return "#" -class AbsSynTree(EvalObj): +class ASTree(EvalObj): def to_obj(self, obj): if isinstance(obj, Node): return obj @@ -300,10 +302,16 @@ class AbsSynTree(EvalObj): # else the obj is a string def __init__(self, stream): - stack = list() + self.stream = stream + self.stack = list() # Empty stack + + def absorb(self): + stack = self.stack while True: - token = stream.read() - if token is None: break + if len(stack) > 0 and stack[0] != '(': + return self.to_node(stack.pop(0)) # Got an expression + token = self.stream.read() + if token is None: return None if token == '(': stack.append(token) elif token == ')': @@ -325,9 +333,6 @@ class AbsSynTree(EvalObj): stack[-1] = self.to_node(None) else: stack.append(self.to_obj(token)) - for i in range(len(stack)): - stack[i] = self.to_node(stack[i]) - self.tree = stack[0] def is_obj(string): return isinstance(string, EvalObj) @@ -452,7 +457,7 @@ class Evaluator(object): def run_expr(self, prog): stack = [0] * 100 # Stack ostack = [0] * 100 # Pending operators - pc = prog.tree # Set to the root + pc = prog # Set to the root cont = Continuation(None, pc, None, None) envt = self.envt top = -1 # Stack top @@ -612,16 +617,20 @@ import sys, pdb #ins_set = ("((lambda (x y) (+ x y)) 1 2)",) #ins_set = ("(+ 1 2)",) # t.feed(ins_set) -# a = AbsSynTree(t) +# a = ASTree(t) # print a.tree.chd.print_() #for ins in ins_set: # print "TEST" # t.feed(ins) -# print e.run_expr(AbsSynTree(t)).ext_repr() +# print e.run_expr(ASTree(t)).ext_repr() # + +a = ASTree(t) while True: sys.stdout.write("Syasi> ") - cmd = sys.stdin.readline() - if cmd == "": break - t.feed(cmd) - print e.run_expr(AbsSynTree(t)).ext_repr() + while True: + exp = a.absorb() + if exp: break + cmd = sys.stdin.readline() + t.feed(cmd) + print e.run_expr(exp).ext_repr() -- cgit v1.2.3-70-g09d2