Browse Source

add helper function for invoking closures that only take one argument

master
nisstyre56 10 years ago
parent
commit
ec520cb785
  1. 34
      Makefile
  2. 21
      RTS.c
  3. 4
      RTS.h
  4. BIN
      RTS.o
  5. BIN
      RTS.so
  6. BIN
      rts_test
  7. BIN
      tokenize.o
  8. BIN
      tokenize.so
  9. BIN
      tokenize_debug
  10. BIN
      tokenize_test

34
Makefile

@ -1,34 +1,34 @@
default: tokenize.c closures.c tokenize.h RTS.h
$(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;
default: tokenize.c RTS.c tokenize.h RTS.h
$(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 -O3 ./tokenize.c -lmaa -o tokenize_test;
$(CC) -DNDEBUG -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 -O3 ./RTS.c -o rts_test;
unsafe: tokenize.c closures.c tokenize.h RTS.h
unsafe: tokenize.c RTS.c tokenize.h RTS.h
$(CC) -DNDEBUG -std=c99 -O3 ./tokenize.c -lmaa;
$(CC) -DNDEBUG -std=c99 -O3 ./closures.c;
$(CC) -DNDEBUG -std=c99 -O3 ./RTS.c;
unsafelib: tokenize.c closures.c tokenize.h RTS.h
unsafelib: tokenize.c RTS.c tokenize.h RTS.h
$(CC) -DLIB -DNDEBUG -c -fpic -std=c99 -O3 ./tokenize.c;
$(CC) -shared -o tokenize.so tokenize.o -lmaa;
$(CC) -DLIB -DNDEBUG -c -fpic -std=c99 -O3 ./closures.c;
$(CC) -shared -o closures.so closures.o;
$(CC) -DLIB -DNDEBUG -c -fpic -std=c99 -O3 ./RTS.c;
$(CC) -shared -o RTS.so RTS.o;
lib: tokenize.c closures.c tokenize.h RTS.h
lib: tokenize.c RTS.c tokenize.h RTS.h
$(CC) -DLIB -DNDEBUG -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 -O3 ./tokenize.c
$(CC) -shared -o tokenize.so tokenize.o -lmaa;
$(CC) -DLIB -DNDEBUG -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 -O3 ./closures.c;
$(CC) -shared -o closures.so closures.o;
$(CC) -DLIB -DNDEBUG -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Werror -std=c99 -O3 ./RTS.c;
$(CC) -shared -o RTS.so RTS.o;
debug: tokenize.c closures.c tokenize.h RTS.h
$(CC) -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./tokenize.c -lmaa;
$(CC) -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./closures.c;
debug: tokenize.c RTS.c tokenize.h RTS.h
$(CC) -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./tokenize.c -lmaa -o tokenize_debug;
$(CC) -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./RTS.c;
debuglib: tokenize.c closures.c tokenize.h RTS.h
debuglib: tokenize.c RTS.c tokenize.h RTS.h
$(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 -Wmissing-prototypes -Werror -std=c99 ./closures.c;
$(CC) -shared -o closures.so closures.o;
$(CC) -g -c -fpic -Wall -Wextra -pedantic -Wpointer-arith -Wmissing-prototypes -Werror -std=c99 ./RTS.c;
$(CC) -shared -o RTS.so RTS.o;

21
RTS.c

@ -145,6 +145,21 @@ invoke(svalue_t *closure, svalue_t **arguments) {
return closure->value.closure->func(arguments, closure->value.closure->fvars);
}
/* Special case where there is only on argument
* this might end up only being used for testing
* since the code generator will construct singleton
* arrays most likely, anyway
*/
inline svalue_t*
invoke1(svalue_t *closure, svalue_t *arg) {
svalue_t **args = malloc(sizeof (svalue_t *));
CHECK(args);
args[0] = arg;
svalue_t* result = invoke(closure, args);
free(args);
return result;
}
/*
* The process for closure conversion basically involves finding all of the free variables
* This will give the number of variables the environment must hold in total
@ -189,9 +204,9 @@ main(void) {
/* Get the final closure */
svalue_t *closure1 = make_closure(make_doubleadder, env);
/* Invoke the closure that the closure returns */
svalue_t *c1 = invoke(closure1, &box_int(23));
svalue_t *c2 = invoke(c1, &box_int(5));
svalue_t *result = invoke(c2, &box_int(334));
svalue_t *c1 = invoke1(closure1, box_int(23));
svalue_t *c2 = invoke1(c1, box_int(5));
svalue_t *result = invoke1(c2, box_int(334));
/* The final result */
printf("print 23 + 5 + 334 == %d\n", result->value.integer);
svalue_t *a = box_int(123);

4
RTS.h

@ -69,6 +69,10 @@ make_closure(svalue_t *(*func)(svalue_t**, svalue_t**),
svalue_t *
invoke(svalue_t*, svalue_t**);
svalue_t *
invoke1(svalue_t*, svalue_t*);
svalue_t *
box_int(int x);

BIN
RTS.o

Binary file not shown.

BIN
RTS.so

Binary file not shown.

BIN
rts_test

Binary file not shown.

BIN
tokenize.o

Binary file not shown.

BIN
tokenize.so

Binary file not shown.

BIN
tokenize_debug

Binary file not shown.

BIN
tokenize_test

Binary file not shown.