diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2021-01-04 15:03:31 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2021-01-04 15:03:31 +0300 |
commit | ae882f68193e00deebce41c32df644b6cb2f2a47 (patch) | |
tree | 336389aa597e73ebb0c75e4e08a1dd802d9acccf | |
parent | 0b4de175110bdf6ffaeb0ba3b86053fd2fec78be (diff) | |
download | csx-ae882f68193e00deebce41c32df644b6cb2f2a47.tar csx-ae882f68193e00deebce41c32df644b6cb2f2a47.tar.xz csx-ae882f68193e00deebce41c32df644b6cb2f2a47.zip |
.
-rw-r--r-- | examples/generated.c | 2 | ||||
-rw-r--r-- | examples/generated.csx | 3 | ||||
-rw-r--r-- | examples/translator.c | 48 | ||||
-rw-r--r-- | include/csx.h | 2 | ||||
-rw-r--r-- | src/csx.c | 22 |
5 files changed, 76 insertions, 1 deletions
diff --git a/examples/generated.c b/examples/generated.c index a428c03..e0c3b3b 100644 --- a/examples/generated.c +++ b/examples/generated.c @@ -1,6 +1,6 @@ #include <csx.h> int main() { -csx_run(csx_list(csx_name("do"),csx_list(csx_name("set"),csx_name("no"),csx_list(csx_name("fn"),csx_list(csx_name("x"),0),csx_list(csx_name("same"),csx_name("x"),csx_list(0),0),0),0),csx_list(csx_name("set"),csx_name("outint"),csx_list(csx_name("fn"),csx_list(csx_name("n"),0),csx_list(csx_name("if"),csx_list(csx_name("<"),csx_name("n"),csx_int(0),0),csx_list(csx_name("do"),csx_list(csx_name("out"),csx_int(45),0),csx_list(csx_name("outint"),csx_list(csx_name("neg"),csx_name("n"),0),0),0),csx_list(csx_name("if"),csx_list(csx_name("<"),csx_name("n"),csx_int(10),0),csx_list(csx_name("out"),csx_list(csx_name("+"),csx_int(48),csx_name("n"),0),0),csx_list(csx_name("do"),csx_list(csx_name("outint"),csx_list(csx_name("div"),csx_name("n"),csx_int(10),0),0),csx_list(csx_name("out"),csx_list(csx_name("+"),csx_int(48),csx_list(csx_name("mod"),csx_name("n"),csx_int(10),0),0),0),0),0),0),0),0),csx_list(csx_name("set"),csx_name("map"),csx_list(csx_name("fn"),csx_list(csx_name("f"),csx_name("l"),0),csx_list(csx_name("if"),csx_list(csx_name("no"),csx_name("l"),0),csx_list(0),csx_list(csx_name("pair"),csx_list(csx_name("f"),csx_list(csx_name("head"),csx_name("l"),0),0),csx_list(csx_name("map"),csx_name("f"),csx_list(csx_name("tail"),csx_name("l"),0),0),0),0),0),0),csx_list(csx_name("map"),csx_list(csx_name("fn"),csx_list(csx_name("n"),0),csx_list(csx_name("outint"),csx_name("n"),0),csx_list(csx_name("out"),csx_int(10),0),0),csx_list(csx_name("quote"),csx_list(csx_int(99),csx_int(37),csx_int(73),csx_int(42),0),0),0),0)); +csx_run(csx_list(csx_name("do"),csx_list(csx_name("set"),csx_name("no"),csx_list(csx_name("fn"),csx_list(csx_name("x"),0),csx_list(csx_name("same"),csx_name("x"),csx_list(0),0),0),0),csx_list(csx_name("set"),csx_name("outint"),csx_list(csx_name("fn"),csx_list(csx_name("n"),0),csx_list(csx_name("if"),csx_list(csx_name("<"),csx_name("n"),csx_int(0),0),csx_list(csx_name("do"),csx_list(csx_name("out"),csx_int(45),0),csx_list(csx_name("outint"),csx_list(csx_name("neg"),csx_name("n"),0),0),0),csx_list(csx_name("if"),csx_list(csx_name("<"),csx_name("n"),csx_int(10),0),csx_list(csx_name("out"),csx_list(csx_name("+"),csx_int(48),csx_name("n"),0),0),csx_list(csx_name("do"),csx_list(csx_name("outint"),csx_list(csx_name("div"),csx_name("n"),csx_int(10),0),0),csx_list(csx_name("out"),csx_list(csx_name("+"),csx_int(48),csx_list(csx_name("mod"),csx_name("n"),csx_int(10),0),0),0),0),0),0),0),0),csx_list(csx_name("set"),csx_name("map"),csx_list(csx_name("fn"),csx_list(csx_name("f"),csx_name("l"),0),csx_list(csx_name("if"),csx_list(csx_name("no"),csx_name("l"),0),csx_list(0),csx_list(csx_name("pair"),csx_list(csx_name("f"),csx_list(csx_name("head"),csx_name("l"),0),0),csx_list(csx_name("map"),csx_name("f"),csx_list(csx_name("tail"),csx_name("l"),0),0),0),0),0),0),csx_list(csx_name("map"),csx_list(csx_name("fn"),csx_list(csx_name("n"),0),csx_list(csx_name("outint"),csx_name("n"),0),csx_list(csx_name("out"),csx_int(10),0),0),csx_list(csx_name("quote"),csx_list(csx_int(99),csx_int(37),csx_int(73),csx_int(42),0),0),0),csx_list(csx_name("set"),csx_name("id"),csx_list(csx_name("fn"),csx_name("args"),csx_name("args"),0),0),csx_list(csx_name("outint"),csx_list(csx_list(csx_name("id"),csx_int(42),csx_int(13),csx_int(31),0),csx_int(2),0),0),csx_list(csx_name("out"),csx_int(10),0),0)); return 0; } diff --git a/examples/generated.csx b/examples/generated.csx index cb2557c..4d3d5fb 100644 --- a/examples/generated.csx +++ b/examples/generated.csx @@ -15,3 +15,6 @@ [map [fn [n] [outint n] [out 10]] [quote [99 37 73 42]] ] +[set id [fn args args]] +[outint [[id 42 13 31] 2]] +[out 10] diff --git a/examples/translator.c b/examples/translator.c index 14b7482..8358a47 100644 --- a/examples/translator.c +++ b/examples/translator.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <ctype.h> +#include <stdlib.h> void readlist() @@ -27,11 +28,58 @@ void readlist() printf(")"); } else if (isspace(c)) { while (isspace(c = getchar())); + } else if (c == '.') { + if (getchar() != '[') exit(1); + if (first) first = 0; + else putchar(','); + printf("csx_dot("); + readlist(); + printf("0)"); + c = getchar(); + } else if (c == '"') { + if (first) first = 0; + else putchar(','); + printf("csx_str(\""); + while (c != EOF && c != '"') { + if (c == '\\') c = getchar(); + if (c == '"' || c == '\\') putchar('\\'); + putchar(c); + c = getchar(); + } + printf("\")"); + } else if (c == '\'') { + if (first) first = 0; + else putchar(','); + printf("csx_list(csx_name(\"quote\"),"); + c = getchar(); + if (isspace(c)) exit(1); + else if (c != '[') { + printf("csx_name(\""); + while (c != EOF && !isspace(c) && c != '[' && c != ']') { + if (c == '"' || c == '\\') putchar('\\'); + putchar(c); + c = getchar(); + } + printf("\"),"); + } else if (c == '.') { + if (getchar() != '[') exit(1); + printf("csx_dot("); + readlist(); + printf("0)"); + c = getchar(); + } else { + printf("csx_list("); + readlist(); + printf("0),"); + c = getchar(); + } + printf("0)"); } else { if (first) first = 0; else putchar(','); printf("csx_name(\""); while (c != EOF && !isspace(c) && c != '[' && c != ']') { + if (c == '\\') c = getchar(); if (c == '"' || c == '\\') putchar('\\'); putchar(c); c = getchar(); diff --git a/include/csx.h b/include/csx.h index 56a0114..8d1e8f0 100644 --- a/include/csx.h +++ b/include/csx.h @@ -18,5 +18,7 @@ csx_base_data *csx_base(csx_base_data base); char *csx_name(const char *name); +void *csx_str(const char *str); + #endif @@ -360,6 +360,8 @@ static void *zip(void *params, void *values) params = tail(params); values = tail(values); } + if (type(params) == type_name) + res = new_pair(new_pair(params, values), res); return res; } @@ -392,6 +394,13 @@ tailcall: res = base_do(fn->body); context = saved; return res; + } else if (type(fn) == type_pair) { + pair_data *res = (void *)fn; + int pos = *(int *)head(ops); + while (pos--) res = res->tail; + return res->head; + } else if (type(fn) == type_null) { + return null; } else { exit(1); } @@ -477,3 +486,16 @@ void *csx_dot(void *a, void *b, void *c, ...) va_end(args); return res; } + +void *csx_str(const char *str) +{ + if (!initiated) init(); + if (!str || !*str) return null; + pair_data *res = new_pair(csx_int(*str), null); + pair_data **p = (pair_data **)&res->tail; + while (*++str) { + *p = new_pair(csx_int(*str), null); + p = (pair_data **)&(*p)->tail; + } + return res; +} |