changeset 117:b9ef95fb35dc

preprocess: Implement recursively resolving built-ins.
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 31 Aug 2019 21:30:28 +0200
parents 3267a9631763
children da9989546b1b
files src/CMakeLists.txt src/preprocess.c src/preprocess.h
diffstat 3 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/CMakeLists.txt	Sat Aug 31 21:29:24 2019 +0200
+++ b/src/CMakeLists.txt	Sat Aug 31 21:30:28 2019 +0200
@@ -17,7 +17,8 @@
     value.c)
 
 ADD_LIBRARY(frontend STATIC
-    parse.c)
+    parse.c
+    preprocess.c)
 
 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
     TARGET_LINK_LIBRARIES(core base)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/preprocess.c	Sat Aug 31 21:30:28 2019 +0200
@@ -0,0 +1,31 @@
+#include "preprocess.h"
+
+#include <src/built-ins.h>
+#include <src/value.h>
+
+void ypreprocess_resolve_builtins(yexpr_t* expr) {
+    switch (expr->typ) {
+        case YEXPR_NUMBER:
+        case YEXPR_STRING:
+        case YEXPR_ATOM:
+        case YEXPR_EXCEPTION:
+        case YEXPR_UNDEF:
+            break;
+        case YEXPR_BUILTIN:
+            assert(expr->typ != YEXPR_BUILTIN /* We shouldn't traverse the same expression twice */);
+        case YEXPR_REF:
+            if (yref_type(&expr->value.ref) == YREF_SYM)
+                ybuiltin_translate(expr);
+            break;
+        case YEXPR_LIST:
+            if (expr->value.list.len == 0)
+                break;
+            yvec_t* list = &expr->value.list;
+            for (size_t i = 0; i < list->len; i++) {
+                ypreprocess_resolve_builtins(YVEC_AT(list, i, yexpr_t));
+            }
+            break;
+        default:
+            assert(false /* unexpected expression type! */);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/preprocess.h	Sat Aug 31 21:30:28 2019 +0200
@@ -0,0 +1,22 @@
+#ifndef src_preprocess_h
+#define src_preprocess_h
+
+#include <src/types.h>
+
+/**
+ * @file preprocess.h
+ * @brief Preprocess a parsed expression tree.
+ * @addtogroup frontend
+ * @{
+ */
+
+/**
+ * @brief Replace IDs that reference a built-in by built-in expressions.
+ */
+void ypreprocess_resolve_builtins(yexpr_t* expr);
+
+/**
+ * @}
+ */
+
+#endif