changeset 74:d5330e2b1488

value: Implement yvalue_clone()
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Aug 2019 18:46:06 +0200
parents 7a5caf93cdc2
children a7569b66b12a
files src/value.c src/value.h
diffstat 2 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/value.c	Sun Aug 25 18:45:46 2019 +0200
+++ b/src/value.c	Sun Aug 25 18:46:06 2019 +0200
@@ -79,6 +79,19 @@
     return ref;
 }
 
+yref_t yvalue_clone(yref_t* ref) {
+    yvalue_t* orig = yvalue_get(*ref);
+    assert(orig->typ == YVALUE_EXPR || orig->typ == YVALUE_FUNC);
+    if (orig->typ == YVALUE_FUNC) {
+        // Functions are immutable for now.
+        return *ref;
+    } else if (orig->typ == YVALUE_EXPR) {
+        yvalue_t new = { .typ = YVALUE_EXPR, .value.expr = yexpr_copy(&orig->value.expr) };
+        return yvalue_set(YVALUE_INSERT, &new);
+    }
+    return yref_new_id(YVALUE_INVALID_ID);
+}
+
 ystr_t yref_debug(yref_t* ref) {
     YREF_TYPE typ = yref_type(ref);
     if (typ == YREF_ID) {
--- a/src/value.h	Sun Aug 25 18:45:46 2019 +0200
+++ b/src/value.h	Sun Aug 25 18:46:06 2019 +0200
@@ -76,6 +76,12 @@
 void yref_destroy(yref_t* ref);
 
 /**
+ * @brief Copies the contents of a reference to a new slot and returns a new
+ * reference. (not for functions, which are immutable for now)
+ */
+yref_t yvalue_clone(yref_t* ref);
+
+/**
  * @brief A value with a name, stored in the global value table to be referenced
  * by name or ID. To be used as pointer (not value).
  *