diff options
| author | Tom Harley | 2020-01-11 19:46:15 +0000 |
|---|---|---|
| committer | Tom Harley | 2020-01-11 19:46:15 +0000 |
| commit | 9e87372371794bc3e7d46698141966c00acaad0e (patch) | |
| tree | 068da13d97cefed2e49ab06843e1dd9d97c32f6a /examples | |
| parent | Added int width specifiers (diff) | |
| download | sinter-9e87372371794bc3e7d46698141966c00acaad0e.tar.gz sinter-9e87372371794bc3e7d46698141966c00acaad0e.zip | |
Tidying up
- Removed unused example files
- Removed redundant common.c
- Added extra files to make sure the current version compiles the nqueens solution correctly
- Added extensionless files to .gitignore
- Added a way to install sinter
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/Makefile | 23 | ||||
| -rw-r--r-- | examples/algebraic.sin | 13 | ||||
| -rw-r--r-- | examples/crosses.int | 8 | ||||
| -rw-r--r-- | examples/e.int | 21 | ||||
| -rw-r--r-- | examples/either.sin | 25 | ||||
| -rw-r--r-- | examples/fib.int | 12 | ||||
| -rw-r--r-- | examples/glue.c | 42 | ||||
| -rw-r--r-- | examples/list_null.c | 27 | ||||
| -rw-r--r-- | examples/maybe.sin | 22 | ||||
| -rw-r--r-- | examples/nq/Makefile | 1 | ||||
| -rw-r--r-- | examples/nq/gglue.ll | 41 | ||||
| -rw-r--r-- | examples/nqueens/list.sin | 56 | ||||
| -rw-r--r-- | examples/nqueens/nqueens.sin | 67 | ||||
| -rw-r--r-- | examples/power.int | 18 | ||||
| -rw-r--r-- | examples/repeat.h | 70 | ||||
| -rw-r--r-- | examples/run_func.c | 75 | ||||
| -rw-r--r-- | examples/tree.sin | 16 | ||||
| -rw-r--r-- | examples/util.sin | 2 |
18 files changed, 41 insertions, 498 deletions
diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index 3707e79..0000000 --- a/examples/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index a3d4337..0000000 --- a/examples/algebraic.sin +++ /dev/null @@ -1,13 +0,0 @@ -[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 deleted file mode 100644 index a1d8c99..0000000 --- a/examples/crosses.int +++ /dev/null @@ -1,8 +0,0 @@ -[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 deleted file mode 100644 index f49b406..0000000 --- a/examples/e.int +++ /dev/null @@ -1,21 +0,0 @@ -[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 deleted file mode 100644 index e16b7b8..0000000 --- a/examples/either.sin +++ /dev/null @@ -1,25 +0,0 @@ -[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 deleted file mode 100644 index 11736d0..0000000 --- a/examples/fib.int +++ /dev/null @@ -1,12 +0,0 @@ -[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/glue.c b/examples/glue.c deleted file mode 100644 index 33e44bd..0000000 --- a/examples/glue.c +++ /dev/null @@ -1,42 +0,0 @@ -#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_null.c b/examples/list_null.c deleted file mode 100644 index 85a3d2c..0000000 --- a/examples/list_null.c +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index 91367cd..0000000 --- a/examples/maybe.sin +++ /dev/null @@ -1,22 +0,0 @@ -[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/nq/Makefile b/examples/nq/Makefile index 1f8a66b..719acc2 100644 --- a/examples/nq/Makefile +++ b/examples/nq/Makefile @@ -1,6 +1,5 @@ CC = clang CXX = clang++ -SINC = ../../sinter DEBUG = -g -O0 diff --git a/examples/nq/gglue.ll b/examples/nq/gglue.ll new file mode 100644 index 0000000..f78a603 --- /dev/null +++ b/examples/nq/gglue.ll @@ -0,0 +1,41 @@ +target triple = "x86_64-unknown-linux-gnu" + +declare fastcc i8* @gen(i8*, i8*) + +declare fastcc i8* @length (i8*) + +define fastcc i32 @inc(i32) { + %res = add i32 1, %0 + ret i32 %res +} + +define fastcc i32 @"+"(i32, i32) { + %res = add i32 %0, %1 + ret i32 %res +} + +define fastcc i32 @-(i32, i32) { + %res = sub i32 %0, %1 + ret i32 %res +} + +define fastcc i32 @count_solns(i32) { + %cast = inttoptr i32 %0 to i8* + %solns = call fastcc i8* @gen(i8* %cast, i8* %cast) + %n_solns = call fastcc i8* @length(i8* %solns) + %res = ptrtoint i8* %n_solns to i32 + ret i32 %res +} + +define fastcc i32 @_count_solns(i32) { + %ext = zext i32 %0 to i128 + %alloc = alloca i128 + store i128 %ext, i128* %alloc + %box = bitcast i128* %alloc to i8* + %solns = call fastcc i8* @gen(i8* %box, i8* %box) + %n_solns = call fastcc i8* @length(i8* %solns) + %uncast = bitcast i8* %n_solns to i128* + %unboxed = load i128, i128* %uncast + %res = trunc i128 %unboxed to i32 + ret i32 %res +} diff --git a/examples/nqueens/list.sin b/examples/nqueens/list.sin deleted file mode 100644 index 9a59945..0000000 --- a/examples/nqueens/list.sin +++ /dev/null @@ -1,56 +0,0 @@ -[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 deleted file mode 100644 index 4779586..0000000 --- a/examples/nqueens/nqueens.sin +++ /dev/null @@ -1,67 +0,0 @@ -[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 deleted file mode 100644 index 119166d..0000000 --- a/examples/power.int +++ /dev/null @@ -1,18 +0,0 @@ -[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 deleted file mode 100644 index 8564abf..0000000 --- a/examples/repeat.h +++ /dev/null @@ -1,70 +0,0 @@ -#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 deleted file mode 100644 index e4ca8fb..0000000 --- a/examples/run_func.c +++ /dev/null @@ -1,75 +0,0 @@ -#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 deleted file mode 100644 index b11cae9..0000000 --- a/examples/tree.sin +++ /dev/null @@ -1,16 +0,0 @@ -[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/util.sin b/examples/util.sin deleted file mode 100644 index 5911a34..0000000 --- a/examples/util.sin +++ /dev/null @@ -1,2 +0,0 @@ -[def == [a b] - [- a b]] |
