Exemplo n.º 1
0
 def __init__(self):
     # initialize the symbol table (for ids -> values)
     self.sym_table = sym_tbl.SymbolTable()
     # holds the type of last expression type
     self.current_value = None
     # the heap {oid:struct_obj}
     self.heap = {}
Exemplo n.º 2
0
 def __init__(self):
     # initialize the symbol table (for ids -> types)
     self.sym_table = symbol_table.SymbolTable()
     # current_type holds the type of the last expression type
     self.current_type = None
     # global env (for return)
     self.sym_table.push_environment()
     # set global return type to int
     self.sym_table.add_id('return')
     self.sym_table.set_info('return', token.INTTYPE)
     # load in built-in function types
     self.sym_table.add_id('print')
     self.sym_table.set_info('print', [[token.STRINGTYPE], token.NIL])
     self.sym_table.add_id('length')
     self.sym_table.set_info('length', [[token.STRINGTYPE], token.INTTYPE])
     self.sym_table.add_id('get')
     self.sym_table.set_info('get', [[token.INTTYPE, token.STRINGTYPE], token.STRINGTYPE])
     self.sym_table.add_id('reads')
     self.sym_table.set_info('reads', [[token.NIL], token.STRINGTYPE])
     self.sym_table.add_id('readi')
     self.sym_table.set_info('readi', [[token.NIL], token.INTTYPE])
     self.sym_table.add_id('readf')
     self.sym_table.set_info('readf', [[token.NIL], token.FLOATTYPE])   
     self.sym_table.add_id('itos')
     self.sym_table.set_info('itos', [[token.INTTYPE], token.STRINGTYPE])   
     self.sym_table.add_id('itof')
     self.sym_table.set_info('itof', [[token.INTTYPE], token.FLOATTYPE])
     self.sym_table.add_id('ftos')
     self.sym_table.set_info('ftos', [[token.FLOATTYPE], token.STRINGTYPE])
 def action_11(self, prev_token):
     self.is_global = True
     # Delete local symbol table
     self.local_table = symbol_table.SymbolTable(50)
     self.current_function = None
     self.backpatch(self.local_store, self.local_memory)
     self.generate("free", self.local_memory)
     self.generate("PROCEND")
Exemplo n.º 4
0
 def assemble(self, asms):
     self.tokenizer = tokenizer.Tokenizer()
     token_lines = [self.tokenizer.tokenize(asm) for asm in asms]
     self.parser = parser.Parser()
     parsed_lines = self.parser.parse(token_lines)
     self.symbol_table = symbol_table.SymbolTable()
     self.symbol_table.generate(parsed_lines)
     self.generator = generator.Generator()
     codes = self.generator.generate(parsed_lines, self.symbol_table)
     return codes
Exemplo n.º 5
0
    def __init__(self, jack_tokenizer, output):
        """
		constructor for compilation engine
		"""

        self.tokenizer = jack_tokenizer
        self.writer = vm_writer.VMWriter(output)
        self.symbol_table = symbol_table.SymbolTable()
        self.class_name = ""
        self.function_name = ""
Exemplo n.º 6
0
def main(args):
    try:
        inFilename = parseArgs(args)
        with open(inFilename) as file:
            symbolTable = st.SymbolTable()
            text = file.read()
            tokens, tokenTypes = lexer.genTokenStream(text)
            try:
                wp = parser.WibblyParser()
                wp.parse(tokens, symbolTable)
            except parser.SyntaxValidException as e:
                print('Syntax Valid')
            except parser.SyntaxInvalidException as e:
                print('Syntax Error')
    except IOError as err:
        print(err)
Exemplo n.º 7
0
def main():
    #Get the filename supplied by command line argument
    #Rework to allow for multiple asm files to be processed at the same time
    file_name = sys.argv[1]

    #Check that the file provided is a .asm file. Raise exception if not
    extension = file_name.split('.')[-1]
    if extension != 'asm':
        raise ValueError("Wrong filetype. Supply a .asm file")

    sym_table = symbol_table.SymbolTable()

    pass1(file_name, sym_table)
    #Create a list of commands to be later exported to a .hack file
    command_list = pass2(file_name)
    write_to_hack_file(command_list, file_name)
