aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2021-01-03 01:19:12 +0300
committerAleksey Veresov <aleksey@veresov.pro>2021-01-03 01:19:12 +0300
commitabfe132b83219efa30220ecfdaf6e26e31b9d13d (patch)
tree70d33ac10f4e3e24b0e3ac496017f465dcdec1ae
parentfaab24e5a6766880c7e4a33433cb4146e3f8a302 (diff)
downloadcsx-abfe132b83219efa30220ecfdaf6e26e31b9d13d.tar
csx-abfe132b83219efa30220ecfdaf6e26e31b9d13d.tar.xz
csx-abfe132b83219efa30220ecfdaf6e26e31b9d13d.zip
Input-output added.
-rw-r--r--examples/generated.c6
-rw-r--r--examples/translator.c2
-rw-r--r--src/csx.c33
3 files changed, 39 insertions, 2 deletions
diff --git a/examples/generated.c b/examples/generated.c
new file mode 100644
index 0000000..0b70fd6
--- /dev/null
+++ b/examples/generated.c
@@ -0,0 +1,6 @@
+#include <csx.h>
+int main()
+{
+csx_eval(csx_list(csx_name("do"),csx_list(csx_name("define"),csx_name("a"),csx_num(10),0),csx_list(csx_name("define"),csx_name("b"),csx_list(csx_name("sum"),csx_name("a"),csx_num(3),0),0),csx_list(csx_name("outname"),csx_list(csx_name("quote"),csx_name("sum-of-10-and-3-is"),0),0),csx_list(csx_name("outnum"),csx_name("b"),0),0));
+return 0;
+}
diff --git a/examples/translator.c b/examples/translator.c
index 448d052..ad676c0 100644
--- a/examples/translator.c
+++ b/examples/translator.c
@@ -14,7 +14,7 @@ void readlist()
readlist();
printf("0)");
c = getchar();
- } else if (isdigit(c)) {
+ } else if (isdigit(c) || c == '-') {
if (first) first = 0;
else putchar(',');
printf("csx_num(");
diff --git a/src/csx.c b/src/csx.c
index 7225792..1a643f2 100644
--- a/src/csx.c
+++ b/src/csx.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include <stdio.h>
typedef enum csx_type {
@@ -380,7 +381,33 @@ static void *base_context(void *arg)
static void *base_quote(void *arg)
{
- return arg;
+ return head(arg);
+}
+
+static void *base_input(void *arg)
+{
+ int res = getchar();
+ return res != EOF ? new_num(res) : null;
+}
+
+static void *base_output(void *arg)
+{
+ arg = eval_each(arg);
+ return putchar(*(int *)head(arg)) != EOF ? one : null;
+}
+
+static void *base_outname(void *arg)
+{
+ arg = eval_each(arg);
+ return fputs(head(arg), stdout) != EOF ? one : null;
+}
+
+static void *base_outnum(void *arg)
+{
+ arg = eval_each(arg);
+ int num = *(int *)head(arg);
+ printf("%d", num);
+ return one;
}
static void *base_fn(void *arg)
@@ -500,6 +527,10 @@ static void new_context()
base_define(csx_list(new_name("if"), new_base(base_if), 0));
base_define(csx_list(new_name("and"), new_base(base_and), 0));
base_define(csx_list(new_name("or"), new_base(base_or), 0));
+ base_define(csx_list(new_name("input"), new_base(base_input), 0));
+ base_define(csx_list(new_name("output"), new_base(base_output), 0));
+ base_define(csx_list(new_name("outname"), new_base(base_outname), 0));
+ base_define(csx_list(new_name("outnum"), new_base(base_outnum), 0));
}