implemented import parsing

This commit is contained in:
Lorenzo Torres 2025-12-01 22:13:47 +01:00
parent 1ca6f024ee
commit bb3da4d20a
6 changed files with 40 additions and 18 deletions

9
lc.c
View file

@ -73,8 +73,9 @@ void print_ast(ast_node *node, int depth) {
print_ast(node->expr.cast.value, depth + 1);
break;
case NODE_ACCESS:
printf("Access: %.*s\n", (int)node->expr.access.member_len, node->expr.access.member);
print_ast(node->expr.access.expr, depth + 1);
printf("Access:\n");
print_ast(node->expr.access.expr, depth + 1);
print_ast(node->expr.access.member, depth + 1);
break;
case NODE_LABEL:
printf("Label: %.*s\n", (int)node->expr.label.name_len, node->expr.label.name);
@ -142,6 +143,10 @@ void print_ast(ast_node *node, int depth) {
printf("Return:\n");
print_ast(node->expr.ret.value, depth + 1);
break;
case NODE_IMPORT:
printf("Import:\n");
print_ast(node->expr.import.path, depth + 1);
break;
default:
printf("Unknown Node Type: %d\n", node->type);
break;

View file

@ -403,8 +403,7 @@ lexer *lexer_init(char *source, usize size, arena *arena)
trie_insert(keywords, lex->allocator, "do", TOKEN_DO);
trie_insert(keywords, lex->allocator, "defer", TOKEN_DEFER);
trie_insert(keywords, lex->allocator, "return", TOKEN_RETURN);
trie_insert(keywords, lex->allocator, "module", TOKEN_MODULE);
trie_insert(keywords, lex->allocator, "static", TOKEN_STATIC);
trie_insert(keywords, lex->allocator, "import", TOKEN_IMPORT);
trie_insert(keywords, lex->allocator, "const", TOKEN_CONST);
trie_insert(keywords, lex->allocator, "extern", TOKEN_EXTERN);
trie_insert(keywords, lex->allocator, "volatile", TOKEN_VOLATILE);

View file

@ -6,7 +6,6 @@
typedef enum {
TOKEN_ERROR,
TOKEN_END,
TOKEN_PLUS, // +
TOKEN_PLUS_PLUS, // ++
TOKEN_MINUS, // -
@ -50,13 +49,11 @@ typedef enum {
TOKEN_RSQUARE, // ]
TOKEN_LCURLY, // {
TOKEN_RCURLY, // }
TOKEN_INTEGER,
TOKEN_FLOAT,
TOKEN_IDENTIFIER,
TOKEN_STRING,
TOKEN_CHAR,
TOKEN_GOTO,
TOKEN_LOOP,
TOKEN_IF,
@ -65,14 +62,11 @@ typedef enum {
TOKEN_BREAK,
TOKEN_DO,
TOKEN_DEFER,
TOKEN_MODULE,
TOKEN_RETURN,
TOKEN_STATIC,
TOKEN_IMPORT,
TOKEN_CONST,
TOKEN_EXTERN,
TOKEN_VOLATILE,
TOKEN_STRUCT,
TOKEN_ENUM,
TOKEN_UNION

View file

@ -340,9 +340,7 @@ ast_node *parse_expression(parser *p)
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
node->type = NODE_ACCESS;
node->expr.access.expr = left;
node->expr.access.member = p->tokens->lexeme;
node->expr.access.member_len = p->tokens->lexeme_len;
advance(p);
node->expr.access.member = parse_expression(p);
return node;
}
@ -425,6 +423,27 @@ static ast_node *parse_statement(parser *p)
error(p, "expected `;` after `goto`.");
return NULL;
}
return node;
} else if (match(p, TOKEN_IMPORT)) {
ast_node *expr = parse_expression(p);
if (!expr) {
error(p, "expected module path after `import`.");
return NULL;
}
if (expr->type != NODE_ACCESS && expr->type != NODE_IDENTIFIER) {
error(p, "expected module path after `import`.");
return NULL;
}
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
node->type = NODE_IMPORT;
node->expr.import.path = expr;
if (!match(p, TOKEN_SEMICOLON)) {
error(p, "expected `;` after `import`.");
return NULL;
}
return node;
} else {
ast_node *expr = parse_expression(p);

View file

@ -77,7 +77,7 @@ typedef enum {
NODE_RETURN,
NODE_LABEL,
NODE_GOTO,
NODE_DO,
NODE_IMPORT,
NODE_FOR,
NODE_WHILE,
NODE_IF,
@ -131,8 +131,7 @@ typedef struct _ast_node {
} subscript;
struct {
struct _ast_node *expr;
char *member;
usize member_len;
struct _ast_node *member;
} access;
struct {
struct _ast_node *expr;
@ -148,6 +147,10 @@ typedef struct _ast_node {
struct {
struct _ast_node *value;
} ret;
struct {
/* This should be an access. */
struct _ast_node *path;
} import;
} expr;
} ast_node;

4
test.c
View file

@ -1,3 +1,5 @@
test:
import example.test.idk.idk;
hello:
goto test;
return 5;