Browse Source

box closures, add missing prototypes

master
nisstyre56 10 years ago
parent
commit
0bd526065d
  1. 10
      Makefile
  2. 46
      RTS.h
  3. 18
      closures.c
  4. 34
      tokenize.h

10
Makefile

@ -1,8 +1,6 @@
$(CC)=clang
default: tokenize.c closures.c tokenize.h RTS.h 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 -Wmissing-prototypes -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 ./closures.c;
unsafe: tokenize.c closures.c tokenize.h RTS.h unsafe: tokenize.c closures.c tokenize.h RTS.h
$(CC) -DNDEBUG -std=c99 -O3 ./tokenize.c -lmaa; $(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; $(CC) -shared -o closures.so closures.o;
debug: tokenize.c closures.c tokenize.h RTS.h 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) -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; $(CC) -shared -o closures.so closures.o;

46
RTS.h

@ -1,32 +1,49 @@
typedef typedef
struct { struct {
size_t size; size_t size;
char* string; char *string;
} sc_string_t; } sc_string_t;
/* This is not the most space efficient representation
* However it is an easy to understand and debug one
*/
typedef typedef
union { union {
sc_string_t string;
double doublev;
int integer; int integer;
float floating; float floating;
double doublev;
sc_string_t string;
struct closure_t *closure; struct closure_t *closure;
} svalue_variants_t; } svalue_variants_t;
/* The tag values for each different type */
typedef typedef
enum { enum {
INT = 0, INT = 0,
FLOAT = 1, FLOAT = 1,
DOUBLE = 2, DOUBLE = 2,
STRING = 3 STRING = 3,
CLOSURE = 4
} stype_t; } stype_t;
/* An actual boxed scheme value */
typedef typedef
struct { struct {
stype_t type_tag; stype_t type_tag;
svalue_variants_t value; svalue_variants_t value;
} svalue_t; } 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 typedef
struct { struct {
svalue_t *(*func)(svalue_t, svalue_t*); svalue_t *(*func)(svalue_t, svalue_t*);
@ -44,12 +61,29 @@ svalue_t *
box_value(svalue_variants_t, stype_t); box_value(svalue_variants_t, stype_t);
closure_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* svalue_t *
invoke(closure_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 #ifndef LIB
static svalue_t* static svalue_t*
make_adder_inner(svalue_t, svalue_t *); make_adder_inner(svalue_t, svalue_t *);

18
closures.c

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "error.h" #include "error.h"
#include "closures.h" #include "RTS.h"
inline svalue_t * inline svalue_t *
box_value(svalue_variants_t value, box_value(svalue_variants_t value,
@ -25,11 +25,14 @@ box_value(svalue_variants_t value,
case STRING: case STRING:
val->value = value; val->value = value;
val->type_tag = type; val->type_tag = type;
case CLOSURE:
val->value = value;
val->type_tag = type;
} }
return val; return val;
} }
svalue_t * inline svalue_t *
box_int(int x) { box_int(int x) {
svalue_t *val = malloc(sizeof(svalue_t)); svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val); CHECK(val);
@ -74,6 +77,16 @@ box_string(char *chars, size_t n) {
return val; 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 inline closure_t
make_closure(svalue_t *(*func)(svalue_t, svalue_t*), make_closure(svalue_t *(*func)(svalue_t, svalue_t*),
@ -111,6 +124,7 @@ main(void) {
(void)box_float; (void)box_float;
(void)box_double; (void)box_double;
(void)box_string; (void)box_string;
(void)box_closure;
return 0; return 0;
} }
#endif #endif

34
tokenize.h

@ -37,17 +37,33 @@ typedef struct {
hsh_HashTable memo; hsh_HashTable memo;
} token_stream; } 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
static uint32_t match_float(source_t, uint32_t, const uint32_t); match_int(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_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 *);