Implement parsing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
add_library(imp
|
||||
am.c
|
||||
memory_stream.c
|
||||
parse.c
|
||||
store.c
|
||||
token.c
|
||||
)
|
||||
|
||||
27
lib/include/parse.h
Normal file
27
lib/include/parse.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef PARSE_H
|
||||
#define PARSE_H
|
||||
|
||||
#include "am.h"
|
||||
#include "store.h"
|
||||
#include "token.h"
|
||||
|
||||
#define PARSE_MAX_DEPTH 128U
|
||||
|
||||
typedef enum {
|
||||
PARSE_STATE_INIT,
|
||||
PARSE_STATE_LIST,
|
||||
PARSE_STATE_DONE,
|
||||
PARSE_STATE_ERROR,
|
||||
} parse_state_t;
|
||||
|
||||
typedef struct {
|
||||
am_t *am;
|
||||
store_t *store;
|
||||
parse_state_t state;
|
||||
parse_state_t *sp, stack[PARSE_MAX_DEPTH];
|
||||
} parse_ctx_t;
|
||||
|
||||
void parse_init(am_t *am, store_t *store, parse_ctx_t *out);
|
||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token);
|
||||
|
||||
#endif
|
||||
119
lib/parse.c
Normal file
119
lib/parse.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "parse.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
void parse_init(am_t *am, store_t *store, parse_ctx_t *out)
|
||||
{
|
||||
out->am = am;
|
||||
out->store = store;
|
||||
out->state = PARSE_STATE_INIT;
|
||||
out->sp = out->stack + PARSE_MAX_DEPTH - 1;
|
||||
}
|
||||
|
||||
static void push_state(parse_ctx_t *ctx, parse_state_t state)
|
||||
{
|
||||
assert(ctx->sp >= ctx->stack);
|
||||
*ctx->sp-- = state;
|
||||
}
|
||||
|
||||
static parse_state_t pop_state(parse_ctx_t *ctx)
|
||||
{
|
||||
assert(ctx->sp < ctx->stack + PARSE_MAX_DEPTH - 1);
|
||||
return *++ctx->sp;
|
||||
}
|
||||
|
||||
static void load_integer(parse_ctx_t *ctx, expr_t **expr, int64_t integer)
|
||||
{
|
||||
*expr = store_alloc(ctx->store);
|
||||
(*expr)->is_atom = true;
|
||||
(*expr)->atom.type = ATOM_TYPE_INTEGER;
|
||||
(*expr)->atom.integer = integer;
|
||||
}
|
||||
|
||||
static void
|
||||
load_symbol(parse_ctx_t *ctx, expr_t **expr, const symbol_t *symbol)
|
||||
{
|
||||
*expr = store_alloc(ctx->store);
|
||||
(*expr)->is_atom = true;
|
||||
(*expr)->atom.type = ATOM_TYPE_SYMBOL;
|
||||
memcpy(&(*expr)->atom.symbol, symbol, sizeof(symbol_t));
|
||||
}
|
||||
|
||||
static expr_t **append(parse_ctx_t *ctx, expr_t *expr)
|
||||
{
|
||||
while (!expr->is_atom)
|
||||
expr = expr->pair.cdr;
|
||||
assert(expr->atom.type == ATOM_TYPE_EMPTY_LIST);
|
||||
expr->is_atom = false;
|
||||
expr->pair.cdr = store_alloc(ctx->store);
|
||||
expr->pair.cdr->is_atom = true;
|
||||
expr->pair.cdr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||
return &expr->pair.car;
|
||||
}
|
||||
|
||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||
{
|
||||
switch (ctx->state) {
|
||||
case PARSE_STATE_INIT:
|
||||
switch (token->type) {
|
||||
case TOKEN_TYPE_INTEGER:
|
||||
load_integer(ctx, &ctx->am->expr, token->integer);
|
||||
ctx->state = PARSE_STATE_DONE;
|
||||
break;
|
||||
case TOKEN_TYPE_SYMBOL:
|
||||
load_symbol(ctx, &ctx->am->expr, &token->symbol);
|
||||
ctx->state = PARSE_STATE_DONE;
|
||||
break;
|
||||
case TOKEN_TYPE_OPEN_PAREN:
|
||||
push_state(ctx, PARSE_STATE_DONE);
|
||||
ctx->am->expr = store_alloc(ctx->store);
|
||||
ctx->am->expr->is_atom = true;
|
||||
ctx->am->expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||
ctx->state = PARSE_STATE_LIST;
|
||||
break;
|
||||
case TOKEN_TYPE_CLOSE_PAREN:
|
||||
ctx->state = PARSE_STATE_ERROR;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case PARSE_STATE_LIST:
|
||||
switch (token->type) {
|
||||
expr_t **end_car;
|
||||
case TOKEN_TYPE_INTEGER:
|
||||
end_car = append(ctx, ctx->am->expr);
|
||||
load_integer(ctx, end_car, token->integer);
|
||||
break;
|
||||
case TOKEN_TYPE_SYMBOL:
|
||||
end_car = append(ctx, ctx->am->expr);
|
||||
load_symbol(ctx, end_car, &token->symbol);
|
||||
break;
|
||||
case TOKEN_TYPE_OPEN_PAREN:
|
||||
am_push(ctx->am);
|
||||
push_state(ctx, PARSE_STATE_LIST);
|
||||
ctx->am->expr = store_alloc(ctx->store);
|
||||
ctx->am->expr->is_atom = true;
|
||||
ctx->am->expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||
ctx->state = PARSE_STATE_LIST;
|
||||
break;
|
||||
case TOKEN_TYPE_CLOSE_PAREN:
|
||||
ctx->state = pop_state(ctx);
|
||||
if (ctx->state == PARSE_STATE_LIST) {
|
||||
expr_t *expr = ctx->am->expr;
|
||||
am_pop(ctx->am);
|
||||
end_car = append(ctx, ctx->am->expr);
|
||||
*end_car = expr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case PARSE_STATE_DONE:
|
||||
case PARSE_STATE_ERROR:
|
||||
break;
|
||||
}
|
||||
|
||||
assert(ctx->state != PARSE_STATE_INIT);
|
||||
return ctx->state;
|
||||
}
|
||||
Reference in New Issue
Block a user