Fix big casting bug in CALL, euler.kl is working now too
This commit is contained in:
@@ -368,6 +368,10 @@ gen_ast(Ast *ast){
|
|||||||
gen("// const Bool %s = ", node->name.str);
|
gen("// const Bool %s = ", node->name.str);
|
||||||
gen_value(node->value);
|
gen_value(node->value);
|
||||||
}break;
|
}break;
|
||||||
|
case TYPE_LAMBDA:{
|
||||||
|
gen("// ");
|
||||||
|
gen_lambda(node->name, node->lambda);
|
||||||
|
} break;
|
||||||
invalid_default_case;
|
invalid_default_case;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
euler.kl
27
euler.kl
@@ -3,7 +3,7 @@
|
|||||||
// @todo: Add blocks of stmts that you can simply define inside a function etc.
|
// @todo: Add blocks of stmts that you can simply define inside a function etc.
|
||||||
|
|
||||||
entry :: ()
|
entry :: ()
|
||||||
printf("\n")
|
print_str("\n")
|
||||||
euler1()
|
euler1()
|
||||||
euler3()
|
euler3()
|
||||||
|
|
||||||
@@ -18,7 +18,9 @@ euler1 :: ()
|
|||||||
result += i
|
result += i
|
||||||
else if i % 5 == 0
|
else if i % 5 == 0
|
||||||
result += i
|
result += i
|
||||||
printf("Euler1: %lld\n", result)
|
print_str("Euler1: ")
|
||||||
|
print_int(result)
|
||||||
|
print_str("\n")
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -61,23 +63,16 @@ euler3 :: ()
|
|||||||
if n > 2
|
if n > 2
|
||||||
results[results_len++] = n
|
results[results_len++] = n
|
||||||
|
|
||||||
printf("Euler3: ")
|
print_str("Euler3: ")
|
||||||
|
|
||||||
is_correct: S64 = 1
|
is_correct: S64 = 1
|
||||||
for i := 0, i < results_len, i++
|
for i := 0, i < results_len, i++
|
||||||
is_correct = is_correct * results[i]
|
is_correct = is_correct * results[i]
|
||||||
printf("%lld ", results[i])
|
print_int(is_correct)
|
||||||
|
|
||||||
printf(":: %lld", is_correct)
|
print_str(":: ")
|
||||||
|
print_int(is_correct)
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
sqrt :: (v: F64): F64 #foreign
|
||||||
// Euler 4
|
print_int :: (i: S64) #foreign
|
||||||
//-----------------------------------------------------------------------------
|
print_str :: (i: String) #foreign
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print_int :: (i: S64)
|
|
||||||
printf("%lld ", i)
|
|
||||||
|
|
||||||
#foreign sqrt :: (v: F64): F64
|
|
||||||
#foreign printf :: (str: String, ...)
|
|
||||||
1
main.cpp
1
main.cpp
@@ -212,6 +212,7 @@ int main(int argument_count, char **arguments){
|
|||||||
files.add("new_types.kl"_s);
|
files.add("new_types.kl"_s);
|
||||||
files.add("enums.kl"_s);
|
files.add("enums.kl"_s);
|
||||||
files.add("globals.kl"_s);
|
files.add("globals.kl"_s);
|
||||||
|
files.add("euler.kl"_s);
|
||||||
String result = compile_files(files);
|
String result = compile_files(files);
|
||||||
printf("%s", result.str);
|
printf("%s", result.str);
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
|
|||||||
26
parsing.cpp
26
parsing.cpp
@@ -183,14 +183,14 @@ parse_optional_type(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Scope *
|
function Ast_Scope *
|
||||||
parse_stmt_scope(){
|
parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
|
||||||
Ast_Scope *scope = 0;
|
Ast_Scope *scope = scope_defined_outside;
|
||||||
|
|
||||||
if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context
|
if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context
|
||||||
Token *token_block = token_get();
|
Token *token_block = token_get();
|
||||||
|
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
scope = begin_stmt_scope(scratch, token_block);
|
if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block);
|
||||||
do{
|
do{
|
||||||
Token *token = token_get();
|
Token *token = token_get();
|
||||||
if(token_match_keyword(keyword_return)){
|
if(token_match_keyword(keyword_return)){
|
||||||
@@ -204,6 +204,7 @@ parse_stmt_scope(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if(token_match_keyword(keyword_for)){
|
else if(token_match_keyword(keyword_for)){
|
||||||
|
Ast_Scope *for_scope = begin_stmt_scope(scratch, token_get());
|
||||||
Ast_Expr *init = 0;
|
Ast_Expr *init = 0;
|
||||||
Ast_Expr *cond = 0;
|
Ast_Expr *cond = 0;
|
||||||
Ast_Expr *iter = 0;
|
Ast_Expr *iter = 0;
|
||||||
@@ -224,8 +225,9 @@ parse_stmt_scope(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Scope *for_block = parse_stmt_scope();
|
parse_stmt_scope(for_scope);
|
||||||
scope->stmts.add(ast_for(token, init, cond, iter, for_block));
|
finalize_stmt_scope(for_scope);
|
||||||
|
scope->stmts.add(ast_for(token, init, cond, iter, for_scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(token_match_keyword(keyword_if)){
|
else if(token_match_keyword(keyword_if)){
|
||||||
@@ -281,7 +283,7 @@ parse_stmt_scope(){
|
|||||||
} while(token_match(SAME_SCOPE));
|
} while(token_match(SAME_SCOPE));
|
||||||
token_expect(CLOSE_SCOPE);
|
token_expect(CLOSE_SCOPE);
|
||||||
|
|
||||||
finalize_stmt_scope(scope);
|
if(!scope_defined_outside) finalize_stmt_scope(scope);
|
||||||
}
|
}
|
||||||
return scope;
|
return scope;
|
||||||
}
|
}
|
||||||
@@ -318,10 +320,11 @@ parse_lambda(Token *token){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
token_expect(TK_CloseParen);
|
token_expect(TK_CloseParen);
|
||||||
|
Ast_Expr *ret = parse_optional_type();
|
||||||
Ast_Expr *ret = parse_optional_type();
|
Token *foreign = token_match(TK_FOREIGN);
|
||||||
Ast_Scope *scope = token_is(OPEN_SCOPE) ? parse_stmt_scope() : 0;
|
Ast_Scope *scope = token_is(OPEN_SCOPE) ? parse_stmt_scope() : 0;
|
||||||
Ast_Lambda *result = ast_lambda(token, params, ret, scope);
|
Ast_Lambda *result = ast_lambda(token, params, ret, scope);
|
||||||
|
if(foreign) set_flag(result->flags, AST_FOREIGN);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,7 +511,6 @@ function Ast_Decl *
|
|||||||
parse_struct(Token *pos){
|
parse_struct(Token *pos){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
|
|
||||||
|
|
||||||
token_match(OPEN_SCOPE);
|
token_match(OPEN_SCOPE);
|
||||||
Ast_Scope *scope = begin_decl_scope(scratch, token_get());
|
Ast_Scope *scope = begin_decl_scope(scratch, token_get());
|
||||||
do{
|
do{
|
||||||
@@ -565,11 +567,6 @@ parse_decl(B32 is_global){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Flag flags = 0;
|
|
||||||
if(token_match(TK_FOREIGN)){
|
|
||||||
flags = set_flag(flags, AST_FOREIGN);
|
|
||||||
}
|
|
||||||
|
|
||||||
Token *tname = token_get();
|
Token *tname = token_get();
|
||||||
if(token_match(TK_Identifier, TK_DoubleColon)){
|
if(token_match(TK_Identifier, TK_DoubleColon)){
|
||||||
// @note parse struct binding
|
// @note parse struct binding
|
||||||
@@ -610,7 +607,6 @@ parse_decl(B32 is_global){
|
|||||||
|
|
||||||
if(result){
|
if(result){
|
||||||
result->name = tname->intern_val;
|
result->name = tname->intern_val;
|
||||||
result->flags = set_flag(result->flags, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -779,8 +779,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){
|
|||||||
S64 was_name_indexed = false;
|
S64 was_name_indexed = false;
|
||||||
S64 default_iter = 0;
|
S64 default_iter = 0;
|
||||||
|
|
||||||
Ast_Decl *decl = (Ast_Decl *)name.type->ast;
|
Ast_Lambda *lambda = (Ast_Lambda *)name.type->ast;
|
||||||
Ast_Lambda *lambda = decl->lambda;
|
|
||||||
for(S64 i = 0; i < lambda->args.len; i++){
|
for(S64 i = 0; i < lambda->args.len; i++){
|
||||||
Ast_Decl *lambda_arg = lambda->args[i];
|
Ast_Decl *lambda_arg = lambda->args[i];
|
||||||
assert(lambda_arg->type);
|
assert(lambda_arg->type);
|
||||||
@@ -829,7 +828,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){
|
|||||||
else unset_flag(it->flags, AST_ITEM_INCLUDED);
|
else unset_flag(it->flags, AST_ITEM_INCLUDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
return operand_rvalue(name.type);
|
return operand_rvalue(name.type->func.ret);
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -877,6 +876,8 @@ resolve_decl(Ast_Decl *ast){
|
|||||||
node->value = op.value;
|
node->value = op.value;
|
||||||
if(op.value.type == type_type){
|
if(op.value.type == type_type){
|
||||||
node->kind = AST_TYPE;
|
node->kind = AST_TYPE;
|
||||||
|
} else if(is_lambda(op.value.type)){
|
||||||
|
node->kind = AST_LAMBDA;
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user