Exemplo n.º 1
0
 def p_is_array_term(p):
     '''is_array_term : is_array_term '[' expression ']'
                     | is_array_term '.' ID
                     | ID '''
     if len(p) > 4:
         if type(p[1]) == leaf:
             new_branch = branch()
             new_branch.setType("ACCESS")
             new_branch.add(p[1])
             new_branch.add(p[3])
             p[0] = new_branch
         else:
             new_branch = p[1]
             new_branch.add(p[3])
             p[0] = new_branch
     elif len(p) > 2:
         if type(p[1]) == leaf:
             new_branch = branch()
             new_branch.setType("ACCESS")
             new_branch.add(p[1])
             new_branch.add(leaf(p[3], "STRING"))
             p[0] = new_branch
         else:
             new_branch = p[1]
             new_branch.add(leaf(p[3], "STRING"))
             p[0] = new_branch
     else:
         p[0] = leaf(p[1], "ID")
Exemplo n.º 2
0
 def p_statement_group(p):
     '''statement : IF '(' expression ')' GOTO LABEL
                  | UNSET '(' term ')'
                  | GOTO LABEL
                  | EXIT                          '''
     global sym_table
     new_branch = branch()
     if len(p) > 5:
         new_branch.add(p[3])
         r_leaf = leaf(p[6], "LABEL")
         new_branch.add(r_leaf)
         new_branch.setType("IF")
         sym_table.appendGrammar(
             7, 'statement -> IF ( expression ) GOTO LABEL ')
     elif len(p) > 3:
         new_branch.add(p[3])
         new_branch.setType("UNSET")
         sym_table.appendGrammar(8, 'statement -> UNSET ( term ) ')
     elif len(p) > 2:
         r_leaf = leaf(p[2], "LABEL")
         new_branch.add(r_leaf)
         new_branch.setType("GOTO")
         sym_table.appendGrammar(9, 'statement -> GOTO LABEL ')
     else:
         new_branch.setType("EXIT")
         sym_table.appendGrammar(10, 'statement -> EXIT ')
     p[0] = new_branch
Exemplo n.º 3
0
    def p_sub_decl(p):
        '''sub_decl : ID "=" expression
                    | ID "=" READ "(" ")"
                    | decl_array "=" "{" expression_list "}"
                    | decl_index "=" "{" expression_list "}"
                    | decl_index "=" expression
                    | decl_index "=" READ "(" ")"
                    | decl_index
                    | decl_array
                    | ID '''
        global sym_table
        new_branch = branch()
        if len(p) == 4:
            if type(p[1]) == branch:
                #TODO assign to index of array
                pass
            else:
                l_leaf = leaf(p[1], "ID")
                r_leaf = p[3]

                new_branch.add(l_leaf)
                new_branch.add(r_leaf)
                new_branch.setType("ASSIGN")

                sym_table.appendGrammar(
                    5, 'statement -> is_array_term = expression')
        elif len(p) > 3:
            if p[4] == '(':
                l_leaf = leaf(p[1], "ID")
                r_leaf = leaf("read()", "READ")

                new_branch.add(l_leaf)
                new_branch.add(r_leaf)
                new_branch.setType("ASSIGN")
            else:
                l_leaf = p[1]
                r_leaf = p[4]
                r_leaf.setType("VARRAY")

                new_branch.add(l_leaf)
                new_branch.add(r_leaf)
                new_branch.setType("ASSIGN")
        else:
            if type(p[1]) == branch:
                #TODO assign to index of array
                pass
            else:
                new_branch = leaf(p[1], "ID")

        p[0] = new_branch
Exemplo n.º 4
0
    def p_arg(p):
        ''' arg : type ID
                | type "&" ID '''

        new_branch = branch()
        new_branch.add(leaf(p[1], p[1]))
        if len(p) == 3:
            new_branch.add(leaf(p[2], "ID"))
        else:
            new_branch.add(leaf(p[3], "ID"))
            new_branch.add(leaf("&", "POINT"))

        new_branch.setType("DECLARE")
        p[0] = new_branch
Exemplo n.º 5
0
 def p_decl_index(p):
     '''decl_index : decl_index "[" term "]"
                 | ID "[" term "]" '''
     if type(p[1]) == leaf:
         p[0] = leaf(p[1], "ID")
     else:
         p[0] = p[1]
