fixed compound node
This commit is contained in:
parent
df1b336c7c
commit
21b43af69d
7 changed files with 117 additions and 102 deletions
|
|
@ -1,21 +1,15 @@
|
|||
import std::io;
|
||||
import std;
|
||||
|
||||
i32 main()
|
||||
{
|
||||
[u32] list = [1, 2, 3, 4, 5];
|
||||
|
||||
// iterative loop
|
||||
loop (0.., list) |i, v| {
|
||||
printf("%d\n", i);
|
||||
u32 x = 4;
|
||||
loop x == 3 {
|
||||
u32 b = 3;
|
||||
}
|
||||
|
||||
// conditional loop
|
||||
loop a == 3 {
|
||||
loop (0.., test) |k, i| {
|
||||
|
||||
}
|
||||
|
||||
// infinite loop
|
||||
loop {
|
||||
u32 b = 3;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
u32 sum<T>(T x, T y)
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import test;
|
||||
import std;
|
||||
|
||||
i32 main()
|
||||
{
|
||||
print_msg("Hello world!");
|
||||
print("Hello world!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
import std::io;
|
||||
|
||||
module log {
|
||||
|
||||
void print_msg([u8] msg)
|
||||
{
|
||||
printf("%s\n", msg);
|
||||
}
|
||||
|
||||
}
|
||||
3
lc.c
3
lc.c
|
|
@ -127,6 +127,7 @@ void print_ast(ast_node *node, int depth) {
|
|||
for (usize i = 0; i < node->expr.compound.stmt_len; ++i) {
|
||||
print_ast(node->expr.compound.statements[i], depth + 1);
|
||||
}
|
||||
break;
|
||||
case NODE_CALL:
|
||||
printf("Call: %.*s\n", (int)node->expr.call.name_len, node->expr.call.name);
|
||||
current = node->expr.call.parameters;
|
||||
|
|
@ -219,7 +220,7 @@ void print_ast(ast_node *node, int depth) {
|
|||
|
||||
int main(void)
|
||||
{
|
||||
FILE *fp = fopen("test.l", "r");
|
||||
FILE *fp = fopen("examples/for.l", "r");
|
||||
usize size = 0;
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size = ftell(fp);
|
||||
|
|
|
|||
72
parser.c
72
parser.c
|
|
@ -448,6 +448,7 @@ ast_node *parse_expression(parser *p)
|
|||
return node;
|
||||
}
|
||||
|
||||
if (match(p, TOKEN_DOT)) {
|
||||
if (match(p, TOKEN_LCURLY)) {
|
||||
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
node->type = NODE_STRUCT_INIT;
|
||||
|
|
@ -518,6 +519,10 @@ ast_node *parse_expression(parser *p)
|
|||
}
|
||||
|
||||
return node;
|
||||
} else {
|
||||
error(p, "unexpected `.`");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (p->tokens && ((p->tokens->type >= TOKEN_DOUBLE_EQ && p->tokens->type <= TOKEN_NOT_EQ) || (p->tokens->type >= TOKEN_LSHIFT_EQ && p->tokens->type <= TOKEN_DOUBLE_AND)))
|
||||
|
|
@ -580,31 +585,56 @@ ast_node *parse_expression(parser *p)
|
|||
|
||||
static ast_node *parse_compound(parser *p)
|
||||
{
|
||||
if (!match(p, TOKEN_LCURLY))
|
||||
{
|
||||
error(p, "expected `{` for beginning of a block.");
|
||||
if (!match(p, TOKEN_LCURLY)) {
|
||||
error(p, "expected `{`.");
|
||||
return NULL;
|
||||
}
|
||||
ast_node *compound = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
compound->type = NODE_UNIT;
|
||||
compound->expr.unit_node.expr = NULL;
|
||||
compound->expr.unit_node.next = NULL;
|
||||
ast_node* tail = compound;
|
||||
// FIXME: This only works with correct blocks, incorrect blocks segfault
|
||||
while (p->tokens->type != TOKEN_RCURLY &&
|
||||
p->tokens->type != TOKEN_END)
|
||||
|
||||
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
node->type = NODE_UNIT;
|
||||
|
||||
if (match(p, TOKEN_RCURLY))
|
||||
{
|
||||
node->expr.unit_node.expr = NULL;
|
||||
node->expr.unit_node.next = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
snapshot arena_start = arena_snapshot(p->allocator);
|
||||
node->expr.unit_node.expr = parse_statement(p);
|
||||
ast_node *tail = node;
|
||||
|
||||
/* In this case, there is only one parameter */
|
||||
if (match(p, TOKEN_RCURLY))
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
ast_node *expr = parse_statement(p);
|
||||
if (expr)
|
||||
{
|
||||
while (!match(p, TOKEN_RCURLY))
|
||||
{
|
||||
ast_node* stmt = parse_statement(p);
|
||||
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
tail->expr.unit_node.next->expr.unit_node.expr = stmt;
|
||||
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
||||
tail = tail->expr.unit_node.next;
|
||||
tail->type = NODE_UNIT;
|
||||
expr = parse_statement(p);
|
||||
if (!expr)
|
||||
{
|
||||
error(p, "expected `}`.");
|
||||
arena_reset_to_snapshot(p->allocator, arena_start);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
||||
tail = tail->expr.unit_node.next;
|
||||
tail->type = NODE_UNIT;
|
||||
}
|
||||
if (p->tokens->type != TOKEN_RCURLY) {
|
||||
error(p, "Unterminated block.");
|
||||
return NULL;
|
||||
}
|
||||
return compound;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
static ast_node *parse_for(parser *p)
|
||||
|
|
@ -650,12 +680,12 @@ static ast_node *parse_for(parser *p)
|
|||
arena_reset_to_snapshot(p->allocator, arena_start);
|
||||
return NULL;
|
||||
}
|
||||
node->expr.fr.slice_len += 1;
|
||||
}
|
||||
|
||||
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
||||
tail = tail->expr.unit_node.next;
|
||||
node->expr.fr.slice_len += 1;
|
||||
tail->type = NODE_UNIT;
|
||||
}
|
||||
else
|
||||
|
|
@ -715,13 +745,13 @@ parse_captures:
|
|||
arena_reset_to_snapshot(p->allocator, arena_start);
|
||||
return NULL;
|
||||
}
|
||||
node->expr.fr.capture_len += 1;
|
||||
}
|
||||
|
||||
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
||||
tail = tail->expr.unit_node.next;
|
||||
tail->type = NODE_UNIT;
|
||||
node->expr.fr.capture_len += 1;
|
||||
} else {
|
||||
error(p, "expected identifier.");
|
||||
arena_reset_to_snapshot(p->allocator, arena_start);
|
||||
|
|
@ -734,6 +764,7 @@ parse_captures:
|
|||
}
|
||||
|
||||
parse_body:;
|
||||
printf("%d %d\n", node->expr.fr.capture_len, node->expr.fr.slice_len);
|
||||
if (node->expr.fr.capture_len != node->expr.fr.slice_len) {
|
||||
error(p, "invalid number of captures.");
|
||||
return NULL;
|
||||
|
|
@ -747,6 +778,7 @@ parse_body:;
|
|||
static ast_node *parse_while(parser *p)
|
||||
{
|
||||
ast_node *condition = parse_expression(p);
|
||||
//printf("%d %d\n", p->tokens->type, TOKEN_RCURLY);
|
||||
ast_node *body = parse_compound(p);
|
||||
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
node->type = NODE_WHILE;
|
||||
|
|
|
|||
2
test.l
2
test.l
|
|
@ -1,5 +1,5 @@
|
|||
u32 a()
|
||||
{
|
||||
Person v = {x = 3, y = 4, z = 5};
|
||||
[u32] v = {1, 2, 3};
|
||||
return z[0];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue