Mercurial > lbo > hg > ylisp
changeset 41:a3eac54f2112
expr: Add yref_new* methods
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 21 Aug 2019 13:08:52 +0200 |
parents | 63b0bf003bb9 |
children | 9bead1335e42 |
files | src/expr.c src/expr.h |
diffstat | 2 files changed, 31 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/expr.c Wed Aug 21 13:08:16 2019 +0200 +++ b/src/expr.c Wed Aug 21 13:08:52 2019 +0200 @@ -1,9 +1,25 @@ #include "expr.h" +#include <src/base/str.h> +#include <src/base/vec.h> + #include <assert.h> #include <stdbool.h> #include <stdio.h> +yref_t yref_new_id(yvalue_id_t id) { + yref_t ref = { .typ = YREF_ID, .ref.id = id }; + return ref; +} +yref_t yref_new_str(ystr_t* sym) { + yref_t ref = { .typ = YREF_SYM, .ref.sym = *sym }; + return ref; +} +yref_t yref_new_c(const char* sym) { + yref_t ref = { .typ = YREF_SYM, .ref.sym = ystr_new(sym) }; + return ref; +} + void yref_destroy(yref_t* ref) { if (ref == NULL) return; if (ref->typ == YREF_SYM) ystr_destroy(&ref->ref.sym);
--- a/src/expr.h Wed Aug 21 13:08:16 2019 +0200 +++ b/src/expr.h Wed Aug 21 13:08:52 2019 +0200 @@ -36,6 +36,21 @@ } yref_t; /** + * Create a new reference with the given ID. + */ +yref_t yref_new_id(yvalue_id_t id); + +/** + * Create a new reference to the given symbol. Takes ownership of `sym`, it must not be freed afterwards. + */ +yref_t yref_new_str(ystr_t* sym); + +/** + * Create a new reference to the given symbol. + */ +yref_t yref_new_c(const char* sym); + +/** * @brief Deallocate memory used by the reference itself. */ void yref_destroy(yref_t* ref);