Exemplo n.º 6
0
def READ(node, sym_table):
    # lookup the last line
    index = sym_table.terminal.search(r'\n', "insert", backwards=True, regexp=True)
    txt = sym_table.terminal.get(str(index),'end-1c')
    if txt == "":
        index ="1.0"
    index = sym_table.terminal.index("%s+1c" % index)

    # print before start waiting
    sym_table.terminal.insert(str(float(index)+1), sym_table.getLog())
    sym_table.cleanLog()
    sym_table.terminal.focus_set()

    # wait input from terminal
    x = Label()
    global read_input
    x.wait_variable(sym_table.read_input)
    value = sym_table.read_input.get()
    typ = ''
    try:
        try:
            value = int(value)
            typ = 'NUM'
        except:
            value = float(value)
            typ = 'FLOAT'
    except:
        typ = 'STRING'
    
    return leaf(value, typ)
Exemplo n.º 7
0
    def p_unary_exp(p):
        '''unary_expr : "+" "+" is_array_term %prec PRE
                    | "-" "-" is_array_term %prec PRE
                    | is_array_term "+" "+" %prec POST
                    | is_array_term "-" "-" %prec POST '''

        r_leaf = leaf("1", "INT")
        global sym_table
        new_branch = branch()
        if p[1] == '+':
            l_leaf = p[3]
            new_branch.setType("ADD")
            sym_table.appendGrammar(12, 'unary_expr -> ++ is_array_term')
        elif p[1] == '-':
            l_leaf = p[3]
            new_branch.setType("SUB")
            sym_table.appendGrammar(12, 'unary_expr -> -- is_array_term')
        # TODO create an special node type that indicates ADD and SUB will occur in the next block
        elif p[2] == '+':
            l_leaf = p[1]
            new_branch.setType("ADD")
            sym_table.appendGrammar(12, 'unary_expr -> is_array_term ++')
        elif p[2] == '-':
            l_leaf = p[1]
            new_branch.setType("SUB")
            sym_table.appendGrammar(12, 'unary_expr -> is_array_term --')

        new_branch.add(l_leaf)
        new_branch.add(r_leaf)

        new_branch2 = branch()
        new_branch2.add(l_leaf)
        new_branch2.add(new_branch)
        new_branch2.setType("ASSIGN")
        p[0] = new_branch2
Exemplo n.º 8
0
    def p_function(p):
        '''function : type ID "(" argument_list  "{" compound_statement
                    | VOID ID "(" argument_list  "{" compound_statement'''
        new_branch = branch()
        new_branch.setType("FUNCTION")
        # add type
        new_branch.add(leaf(p[1], p[1].upper()))
        # add ID
        new_branch.add(leaf(p[2], "ID"))
        # add new scope
        new_branch.add(p[6])
        # if has add arguments
        if p[4] != None:
            new_branch.add(p[4])

        p[0] = new_branch
Exemplo n.º 9
0
 def p_decl_array(p):
     '''decl_array : decl_array "[" "]"
                 | ID "[" "]" '''
     if type(p[1]) == leaf:
         p[0] = leaf(p[1], "ID")
     else:
         p[0] = p[1]
Exemplo n.º 10
0
 def p_expression_string(p):
     'factor : STRING'
     string = p[1].replace("'", "")
     string = string.replace("\"", "")
     l_leaf = leaf(string, "STRING")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(44, 'factor -> STRING')
Exemplo n.º 11
0
    def p_struct(p):
        'struct : STRUCT ID "{" assigment_list "}" ";" '
        new_branch = branch()
        new_branch.setType("STRUCT")

        new_branch.add(leaf(p[2], "ID"))
        new_branch.add(p[4])
        p[0] = new_branch
Exemplo n.º 12
0
    def p_term_group(p):
        '''expression : '(' INT ')' factor
                    | '(' FLOAT ')' factor
                    | '(' CHAR ')' factor
                    | '(' factor ')'
                    | '~' factor
                    | '!' factor
                    | '&' VAR
                    | ABS '(' factor ')'
                    | ARRAY '(' ')'
                    | READ '(' ')'           
                                '''
        global sym_table
        new_branch = branch()
        if p[1] == '(':
            l_leaf = p[4]
            new_branch.add(l_leaf)
            if p[2] == 'int':
                new_branch.setType("TOINT")
                sym_table.appendGrammar(32, 'expression -> ( INT ) factor')
            elif p[2] == 'float':
                new_branch.setType("TOFLOAT")
                sym_table.appendGrammar(33, 'expression -> ( FLOAT ) factor')
            elif p[2] == 'char':
                new_branch.setType("TOCHAR")
                sym_table.appendGrammar(34, 'expression -> ( CHAR ) factor')
            else:
                new_branch = p[2]
                sym_table.appendGrammar(35, 'expression -> ( factor )')
        elif p[1] == '~':
            l_leaf = p[2]
            new_branch.add(l_leaf)
            new_branch.setType("BNOT")
            sym_table.appendGrammar(36, 'expression -> ~ factor')
        elif p[1] == '!':
            l_leaf = p[2]
            new_branch.add(l_leaf)
            new_branch.setType("NOT")
            sym_table.appendGrammar(37, 'expression -> ! factor')
        elif p[1] == '&':
            l_leaf = leaf(p[2], "ID")
            new_branch.add(l_leaf)
            new_branch.setType("POINT")
            sym_table.appendGrammar(38, 'expression -> & factor')
        elif p[1] == 'abs':
            l_leaf = p[3]
            new_branch.add(l_leaf)
            new_branch.setType("ABS")
            sym_table.appendGrammar(39, 'expression -> ABS ( factor )')
        elif p[1] == 'array':
            new_branch.setType("ARRAY")
            sym_table.appendGrammar(40, 'expression -> ARRAY ( )')
        elif p[1] == 'read':
            new_branch.setType("READ")
            sym_table.appendGrammar(41, 'expression -> READ ( )')

        p[0] = new_branch
