From 0bd526065d826c5e9be5f1af0481c20a20362c33 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sun, 13 Jul 2014 20:43:21 -0400 Subject: [PATCH] box closures, add missing prototypes --- Makefile | 10 ++++------ RTS.h | 46 ++++++++++++++++++++++++++++++++++++++++------ closures.c | 18 ++++++++++++++++-- tokenize.h | 34 +++++++++++++++++++++++++--------- 4 files changed, 85 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 0e2c273..11553fc 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,6 @@ -$(CC)=clang - default: tokenize.c closures.c tokenize.h RTS.h - $(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 -O3 ./tokenize.c -lmaa; - $(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 -O3 ./closures.c; + $(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 -O3 ./tokenize.c -lmaa; + $(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 -O3 ./closures.c; unsafe: tokenize.c closures.c tokenize.h RTS.h $(CC) -DNDEBUG -std=c99 -O3 ./tokenize.c -lmaa; @@ -23,8 +21,8 @@ lib: tokenize.c closures.c tokenize.h RTS.h $(CC) -shared -o closures.so closures.o; debug: tokenize.c closures.c tokenize.h RTS.h - $(CC) -g -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 ./tokenize.c; + $(CC) -g -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./tokenize.c; $(CC) -shared -o tokenize.so tokenize.o -lmaa; - $(CC) -g -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 ./closures.c; + $(CC) -g -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./closures.c; $(CC) -shared -o closures.so closures.o; diff --git a/RTS.h b/RTS.h index 1639ceb..d0fbb6a 100644 --- a/RTS.h +++ b/RTS.h @@ -1,32 +1,49 @@ typedef struct { size_t size; - char* string; + char *string; } sc_string_t; +/* This is not the most space efficient representation + * However it is an easy to understand and debug one + */ typedef union { - sc_string_t string; - double doublev; int integer; float floating; + double doublev; + sc_string_t string; struct closure_t *closure; } svalue_variants_t; +/* The tag values for each different type */ typedef enum { INT = 0, FLOAT = 1, DOUBLE = 2, - STRING = 3 + STRING = 3, + CLOSURE = 4 } stype_t; +/* An actual boxed scheme value */ typedef struct { stype_t type_tag; svalue_variants_t value; } svalue_t; +/* A closure is a function pointer taking a + * single argument and an environment + * What will happen is that applications of + * procedures will be 'curried' but they will + * always be required to be fully applied earlier + * on in the compiler by checking applications + * against the arity of a procedure (obtained by looking + * at its definition). This simplifies compilation, but + * does not change the semantics of procedures in any + * way + */ typedef struct { svalue_t *(*func)(svalue_t, svalue_t*); @@ -44,12 +61,29 @@ svalue_t * box_value(svalue_variants_t, stype_t); closure_t -make_closure(svalue_t* (*func)(svalue_t, svalue_t*), +make_closure(svalue_t *(*func)(svalue_t, svalue_t*), svalue_t*); -svalue_t* +svalue_t * invoke(closure_t, svalue_t); + +inline svalue_t * +box_int(int x); + +inline svalue_t * +box_float(float x); + +inline svalue_t * +box_double(double x); + +inline svalue_t * +box_string(char *, + size_t); + +inline svalue_t * +box_closure(closure_t); + #ifndef LIB static svalue_t* make_adder_inner(svalue_t, svalue_t *); diff --git a/closures.c b/closures.c index bc4b50c..34aa069 100644 --- a/closures.c +++ b/closures.c @@ -2,7 +2,7 @@ #include #include #include "error.h" -#include "closures.h" +#include "RTS.h" inline svalue_t * box_value(svalue_variants_t value, @@ -25,11 +25,14 @@ box_value(svalue_variants_t value, case STRING: val->value = value; val->type_tag = type; + case CLOSURE: + val->value = value; + val->type_tag = type; } return val; } -svalue_t * +inline svalue_t * box_int(int x) { svalue_t *val = malloc(sizeof(svalue_t)); CHECK(val); @@ -74,6 +77,16 @@ box_string(char *chars, size_t n) { return val; } +inline svalue_t * +box_closure(closure_t closure) { + svalue_t *val = malloc(sizeof(svalue_t)); + CHECK(val); + svalue_variants_t value_val; + value_val.closure = (struct closure_t *)&closure; + val = box_value(value_val, CLOSURE); + return val; +} + inline closure_t make_closure(svalue_t *(*func)(svalue_t, svalue_t*), @@ -111,6 +124,7 @@ main(void) { (void)box_float; (void)box_double; (void)box_string; + (void)box_closure; return 0; } #endif diff --git a/tokenize.h b/tokenize.h index a3c869f..0365152 100644 --- a/tokenize.h +++ b/tokenize.h @@ -37,17 +37,33 @@ typedef struct { hsh_HashTable memo; } token_stream; -bool push_token(token_stream*, token_t); +bool +push_token(token_stream*, token_t); -bool pop_token(token_stream*); +bool +pop_token(token_stream*); -token_t peek_token(token_stream*); +token_t +peek_token(token_stream*); -token_stream tokenize(source_t, uint32_t, const uint32_t); +token_stream +tokenize(source_t, uint32_t, const uint32_t); -bool release_tokens(token_stream*); +bool +release_tokens(token_stream*); -static uint32_t match_int(source_t, uint32_t, const uint32_t); -static uint32_t match_float(source_t, uint32_t, const uint32_t); -static uint32_t match_identifier(source_t, uint32_t, const uint32_t); -static uint32_t match_symbol(source_t, uint32_t, const uint32_t); +static uint32_t +match_int(source_t, uint32_t, const uint32_t); + +static uint32_t +match_float(source_t, uint32_t, const uint32_t); + +static uint32_t +match_identifier(source_t, uint32_t, const uint32_t); + +static uint32_t +match_symbol(source_t, uint32_t, const uint32_t); + +int +free_token(const void *, + const void *);