Skip to content

Commit

Permalink
minor typecheck adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
linxuanm committed Mar 6, 2024
1 parent 85da062 commit 4b3a8b1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SRC_MAIN = \
# build settings

LIBS = \
$(C0_UTIL_PATH)/shorthand.c1 \
$(C0_UTIL_PATH)/vector.c1 \
$(C0_UTIL_PATH)/ordered_map.c1 \
$(C0_UTIL_PATH)/regex.c1
Expand All @@ -47,5 +48,5 @@ clean:
init: clean
echo "% $(CC) $(CFLAGS) -o $(OBJ) $(LIBS) $(SRC)" > README.txt

test: init
test: $(OBJ)
$(PY) tests/unit_test.py
2 changes: 1 addition & 1 deletion c0-utils
4 changes: 4 additions & 0 deletions src/ast.c1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct ast_type_t {
string iden;

// 'A', 'P'
// `content == NULL`: then this is a null pointer (can be any pointer type)
struct ast_type_t *content;

// positional information
Expand Down Expand Up @@ -79,6 +80,9 @@ struct ast_exp_t {
// positional information
u_pos_t *pos;

// the type of this expression (populated on typecheck)
ast_type_t *etype;

// number of C0VM instructions needed to load this expression
int n_c0vm_code;
};
Expand Down
5 changes: 2 additions & 3 deletions src/compile.c1
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ c_result_t *compile(vec_t *file_reprs, cparams_t *params) {
if (params->dump_ast) {
println("// INCLUDED LIBS");

void *[] libs = alloc_array(void *, om_size(gctx->libs));
__om_collect_keys(gctx->libs->root, libs, 0);
vec_t *libs = om_get_keys(gctx->libs);
for (int l = 0; l < om_size(gctx->libs); l++) {
printf("#use <%s>\n", * (string *) libs[l]);
printf("#use <%s>\n", * (string *) v_get(libs, l));
}
if (!om_empty(gctx->libs)) println("");

Expand Down
25 changes: 14 additions & 11 deletions src/parser.c1
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,11 @@ ast_func_t *__p_parse_func(p_ctx_t *ctx)
func->param_types = v_create_vector();
func->param_names = v_create_vector();

func->ret_type = __p_expect_type(ctx);
if (func->ret_type == NULL) return NULL;
ast_type_t *rtype = __p_expect_type(ctx);
if (rtype == NULL) return NULL;
func->ret_type = rtype;
if (!t_assert_valid_type(ctx->glob_ctx, rtype, false)) return NULL;
if (!t_assert_small_type(ctx->glob_ctx, rtype)) return NULL;

l_token_t *iden_tok = __p_expect_id_tok(ctx, true);
if (iden_tok == NULL) return NULL;
Expand Down Expand Up @@ -1243,14 +1246,14 @@ ast_program_t *__p_parse_program(p_ctx_t *ctx)
Parsing each file updates the declared types that the parse has accumulated
so far.
*/
e_error_t *p_parse_file(fr_file_t *file, s_glob_ctx_t *glob_ctx)
//@requires file != NULL;
//@requires v_is_vector(file->tokens);
{
p_ctx_t *ctx = p_create_ctx(file->tokens, glob_ctx);
file->ast = __p_parse_program(ctx);

return ctx->glob_ctx->err;
}
// e_error_t *p_parse_file(fr_file_t *file, s_glob_ctx_t *glob_ctx)
// //@requires file != NULL;
// //@requires v_is_vector(file->tokens);
// {
// p_ctx_t *ctx = p_create_ctx(file->tokens, glob_ctx);
// file->ast = __p_parse_program(ctx);

// return ctx->glob_ctx->err;
// }

// END parsing stage
38 changes: 35 additions & 3 deletions src/type_check.c1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ e_error_t *t_is_valid_type(
}

if (type->type == 'A' || type->type == 'P') {
// type of a `NULL` literal
if (type->type == 'P' && type->content == NULL) return NULL;

return t_is_valid_type(gctx, type->content, false);
}

Expand Down Expand Up @@ -96,7 +99,12 @@ bool t_match_type(s_glob_ctx_t *gctx, ast_type_t *expect, ast_type_t *real)

if (expect->type == 'X') {
return string_equal(expect->iden, real->iden);
} else if (expect->type == 'A' || expect->type == 'P') {
} else if (expect->type == 'A') {
return t_match_type(gctx, expect->content, real->content);
} else if (expect->type == 'P') {
// a `NULL` literal matches any pointer type
if (real->content == NULL) return true;

return t_match_type(gctx, expect->content, real->content);
} else {
return true;
Expand All @@ -107,6 +115,7 @@ bool t_match_type(s_glob_ctx_t *gctx, ast_type_t *expect, ast_type_t *real)

// BEGIN type assertions

// only used for AST type
bool t_assert_valid_type(s_glob_ctx_t *gctx, ast_type_t *type, bool allow_void)
//@requires __s_is_glob_ctx(gctx);
//@requires type != NULL;
Expand All @@ -115,6 +124,7 @@ bool t_assert_valid_type(s_glob_ctx_t *gctx, ast_type_t *type, bool allow_void)
return !s_has_err(gctx);
}

// only used for AST type
bool t_assert_small_type(s_glob_ctx_t *gctx, ast_type_t *type)
//@requires __s_is_glob_ctx(gctx);
//@requires t_is_valid_type(gctx, type, true) == NULL;
Expand All @@ -128,19 +138,41 @@ bool t_assert_small_type(s_glob_ctx_t *gctx, ast_type_t *type)
}

bool t_assert_match_type(
s_glob_ctx_t *gctx, ast_type_t *expect, ast_type_t *real
s_glob_ctx_t *gctx, ast_type_t *expect, ast_type_t *real, u_pos_t *pos
)
//@requires __s_is_glob_ctx(gctx);
//@requires t_is_valid_type(gctx, expect, true) == NULL;
//@requires t_is_valid_type(gctx, real, true) == NULL;
{
if (!t_match_type(gctx, expect, real)) {
// TODO: get a AST_TO_STRING function for better error message
s_set_err_at(gctx, 31, "type msimatch", real->pos);
s_set_err_at(gctx, 31, "type msimatch", pos);
return false;
}

return true;
}

// END type assertions

// BEGIN exp type check

// populates the `type`
bool t_check_term(s_glob_ctx_t *gctx, ast_exp_t *term)
//@requires __s_is_glob_ctx(gctx);
//@ensures xor(\result, e_has_err(gctx->err));
{
ast_type_t *type = alloc(ast_type_t);
if (term->type == 'I') type->type = 'I';
else if (term->type == 'C') type->type = 'C';
else if (term->type == 'S') type->type = 'S';
else if (term->type == 'B') type->type = 'S';
else if (term->type == 'N') type->type = 'S';
else if (term->type == 'D') type->type = 'S';
else { error("unimplemented type"); }

term->etype = type;
return true;
}

// END exp type check

0 comments on commit 4b3a8b1

Please sign in to comment.