summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-07-17 18:09:04 -0700
committerCarson Fleming <cflems@cflems.net>2026-07-17 18:09:04 -0700
commitb43aebd9d3c2e072ee2acc50d4381dcb0f01ec98 (patch)
tree26727e2abac02f19a557171fbd8f08ae1d21a68e
parentbfb8020c1a64f505537163caa465bd7f06e43f18 (diff)
downloadccc-b43aebd9d3c2e072ee2acc50d4381dcb0f01ec98.tar.gz
type shortcuts for literals
-rw-r--r--ast.h7
-rw-r--r--parser.c4
-rw-r--r--scope.c18
-rw-r--r--scope.h4
-rw-r--r--type_checker.c68
5 files changed, 31 insertions, 70 deletions
diff --git a/ast.h b/ast.h
index 89fd0f8..918ae68 100644
--- a/ast.h
+++ b/ast.h
@@ -172,6 +172,13 @@ struct root_node {
struct ast {
struct root_node* root_node;
struct scope* root_scope;
+
+ /* nice shortcuts to have */
+ const struct type_def* void_type;
+ const struct type_def* char_type;
+ const struct type_def* integral_type;
+ const struct type_def* decimal_type;
+ const struct type_def* string_type;
};
void ast_destroy(struct ast* ast);
diff --git a/parser.c b/parser.c
index 0d403a6..40cdb1c 100644
--- a/parser.c
+++ b/parser.c
@@ -460,9 +460,9 @@ static bool parse_root(struct root_node* p_node) {
void parse(const char* path, struct ast* ast) {
lexer_load(path);
scope_push(&scope);
- scope_install_default_types(scope);
-
ast->root_scope = scope;
+ scope_install_default_types(ast);
+
struct root_node** p_node = &ast->root_node;
for (;;) {
diff --git a/scope.c b/scope.c
index db5c240..1d15ff2 100644
--- a/scope.c
+++ b/scope.c
@@ -1,4 +1,5 @@
#include "scope.h"
+#include "ast.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -159,9 +160,9 @@ void scope_pop(struct scope** p_scope) {
*p_scope = (*p_scope)->next_out;
}
-void scope_install_default_types(struct scope* scope) {
+void scope_install_default_types(struct ast* ast) {
/* TODO: don't let people make void variables */
- scope_define_type(scope, (struct type_def) {
+ ast->void_type = scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("void"),
.how_long = 0,
@@ -173,7 +174,7 @@ void scope_install_default_types(struct scope* scope) {
.sz = 0,
});
- scope_define_type(scope, (struct type_def) {
+ ast->char_type = scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("char"),
.how_long = 0,
@@ -185,7 +186,7 @@ void scope_install_default_types(struct scope* scope) {
.is_floating = false,
});
- scope_define_type(scope, (struct type_def) {
+ scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("short"),
.how_long = 0,
@@ -197,7 +198,7 @@ void scope_install_default_types(struct scope* scope) {
.is_floating = false,
});
- scope_define_type(scope, (struct type_def) {
+ scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("int"),
.how_long = 0,
@@ -209,7 +210,7 @@ void scope_install_default_types(struct scope* scope) {
.is_floating = false,
});
- scope_define_type(scope, (struct type_def) {
+ ast->integral_type = scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("int"),
.how_long = 1,
@@ -220,8 +221,9 @@ void scope_install_default_types(struct scope* scope) {
.is_signed = true,
.is_floating = false,
});
+ ast->string_type = ast->integral_type; /* TODO: support pointers */
- scope_define_type(scope, (struct type_def) {
+ scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("float"),
.how_long = 0,
@@ -233,7 +235,7 @@ void scope_install_default_types(struct scope* scope) {
.is_floating = true,
});
- scope_define_type(scope, (struct type_def) {
+ ast->decimal_type = scope_define_type(ast->root_scope, (struct type_def) {
.key = {
.name = strdup("double"),
.how_long = 0,
diff --git a/scope.h b/scope.h
index 1d3c8ac..19561e0 100644
--- a/scope.h
+++ b/scope.h
@@ -54,10 +54,12 @@ struct scope {
unsigned long long bp_offset;
};
+struct ast;
+
void scope_push(struct scope** p_scope);
void scope_pop(struct scope** p_scope);
void scope_destroy(struct scope* scope);
-void scope_install_default_types(struct scope* scope);
+void scope_install_default_types(struct ast* ast);
bool scope_get_type(
const struct scope* scope,
const struct type_def** p_entry,
diff --git a/type_checker.c b/type_checker.c
index 538f51d..2842ea2 100644
--- a/type_checker.c
+++ b/type_checker.c
@@ -11,6 +11,7 @@
exit(1);\
}
+static struct ast* ast_ref;
static struct scope* scope;
static void type_check_expr(struct expr_node* node);
@@ -38,74 +39,20 @@ static const struct type_def* resolve_type_ref(struct type_ref_node* node) {
return node->def_ref;
}
-/* TODO: we likely want some shortcut to access char, int, double, long, etc. */
static const struct type_def* resolve_int_lit(struct int_lit_node* node) {
- const struct type_def* resolved_type;
- scope_get_type(scope, &resolved_type, &(struct type_key) {
- .name = "int",
- .how_long = 1,
- .marked_signed = false,
- .marked_unsigned = false,
- });
-
- if (resolved_type == NULL)
- TYPE_PANIC(
- "no integral type exists, "
- "ensure install_default_types was called.");
-
- return resolved_type;
+ return ast_ref->integral_type;
}
static const struct type_def* resolve_float_lit(struct float_lit_node* node) {
- const struct type_def* resolved_type;
- scope_get_type(scope, &resolved_type, &(struct type_key) {
- .name = "float",
- .how_long = 0,
- .marked_signed = false,
- .marked_unsigned = false,
- });
-
- if (resolved_type == NULL)
- TYPE_PANIC(
- "no floating point type exists, "
- "ensure install_default_types was called.");
-
- return resolved_type;
+ return ast_ref->decimal_type;
}
static const struct type_def* resolve_char_lit(struct char_lit_node* node) {
- const struct type_def* resolved_type;
- scope_get_type(scope, &resolved_type, &(struct type_key) {
- .name = "char",
- .how_long = 0,
- .marked_signed = false,
- .marked_unsigned = false,
- });
-
- if (resolved_type == NULL)
- TYPE_PANIC(
- "no character type exists, "
- "ensure install_default_types was called.");
-
- return resolved_type;
+ return ast_ref->char_type;
}
static const struct type_def* resolve_str_lit(struct str_lit_node* node) {
- /* TODO: pointer types */
- const struct type_def* resolved_type;
- scope_get_type(scope, &resolved_type, &(struct type_key) {
- .name = "int",
- .how_long = 1,
- .marked_signed = false,
- .marked_unsigned = true,
- });
-
- if (resolved_type == NULL)
- TYPE_PANIC(
- "no string type exists, "
- "ensure install_default_types was called.");
-
- return resolved_type;
+ return ast_ref->string_type;
}
static const struct type_def* resolve_var_ref(struct var_ref_node* node) {
@@ -213,7 +160,6 @@ static void type_check_return(struct return_node* node) {
}
static void type_check_if(struct if_node* node) {
- printf("tc if\n");
type_check_expr(node->cond);
type_check_stmt(node->true_branch);
if (node->false_branch != NULL) type_check_stmt(node->false_branch);
@@ -277,6 +223,7 @@ static void type_check_root(struct root_node* node) {
}
void type_check(struct ast* ast) {
+ ast_ref = ast;
scope = ast->root_scope;
struct root_node* node = ast->root_node;
@@ -284,4 +231,7 @@ void type_check(struct ast* ast) {
type_check_root(node);
node = node->next;
}
+
+ ast_ref = NULL;
+ scope = NULL;
}