Add store into abstract machine struct
This commit is contained in:
5
lib/am.c
5
lib/am.c
@@ -1,5 +1,8 @@
|
||||
#include "am.h"
|
||||
|
||||
#include "env.h"
|
||||
#include "store.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -7,6 +10,8 @@ void am_init(am_t *am)
|
||||
{
|
||||
memset(am, 0, sizeof(am_t));
|
||||
am->sp = am->stack + AM_STACK_SIZE - 1;
|
||||
store_init(am);
|
||||
env_init(am);
|
||||
}
|
||||
|
||||
void am_push(am_t *am)
|
||||
|
||||
12
lib/env.c
12
lib/env.c
@@ -49,10 +49,10 @@ static expr_t **lookup(am_t *am, bool *found)
|
||||
return &prev->pair.cdr;
|
||||
}
|
||||
|
||||
void env_init(am_t *am, store_t *store)
|
||||
void env_init(am_t *am)
|
||||
{
|
||||
am->env = expr_empty_list(store);
|
||||
prim_load(am, store);
|
||||
am->env = expr_empty_list(am);
|
||||
prim_load(am);
|
||||
}
|
||||
|
||||
void env_fetch(am_t *am)
|
||||
@@ -62,7 +62,7 @@ void env_fetch(am_t *am)
|
||||
am->val = found ? val : NULL;
|
||||
}
|
||||
|
||||
void env_set(am_t *am, store_t *store)
|
||||
void env_set(am_t *am)
|
||||
{
|
||||
bool found;
|
||||
expr_t **loc = lookup(am, &found);
|
||||
@@ -70,7 +70,7 @@ void env_set(am_t *am, store_t *store)
|
||||
*loc = am->val;
|
||||
} else {
|
||||
(*loc)->is_atom = false;
|
||||
(*loc)->pair.cdr = expr_empty_list(store);
|
||||
(*loc)->pair.car = expr_pair(store, am->expr, am->val);
|
||||
(*loc)->pair.cdr = expr_empty_list(am);
|
||||
(*loc)->pair.car = expr_pair(am, am->expr, am->val);
|
||||
}
|
||||
}
|
||||
|
||||
27
lib/expr.c
27
lib/expr.c
@@ -1,53 +1,54 @@
|
||||
#include "expr.h"
|
||||
#include "store.h"
|
||||
|
||||
#include "am.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
expr_t *expr_integer(store_t *store, int64_t value)
|
||||
expr_t *expr_integer(am_t *am, int64_t value)
|
||||
{
|
||||
expr_t *expr = store_alloc(store);
|
||||
expr_t *expr = store_alloc(am);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_INTEGER;
|
||||
expr->atom.integer = value;
|
||||
return expr;
|
||||
}
|
||||
|
||||
expr_t *expr_symbol(store_t *store, const symbol_t *symbol)
|
||||
expr_t *expr_symbol(am_t *am, const symbol_t *symbol)
|
||||
{
|
||||
expr_t *expr = store_alloc(store);
|
||||
expr_t *expr = store_alloc(am);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_SYMBOL;
|
||||
memcpy(&expr->atom.symbol, symbol, sizeof(symbol_t));
|
||||
return expr;
|
||||
}
|
||||
|
||||
expr_t *expr_str_symbol(store_t *store, const char *str)
|
||||
expr_t *expr_str_symbol(am_t *am, const char *str)
|
||||
{
|
||||
symbol_t symbol = { .len = strlen(str) };
|
||||
memcpy(symbol.buf, str, symbol.len);
|
||||
return expr_symbol(store, &symbol);
|
||||
return expr_symbol(am, &symbol);
|
||||
}
|
||||
|
||||
expr_t *expr_empty_list(store_t *store)
|
||||
expr_t *expr_empty_list(am_t *am)
|
||||
{
|
||||
expr_t *expr = store_alloc(store);
|
||||
expr_t *expr = store_alloc(am);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||
return expr;
|
||||
}
|
||||
|
||||
expr_t *expr_pair(store_t *store, expr_t *car, expr_t *cdr)
|
||||
expr_t *expr_pair(am_t *am, expr_t *car, expr_t *cdr)
|
||||
{
|
||||
expr_t *expr = store_alloc(store);
|
||||
expr_t *expr = store_alloc(am);
|
||||
expr->is_atom = false;
|
||||
expr->pair.car = car;
|
||||
expr->pair.cdr = cdr;
|
||||
return expr;
|
||||
}
|
||||
|
||||
expr_t *expr_prim_proc(store_t *store, prim_proc_t prim_proc)
|
||||
expr_t *expr_prim_proc(am_t *am, prim_proc_t prim_proc)
|
||||
{
|
||||
expr_t *expr = store_alloc(store);
|
||||
expr_t *expr = store_alloc(am);
|
||||
expr->is_atom = true;
|
||||
expr->atom.type = ATOM_TYPE_PRIM_PROC;
|
||||
expr->atom.prim_proc = prim_proc;
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
#define AM_H
|
||||
|
||||
#include "expr.h"
|
||||
#include "store.h"
|
||||
|
||||
#define AM_STACK_SIZE 128U
|
||||
|
||||
typedef struct am {
|
||||
expr_t *argl, *env, *expr, *val;
|
||||
expr_t **sp, *stack[AM_STACK_SIZE];
|
||||
store_t store;
|
||||
} am_t;
|
||||
|
||||
void am_init(am_t *am);
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#define ENV_H
|
||||
|
||||
#include "am.h"
|
||||
#include "store.h"
|
||||
|
||||
void env_init(am_t *am, store_t *store);
|
||||
void env_init(am_t *am);
|
||||
void env_fetch(am_t *am);
|
||||
void env_set(am_t *am, store_t *store);
|
||||
void env_set(am_t *am);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -8,14 +8,13 @@
|
||||
#include <stdint.h>
|
||||
|
||||
struct am;
|
||||
struct store;
|
||||
|
||||
typedef struct {
|
||||
char buf[MAX_SYMBOL_LEN];
|
||||
size_t len;
|
||||
} symbol_t;
|
||||
|
||||
typedef void (*prim_proc_t)(struct am *am, struct store *store);
|
||||
typedef void (*prim_proc_t)(struct am *am);
|
||||
|
||||
typedef enum {
|
||||
ATOM_TYPE_EMPTY_LIST,
|
||||
@@ -47,11 +46,11 @@ typedef struct expr {
|
||||
};
|
||||
} expr_t;
|
||||
|
||||
expr_t *expr_integer(struct store *store, int64_t value);
|
||||
expr_t *expr_symbol(struct store *store, const symbol_t *symbol);
|
||||
expr_t *expr_str_symbol(struct store *store, const char *str);
|
||||
expr_t *expr_empty_list(struct store *store);
|
||||
expr_t *expr_pair(struct store *store, expr_t *car, expr_t *cdr);
|
||||
expr_t *expr_prim_proc(struct store *store, prim_proc_t prim_proc);
|
||||
expr_t *expr_integer(struct am *am, int64_t value);
|
||||
expr_t *expr_symbol(struct am *am, const symbol_t *symbol);
|
||||
expr_t *expr_str_symbol(struct am *am, const char *str);
|
||||
expr_t *expr_empty_list(struct am *am);
|
||||
expr_t *expr_pair(struct am *am, expr_t *car, expr_t *cdr);
|
||||
expr_t *expr_prim_proc(struct am *am, prim_proc_t prim_proc);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define PARSE_H
|
||||
|
||||
#include "am.h"
|
||||
#include "store.h"
|
||||
#include "token.h"
|
||||
|
||||
#define PARSE_MAX_DEPTH 128U
|
||||
@@ -21,7 +20,7 @@ typedef struct {
|
||||
parse_state_t *sp, stack[PARSE_MAX_DEPTH];
|
||||
} parse_ctx_t;
|
||||
|
||||
void parse_init(am_t *am, store_t *store, parse_ctx_t *out);
|
||||
void parse_init(am_t *am, parse_ctx_t *out);
|
||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
|
||||
#include "am.h"
|
||||
#include "expr.h"
|
||||
#include "store.h"
|
||||
|
||||
void prim_load(am_t *am, store_t *store);
|
||||
void prim_load(am_t *am);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,7 +10,9 @@ typedef struct store {
|
||||
expr_t buffer[STORE_SIZE];
|
||||
} store_t;
|
||||
|
||||
void store_init(store_t *store);
|
||||
expr_t *store_alloc(store_t *store);
|
||||
struct am;
|
||||
|
||||
void store_init(struct am *am);
|
||||
expr_t *store_alloc(struct am *am);
|
||||
|
||||
#endif
|
||||
|
||||
17
lib/parse.c
17
lib/parse.c
@@ -3,10 +3,9 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
void parse_init(am_t *am, store_t *store, parse_ctx_t *out)
|
||||
void parse_init(am_t *am, parse_ctx_t *out)
|
||||
{
|
||||
out->am = am;
|
||||
out->store = store;
|
||||
out->state = PARSE_STATE_INIT;
|
||||
out->sp = out->stack + PARSE_MAX_DEPTH - 1;
|
||||
}
|
||||
@@ -32,7 +31,7 @@ static void append(parse_ctx_t *ctx, expr_t *expr)
|
||||
|
||||
list->is_atom = false;
|
||||
list->pair.car = expr;
|
||||
list->pair.cdr = expr_empty_list(ctx->store);
|
||||
list->pair.cdr = expr_empty_list(ctx->am);
|
||||
}
|
||||
|
||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||
@@ -41,16 +40,16 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||
case PARSE_STATE_INIT:
|
||||
switch (token->type) {
|
||||
case TOKEN_TYPE_INTEGER:
|
||||
ctx->am->expr = expr_integer(ctx->store, token->integer);
|
||||
ctx->am->expr = expr_integer(ctx->am, token->integer);
|
||||
ctx->state = PARSE_STATE_DONE;
|
||||
break;
|
||||
case TOKEN_TYPE_SYMBOL:
|
||||
ctx->am->expr = expr_symbol(ctx->store, &token->symbol);
|
||||
ctx->am->expr = expr_symbol(ctx->am, &token->symbol);
|
||||
ctx->state = PARSE_STATE_DONE;
|
||||
break;
|
||||
case TOKEN_TYPE_OPEN_PAREN:
|
||||
push_state(ctx, PARSE_STATE_DONE);
|
||||
ctx->am->expr = expr_empty_list(ctx->store);
|
||||
ctx->am->expr = expr_empty_list(ctx->am);
|
||||
ctx->state = PARSE_STATE_LIST;
|
||||
break;
|
||||
case TOKEN_TYPE_CLOSE_PAREN:
|
||||
@@ -62,15 +61,15 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||
case PARSE_STATE_LIST:
|
||||
switch (token->type) {
|
||||
case TOKEN_TYPE_INTEGER:
|
||||
append(ctx, expr_integer(ctx->store, token->integer));
|
||||
append(ctx, expr_integer(ctx->am, token->integer));
|
||||
break;
|
||||
case TOKEN_TYPE_SYMBOL:
|
||||
append(ctx, expr_symbol(ctx->store, &token->symbol));
|
||||
append(ctx, expr_symbol(ctx->am, &token->symbol));
|
||||
break;
|
||||
case TOKEN_TYPE_OPEN_PAREN:
|
||||
am_push(ctx->am);
|
||||
push_state(ctx, PARSE_STATE_LIST);
|
||||
ctx->am->expr = expr_empty_list(ctx->store);
|
||||
ctx->am->expr = expr_empty_list(ctx->am);
|
||||
ctx->state = PARSE_STATE_LIST;
|
||||
break;
|
||||
case TOKEN_TYPE_CLOSE_PAREN:
|
||||
|
||||
20
lib/prim.c
20
lib/prim.c
@@ -11,7 +11,7 @@ typedef struct {
|
||||
prim_proc_t prim_proc;
|
||||
} prim_table_entry_t;
|
||||
|
||||
static void add(am_t *am, store_t *store)
|
||||
static void add(am_t *am)
|
||||
{
|
||||
assert(am->argl);
|
||||
|
||||
@@ -21,10 +21,10 @@ static void add(am_t *am, store_t *store)
|
||||
assert(list->pair.car->atom.type == ATOM_TYPE_INTEGER);
|
||||
total += list->pair.car->atom.integer;
|
||||
}
|
||||
am->val = expr_integer(store, total);
|
||||
am->val = expr_integer(am, total);
|
||||
}
|
||||
|
||||
static void mul(am_t *am, store_t *store)
|
||||
static void mul(am_t *am)
|
||||
{
|
||||
assert(am->argl);
|
||||
|
||||
@@ -34,10 +34,10 @@ static void mul(am_t *am, store_t *store)
|
||||
assert(list->pair.car->atom.type == ATOM_TYPE_INTEGER);
|
||||
total *= list->pair.car->atom.integer;
|
||||
}
|
||||
am->val = expr_integer(store, total);
|
||||
am->val = expr_integer(am, total);
|
||||
}
|
||||
|
||||
static void sub(am_t *am, store_t *store)
|
||||
static void sub(am_t *am)
|
||||
{
|
||||
assert(am->argl);
|
||||
assert(!am->argl->is_atom);
|
||||
@@ -55,7 +55,7 @@ static void sub(am_t *am, store_t *store)
|
||||
total -= list->pair.car->atom.integer;
|
||||
}
|
||||
}
|
||||
am->val = expr_integer(store, total);
|
||||
am->val = expr_integer(am, total);
|
||||
}
|
||||
|
||||
static const prim_table_entry_t prim_table[] = {
|
||||
@@ -64,11 +64,11 @@ static const prim_table_entry_t prim_table[] = {
|
||||
{ "-", sub },
|
||||
};
|
||||
|
||||
void prim_load(am_t *am, store_t *store)
|
||||
void prim_load(am_t *am)
|
||||
{
|
||||
for (unsigned i = 0; i < NELEMS(prim_table); ++i) {
|
||||
am->expr = expr_str_symbol(store, prim_table[i].name);
|
||||
am->val = expr_prim_proc(store, prim_table[i].prim_proc);
|
||||
env_set(am, store);
|
||||
am->expr = expr_str_symbol(am, prim_table[i].name);
|
||||
am->val = expr_prim_proc(am, prim_table[i].prim_proc);
|
||||
env_set(am);
|
||||
}
|
||||
}
|
||||
|
||||
12
lib/store.c
12
lib/store.c
@@ -1,14 +1,16 @@
|
||||
#include "store.h"
|
||||
|
||||
#include "am.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void store_init(store_t *store)
|
||||
void store_init(am_t *am)
|
||||
{
|
||||
memset(store, 0, sizeof(store_t));
|
||||
store->free = store->buffer;
|
||||
memset(&am->store, 0, sizeof(store_t));
|
||||
am->store.free = am->store.buffer;
|
||||
}
|
||||
|
||||
expr_t *store_alloc(store_t *store)
|
||||
expr_t *store_alloc(am_t *am)
|
||||
{
|
||||
return store->free++;
|
||||
return am->store.free++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user