Passing arrays as arguments to functions working
This commit is contained in:
58
ccodegen.cpp
58
ccodegen.cpp
@@ -2,6 +2,7 @@
|
||||
#define gen(...) pctx->gen.addf(__VA_ARGS__)
|
||||
#define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0)
|
||||
global S32 global_indent;
|
||||
global S32 is_inside_struct;
|
||||
|
||||
function void
|
||||
gen_indent(){
|
||||
@@ -253,6 +254,40 @@ gen_block(Ast_Block *block){
|
||||
genln("}");
|
||||
}
|
||||
|
||||
enum {
|
||||
ALWAYS_EMIT_VALUE = 0,
|
||||
DONT_EMIT_VALUE = 1,
|
||||
};
|
||||
|
||||
function void
|
||||
gen_var(Intern_String name, Ast_Resolved_Type *type, Ast_Expr *expr, B32 emit_value){
|
||||
if(is_array(type)){
|
||||
gen("Slice %s", name.str);
|
||||
} else{
|
||||
gen_simple_decl(type, name);
|
||||
}
|
||||
|
||||
if(emit_value == DONT_EMIT_VALUE){
|
||||
return;
|
||||
}
|
||||
|
||||
if(expr){
|
||||
gen(" = ");
|
||||
gen_expr(expr);
|
||||
} else if(is_array(type)){
|
||||
gen(" = (Slice){%d, (", type->arr.size);
|
||||
gen_simple_decl(type, {});
|
||||
gen("){}");
|
||||
gen("}");
|
||||
} else { // Default zero
|
||||
if(is_numeric(type)){
|
||||
gen(" = 0");
|
||||
} else {
|
||||
gen(" = {}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
gen_ast(Ast *ast){
|
||||
switch(ast->kind){
|
||||
@@ -280,24 +315,7 @@ gen_ast(Ast *ast){
|
||||
|
||||
CASE(VAR, Var){
|
||||
Sym *sym = resolved_get(node);
|
||||
|
||||
if(is_array(sym->type)){
|
||||
gen("Slice %s", node->name.str);
|
||||
} else{
|
||||
gen_simple_decl(sym->type, node->name);
|
||||
}
|
||||
|
||||
if(node->expr){
|
||||
gen(" = ");
|
||||
gen_expr(node->expr);
|
||||
} else if(is_array(sym->type)){
|
||||
gen(" = (Slice){%d, (", sym->type->arr.size);
|
||||
gen_simple_decl(sym->type, {});
|
||||
gen("){}");
|
||||
gen("}");
|
||||
} else { // Default zero
|
||||
gen(" = {}");
|
||||
}
|
||||
gen_var(sym->name, sym->type, node->expr, is_inside_struct ? DONT_EMIT_VALUE : ALWAYS_EMIT_VALUE);
|
||||
gen(";");
|
||||
BREAK();
|
||||
}
|
||||
@@ -381,7 +399,7 @@ gen_ast(Ast *ast){
|
||||
For(lambda->args){
|
||||
assert(it->kind == AST_LAMBDA_ARG);
|
||||
Ast_Resolved_Type *type = resolved_type_get(it->typespec);
|
||||
gen_simple_decl(type, it->name);
|
||||
gen_var(it->name, type, 0, DONT_EMIT_VALUE);
|
||||
if(&it != (lambda->args.end() - 1)) gen(", ");
|
||||
}
|
||||
gen(")");
|
||||
@@ -405,6 +423,7 @@ gen_ast(Ast *ast){
|
||||
if(node->value->kind == AST_STRUCT){
|
||||
gen("typedef struct %s{", node->name.str);
|
||||
global_indent++;
|
||||
is_inside_struct++;
|
||||
For(agg->members){
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
@@ -414,6 +433,7 @@ gen_ast(Ast *ast){
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
is_inside_struct--;
|
||||
global_indent--;
|
||||
genln("}%s;", node->name.str);
|
||||
}
|
||||
|
||||
40
program.c
40
program.c
@@ -35,10 +35,7 @@ int main(){
|
||||
entry();
|
||||
}
|
||||
|
||||
#line 0 "program.kl"
|
||||
#line 1
|
||||
|
||||
#line 3
|
||||
typedef struct Token{
|
||||
U8 kind;
|
||||
U8 *str;
|
||||
@@ -47,61 +44,40 @@ typedef struct Token{
|
||||
Number = 0,
|
||||
};*/
|
||||
}Token;
|
||||
#line 12
|
||||
String kind_name(U8 kind){
|
||||
#line 15
|
||||
if((kind==0)){
|
||||
#line 14
|
||||
return LIT("<Number>");
|
||||
}
|
||||
else{
|
||||
#line 16
|
||||
return LIT("<Unknown>");
|
||||
}
|
||||
}
|
||||
#line 18
|
||||
Bool is_numeric(U8 c){
|
||||
#line 19
|
||||
Bool result = ((c>=48)&&(c<=57));
|
||||
#line 20
|
||||
return result;
|
||||
}
|
||||
#line 22
|
||||
void print_tokens(Slice tokens){
|
||||
for(S64 i = 0;(i<tokens.len);(i++)){
|
||||
Token *t = (&(((Token *)tokens.data)[i]));
|
||||
printf("%d. %.*s", i, ((S32 )t->len), t->str);
|
||||
}
|
||||
}
|
||||
void entry(){
|
||||
#line 23
|
||||
String string_to_lex = LIT("Identifier 2425525 Not_Number");
|
||||
#line 24
|
||||
Slice token_array = (Slice){32, (Token [32]){}};
|
||||
#line 25
|
||||
S64 token_count = 0;
|
||||
#line 27
|
||||
Token t;
|
||||
#line 28
|
||||
Token t = {};
|
||||
for(S64 i = 0;(i<string_to_lex.len);i+=1){
|
||||
#line 29
|
||||
if(is_numeric((string_to_lex.str[i]))){
|
||||
#line 30
|
||||
t.kind=0;
|
||||
#line 31
|
||||
t.str=(&(string_to_lex.str[i]));
|
||||
#line 32
|
||||
t.len=i;
|
||||
#line 33
|
||||
for(;is_numeric((string_to_lex.str[i]));){
|
||||
#line 34
|
||||
i+=1;
|
||||
}
|
||||
#line 35
|
||||
t.len=(i-t.len);
|
||||
#line 36
|
||||
(((Token *)token_array.data)[(token_count++)])=t;
|
||||
}
|
||||
}
|
||||
#line 38
|
||||
for(S64 i = 0;(i<token_count);(i++)){
|
||||
#line 39
|
||||
Token *tk = (&(((Token *)token_array.data)[i]));
|
||||
#line 40
|
||||
printf("%.*s", ((S32 )tk->len), tk->str);
|
||||
}
|
||||
print_tokens(token_array);
|
||||
}
|
||||
@@ -19,6 +19,11 @@ is_numeric :: (c: U8): Bool
|
||||
result := c >= '0 && c <= '9
|
||||
return result
|
||||
|
||||
print_tokens :: (tokens: []Token)
|
||||
for i := 0, i < tokens.len, i++
|
||||
t := &tokens[i]
|
||||
printf("%d. %.*s", i, cast(t.len: S32), t.str)
|
||||
|
||||
entry :: ()
|
||||
string_to_lex := "Identifier 2425525 Not_Number"
|
||||
token_array: [32]Token
|
||||
@@ -34,7 +39,5 @@ entry :: ()
|
||||
i+=1
|
||||
t.len = i - t.len
|
||||
token_array[token_count++] = t
|
||||
print_tokens(token_array)
|
||||
|
||||
for i := 0, i < token_count, i++
|
||||
tk := &token_array[i]
|
||||
printf("%.*s", cast(tk.len: S32), tk.str)
|
||||
|
||||
@@ -313,6 +313,10 @@ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Resolved_
|
||||
else if(is_untyped(expr->type)){
|
||||
expr->value = convert_untyped_to_typed(pos, expr->value, type);
|
||||
}
|
||||
else if(is_array(type) && type->arr.size == ARRAY_SIZE_INFERRED){
|
||||
if(type->arr.inferred_size_hash == expr->type->arr.inferred_size_hash)
|
||||
expr->type = type;
|
||||
}
|
||||
|
||||
if(type && expr->type != type){
|
||||
parsing_error(pos, "Assigning but incompatible types, expression: %s expected var type: %s", docname(expr->type), docname(type));
|
||||
|
||||
Reference in New Issue
Block a user