コード例 #1
0
 def evaluate(self, variables, functions):
     x = functions.lookup(self._name)
     y = VarTree()
     for i in range(len(x[0])):
         y.assign(x[0][i],
                  self._parameter[i].evaluate(variables, functions))
     return x[1].evaluate(y, functions)
コード例 #2
0
 def evaluate(self, variables, functions):
     temp = VarTree()
     [params, body] = functions.lookup(self._name)
     for i in range(len(params)):
         par = params[i]
         parVal = self._args[i].evaluate(variables, functions)
         temp.assign(par, parVal)
     return body.evaluate(temp, functions)
コード例 #3
0
ファイル: clock.py プロジェクト: srobo-legacy/compd
    def __init__(self, root):
        self.tree = VarTree()
        root.clock = self.tree

        self.tree.event_time = 0
        self.tree.paused_time = 0
        self.tree.paused = False

        glib.timeout_add(1000, self.tick)
コード例 #4
0
    def __init__(self, root):
        self.tree = VarTree()
        root.scheduler = self.tree

        self.tree.curslot_id = 0
        self.tree.curslot_duration = 0
        self.tree.curslot_time = 0
        self.tree.curslot_type = 0

        root.subscribe( "sr.clock.event_time", self.tick )
コード例 #5
0
def eval_postfix(iter):

    s = LinkedList()  #s = stack
    t = VarTree()  #t = tree

    for token in iter:
        if (str(token)).isalnum():
            s.push(token)

        elif token == '=':
            val = s.pop()
            if not (str(val)
                    ).isdigit():  #allows assigning one variable to another
                val = t.lookup(
                    val
                )  #if it finds a variable, grabs its value from the tree
            var = s.pop()
            t.assign(str(var), val)
            s.push(var)

        else:
            right = s.pop()
            left = s.pop()
            s.push(eval(str(left) + token + str(right)))

    #the solution should be the only value in the stack
    sol = s.pop()
    if not (str(sol)).isdigit():
        sol = t.lookup(sol)
    return int(sol)
コード例 #6
0
 def __init__(self):
     self.first_stmt = -1  # 'main' not yet found
     self.last_temp = 0  # no registers yet claimed
     self.functions = VarTree()  # no functions yet
     self.variables = VarTree()  # no global variables yet
     self.code = []  # no instructions yet
コード例 #7
0
        yield ":"
        yield from self._false
        yield ")"

    def postfix(self):
        pass

    def evaluate(self, variables, functions):
        if self._conditional.evaluate(variables, functions):
            return self._true.evaluate(variables, functions)
        else:
            return self._false.evaluate(variables, functions)


if __name__ == '__main__':
    V = VarTree()
    VA = Var("A")
    Sum = Oper(Value(2), '+', Value(3))
    A = Oper(VA, '=', Sum)
    print("Infix iteration: ", list(A))
    print("String version ", A)
    print("Postfix iteration: ", list(A.postfix()))
    print("Execution: ", A.evaluate(V))
    print("Afterwards, A = ", VA.evaluate(V))

    # If A == 5, return A+2 else return 3
    CondTest = Cond(Oper(VA, '==', Value(5)), Oper(VA, '+', Value(2)),
                    Value(3))
    print(CondTest, '-->', CondTest.evaluate(V))

#Output:
コード例 #8
0
    next(iterator)              # "deffn"
    name = next(iterator)       # function name
    next(iterator)              # (
    parms = [next(iterator)]    # first argument
    while next(iterator)==',':
        parms.append(next(iterator))
    next(iterator)              # =
    return name, parms, tree_assign(iterator)

def evaluate(expr):
    """Define a new function, or evaluate an expression
       The decision is based on the first token in the input line
    """
    iterator = Peekable(new_split_iter(expr))
    if peek(iterator) == "deffn":
        name, parms, body = define_func(iterator)
        functions.assign(name, (parms, body))
    else:
        print(expr,':',tree_assign(iterator).evaluate(variables, functions))

functions = VarTree()
variables = VarTree()
if __name__ == "__main__":
    evaluate("deffn sqr(x) = x*x")
    evaluate("deffn abs(x) = x > 0 ? x : 0-x")
    evaluate("deffn fact(n) = n <= 1 ? 1 : n * fact(n-1)")
    evaluate("sqr(4)")
    evaluate("abs(3-5)")
    evaluate("fact(5)")

コード例 #9
0
from vartree import VarTree
from linkedlist import linkedlist
from infixtopostfix import to_postfix

ops = ['+', '-', '/', '%', '*', '=']
var = VarTree()


def eval_postfix(expr):
    stack = linkedlist(expr)
    for i in expr:
        if i in ops:
            if i == "=":
                second = stack.pop()
                first = stack.pop()
                if second.isalpha():
                    second = var.lookup(second)
                var.assign(first, second)
                stack.push(first)
            else:
                second = stack.pop()
                if second.isalpha():
                    second = var.lookup(second)
                first = stack.pop()
                if first.isalpha():
                    first = var.lookup(first)
                stack.push(str(eval(str(first) + i + str(second))))
        else:
            stack.push(i)

    value = stack.pop()
コード例 #10
0
	def evaluate(self, variables, functions):
		parms, body = functions.lookup(self._name)
		newTree = VarTree()
		for i in range(0, len(parms)):
			newTree.assign(parms[i], self._arg[i].evaluate(variables, functions))
		return body.evaluate(newTree, functions)