Exemplo n.º 8
0
def main(filename):

  outputFilename = os.path.splitext(filename)[0]+".hack"
  st = symbol_table.SymbolTable()  

  # First pass store all LABELS
  p = parser.Parser(filename)
  p.advance()
  instructionCount = 0
  while p.hasMoreCommands():
    commandType = p.commandType()
    if p.commandType() == command_types.L_COMMAND: 
      symbol = p.symbol()
      st.addEntry(symbol, instructionCount)
    else:
      instructionCount += 1
    p.advance()

  # Second pass
  n = 16  
  p = parser.Parser(filename)
  p.advance()
  while p.hasMoreCommands():

    command = ""

    if p.commandType() == command_types.A_COMMAND:
      symbol = p.symbol()
      address = ""
      if st.contains(symbol):
        address = st.getAddress(symbol)
      elif symbol.isdigit():
        address = symbol
      else:
        st.addEntry(symbol, n)
        address = n
        n += 1
      command = format(int(address), '#018b')[2:]
    elif p.commandType() == command_types.C_COMMAND:
      command = "111" + code.comp(p.comp()) + code.dest(p.dest()) + code.jump(p.jump())

    if bool(command):
      with open(outputFilename, 'a') as outFile:
        outFile.write(command+"\n")

    p.advance()
Exemplo n.º 9
0
def _parse_symbol_table():
    """Read symbol table from video player debug file."""

    opcode_data = {}
    for name, data in symbol_table.SymbolTable(
            "player/iivision.dbg").parse().items():
        if name.startswith("\"op_"):
            op_name = name[4:-1]
            start_addr = int(data["val"], 16)

            opcode_data.setdefault(op_name, {})["start"] = start_addr

    opcode_addrs = []
    for op_name, addrs in opcode_data.items():
        for op in OpcodeCommand:
            if op.name.lower() != op_name:
                continue
            opcode_addrs.append((op, addrs["start"]))

    return sorted(opcode_addrs, key=lambda x: x[1])
Exemplo n.º 10
0
 def CompileSubroutineDec(self, subroutine, env):
     new_env = symbol_table.SymbolTable(env)
     sub_type = subroutine.subroutine_type.keyword
     return getattr(self,
                    "Compile" + sub_type.capitalize() + "Dec")(subroutine,
                                                               new_env)
Exemplo n.º 11
0
 def CompileVMCode(self, jack_program):
     env = symbol_table.SymbolTable(None)
     return os.linesep.join(self.CompileClass(jack_program, env))
Exemplo n.º 12
0
 def __init__(self):
     self.symtab = symbol_table.SymbolTable()
Exemplo n.º 13
0
 def delayEval(curAddr):
     symtab = symbol_table.SymbolTable()
     symtab.insert(".", curAddr)
     self[1].replace_idents(symtab)
     return self[1].eval()
    def __init__(self):
        self.semantic_stack = []

        # True if in insertion mode in symbol table. False if in search mode in symbol table
        self.is_insert = True
        # True if in global environment. False if in local environment
        self.is_global = True
        # True if next variable should be treated as array. False if next variable is a simple variable (not array)
        self.is_array = False
        # Keeps track of number of global variable declarations
        self.global_memory = 0
        # Keeps track of number of local variable declarations
        self.local_memory = 0

        # self.local_table =
        self.temp_var_num = 0

        self.local_table = symbol_table.SymbolTable(50)
        self.global_table = symbol_table.SymbolTable(50)

        # Keeps track of alloc statement
        self.global_store = 0
        self.local_store = 0

        self.quadruples = quadruple.Quadruples()

        self.create_num = 0

        # Initialize current function as None
        self.current_function = None

        # Initialization of constant table
        self.constant_table = symbol_table.SymbolTable(50)

        # Parameter count stack, where each element is the number of
        # parameters of a function or procedures
        self.param_count = []

        # Parameter info stack, where each element is a list of symbol
        # table entries representing the parameter info for a function
        # or procedure
        self.param_stack = []

        # Keep track of the current index into the list on the
        # top of param_stack
        self.next_param = 0

        self.valid_semantic_actions = [
            1,
            2,
            6,
            4,
            7,
            13,
            9,
            3,
            55,
            56,
            # 30, 40, 42, 44, 31, 41, 46, 48
            # 31, 41, 46, 48
            30,
            40,
            42,
            44,
            31,
            41,
            46,
            43,
            45,
            48,
            32,
            33,
            34,
            53,
            54,
            38,
            39,
            47,
            22,
            24,
            25,
            26,
            27,
            28,
            29,
            5,
            11,
            15,
            16,
            17,
            19,
            20,
            21,
            35,
            51,
            54,
            36,
            37,
            49,
            50,
            52,
            "51READ",
            "51WRITE"
        ]
        self.actions_tested = []
