aboutsummaryrefslogtreecommitdiff
path: root/src/analyze.c
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2026-05-01 14:40:06 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2026-05-01 14:40:06 +0300
commit7a68d1106b37e16c24e58bc77ce49fc6beadce9c (patch)
treeb0a3bb0b0124294b213cdfd3ae7b214bc32b19f5 /src/analyze.c
parent48cd04e1a57f076287fb6ecdd0a4236e191ff7d3 (diff)
downloadfwd-7a68d1106b37e16c24e58bc77ce49fc6beadce9c.tar.gz
fwd-7a68d1106b37e16c24e58bc77ce49fc6beadce9c.zip
run formatter
+ Should really get this done automatically somehow
Diffstat (limited to 'src/analyze.c')
-rw-r--r--src/analyze.c176
1 files changed, 112 insertions, 64 deletions
diff --git a/src/analyze.c b/src/analyze.c
index fc94e15..fd1aabb 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -21,7 +21,8 @@ struct state {
};
static int analyze(struct state *state, struct scope *scope, struct ast *node);
-static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node);
+static int analyze_known_block(struct state *state, struct scope *scope,
+ struct ast *node);
static int analyze_list(struct state *state, struct scope *scope,
struct ast *nodes);
@@ -157,7 +158,8 @@ static int analyze_comparison(struct state *state, struct scope *scope,
return 0;
}
-static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_known_block(struct state *state, struct scope *scope,
+ struct ast *node)
{
assert(node && node->k == AST_BLOCK);
@@ -229,7 +231,8 @@ static int analyze_init(struct state *state, struct scope *scope,
return -1;
}
-static int deduce_closure_types(struct scope *scope, struct ast *node, struct type *type)
+static int deduce_closure_types(struct scope *scope, struct ast *node,
+ struct type *type)
{
/* a bit of checking duplication here, hmm */
if (node->k != AST_CLOSURE)
@@ -242,13 +245,15 @@ static int deduce_closure_types(struct scope *scope, struct ast *node, struct ty
struct ast *param = closure_bindings(node);
struct type *t = (type->k == TYPE_CLOSURE)
- ? tclosure_args(type)
- : tpure_closure_args(type)
- ;
+ ? tclosure_args(type)
+ : tpure_closure_args(type)
+ ;
if (ast_list_len(param) != type_list_len(t)) {
- semantic_error(scope, node, "expected %zu closure parameters, got %zu",
- type_list_len(t), ast_list_len(param));
+ semantic_error(scope, node,
+ "expected %zu closure parameters, got %zu",
+ type_list_len(t), ast_list_len(param)
+ );
return -1;
}
@@ -286,7 +291,8 @@ static int analyze_call(struct state *state, struct scope *scope,
size_t got = ast_list_len(args);
if (expected != got) {
semantic_error(scope, node, "expected %d params, got %d",
- expected, got);
+ expected, got
+ );
return -1;
}
@@ -351,15 +357,18 @@ static int analyze_deref(struct state *state, struct scope *scope,
if (expr->t->k == TYPE_PTR) {
semantic_error(node->scope, node,
- "deref of raw ptr not allowed");
+ "deref of raw ptr not allowed"
+ );
semantic_info(node->scope, node,
- "use a nil check to convert to ref");
+ "use a nil check to convert to ref"
+ );
return -1;
}
if (expr->t->k != TYPE_REF) {
semantic_error(node->scope, node,
- "deref of something not a reference");
+ "deref of something not a reference"
+ );
return -1;
}
@@ -373,7 +382,8 @@ static int analyze_id(struct state *state, struct scope *scope,
struct ast *found = file_scope_find_symbol(scope, id_str(node));
if (!found) {
semantic_error(scope, node, "no symbol named \"%s\"",
- id_str(node));
+ id_str(node)
+ );
return -1;
}
@@ -468,7 +478,8 @@ static int analyze_str(struct state *state, struct scope *scope,
return 0;
}
-static int analyze_template(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_template(struct state *state, struct scope *scope,
+ struct ast *node)
{
struct scope *template_scope = create_scope();
scope_add_scope(scope, template_scope);
@@ -479,14 +490,17 @@ static int analyze_template(struct state *state, struct scope *scope, struct ast
return 0;
}
-static int analyze_instance(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_instance(struct state *state, struct scope *scope,
+ struct ast *node)
{
(void)state;
struct ast *id = instance_templ(node);
struct ast *template = file_scope_find_template(scope, id_str(id));
if (!template) {
- semantic_error(scope, node, "no such template: %s\n", id_str(id));
+ semantic_error(scope, node, "no such template: %s\n",
+ id_str(id)
+ );
return -1;
}
@@ -495,7 +509,8 @@ static int analyze_instance(struct state *state, struct scope *scope, struct ast
struct type *args = instance_type_args(node);
if (ast_list_len(params) != type_list_len(args)) {
semantic_error(scope, node, "expected %zu types, got %zu\n",
- ast_list_len(params), type_list_len(args));
+ ast_list_len(params), type_list_len(args)
+ );
return -1;
}
@@ -528,7 +543,8 @@ static int analyze_instance(struct state *state, struct scope *scope, struct ast
return 0;
}
-static int analyze_struct(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_struct(struct state *state, struct scope *scope,
+ struct ast *node)
{
struct scope *struct_scope = create_scope();
scope_add_scope(scope, struct_scope);
@@ -541,11 +557,14 @@ static int analyze_struct(struct state *state, struct scope *scope, struct ast *
return 0;
}
-static int analyze_construct(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_construct(struct state *state, struct scope *scope,
+ struct ast *node)
{
struct ast *def = file_scope_find_type(scope, construct_id(node));
if (!def) {
- semantic_error(scope, node, "no such type: %s", construct_id(node));
+ semantic_error(scope, node, "no such type: %s",
+ construct_id(node)
+ );
return -1;
}
@@ -559,15 +578,20 @@ static int analyze_construct(struct state *state, struct scope *scope, struct as
if (ast_list_len(params) != ast_list_len(args)) {
semantic_error(scope, node, "expected %zu args, got %zu\n",
- ast_list_len(params), ast_list_len(args));
+ ast_list_len(params), ast_list_len(args)
+ );
return -1;
}
foreach_node(arg, args) {
- struct ast *exists = scope_find_symbol(def->scope, construction_id(arg));
+ struct ast *exists = scope_find_symbol(def->scope,
+ construction_id(arg)
+ );
if (!exists) {
semantic_error(scope, arg, "no member %s in %s\n",
- construction_id(arg), construct_id(node));
+ construction_id(arg),
+ construct_id(node)
+ );
return -1;
}
@@ -588,7 +612,8 @@ static int analyze_construct(struct state *state, struct scope *scope, struct as
return 0;
}
-static int analyze_explode(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_explode(struct state *state, struct scope *scope,
+ struct ast *node)
{
if (analyze(state, scope, explode_expr(node)))
return -1;
@@ -597,8 +622,9 @@ static int analyze_explode(struct state *state, struct scope *scope, struct ast
if (type->k != TYPE_ID) {
char *str = type_str(type);
semantic_error(scope, explode_expr(node),
- "expected struct type, got %s\n",
- str);
+ "expected struct type, got %s\n",
+ str
+ );
free(str);
return -1;
}
@@ -606,8 +632,9 @@ static int analyze_explode(struct state *state, struct scope *scope, struct ast
struct ast *def = file_scope_find_type(scope, type->id);
if (!def || def->k != AST_STRUCT_DEF) {
semantic_error(scope, explode_expr(node),
- "no such struct type: %s\n",
- type->id);
+ "no such struct type: %s\n",
+ type->id
+ );
return -1;
}
@@ -616,16 +643,20 @@ static int analyze_explode(struct state *state, struct scope *scope, struct ast
if (ast_list_len(params) != ast_list_len(args)) {
semantic_error(scope, node, "expected %zu args, got %zu\n",
- ast_list_len(params), ast_list_len(args));
+ ast_list_len(params), ast_list_len(args)
+ );
return -1;
}
foreach_node(arg, args) {
struct ast *var = deconstruction_var(arg);
- struct ast *exists = scope_find_symbol(def->scope, deconstruction_id(arg));
+ struct ast *exists = scope_find_symbol(def->scope,
+ deconstruction_id(arg)
+ );
if (!exists) {
semantic_error(scope, arg, "no member %s in %s\n",
- deconstruction_id(arg), type->id);
+ deconstruction_id(arg), type->id
+ );
return -1;
}
@@ -672,7 +703,8 @@ static int analyze_if(struct state *state, struct scope *scope,
return 0;
}
-static int analyze_nil(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_nil(struct state *state, struct scope *scope,
+ struct ast *node)
{
(void)state;
(void)scope;
@@ -703,7 +735,8 @@ static bool castable_type(struct type *type)
return false;
}
-static int analyze_as(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_as(struct state *state, struct scope *scope,
+ struct ast *node)
{
if (analyze(state, scope, as_expr(node)))
return -1;
@@ -734,7 +767,8 @@ static int analyze_as(struct state *state, struct scope *scope, struct ast *node
return 0;
}
-static int analyze_sizeof(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_sizeof(struct state *state, struct scope *scope,
+ struct ast *node)
{
if (analyze_type(state, scope, sizeof_type(node)))
return -1;
@@ -744,7 +778,8 @@ static int analyze_sizeof(struct state *state, struct scope *scope, struct ast *
return 0;
}
-static int analyze_nil_check(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_nil_check(struct state *state, struct scope *scope,
+ struct ast *node)
{
struct ast *expr = nil_check_expr(node);
if (analyze(state, scope, expr))
@@ -781,7 +816,8 @@ static int analyze_nil_check(struct state *state, struct scope *scope, struct as
return 0;
}
-static int analyze_forget(struct state *state, struct scope *scope, struct ast *node)
+static int analyze_forget(struct state *state, struct scope *scope,
+ struct ast *node)
{
struct ast *def = file_scope_find_symbol(scope, forget_id(node));
if (!def) {
@@ -793,7 +829,8 @@ static int analyze_forget(struct state *state, struct scope *scope, struct ast *
return 0;
}
-static int analyze_put(struct state *state, struct scope *scope, struct ast *put)
+static int analyze_put(struct state *state, struct scope *scope,
+ struct ast *put)
{
struct ast *dst = put_dst(put);
if (analyze(state, scope, dst))
@@ -802,7 +839,9 @@ static int analyze_put(struct state *state, struct scope *scope, struct ast *put
struct type *type = dst->t;
if (type->k != TYPE_REF) {
char *str = type_str(type);
- semantic_error(scope, put, "trying to write to non-ref type %s\n", str);
+ semantic_error(scope, put,
+ "trying to write to non-ref type %s\n", str
+ );
free(str);
return -1;
}
@@ -811,7 +850,8 @@ static int analyze_put(struct state *state, struct scope *scope, struct ast *put
return 0;
}
-static int analyze_write(struct state *state, struct scope *scope, struct ast *write)
+static int analyze_write(struct state *state, struct scope *scope,
+ struct ast *write)
{
struct ast *src = write_src(write);
if (analyze(state, scope, src))
@@ -1125,14 +1165,15 @@ static struct type *fwd_type_kind(fwd_type_t type)
case FWD_U64: return tgen_type(TYPE_U64, NULL, NULL, NULL_LOC());
case FWD_PTR: return tgen_nil(NULL_LOC());
default:
- break;
+ break;
}
abort();
return NULL;
}
-int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, fwd_type_t rtype, ...)
+int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func,
+ fwd_type_t rtype, ...)
{
struct scope *scope = (void *)state;
struct ast *vars = NULL;
@@ -1153,7 +1194,9 @@ int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, f
assert(name);
sprintf(name, "%s%zu", "var", idx);
- struct ast *new = gen_var(name, fwd_type_kind(type), NULL_LOC());
+ struct ast *new = gen_var(name, fwd_type_kind(type),
+ NULL_LOC()
+ );
if (vars)
new->n = vars;
@@ -1167,8 +1210,12 @@ int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, f
if (rtype != FWD_VOID) {
char *name = strdup("ret");
struct ast *new = gen_var(name,
- tgen_closure(fwd_type_kind(rtype), NULL_LOC()),
- NULL_LOC());
+ tgen_closure(
+ fwd_type_kind(rtype),
+ NULL_LOC()
+ ),
+ NULL_LOC()
+ );
if (vars)
new->n = vars;
@@ -1178,7 +1225,8 @@ int fwd_register(struct fwd_state *state, const char *name, fwd_extern_t func, f
vars = reverse_ast_list(vars);
struct ast *def = gen_proc(strdup(name), vars,
- fwd_type_kind(rtype), NULL, NULL_LOC());
+ fwd_type_kind(rtype), NULL, NULL_LOC()
+ );
if (scope_add_symbol(scope, def))
return -1;
@@ -1191,23 +1239,23 @@ int analyze_root(struct scope *scope, struct ast *root)
foreach_node(node, root) {
switch (node->k) {
case AST_PROC_DEF:
- if (scope_add_symbol(scope, node))
- return -1;
- break;
+ if (scope_add_symbol(scope, node))
+ return -1;
+ break;
case AST_STRUCT_DEF:
- if (scope_add_type(scope, node))
- return -1;
- break;
+ if (scope_add_type(scope, node))
+ return -1;
+ break;
case AST_STRUCT_CONT:
- abort();
- break;
+ abort();
+ break;
case AST_TRAIT_DEF:
- if (scope_add_trait(scope, node))
- return -1;
- break;
+ if (scope_add_trait(scope, node))
+ return -1;
+ break;
case AST_IMPORT: {
if (!try_import_mod(scope, node))
@@ -1220,20 +1268,20 @@ int analyze_root(struct scope *scope, struct ast *root)
}
case AST_TEMPLATE:
- if (scope_add_template(scope, node))
- return -1;
- break;
+ if (scope_add_template(scope, node))
+ return -1;
+ break;
case AST_SUPERTEMPLATE:
- if (scope_add_template(scope, node))
- return -1;
- break;
+ if (scope_add_template(scope, node))
+ return -1;
+ break;
case AST_INSTANCE:
- break;
+ break;
default:
- abort();
+ abort();
}
}