summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile23
-rw-r--r--examples/algebraic.sin13
-rw-r--r--examples/crosses.int8
-rw-r--r--examples/e.int21
-rw-r--r--examples/either.sin25
-rw-r--r--examples/fib.int12
-rw-r--r--examples/function.sin76
-rw-r--r--examples/glue.c42
-rw-r--r--examples/list.sin53
-rw-r--r--examples/list_null.c27
-rw-r--r--examples/maybe.sin22
-rw-r--r--examples/nqueens/list.sin56
-rw-r--r--examples/nqueens/nqueens.sin67
-rw-r--r--examples/power.int18
-rw-r--r--examples/repeat.h70
-rw-r--r--examples/run_func.c75
-rw-r--r--examples/tree.sin16
-rw-r--r--examples/unit/Makefile19
-rwxr-xr-xexamples/unit/run_tests.sh21
-rw-r--r--examples/unit/test_001_return_literal_int.sin2
-rw-r--r--examples/unit/test_002_return_right_value.sin7
-rw-r--r--examples/unit/test_framework.c13
-rw-r--r--examples/util.sin2
23 files changed, 688 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..3707e79
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,23 @@
+CC = clang
+CXX = clang++
+SINC = ../sinter
+
+CFLAGS = -g -O0 -Werror -Wpedantic -Wall
+LDFLAGS = -L$(HOME)/usr/lib -g
+LDLIBS = -lgc
+
+LLCFLAGS = $(CFLAGS)
+SINCFLAGS =
+
+TRGT =
+
+include ../common.mak
+
+.PHONY: default all clean
+
+default: all
+
+all: $(TRGT)
+
+clean:
+ $(RM) *.ll *.dot *.svg *.o $(TRGT)
diff --git a/examples/algebraic.sin b/examples/algebraic.sin
new file mode 100644
index 0000000..a3d4337
--- /dev/null
+++ b/examples/algebraic.sin
@@ -0,0 +1,13 @@
+[dec just [x]]
+[dec none []]
+[dec has_value [x]]
+[dec Maybe.>>= [a f]]
+[dec just.val [x]]
+
+[def double [x]
+ [just [* x 2]]]
+
+[def inter_main []
+ [debug_int
+ [just.val
+ [Maybe.>>= [just 10] double]]]]
diff --git a/examples/crosses.int b/examples/crosses.int
new file mode 100644
index 0000000..a1d8c99
--- /dev/null
+++ b/examples/crosses.int
@@ -0,0 +1,8 @@
+[dec loop [c]]
+[dec create_window [c x y w h]]
+[dec init_sdl []]
+
+[def on_click [x y] [debug_int [+ x y]]]
+
+[def inter_main []
+ [loop [create_window [init_sdl] 10 10 100 100]]]
diff --git a/examples/e.int b/examples/e.int
new file mode 100644
index 0000000..f49b406
--- /dev/null
+++ b/examples/e.int
@@ -0,0 +1,21 @@
+[type cons [(INT head) (cons tail)]]
+[def (INT ==) [(INT a) (INT b)] [if [- a b] 0 1]]
+[def (INT hastail) [(INT list)] [if [cons.tail list] 1 0]]
+[def (INT carry_guess) [(INT d) (INT base)] [/ d base]]
+[def (INT remainder) [(INT d) (INT base)] [% d base]]
+[def (INT carry_propagate) [(INT a) (INT b)] [+ 1 1]]
+[def (INT next_carry) [(INT base) (INT ds)]
+ [carry_propagate [+ base 1] [cons.tail ds]]]
+[def (INT d_corrected) [(INT base) (INT ds)]
+ [+ [cons.head ds] [next_carry base ds]]]
+
+[def (INT carry_propagate) [(INT base) (INT ds)] [
+ if [== [carry_guess [cons.head ds] base] [/ [+ [cons.head ds] 9] base]]
+ [cons [carry_guess [cons.head ds] base]
+ [+ [remainder [cons.head ds] base] [next_carry base [cons.tail ds]]]]
+ [cons [/ d_corrected base] [% d_corrected base]]
+]]
+
+[def (INT e) [(INT n)]
+ [
+[def (INT inter_main) [] [debug_int [+ 1 1]]]
diff --git a/examples/either.sin b/examples/either.sin
new file mode 100644
index 0000000..e16b7b8
--- /dev/null
+++ b/examples/either.sin
@@ -0,0 +1,25 @@
+[type left_ [kind val]]
+[type right_ [kind val]]
+
+[def left [val]
+ [left_ 0 val]]
+
+[def right [val]
+ [right_ 1 val]]
+
+[def left.val [x]
+ [left_.val x]]
+
+[def right.val [x]
+ [right_.val x]]
+
+[def is_right [x]
+ [right_.kind x]]
+
+[def Either.>>= [a f]
+ [if [is_right a]
+ [f [right.val a]]
+ a]]
+
+[def Either.return [x]
+ [right x]]
diff --git a/examples/fib.int b/examples/fib.int
new file mode 100644
index 0000000..11736d0
--- /dev/null
+++ b/examples/fib.int
@@ -0,0 +1,12 @@
+[dec wowow []]
+[type test [(INT x) (INT y)]]
+[def testest [] [test.y [test 3 10]]]
+[def dec [n] [- n 1]]
+[def inc [n] [+ n 1]]
+[def ttt [x y] [test y x]]
+[def ! [b] [if b 0 1]]
+[def != [x y] [- x y]]
+[def == [x y] [! [!= x y]]]
+[def or [a b] [if a 1 b]]
+[def fib [n] [debug_int [if [or [== n 0] [== n 1]] 1 [+ [fib [- n 1]] [fib [- n 2]]]]]]
+[def inter_main [] [fib [test.y [ttt 10 10]]]]
diff --git a/examples/function.sin b/examples/function.sin
new file mode 100644
index 0000000..461b9e2
--- /dev/null
+++ b/examples/function.sin
@@ -0,0 +1,76 @@
+[type hunk [func args_req arg_c args]]
+
+[dec run_func [f v as]]
+
+[dec list [x n]]
+[dec nil []]
+[dec is_nil [x]]
+[dec append [xs x]]
+[dec head [xs]]
+[dec tail [xs]]
+
+[dec left [x]]
+[dec right [x]]
+[dec is_right [x]]
+[dec right.val [x]]
+[dec left.val [x]]
+
+[def = [a b]
+ [if [- a b]
+ 0
+ 1]]
+
+[def add_arg [h a]
+ [hunk [hunk.func h]
+ [hunk.args_req h]
+ [+ [hunk.arg_c h] 1]
+ [append [hunk.args h] a]]]
+
+[def run_hunk [h]
+ [run_func [hunk.func h] [hunk.args_req h] [hunk.args h]]]
+
+[def maybe_run_hunk [h]
+ [if [= [hunk.args_req h] [hunk.arg_c h]]
+ [left [run_hunk h]]
+ [right h]]]
+
+[def add_arg_and_maybe_run [h a]
+ [maybe_run_hunk [add_arg h a]]]
+
+[def !1 [h a]
+ [add_arg_and_maybe_run h a]]
+
+[def swap_args [f a b]
+ [f b a]]
+
+[def make_hunk [f v]
+ [if v
+ [right [hunk f v 0 [nil]]]
+ [left [run_func f 0 [nil]]]]]
+
+[def !> [h a]
+ [if [is_right h]
+ [!1 [right.val h] a]
+ h]]
+
+[dec add_args_and_maybe_run_rev [as h]]
+
+[def add_args_and_maybe_run [h as]
+ [if [is_nil as]
+ h
+ [add_args_and_maybe_run [!> h [head as]]
+ [tail as]]]]
+
+[def ! [h as]
+ [add_args_and_maybe_run h as]]
+
+[def countdown [x]
+ [if x
+ [list x [countdown [- x 1]]]
+ [nil]]]
+
+[def mult [x y]
+ [* x y]]
+
+[def inter_main []
+ [debug_int [left.val [! [make_hunk mult 2] [countdown 10]]]]]
diff --git a/examples/glue.c b/examples/glue.c
new file mode 100644
index 0000000..33e44bd
--- /dev/null
+++ b/examples/glue.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+#include <SDL2/SDL.h>
+
+void *on_click(void *x, void *y);
+
+void *init_sdl() {
+ return (void *) SDL_Init(SDL_INIT_VIDEO);
+}
+
+void *create_window(void *init, void *xv, void *yv, void *wv, void *hv) {
+ int x = (int) xv, y = (int) yv, w = (int) wv, h = (int) hv;
+ int window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
+ SDL_Window *window = SDL_CreateWindow("window", x, y, w, h, window_flags);
+ int rend_flags = SDL_RENDERER_ACCELERATED;
+ SDL_Renderer *rend = SDL_CreateRenderer(window, -1, rend_flags);
+ printf("window created\n");
+ return (void *) window;
+}
+
+void *loop(void *window) {
+ int running = 1;
+ SDL_Event e;
+ while (running) {
+ while (SDL_PollEvent(&e)) {
+ printf("event\n");
+ switch (e.type) {
+ case SDL_QUIT:
+ running = 0;
+ break;
+ case SDL_MOUSEBUTTONDOWN: {
+ SDL_MouseButtonEvent mbe = e.button;
+ if (mbe.button == SDL_BUTTON_LEFT) {
+ on_click((void *) mbe.x, (void *) mbe.y);
+ }
+ break;
+ }
+ }
+ }
+ }
+ return 0;
+}
diff --git a/examples/list.sin b/examples/list.sin
new file mode 100644
index 0000000..5952ae3
--- /dev/null
+++ b/examples/list.sin
@@ -0,0 +1,53 @@
+[type list_ [kind head tail]]
+[type nil_ [kind]]
+
+[def list [head tail]
+ [list_ 1 head tail]]
+
+[def nil []
+ [nil_ 0]]
+
+[def head [xs]
+ [list_.head xs]]
+
+[def tail [xs]
+ [list_.tail xs]]
+
+[def has_tail [xs]
+ [nil_.kind xs]]
+
+[def is_nil [x]
+ [if [nil_.kind x]
+ 0
+ 1]]
+
+[def concat [as bs]
+ [list [head as]
+ [if [is_nil [tail as]]
+ bs
+ [concat [tail as] bs]]]]
+
+[def alt_concat [as bs]
+ [if [is_nil [tail as]]
+ [list [head as] bs]
+ [list [head as] [concat [tail as] bs]]]]
+
+[def prepend [x xs]
+ [list x xs]]
+
+[def append [xs x]
+ [if [is_nil xs]
+ [list [debug_int x] [nil]]
+ [list [head xs] [append [tail xs] x]]]]
+
+[def map [xs f]
+ [if [is_nil xs]
+ xs
+ [list [f [head xs]]
+ [map [tail xs] f]]]]
+
+[def debug_list_int [xs]
+ [if [is_nil xs]
+ xs
+ [list [debug_int [head xs]]
+ [debug_list_int [tail xs]]]]]
diff --git a/examples/list_null.c b/examples/list_null.c
new file mode 100644
index 0000000..85a3d2c
--- /dev/null
+++ b/examples/list_null.c
@@ -0,0 +1,27 @@
+typedef struct list_t {
+ void *content;
+ struct list_t *next;
+} list_t;
+
+void *list(void *head, void *tail) {
+ list_t *ret = gc_alloc(sizeof (*ret));
+ ret->content = head;
+ ret->next = (list_t *) tail;
+ return ret;
+}
+
+void *nil() {
+ return (void *) 0;
+}
+
+void *head(void *list) {
+ return list->head;
+}
+
+void *tail(void *list) {
+ return list->tail;
+}
+
+void *has_tail(void *list) {
+ return list->tail;
+}
diff --git a/examples/maybe.sin b/examples/maybe.sin
new file mode 100644
index 0000000..91367cd
--- /dev/null
+++ b/examples/maybe.sin
@@ -0,0 +1,22 @@
+[type just_ [kind val]]
+[type none_ [kind]]
+
+[def just [val]
+ [just_ 1 val]]
+
+[def just.val [x]
+ [just_.val x]]
+
+[def none []
+ [none_ 0]]
+
+[def has_value [x]
+ [just_.kind x]]
+
+[def Maybe.>>= [a f]
+ [if [has_value a]
+ [f [just.val a]]
+ a]]
+
+[def Maybe.return [x]
+ [just x]]
diff --git a/examples/nqueens/list.sin b/examples/nqueens/list.sin
new file mode 100644
index 0000000..9a59945
--- /dev/null
+++ b/examples/nqueens/list.sin
@@ -0,0 +1,56 @@
+[type cons_ [kind head tail]]
+[type nil_ [kind]]
+
+[def cons [head tail]
+ [cons_ 1 head tail]]
+
+[def nil []
+ [nil_ 0]]
+
+[def head [xs]
+ [cons_.head xs]]
+
+[def tail [xs]
+ [cons_.tail xs]]
+
+[def has_tail [xs]
+ [nil_.kind xs]]
+
+[def is_nil [x]
+ [if [nil_.kind x]
+ 0
+ 1]]
+
+[def concat [as bs]
+ [cons [head as]
+ [if [is_nil [tail as]]
+ bs
+ [concat [tail as] bs]]]]
+
+[def alt_concat [as bs]
+ [if [is_nil [tail as]]
+ [cons [head as] bs]
+ [cons [head as] [concat [tail as] bs]]]]
+
+[def flatten_once [xs]
+ [concat [head xs] [flatten_once [tail xs]]]]
+
+[def List.>>= [ma f]
+ [flatten_once [map ma f]]]
+
+[def List.return [a]
+ [cons a [nil]]]
+
+[def prepend [x xs]
+ [cons x xs]]
+
+[def append [xs x]
+ [if [is_nil xs]
+ [cons [debug_int x] [nil]]
+ [cons [head xs] [append [tail xs] x]]]]
+
+[def map [xs f]
+ [if [is_nil xs]
+ xs
+ [cons [f [head xs]]
+ [map [tail xs] f]]]]
diff --git a/examples/nqueens/nqueens.sin b/examples/nqueens/nqueens.sin
new file mode 100644
index 0000000..4779586
--- /dev/null
+++ b/examples/nqueens/nqueens.sin
@@ -0,0 +1,67 @@
+[dec cons [head tail]]
+[dec nil []]
+[dec head [xs]]
+[dec tail [xs]]
+[dec length [xs]]
+
+[type pair [(INT left) (INT right)]]
+
+[def nsoln [nq]
+ [length [gen nq nq]]]
+
+[def inter_main []
+ [debug_int [nsoln 4]]]
+
+[def (INT ==) [(INT a) (INT b)]
+ [if [- a b] 1 0]]
+
+[def (INT not) [(INT x)]
+ [if x 0 1]]
+
+[def (INT !=) [(INT a) (INT b)]
+ [not [== a b]]]
+
+[def (INT and) [(INT a) (INT b)]
+ [if a [if b 1 0] 0]]
+
+[def (INT length) [(INT xs)]
+ [debug_int [if xs [+ 1 [length [cons.tail xs]]] 0]]]
+
+[def (INT count) [(INT from) (INT to)]
+ [if [== from to] to [cons from [count [+ from 1] to]]]]
+
+[def (INT concat) [(INT as) (INT bs)]
+ [if [cons.tail as]
+ [cons [cons.head as] [concat [cons.tail as] bs]]
+ [cons [cons.head as] bs]]]
+
+[def (INT combo2) [(INT a) (INT bs)]
+ [if bs
+ [cons [pair a [cons.head bs]] [combo2 a [cons.tail bs]]]
+ 0]]
+
+[def (INT combo) [(INT as) (INT bs)]
+ [if as
+ [concat [combo2 [cons.head as] bs] [combo [cons.tail as] bs]]
+ 0]]
+
+[def (INT safe) [(INT x) (INT d) (INT qs)]
+ [if qs
+ [and [and [!= x [cons.head qs]]
+ [!= x [+ [cons.head qs] d]]]
+ [and [!= x [- [cons.head qs] d]]
+ [safe x [+ d 1] [cons.tail qs]]]]
+ 1]]
+
+[def (INT safe_filter) [(INT xs)]
+ [if [safe [pair.left xs] 1 [pair.right xs]]
+ [cons [cons.head xs] [safe_filter [cons.tail xs]]]
+ [safe_filter [cons.tail xs]]]]
+
+[def (INT gen) [(INT n) (INT nq)]
+ [if n [safe_filter [combo [gen [- n 1] nq] [count 1 nq]]] 0]]
+
+[def (INT nsoln) [(INT nq)]
+ [length [gen nq nq]]]
+
+[def (INT inter_main) [] [debug_int [nsoln 4]]]
diff --git a/examples/power.int b/examples/power.int
new file mode 100644
index 0000000..119166d
--- /dev/null
+++ b/examples/power.int
@@ -0,0 +1,18 @@
+[def -- [x]
+ [- x 1]]
+
+[def +++ [x y]
+ [+ x y]]
+
+[def *** [x y]
+ [if x
+ [+++ y [*** [-- x] y]]
+ 0]]
+
+[def ^^^ [b p]
+ [if p
+ [*** b [^^^ b [-- p]]]
+ 1]]
+
+[def inter_main []
+ [debug_int [^^^ 3 6]]]
diff --git a/examples/repeat.h b/examples/repeat.h
new file mode 100644
index 0000000..8564abf
--- /dev/null
+++ b/examples/repeat.h
@@ -0,0 +1,70 @@
+#ifndef REPEAT_H
+#define REPEAT_H
+
+#define repeat_1(x) x
+#define repeat_2(x) x, repeat_1(x)
+#define repeat_3(x) x, repeat_2(x)
+#define repeat_4(x) x, repeat_3(x)
+#define repeat_5(x) x, repeat_4(x)
+#define repeat_6(x) x, repeat_5(x)
+#define repeat_7(x) x, repeat_6(x)
+#define repeat_8(x) x, repeat_7(x)
+#define repeat_9(x) x, repeat_8(x)
+#define repeat_10(x) x, repeat_9(x)
+#define repeat_11(x) x, repeat_10(x)
+#define repeat_12(x) x, repeat_11(x)
+#define repeat_13(x) x, repeat_12(x)
+#define repeat_14(x) x, repeat_13(x)
+#define repeat_15(x) x, repeat_14(x)
+#define repeat_16(x) x, repeat_15(x)
+#define repeat_17(x) x, repeat_16(x)
+#define repeat_18(x) x, repeat_17(x)
+#define repeat_19(x) x, repeat_18(x)
+#define repeat_20(x) x, repeat_19(x)
+#define repeat_21(x) x, repeat_20(x)
+#define repeat_22(x) x, repeat_21(x)
+#define repeat_23(x) x, repeat_22(x)
+#define repeat_24(x) x, repeat_23(x)
+#define repeat_25(x) x, repeat_24(x)
+#define repeat_26(x) x, repeat_25(x)
+#define repeat_27(x) x, repeat_26(x)
+#define repeat_28(x) x, repeat_27(x)
+#define repeat_29(x) x, repeat_28(x)
+#define repeat_30(x) x, repeat_29(x)
+#define repeat_31(x) x, repeat_30(x)
+#define repeat_32(x) x, repeat_31(x)
+
+#define repeat_n_1(x) concat(x, 1)
+#define repeat_n_2(x) repeat_n_1(x), concat(x, 2)
+#define repeat_n_3(x) repeat_n_2(x), concat(x, 3)
+#define repeat_n_4(x) repeat_n_3(x), concat(x, 4)
+#define repeat_n_5(x) repeat_n_4(x), concat(x, 5)
+#define repeat_n_6(x) repeat_n_5(x), concat(x, 6)
+#define repeat_n_7(x) repeat_n_6(x), concat(x, 7)
+#define repeat_n_8(x) repeat_n_7(x), concat(x, 8)
+#define repeat_n_9(x) repeat_n_8(x), concat(x, 9)
+#define repeat_n_10(x) repeat_n_9(x), concat(x, 10)
+#define repeat_n_11(x) repeat_n_10(x), concat(x, 11)
+#define repeat_n_12(x) repeat_n_11(x), concat(x, 12)
+#define repeat_n_13(x) repeat_n_12(x), concat(x, 13)
+#define repeat_n_14(x) repeat_n_13(x), concat(x, 14)
+#define repeat_n_15(x) repeat_n_14(x), concat(x, 15)
+#define repeat_n_16(x) repeat_n_15(x), concat(x, 16)
+#define repeat_n_17(x) repeat_n_16(x), concat(x, 17)
+#define repeat_n_18(x) repeat_n_17(x), concat(x, 18)
+#define repeat_n_19(x) repeat_n_18(x), concat(x, 19)
+#define repeat_n_20(x) repeat_n_19(x), concat(x, 20)
+#define repeat_n_21(x) repeat_n_20(x), concat(x, 21)
+#define repeat_n_22(x) repeat_n_21(x), concat(x, 22)
+#define repeat_n_23(x) repeat_n_22(x), concat(x, 23)
+#define repeat_n_24(x) repeat_n_23(x), concat(x, 24)
+#define repeat_n_25(x) repeat_n_24(x), concat(x, 25)
+#define repeat_n_26(x) repeat_n_25(x), concat(x, 26)
+#define repeat_n_27(x) repeat_n_26(x), concat(x, 27)
+#define repeat_n_28(x) repeat_n_27(x), concat(x, 28)
+#define repeat_n_29(x) repeat_n_28(x), concat(x, 29)
+#define repeat_n_30(x) repeat_n_29(x), concat(x, 30)
+#define repeat_n_31(x) repeat_n_30(x), concat(x, 31)
+#define repeat_n_32(x) repeat_n_31(x), concat(x, 32)
+
+#endif /* REPEAT_H */
diff --git a/examples/run_func.c b/examples/run_func.c
new file mode 100644
index 0000000..e4ca8fb
--- /dev/null
+++ b/examples/run_func.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "repeat.h"
+
+typedef struct list {
+ void *kind;
+ void *contents;
+ struct list *next;
+} list;
+
+/* FIXME: double fprintf is ugly */
+#define error(v, ...) do { \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, "\n"); \
+ exit(v); \
+} while (0)
+
+#define _concat(a, b) a ## b
+#define concat(a, b) _concat(a, b)
+
+#define assign_arg(n) \
+ concat(arg_, n) = l->contents; \
+ fprintf(stderr, "%p: %i\n", concat(arg_, n), *((int *) concat(arg_, n))); \
+ l = l->next
+
+#define cast_apply_f(n) return ((void *(*)(concat(repeat_, n)(void *))) f)\
+(concat(repeat_n_, n)(arg_))
+
+#define new_case(n) \
+ assign_arg(n); \
+ if (arity == n) cast_apply_f(n)
+
+void *run_func(void *f, void *v, void *as) {
+ int arity = *((int *) v);
+ fprintf(stderr, "calling function %p with arity %i\n", f, arity);
+ list *l = (list *) as;
+
+ void repeat_n_32(*arg_);
+
+ if (arity == 0) return ((void *(*)()) f)();
+ new_case(1);
+ new_case(2);
+ new_case(3);
+ new_case(4);
+ new_case(5);
+ new_case(6);
+ new_case(7);
+ new_case(8);
+ new_case(9);
+ new_case(10);
+ new_case(11);
+ new_case(12);
+ new_case(13);
+ new_case(14);
+ new_case(15);
+ new_case(16);
+ new_case(17);
+ new_case(18);
+ new_case(19);
+ new_case(20);
+ new_case(21);
+ new_case(22);
+ new_case(23);
+ new_case(24);
+ new_case(25);
+ new_case(26);
+ new_case(27);
+ new_case(28);
+ new_case(29);
+ new_case(30);
+ new_case(31);
+ new_case(32);
+ error(1, "arity %i not handled", arity);
+}
diff --git a/examples/tree.sin b/examples/tree.sin
new file mode 100644
index 0000000..b11cae9
--- /dev/null
+++ b/examples/tree.sin
@@ -0,0 +1,16 @@
+[type branch_ [kind val l r]]
+[type leaf_ [kind val]]
+
+[dec == [a b]]
+
+[def branch [v l r]
+ [branch_ 0 v l r]]
+
+[def leaf [x]
+ [leaf_ 1 x]]
+
+[def is_branch [x]
+ [== [branch_.kind x] 0]]
+
+[def is_leaf [x]
+ [== [leaf_.kind x] 1]]
diff --git a/examples/unit/Makefile b/examples/unit/Makefile
new file mode 100644
index 0000000..aadf541
--- /dev/null
+++ b/examples/unit/Makefile
@@ -0,0 +1,19 @@
+CC = clang
+SINC = ../../sinter
+
+TRGT = $(patsubst %.sin,%,$(wildcard test*.sin))
+LDFLAGS = -L$(HOME)/usr/lib
+LDLIBS = -lgc
+
+include ../../common.mak
+
+.PHONY: default all clean
+
+default: all
+
+all: $(TRGT)
+
+$(TRGT): test_framework.o ../../common.o
+
+clean:
+ $(RM) *.o *.ll $(TRGT)
diff --git a/examples/unit/run_tests.sh b/examples/unit/run_tests.sh
new file mode 100755
index 0000000..2146746
--- /dev/null
+++ b/examples/unit/run_tests.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+make --silent clean
+make --silent test_framework.o
+
+for t in test_*.sin ; do
+ exe="$(basename -s .sin $t)"
+ echo -n "$exe... "
+ make --silent "$exe"
+ if [ $? -ne 0 ] ; then
+ echo "cannot build"
+ continue
+ fi
+ echo -n "built "
+ ./"$exe"
+ if [ $? -ne 0 ] ; then
+ echo "failed"
+ continue
+ fi
+ echo "passed"
+done
diff --git a/examples/unit/test_001_return_literal_int.sin b/examples/unit/test_001_return_literal_int.sin
new file mode 100644
index 0000000..d367d11
--- /dev/null
+++ b/examples/unit/test_001_return_literal_int.sin
@@ -0,0 +1,2 @@
+[def test []
+ 0]
diff --git a/examples/unit/test_002_return_right_value.sin b/examples/unit/test_002_return_right_value.sin
new file mode 100644
index 0000000..680ed7a
--- /dev/null
+++ b/examples/unit/test_002_return_right_value.sin
@@ -0,0 +1,7 @@
+[dec assert_equals [a b c]]
+
+[def returns_ten []
+ 10]
+
+[def test []
+ [assert_equals [returns_ten] 10 0]]
diff --git a/examples/unit/test_framework.c b/examples/unit/test_framework.c
new file mode 100644
index 0000000..22ededd
--- /dev/null
+++ b/examples/unit/test_framework.c
@@ -0,0 +1,13 @@
+#include <assert.h>
+
+void *test(void);
+
+void *inter_main(void) {
+ return test();
+}
+
+void *assert_equals(void *a, void *b, void *c) {
+ int ia = *((int *) a), ib = *((int *) b);
+ assert(ia == ib);
+ return c;
+}
diff --git a/examples/util.sin b/examples/util.sin
new file mode 100644
index 0000000..5911a34
--- /dev/null
+++ b/examples/util.sin
@@ -0,0 +1,2 @@
+[def == [a b]
+ [- a b]]