diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-02 07:20:53 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-02 07:37:39 -0500 |
| commit | 1a8f96c65f94227faa9747ef876a60f3c313c6f1 (patch) | |
| tree | d80a396958ff2fc752b620cc5314e27e40b58ecb /src/ast.c | |
| parent | Use `type*` instead of `type&` to denote a pointer type (for now) (diff) | |
| download | cup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.tar.xz cup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.zip | |
Type checking of expressions / functions!
This is a bit of a chonky commit, but it adds in the basics of checking
the types of expressions / function calls / return types. There's still
a lot of work to be done, including:
(1) Adding new core types, and casting between allowed types
automatically
(2) Picking the corrent output type based on input types (for instance
float+int == float)
(3) We need much better error reporting, the error messages are really
vague and unhelpful as-is
(4) We also need to work to ensure that a function with a return type
actually returns
(5) Possible re-factoring to make stuff less hacky when we have more
types / structs / arrays / etc.
Diffstat (limited to 'src/ast.c')
| -rw-r--r-- | src/ast.c | 29 |
1 files changed, 25 insertions, 4 deletions
@@ -55,6 +55,15 @@ char *data_type_to_str(DataType type) } } +bool type_equals(Type *a, Type *b) +{ + if (a == NULL && b == NULL) + return true; + if (a == NULL || b == NULL) + return false; + return a->type == b->type && type_equals(a->ptr, b->ptr); +} + i64 size_for_type(Type *type) { switch (type->type) @@ -64,12 +73,24 @@ i64 size_for_type(Type *type) default: assert(false && "Unreachable type"); } } - Type *type_new(DataType type) { - Type *t = calloc(sizeof(Type), 1); - t->type = type; - return t; + // For the core types, we don't need to allocate any memory, just + // return a pointer to a static instance. + static Type type_int = {.type = TYPE_INT, .ptr = NULL}; + if (type == TYPE_INT) return &type_int; + + Type *self = calloc(sizeof(Type), 1); + self->type = type; + return self; +} + +Node *Node_from_int_literal(i64 value) +{ + Node *self = Node_new(AST_LITERAL); + self->literal.type = self->expr_type = type_new(TYPE_INT); + self->literal.as_int = value; + return self; } void print_type_to_file(FILE *out, Type *type) |