Skip to content

Commit

Permalink
#245 sync upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Nov 6, 2022
1 parent 53436ab commit 7a58cef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ravicomp/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ static AstNode *allocate_expr_ast_node(ParserState *parser, enum AstNodeType typ
return node;
}

AstNode *raviX_cast_to_integer(CompilerState *compiler_state, AstNode *subexpr) {
AstNode *expr = raviX_allocate_ast_node_at_line(compiler_state, EXPR_UNARY, subexpr->line_number);
expr->unary_expr.expr = subexpr;
expr->unary_expr.unary_op = UNOPR_TO_INTEGER;
subexpr->common_expr.truncate_results = 1;
set_typecode(&expr->common_expr.type, RAVI_TNUMINT);
return expr;
}

static void error_expected(LexerState *ls, int token)
{
raviX_token2str(token, &ls->compiler_state->error_message);
Expand Down
1 change: 1 addition & 0 deletions ravicomp/src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ typedef struct ParserState {
void raviX_print_ast_node(TextBuffer *buf, AstNode *node, int level); /* output the AST structure recursively */
void raviX_dump_ast_node(TextBuffer *buf, AstNode *node, int level);
const char *raviX_get_type_name(ravitype_t tt);
AstNode *raviX_cast_to_integer(CompilerState *compiler_state, AstNode *subexpr);

int raviX_ast_simplify(CompilerState* compiler_state);
int raviX_ast_lower(CompilerState *compiler_state);
Expand Down
16 changes: 13 additions & 3 deletions ravicomp/src/typechecker.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ static void typecheck_for_num_statment(CompilerState *compiler_state, AstNode *f
if ((index_type & A) != 0)
break;
}
END_FOR_EACH_PTR(expr);
END_FOR_EACH_PTR(expr)
if ((index_type & A) == 0) { /* not any */
/* for I+F we use F */
ravitype_t symbol_type = index_type == I ? RAVI_TNUMINT : RAVI_TNUMFLT;
Expand All @@ -466,7 +466,17 @@ static void typecheck_for_num_statment(CompilerState *compiler_state, AstNode *f
assert(0); /* cannot happen */
}
}
END_FOR_EACH_PTR(sym);
END_FOR_EACH_PTR(sym)
}
else {
FOR_EACH_PTR(node->for_stmt.expr_list, AstNode, expr)
{
if (expr->common_expr.type.type_code != RAVI_TNUMINT) {
AstNode *cast_to_int_expr = raviX_cast_to_integer(compiler_state, expr);
REPLACE_CURRENT_PTR(AstNode, expr, cast_to_int_expr);
}
}
END_FOR_EACH_PTR(expr)
}
typecheck_ast_list(compiler_state, function, node->for_stmt.for_statement_list);
}
Expand All @@ -479,7 +489,7 @@ static void typecheck_if_statement(CompilerState *compiler_state, AstNode *funct
typecheck_ast_node(compiler_state, function, test_then_block->test_then_block.condition);
typecheck_ast_list(compiler_state, function, test_then_block->test_then_block.test_then_statement_list);
}
END_FOR_EACH_PTR(node);
END_FOR_EACH_PTR(node)
if (node->if_stmt.else_statement_list) {
typecheck_ast_list(compiler_state, function, node->if_stmt.else_statement_list);
}
Expand Down

0 comments on commit 7a58cef

Please sign in to comment.