Rename type AST nodes

This commit is contained in:
Bruce Hill 2024-02-05 13:22:30 -05:00
parent c2245c8570
commit ee0f45e295
4 changed files with 37 additions and 37 deletions

16
ast.c
View File

@ -139,16 +139,16 @@ CORD type_ast_to_cord(type_ast_t *t)
switch (t->tag) {
#define T(type, ...) case type: { auto data = t->__data.type; (void)data; return CORD_asprintf("\x1b[32;1m" #type "\x1b[m" __VA_ARGS__); }
T(TypeUnknown, "")
T(TypeVar, "(\x1b[36;1m%s\x1b[m)", data.var.name)
T(TypePointer, "(%r, is_optional=%d, is_stack=%d, is_readonly=%d)",
T(UnknownTypeAST, "")
T(VarTypeAST, "(\x1b[36;1m%s\x1b[m)", data.var.name)
T(PointerTypeAST, "(%r, is_optional=%d, is_stack=%d, is_readonly=%d)",
type_ast_to_cord(data.pointed), data.is_optional,
data.is_stack, data.is_readonly)
T(TypeStruct, "(%r)", arg_list_to_cord(data.fields))
T(TypeTaggedUnion, "(%r)", tags_to_cord(data.tags))
T(TypeArray, "(%r)", type_ast_to_cord(data.item))
T(TypeTable, "(%r => %r)", type_ast_to_cord(data.key), type_ast_to_cord(data.value))
T(TypeFunction, "(args=%r, ret=%r)", arg_list_to_cord(data.args), type_ast_to_cord(data.ret))
T(StructTypeAST, "(%r)", arg_list_to_cord(data.fields))
T(TaggedUnionTypeAST, "(%r)", tags_to_cord(data.tags))
T(ArrayTypeAST, "(%r)", type_ast_to_cord(data.item))
T(TableTypeAST, "(%r => %r)", type_ast_to_cord(data.key), type_ast_to_cord(data.value))
T(FunctionTypeAST, "(args=%r, ret=%r)", arg_list_to_cord(data.args), type_ast_to_cord(data.ret))
#undef T
}
return NULL;

32
ast.h
View File

@ -65,14 +65,14 @@ typedef enum {
} binop_e;
typedef enum {
TypeUnknown,
TypeVar,
TypePointer,
TypeStruct,
TypeTaggedUnion,
TypeArray,
TypeTable,
TypeFunction,
UnknownTypeAST,
VarTypeAST,
PointerTypeAST,
StructTypeAST,
TaggedUnionTypeAST,
ArrayTypeAST,
TableTypeAST,
FunctionTypeAST,
} type_ast_e;
typedef struct tag_s {
@ -87,30 +87,30 @@ struct type_ast_s {
sss_file_t *file;
const char *start, *end;
union {
struct {} TypeUnknown;
struct {} UnknownTypeAST;
struct {
var_t var;
} TypeVar;
} VarTypeAST;
struct {
type_ast_t *pointed;
bool is_optional:1, is_stack:1, is_readonly:1;
} TypePointer;
} PointerTypeAST;
struct {
arg_list_t *fields;
} TypeStruct;
} StructTypeAST;
struct {
tag_t *tags;
} TypeTaggedUnion;
} TaggedUnionTypeAST;
struct {
type_ast_t *item;
} TypeArray;
} ArrayTypeAST;
struct {
type_ast_t *key, *value;
} TypeTable;
} TableTypeAST;
struct {
arg_list_t *args;
type_ast_t *ret;
} TypeFunction;
} FunctionTypeAST;
} __data;
};

View File

