diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-03 03:09:08 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-03 03:09:08 -0500 |
| commit | 5b3df939bb8b399dc48102cf0c9f84e8f8702ea4 (patch) | |
| tree | 2e430510f818e0e484898f2229227f48c431c0f0 /src/builtins.c | |
| parent | Allow implicitly converting between integer-like-types (diff) | |
| download | cup-5b3df939bb8b399dc48102cf0c9f84e8f8702ea4.tar.xz cup-5b3df939bb8b399dc48102cf0c9f84e8f8702ea4.zip | |
Move builtins to a separate file
Probably want to add more builtins in the future, so pulling it out
of `parser.c` seems like the reasonable thing to do
Diffstat (limited to 'src/builtins.c')
| -rw-r--r-- | src/builtins.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/builtins.c b/src/builtins.c new file mode 100644 index 0000000..591caeb --- /dev/null +++ b/src/builtins.c @@ -0,0 +1,47 @@ +#include "builtins.h" +#include "ast.h" +#include "utils.h" +#include <string.h> +#include <stdlib.h> +#include <assert.h> + + +#define MAX_BUILTINS_COUNT 128 +static Node *all_builtins[MAX_BUILTINS_COUNT]; +static i64 builtins_count = 0; + +static void push_builtin(Node *node) +{ + assert(builtins_count < MAX_BUILTINS_COUNT); + all_builtins[builtins_count++] = node; +} + +void initialize_builtins() +{ + Node *node; + // FIXME: The `TYPE_ANY` is a hack + node = Node_new(AST_BUILTIN); + node->func.name = "print"; + node->func.return_type = type_new(TYPE_INT); + node->func.num_args = 1; + node->func.args = (Variable *)calloc(sizeof(Variable), 1); + node->func.args[0] = (Variable){"val", type_new(TYPE_ANY), 0}; + push_builtin(node); + + node = Node_new(AST_BUILTIN); + node->func.name = "putc"; + node->func.return_type = type_new(TYPE_INT); + node->func.num_args = 1; + node->func.args = (Variable *)calloc(sizeof(Variable), 2); + node->func.args[0] = (Variable){"arg", type_new(TYPE_INT), 0}; + push_builtin(node); +} + +Node *find_builtin_function(Token *token) +{ + for (i64 i = 0; i < builtins_count; i++) { + if (strcmp(all_builtins[i]->func.name, token->value.as_string) == 0) + return all_builtins[i]; + } + return NULL; +}
\ No newline at end of file |