Require boolean in for, if
This commit is contained in:
@@ -5,16 +5,16 @@ pointer_type :: *S64
|
|||||||
if_stmt :: (cond: S64): type
|
if_stmt :: (cond: S64): type
|
||||||
CONSTANT :: 10
|
CONSTANT :: 10
|
||||||
thing := 10
|
thing := 10
|
||||||
if i := thing + cond, cond + CONSTANT
|
if i := thing + cond, cond > CONSTANT
|
||||||
return i + CONSTANT
|
return i + CONSTANT
|
||||||
else if cond - CONSTANT
|
else if cond < CONSTANT
|
||||||
return i - CONSTANT
|
return i - CONSTANT
|
||||||
else
|
else
|
||||||
return CONSTANT
|
return CONSTANT
|
||||||
|
|
||||||
|
|
||||||
for_stmt :: ()
|
for_stmt :: ()
|
||||||
for i := 0, i + 10, i+=1
|
for i := 0, i < 10, i+=1
|
||||||
pass
|
pass
|
||||||
|
|
||||||
add_10 :: (size: S64): S64
|
add_10 :: (size: S64): S64
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ compounds :: ()
|
|||||||
for
|
for
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for i := 0, i, i+=1
|
for i := 0, i == 4, i+=1
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for j:=0, j < 10
|
for j:=0, j < 10
|
||||||
|
|||||||
@@ -349,6 +349,39 @@ resolve_typespec(Ast_Expr *ast, B32 ast_can_be_null){
|
|||||||
return resolved.type_val;
|
return resolved.type_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Operand
|
||||||
|
require_const_int(Ast_Expr *expr, B32 ast_can_be_null){
|
||||||
|
Operand op = resolve_expr(expr);
|
||||||
|
|
||||||
|
if(expr == 0 && ast_can_be_null)
|
||||||
|
return op;
|
||||||
|
else if(expr == 0)
|
||||||
|
parsing_error(expr->pos, "This field cannot be null");
|
||||||
|
|
||||||
|
if(!op.is_const)
|
||||||
|
parsing_error(expr->pos, "Expected a const value");
|
||||||
|
if(!is_int(op.type))
|
||||||
|
parsing_error(expr->pos, "Expected a constant integer got instead %s", docname(op.type));
|
||||||
|
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Operand
|
||||||
|
resolve_and_require_bool(const char *error, Ast_Expr *expr, B32 ast_can_be_null = AST_CANT_BE_NULL){
|
||||||
|
if(!expr){
|
||||||
|
if(ast_can_be_null)
|
||||||
|
return {};
|
||||||
|
else parsing_error(0, "Compiler error: Null expression");
|
||||||
|
}
|
||||||
|
|
||||||
|
Operand op = resolve_expr(expr);
|
||||||
|
if(!is_bool(op.type)){
|
||||||
|
type_error(expr->pos, type_bool, op.type, "%s", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
// @note: Ret is return value of function passed down the stack
|
// @note: Ret is return value of function passed down the stack
|
||||||
// to check if type matches
|
// to check if type matches
|
||||||
function void
|
function void
|
||||||
@@ -390,11 +423,13 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
}
|
}
|
||||||
|
|
||||||
S64 scope = scope_open();
|
S64 scope = scope_open();
|
||||||
|
{
|
||||||
resolve_expr(node->init, ret);
|
resolve_expr(node->init, ret);
|
||||||
resolve_expr(node->cond, ret);
|
resolve_and_require_bool("Conditional in a for loop condition", node->cond, AST_CAN_BE_NULL);
|
||||||
resolve_expr(node->iter, ret);
|
resolve_expr(node->iter, ret);
|
||||||
For(node->block->stmts)
|
For(node->block->stmts)
|
||||||
resolve_stmt(it, ret);
|
resolve_stmt(it, ret);
|
||||||
|
}
|
||||||
scope_close(scope);
|
scope_close(scope);
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -403,9 +438,12 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
For(node->ifs){
|
For(node->ifs){
|
||||||
resolve_stmt(it->init, ret);
|
resolve_stmt(it->init, ret);
|
||||||
S64 scope = scope_open();
|
S64 scope = scope_open();
|
||||||
resolve_expr(it->expr); // @todo: typechecking
|
{
|
||||||
|
// @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL
|
||||||
|
resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL);
|
||||||
For_It(it->block->stmts, jt)
|
For_It(it->block->stmts, jt)
|
||||||
resolve_stmt(jt, ret);
|
resolve_stmt(jt, ret);
|
||||||
|
}
|
||||||
scope_close(scope);
|
scope_close(scope);
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
@@ -421,23 +459,6 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Operand
|
|
||||||
require_const_int(Ast_Expr *expr, B32 ast_can_be_null){
|
|
||||||
Operand op = resolve_expr(expr);
|
|
||||||
|
|
||||||
if(expr == 0 && ast_can_be_null)
|
|
||||||
return op;
|
|
||||||
else if(expr == 0)
|
|
||||||
parsing_error(expr->pos, "This field cannot be null");
|
|
||||||
|
|
||||||
if(!op.is_const)
|
|
||||||
parsing_error(expr->pos, "Expected a const value");
|
|
||||||
if(!is_int(op.type))
|
|
||||||
parsing_error(expr->pos, "Expected a constant integer got instead %s", docname(op.type));
|
|
||||||
|
|
||||||
return op;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Operand
|
function Operand
|
||||||
resolve_lambda(Ast_Lambda *lambda, Sym *sym = 0){
|
resolve_lambda(Ast_Lambda *lambda, Sym *sym = 0){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
|
|||||||
Reference in New Issue
Block a user