]> git.lizzy.rs Git - nothing.git/commitdiff
(#542) Implement format_list
authorrexim <reximkut@gmail.com>
Sun, 16 Dec 2018 17:47:22 +0000 (00:47 +0700)
committerrexim <reximkut@gmail.com>
Sun, 16 Dec 2018 17:47:22 +0000 (00:47 +0700)
src/ebisp/interpreter.c
src/ebisp/interpreter.h

index a1dce0f330c3198966ec1c97109bdd3fe8f47d69..8b7964c1d8d1c5ed876d7fd4b6231ffdf0ac067d 100644 (file)
@@ -518,4 +518,54 @@ match_list(struct Gc *gc, const char *format, struct Expr xs, ...)
     return eval_success(NIL(gc));
 }
 
-/* TODO(#542): format_list(). Similar to match_list() but for constructing list */
+static struct Expr
+format_list_rec(Gc *gc, const char *format, va_list args)
+{
+    assert(gc);
+    assert(format);
+
+    if (*format == 0) {
+        return NIL(gc);
+    }
+
+    switch (*format) {
+    case 'd': {
+        long int p = va_arg(args, long int);
+        return CONS(gc, NUMBER(gc, p),
+                    format_list_rec(gc, format + 1, args));
+    }
+
+    case 's': {
+        const char* p = va_arg(args, const char*);
+        return CONS(gc, STRING(gc, p),
+                    format_list_rec(gc, format + 1, args));
+    }
+
+    case 'q': {
+        const char* p = va_arg(args, const char*);
+        return CONS(gc, SYMBOL(gc, p),
+                    format_list_rec(gc, format + 1, args));
+    }
+
+    case 'e': {
+        struct Expr p = va_arg(args, struct Expr);
+        return CONS(gc, p, format_list_rec(gc, format + 1, args));
+    }
+
+    default: {
+        fprintf(stderr, "Wrong format parameter: %c\n", *format);
+        assert(0);
+    }
+    }
+}
+
+struct Expr
+format_list(Gc *gc, const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    struct Expr result = format_list_rec(gc, format, args);
+    va_end(args);
+
+    return result;
+}
index fb3153eca758adc2f264443f0b4d79ec9b725970..67572bb13041993b0f998a268b4d6e2b03c0d620 100644 (file)
@@ -29,4 +29,7 @@ void load_std_library(Gc *gc, struct Scope *scope);
 struct EvalResult
 match_list(struct Gc *gc, const char *format, struct Expr args, ...);
 
+struct Expr
+format_list(Gc *gc, const char *format, ...);
+
 #endif  // INTERPRETER_H_