diff --git a/examples/fibtest.lua b/examples/fibtest.lua new file mode 100644 index 0000000..58fe4f8 --- /dev/null +++ b/examples/fibtest.lua @@ -0,0 +1,11 @@ +local function fib(num) + if num <= 1 then + return num + else + return fib(num-2) + fib(num-1) + end +end + +for (var i = 1; i < 40; i++) do + print("The fib number of " .. i .. " is " .. fib(i)) +end diff --git a/examples/fortest.lua b/examples/fortest.lua deleted file mode 100644 index afcf7d1..0000000 --- a/examples/fortest.lua +++ /dev/null @@ -1,13 +0,0 @@ -local function fact(num) - var total = 1 - for (var i = num; i > 0; i = i - 1) do - total = total * i - end - return total -end - -for (var x = 0; x < 1000; x=x+1) do - for (var z = 0; z < 100; z=z+1) do - print("The factorial of " .. x .. "." .. z .. " is " .. fact(z)) - end -end \ No newline at end of file diff --git a/src/cmem.c b/src/cmem.c index 01ef61b..3711274 100644 --- a/src/cmem.c +++ b/src/cmem.c @@ -6,9 +6,7 @@ #include "cobj.h" #include "cbaselib.h" -/* - copy buffer to new larger buffer, and free the old buffer -*/ +// realloc wrapper void *cosmoM_reallocate(CState* state, void *buf, size_t oldSize, size_t newSize) { state->allocatedBytes += newSize - oldSize; @@ -30,7 +28,7 @@ void *cosmoM_reallocate(CState* state, void *buf, size_t oldSize, size_t newSize cosmoM_checkGarbage(state, 0); #endif - // otherwise just use realloc to do all the heavy lifting + // if NULL is passed, realloc() acts like malloc() void *newBuf = realloc(buf, newSize); if (newBuf == NULL) { @@ -247,7 +245,7 @@ COSMO_API void cosmoM_collectGarbage(CState *state) { // set our next GC event cosmoM_updateThreshhold(state); - state->freezeGC--; // we don't want to use cosmoM_unfreezeGC because that might trigger a GC event + state->freezeGC--; // we don't want to use cosmoM_unfreezeGC because that might trigger a GC event (if GC_STRESS is defined) #ifdef GC_DEBUG printf("-- GC end, reclaimed %ld bytes (started at %ld, ended at %ld), next garbage collection scheduled at %ld bytes\n", start - state->allocatedBytes, start, state->allocatedBytes, state->nextGC); diff --git a/src/cparse.c b/src/cparse.c index a8c271a..fea7105 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -360,7 +360,7 @@ static void alignStack(CParseState *pstate, int alignment) { pstate->compiler->pushedValues = alignment; } -// ================================================================ [PRATT'S PARSER] ================================================================ +// ================================================================ [PARSER] ================================================================ static void number(CParseState *pstate, bool canAssign) { cosmo_Number num = strtod(pstate->previous.start, NULL); diff --git a/src/cvalue.h b/src/cvalue.h index 7d6b325..aad5dc8 100644 --- a/src/cvalue.h +++ b/src/cvalue.h @@ -25,8 +25,6 @@ typedef double cosmo_Number; both are great resources :) - Performance notes: this can actually degrade performance, so only enable if you know what you're doing. - TL;DR: we can store payloads in the NaN value in the IEEE 754 standard. */ typedef union CValue { @@ -34,28 +32,28 @@ typedef union CValue { cosmo_Number num; } CValue; -#define MASK_TYPE ((uint64_t)0x7) -#define MASK_PAYLOAD ((uint64_t)0x0007fffffffffff8) +#define MASK_TYPE ((uint64_t)0x0007000000000000) +#define MASK_PAYLOAD ((uint64_t)0x0000ffffffffffff) // 3 bits (low bits) are reserved for the type -#define MAKE_PAYLOAD(x) (((uint64_t)(x) << 3) & MASK_PAYLOAD) -#define READ_PAYLOAD(x) (((x).data & MASK_PAYLOAD) >> 3) +#define MAKE_PAYLOAD(x) ((uint64_t)(x) & MASK_PAYLOAD) +#define READ_PAYLOAD(x) ((x).data & MASK_PAYLOAD) // The bits that must be set to indicate a quiet NaN. #define MASK_QUIETNAN ((uint64_t)0x7ff8000000000000) #define GET_TYPE(x) \ - ((((x).data & MASK_QUIETNAN) == MASK_QUIETNAN) ? ((x).data & MASK_TYPE) : COSMO_TNUMBER) + ((((x).data & MASK_QUIETNAN) == MASK_QUIETNAN) ? (((x).data & MASK_TYPE) >> 48) : COSMO_TNUMBER) -#define SIG_MASK (MASK_QUIETNAN | MASK_TYPE) -#define BOOL_SIG (MASK_QUIETNAN | COSMO_TBOOLEAN) -#define OBJ_SIG (MASK_QUIETNAN | COSMO_TOBJ) -#define NIL_SIG (MASK_QUIETNAN | COSMO_TNIL) +static const uint64_t SIG_MASK = (MASK_QUIETNAN | MASK_TYPE); +static const uint64_t BOOL_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TBOOLEAN) << 48)); +static const uint64_t OBJ_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TOBJ) << 48)); +static const uint64_t NIL_SIG = (MASK_QUIETNAN | ((uint64_t)(COSMO_TNIL) << 48)); #define cosmoV_newNumber(x) ((CValue){.num = x}) -#define cosmoV_newObj(x) ((CValue){.data = MASK_QUIETNAN | MAKE_PAYLOAD((uintptr_t)x) | COSMO_TOBJ}) -#define cosmoV_newBoolean(x) ((CValue){.data = MASK_QUIETNAN | MAKE_PAYLOAD(x) | COSMO_TBOOLEAN}) -#define cosmoV_newNil() ((CValue){.data = MASK_QUIETNAN | COSMO_TNIL}) +#define cosmoV_newBoolean(x) ((CValue){.data = MAKE_PAYLOAD(x) | BOOL_SIG}) +#define cosmoV_newObj(x) ((CValue){.data = MAKE_PAYLOAD((uintptr_t)x) | OBJ_SIG}) +#define cosmoV_newNil() ((CValue){.data = NIL_SIG}) #define cosmoV_readNumber(x) ((x).num) #define cosmoV_readBoolean(x) ((bool)READ_PAYLOAD(x))