From 611543c48b3bc16972946a73d0789e1ca9127876 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 24 Jan 2019 00:15:58 -0500 Subject: [PATCH] Add source amalgamation The amalgamated source concatenates all sources to a file janet.c which can be used for embedding janet, much in the same way as sqlite or mongoose. --- Makefile | 21 ++++++++----- src/core/abstract.c | 2 ++ src/core/array.c | 39 +++++++++++++----------- src/core/asm.c | 9 ++++-- src/core/buffer.c | 58 ++++++++++++++++++----------------- src/core/bytecode.c | 2 ++ src/core/capi.c | 2 ++ src/core/cfuns.c | 2 ++ src/core/compile.c | 6 ++-- src/core/compile.h | 2 ++ src/core/corelib.c | 6 ++-- src/core/debug.c | 34 +++++++++++---------- src/core/emit.c | 2 ++ src/core/emit.h | 2 ++ src/core/fiber.c | 26 ++++++++-------- src/core/fiber.h | 2 ++ src/core/gc.c | 2 ++ src/core/gc.h | 3 +- src/core/io.c | 60 ++++++++++++++++++++----------------- src/core/marsh.c | 7 +++-- src/core/math.c | 9 ++++-- src/core/os.c | 9 ++++-- src/core/parse.c | 46 ++++++++++++++-------------- src/core/peg.c | 14 +++++---- src/core/pp.c | 2 ++ src/core/regalloc.c | 2 ++ src/core/run.c | 2 ++ src/core/specials.c | 2 ++ src/core/string.c | 73 +++++++++++++++++++++++---------------------- src/core/strtod.c | 5 +++- src/core/struct.c | 4 +-- src/core/symcache.c | 2 ++ src/core/symcache.h | 2 ++ src/core/table.c | 26 ++++++++-------- src/core/tuple.c | 18 ++++++----- src/core/util.c | 5 +++- src/core/util.h | 2 ++ src/core/value.c | 2 ++ src/core/vector.c | 2 ++ src/core/vector.h | 2 ++ src/core/vm.c | 2 ++ src/core/wrap.c | 2 ++ tools/amalg.janet | 65 ++++++++++++++++++++++++++++++++++++++++ tools/xxd.c | 4 +-- 44 files changed, 373 insertions(+), 216 deletions(-) create mode 100644 tools/amalg.janet diff --git a/Makefile b/Makefile index 64cd1abe..287e2b2c 100644 --- a/Makefile +++ b/Makefile @@ -119,17 +119,24 @@ build/init.gen.c: src/mainclient/init.janet build/xxd build/webinit.gen.c: src/webclient/webinit.janet build/xxd build/xxd $< $@ janet_gen_webinit +######################## +##### Amalgamation ##### +######################## + +amalg: build/janet.c build/janet.h + +build/janet.c: $(JANET_LOCAL_HEADERS) $(JANET_CORE_SOURCES) tools/amalg.janet $(JANET_TARGET) + $(JANET_TARGET) tools/amalg.janet > $@ + +build/janet.h: src/include/janet/janet.h + cp $< $@ + ################### ##### Testing ##### ################### -TEST_SOURCES=$(wildcard ctest/*.c) -TEST_PROGRAMS=$(patsubst ctest/%.c,build/%.out,$(TEST_SOURCES)) TEST_SCRIPTS=$(wildcard test/suite*.janet) -build/%.out: ctest/%.c $(JANET_CORE_OBJECTS) - $(CC) $(CFLAGS) -o $@ $^ $(CLIBS) - repl: $(JANET_TARGET) ./$(JANET_TARGET) @@ -142,11 +149,9 @@ valgrind: $(JANET_TARGET) $(VALGRIND_COMMAND) ./$(JANET_TARGET) test: $(JANET_TARGET) $(TEST_PROGRAMS) - for f in build/*.out; do "$$f" || exit; done for f in test/*.janet; do ./$(JANET_TARGET) "$$f" || exit; done valtest: $(JANET_TARGET) $(TEST_PROGRAMS) - for f in build/*.out; do $(VALGRIND_COMMAND) "$$f" || exit; done for f in test/*.janet; do $(VALGRIND_COMMAND) ./$(JANET_TARGET) "$$f" || exit; done callgrind: $(JANET_TARGET) @@ -203,6 +208,6 @@ uninstall: -rm -rf $(INCLUDEDIR) $(LDCONFIG) -.PHONY: clean install repl debug valgrind test \ +.PHONY: clean install repl debug valgrind test amalg \ valtest emscripten dist uninstall docs grammar \ $(TEST_PROGRAM_PHONIES) $(TEST_PROGRAM_VALPHONIES) diff --git a/src/core/abstract.c b/src/core/abstract.c index e4827b07..89dd60a1 100644 --- a/src/core/abstract.c +++ b/src/core/abstract.c @@ -20,8 +20,10 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" +#endif /* Create new userdata */ void *janet_abstract(const JanetAbstractType *atype, size_t size) { diff --git a/src/core/array.c b/src/core/array.c index aef24a93..4bd7a8c7 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -20,9 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" #include "util.h" +#endif + #include /* Initializes an array */ @@ -119,26 +122,26 @@ Janet janet_array_peek(JanetArray *array) { /* C Functions */ -static Janet cfun_new(int32_t argc, Janet *argv) { +static Janet cfun_array_new(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); int32_t cap = janet_getinteger(argv, 0); JanetArray *array = janet_array(cap); return janet_wrap_array(array); } -static Janet cfun_pop(int32_t argc, Janet *argv) { +static Janet cfun_array_pop(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetArray *array = janet_getarray(argv, 0); return janet_array_pop(array); } -static Janet cfun_peek(int32_t argc, Janet *argv) { +static Janet cfun_array_peek(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetArray *array = janet_getarray(argv, 0); return janet_array_peek(array); } -static Janet cfun_push(int32_t argc, Janet *argv) { +static Janet cfun_array_push(int32_t argc, Janet *argv) { janet_arity(argc, 1, -1); JanetArray *array = janet_getarray(argv, 0); int32_t newcount = array->count - 1 + argc; @@ -148,7 +151,7 @@ static Janet cfun_push(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_ensure(int32_t argc, Janet *argv) { +static Janet cfun_array_ensure(int32_t argc, Janet *argv) { janet_fixarity(argc, 3); JanetArray *array = janet_getarray(argv, 0); int32_t newcount = janet_getinteger(argv, 1); @@ -158,7 +161,7 @@ static Janet cfun_ensure(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_slice(int32_t argc, Janet *argv) { +static Janet cfun_array_slice(int32_t argc, Janet *argv) { JanetRange range = janet_getslice(argc, argv); JanetView view = janet_getindexed(argv, 0); JanetArray *array = janet_array(range.end - range.start); @@ -167,7 +170,7 @@ static Janet cfun_slice(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static Janet cfun_concat(int32_t argc, Janet *argv) { +static Janet cfun_array_concat(int32_t argc, Janet *argv) { int32_t i; janet_arity(argc, 1, -1); JanetArray *array = janet_getarray(argv, 0); @@ -191,7 +194,7 @@ static Janet cfun_concat(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static Janet cfun_insert(int32_t argc, Janet *argv) { +static Janet cfun_array_insert(int32_t argc, Janet *argv) { size_t chunksize, restsize; janet_arity(argc, 2, -1); JanetArray *array = janet_getarray(argv, 0); @@ -212,47 +215,47 @@ static Janet cfun_insert(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static const JanetReg cfuns[] = { - {"array/new", cfun_new, +static const JanetReg array_cfuns[] = { + {"array/new", cfun_array_new, JDOC("(array/new capacity)\n\n" "Creates a new empty array with a pre-allocated capacity. The same as " "(array) but can be more efficient if the maximum size of an array is known.") }, - {"array/pop", cfun_pop, + {"array/pop", cfun_array_pop, JDOC("(array/pop arr)\n\n" "Remove the last element of the array and return it. If the array is empty, will return nil. Modifies " "the input array.") }, - {"array/peek", cfun_peek, + {"array/peek", cfun_array_peek, JDOC("(array/peek arr)\n\n" "Returns the last element of the array. Does not modify the array.") }, - {"array/push", cfun_push, + {"array/push", cfun_array_push, JDOC("(array/push arr x)\n\n" "Insert an element in the end of an array. Modifies the input array and returns it.") }, - {"array/ensure", cfun_ensure, + {"array/ensure", cfun_array_ensure, JDOC("(array/ensure arr capacity)\n\n" "Ensures that the memory backing the array has enough memory for capacity " "items. Capacity must be an integer. If the backing capacity is already enough, " "then this function does nothing. Otherwise, the backing memory will be reallocated " "so that there is enough space.") }, - {"array/slice", cfun_slice, + {"array/slice", cfun_array_slice, JDOC("(array/slice arrtup [, start=0 [, end=(length arrtup)]])\n\n" "Takes a slice of array or tuple from start to end. The range is half open, " "[start, end). Indexes can also be negative, indicating indexing from the end of the " "end of the array. By default, start is 0 and end is the length of the array. " "Returns a new array.") }, - {"array/concat", cfun_concat, + {"array/concat", cfun_array_concat, JDOC("(array/concat arr & parts)\n\n" "Concatenates a variadic number of arrays (and tuples) into the first argument " "which must an array. If any of the parts are arrays or tuples, their elements will " "be inserted into the array. Otherwise, each part in parts will be appended to arr in order. " "Return the modified array arr.") }, - {"array/insert", cfun_insert, + {"array/insert", cfun_array_insert, JDOC("(array/insert arr at & xs)\n\n" "Insert all of xs into array arr at index at. at should be an integer " "0 and the length of the array. A negative value for at will index from " @@ -264,5 +267,5 @@ static const JanetReg cfuns[] = { /* Load the array module */ void janet_lib_array(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, array_cfuns); } diff --git a/src/core/asm.c b/src/core/asm.c index eebaeb05..75696abd 100644 --- a/src/core/asm.c +++ b/src/core/asm.c @@ -20,9 +20,12 @@ * IN THE SOFTWARE. */ -#include +#ifndef JANET_AMALG #include #include "util.h" +#endif + +#include /* Conditionally compile this file */ #ifdef JANET_ASSEMBLER @@ -930,7 +933,7 @@ static Janet cfun_disasm(int32_t argc, Janet *argv) { return janet_disasm(f->def); } -static const JanetReg cfuns[] = { +static const JanetReg asm_cfuns[] = { {"asm", cfun_asm, JDOC("(asm assembly)\n\n" "Returns a new function that is the compiled result of the assembly.\n" @@ -948,7 +951,7 @@ static const JanetReg cfuns[] = { /* Load the library */ void janet_lib_asm(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, asm_cfuns); } #endif diff --git a/src/core/buffer.c b/src/core/buffer.c index 59ca8970..d318a6e0 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" #include "util.h" +#endif /* Initialize a buffer */ JanetBuffer *janet_buffer_init(JanetBuffer *buffer, int32_t capacity) { @@ -155,14 +157,14 @@ void janet_buffer_push_u64(JanetBuffer *buffer, uint64_t x) { /* C functions */ -static Janet cfun_new(int32_t argc, Janet *argv) { +static Janet cfun_buffer_new(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); int32_t cap = janet_getinteger(argv, 0); JanetBuffer *buffer = janet_buffer(cap); return janet_wrap_buffer(buffer); } -static Janet cfun_new_filled(int32_t argc, Janet *argv) { +static Janet cfun_buffer_new_filled(int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); int32_t count = janet_getinteger(argv, 0); int32_t byte = 0; @@ -175,7 +177,7 @@ static Janet cfun_new_filled(int32_t argc, Janet *argv) { return janet_wrap_buffer(buffer); } -static Janet cfun_u8(int32_t argc, Janet *argv) { +static Janet cfun_buffer_u8(int32_t argc, Janet *argv) { int32_t i; janet_arity(argc, 1, -1); JanetBuffer *buffer = janet_getbuffer(argv, 0); @@ -185,7 +187,7 @@ static Janet cfun_u8(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_word(int32_t argc, Janet *argv) { +static Janet cfun_buffer_word(int32_t argc, Janet *argv) { int32_t i; janet_arity(argc, 1, -1); JanetBuffer *buffer = janet_getbuffer(argv, 0); @@ -199,7 +201,7 @@ static Janet cfun_word(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_chars(int32_t argc, Janet *argv) { +static Janet cfun_buffer_chars(int32_t argc, Janet *argv) { int32_t i; janet_arity(argc, 1, -1); JanetBuffer *buffer = janet_getbuffer(argv, 0); @@ -210,14 +212,14 @@ static Janet cfun_chars(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_clear(int32_t argc, Janet *argv) { +static Janet cfun_buffer_clear(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetBuffer *buffer = janet_getbuffer(argv, 0); buffer->count = 0; return argv[0]; } -static Janet cfun_popn(int32_t argc, Janet *argv) { +static Janet cfun_buffer_popn(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetBuffer *buffer = janet_getbuffer(argv, 0); int32_t n = janet_getinteger(argv, 1); @@ -230,7 +232,7 @@ static Janet cfun_popn(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_slice(int32_t argc, Janet *argv) { +static Janet cfun_buffer_slice(int32_t argc, Janet *argv) { JanetRange range = janet_getslice(argc, argv); JanetByteView view = janet_getbytes(argv, 0); JanetBuffer *buffer = janet_buffer(range.end - range.start); @@ -253,7 +255,7 @@ static void bitloc(int32_t argc, Janet *argv, JanetBuffer **b, int32_t *index, i *bit = which_bit; } -static Janet cfun_bitset(int32_t argc, Janet *argv) { +static Janet cfun_buffer_bitset(int32_t argc, Janet *argv) { int bit; int32_t index; JanetBuffer *buffer; @@ -262,7 +264,7 @@ static Janet cfun_bitset(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_bitclear(int32_t argc, Janet *argv) { +static Janet cfun_buffer_bitclear(int32_t argc, Janet *argv) { int bit; int32_t index; JanetBuffer *buffer; @@ -271,7 +273,7 @@ static Janet cfun_bitclear(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_bitget(int32_t argc, Janet *argv) { +static Janet cfun_buffer_bitget(int32_t argc, Janet *argv) { int bit; int32_t index; JanetBuffer *buffer; @@ -279,7 +281,7 @@ static Janet cfun_bitget(int32_t argc, Janet *argv) { return janet_wrap_boolean(buffer->data[index] & (1 << bit)); } -static Janet cfun_bittoggle(int32_t argc, Janet *argv) { +static Janet cfun_buffer_bittoggle(int32_t argc, Janet *argv) { int bit; int32_t index; JanetBuffer *buffer; @@ -288,7 +290,7 @@ static Janet cfun_bittoggle(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_blit(int32_t argc, Janet *argv) { +static Janet cfun_buffer_blit(int32_t argc, Janet *argv) { janet_arity(argc, 2, 5); JanetBuffer *dest = janet_getbuffer(argv, 0); JanetByteView src = janet_getbytes(argv, 1); @@ -315,67 +317,67 @@ static Janet cfun_blit(int32_t argc, Janet *argv) { return argv[0]; } -static const JanetReg cfuns[] = { - {"buffer/new", cfun_new, +static const JanetReg buffer_cfuns[] = { + {"buffer/new", cfun_buffer_new, JDOC("(buffer/new capacity)\n\n" "Creates a new, empty buffer with enough memory for capacity bytes. " "Returns a new buffer.") }, - {"buffer/new-filled", cfun_new_filled, + {"buffer/new-filled", cfun_buffer_new_filled, JDOC("(buffer/new-filled count [, byte=0])\n\n" "Creates a new buffer of length count filled with byte. " "Returns the new buffer.") }, - {"buffer/push-byte", cfun_u8, + {"buffer/push-byte", cfun_buffer_u8, JDOC("(buffer/push-byte buffer x)\n\n" "Append a byte to a buffer. Will expand the buffer as necessary. " "Returns the modified buffer. Will throw an error if the buffer overflows.") }, - {"buffer/push-word", cfun_word, + {"buffer/push-word", cfun_buffer_word, JDOC("(buffer/push-word buffer x)\n\n" "Append a machine word to a buffer. The 4 bytes of the integer are appended " "in twos complement, big endian order, unsigned. Returns the modified buffer. Will " "throw an error if the buffer overflows.") }, - {"buffer/push-string", cfun_chars, + {"buffer/push-string", cfun_buffer_chars, JDOC("(buffer/push-string buffer str)\n\n" "Push a string onto the end of a buffer. Non string values will be converted " "to strings before being pushed. Returns the modified buffer. " "Will throw an error if the buffer overflows.") }, - {"buffer/popn", cfun_popn, + {"buffer/popn", cfun_buffer_popn, JDOC("(buffer/popn buffer n)\n\n" "Removes the last n bytes from the buffer. Returns the modified buffer.") }, - {"buffer/clear", cfun_clear, + {"buffer/clear", cfun_buffer_clear, JDOC("(buffer/clear buffer)\n\n" "Sets the size of a buffer to 0 and empties it. The buffer retains " "its memory so it can be efficiently refilled. Returns the modified buffer.") }, - {"buffer/slice", cfun_slice, + {"buffer/slice", cfun_buffer_slice, JDOC("(buffer/slice bytes [, start=0 [, end=(length bytes)]])\n\n" "Takes a slice of a byte sequence from start to end. The range is half open, " "[start, end). Indexes can also be negative, indicating indexing from the end of the " "end of the array. By default, start is 0 and end is the length of the buffer. " "Returns a new buffer.") }, - {"buffer/bit-set", cfun_bitset, + {"buffer/bit-set", cfun_buffer_bitset, JDOC("(buffer/bit-set buffer index)\n\n" "Sets the bit at the given bit-index. Returns the buffer.") }, - {"buffer/bit-clear", cfun_bitclear, + {"buffer/bit-clear", cfun_buffer_bitclear, JDOC("(buffer/bit-clear buffer index)\n\n" "Clears the bit at the given bit-index. Returns the buffer.") }, - {"buffer/bit", cfun_bitget, + {"buffer/bit", cfun_buffer_bitget, JDOC("(buffer/bit buffer index)\n\n" "Gets the bit at the given bit-index. Returns true if the bit is set, false if not.") }, - {"buffer/bit-toggle", cfun_bittoggle, + {"buffer/bit-toggle", cfun_buffer_bittoggle, JDOC("(buffer/bit-toggle buffer index)\n\n" "Toggles the bit at the given bit index in buffer. Returns the buffer.") }, - {"buffer/blit", cfun_blit, + {"buffer/blit", cfun_buffer_blit, JDOC("(buffer/blit dest src [, dest-start=0 [, src-start=0 [, src-end=-1]]])\n\n" "Insert the contents of src into dest. Can optionally take indices that " "indicate which part of src to copy into which part of dest. Indices can be " @@ -385,5 +387,5 @@ static const JanetReg cfuns[] = { }; void janet_lib_buffer(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, buffer_cfuns); } diff --git a/src/core/bytecode.c b/src/core/bytecode.c index 9de94445..1906e177 100644 --- a/src/core/bytecode.c +++ b/src/core/bytecode.c @@ -20,8 +20,10 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" +#endif /* Look up table for instructions */ enum JanetInstructionType janet_instructions[JOP_INSTRUCTION_COUNT] = { diff --git a/src/core/capi.c b/src/core/capi.c index 28cf9695..6c5d09f7 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "state.h" #include "fiber.h" +#endif void janet_panicv(Janet message) { if (janet_vm_fiber != NULL) { diff --git a/src/core/cfuns.c b/src/core/cfuns.c index 5dfc4532..bb28c69d 100644 --- a/src/core/cfuns.c +++ b/src/core/cfuns.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "compile.h" #include "emit.h" #include "vector.h" +#endif static int fixarity0(JanetFopts opts, JanetSlot *args) { (void) opts; diff --git a/src/core/compile.c b/src/core/compile.c index 9ee1b285..bbaae4e9 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -20,11 +20,13 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "compile.h" #include "emit.h" #include "vector.h" #include "util.h" +#endif JanetFopts janetc_fopts_default(JanetCompiler *c) { JanetFopts ret; @@ -724,7 +726,7 @@ static Janet cfun(int32_t argc, Janet *argv) { } } -static const JanetReg cfuns[] = { +static const JanetReg compile_cfuns[] = { {"compile", cfun, JDOC("(compile ast env [, source])\n\n" "Compiles an Abstract Syntax Tree (ast) into a janet function. " @@ -736,5 +738,5 @@ static const JanetReg cfuns[] = { }; void janet_lib_compile(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, compile_cfuns); } diff --git a/src/core/compile.h b/src/core/compile.h index bfaa2548..c24aaa41 100644 --- a/src/core/compile.h +++ b/src/core/compile.h @@ -23,8 +23,10 @@ #ifndef JANET_COMPILE_H #define JANET_COMPILE_H +#ifndef JANET_AMALG #include #include "regalloc.h" +#endif /* Tags for some functions for the prepared inliner */ #define JANET_FUN_DEBUG 1 diff --git a/src/core/corelib.c b/src/core/corelib.c index 6142ab0f..6cfaef36 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "compile.h" #include "state.h" #include "util.h" +#endif /* Generated bytes */ extern const unsigned char *janet_gen_core; @@ -236,7 +238,7 @@ static Janet janet_core_hash(int32_t argc, Janet *argv) { return janet_wrap_number(janet_hash(argv[0])); } -static const JanetReg cfuns[] = { +static const JanetReg corelib_cfuns[] = { {"native", janet_core_native, JDOC("(native path [,env])\n\n" "Load a native module from the given path. The path " @@ -596,7 +598,7 @@ JanetTable *janet_core_env(void) { Janet ret = janet_wrap_table(env); /* Load main functions */ - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, corelib_cfuns); #ifndef JANET_NO_BOOTSTRAP janet_quick_asm(env, JANET_FUN_YIELD, "debug", 0, 1, debug_asm, sizeof(debug_asm), diff --git a/src/core/debug.c b/src/core/debug.c index 57c26b0a..6fcda70c 100644 --- a/src/core/debug.c +++ b/src/core/debug.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" #include "state.h" #include "util.h" +#endif /* Implements functionality to build a debugger from within janet. * The repl should also be able to serve as pretty featured debugger @@ -111,7 +113,7 @@ static void helper_find_fun(int32_t argc, Janet *argv, JanetFuncDef **def, int32 *bytecode_offset = offset; } -static Janet cfun_break(int32_t argc, Janet *argv) { +static Janet cfun_debug_break(int32_t argc, Janet *argv) { JanetFuncDef *def; int32_t offset; helper_find(argc, argv, &def, &offset); @@ -119,7 +121,7 @@ static Janet cfun_break(int32_t argc, Janet *argv) { return janet_wrap_nil(); } -static Janet cfun_unbreak(int32_t argc, Janet *argv) { +static Janet cfun_debug_unbreak(int32_t argc, Janet *argv) { JanetFuncDef *def; int32_t offset; helper_find(argc, argv, &def, &offset); @@ -127,7 +129,7 @@ static Janet cfun_unbreak(int32_t argc, Janet *argv) { return janet_wrap_nil(); } -static Janet cfun_fbreak(int32_t argc, Janet *argv) { +static Janet cfun_debug_fbreak(int32_t argc, Janet *argv) { JanetFuncDef *def; int32_t offset; helper_find_fun(argc, argv, &def, &offset); @@ -135,7 +137,7 @@ static Janet cfun_fbreak(int32_t argc, Janet *argv) { return janet_wrap_nil(); } -static Janet cfun_unfbreak(int32_t argc, Janet *argv) { +static Janet cfun_debug_unfbreak(int32_t argc, Janet *argv) { JanetFuncDef *def; int32_t offset; helper_find_fun(argc, argv, &def, &offset); @@ -143,7 +145,7 @@ static Janet cfun_unfbreak(int32_t argc, Janet *argv) { return janet_wrap_nil(); } -static Janet cfun_lineage(int32_t argc, Janet *argv) { +static Janet cfun_debug_lineage(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetFiber *fiber = janet_getfiber(argv, 0); JanetArray *array = janet_array(0); @@ -200,7 +202,7 @@ static Janet doframe(JanetStackFrame *frame) { return janet_wrap_table(t); } -static Janet cfun_stack(int32_t argc, Janet *argv) { +static Janet cfun_debug_stack(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetFiber *fiber = janet_getfiber(argv, 0); JanetArray *array = janet_array(0); @@ -216,7 +218,7 @@ static Janet cfun_stack(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static Janet cfun_argstack(int32_t argc, Janet *argv) { +static Janet cfun_debug_argstack(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetFiber *fiber = janet_getfiber(argv, 0); JanetArray *array = janet_array(fiber->stacktop - fiber->stackstart); @@ -225,9 +227,9 @@ static Janet cfun_argstack(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static const JanetReg cfuns[] = { +static const JanetReg debug_cfuns[] = { { - "debug/break", cfun_break, + "debug/break", cfun_debug_break, JDOC("(debug/break source byte-offset)\n\n" "Sets a breakpoint with source a key at a given byte offset. An offset " "of 0 is the first byte in a file. Will throw an error if the breakpoint location " @@ -236,33 +238,33 @@ static const JanetReg cfuns[] = { "wil set a breakpoint at the 1000th byte of the file core.janet.") }, { - "debug/unbreak", cfun_unbreak, + "debug/unbreak", cfun_debug_unbreak, JDOC("(debug/unbreak source byte-offset)\n\n" "Remove a breakpoint with a source key at a given byte offset. An offset " "of 0 is the first byte in a file. Will throw an error if the breakpoint " "cannot be found.") }, { - "debug/fbreak", cfun_fbreak, + "debug/fbreak", cfun_debug_fbreak, JDOC("(debug/fbreak fun [,pc=0])\n\n" "Set a breakpoint in a given function. pc is an optional offset, which " "is in bytecode instructions. fun is a function value. Will throw an error " "if the offset is too large or negative.") }, { - "debug/unfbreak", cfun_unfbreak, + "debug/unfbreak", cfun_debug_unfbreak, JDOC("(debug/unfbreak fun [,pc=0])\n\n" "Unset a breakpoint set with debug/fbreak.") }, { - "debug/arg-stack", cfun_argstack, + "debug/arg-stack", cfun_debug_argstack, JDOC("(debug/arg-stack fiber)\n\n" "Gets all values currently on the fiber's argument stack. Normally, " "this should be empty unless the fiber signals while pushing arguments " "to make a function call. Returns a new array.") }, { - "debug/stack", cfun_stack, + "debug/stack", cfun_debug_stack, JDOC("(debug/stack fib)\n\n" "Gets information about the stack as an array of tables. Each table " "in the array contains information about a stack frame. The top most, current " @@ -279,7 +281,7 @@ static const JanetReg cfuns[] = { "\t:tail - boolean indicating a tail call") }, { - "debug/lineage", cfun_lineage, + "debug/lineage", cfun_debug_lineage, JDOC("(debug/lineage fib)\n\n" "Returns an array of all child fibers from a root fiber. This function " "is useful when a fiber signals or errors to an ancestor fiber. Using this function, " @@ -291,5 +293,5 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_debug(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, debug_cfuns); } diff --git a/src/core/emit.c b/src/core/emit.c index a570380e..e0e09341 100644 --- a/src/core/emit.c +++ b/src/core/emit.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "emit.h" #include "vector.h" #include "regalloc.h" +#endif /* Get a register */ int32_t janetc_allocfar(JanetCompiler *c) { diff --git a/src/core/emit.h b/src/core/emit.h index 94cc830d..59daf86c 100644 --- a/src/core/emit.h +++ b/src/core/emit.h @@ -23,7 +23,9 @@ #ifndef JANET_EMIT_H #define JANET_EMIT_H +#ifndef JANET_AMALG #include "compile.h" +#endif void janetc_emit(JanetCompiler *c, uint32_t instr); diff --git a/src/core/fiber.c b/src/core/fiber.c index 151481cf..7896e567 100644 --- a/src/core/fiber.c +++ b/src/core/fiber.c @@ -20,11 +20,13 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "fiber.h" #include "state.h" #include "gc.h" #include "util.h" +#endif static void fiber_reset(JanetFiber *fiber) { fiber->maxstack = JANET_STACK_MAX; @@ -296,7 +298,7 @@ void janet_fiber_popframe(JanetFiber *fiber) { /* CFuns */ -static Janet cfun_new(int32_t argc, Janet *argv) { +static Janet cfun_fiber_new(int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); JanetFunction *func = janet_getfunction(argv, 0); JanetFiber *fiber; @@ -345,7 +347,7 @@ static Janet cfun_new(int32_t argc, Janet *argv) { return janet_wrap_fiber(fiber); } -static Janet cfun_status(int32_t argc, Janet *argv) { +static Janet cfun_fiber_status(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetFiber *fiber = janet_getfiber(argv, 0); uint32_t s = (fiber->flags & JANET_FIBER_STATUS_MASK) >> @@ -353,19 +355,19 @@ static Janet cfun_status(int32_t argc, Janet *argv) { return janet_ckeywordv(janet_status_names[s]); } -static Janet cfun_current(int32_t argc, Janet *argv) { +static Janet cfun_fiber_current(int32_t argc, Janet *argv) { (void) argv; janet_fixarity(argc, 0); return janet_wrap_fiber(janet_vm_fiber); } -static Janet cfun_maxstack(int32_t argc, Janet *argv) { +static Janet cfun_fiber_maxstack(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetFiber *fiber = janet_getfiber(argv, 0); return janet_wrap_integer(fiber->maxstack); } -static Janet cfun_setmaxstack(int32_t argc, Janet *argv) { +static Janet cfun_fiber_setmaxstack(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetFiber *fiber = janet_getfiber(argv, 0); int32_t maxs = janet_getinteger(argv, 1); @@ -376,9 +378,9 @@ static Janet cfun_setmaxstack(int32_t argc, Janet *argv) { return argv[0]; } -static const JanetReg cfuns[] = { +static const JanetReg fiber_cfuns[] = { { - "fiber/new", cfun_new, + "fiber/new", cfun_fiber_new, JDOC("(fiber/new func [,sigmask])\n\n" "Create a new fiber with function body func. Can optionally " "take a set of signals to block from the current parent fiber " @@ -396,7 +398,7 @@ static const JanetReg cfuns[] = { "\t0-9 - block a specific user signal") }, { - "fiber/status", cfun_status, + "fiber/status", cfun_fiber_status, JDOC("(fiber/status fib)\n\n" "Get the status of a fiber. The status will be one of:\n\n" "\t:dead - the fiber has finished\n" @@ -408,19 +410,19 @@ static const JanetReg cfuns[] = { "\t:new - the fiber has just been created and not yet run") }, { - "fiber/current", cfun_current, + "fiber/current", cfun_fiber_current, JDOC("(fiber/current)\n\n" "Returns the currently running fiber.") }, { - "fiber/maxstack", cfun_maxstack, + "fiber/maxstack", cfun_fiber_maxstack, JDOC("(fiber/maxstack fib)\n\n" "Gets the maximum stack size in janet values allowed for a fiber. While memory for " "the fiber's stack is not allocated up front, the fiber will not allocated more " "than this amount and will throw a stack-overflow error if more memory is needed. ") }, { - "fiber/setmaxstack", cfun_setmaxstack, + "fiber/setmaxstack", cfun_fiber_setmaxstack, JDOC("(fiber/setmaxstack fib maxstack)\n\n" "Sets the maximum stack size in janet values for a fiber. By default, the " "maximum stack size is usually 8192.") @@ -430,5 +432,5 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_fiber(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, fiber_cfuns); } diff --git a/src/core/fiber.h b/src/core/fiber.h index 7b4daa8c..bc79f650 100644 --- a/src/core/fiber.h +++ b/src/core/fiber.h @@ -23,7 +23,9 @@ #ifndef JANET_FIBER_H_defined #define JANET_FIBER_H_defined +#ifndef JANET_AMALG #include +#endif extern JANET_THREAD_LOCAL JanetFiber *janet_vm_fiber; diff --git a/src/core/gc.c b/src/core/gc.c index 49c2592a..fef9855f 100644 --- a/src/core/gc.c +++ b/src/core/gc.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "state.h" #include "symcache.h" #include "gc.h" +#endif /* GC State */ JANET_THREAD_LOCAL void *janet_vm_blocks; diff --git a/src/core/gc.h b/src/core/gc.h index 501e64f3..db738c1c 100644 --- a/src/core/gc.h +++ b/src/core/gc.h @@ -23,7 +23,9 @@ #ifndef JANET_GC_H #define JANET_GC_H +#ifndef JANET_AMALG #include +#endif /* The metadata header associated with an allocated block of memory */ #define janet_gc_header(mem) ((JanetGCMemoryHeader *)(mem) - 1) @@ -36,7 +38,6 @@ #define janet_gc_type(m) (janet_gc_header(m)->flags & 0xFF) #define janet_gc_mark(m) (janet_gc_header(m)->flags |= JANET_MEM_REACHABLE) -#define janet_gc_unmark(m) (janet_gc_header(m)->flags &= ~JANET_MEM_COLOR) #define janet_gc_reachable(m) (janet_gc_header(m)->flags & JANET_MEM_REACHABLE) /* Memory header struct. Node of a linked list of memory blocks. */ diff --git a/src/core/io.c b/src/core/io.c index 221a3ce3..741d7760 100644 --- a/src/core/io.c +++ b/src/core/io.c @@ -25,9 +25,12 @@ #define _BSD_SOURCE #include -#include #include + +#ifndef JANET_AMALG +#include #include "util.h" +#endif #define IO_WRITE 1 #define IO_READ 2 @@ -45,11 +48,11 @@ struct IOFile { int flags; }; -static int janet_io_gc(void *p, size_t len); +static int cfun_io_gc(void *p, size_t len); -JanetAbstractType janet_io_filetype = { +JanetAbstractType cfun_io_filetype = { "core/file", - janet_io_gc, + cfun_io_gc, NULL }; @@ -93,7 +96,7 @@ static int checkflags(const uint8_t *str) { } static Janet makef(FILE *f, int flags) { - IOFile *iof = (IOFile *) janet_abstract(&janet_io_filetype, sizeof(IOFile)); + IOFile *iof = (IOFile *) janet_abstract(&cfun_io_filetype, sizeof(IOFile)); iof->file = f; iof->flags = flags; return janet_wrap_abstract(iof); @@ -101,14 +104,14 @@ static Janet makef(FILE *f, int flags) { /* Open a process */ #ifdef __EMSCRIPTEN__ -static Janet janet_io_popen(int32_t argc, Janet *argv) { +static Janet cfun_io_popen(int32_t argc, Janet *argv) { (void) argc; (void) argv; janet_panic("not implemented on this platform"); return janet_wrap_nil(); } #else -static Janet janet_io_popen(int32_t argc, Janet *argv) { +static Janet cfun_io_popen(int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); const uint8_t *fname = janet_getstring(argv, 0); const uint8_t *fmode = NULL; @@ -135,7 +138,7 @@ static Janet janet_io_popen(int32_t argc, Janet *argv) { } #endif -static Janet janet_io_fopen(int32_t argc, Janet *argv) { +static Janet cfun_io_fopen(int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); const uint8_t *fname = janet_getstring(argv, 0); const uint8_t *fmode; @@ -164,9 +167,9 @@ static void read_chunk(IOFile *iof, JanetBuffer *buffer, int32_t nBytesMax) { } /* Read a certain number of bytes into memory */ -static Janet janet_io_fread(int32_t argc, Janet *argv) { +static Janet cfun_io_fread(int32_t argc, Janet *argv) { janet_arity(argc, 2, 3); - IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype); + IOFile *iof = janet_getabstract(argv, 0, &cfun_io_filetype); if (iof->flags & IO_CLOSED) janet_panic("file is closed"); JanetBuffer *buffer; if (argc == 2) { @@ -210,9 +213,9 @@ static Janet janet_io_fread(int32_t argc, Janet *argv) { } /* Write bytes to a file */ -static Janet janet_io_fwrite(int32_t argc, Janet *argv) { +static Janet cfun_io_fwrite(int32_t argc, Janet *argv) { janet_arity(argc, 1, -1); - IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype); + IOFile *iof = janet_getabstract(argv, 0, &cfun_io_filetype); if (iof->flags & IO_CLOSED) janet_panic("file is closed"); if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE))) @@ -233,9 +236,9 @@ static Janet janet_io_fwrite(int32_t argc, Janet *argv) { } /* Flush the bytes in the file */ -static Janet janet_io_fflush(int32_t argc, Janet *argv) { +static Janet cfun_io_fflush(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); - IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype); + IOFile *iof = janet_getabstract(argv, 0, &cfun_io_filetype); if (iof->flags & IO_CLOSED) janet_panic("file is closed"); if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE))) @@ -246,7 +249,7 @@ static Janet janet_io_fflush(int32_t argc, Janet *argv) { } /* Cleanup a file */ -static int janet_io_gc(void *p, size_t len) { +static int cfun_io_gc(void *p, size_t len) { (void) len; IOFile *iof = (IOFile *)p; if (!(iof->flags & (IO_NOT_CLOSEABLE | IO_CLOSED))) { @@ -256,9 +259,9 @@ static int janet_io_gc(void *p, size_t len) { } /* Close a file */ -static Janet janet_io_fclose(int32_t argc, Janet *argv) { +static Janet cfun_io_fclose(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); - IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype); + IOFile *iof = janet_getabstract(argv, 0, &cfun_io_filetype); if (iof->flags & IO_CLOSED) janet_panic("file is closed"); if (iof->flags & (IO_NOT_CLOSEABLE)) @@ -276,9 +279,9 @@ static Janet janet_io_fclose(int32_t argc, Janet *argv) { } /* Seek a file */ -static Janet janet_io_fseek(int32_t argc, Janet *argv) { +static Janet cfun_io_fseek(int32_t argc, Janet *argv) { janet_arity(argc, 2, 3); - IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype); + IOFile *iof = janet_getabstract(argv, 0, &cfun_io_filetype); if (iof->flags & IO_CLOSED) janet_panic("file is closed"); long int offset = 0; @@ -302,9 +305,9 @@ static Janet janet_io_fseek(int32_t argc, Janet *argv) { return argv[0]; } -static const JanetReg cfuns[] = { +static const JanetReg io_cfuns[] = { { - "file/open", janet_io_fopen, + "file/open", cfun_io_fopen, JDOC("(file/open path [,mode])\n\n" "Open a file. path is an absolute or relative path, and " "mode is a set of flags indicating the mode to open the file in. " @@ -318,14 +321,14 @@ static const JanetReg cfuns[] = { "\t+ - append to the file instead of overwriting it") }, { - "file/close", janet_io_fclose, + "file/close", cfun_io_fclose, JDOC("(file/close f)\n\n" "Close a file and release all related resources. When you are " "done reading a file, close it to prevent a resource leak and let " "other processes read the file.") }, { - "file/read", janet_io_fread, + "file/read", cfun_io_fread, JDOC("(file/read f what [,buf])\n\n" "Read a number of bytes from a file into a buffer. A buffer can " "be provided as an optional fourth argument, otherwise a new buffer " @@ -337,19 +340,19 @@ static const JanetReg cfuns[] = { "\tn (integer) - read up to n bytes from the file") }, { - "file/write", janet_io_fwrite, + "file/write", cfun_io_fwrite, JDOC("(file/write f bytes)\n\n" "Writes to a file. 'bytes' must be string, buffer, or symbol. Returns the " "file.") }, { - "file/flush", janet_io_fflush, + "file/flush", cfun_io_fflush, JDOC("(file/flush f)\n\n" "Flush any buffered bytes to the file system. In most files, writes are " "buffered for efficiency reasons. Returns the file handle.") }, { - "file/seek", janet_io_fseek, + "file/seek", cfun_io_fseek, JDOC("(file/seek f [,whence [,n]])\n\n" "Jump to a relative location in the file. 'whence' must be one of\n\n" "\t:cur - jump relative to the current file location\n" @@ -360,7 +363,7 @@ static const JanetReg cfuns[] = { "number to handle large files of more the 4GB. Returns the file handle.") }, { - "file/popen", janet_io_popen, + "file/popen", cfun_io_popen, JDOC("(file/popen path [,mode])\n\n" "Open a file that is backed by a process. The file must be opened in either " "the :r (read) or the :w (write) mode. In :r mode, the stdout of the " @@ -372,7 +375,8 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_io(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, io_cfuns); + /* stdout */ janet_def(env, "stdout", makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE), diff --git a/src/core/marsh.c b/src/core/marsh.c index 92a0f9cd..39e659ff 100644 --- a/src/core/marsh.c +++ b/src/core/marsh.c @@ -20,13 +20,14 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include - #include "state.h" #include "vector.h" #include "gc.h" #include "fiber.h" #include "util.h" +#endif typedef struct { jmp_buf err; @@ -1144,7 +1145,7 @@ static Janet cfun_unmarshal(int32_t argc, Janet *argv) { return ret; } -static const JanetReg cfuns[] = { +static const JanetReg marsh_cfuns[] = { { "marshal", cfun_marshal, JDOC("(marshal x [,reverse-lookup [,buffer]])\n\n" @@ -1174,5 +1175,5 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_marsh(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, marsh_cfuns); } diff --git a/src/core/math.c b/src/core/math.c index d9ce0dd2..b49f9fbf 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -20,9 +20,12 @@ * IN THE SOFTWARE. */ -#include #include + +#ifndef JANET_AMALG +#include #include "util.h" +#endif /* Get a random number */ Janet janet_rand(int32_t argc, Janet *argv) { @@ -87,7 +90,7 @@ static Janet janet_not(int32_t argc, Janet *argv) { return janet_wrap_boolean(!janet_truthy(argv[0])); } -static const JanetReg cfuns[] = { +static const JanetReg math_cfuns[] = { { "%", janet_remainder, JDOC("(% dividend divisor)\n\n" @@ -178,7 +181,7 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_math(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, math_cfuns); #ifndef JANET_NO_BOOTSTRAP janet_def(env, "math/pi", janet_wrap_number(3.1415926535897931), JDOC("The value pi.")); diff --git a/src/core/os.c b/src/core/os.c index bc8bac5b..f85886b3 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -20,10 +20,13 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include +#include "util.h" +#endif + #include #include -#include "util.h" #ifdef JANET_WINDOWS #include @@ -296,7 +299,7 @@ static Janet os_date(int32_t argc, Janet *argv) { return janet_wrap_struct(janet_struct_end(st)); } -static const JanetReg cfuns[] = { +static const JanetReg os_cfuns[] = { { "os/which", os_which, JDOC("(os/which)\n\n" @@ -375,5 +378,5 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_os(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, os_cfuns); } diff --git a/src/core/parse.c b/src/core/parse.c index df988e8f..1dbfab56 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -20,8 +20,10 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "util.h" +#endif /* Check if a character is whitespace */ static int is_whitespace(uint8_t c) { @@ -614,7 +616,7 @@ static JanetAbstractType janet_parse_parsertype = { }; /* C Function parser */ -static Janet cfun_parser(int32_t argc, Janet *argv) { +static Janet cfun_parse_parser(int32_t argc, Janet *argv) { (void) argv; janet_fixarity(argc, 0); JanetParser *p = janet_abstract(&janet_parse_parsertype, sizeof(JanetParser)); @@ -622,7 +624,7 @@ static Janet cfun_parser(int32_t argc, Janet *argv) { return janet_wrap_abstract(p); } -static Janet cfun_consume(int32_t argc, Janet *argv) { +static Janet cfun_parse_consume(int32_t argc, Janet *argv) { janet_arity(argc, 2, 3); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); JanetByteView view = janet_getbytes(argv, 1); @@ -647,13 +649,13 @@ static Janet cfun_consume(int32_t argc, Janet *argv) { return janet_wrap_integer(i); } -static Janet cfun_has_more(int32_t argc, Janet *argv) { +static Janet cfun_parse_has_more(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); return janet_wrap_boolean(janet_parser_has_more(p)); } -static Janet cfun_byte(int32_t argc, Janet *argv) { +static Janet cfun_parse_byte(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); int32_t i = janet_getinteger(argv, 1); @@ -661,7 +663,7 @@ static Janet cfun_byte(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_status(int32_t argc, Janet *argv) { +static Janet cfun_parse_status(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); const char *stat = NULL; @@ -679,7 +681,7 @@ static Janet cfun_status(int32_t argc, Janet *argv) { return janet_ckeywordv(stat); } -static Janet cfun_error(int32_t argc, Janet *argv) { +static Janet cfun_parse_error(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); const char *err = janet_parser_error(p); @@ -687,26 +689,26 @@ static Janet cfun_error(int32_t argc, Janet *argv) { return janet_wrap_nil(); } -static Janet cfun_produce(int32_t argc, Janet *argv) { +static Janet cfun_parse_produce(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); return janet_parser_produce(p); } -static Janet cfun_flush(int32_t argc, Janet *argv) { +static Janet cfun_parse_flush(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); janet_parser_flush(p); return argv[0]; } -static Janet cfun_where(int32_t argc, Janet *argv) { +static Janet cfun_parse_where(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype); return janet_wrap_integer(p->offset); } -static Janet cfun_state(int32_t argc, Janet *argv) { +static Janet cfun_parse_state(int32_t argc, Janet *argv) { size_t i; const uint8_t *str; size_t oldcount; @@ -735,39 +737,39 @@ static Janet cfun_state(int32_t argc, Janet *argv) { return janet_wrap_string(str); } -static const JanetReg cfuns[] = { +static const JanetReg parse_cfuns[] = { { - "parser/new", cfun_parser, + "parser/new", cfun_parse_parser, JDOC("(parser/new)\n\n" "Creates and returns a new parser object. Parsers are state machines " "that can receive bytes, and generate a stream of janet values. ") }, { - "parser/has-more", cfun_has_more, + "parser/has-more", cfun_parse_has_more, JDOC("(parser/has-more parser)\n\n" "Check if the parser has more values in the value queue.") }, { - "parser/produce", cfun_produce, + "parser/produce", cfun_parse_produce, JDOC("(parser/produce parser)\n\n" "Dequeue the next value in the parse queue. Will return nil if " "no parsed values are in the queue, otherwise will dequeue the " "next value.") }, { - "parser/consume", cfun_consume, + "parser/consume", cfun_parse_consume, JDOC("(parser/consume parser bytes [, index])\n\n" "Input bytes into the parser and parse them. Will not throw errors " "if there is a parse error. Starts at the byte index given by index. Returns " "the number of bytes read.") }, { - "parser/byte", cfun_byte, + "parser/byte", cfun_parse_byte, JDOC("(parser/byte parser b)\n\n" "Input a single byte into the parser byte stream. Returns the parser.") }, { - "parser/error", cfun_error, + "parser/error", cfun_parse_error, JDOC("(parser/error parser)\n\n" "If the parser is in the error state, returns the message associated with " "that error. Otherwise, returns nil. Also flushes the parser state and parser " @@ -775,7 +777,7 @@ static const JanetReg cfuns[] = { "parser/error.") }, { - "parser/status", cfun_status, + "parser/status", cfun_parse_status, JDOC("(parser/status parser)\n\n" "Gets the current status of the parser state machine. The status will " "be one of:\n\n" @@ -784,14 +786,14 @@ static const JanetReg cfuns[] = { "\t:root - the parser can either read more values or safely terminate.") }, { - "parser/flush", cfun_flush, + "parser/flush", cfun_parse_flush, JDOC("(parser/flush parser)\n\n" "Clears the parser state and parse queue. Can be used to reset the parser " "if an error was encountered. Does not reset the line and column counter, so " "to begin parsing in a new context, create a new parser.") }, { - "parser/state", cfun_state, + "parser/state", cfun_parse_state, JDOC("(parser/state parser)\n\n" "Returns a string representation of the internal state of the parser. " "Each byte in the string represents a nested data structure. For example, " @@ -799,7 +801,7 @@ static const JanetReg cfuns[] = { "string inside of square brackets inside parentheses. Can be used to augment a REPL prompt.") }, { - "parser/where", cfun_where, + "parser/where", cfun_parse_where, JDOC("(parser/where parser)\n\n" "Returns the current line number and column number of the parser's location " "in the byte stream as a tuple (line, column). Lines and columns are counted from " @@ -810,5 +812,5 @@ static const JanetReg cfuns[] = { /* Load the library */ void janet_lib_parse(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, parse_cfuns); } diff --git a/src/core/peg.c b/src/core/peg.c index 097ea2f2..3f713e19 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -20,11 +20,13 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include #include "util.h" #include "vector.h" #include "util.h" +#endif /* * Runtime @@ -1043,13 +1045,13 @@ static Peg *compile_peg(Janet x) { * C Functions */ -static Janet cfun_compile(int32_t argc, Janet *argv) { +static Janet cfun_peg_compile(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); Peg *peg = compile_peg(argv[0]); return janet_wrap_abstract(peg); } -static Janet cfun_match(int32_t argc, Janet *argv) { +static Janet cfun_peg_match(int32_t argc, Janet *argv) { janet_arity(argc, 2, -1); Peg *peg; if (janet_checktype(argv[0], JANET_ABSTRACT) && @@ -1084,13 +1086,13 @@ static Janet cfun_match(int32_t argc, Janet *argv) { return result ? janet_wrap_array(s.captures) : janet_wrap_nil(); } -static const JanetReg cfuns[] = { - {"peg/compile", cfun_compile, +static const JanetReg peg_cfuns[] = { + {"peg/compile", cfun_peg_compile, JDOC("(peg/compile peg)\n\n" "Compiles a peg source data structure into a . This will speed up matching " "if the same peg will be used multiple times.") }, - {"peg/match", cfun_match, + {"peg/match", cfun_peg_match, JDOC("(peg/match peg text [,start=0])\n\n" "Match a Parsing Expression Grammar to a byte string and return an array of captured values. " "Returns nil if text does not match the language defined by peg. The syntax of PEGs are very " @@ -1101,5 +1103,5 @@ static const JanetReg cfuns[] = { /* Load the peg module */ void janet_lib_peg(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, peg_cfuns); } diff --git a/src/core/pp.c b/src/core/pp.c index 824ea056..5af5ce8c 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "util.h" #include "state.h" +#endif /* Implements a pretty printer for Janet. The pretty printer * is farily simple and not that flexible, but fast. */ diff --git a/src/core/regalloc.c b/src/core/regalloc.c index 648e0e10..0ac0dbb4 100644 --- a/src/core/regalloc.c +++ b/src/core/regalloc.c @@ -20,8 +20,10 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "regalloc.h" +#endif void janetc_regalloc_init(JanetcRegisterAllocator *ra) { ra->chunks = NULL; diff --git a/src/core/run.c b/src/core/run.c index 9595cf20..8f11d255 100644 --- a/src/core/run.c +++ b/src/core/run.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "state.h" #include "vector.h" +#endif /* Error reporting */ void janet_stacktrace(JanetFiber *fiber, const char *errtype, Janet err) { diff --git a/src/core/specials.c b/src/core/specials.c index db00e93f..a82d3236 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -20,11 +20,13 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "compile.h" #include "util.h" #include "vector.h" #include "emit.h" +#endif static JanetSlot janetc_quote(JanetFopts opts, int32_t argn, const Janet *argv) { if (argn != 1) { diff --git a/src/core/string.c b/src/core/string.c index 4243f958..452db7e1 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -20,11 +20,14 @@ * IN THE SOFTWARE. */ -#include #include + +#ifndef JANET_AMALG +#include #include "gc.h" #include "util.h" #include "state.h" +#endif /* Begin building a string */ uint8_t *janet_string_begin(int32_t length) { @@ -164,13 +167,13 @@ static int32_t kmp_next(struct kmp_state *state) { /* CFuns */ -static Janet cfun_slice(int32_t argc, Janet *argv) { +static Janet cfun_string_slice(int32_t argc, Janet *argv) { JanetRange range = janet_getslice(argc, argv); JanetByteView view = janet_getbytes(argv, 0); return janet_stringv(view.bytes + range.start, range.end - range.start); } -static Janet cfun_repeat(int32_t argc, Janet *argv) { +static Janet cfun_string_repeat(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetByteView view = janet_getbytes(argv, 0); int32_t rep = janet_getinteger(argv, 1); @@ -187,7 +190,7 @@ static Janet cfun_repeat(int32_t argc, Janet *argv) { return janet_wrap_string(janet_string_end(newbuf)); } -static Janet cfun_bytes(int32_t argc, Janet *argv) { +static Janet cfun_string_bytes(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetByteView view = janet_getbytes(argv, 0); Janet *tup = janet_tuple_begin(view.len); @@ -198,7 +201,7 @@ static Janet cfun_bytes(int32_t argc, Janet *argv) { return janet_wrap_tuple(janet_tuple_end(tup)); } -static Janet cfun_frombytes(int32_t argc, Janet *argv) { +static Janet cfun_string_frombytes(int32_t argc, Janet *argv) { int32_t i; uint8_t *buf = janet_string_begin(argc); for (i = 0; i < argc; i++) { @@ -208,7 +211,7 @@ static Janet cfun_frombytes(int32_t argc, Janet *argv) { return janet_wrap_string(janet_string_end(buf)); } -static Janet cfun_asciilower(int32_t argc, Janet *argv) { +static Janet cfun_string_asciilower(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetByteView view = janet_getbytes(argv, 0); uint8_t *buf = janet_string_begin(view.len); @@ -223,7 +226,7 @@ static Janet cfun_asciilower(int32_t argc, Janet *argv) { return janet_wrap_string(janet_string_end(buf)); } -static Janet cfun_asciiupper(int32_t argc, Janet *argv) { +static Janet cfun_string_asciiupper(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetByteView view = janet_getbytes(argv, 0); uint8_t *buf = janet_string_begin(view.len); @@ -238,7 +241,7 @@ static Janet cfun_asciiupper(int32_t argc, Janet *argv) { return janet_wrap_string(janet_string_end(buf)); } -static Janet cfun_reverse(int32_t argc, Janet *argv) { +static Janet cfun_string_reverse(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetByteView view = janet_getbytes(argv, 0); uint8_t *buf = janet_string_begin(view.len); @@ -262,7 +265,7 @@ static void findsetup(int32_t argc, Janet *argv, struct kmp_state *s, int32_t ex s->i = start; } -static Janet cfun_find(int32_t argc, Janet *argv) { +static Janet cfun_string_find(int32_t argc, Janet *argv) { int32_t result; struct kmp_state state; findsetup(argc, argv, &state, 0); @@ -273,7 +276,7 @@ static Janet cfun_find(int32_t argc, Janet *argv) { : janet_wrap_integer(result); } -static Janet cfun_findall(int32_t argc, Janet *argv) { +static Janet cfun_string_findall(int32_t argc, Janet *argv) { int32_t result; struct kmp_state state; findsetup(argc, argv, &state, 0); @@ -307,7 +310,7 @@ static void replacesetup(int32_t argc, Janet *argv, struct replace_state *s) { s->substlen = subst.len; } -static Janet cfun_replace(int32_t argc, Janet *argv) { +static Janet cfun_string_replace(int32_t argc, Janet *argv) { int32_t result; struct replace_state s; uint8_t *buf; @@ -327,7 +330,7 @@ static Janet cfun_replace(int32_t argc, Janet *argv) { return janet_wrap_string(janet_string_end(buf)); } -static Janet cfun_replaceall(int32_t argc, Janet *argv) { +static Janet cfun_string_replaceall(int32_t argc, Janet *argv) { int32_t result; struct replace_state s; JanetBuffer b; @@ -347,7 +350,7 @@ static Janet cfun_replaceall(int32_t argc, Janet *argv) { return janet_wrap_string(ret); } -static Janet cfun_split(int32_t argc, Janet *argv) { +static Janet cfun_string_split(int32_t argc, Janet *argv) { int32_t result; JanetArray *array; struct kmp_state state; @@ -370,7 +373,7 @@ static Janet cfun_split(int32_t argc, Janet *argv) { return janet_wrap_array(array); } -static Janet cfun_checkset(int32_t argc, Janet *argv) { +static Janet cfun_string_checkset(int32_t argc, Janet *argv) { uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0}; janet_arity(argc, 2, 3); JanetByteView set = janet_getbytes(argv, 0); @@ -398,7 +401,7 @@ static Janet cfun_checkset(int32_t argc, Janet *argv) { return janet_wrap_true(); } -static Janet cfun_join(int32_t argc, Janet *argv) { +static Janet cfun_string_join(int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); JanetView parts = janet_getindexed(argv, 0); JanetByteView joiner; @@ -451,7 +454,7 @@ static struct formatter { {"F", "%F", "%.*F"} }; -static Janet cfun_number(int32_t argc, Janet *argv) { +static Janet cfun_string_number(int32_t argc, Janet *argv) { janet_arity(argc, 1, 4); double x = janet_getnumber(argv, 0); struct formatter fmter = formatters[0]; @@ -486,7 +489,7 @@ static Janet cfun_number(int32_t argc, Janet *argv) { return janet_cstringv(buf); } -static Janet cfun_pretty(int32_t argc, Janet *argv) { +static Janet cfun_string_pretty(int32_t argc, Janet *argv) { janet_arity(argc, 1, 3); JanetBuffer *buffer = NULL; int32_t depth = 4; @@ -498,9 +501,9 @@ static Janet cfun_pretty(int32_t argc, Janet *argv) { return janet_wrap_buffer(buffer); } -static const JanetReg cfuns[] = { +static const JanetReg string_cfuns[] = { { - "string/slice", cfun_slice, + "string/slice", cfun_string_slice, JDOC("(string/slice bytes [,start=0 [,end=(length str)]])\n\n" "Returns a substring from a byte sequence. The substring is from " "index start inclusive to index end exclusive. All indexing " @@ -508,49 +511,49 @@ static const JanetReg cfuns[] = { "from the end of the string.") }, { - "string/repeat", cfun_repeat, + "string/repeat", cfun_string_repeat, JDOC("(string/repeat bytes n)\n\n" "Returns a string that is n copies of bytes concatenated.") }, { - "string/bytes", cfun_bytes, + "string/bytes", cfun_string_bytes, JDOC("(string/bytes str)\n\n" "Returns an array of integers that are the byte values of the string.") }, { - "string/from-bytes", cfun_frombytes, + "string/from-bytes", cfun_string_frombytes, JDOC("(string/from-bytes byte-array)\n\n" "Creates a string from an array of integers with byte values. All integers " "will be coerced to the range of 1 byte 0-255.") }, { - "string/ascii-lower", cfun_asciilower, + "string/ascii-lower", cfun_string_asciilower, JDOC("(string/ascii-lower str)\n\n" "Returns a new string where all bytes are replaced with the " "lowercase version of themselves in ASCII. Does only a very simple " "case check, meaning no unicode support.") }, { - "string/ascii-upper", cfun_asciiupper, + "string/ascii-upper", cfun_string_asciiupper, JDOC("(string/ascii-upper str)\n\n" "Returns a new string where all bytes are replaced with the " "uppercase version of themselves in ASCII. Does only a very simple " "case check, meaning no unicode support.") }, { - "string/reverse", cfun_reverse, + "string/reverse", cfun_string_reverse, JDOC("(string/reverse str)\n\n" "Returns a string that is the reversed version of str.") }, { - "string/find", cfun_find, + "string/find", cfun_string_find, JDOC("(string/find patt str)\n\n" "Searches for the first instance of pattern patt in string " "str. Returns the index of the first character in patt if found, " "otherwise returns nil.") }, { - "string/find-all", cfun_findall, + "string/find-all", cfun_string_findall, JDOC("(string/find patt str)\n\n" "Searches for all instances of pattern patt in string " "str. Returns an array of all indices of found patterns. Overlapping " @@ -559,38 +562,38 @@ static const JanetReg cfuns[] = { "occurrences are found, will return an empty array.") }, { - "string/replace", cfun_replace, + "string/replace", cfun_string_replace, JDOC("(string/replace patt subst str)\n\n" "Replace the first occurrence of patt with subst in the string str. " "Will return the new string if patt is found, otherwise returns str.") }, { - "string/replace-all", cfun_replaceall, + "string/replace-all", cfun_string_replaceall, JDOC("(string/replace-all patt subst str)\n\n" "Replace all instances of patt with subst in the string str. " "Will return the new string if patt is found, otherwise returns str.") }, { - "string/split", cfun_split, + "string/split", cfun_string_split, JDOC("(string/split delim str)\n\n" "Splits a string str with delimiter delim and returns an array of " "substrings. The substrings will not contain the delimiter delim. If delim " "is not found, the returned array will have one element.") }, { - "string/check-set", cfun_checkset, + "string/check-set", cfun_string_checkset, JDOC("(string/check-set set str)\n\n" "Checks if any of the bytes in the string set appear in the string str. " "Returns true if some bytes in set do appear in str, false if no bytes do.") }, { - "string/join", cfun_join, + "string/join", cfun_string_join, JDOC("(string/join parts [,sep])\n\n" "Joins an array of strings into one string, optionally separated by " "a separator string sep.") }, { - "string/number", cfun_number, + "string/number", cfun_string_number, JDOC("(string/number x [,format [,maxlen [,precision]]])\n\n" "Formats a number as string. The format parameter indicates how " "to display the number, either as floating point, scientific, or " @@ -606,7 +609,7 @@ static const JanetReg cfuns[] = { "Returns a string representation of x.") }, { - "string/pretty", cfun_pretty, + "string/pretty", cfun_string_pretty, JDOC("(string/pretty x [,depth=4 [,buffer=@\"\"]])\n\n" "Pretty prints a value to a buffer. Optionally allows setting max " "recursion depth, as well as writing to a buffer. Returns the buffer.") @@ -616,5 +619,5 @@ static const JanetReg cfuns[] = { /* Module entry point */ void janet_lib_string(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, string_cfuns); } diff --git a/src/core/strtod.c b/src/core/strtod.c index 15b2a292..cc2c21d6 100644 --- a/src/core/strtod.c +++ b/src/core/strtod.c @@ -44,10 +44,13 @@ * as it will not fit in the range for a signed 32 bit integer. The string * '0xbeef' would parse to an integer as it is in the range of an int32_t. */ -#include #include #include +#ifndef JANET_AMALG +#include +#endif + /* Lookup table for getting values of characters when parsing numbers. Handles * digits 0-9 and a-z (and A-Z). A-Z have values of 10 to 35. */ static uint8_t digit_lookup[128] = { diff --git a/src/core/struct.c b/src/core/struct.c index 6e6802d8..7481fab9 100644 --- a/src/core/struct.c +++ b/src/core/struct.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" #include "util.h" +#endif /* Begin creation of a struct */ JanetKV *janet_struct_begin(int32_t count) { @@ -218,5 +220,3 @@ int janet_struct_compare(const JanetKV *lhs, const JanetKV *rhs) { } return 0; } - -#undef janet_maphash diff --git a/src/core/symcache.c b/src/core/symcache.c index c63d2e9b..c605b623 100644 --- a/src/core/symcache.c +++ b/src/core/symcache.c @@ -25,10 +25,12 @@ * checks, all symbols are interned so that there is a single copy of it in the * whole program. Equality is then just a pointer check. */ +#ifndef JANET_AMALG #include #include "state.h" #include "gc.h" #include "util.h" +#endif /* Cache state */ JANET_THREAD_LOCAL const uint8_t **janet_vm_cache = NULL; diff --git a/src/core/symcache.h b/src/core/symcache.h index cf8faee8..0e729754 100644 --- a/src/core/symcache.h +++ b/src/core/symcache.h @@ -23,7 +23,9 @@ #ifndef JANET_SYMCACHE_H_defined #define JANET_SYMCACHE_H_defined +#ifndef JANET_AMALG #include +#endif /* Initialize the cache (allocate cache memory) */ void janet_symcache_init(void); diff --git a/src/core/table.c b/src/core/table.c index 171c8456..3e435c33 100644 --- a/src/core/table.c +++ b/src/core/table.c @@ -20,9 +20,11 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "gc.h" #include "util.h" +#endif /* Initialize a table */ JanetTable *janet_table_init(JanetTable *table, int32_t capacity) { @@ -194,13 +196,13 @@ void janet_table_merge_struct(JanetTable *table, const JanetKV *other) { /* C Functions */ -static Janet cfun_new(int32_t argc, Janet *argv) { +static Janet cfun_table_new(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); int32_t cap = janet_getinteger(argv, 0); return janet_wrap_table(janet_table(cap)); } -static Janet cfun_getproto(int32_t argc, Janet *argv) { +static Janet cfun_table_getproto(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetTable *t = janet_gettable(argv, 0); return t->proto @@ -208,7 +210,7 @@ static Janet cfun_getproto(int32_t argc, Janet *argv) { : janet_wrap_nil(); } -static Janet cfun_setproto(int32_t argc, Janet *argv) { +static Janet cfun_table_setproto(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetTable *table = janet_gettable(argv, 0); JanetTable *proto = NULL; @@ -219,21 +221,21 @@ static Janet cfun_setproto(int32_t argc, Janet *argv) { return argv[0]; } -static Janet cfun_tostruct(int32_t argc, Janet *argv) { +static Janet cfun_table_tostruct(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetTable *t = janet_gettable(argv, 0); return janet_wrap_struct(janet_table_to_struct(t)); } -static Janet cfun_rawget(int32_t argc, Janet *argv) { +static Janet cfun_table_rawget(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); JanetTable *table = janet_gettable(argv, 0); return janet_table_rawget(table, argv[1]); } -static const JanetReg cfuns[] = { +static const JanetReg table_cfuns[] = { { - "table/new", cfun_new, + "table/new", cfun_table_new, JDOC("(table/new capacity)\n\n" "Creates a new empty table with pre-allocated memory " "for capacity entries. This means that if one knows the number of " @@ -241,24 +243,24 @@ static const JanetReg cfuns[] = { "can be avoided. Returns the new table.") }, { - "table/to-struct", cfun_tostruct, + "table/to-struct", cfun_table_tostruct, JDOC("(table/to-struct tab)\n\n" "Convert a table to a struct. Returns a new struct. This function " "does not take into account prototype tables.") }, { - "table/getproto", cfun_getproto, + "table/getproto", cfun_table_getproto, JDOC("(table/getproto tab)\n\n" "Get the prototype table of a table. Returns nil if a table " "has no prototype, otherwise returns the prototype.") }, { - "table/setproto", cfun_setproto, + "table/setproto", cfun_table_setproto, JDOC("(table/setproto tab proto)\n\n" "Set the prototype of a table. Returns the original table tab.") }, { - "table/rawget", cfun_rawget, + "table/rawget", cfun_table_rawget, JDOC("(table/rawget tab key)\n\n" "Gets a value from a table without looking at the prototype table. " "If a table tab does not contain t directly, the function will return " @@ -269,5 +271,5 @@ static const JanetReg cfuns[] = { /* Load the table module */ void janet_lib_table(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, table_cfuns); } diff --git a/src/core/tuple.c b/src/core/tuple.c index 13c8bb14..2d7f1090 100644 --- a/src/core/tuple.c +++ b/src/core/tuple.c @@ -20,10 +20,12 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "symcache.h" #include "gc.h" #include "util.h" +#endif /* Create a new empty tuple of the given size. This will return memory * which should be filled with Janets. The memory will not be collected until @@ -91,13 +93,13 @@ int janet_tuple_compare(const Janet *lhs, const Janet *rhs) { /* C Functions */ -static Janet cfun_slice(int32_t argc, Janet *argv) { +static Janet cfun_tuple_slice(int32_t argc, Janet *argv) { JanetRange range = janet_getslice(argc, argv); JanetView view = janet_getindexed(argv, 0); return janet_wrap_tuple(janet_tuple_n(view.items + range.start, range.end - range.start)); } -static Janet cfun_prepend(int32_t argc, Janet *argv) { +static Janet cfun_tuple_prepend(int32_t argc, Janet *argv) { janet_arity(argc, 1, -1); JanetView view = janet_getindexed(argv, 0); Janet *n = janet_tuple_begin(view.len - 1 + argc); @@ -108,7 +110,7 @@ static Janet cfun_prepend(int32_t argc, Janet *argv) { return janet_wrap_tuple(janet_tuple_end(n)); } -static Janet cfun_append(int32_t argc, Janet *argv) { +static Janet cfun_tuple_append(int32_t argc, Janet *argv) { janet_arity(argc, 1, -1); JanetView view = janet_getindexed(argv, 0); Janet *n = janet_tuple_begin(view.len - 1 + argc); @@ -117,9 +119,9 @@ static Janet cfun_append(int32_t argc, Janet *argv) { return janet_wrap_tuple(janet_tuple_end(n)); } -static const JanetReg cfuns[] = { +static const JanetReg tuple_cfuns[] = { { - "tuple/slice", cfun_slice, + "tuple/slice", cfun_tuple_slice, JDOC("(tuple/slice arrtup [,start=0 [,end=(length arrtup)]])\n\n" "Take a sub sequence of an array or tuple from index start " "inclusive to index end exclusive. If start or end are not provided, " @@ -127,13 +129,13 @@ static const JanetReg cfuns[] = { "Returns the new tuple.") }, { - "tuple/append", cfun_append, + "tuple/append", cfun_tuple_append, JDOC("(tuple/append tup & items)\n\n" "Returns a new tuple that is the result of appending " "each element in items to tup.") }, { - "tuple/prepend", cfun_prepend, + "tuple/prepend", cfun_tuple_prepend, JDOC("(tuple/prepend tup & items)\n\n" "Prepends each element in items to tuple and " "returns a new tuple. Items are prepended such that the " @@ -144,5 +146,5 @@ static const JanetReg cfuns[] = { /* Load the tuple module */ void janet_lib_tuple(JanetTable *env) { - janet_cfuns(env, NULL, cfuns); + janet_cfuns(env, NULL, tuple_cfuns); } diff --git a/src/core/util.c b/src/core/util.c index 18372e0e..4264917e 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -20,11 +20,14 @@ * IN THE SOFTWARE. */ -#include #include + +#ifndef JANET_AMALG +#include #include "util.h" #include "state.h" #include "gc.h" +#endif /* Base 64 lookup table for digits */ const char janet_base64[65] = diff --git a/src/core/util.h b/src/core/util.h index 0dd059ef..c10586d7 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -23,7 +23,9 @@ #ifndef JANET_UTIL_H_defined #define JANET_UTIL_H_defined +#ifndef JANET_AMALG #include +#endif /* Omit docstrings in some builds */ #ifdef JANET_NO_BOOTSTRAP diff --git a/src/core/value.c b/src/core/value.c index 8d961c3a..dbdd60dc 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -20,7 +20,9 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include +#endif /* * Define a number of functions that can be used internally on ANY Janet. diff --git a/src/core/vector.c b/src/core/vector.c index 6cead975..423de6f0 100644 --- a/src/core/vector.c +++ b/src/core/vector.c @@ -20,7 +20,9 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include "vector.h" +#endif /* Grow the buffer dynamically. Used for push operations. */ void *janet_v_grow(void *v, int32_t increment, int32_t itemsize) { diff --git a/src/core/vector.h b/src/core/vector.h index 169c3a48..0852e113 100644 --- a/src/core/vector.h +++ b/src/core/vector.h @@ -23,7 +23,9 @@ #ifndef JANET_VECTOR_H_defined #define JANET_VECTOR_H_defined +#ifndef JANET_AMALG #include +#endif /* * vector code modified from diff --git a/src/core/vm.c b/src/core/vm.c index 400e6e0c..9857caff 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -20,12 +20,14 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include #include "state.h" #include "fiber.h" #include "gc.h" #include "symcache.h" #include "util.h" +#endif /* VM state */ JANET_THREAD_LOCAL JanetTable *janet_vm_registry; diff --git a/src/core/wrap.c b/src/core/wrap.c index 705879c1..a1506815 100644 --- a/src/core/wrap.c +++ b/src/core/wrap.c @@ -20,7 +20,9 @@ * IN THE SOFTWARE. */ +#ifndef JANET_AMALG #include +#endif void *janet_memalloc_empty(int32_t count) { int32_t i; diff --git a/tools/amalg.janet b/tools/amalg.janet new file mode 100644 index 00000000..8d3eb16a --- /dev/null +++ b/tools/amalg.janet @@ -0,0 +1,65 @@ +# Creates an amalgamated janet.c and janet.h to +# allow for easy embedding + +(def {:year YY :month MM :month-day DD} (os/date)) + +# Order is important here, as some headers +# depend on other headers. +(def headers + @["src/core/util.h" + "src/core/state.h" + "src/core/gc.h" + "src/core/vector.h" + "src/core/fiber.h" + "src/core/regalloc.h" + "src/core/compile.h" + "src/core/emit.h" + "src/core/symcache.h"]) + +(def sources + @["src/core/abstract.c" + "src/core/array.c" + "src/core/asm.c" + "src/core/buffer.c" + "src/core/bytecode.c" + "src/core/capi.c" + "src/core/cfuns.c" + "src/core/compile.c" + "src/core/corelib.c" + "src/core/debug.c" + "src/core/emit.c" + "src/core/fiber.c" + "src/core/gc.c" + "src/core/io.c" + "src/core/marsh.c" + "src/core/math.c" + "src/core/os.c" + "src/core/parse.c" + "src/core/peg.c" + "src/core/pp.c" + "src/core/regalloc.c" + "src/core/run.c" + "src/core/specials.c" + "src/core/string.c" + "src/core/strtod.c" + "src/core/struct.c" + "src/core/symcache.c" + "src/core/table.c" + "src/core/tuple.c" + "src/core/util.c" + "src/core/value.c" + "src/core/vector.c" + "src/core/vm.c" + "src/core/wrap.c"]) + +(print "/* Amalgamated build - DO NOT EDIT */") +(print "/* Generated " YY "-" (inc MM) "-" (inc DD) + " with janet version " janet/version "-" janet/build " */") +(print ```#define JANET_AMALG +#include "janet.h"```) + +(each h headers (print (slurp h))) +(each s sources (print (slurp s))) + +# Relies on this file being built +(print (slurp "build/core.gen.c")) diff --git a/tools/xxd.c b/tools/xxd.c index 90d700f3..606b1f9a 100644 --- a/tools/xxd.c +++ b/tools/xxd.c @@ -60,7 +60,7 @@ int main(int argc, const char **argv) { /* Write the header */ fprintf(out, "/* Auto generated - DO NOT EDIT */\n\n#include \n\n"); - fprintf(out, "static const unsigned char bytes[] = {"); + fprintf(out, "static const unsigned char bytes_%s[] = {", argv[3]); /* Read in chunks from buffer */ while ((bytesRead = fread(buf, 1, sizeof(buf), in)) > 0) { @@ -91,7 +91,7 @@ int main(int argc, const char **argv) { /* Write the tail */ fputs("\n};\n\n", out); - fprintf(out, "const unsigned char *%s = bytes;\n\n", argv[3]); + fprintf(out, "const unsigned char *%s = bytes_%s;\n\n", argv[3], argv[3]); /* Write chunk size */ fprintf(out, "int32_t %s_size = %d;\n", argv[3], totalRead);