예제 #1
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]
예제 #2
0
 def parse():
     st = SymbolTable()
     Parser.analyze_program(st)
     if Parser.is_valid(Parser.tok.curr):
         utils.print_error(Parser)
         raise ValueError('Found remaning values after last block')
     return nd.FuncCall(const.MAIN, []).eval(st)
예제 #3
0
def test_check_all_functions():
  st = SymbolTable()

  foo = [1,2,3]
  bar = 'hi'

  st.add('a', foo)
  st.add('b', bar)
  eq_(st.find('a'), foo)
  eq_(st.find('b'), bar)

  st.remove('a')
  assert not st.find('a')
  assert not st.check_scope('a')
  eq_(st.check_scope('b'), bar)

  st.add('c', foo)

  with st.in_scope() as st:
    assert st.find('c') == foo
    assert st.find('b') == bar
    assert not st.check_scope('c')
    assert not st.check_scope('b')
    st.add('b', foo)
    assert st.find('b') == foo

  eq_(st.find('b'), bar)
예제 #4
0
 def __init__(self, tokenizer, out_file_name):
     self._tokenizer = tokenizer
     self._vm_writer = VMWriter(out_file_name)
     self._class_name = None
     self._symbol_table = SymbolTable()
     self._counter = 0
     self._subroutine_name = None
예제 #5
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)
예제 #6
0
def main():
    with open(sys.argv[1], 'r') as myfile:
        input_data=myfile.read().replace('\n', '')

    st = SymbolTable()
    Parser.tokens.origin = input_data
    result = Parser.init_parse()
    result.eval(st)
예제 #7
0
파일: scanner.py 프로젝트: AndreiJeler/FLCD
 def __init__(self, file):
     self._st = SymbolTable()
     self._pif = PIF()
     self._tokens = get_tokens()
     self._file_name = file
     self._separators = separators
     self._operators = operators
     self._reserved_words = reserved_words
예제 #8
0
def main(input_path: Path, output_path: Path):
    """ wire up argument and file parsing to run assembler """
    with open(input_path) as input_file, open(output_path, 'w') as output_file:
        stripped = strip_whitespace(input_file)
        symbol_table = SymbolTable()
        unlabelled = populate_table(symbol_table, stripped)
        for instruction in translate_symbols(symbol_table, unlabelled):
            print(instruction, file=output_file)
예제 #9
0
 def __init__(self, f):
     self.asm_file = f
     self.table = SymbolTable()
     self.data = self.read_file()
     self.parser = Parser(self.data)
     self.commands = self.parser.commands
     self.translator = Translator()
     self.filename = self.asm_file.split(".")[0]
예제 #10
0
    def parse(self):
        """
    Parsea un archivo entero hasta el final y devuelve los errores encontrados.
    """
        result = ""
        source = self.file
        source = re.sub(r'\t+', '', source)  # Esto quita los carácteres \t'.
        source = re.sub(r' {2,}', ' ',
                        source)  # Elimina el exceso de espacios.

        line = 1

        while (len(source) > 0):
            # Esto se usa para ver cuáles elementos existen y cuál de los existentes
            # es el más cercano.
            len1 = -1
            len2 = -1
            len3 = -1

            v = []

            # Si existe lo añade al vector de elementos existentes.
            if ';' in source:
                len1 = source.index(';')
                v += [len1]
            if '{' in source:
                len2 = source.index('{')
                v += [len2]
            if '}' in source:
                len3 = source.index('}')
                v += [len3]

            this = min(v)

            #  Dice el número de línea según cuántos '\n' se han econtrado.
            if '\n' in source[:this]:
                line += source[:this].count('\n')

            if this == len1:  # Si el más cercano fue un ';'.
                result += self.parsestatement(
                    self.expr['nl'].sub('', source[:this].strip()), line)
            elif this == len2:  # Si el más cercano fue un '{'.
                result += self.parsefunction(
                    self.expr['nl'].sub('', source[:this].strip()), line)
            else:  # Si el más cercano fue un '}'.
                self.retscope()
                self.table = self.table.getfather()

            # Elimina lo que se acaba de parsear junto con el carácter delimitador.
            source = source[this + 1:]

        # Reinicia el scope.
        self.table = SymbolTable()
        self.scope = None
        # Para eliminar las línea repetidas.
        l = list(dict.fromkeys(re.sub(r'^\n', '', result).split('\n')))
        res = ''.join(s + '\n' for s in l)
        return re.sub(r'\n$', '', res)
