Consolidate expression creation functions in expr module

This commit is contained in:
2025-08-10 13:27:20 +01:00
parent fade9395fa
commit 03bd0ff597
9 changed files with 189 additions and 76 deletions

46
lib/expr.c Normal file
View File

@@ -0,0 +1,46 @@
#include "expr.h"
#include "store.h"
#include <string.h>
expr_t *expr_integer(store_t *store, int64_t value)
{
expr_t *expr = store_alloc(store);
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 = store_alloc(store);
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)
{
symbol_t symbol = { .len = strlen(str) };
memcpy(symbol.buf, str, symbol.len);
return expr_symbol(store, &symbol);
}
expr_t *expr_empty_list(store_t *store)
{
expr_t *expr = store_alloc(store);
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 = store_alloc(store);
expr->is_atom = false;
expr->pair.car = car;
expr->pair.cdr = cdr;
return expr;
}