Example #1
0
    def evaluate(self, table: SymbolTable):
        if self.value not in SymbolTable.functions:
            raise ValueError("The function " + self.value + " wasn't declared")
        n, dec = SymbolTable.functions[self.value]
        st = SymbolTable()
        # st.table = table.table

        if len(self.children) + 1 != len(dec.children):
            raise ValueError("Not enough arguments in function call: " +
                             self.value)

        for i in range(len(self.children)):
            c1 = self.children[i].evaluate(table)
            c2 = dec.children[i][1].evaluate(table)
            if c1[0] == c2[0]:
                st.setter(dec.children[i][0].value, c2)
                st.setter(dec.children[i][0].value, c1)
            else:
                raise ValueError("Function call '" + self.value +
                                 "' has the wrong type for argument " +
                                 str(i) + ": " + c1[0] + " != " + c2[0])

        dec.children[-1].evaluate(st)

        if st.getter("return") != None:
            ret = st.getter("return")
            if ret[0] == dec.returnType:
                return ret
            else:
                raise ValueError(
                    "Return type is not the same as the Declaration type: " +
                    ret[0] + " != " + dec.returnType)
Example #2
0
    def Evaluate(self, st):

        new_st = SymbolTable(st)
        get_node = st.getter(self.value)
        node = get_node[0]
        func_void = get_node[1]

        if func_void == "FUNCTION":
            new_st.declare(node.children[0][0], node.children[0][1])

        if node.children:
            if len(self.children) > len(node.children[1]):
                raise ValueError(f"Too many arguments in {self.value}")

            if len(self.children) < len(node.children[1]):
                raise ValueError(f"missing arguments in {self.value}")

        for i in range(len(node.children[1])):
            new_st.create(node.children[1][i][0],
                          self.children[i].Evaluate(st),
                          node.children[1][i][1])

        for child in node.children[2]:
            child.Evaluate(new_st)

        if func_void == "FUNCTION":
            return_value = new_st.getter(self.value)
            # print(return_value)

            #if the type match
            if return_value[1] == node.children[0][1]:
                return return_value[0]
Example #3
0
 def evaluate(self, table: SymbolTable):
     for i in self.children:
         i.evaluate(table)
         if table.getter("return") != None:
             break
Example #4
0
 def evaluate(self, table: SymbolTable):
     return table.getter(self.value)