aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-09-28 04:59:07 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-09-28 04:59:07 +0300
commit6b08e86c9a16bfac5a208a04926dcc66b861a096 (patch)
treed0aa6b84d1d0bd62268434187d12f7bf02397030 /examples
parentaee665f2bc7d66d5e6ecb1e31f9e2ccf614c7fa2 (diff)
downloadcsx-6b08e86c9a16bfac5a208a04926dcc66b861a096.tar
csx-6b08e86c9a16bfac5a208a04926dcc66b861a096.tar.xz
csx-6b08e86c9a16bfac5a208a04926dcc66b861a096.zip
Something...
Diffstat (limited to 'examples')
-rw-r--r--examples/math.c70
-rw-r--r--examples/strfib.c53
2 files changed, 53 insertions, 70 deletions
diff --git a/examples/math.c b/examples/math.c
deleted file mode 100644
index df0aabc..0000000
--- a/examples/math.c
+++ /dev/null
@@ -1,70 +0,0 @@
-#include <csx.h>
-
-
-v def_rat_sum(v construct, v numer, v denom)
-{
- v a = atom("a");
- v b = atom("b");
- return el(define, l(atom("rat_sum"), a, b),
- l(construct, l(sum, l(mul, l(numer, a), l(denom, b)),
- l(mul, l(numer, b), l(denom, a))),
- l(mul, l(denom, a), l(denom, b)))
- );
-}
-
-v def_rat_sum(v construct, v numer, v denom)
-{
- v a = atom("a");
- v b = atom("b");
- return el(define, l(atom("rat_sub"), a, b),
- l(construct, l(sub, l(mul, l(numer, a), l(denom, b)),
- l(mul, l(numer, b), l(denom, a))),
- l(mul, l(denom, a), l(denom, b)))
- );
-}
-
-v def_rat_equ(v construct, v numer, v denom)
-{
- v a = atom("a");
- v b = atom("b");
- return el(define, l(atom("rat_equ"), a, b),
- l(equ, l(mul, l(numer, a), l(denom, b)),
- l(mul, l(numer, b), l(denom, a)))
- );
-}
-
-void printres(v a, v b, v absum, v absub, int *are_equ, v numer, v denom)
-{
- int an = *(int *)el(numer, a);
- int ad = *(int *)el(denom, a);
- int bn = *(int *)el(numer, b);
- int bd = *(int *)el(denom, b);
- int sumn = *(int *)el(numer, absum);
- int sumd = *(int *)el(denom, absum);
- int subn = *(int *)el(numer, absub);
- int subd = *(int *)el(denom, absub);
- printf("%d/%d + %d/%d = %d/%d\n", an, ad, bn, bd, sumn, sumd);
- printf("%d/%d - %d/%d = %d/%d\n", an, ad, bn, bd, subn, subd);
- if (*are_equ) {
- puts("And they are equal.\n");
- } else {
- puts("And they are not equal.\n");
- }
-}
-
-int main()
-{
- v rat = l(define, atom("rat"), cons);
- v rat_numer = l(define, atom("rat_numer"), car);
- v rat_denom = l(define, atom("rat_denom"), cdr);
- v rat_sum = def_rat_sum(rat, rat_numer, rat_denom);
- v rat_sub = def_rat_sub(rat, rat_numer, rat_denom);
- v rat_equ = def_rat_equ(rat, rat_numer, rat_denom);
- v number_a = el(rat, n(19), n(99));
- v number_b = el(rat, n(7), n(3));
- v absum = el(rat_sum, number_a, number_b);
- v absub = el(rat_sub, number_a, number_b);
- v are_equ = el(rat_equ, number_a, number_b);
- printres(number_a, number_b, sum, sub, are_equ, numer, denom);
- return 0;
-}
diff --git a/examples/strfib.c b/examples/strfib.c
new file mode 100644
index 0000000..6e22c7f
--- /dev/null
+++ b/examples/strfib.c
@@ -0,0 +1,53 @@
+#include <csx/csx.h>
+#include <stdio.h>
+
+
+static csx_type_function_list *l;
+static csx_type_function_string *s;
+static csx_type_function_atom *a;
+static csx_type_function_evaluate *e;
+
+static char *append;
+static char *define;
+static char *cdr;
+static char *ifx;
+static char *not;
+
+void printfibs(char *strfib)
+{
+ const char *input = "oooooooooo" + 9;
+ int i;
+ for (i = 0; i != 10; ++i)
+ puts(e(l(strfib, s(input - i), 0)));
+}
+
+void process()
+{
+ char *strfib = a("strfib");
+ char *n = a("n");
+ e(l(define, l(strfib, n, 0),
+ l(ifx, l(not, n, 0), s(""),
+ l(ifx, l(not, l(cdr, n, 0), 0), s("o"),
+ l(append, l(strfib, l(cdr, n, 0), 0),
+ l(strfib, l(cdr, l(cdr, n, 0), 0), 0), 0),
+ 0),
+ 0),
+ 0));
+ printfibs(strfib);
+}
+
+int main()
+{
+ l = csx_list;
+ s = csx_string;
+ a = csx_atom;
+ e = csx_evaluate;
+ append = a("append");
+ define = a("define");
+ cdr = a("cdr");
+ ifx = a("ifx");
+ not = a("not");
+ process();
+ csx_free();
+ return 0;
+}