Add minimal abstract machine module

This commit is contained in:
2025-08-09 19:42:49 +01:00
parent e50fd10f9b
commit f97cea9290
5 changed files with 71 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
add_library(imp
am.c
memory_stream.c
store.c
token.c

22
lib/am.c Normal file
View File

@@ -0,0 +1,22 @@
#include "am.h"
#include <assert.h>
#include <string.h>
void am_init(am_t *am)
{
memset(am, 0, sizeof(am_t));
am->sp = am->stack + AM_STACK_SIZE - 1;
}
void am_push(am_t *am)
{
assert(am->sp >= am->stack);
*am->sp-- = am->expr;
}
void am_pop(am_t *am)
{
assert(am->sp < am->stack + AM_STACK_SIZE - 1);
am->expr = *++am->sp;
}

17
lib/include/am.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef AM_H
#define AM_H
#include "expr.h"
#define AM_STACK_SIZE 128U
typedef struct {
expr_t *expr;
expr_t **sp, *stack[AM_STACK_SIZE];
} am_t;
void am_init(am_t *am);
void am_push(am_t *am);
void am_pop(am_t *am);
#endif