Esempio n. 1
0
def notices_stmt(node):

    try:  # try the if-then pattern
        (NOTICES, cond, then_stmt, (NIL, ), STAWP) = node
        assert_match(NOTICES, 'notices')
        assert_match(STAWP, 'stawp')
        assert_match(NIL, 'nil')

    except ValueError:  # if-then pattern didn't match
        (NOTICES, cond, then_stmt, else_stmt, STAWP) = node
        assert_match(NOTICES, 'notices')

        value = walk(cond)

        if value != 0:
            walk(then_stmt)
        else:
            walk(else_stmt)

        assert_match(STAWP, 'stawp')

    else:  # if-then pattern matched
        value = walk(cond)
        if value != 0:
            walk(then_stmt)
Esempio n. 2
0
def put_stmt(node):

    (PUT, exp) = node
    assert_match(PUT, 'put')

    (t, v) = walk(exp)
    print("{}".format(v))
Esempio n. 3
0
def nuzzels_stmt(node):

    (NUZZELS, exp) = node
    assert_match(NUZZELS, 'nuzzels')

    value = walk(exp)
    print("> {}".format(value))
Esempio n. 4
0
def id_exp(node):
    
    (ID, name) = node
    assert_match(ID, 'id')
    
    code = [('push', name)]
    return code
Esempio n. 5
0
def get_stmt(node):

    (GET, name) = node
    assert_match(GET, 'get')

    (sym_type, data_type, *_) = state.symbol_table.lookup_sym(name)

    if sym_type != 'scalar-val':
        raise ValueError(\
            "Cannot assign a value to the name {} in a get"\
            .format(name))

    s = input("Value for " + name + '? ')

    try:
        if data_type == 'integer':
            value = ('scalar-val', 'integer', int(s))
        elif data_type == 'float':
            value = ('scalar-val', 'float', float(s))
        elif data_type == 'string':
            value = ('scalar-val', 'string', s)
    except ValueError:
        raise ValueError(\
            "unexpected value for variable {} of type {}"\
            .format(name, data_type))
    else:
        raise ValueError(\
            "variable {} of type {} and not supported in get statement"\
            .format(name, data_type))

    state.symbol_table.update_sym(name, sym_type, value)
Esempio n. 6
0
def integer_exp(node):

    (INTEGER, value) = node
    assert_match(INTEGER, 'integer')
    
    code = [('push', str(value))]
    return code
Esempio n. 7
0
def seq(node, level):

    (SEQ, stmt, stmt_list) = node
    assert_match(SEQ, 'seq')
    
    walk(stmt, level+1)
    walk(stmt_list, level+1)
Esempio n. 8
0
def while_stmt(node, level):

    (WHILE, cond, body) = node
    assert_match(WHILE, 'while')
    
    walk(cond, level+1)
    walk(body, level+1)
Esempio n. 9
0
def while_stmt(node):

    (WHILE, cond, body) = node
    assert_match(WHILE, 'while')
    
    walk(cond)
    walk(body)
Esempio n. 10
0
def assign_stmt(node):

    (ASSIGN, name, exp) = node
    assert_match(ASSIGN, 'assign')

    (type, value) = walk(exp)
    state.symbol_table.update_sym(name, (type, value))
Esempio n. 11
0
def paren_exp(node):
    
    (PAREN, exp) = node
    assert_match(PAREN, 'paren')
    
    # get rid of parenthesis - not necessary in AST
    return walk(exp)
def seq(node):

    (SEQ, stmt, stmt_list) = node
    assert_match(SEQ, 'seq')

    walk(stmt)
    walk(stmt_list)
Esempio n. 13
0
def print_stmt(node):

    (PRINT, exp) = node
    assert_match(PRINT, 'print')

    (_, v) = walk(exp)
    print(str(v))
def paren_exp(node):

    (PAREN, exp) = node
    assert_match(PAREN, 'paren')

    # return the value of the parenthesized expression
    return walk(exp)
def assign_stmt(node):

    (ASSIGN, name, exp) = node
    assert_match(ASSIGN, 'assign')

    value = walk(exp)
    state.symbol_table[name] = value
def uminus_exp(node):

    (UMINUS, exp) = node
    assert_match(UMINUS, 'uminus')

    val = walk(exp)
    return -val
def not_exp(node):

    (NOT, exp) = node
    assert_match(NOT, 'not')

    val = walk(exp)
    return 0 if val != 0 else 1
Esempio n. 18
0
def nil(node):

    (NIL, ) = node
    assert_match(NIL, 'nil')

    # empty node, so we do nothing
    pass
Esempio n. 19
0
def fundef_stmt(node):
    global frame_size

    (FUNDEF, name, formal_arglist, body, f_frame_size) = node
    assert_match(FUNDEF, 'fundef')

    frame_size = f_frame_size
    ignore_label = label()

    code = [('jump', ignore_label)]
    code += [('#', ' ')]
    code += [('#', 'Start of function ' + name)]
    code += [('#', ' ')]
    code += [(name + ':', )]
    code += [('pushf', str(frame_size))]
    code += init_formal_args(formal_arglist, 0, frame_size)
    code += walk(body)
    code += [('popf', str(frame_size))]
    code += [('return', )]
    code += [('#', ' ')]
    code += [('#', 'End of function ' + name)]
    code += [('#', ' ')]
    code += [(ignore_label + ':', )]
    code += [('noop', )]

    frame_size = None

    return code
Esempio n. 20
0
def print_stmt(node):

    (PRINT, exp) = node
    assert_match(PRINT, 'print')

    value = walk(exp)
    print("{}".format(value))
Esempio n. 21
0
def put_stmt(node):

    (PUT, exp) = node
    assert_match(PUT, 'put')

    value = walk(exp)
    print("> {}".format(value))
Esempio n. 22
0
def put_stmt(node):

    (PUT, exp) = node
    assert_match(PUT, 'put')

    (_, v) = walk(exp)
    print(str(v))
def nil(node):

    (NIL, ) = node
    assert_match(NIL, 'nil')

    # do nothing!
    pass
Esempio n. 24
0
def put_stmt(node):

    (PUT, exp) = node
    assert_match(PUT, 'put')
    
    t = walk(exp)

    return ('put', t)
Esempio n. 25
0
def get_stmt(node):

    (GET, name) = node
    assert_match(GET, 'get')
    
    target_name = state.symbol_table.get_target_name(name)

    return ('get', target_name)
Esempio n. 26
0
def get_stmt(node):

    (GET, name) = node
    assert_match(GET, 'get')

    code = [('input', name)]

    return code
Esempio n. 27
0
def block_stmt(node):

    (BLOCK, stmt_list) = node
    assert_match(BLOCK, 'block')

    state.symbol_table.push_scope()
    walk(stmt_list)
    state.symbol_table.pop_scope()
Esempio n. 28
0
def id_exp(node):
    
    (ID, name) = node
    assert_match(ID, 'id')
    
    target_name = state.symbol_table.get_target_name(name)
    
    return ('id', target_name)
Esempio n. 29
0
def paren_exp(node):

    (PAREN, exp) = node
    assert_match(PAREN, 'paren')

    exp_code = walk(exp)

    return exp_code
Esempio n. 30
0
def block_stmt(node):

    (BLOCK, s) = node
    assert_match(BLOCK, 'block')

    code = walk(s)

    return code