예제 #11
0
파일: ppparser.py 프로젝트: Plingot/hlakit
    def p_id(self, p):
        '''id : ID'''
        macro = SymbolTable().lookup_symbol(p[1])
        if macro:
            print "Replacing %s with %s" % (p[1], macro.value)
            p[0] = macro.value
            return

        p[0] = p[1]
예제 #12
0
def test_check_scoping():
  st = SymbolTable()

  for val in xrange(0, 10):
    st.enter_scope()
    st.add('a', val)

  for val in xrange(9, -1, -1):
    assert st.find('a') == val
    st.leave_scope()
예제 #13
0
 def __init__(self, filename):
     self._input = self.preprocess(filename)
     self._name = filename.split('/')[-1]
     self._name = self._name.split('.')[0]
     self._table = SymbolTable()
     self.first_pass()
     self._parser = Parser(self._input)
     self._output = []
     self._code = Code()
     self.second_pass()
     self.write_output()
예제 #14
0
파일: main.py 프로젝트: vvnwu/nand2tetris
def main():
    print("hello world")
    #print(format(5,'b:5.'))
    # print(bin(5))
    # print('{0:5.b}'.format(5))
    st = SymbolTable()
    filename = 'C:\\Users\\wuviv\\OneDrive\\Documents\\nand2tetris\\nand2tetris\projects\\06\\add\\Add.asm'  #sys.argv[0]
    filenameout = 'test.hack'  #sys.argv[1]

    first_pass(st, filename)
    second_pass(st, filename, filenameout)
예제 #15
0
def main():
    instream = InStream('misspellings.txt')
    lines = instream.readAllLines()
    misspellings = SymbolTable()
    for line in lines:
        tokens = line.split(' ')
        misspellings[tokens[0]] = tokens[1]
    while not stdio.isEmpty():
        word = stdio.readString()
        if word in misspellings:
            stdio.write(word + '-->' + misspellings[word])
예제 #16
0
def test_lookups():
    st = SymbolTable()
    st.define("first", "int", SymbolType.STATIC)
    st.define("second", "SomeClass", SymbolType.FIELD)
    st.define("third", "String", SymbolType.ARG)
    st.define("fourth", "bool", SymbolType.VAR)

    assert (st.KindOf("first") == SymbolType.STATIC)
    assert (st.TypeOf("second") == "SomeClass")
    assert (st.IndexOf("third") == 0)
    assert (st.IndexOf("fourth") == 1)
예제 #17
0
def test_var_count():
    st = SymbolTable()
    st.define("first", "int", SymbolType.STATIC)
    st.define("second", "SomeClass", SymbolType.FIELD)
    st.define("third", "String", SymbolType.ARG)
    st.define("fourth", "bool", SymbolType.VAR)

    assert (st.varCount(SymbolType.STATIC) == 1)
    assert (st.varCount(SymbolType.FIELD) == 1)
    assert (st.varCount(SymbolType.ARG) == 1)
    assert (st.varCount(SymbolType.VAR) == 1)
예제 #18
0
 def __init__(self, file):
     self._st = SymbolTable()
     self._pif = PIF()
     self._tokens = get_tokens()
     self._file_name = file
     self._separators = separators
     self._operators = operators
     self._reserved_words = reserved_words
     self._identifier_FA = FiniteAutomatan("identifier.in")
     self._integer_FA = FiniteAutomatan("integer.in")
     self._string_FA = FiniteAutomatan("string.in")
     self._string_FA.delta.add_transition("Q2", "Q2", " ")
