Example #1
0
 def interpret_func_call(self, node):
     """Execute a function call by pushing a new frame onto the symbol table, loading the parameters of the function call into that frame, and then executing the statements in the function definition."""
     ind = len(self.sym_tab) - 1
     while ind >= 0 and (node.FUNC_DEF_TYPE,
                         node.value) not in self.sym_tab[ind]:
         ind -= 1
     if ind < 0:
         uninitialized_function(node)
     func_def_node = self.sym_tab[ind][(node.FUNC_DEF_TYPE, node.value)]
     param_node, slist_node = func_def_node.children
     if len(param_node.children) != len(node.children):
         argument_length_mismatch(node)
     arg_vals = []
     einterpreter = ExpressionInterpreter(self.sym_tab)
     for expr in node.children:
         arg_vals.append(einterpreter.interpret_expression(expr))
     self.sym_tab.append(
         dict(
             zip([(param.type_, param.value)
                  for param in param_node.children], arg_vals)))
     sinterpreter = StatementInterpreter(self.sym_tab)
     ret_val = sinterpreter.interpret_statement_list(slist_node)
     self.sym_tab.pop()
     if ret_val == None:
         return 0
     return ret_val
Example #2
0
 def testInterpreterGCD(self, mock_input, mock_output):
     """Verify the correctness of the gcd.oats program (The gcd of 1071 and 462 is 21)."""
     mock_input.side_effect = ["1071", "462"]
     lex = Lexer("Test/Examples/gcd.oats")
     sparser = StatementParser(lex)
     root = sparser.parse_main()
     sym_tab = [dict()]
     sinterpreter = StatementInterpreter(sym_tab)
     sinterpreter.interpret_statement_list(root)
     self.assertEqual(mock_output.getvalue(), "21\n")
Example #3
0
 def testInterpreterFib(self, mock_input, mock_output):
     """Verify the correctness of the fibonnaci.oats program (The 20th fibonnaci number is 6765)."""
     mock_input.side_effect = ["20"]
     lex = Lexer("Test/Examples/fibonnaci.oats")
     sparser = StatementParser(lex)
     root = sparser.parse_main()
     sym_tab = [dict()]
     sinterpreter = StatementInterpreter(sym_tab)
     sinterpreter.interpret_statement_list(root)
     self.assertEqual(mock_output.getvalue(), "6765\n")
Example #4
0
    def interpret_while(self, node):
        """Execute a while node.

        A while node has two children: An expression node and a statement list node. The expression node, representing the loop condition, is evaluated, and if the result is not equal to 0, the statement list node representing the body of the loop is executed. The process then repeats."""
        ret_val = None
        einterpreter = ExpressionInterpreter(self.sym_tab)
        sinterpreter = StatementInterpreter(self.sym_tab)
        condition_node = node.children[0]
        body_node = node.children[1]
        while einterpreter.interpret_expression(condition_node) != 0:
            ret_val = sinterpreter.interpret_statement_list(body_node)
            if ret_val != None:
                break
        return ret_val
Example #5
0
 def interpret_if(self, node):
     """Execute an if node.
     
     An if node has an even number of children, and has at least two children. Every subsequent pair of children consists of an expression node and a statement list node. The expression node corresponds to the condition, and the statement list node corresponds to the body of that if, elseif, or else statement."""
     ret_val = None
     einterpreter = ExpressionInterpreter(self.sym_tab)
     for k in range(0, len(node.children), 2):
         condition_node = node.children[k]
         body_node = node.children[k + 1]
         if einterpreter.interpret_expression(condition_node) != 0:
             sinterpreter = StatementInterpreter(self.sym_tab)
             ret_val = sinterpreter.interpret_statement_list(body_node)
             break
     return ret_val
Example #6
0
    def interpret_for(self, node):
        """Execute a for node.

        A for node has four children: An assignment node, an expression node, a statement list node, and an assignment node. The first assignment node represents the initial assignment step of a traditional for loop, and is only executed once. The expression node represents the loop condition. If the loop condition evaluates to a nonzero result, then the statement list node representing the body of the loop is executed. Then the last assignment node, representing the increment/decrement step of a traditional for loop, is executed. The process then repeats, until the loop condition evaluates to zero."""
        ret_val = None
        einterpreter = ExpressionInterpreter(self.sym_tab)
        sinterpreter = StatementInterpreter(self.sym_tab)
        assignment_node = node.children[0]
        condition_node = node.children[1]
        change_node = node.children[2]
        body_node = node.children[3]
        sinterpreter.interpret_assignment(assignment_node)
        while einterpreter.interpret_expression(condition_node) != 0:
            ret_val = sinterpreter.interpret_statement_list(body_node)
            if ret_val != None:
                break
            sinterpreter.interpret_assignment(change_node)
        return ret_val
Example #7
0
from Lexer.Lexer import Lexer
from Parser.StatementParser import StatementParser
from Interpreter.StatementInterpreter import StatementInterpreter
from Common.Common import *
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("file",
                    nargs=1,
                    help="Filename of the .oats file to be interpreted")
parser.add_argument("-d", "--debug", action='store_true')
args = parser.parse_args()
lexer = Lexer(args.file[0])
parser = StatementParser(lexer)
root = parser.parse_main()
if args.debug:
    print("-" * 50)
    print("PARSE TREE")
    print("-" * 50)
    root.print_recursive()
    print("-" * 50)
    print("PROGRAM")
    print("-" * 50)
sym_tab = [dict()]
interpreter = StatementInterpreter(sym_tab)
interpreter.interpret_statement_list(root)