Browse Source

updated makefile, start closure RTS code

master
nisstyre56 10 years ago
parent
commit
965c77016b
  1. 18
      Makefile
  2. 114
      closures.c
  3. 58
      closures.h
  4. 1
      error.h
  5. 35
      tokenize.c

18
Makefile

@ -1,3 +1,15 @@
schreammake: tokenize.c
gcc -DNDEBUG -c -fPIC -O3 -Wall -Wextra -pedantic -Wpointer-arith -Werror --std=c99 ./tokenize.c;
gcc -lmaa -shared -o tokenize.so tokenize.o;
$(CC)=clang
default: tokenize.c closures.c
$(CC) -DNDEBUG -c -fPIC -Wall -Wextra -pedantic -Wpointer-arith -Werror --std=c99 -O3 ./tokenize.c
$(CC) -lmaa -shared -o tokenize.so tokenize.o;
$(CC) -DNDEBUG -c -fPIC -Wall -Wextra -pedantic -Wpointer-arith -Werror --std=c99 -O3 ./closures.c;
$(CC) -shared -o closures.so closures.o;
debug: tokenize.c closures.c
$(CC) -g -c -fPIC -Wall -Wextra -pedantic -Wpointer-arith -Werror --std=c99 ./tokenize.c;
$(CC) -lmaa -shared -o tokenize.so tokenize.o;
$(CC) -g -c -fPIC -Wall -Wextra -pedantic -Wpointer-arith -Werror --std=c99 ./closures.c;
$(CC) -shared -o closures.so closures.o;

114
closures.c

@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "error.h"
#include "closures.h"
inline svalue_t *
box_value(svalue_variants_t value,
stype_t type) {
svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val);
switch (type) {
case INT:
val->value = value;
val->type_tag = type;
break;
case FLOAT:
val->value = value;
val->type_tag = type;
break;
case DOUBLE:
val->value = value;
val->type_tag = type;
case STRING:
val->value = value;
val->type_tag = type;
}
return val;
}
svalue_t *
box_int(int x) {
svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val);
svalue_variants_t value_val;
value_val.integer = x;
val = box_value(value_val, INT);
return val;
}
inline svalue_t *
box_float(float x) {
svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val);
svalue_variants_t value_val;
value_val.floating = x;
val = box_value(value_val, FLOAT);
return val;
}
inline svalue_t *
box_double(double x) {
svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val);
svalue_variants_t value_val;
value_val.doublev = x;
val = box_value(value_val, DOUBLE);
return val;
}
inline svalue_t *
box_string(char *chars, size_t n) {
sc_string_t strval;
strval.string = chars;
strval.size = n;
svalue_t *val = malloc(sizeof(svalue_t));
CHECK(val);
svalue_variants_t value_val;
value_val.string = strval;
val = box_value(value_val, STRING);
return val;
}
inline closure_t
make_closure(svalue_t *(*func)(svalue_t, svalue_t*),
svalue_t *fvars) {
closure_t closure;
closure.func = func;
closure.fvars = fvars;
return closure;
}
inline svalue_t*
invoke(closure_t closure, svalue_t val) {
svalue_t *(*func)(svalue_t, svalue_t*) = closure.func;
return func(val, closure.fvars);
}
/*static svalue_t*
make_adder_inner(svalue_t x, svalue_t *env) {
svalue_variants_t val;
val.integer = env[0].value.integer + x.value.integer;
return box_value(val, INT);
}
static closure_t
make_adder(svalue_t *inc) {
closure_t closure = make_closure(make_adder_inner, inc);
return closure;
}*/
/*int
main(void) {
closure_t add2 = make_adder(box_int(2));
printf("%d\n", invoke(add2, *box_int(5))->value.integer);
(void)box_float;
(void)box_double;
(void)box_string;
return 0;
}*/

58
closures.h

@ -0,0 +1,58 @@
typedef
struct {
size_t size;
char* string;
} sc_string_t;
typedef
union {
sc_string_t string;
double doublev;
int integer;
float floating;
struct closure_t *closure;
} svalue_variants_t;
typedef
enum {
INT = 0,
FLOAT = 1,
DOUBLE = 2,
STRING = 3
} stype_t;
typedef
struct {
stype_t type_tag;
svalue_variants_t value;
} svalue_t;
typedef
struct {
svalue_t *(*func)(svalue_t, svalue_t*);
svalue_t *fvars;
} closure_t;
typedef
struct {
svalue_t head;
struct cell *tail;
}
cell;
svalue_t *
box_value(svalue_variants_t, stype_t);
closure_t
make_closure(svalue_t* (*func)(svalue_t, svalue_t*),
svalue_t*);
svalue_t*
invoke(closure_t, svalue_t);
/*static svalue_t*
make_adder_inner(svalue_t, svalue_t *);
static closure_t
make_adder(svalue_t *);
*/

