aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2021-01-10 02:37:23 +0300
committerAleksey Veresov <aleksey@veresov.pro>2021-01-10 02:37:23 +0300
commitea1cd66193d64bb1cd56c24f0db011eadbf558e6 (patch)
tree9447d65fc7c31833842dcb8564a45a60c9ec2c14
parenta0f273300aec59d31eb332e531efd8249fc67f20 (diff)
downloadcsx-ea1cd66193d64bb1cd56c24f0db011eadbf558e6.tar
csx-ea1cd66193d64bb1cd56c24f0db011eadbf558e6.tar.xz
csx-ea1cd66193d64bb1cd56c24f0db011eadbf558e6.zip
Optimizations + human-readable translator output.
-rw-r--r--examples/add.c17
-rw-r--r--examples/generated.csx67
-rw-r--r--examples/interpreter.csx36
-rw-r--r--examples/translator.c37
-rw-r--r--src/csx.c2
5 files changed, 31 insertions, 128 deletions
diff --git a/examples/add.c b/examples/add.c
deleted file mode 100644
index 1c43c9b..0000000
--- a/examples/add.c
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <csx.h>
-#include <stdio.h>
-
-
-static csx_list_fn *l;
-static csx_int_fn *i;
-
-
-int main()
-{
- l = csx_list;
- i = csx_int;
- char *plus = csx_name("+");
- printf("%d\n", *(int *)csx_run(l(plus, i(37), i(73), 0)));
- csx_free();
- return 0;
-}
diff --git a/examples/generated.csx b/examples/generated.csx
deleted file mode 100644
index 574152f..0000000
--- a/examples/generated.csx
+++ /dev/null
@@ -1,67 +0,0 @@
-{ Base Utilities }
-
-[set no [fn [x] [same x []]]]
-[set id [fn [arg] arg]]
-[set list [fn args args]]
-
-[set catrev [fn [a b] [if a [catrev [tail a] [pair [head a] b]] b]]]
-[set rev [fn [l] [catrev l []]]]
-[set cat [fn [a b] [catrev [rev a] b]]]
-
-[set map [fn [f l] [if l [pair [f [head l]] [map f [tail l]]]]]]
-[set reduce [fn [f l] [if [no l] [] [if [no [tail l]] [head l]
- [f [head l] [reduce f [tail l]]]
-]]]]
-
-[set - [fn =[a rest] [+ a [reduce + [map neg rest]]]]]
-
-
-{ Input-Output }
-
-[set newline [str [list 10]]]
-
-[set outint [fn [n]
- [set zero 48]
- [set minus 45]
- [if [< n 0]
- [do [out minus] [outint [neg n]]]
- [if [< n 10]
- [out [+ zero n]]
- [do
- [outint [div n 10]]
- [out [+ zero [mod n 10]]]
- ]
- ]
- ]
-]]
-
-[set outstr [fn [str]
- [set outstrat [fn [str i len] [if [no [same i len]] [do
- [out [str i]]
- [outstrat str [+ i 1] len]
- ]]]]
- [outstrat str 0 [len str]]
-]]
-
-[set output [fn objs [map [fn [obj] [if
- [same [type obj] 'int] [outint obj]
- [same [type obj] 'str] [outstr obj]
-]] objs]]]
-
-[set instr [fn []
- [set instract [fn []
- [set c [in]]
- [if [no [same c 10]] [pair c [instract]]]
- ]]
- [str [instract]]
-]]
-
-
-{ The Program }
-
-[output "Hello, I am Casey Shawn Exton. What is your name?" newline]
-[output "> "]
-[set name [instr]]
-[output "Nice to meet you, " name "." newline]
-[output "Your name is " [len name] " characters long." newline]
-[output "I have to go. Goodbye!" newline]
diff --git a/examples/interpreter.csx b/examples/interpreter.csx
index 4632a24..2d87e81 100644
--- a/examples/interpreter.csx
+++ b/examples/interpreter.csx
@@ -207,40 +207,4 @@
[read [tail res]]
]]
-[output "-= CSX interpreter loaded =-" newline]
-[output "========= Base functions: ========================================" newline]
-[output "[set name value] | name will have the value value" newline]
-[output "[set? name] | check if name is set as some value" newline]
-[output "[sethead p v] | head of p will be changed to the v value" newline]
-[output "[settail p v] | head of p will be changed to the v value" newline]
-[output "[pair head tail] | creates pair of head and value" newline]
-[output "[head pair] | returns head of pair" newline]
-[output "[tail pair] | returns tail of pair" newline]
-[output "[quote x] | returns unevaluated x" newline]
-[output "[same a b] | checks if a and b are the same thing" newline]
-[output "[type x] | returns type of x" newline]
-[output "[do ...] | runs statements returning value of the last one" newline]
-[output "[fn args ...] | creates function" newline]
-[output " | you can make vararg function with pairs" newline]
-[output "[sx args ...] | same as fn but for macro" newline]
-[output "[if a b ... c] | executes b if a (for each a), else c" newline]
-[output "[+ ...] | sum of the arguments" newline]
-[output "[* ...] | product of the arguments" newline]
-[output "[neg a] | negate a" newline]
-[output "[div a b] | a / b" newline]
-[output "[mod a b] | a % b" newline]
-[output "[< ...] | checks if arguments are increasing" newline]
-[output "[> ...] | checks if arguments are decreasing" newline]
-[output "[out ch] | outputs character with ch code" newline]
-[output "[in] | null if input ended, else character code" newline]
-[output "[name str] | creates name from str string" newline]
-[output "[str obj] | creates str from name or list of codes" newline]
-[output "[len obj] | length of list or string" newline]
-[output "[run x] | runs the x expression" newline]
-[output "[run x context] | runs the x expression in given context" newline]
-[output " | returns pair of value and new context" newline]
-[output "[context] | returns current context" newline]
-[output "[newcontext] | returns new base context" newline]
-[output "==================================================================" newline]
-[output "Have a good time!" newline]
[read [newcontext]]
diff --git a/examples/translator.c b/examples/translator.c
index 26bd5ac..0791e65 100644
--- a/examples/translator.c
+++ b/examples/translator.c
@@ -4,12 +4,23 @@
int c;
+int indent = 0;
-void items();
+int items();
+
+void prindent()
+{
+ int i;
+ printf("\n ");
+ for (i = 0; i != indent; ++i) {
+ printf(" ");
+ }
+}
void name(char a)
{
+ prindent();
printf("csx_name(\"");
if (a) putchar(a);
while (c != EOF && !isspace(c) && c != '[' && c != ']') {
@@ -22,6 +33,7 @@ void name(char a)
void num(char a, char b)
{
+ prindent();
printf("csx_int(");
if (a) putchar(a);
if (b) putchar(b);
@@ -35,8 +47,9 @@ void num(char a, char b)
void list()
{
+ prindent();
printf("csx_list(");
- items();
+ if (items()) prindent();
printf("0)");
c = getchar();
}
@@ -44,8 +57,10 @@ void list()
void pair()
{
if ((c = getchar()) == '[') {
+ prindent();
printf("csx_pair(");
items();
+ prindent();
printf("0)");
c = getchar();
} else name('=');
@@ -53,13 +68,16 @@ void pair()
void quote()
{
- printf("csx_list(csx_name(\"quote\"),");
+ prindent();
+ printf("csx_list(csx_name(\"quote\"),\n");
c = getchar();
if (c == '[') list();
else if (c == '=') pair();
else if (c == '\'') quote();
else name(0);
- printf(",0)");
+ printf(",\n");
+ prindent();
+ printf("0)");
}
void skip()
@@ -78,9 +96,10 @@ void skip()
}
}
-void items()
+int items()
{
int first = 1;
+ ++indent;
c = getchar();
skip();
while (c != EOF && c != ']') {
@@ -92,6 +111,7 @@ void items()
else if (isdigit(c)) num(c, 0);
else if (c == '=') pair();
else if (c == '"') {
+ prindent();
printf("csx_str(\"");
c = getchar();
while (c != EOF && c != '"') {
@@ -107,12 +127,15 @@ void items()
skip();
}
if (!first) putchar(',');
+ --indent;
+ return !first;
}
int main()
{
- printf("#include <csx.h>\nint main(){csx_run(csx_list(csx_name(\"do\"),");
+ printf("#include <csx.h>\n\nint main()\n{\n");
+ printf(" csx_run(csx_list(csx_name(\"do\"),");
items();
- printf("0));return 0;}\n");
+ printf("\n 0));\n csx_free();\n return 0;\n}\n");
return 0;
}
diff --git a/src/csx.c b/src/csx.c
index 73f8690..440d732 100644
--- a/src/csx.c
+++ b/src/csx.c
@@ -229,7 +229,7 @@ static void *lookup_frame(const char *name)
{
pair_data *frame = context->head;
while (type(frame) == type_pair) {
- if (!strcmp(head(frame->head), name)) return frame->head;
+ if (head(frame->head) == name) return frame->head;
frame = frame->tail;
}
return null;