Browse Source

code gen for improved parser

master
Martin Dørum 10 months ago
parent
commit
3be4c7ac60
1 changed files with 101 additions and 4 deletions
  1. 101
    4
      lib/parse/parse.c

+ 101
- 4
lib/parse/parse.c View File

@@ -18,6 +18,8 @@ static int parse_object_literal(
l2_trace_scope("object literal");
// '{' and EOL already skipped by parse_object_or_function_literal

l2_gen_namespace(gen);

while (1) {
struct l2_token *tok = l2_lexer_peek(lexer, 1);
if (tok->kind == L2_TOK_CLOSE_BRACE) {
@@ -30,6 +32,7 @@ static int parse_object_literal(
}

l2_trace("key: '%s'", tok->v.str);
char *key = l2_token_extract_str(tok);
l2_lexer_consume(lexer); // ident

tok = l2_lexer_peek(lexer, 1);
@@ -45,31 +48,95 @@ static int parse_object_literal(
return -1;
}

l2_lexer_skip_opt(lexer, L2_TOK_EOL);
l2_gen_namespace_set(gen, &key);
l2_gen_pop(gen);

tok = l2_lexer_peek(lexer, 1);
if (tok->kind != L2_TOK_EOL && tok->kind != L2_TOK_CLOSE_BRACE) {
l2_parse_err(err, tok, "In object literal: Expected EOL or '}', got %s",
l2_token_kind_name(tok->kind));
return -1;
}

if (tok->kind == L2_TOK_EOL) {
l2_lexer_consume(lexer); // EOL
}
}

return 0;
}

static int parse_function_literal(
static int parse_function_literal_impl(
struct l2_lexer *lexer, struct l2_generator *gen, struct l2_parse_error *err) {
l2_trace_scope("function literal");
// '{' and EOL already skipped by parse_object_or_function_literal

int first = 1;
while (1) {
if (l2_lexer_peek(lexer, 1)->kind == L2_TOK_CLOSE_BRACE) {
l2_lexer_consume(lexer); // '}'
break;
}

if (!first) {
l2_gen_pop(gen);
}

l2_trace_scope("function literal expression");
if (parse_expression(lexer, gen, err) < 0) {
return -1;
}

l2_lexer_skip_opt(lexer, L2_TOK_EOL);
first = 0;
}

// All functions must put _something_ on the stack
if (first) {
l2_gen_push(gen, 0);
}

l2_gen_ret(gen);
return 0;
}

static int parse_function_literal(
struct l2_lexer *lexer, struct l2_generator *gen, struct l2_parse_error *err) {
l2_gen_flush(gen);

struct l2_io_writer *prev_writer = gen->writer.w;

// Generate the function to a buffer in memory
struct l2_io_mem_writer w = {0};
w.w.write = l2_io_mem_write;
gen->writer.w = &w.w;

// Generates three words; PUSH, 0, RJMP
l2_gen_rjmp(gen, 0);

l2_word pos = gen->pos;

// Generate the function body itself
int ret = parse_function_literal_impl(lexer, gen, err);
l2_gen_flush(gen);
gen->writer.w = prev_writer;
if (ret < 0) {
free(w.mem);
return -1;
}

l2_word *ops = (l2_word *)w.mem;
l2_word opcount = w.len / sizeof(l2_word);

// Due to the earlier gen_rjmp, the second word will be the argument to RJMP.
// Need to set it properly to skip the function body.
// The '- 3' is because we don't skip the PUSH, <count>, RJMP sequence.
ops[1] = opcount - 3;

l2_bufio_put_n(&gen->writer, ops, opcount * sizeof(l2_word));
free(w.mem);

l2_gen_function(gen, pos);
return 0;
}

@@ -120,20 +187,32 @@ static int parse_arg_level_expression_base(
} else if (tok->kind == L2_TOK_IDENT) {
l2_trace_scope("ident");
l2_trace("ident '%s'", tok->v.str);
char *ident = l2_token_extract_str(tok);
l2_lexer_consume(lexer); // ident

l2_gen_stack_frame_lookup(gen, &ident);
} else if (tok->kind == L2_TOK_NUMBER) {
l2_trace_scope("number literal");
l2_trace("number %g", tok->v.num);
double number = tok->v.num;
l2_lexer_consume(lexer); // number

l2_gen_number(gen, number);
} else if (tok->kind == L2_TOK_STRING) {
l2_trace_scope("string literal");
l2_trace("string '%s'", tok->v.str);
char *str = l2_token_extract_str(tok);
l2_lexer_consume(lexer); // string

l2_gen_string(gen, &str);
} else if (tok->kind == L2_TOK_QUOT && tok2->kind == L2_TOK_IDENT) {
l2_trace_scope("atom literal");
l2_trace("atom '%s'", tok->v.str);
char *ident = l2_token_extract_str(tok2);
l2_lexer_consume(lexer); // "'"
l2_lexer_consume(lexer); // ident

l2_gen_atom(gen, &ident);
} else if (tok->kind == L2_TOK_OPEN_BRACE) {
if (parse_object_or_function_literal(lexer, gen, err) < 0) {
return -1;
@@ -159,13 +238,20 @@ static int parse_arg_level_expression(
struct l2_token *tok2 = l2_lexer_peek(lexer, 2);

if (tok->kind == L2_TOK_OPEN_PAREN && tok2->kind == L2_TOK_CLOSE_PAREN) {
l2_trace_scope("noadic func call");
l2_trace_scope("niladic func call");
l2_lexer_consume(lexer); // '('
l2_lexer_consume(lexer); // ')'

l2_gen_push(gen, 0);
l2_gen_func_call(gen);
} else if (tok->kind == L2_TOK_PERIOD && tok2->kind == L2_TOK_IDENT) {
l2_trace_scope("lookup");
l2_trace("ident '%s'", tok2->v.str);
char *ident = l2_token_extract_str(tok2);
l2_lexer_consume(lexer); // '.'
l2_lexer_consume(lexer); // ident

l2_gen_namespace_lookup(gen, &ident);
} else {
break;
}
@@ -189,6 +275,8 @@ static int parse_func_call_after_base(
} while (!tok_is_end(l2_lexer_peek(lexer, 1)));

// The 'argc' previous expressions were arguments, the one before that was the function
l2_gen_push(gen, argc);
l2_gen_func_call(gen);

return 0;
}
@@ -202,12 +290,15 @@ static int parse_expression(
if (tok->kind == L2_TOK_IDENT && tok2->kind == L2_TOK_COLON_EQ) {
l2_trace_scope("assign expression");
l2_trace("ident '%s'", tok->v.str);
char *ident = l2_token_extract_str(tok);
l2_lexer_consume(lexer); // ident
l2_lexer_consume(lexer); // :=

if (parse_expression(lexer, gen, err) < 0) {
return -1;
}

l2_gen_assignment(gen, &ident);
} else {
if (parse_arg_level_expression(lexer, gen, err) < 0) {
return -1;
@@ -229,13 +320,19 @@ int l2_parse_program(
while (1) {
l2_lexer_skip_opt(lexer, L2_TOK_EOL);
if (l2_lexer_peek(lexer, 1)->kind == L2_TOK_EOF) {
return 0;
break;
}

if (parse_expression(lexer, gen, err) < 0) {
l2_gen_halt(gen);
l2_gen_flush(gen);
return -1;
}

l2_gen_pop(gen);
}

l2_gen_halt(gen);
l2_gen_flush(gen);
return 0;
}

Loading…
Cancel
Save