commit 34ada58581c1a9aba9fa84f3d3010a4246a167fe
parent 62e8ec7b902300ba15b6b0a3b93f4da92a6321bd
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 30 Dec 2020 10:35:07 -0500
Implement -N <namespace> option
Diffstat:
4 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/include/parse.h b/include/parse.h
@@ -6,5 +6,6 @@ struct ast_subunit;
struct lexer;
void parse(struct lexer *lexer, struct ast_subunit *unit);
+void parse_identifier(struct lexer *lexer, struct identifier *ident);
#endif
diff --git a/src/check.c b/src/check.c
@@ -15,11 +15,22 @@
struct context {
struct type_store store;
const struct type *current_fntype;
+ struct identifier *ns;
struct scope *unit;
struct scope *scope;
};
static void
+mkident(struct context *ctx, struct identifier *out, const struct identifier *in)
+{
+ identifier_dup(out, in);
+ if (ctx->ns) {
+ out->ns = xcalloc(1, sizeof(struct identifier));
+ identifier_dup(out->ns, ctx->ns);
+ }
+}
+
+static void
expect(bool constraint, char *fmt, ...)
{
// TODO: Bring along line numbers and such
@@ -655,9 +666,8 @@ check_function(struct context *ctx,
struct declaration *decl = xcalloc(1, sizeof(struct declaration));
decl->type = DECL_FUNC;
decl->func.type = fntype;
- // TODO: Rewrite ident to be a member of the unit's namespace
- identifier_dup(&decl->ident, &afndecl->ident);
decl->func.flags = afndecl->flags;
+ mkident(ctx, &decl->ident, &afndecl->ident);
decl->func.scope = scope_push(&ctx->scope, TR_CHECK);
struct ast_function_parameters *params = afndecl->prototype.params;
@@ -777,6 +787,7 @@ check(const struct ast_unit *aunit, struct unit *unit)
{
struct context ctx = {0};
ctx.store.check_context = &ctx;
+ ctx.ns = unit->ns;
// Top-level scope management involves:
//
diff --git a/src/main.c b/src/main.c
@@ -53,8 +53,10 @@ parse_stage(const char *s)
int
main(int argc, char *argv[])
{
- enum stage stage = parse_stage(getenv("HA_STAGE"));
char *output = NULL;
+ struct unit unit = {0};
+ struct lexer lexer;
+
int c;
while ((c = getopt(argc, argv, "o:T:t:N:")) != -1) {
switch (c) {
@@ -66,7 +68,12 @@ main(int argc, char *argv[])
case 't':
assert(0); // TODO: Typedefs
case 'N':
- assert(0); // TODO: Namespace
+ unit.ns = xcalloc(1, sizeof(struct identifier));
+ FILE *in = fmemopen(optarg, strlen(optarg), "r");
+ lex_init(&lexer, in);
+ parse_identifier(&lexer, unit.ns);
+ lex_finish(&lexer);
+ break;
default:
usage(argv[0]);
return 1;
@@ -82,8 +89,8 @@ main(int argc, char *argv[])
struct ast_unit aunit = {0};
struct ast_subunit *subunit = &aunit.subunits;
struct ast_subunit **next = &aunit.subunits.next;
+ enum stage stage = parse_stage(getenv("HA_STAGE"));
- struct lexer lexer;
for (size_t i = 0; i < ninputs; ++i) {
FILE *in;
const char *path = argv[optind + i];
@@ -118,7 +125,6 @@ main(int argc, char *argv[])
return 0;
}
- struct unit unit = {0};
check(&aunit, &unit);
if (stage == STAGE_CHECK) {
return 0;
diff --git a/src/parse.c b/src/parse.c
@@ -62,7 +62,7 @@ want(struct lexer *lexer, enum lexical_token ltok, struct token *tok)
}
}
-static void
+void
parse_identifier(struct lexer *lexer, struct identifier *ident)
{
struct token tok = {0};