Exemplo n.º 13
0
    def p_function_call(p):
        'function_call : ID "(" parentheses_expression'
        new_branch = branch()
        new_branch.setType("CALL")
        new_branch.add(leaf(p[1], "ID"))
        if p[3] != None:
            new_branch.add(p[3])

        p[0] = new_branch
Exemplo n.º 14
0
    def p_term_group(p):
        '''expression : '(' INT ')' expression %prec CAST
                    | '(' FLOAT ')' expression %prec CAST
                    | '(' CHAR ')' expression %prec CAST
                    | '(' ID ')' expression %prec CAST
                    | '(' expression ')'
                    | '~' expression
                    | '!' expression %prec NOT_PRE
                    | '&' ID %prec POINT
                    | expression '?' expression ':' expression
                    | function_call '''

        global sym_table
        new_branch = branch()
        if p[1] == '(':
            if len(p) > 4:
                l_leaf = p[4]
                new_branch.add(l_leaf)
                if p[2] == 'int':
                    new_branch.setType("TOINT")
                    sym_table.appendGrammar(
                        32, 'expression -> ( INT ) expression')
                elif p[2] == 'float':
                    new_branch.setType("TOFLOAT")
                    sym_table.appendGrammar(
                        33, 'expression -> ( FLOAT ) expression')
                elif p[2] == 'char':
                    new_branch.setType("TOCHAR")
                    sym_table.appendGrammar(
                        34, 'expression -> ( CHAR ) expression')
                else:
                    new_branch.setType("TOID")
                    sym_table.appendGrammar(34,
                                            'expression -> ( ID ) expression')
            else:
                new_branch = p[2]
                sym_table.appendGrammar(35, 'expression -> ( expression )')
        elif p[1] == '~':
            l_leaf = p[2]
            new_branch.add(l_leaf)
            new_branch.setType("BNOT")
            sym_table.appendGrammar(36, 'expression -> ~ expression')
        elif p[1] == '!':
            l_leaf = p[2]
            new_branch.add(l_leaf)
            new_branch.setType("NOT")
            sym_table.appendGrammar(37, 'expression -> ! expression')
        elif p[1] == '&':
            l_leaf = leaf(p[2], "ID")
            new_branch.add(l_leaf)
            new_branch.setType("POINT")
            sym_table.appendGrammar(38, 'expression -> & ID')
        else:
            new_branch = p[1]
            sym_table.appendGrammar(38.1, 'expression -> function_call')

        p[0] = new_branch
Exemplo n.º 15
0
 def p_term_decimal(p):
     'term : NUMBER "." NUMBER '
     powder = p[3]
     for i in range(len(str(p[3]))):
         powder = powder / 10
     double = float(p[1]) + powder
     l_leaf = leaf(double, "FLOAT")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(43, 'term -> NUMBER "." NUMBER')
Exemplo n.º 16
0
 def p_expression_decimal(p):
     'factor : NUMBER "." NUMBER'
     powder = p[3]
     for i in range(len(str(p[3]))):
         powder = powder / 10
     double = float(p[1]) + powder
     l_leaf = leaf(double, "FLOAT")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(43, 'factor -> DECIMAL')
Exemplo n.º 17
0
 def p_term_uminus(p):
     "term : '-' factor %prec UMINUS"
     l_leaf = p[2]
     r_leaf = leaf(-1, "NUM")
     global sym_table
     new_branch = branch()
     new_branch.add(l_leaf)
     new_branch.add(r_leaf)
     new_branch.setType("MUL")
     sym_table.appendGrammar(31, 'term -> - factor')
     p[0] = new_branch
Exemplo n.º 18
0
 def p_expression_uminus(p):
     "expression : '-' expression %prec UMINUS"
     l_leaf = p[2]
     r_leaf = leaf(-1, "INT")
     global sym_table
     new_branch = branch()
     new_branch.add(l_leaf)
     new_branch.add(r_leaf)
     new_branch.setType("MUL")
     sym_table.appendGrammar(31, 'term -> - expression')
     p[0] = new_branch
