From 672512b0a2b3b4ae88f204293f4379c1fbb79850 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Wed, 6 Jan 2021 01:37:05 +0300 Subject: Added interpreter. --- examples/translator.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'examples/translator.c') diff --git a/examples/translator.c b/examples/translator.c index 01920c9..26bd5ac 100644 --- a/examples/translator.c +++ b/examples/translator.c @@ -20,10 +20,11 @@ void name(char a) printf("\")"); } -void num(char a) +void num(char a, char b) { printf("csx_int("); if (a) putchar(a); + if (b) putchar(b); c = getchar(); while (c != EOF && isdigit(c)) { putchar(c); @@ -32,6 +33,14 @@ void num(char a) printf(")"); } +void list() +{ + printf("csx_list("); + items(); + printf("0)"); + c = getchar(); +} + void pair() { if ((c = getchar()) == '[') { @@ -42,12 +51,15 @@ void pair() } else name('='); } -void list() +void quote() { - printf("csx_list("); - items(); - printf("0)"); + printf("csx_list(csx_name(\"quote\"),"); c = getchar(); + if (c == '[') list(); + else if (c == '=') pair(); + else if (c == '\'') quote(); + else name(0); + printf(",0)"); } void skip() @@ -75,9 +87,9 @@ void items() if (first) first = 0; else putchar(','); if (c == '[') list(); else if (c == '-') - if (isdigit(c = getchar())) num('-'); + if (isdigit(c = getchar())) num('-', c); else name('-'); - else if (isdigit(c)) num(c); + else if (isdigit(c)) num(c, 0); else if (c == '=') pair(); else if (c == '"') { printf("csx_str(\""); @@ -90,14 +102,8 @@ void items() } printf("\")"); c = getchar(); - } else if (c == '\'') { - printf("csx_list(csx_name(\"quote\"),"); - c = getchar(); - if (c == '[') list(); - else if (c == '=') pair(); - else name(0); - printf(",0)"); - } else name(0); + } else if (c == '\'') quote(); + else name(0); skip(); } if (!first) putchar(','); -- cgit v1.2.3