1
error.h

@ -0,0 +1 @@
#define CHECK(ptr) if ((ptr) == NULL) { printf("Failed to allocate memory\n"); exit(EXIT_FAILURE); }

35
tokenize.c

@ -5,6 +5,7 @@
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include "error.h"
#include "maa.h"
#include "tokenize.h"
@ -13,7 +14,7 @@
* it also tokenizes things like number, string, and symbol literals
*/
static const token_t nulltok = {
const token_t nulltok = {
.token_type = EMPTY,
{
.null_token=false
@ -61,7 +62,7 @@ string_head(uint32_t n,
assert(n > 0 && n <= in_len);
int iserror = snprintf(out, (size_t)n+1 , "%s", in);
assert((iserror != -1) && (iserror == in_len));
assert((iserror != -1) && ((size_t)iserror == in_len));
if (iserror == -1) {
printf("Out of memory");
@ -89,7 +90,7 @@ push_token(token_stream *tokens,
size_t len;
size_t max;
assert(tokens != NULL);
CHECK(tokens);
len = tokens->length;
max = tokens->max_length;
@ -120,13 +121,13 @@ push_token(token_stream *tokens,
bool
pop_token(token_stream *tokens) {
size_t len;
assert(tokens != NULL);
CHECK(tokens);
len = tokens->length;
assert(len != 0);
len--;
assert(tokens->tokens != NULL);
CHECK(tokens->tokens);
tokens->length--;
return true;
@ -139,7 +140,7 @@ peek_token(token_stream *tokens) {
*/
size_t len = tokens->length;
size_t max = tokens->max_length;
assert(tokens != NULL);
CHECK(tokens);
assert(len != 0);
if (len == 0 || len > max) {
@ -157,7 +158,7 @@ match_int(source_t source,
*/
uint32_t i = begin;
uint32_t test;
assert(source != NULL);
CHECK(source);
assert(length > 0);
if (source[i] == '+' ||
@ -192,7 +193,7 @@ match_float(source_t source,
* ALWAYS returns the position + 1 to avoid confusion with false (which is a valid index)
*/
uint32_t i, leading_int_match, trailing_int_match;
assert(source != NULL);
CHECK(source);
assert(length > 0);
i = begin;
@ -242,7 +243,7 @@ match_identifier(source_t source,
* return false
*/
uint32_t i = begin;
assert(source != NULL);
CHECK(source);
assert(length > 0);
while (i < length &&
@ -264,7 +265,7 @@ match_symbol(source_t source,
uint32_t begin,
const uint32_t length) {
uint32_t i;
assert(source != NULL);
CHECK(source);
assert(length > 0);
i = begin;
@ -316,7 +317,7 @@ tokenize(source_t source,
assert(begin == 0);
assert(length > 0);
assert(source != NULL);
CHECK(source);
token_stack.length = 0;
token_stack.max_length = STACK_SIZE;
@ -353,7 +354,7 @@ tokenize(source_t source,
source[position] = lookahead;
assert(position > begin);
current_token_val = calloc(((position - begin) + 1), sizeof(char));
assert(current_token_val != NULL);
CHECK(current_token_val);
extract_token(position, begin, source, current_token_val);
hsh_insert(token_stack.memo, current_token_val, current_token_val);
current_token.floating = current_token_val;
@ -374,7 +375,7 @@ tokenize(source_t source,
source[position] = lookahead;
current_token_val = calloc(((position - begin) + 1), sizeof(char));
assert(current_token_val != NULL);
CHECK(current_token_val);
extract_token(position, begin, source, current_token_val);
hsh_insert(token_stack.memo, current_token_val, current_token_val);
current_token.integer = current_token_val;
@ -395,7 +396,7 @@ tokenize(source_t source,
source[position] = lookahead;
current_token_val = calloc(((position - begin) + 1), sizeof(char));
assert(current_token_val != NULL);
CHECK(current_token_val);
extract_token(position, begin, source, current_token_val);
hsh_insert(token_stack.memo, current_token_val, current_token_val);
current_token.symbol = current_token_val;
@ -421,7 +422,7 @@ tokenize(source_t source,
source[position] = lookahead;
current_token_val = calloc(((position - begin) + 1), sizeof(char));
assert(current_token_val != NULL);
CHECK(current_token_val);
extract_token(position, begin, source, current_token_val);
hsh_insert(token_stack.memo, current_token_val, current_token_val);
current_token.identifier = current_token_val;
@ -457,8 +458,8 @@ release_tokens(token_stream *tokens) {
/* Iterate through the stack, release each token
* Then release the entire stack
*/
assert(tokens != NULL);
assert(tokens->tokens != NULL);
CHECK(tokens);
CHECK(tokens->tokens);
assert(tokens->max_length > 0);
free(tokens->tokens);
hsh_iterate(tokens->memo, free_token);