Esempio n. 1
0
def _statement(code: Code,
               statement,
               loop_start: Optional[int] = None,
               breaks: Optional[List[int]] = None):
    node_type = statement['node']

    if node_type == Node.VARIABLE_DEFINITION:
        _statement_var_def(code, statement)
    elif node_type == Node.CONSTANT_DEFINITION:
        _statement_var_def(code, statement)
    elif node_type == Node.VARIABLE_STORE:
        _statement_var_store(code, statement)
    elif node_type == Node.ARRAY_STORE:
        _statement_array_store(code, statement)
    elif node_type == Node.FUNCTION_CALL:
        _statement_function_call(code, statement)
    elif node_type == Node.RETURN:
        _statement_return(code, statement)
    elif node_type == Node.RETURN_VOID:
        code.return_void()
    elif node_type == Node.IF:
        _statement_if(code, statement, loop_start, breaks)
    elif node_type == Node.IF_ELSE:
        _statement_if_else(code, statement, loop_start, breaks)
    elif node_type == Node.WHILE:
        _statement_while(code, statement)
    elif node_type == Node.BREAK:
        break_pos = code.pos()
        code.goto()
        breaks.append(break_pos)
    elif node_type == Node.CONTINUE:
        code.goto(loop_start)
    else:
        raise NotImplementedError(node_type)
Esempio n. 2
0
def _statement_return(code: Code, statement):
    expression = statement['expression']
    t = statement['type']

    if expression:
        _expression(code, expression)

    if isinstance(t, TypeVoid):
        code.return_void()
    elif isinstance(t, TypeInt):
        code.return_int()
    elif isinstance(t, TypeReal):
        code.return_double()
    elif isinstance(t, TypeBool):
        code.return_int()
    elif isinstance(t, TypeStr):
        code.return_reference()
    elif isinstance(t, TypeArray):
        code.return_reference()
    else:
        raise NotImplementedError()