From 1a3dc02a18a9050877288a5867e44d9c47a53542 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 11 Jan 2020 02:53:36 +0000 Subject: Added int width specifiers The width of an int can be specified at the site of its creation. For example, the following function returns an int of width 2 bits and value 1: [def two_bit_int [] (2 1)] Ints can still be written the old way; in these cases the compiler looks for a default int bit-width (specified with the -w flag). If a default has not been defined, the compiler throws an error. --- examples/nq/Makefile | 11 +++++++---- examples/nq/closures.sin | 4 +++- examples/nq/glue.c | 9 +++++---- examples/nq/list.sin | 6 +++++- examples/nq/nqueens.sin | 8 +++++--- examples/nq/run_func.c | 1 + examples/nq/util.sin | 2 ++ 7 files changed, 28 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/nq/Makefile b/examples/nq/Makefile index 60b2d94..1f8a66b 100644 --- a/examples/nq/Makefile +++ b/examples/nq/Makefile @@ -2,17 +2,20 @@ CC = clang CXX = clang++ SINC = ../../sinter -DEBUG = -g -O3 +DEBUG = -g -O0 CFLAGS = $(DEBUG) -Werror -Wpedantic -Wall -I$(HOME)/usr/include LDFLAGS = $(DEBUG) -L$(HOME)/usr/lib LDLIBS = -lgc LLCFLAGS = $(DEBUG) -Werror -Wpedantic -Wall -SINCFLAGS = -u always +SINCFLAGS = -u never -w 32 TRGT = nqueens +SINS = $(wildcard *.sin) +LLEXT = $(SINS:.sin=.ll) + include ../../common.mak .PHONY: default all clean @@ -21,7 +24,7 @@ default: all all: $(TRGT) -$(TRGT): nqueens.o util.o either.o list.o closures.o run_func.o glue.o +$(TRGT): nqueens.o util.o either.o list.o closures.o run_func.o glue.o gglue.o clean: - $(RM) *.ll *.dot *.svg *.o $(TRGT) + $(RM) $(LLEXT) *.dot *.svg *.o $(TRGT) diff --git a/examples/nq/closures.sin b/examples/nq/closures.sin index 2e23daf..36d5427 100644 --- a/examples/nq/closures.sin +++ b/examples/nq/closures.sin @@ -16,10 +16,12 @@ [dec tail [xs]] [dec is_nil [xs]] +[dec inc [x]] + [def add_arg [c a] [closure_ [closure_.func c] [closure_.arity c] - [+ [closure_.arg_c c] 1] + [inc [closure_.arg_c c]] [append [closure_.args c] a]]] [def run_closure [h] diff --git a/examples/nq/glue.c b/examples/nq/glue.c index 07dfae7..a1ef772 100644 --- a/examples/nq/glue.c +++ b/examples/nq/glue.c @@ -1,12 +1,13 @@ #include #include #include +#include #include -void *count_solns(void *n); +int count_solns(int n); -void *gc_alloc(int size) { +void *gc_alloc(size_t size) { void *p = GC_MALLOC(size); /* @@ -34,8 +35,8 @@ int main(int argc, char **argv) { printf("usage: %s n\n", argv[0]); } else { - uintptr_t n = atoi(argv[1]); - int n_solns = (int) count_solns((void *) n); + int n = atoi(argv[1]); + int n_solns = count_solns(n); printf("%i solutions found\n", n_solns); } diff --git a/examples/nq/list.sin b/examples/nq/list.sin index 87c2509..2023a95 100644 --- a/examples/nq/list.sin +++ b/examples/nq/list.sin @@ -2,9 +2,13 @@ [type nil_ [kind]] [dec = [x y]] +[dec + [x y]] +[dec - [x y]] [dec !> [c a]] +[dec inc [x]] + [def cons [head tail] [list_ 0 head tail]] @@ -28,7 +32,7 @@ [def _length [xs n] [if [is_nil xs] n - [_length [tail xs] [+ n 1]]]] + [_length [tail xs] [inc n]]]] [def length [xs] [_length xs 0]] diff --git a/examples/nq/nqueens.sin b/examples/nq/nqueens.sin index 6839dc0..35777fd 100644 --- a/examples/nq/nqueens.sin +++ b/examples/nq/nqueens.sin @@ -23,6 +23,11 @@ [dec !> [c a]] [dec closure [f a]] +[dec count_solns [n]] + +[dec + [x y]] +[dec - [x y]] + [def safe [x d qs] [if [is_nil qs] 1 @@ -78,6 +83,3 @@ [!>>= [gen [- n 1] nq] [!> [closure gen_1 2] nq]] [cons [nil] [nil]]]] - -[def count_solns [n] - [length [gen n n]]] diff --git a/examples/nq/run_func.c b/examples/nq/run_func.c index 1442ecb..efb3bde 100644 --- a/examples/nq/run_func.c +++ b/examples/nq/run_func.c @@ -75,5 +75,6 @@ void *run_func(void *f, void *v, void *as) { void *inter_assert(void *a, void *b) { assert(a); + /* assert(*((int *) a)); */ return b; } diff --git a/examples/nq/util.sin b/examples/nq/util.sin index 20cf3c4..13e310e 100644 --- a/examples/nq/util.sin +++ b/examples/nq/util.sin @@ -8,6 +8,8 @@ y 0]] +[dec - [x y]] + [def != [x y] [- x y]] -- cgit v1.2.1