Skip to content

Commit

Permalink
feat: global variables (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
v420v committed Dec 29, 2024
1 parent 36e19f9 commit 394cad4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/codegen/codegen.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ func gen(g *Gen, object *Object) u0 {

printf("\tleave\n");
printf("\tret\n");
} else if object.kind == OBJ_VAR {
printf(".data\n");
printf(".global %s\n", object.name);
printf("%s:\n", object.name);
printf("\t.zero %d\n", object.ty.size);
printf(".text\n");
}
object = object.next;
}
Expand Down
30 changes: 24 additions & 6 deletions src/parser/parser.ibu
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ func new_array_type(base *Type, len i32) *Type {
}

func new_func_type(params_types *Vec, return_type *Type, is_variadic bool) *Type {
let ty *Type;
ty = alloc(typesize(Type));
let ty *Type = alloc(typesize(Type));
if ty == nil {
eprintf("memory allocation failed\n");
exit(1);
Expand Down Expand Up @@ -1439,9 +1438,9 @@ break:
return is_variadic;
}

func add_function_object(p *Parser, fn *Object) u0 {
fn.next = p.globals;
p.globals = fn;
func add_global_object(p *Parser, obj *Object) u0 {
obj.next = p.globals;
p.globals = obj;
}

func parse_func(p *Parser) u0 {
Expand Down Expand Up @@ -1490,7 +1489,7 @@ func parse_func(p *Parser) u0 {
fn.params = params;
fn.is_global = true;
fn.ty = new_func_type(types, return_ty, is_variadic);
add_function_object(p, fn);
add_global_object(p, fn);
if is_declaration {
fn.is_declaration = true;
fn.locals = p.locals;
Expand All @@ -1509,10 +1508,29 @@ func parse_func(p *Parser) u0 {
leave_scope(p);
}

func parse_global_let(p *Parser) u0 {
parser_skip(p, "let");
let name *u8 = p.tokens.lit;
parser_next(p);
let ty *Type = parse_ty(p);
let obj *Object = alloc(typesize(Object));
if obj == nil {
eprintf("memory allocation failed\n");
}
obj.kind = OBJ_VAR;
obj.name = name;
obj.ty = ty;
obj.is_global = true;
parser_skip(p, ";");
add_global_object(p, obj);
}

func parse(p *Parser) u0 {
while p.tokens.kind != TK_EOF {
if strcmp(p.tokens.lit, "func") {
parse_func(p);
} else if strcmp(p.tokens.lit, "let") {
parse_global_let(p);
} else if strcmp(p.tokens.lit, "struct") {
parse_struct(p);
} else {
Expand Down

0 comments on commit 394cad4

Please sign in to comment.