예제 #19
0
    def run(source):

        source = PrePro.filter(source)
        Parser.tokens = Tokenizer(source)
        Parser.tokens.selectNext()

        st = SymbolTable(None)

        Parser.program().Evaluate(st)
        
        if Parser.tokens.actual.tp != "EOF":
            raise ValueError(f"{Parser.tokens.actual.value} invalid at end of sentence")
예제 #20
0
def test_simple():
  st = SymbolTable()

  val = 1
  st.add('a', val)
  eq_(st.find('a'), val)

  st.enter_scope()
  eq_(st.find('a'), val)

  st.leave_scope()
  eq_(st.find('a'), val)
예제 #21
0
파일: node.py 프로젝트: pedrocunial/gil
 def eval(self, st):
     func_args, func_body = st.get(self.value)
     if len(func_args) != len(self.children):
         raise ValueError('Unmatching sizes between defined function args' +
                          f' and calling args ({len(func_args)} -- ' +
                          f'{len(self.children)})')
     inner_st = SymbolTable(father=st)
     inner_st.set(self.value, None)  # default return value
     for key, value in zip(func_args, self.children):
         inner_st.set(key, value.eval(st))
     func_body.eval(inner_st)
     return inner_st.get(self.value)
예제 #22
0
    def eval(self, st):
        new_st = SymbolTable(st)
        func = st.getSymbol(self.symbol)

        counter = 0

        if (func.args is not None):
            while (counter < len(func.args)):
                symbol = func.args[counter]
                new_st.setSymbol(symbol, self.args[counter].eval(st))
                counter += 1

        return func.child.eval(new_st)
 def setUp(self):
     self.table = SymbolTable()
     self.table.put(LetterGrade("A+"), 4.33)
     self.table.put(LetterGrade("A "), 4.00)
     self.table.put(LetterGrade("A-"), 3.67)
     self.table.put(LetterGrade("B+"), 3.33)
     self.table.put(LetterGrade("B "), 3.00)
     self.table.put(LetterGrade("B-"), 2.67)
     self.table.put(LetterGrade("C+"), 2.33)
     self.table.put(LetterGrade("C "), 2.00)
     self.table.put(LetterGrade("C-"), 1.67)
     self.table.put(LetterGrade("D "), 1.00)
     self.table.put(LetterGrade("F "), 0.00)
예제 #24
0
 def class_(self, state, ast):
    """Create class symbol table and pass on control to classVarDec and 
    subroutineDec.  No code geneartion here"""
    ast.next() #'class keyword'
    state['classname'] = ast.next_val()
    ast.next() #'{'
    state['sym_tbl'] = SymbolTable()
    while(ast.get_key() == 'classVarDec'):
       self.classVarDec(state, ast.next_sec())
    while(ast.get_key() == 'subroutineDec'):
       self.subroutineDec(state, ast.next_sec())
    ast.next() #'}'
    return
예제 #25
0
 def __init__(self, string=None):
     """
 Inicializa cada una de las variables y agarra un string de archivo.
 """
     self.file = string
     self.table = SymbolTable()
     self.scopestack = []
     self.scope = None
     self.types = {'void', 'int', 'float', 'string'}
     self.reserved = {'if', 'while'}
     self.err = dict()
     self.generate_errors()
     self.expr = dict()
     self.generate_expressions()
예제 #26
0
 def subroutineDec(self, state, ast):
    statelocal = state.copy() #we keep a local frame for each function
    statelocal['sym_tbl'] = SymbolTable(state['sym_tbl']) 
    statelocal['fkind'] = ast.next_val() #constructor, function, method
    statelocal['fype']= ast.next_val() #return type
    statelocal['fname'] = ast.next_val() #function name
    statelocal['cf_count'] = 0 #control flow labels
    #new symbol table with parent linkage
    if statelocal['fkind'] == 'method':
       statelocal['sym_tbl'].var_counter['argument'] += 1
    ast.next() #'('
    self.parameterList(statelocal,ast.next_sec())
    ast.next() #')'
    self.subroutineBody(statelocal, ast.next_sec())
    return
