feat: Make builtins work

This commit is contained in:
overflowerror 2024-04-26 15:58:34 +02:00
parent e95e2adc6d
commit 8a645a635d
12 changed files with 35 additions and 20 deletions

View file

@ -154,7 +154,7 @@ struct expression* builtin_call_expression_new(void) {
}
void builtin_call_expression_add_argument(struct expression* builtin_call, struct expression* argument) {
safe_realloc(builtin_call->builtin_call.arguments, ++builtin_call->builtin_call.argument_number * sizeof(struct expression*));
builtin_call->builtin_call.arguments = safe_realloc(builtin_call->builtin_call.arguments, (++builtin_call->builtin_call.argument_number) * sizeof(struct expression*));
builtin_call->builtin_call.arguments[builtin_call->builtin_call.argument_number - 1] = argument;
}

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include <error.h>
@ -496,6 +497,18 @@ region_t* codegen_calc_expr(FILE* out, scope_t* scope, struct calc_expression ex
return result;
}
region_t* codegen_builtin_expr(FILE* out, scope_t* scope, struct builtin_call_expression expr) {
builtin_t builtin = find_builtin(expr.id);
region_t** args = alloca(expr.argument_number * sizeof(region_t*));
for (size_t i = 0; i < expr.argument_number; i++) {
args[i] = codegen_expr(out, scope, expr.arguments[i]);
}
return builtin(out, scope, expr.argument_number, args);
}
region_t* codegen_expr(FILE* out, scope_t* scope, struct expression* expr) {
switch(expr->kind) {
case LITERAL:
@ -504,6 +517,8 @@ region_t* codegen_expr(FILE* out, scope_t* scope, struct expression* expr) {
return codegen_variable_expr(out, scope, expr->variable);
case MACRO:
return codegen_macro_expr(out, scope, expr->macro);
case BUILTIN_CALL:
return codegen_builtin_expr(out, scope, expr->builtin_call);
case CALCULATION:
return codegen_calc_expr(out, scope, expr->calc);
default:

View file

@ -1,3 +1,3 @@
var tmp = 128;
print to_str$(tmp);
print to_str(tmp);
print "\n";

View file

@ -3,5 +3,5 @@ var b = 10;
var c = 10;
var d = 18;
var r = (((((a + 2) * b) + c) - 0x14) - d);
print to_str$(r);
print to_str(r);
print "\n";

View file

@ -4,5 +4,5 @@ var a = 1;
print "a\n";
var b = (a - 1);
print to_str$(b);
print to_str(b);
print "\n";

View file

@ -1,5 +1,5 @@
var a = 128;
var b = 16;
var c = (a / b);
print to_str$(c);
print to_str(c);
print "\n";

View file

@ -1,5 +1,5 @@
var a = 255;
var b = 99;
var c = (a % b);
print to_str$(c);
print to_str(c);
print "\n";

View file

@ -1,40 +1,40 @@
var a = 5;
var b = 3;
print to_str$(a);
print to_str(a);
print "\n";
print to_str$(b);
print to_str(b);
print "\n";
a = (a * b);
b = (a / b);
print to_str$(a);
print to_str(a);
print "\n";
print to_str$(b);
print to_str(b);
print "\n";
a = (a + b);
b = a;
print to_str$(a);
print to_str(a);
print "\n";
print to_str$(b);
print to_str(b);
print "\n";
a = a;
b = (b / 2);
print to_str$(a);
print to_str(a);
print "\n";
print to_str$(b);
print to_str(b);
print "\n";
a = 0;
b = 1;
print to_str$(a);
print to_str(a);
print "\n";
print to_str$(b);
print to_str(b);
print "\n";

View file

@ -1,3 +1,3 @@
var a = 1;
print to_str$(a);
print to_str(a);
print "\n";

View file

@ -1,7 +1,7 @@
var i = 10;
while i {
print to_str$(i);
print to_str(i);
print "\n";
i = (i - 1);
}

View file

@ -1,7 +1,7 @@
var bottles = 100;
while bottles {
var bottles_string = to_str$(bottles);
var bottles_string = to_str(bottles);
if (bottles == 1) {
print bottles_string;
@ -18,7 +18,7 @@ while bottles {
}
bottles = (bottles - 1);
bottles_string = to_str$(bottles);
bottles_string = to_str(bottles);
print bottles_string;