Exemplo n.º 15
0
 def test_parse(self):
     dbg = io.StringIO(DEBUG_FILE)
     s = symbol_table.SymbolTable()
     self.assertEqual(
         {"\"op_ack\"", "\"op_tick\"", "\"rle1\""}, s.parse(dbg).keys())
Exemplo n.º 16
0
import analyzer
import symbol_table

table = symbol_table.SymbolTable()

rodata = "SECTION .rodata" + '\n'

data = "SECTION .data" + '\n'
bss = "SECTION .bss" + '\n'

text = dict()
text['functions'] = '\n' + "SECTION .text" + '\n'
text['functions'] += "EXTERN print_number, print_text, print_boolean" + '\n'
text['code'] = "global _start" + '\n'
text['code'] += "_start:" + '\n'

rodata_index = 0
data_index = 0
label_index = 0
loop_index = 0

builtin_functions = ['print']


def generate(node):
    initialize_code()
    generate_text(node, 'code')
    terminate_code()
    return rodata + data + bss + text['functions'] + text['code']

Exemplo n.º 17
0
 def __init__(self, tokens, writer, module_name):
     self.tokens = tokens
     self.symbols = symbol_table.SymbolTable()
     self.writer = writer
     self.module_name = module_name
Exemplo n.º 18
0
class TypeChecker(NodeVisitor):
    table = symbol_table.SymbolTable(None, 'program')

    result_types = defaultdict(lambda :{
            (types_.Int, types_.Int): types_.Int,
            (types_.Int, types_.Float): types_.Float,
            (types_.Float, types_.Int): types_.Float,
            (types_.Float, types_.Float): types_.Float
        })
    result_types['.+'] = {(types_.Matrix, types_.Matrix): types_.Matrix}
    result_types['.-'] = {(types_.Matrix, types_.Matrix): types_.Matrix}
    result_types['./'] = {(types_.Matrix, types_.Matrix): types_.Matrix}
    result_types['.*'] = {(types_.Matrix, types_.Matrix): types_.Matrix}


    def __init__(self):
        self.errors = False

    def visit_ID(self, node):
        symbol = self.table.get(node.id)
        if not symbol:
            return types_.Id()
        return symbol.symbol_type

    def visit_Number(self, node):
        if node.type == 'integer':
            return types_.Int()
        return types_.Float()

    def visit_Matrix(self, node):

        if node.type == None:
            return self.visit(node.content)

        elif node.type == 'zeros':
            node.matrix = [0 for i in range(node.dim)] * node.dim

        elif node.type == 'ones':
            node.matrix = [1 for i in range(node.dim)] * node.dim

        elif node.type == 'eye':
            node.matrix = [[1 if i == j else 0 for i in range(node.dim)] for j in range(node.dim)]

            return types_.Matrix(node.dim, node.dim, types_.Int())


    def visit_IDAt(self, node):
        type_index1 = self.visit(node.index1)
        type_index2 = self.visit(node.index2)
        index1 = None
        index2 = None
        if not type_index1.is_integer:
            print(f"Error in line {node.line}: first index is not an integer!")
        else:
            index1 = node.index1.value

        if not type_index2.is_integer:
            print(f"Error in line {node.line}: second index is not an integer!")
        else:
            index2 = node.index2.value

        id = self.visit_ID(node)
        if id.is_id():
            print(f"Error in line {node.line}: variable {node.id} not known!")
        elif not id.is_matrix():
            print(f"Error in line {node.line}: variable {node.id} not a matrix!")
        else:
            if index1 < id.x and index2 < id.y:
                return id.element_type()
            else:
                print(f"Error in line {node.line}: index out of bounds!")

        return types_.Int()

    def visit_Condition(self, node):

        bool = self.visit(node.bool_expression)

        if not bool.is_boolean():
            print(f"Error in line {node.line}: Condition is not of boolean type!")

        self.table.pushScope('condition')
        self.visit(node.statement1)
        self.table.popScope()
        self.table.pushScope('condition')
        self.visit(node.statement2)
        self.table.popScope()

    def visit_BinaryOperation(self, node):
        right = self.visit(node.right)
        left = self.visit(node.left)
        if node.operator in ['.+', '.-', '.*', './']:

            if left.is_matrix() and  right.is_matrix() \
                    and (left.x != right.x or left.y != right.y):
                print(f"Error in line {node.line}: Matrix binary operation require equal dimensions!")

            return right

        elif node.operator in ['+', '-', '*', '/']:
            if not right.is_numeric():
                print(f"Error in line {node.line}: {node.operator} require number arguments!")
                return types_.Int()
            if not left.is_numeric():
                print(f"Error in line {node.line}: {node.operator} require number arguments!")
                return types_.Int()
            return TypeChecker.result_types[node.operator][(left.__class__,right.__class__)]()

    def visit_UnaryOperation(self, node):

        arg = self.visit(node.arg)
        if node.operator == '\'':
            if not arg.is_matrix():
                print(f"Error in line {node.line}: Wrong transpose argument (of matrix type required)!")
            else:
                return types_.Matrix(-1,-1,None)

        if node.operator == '.-':
            if not arg.is_matrix():
                print(f"Error in line {node.line}: Wrong .- argument (of matrix type required)!")
            else:
                return types_.Matrix(-1, -1, None)

        if node.operator == '-':
            if not arg.is_numeric():
                print(f"Error in line {node.line}: Wrong - argument (of number type required)!")
                return types_.Int()
            else:
                return arg

    def visit_Assignment(self, node):

        if not isinstance(node.left, entities.ID):
            print(f"Error in line {node.line}: Left operand is not a variable!")
            return

        right = self.visit(node.right)
        if node.operator == '=':
            self.table.put(node.left.id, symbol_table.VariableSymbol(right, node.right))
            return

        if node.operator != '=':
            id = self.table.get(node.left.id)
            if not id:
                print(f"Error in line {node.line}: Unknown variable!")
            if not id.symbol_type.is_numeric():
                print(f"Error in line {node.line}: Such assignment requires right arg of number type!")
        return

    def visit_Relation(self, node):

        left = self.visit(node.left)
        if not left.is_numeric():
            print(f"Error in line {node.line}: left operand of relation is not numeric!")

        right = self.visit(node.right)
        if not right.is_numeric():
            print(f"Error in line {node.line}: right operand of relation is not numeric!")

        return types_.Boolean()

    def visit_WhileLoop(self, node):

        expr = self.visit(node.expr)

        if not expr.is_boolean():
            print(f"Error in line {node.line}: while condition has to be of boolean type!")

        self.table.pushScope('loop')
        prog = self.visit(node.prog)
        self.table.popScope()

    def visit_ForLoop(self, node):

        self.table.pushScope('loop')
        beg = self.visit(node.beg)
        end = self.visit(node.end)

        if not beg.is_integer():
            print(f"Error in line {node.line}: Beginning value of iterator has to be an integer!")

        if not end.is_integer():
            print(f"Error in line {node.line}: End value of iterator has to be a number!")

        self.visit(node.prog)
        self.table.popScope()

    def visit_Print(self, node):

        self.visit(node.list)

    def visit_Program(self, node):

        self.visit(node.statement)
        self.visit(node.program)

    def visit_List(self, node):
        length = len(node.get_value())
        types = []
        for n in node.get_value():
            types.append(self.visit(n))
        diff = False
        for i in range(length - 1):
            if type(types[i]) != type(types[i + 1]):
                diff = True

        if diff:
            return types_.List(length, None)
        return types_.List(length, types[0])

    def visit_List2D(self, node):
        list_of_vectors = node.get_value()
        list_of_length = []
        list_of_type = []
        for vector in list_of_vectors:
            l_type = self.visit(vector)
            list_of_length.append(l_type.length)
            list_of_type.append(l_type.element_type)
        ok = True
        diff = False
        for i in range(len(list_of_length) - 1):
            if list_of_length[i] != list_of_length[i + 1]:
                ok = False
            if type(list_of_type[i]) != type(list_of_type[i + 1]):
                diff = True
        if None in list_of_type:
            diff = True

        if not ok:
            print(f"Error in line {node.line}: Vectors of different lengths!")

        if diff:
            print(f"Error in line {node.line}: All variables in a matrix has to be of one type!")
            return types_.Matrix(len(list_of_length), list_of_length[0], types_.Int())

        return types_.Matrix(len(list_of_length), list_of_length[0], list_of_type[0])

    def visit_Empty(self, node):
        return 'empty'

    def visit_Break(self, node):
        if not self.table.is_inside_loop():
            print(f"Error in line {node.line}: Break outside a loop")
        return 'break'

    def visit_Contnue(self, node):
        if not self.table.is_inside_loop():
            print(f"Error in line {node.line}: Continue outside a loop")
        return 'continue'

    def visit_Return(self, node):

        if node.variable:
            n_type = self.visit(node.variable)
            if n_type not in ['integer', 'float', 'boolean'] and not 'matrix' in n_type:
                print(f"Error in line {node.line}: Wrong returned type!")
        return 'return'