@ -11,7 +11,7 @@
CORD compile_type(type_ast_t *t)
{
switch (t->tag) {
case TypeVar: return CORD_cat(Match(t, TypeVar)->var.name, "_t");
case VarTypeAST: return CORD_cat(Match(t, VarTypeAST)->var.name, "_t");
default: errx(1, "Not implemented");
}
}
@ -239,13 +239,13 @@ CORD compile(ast_t *ast)
auto def = Match(ast, TypeDef);
CORD code;
switch (def->type->tag) {
case TypeVar: {
case VarTypeAST: {
CORD_sprintf(&code, "typedef %r %s_t;\n", compile_type(def->type), def->var.name);
break;
}
case TypeStruct: {
case StructTypeAST: {
CORD_sprintf(&code, "typedef struct %s_s %s_t;\nstruct %s_s {\n", def->var.name, def->var.name, def->var.name);
for (arg_list_t *field = Match(def->type, TypeStruct)->fields; field; field = field->next) {
for (arg_list_t *field = Match(def->type, StructTypeAST)->fields; field; field = field->next) {
CORD_sprintf(&code, "%r%r %s;\n", code, compile_type(field->type), field->var.name);
}
code = CORD_cat(code, "};\n");

18
parse.c
View File

@ -435,7 +435,7 @@ type_ast_t *parse_table_type(parse_ctx_t *ctx, const char *pos) {
type_ast_t *value_type = expect(ctx, start, &pos, parse_type, "I couldn't parse the rest of this table type");
whitespace(&pos);
expect_closing(ctx, &pos, "}", "I wasn't able to parse the rest of this table type");
return NewTypeAST(ctx->file, start, pos, TypeTable, .key=key_type, .value=value_type);
return NewTypeAST(ctx->file, start, pos, TableTypeAST, .key=key_type, .value=value_type);
}
type_ast_t *parse_struct_type(parse_ctx_t *ctx, const char *pos) {
@ -446,7 +446,7 @@ type_ast_t *parse_struct_type(parse_ctx_t *ctx, const char *pos) {
arg_list_t *args = parse_args(ctx, &pos, false);
whitespace(&pos);
expect_closing(ctx, &pos, ")", "I wasn't able to parse the rest of this struct type");
return NewTypeAST(ctx->file, start, pos, TypeStruct, .fields=args);
return NewTypeAST(ctx->file, start, pos, StructTypeAST, .fields=args);
}
type_ast_t *parse_func_type(parse_ctx_t *ctx, const char *pos) {
@ -459,7 +459,7 @@ type_ast_t *parse_func_type(parse_ctx_t *ctx, const char *pos) {
spaces(&pos);
if (!match(&pos, "->")) return NULL;
type_ast_t *ret = optional(ctx, &pos, parse_type);
return NewTypeAST(ctx->file, start, pos, TypeFunction, .args=args, .ret=ret);
return NewTypeAST(ctx->file, start, pos, FunctionTypeAST, .args=args, .ret=ret);
}
type_ast_t *parse_array_type(parse_ctx_t *ctx, const char *pos) {
@ -468,7 +468,7 @@ type_ast_t *parse_array_type(parse_ctx_t *ctx, const char *pos) {
type_ast_t *type = expect(ctx, start, &pos, parse_type,
"I couldn't parse an array item type after this point");
expect_closing(ctx, &pos, "]", "I wasn't able to parse the rest of this array type");
return NewTypeAST(ctx->file, start, pos, TypeArray, .item=type);
return NewTypeAST(ctx->file, start, pos, ArrayTypeAST, .item=type);
}
type_ast_t *parse_pointer_type(parse_ctx_t *ctx, const char *pos) {
@ -488,7 +488,7 @@ type_ast_t *parse_pointer_type(parse_ctx_t *ctx, const char *pos) {
spaces(&pos);
type_ast_t *type = expect(ctx, start, &pos, parse_type,
"I couldn't parse a pointer type after this point");
return NewTypeAST(ctx->file, start, pos, TypePointer, .pointed=type, .is_optional=optional, .is_stack=is_stack, .is_readonly=is_readonly);
return NewTypeAST(ctx->file, start, pos, PointerTypeAST, .pointed=type, .is_optional=optional, .is_stack=is_stack, .is_readonly=is_readonly);
}
type_ast_t *parse_type_name(parse_ctx_t *ctx, const char *pos) {
@ -504,7 +504,7 @@ type_ast_t *parse_type_name(parse_ctx_t *ctx, const char *pos) {
id = heap_strf("%s.%s", id, next_id);
pos = next;
}
return NewTypeAST(ctx->file, start, pos, TypeVar, .var.name=id);
return NewTypeAST(ctx->file, start, pos, VarTypeAST, .var.name=id);
}
type_ast_t *parse_type(parse_ctx_t *ctx, const char *pos) {
@ -1188,7 +1188,7 @@ ast_t *parse_fncall_suffix(parse_ctx_t *ctx, ast_t *fn, bool is_extern) {
if (match(&pos, ":"))
extern_return_type = expect(ctx, start, &pos, parse_type, "I couldn't parse the return type of this external function call");
else
extern_return_type = NewTypeAST(ctx->file, pos, pos, TypeVar, .var.name="Void");
extern_return_type = NewTypeAST(ctx->file, pos, pos, VarTypeAST, .var.name="Void");
}
REVERSE_LIST(args);
return NewAST(ctx->file, start, pos, FunctionCall, .fn=fn, .args=args, .extern_return_type=extern_return_type);
@ -1501,7 +1501,7 @@ type_ast_t *parse_enum_type(parse_ctx_t *ctx, const char *pos) {
parser_err(ctx, tag_start, pos, "This tag value (%ld) is a duplicate of an earlier tag value", next_value);
}
type_ast_t *type = NewTypeAST(ctx->file, tag_start, pos, TypeStruct, .fields=fields);
type_ast_t *type = NewTypeAST(ctx->file, tag_start, pos, StructTypeAST, .fields=fields);
tags = new(tag_t, .name=tag_name, .value=next_value, .type=type, .next=tags);
const char *next_pos = pos;
@ -1518,7 +1518,7 @@ type_ast_t *parse_enum_type(parse_ctx_t *ctx, const char *pos) {
REVERSE_LIST(tags);
return NewTypeAST(ctx->file, start, pos, TypeTaggedUnion, .tags=tags);
return NewTypeAST(ctx->file, start, pos, TaggedUnionTypeAST, .tags=tags);
}
arg_list_t *parse_args(parse_ctx_t *ctx, const char **pos, bool allow_unnamed)