Exemplo n.º 19
0
    def p_declare_struct(p):
        '''declare_struct : declare_struct "," decl_index 
                        | declare_struct "," ID
                        | STRUCT ID decl_index
                        | STRUCT ID ID '''
        new_branch = branch()
        new_branch.setType("ARRAY")
        if p[1] == 'struct':
            new_branch.add(leaf(p[2], "ID"))
            if type(p[3]) == str:
                new_branch.add(leaf(p[3], "ID"))
            else:
                new_branch.add(p[3])
        else:
            new_branch = p[1]
            if type(p[3]) == str:
                new_branch.add(leaf(p[3], "ID"))
            else:
                new_branch.add(p[3])

        p[0] = new_branch
Exemplo n.º 20
0
    def p_declaration_statement(p):
        '''declaration : declaration "," sub_decl
		            | type sub_decl '''
        if len(p) == 3:
            new_branch = branch()
            new_branch.add(leaf(p[1], p[1]))
            new_branch.add(p[2])
            new_branch.setType("DECLARE")
            p[0] = new_branch
        else:
            new_branch = p[1]
            new_branch.add(p[3])
            p[0] = new_branch
Exemplo n.º 21
0
    def p_native_statement(p):
        '''native_statement : PRINT "(" STRING "," expression_list ")" ";"
                            | PRINT "(" STRING ")" ";"
                            | GOTO ID ";"
                            | ID ":"  '''
        new_branch = branch()
        if len(p) > 6:
            new_branch.setType("PRINT")
            new_branch.add(leaf(p[3], "STRING"))
            p[5].setType("SCOPE")
            p[5].setValue("EXPS")
            new_branch.add(p[5])
        elif len(p) > 4:
            new_branch.setType("PRINT")
            new_branch.add(leaf(p[3], "STRING"))
        elif len(p) > 3:
            new_branch.setType("GOTO")
            new_branch.add(leaf(p[2], "ID"))
        else:
            new_branch.setType("LABEL")
            new_branch.add(leaf(p[1], "ID"))

        p[0] = new_branch
Exemplo n.º 22
0
def TOVALUE(value, sym_table):
    try:
        if value.getType() == leaf.TYPE["ID"]:
            return value.execute(sym_table)
        elif value.getType() == leaf.TYPE["ACCESS"]:
            result = value.execute(sym_table)
            index = result.getRef()
            array = result.getValue()
            value = array[index]
            return leaf(array[index], TYPEOF(value))
        elif type(value) == sym:
            if value.getType() == leaf.TYPE["STRUCT"]:
                index = value.getRef()
                array = value.getValue()
                return array[index]
            return value.getValue()
        elif value.getType() > 5:
            value = value.execute(sym_table)
            value = leaf(value, TYPEOF(value))
        return value
    except:
        # its the value itself
        return value
Exemplo n.º 23
0
    def p_term_array(p):
        '''is_array_term : is_array_term '[' factor ']'
	  	                | VAR
                                                '''
        global sym_table
        if len(p) > 2:
            new_branch = branch()
            new_branch.add(p[1])
            new_branch.add(p[3])
            new_branch.setType("ACCESS")
            p[0] = new_branch
            sym_table.appendGrammar(
                45, 'is_array_term -> is_array_term [ factor ]')
        else:
            l_leaf = leaf(p[1], "ID")
            p[0] = l_leaf
            sym_table.appendGrammar(46, 'is_array_term -> VAR')
Exemplo n.º 24
0
 def p_expression_decimal(p):
     'factor : DECIMAL'
     l_leaf = leaf(p[1], "FLOAT")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(43, 'factor -> DECIMAL')
Exemplo n.º 25
0
def TOCHAR(node, sym_table):
    return leaf(CONVERT('char', node, sym_table), "STRING")
Exemplo n.º 26
0
def TOFLOAT(node, sym_table):
    return leaf(CONVERT('float', node, sym_table), "FLOAT")
Exemplo n.º 27
0
def TOINT(node, sym_table):
    return leaf(CONVERT('int', node, sym_table), "NUM")
Exemplo n.º 28
0
 def p_term_string(p):
     'term : STRING'
     l_leaf = leaf(p[1], "STRING")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(44, 'factor -> STRING')
Exemplo n.º 29
0
 def p_term_number(p):
     'term : NUMBER'
     l_leaf = leaf(p[1], "INT")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(42, 'term -> NUMBER')
Exemplo n.º 30
0
 def p_expression_number(p):
     'factor : NUMBER'
     l_leaf = leaf(p[1], "NUM")
     p[0] = l_leaf
     global sym_table
     sym_table.appendGrammar(42, 'factor -> NUMBER')