예제 #27
0
    def main():
        filename = sys.argv[1]
        output = open(filename.split('.')[0] + '.hack', 'w')

        firstPass = Parser(filename)
        symbolTable = SymbolTable()
        rom_address = 0
        ramAddress = 16

        # First pass adds L_COMMANDs and ROM addresses to symbol table
        while firstPass.hasMoreCommands():
            firstPass.advance()
            command = firstPass.commandType()
            if command == 'A_COMMAND' or command == 'C_COMMAND':
                rom_address += 1
            elif command == 'L_COMMAND':
                symbolTable.addEntry(firstPass.symbol(), rom_address)

        # When A_COMMAND is encountered:
        #   if symbol is a digit write it to file
        #   if symbol is not a digit, look it up in the symbol table. If it's there, write the address
        #   if symbol is not a digit, look it up in the symbol table. If it is not there, add it then write the address
        secondPass = Parser(filename)
        while secondPass.hasMoreCommands():
            secondPass.advance()
            command = secondPass.commandType()
            symbol = secondPass.symbol()
            if command == 'A_COMMAND' and symbol:
                if symbol.isdigit():
                    output.write('0' + '{0:015b}'.format(int(symbol)) + '\n')
                elif symbolTable.contains(symbol):
                    symbolAddress = symbolTable.getAddress(symbol)
                    output.write('0' + '{0:015b}'.format(int(symbolAddress)) +
                                 '\n')
                else:
                    symbolTable.addEntry(symbol, ramAddress)
                    ramAddress += 1
                    symbolAddress = symbolTable.getAddress(symbol)
                    output.write('0' + '{0:015b}'.format(int(symbolAddress)) +
                                 '\n')
            else:
                dest = Code.dest(secondPass.dest())
                jump = Code.jump(secondPass.jump())
                comp = Code.comp(secondPass.comp())
                if comp != None:
                    output.write('111' + comp + dest + jump + '\n')

        output.close()
예제 #28
0
def test_st_define():
    st = SymbolTable()
    st.define("first", "int", SymbolType.STATIC)
    st.define("second", "SomeClass", SymbolType.FIELD)
    st.define("third", "String", SymbolType.ARG)
    st.define("fourth", "bool", SymbolType.VAR)

    assert (st.classTable == {
        "first": ("int", SymbolType.STATIC, 0),
        "second": ("SomeClass", SymbolType.FIELD, 1),
    })

    assert (st.subroutineTable == {
        "third": ("String", SymbolType.ARG, 0),
        "fourth": ("bool", SymbolType.VAR, 1),
    })
예제 #29
0
 def __init__(self, tokenizer):
     self._name = tokenizer.get_filename().replace('.jack','')
     # tokenizer for input
     self._tokenizer = tokenizer
     # symbol table
     self._symbols = SymbolTable()
     # vm output fiole
     self._writer = VMWriter(self._name + '.vm')
     # Input should be a tokenized .jack file containing one class
     assert self._tokenizer.has_more_tokens()
     self._tokenizer.advance()
     self._class = None
     self._subroutine = None
     self._counter = 0
     self.compile_class()
     self.close()
예제 #30
0
파일: ppparser.py 프로젝트: Plingot/hlakit
    def p_pp_define(self, p):
        '''pp_define : PPDEFINE ID empty
                     | PPDEFINE ID pp_define_body
                     | PPDEFINE ID '(' pp_define_params ')' pp_define_body'''

        name = p[2]
        value = []
        params = None

        if len(p) == 4:
            value = p[3]
        elif len(p) == 7:
            value = p[6]
            params = p[4]

        SymbolTable().new_symbol(name, PPMacro(name, value, params))