diff --git a/scripts/advent_1a.toy b/scripts/advent_1a.toy deleted file mode 100644 index 3ea4e40..0000000 --- a/scripts/advent_1a.toy +++ /dev/null @@ -1,29 +0,0 @@ -//TODO: Not yet functional - -//advent of code thingy -var arr = [ - 3, 4, - 4, 3, - 2, 5, - 1, 3, - 3, 9, - 3, 3, -]; - -var total = 0; -var counter = 0; - -while (counter < arr.length) { - var difference = arr[counter] - arr[counter+1]; - - if (difference < 0) { - difference = -difference; - } - - total += difference; - - counter += 2; -} - -print difference; - diff --git a/scripts/benchpress.toy b/scripts/benchpress.toy deleted file mode 100644 index 619c6d8..0000000 --- a/scripts/benchpress.toy +++ /dev/null @@ -1,17 +0,0 @@ -//calculate the nth fibonacci number, and print it - -var counter: int = 0; - -var first: int = 1; -var second: int = 0; - -//This causes integer overflow, but that's fine for now -while (counter < 100_000) { - var third: int = first + second; - first = second; - second = third; - - print third; - - ++counter; -} \ No newline at end of file diff --git a/scripts/breakdancing.toy b/scripts/breakdancing.toy deleted file mode 100644 index c4db44b..0000000 --- a/scripts/breakdancing.toy +++ /dev/null @@ -1,100 +0,0 @@ -//make sure it works with multiple repititions - -//------------------------- - -//test break -while (true) { - break; - assert false, "break failed"; -} - -//test continue -var flag1: bool = true; -while (flag1) { - flag1 = false; - continue; - assert false, "continue failed"; -} - -print "done"; - -//------------------------- - -//test break -while (true) { - break; - assert false, "break failed"; -} - -//test continue -var flag2: bool = true; -while (flag2) { - flag2 = false; - continue; - assert false, "continue failed"; -} - -print "done"; - -//------------------------- - -//test break -while (true) { - break; - assert false, "break failed"; -} - -//test continue -var flag3: bool = true; -while (flag3) { - flag3 = false; - continue; - assert false, "continue failed"; -} - -print "done"; - -//------------------------- - -{ - //test break - while (true) { - break; - assert false, "break failed"; - } - - //test continue - var flag4: bool = true; - while (flag4) { - flag4 = false; - continue; - assert false, "continue failed"; - } - - print "done"; -} - -//------------------------- - -{ - //test break - while (true) { - { - break; - } - assert false, "break failed"; - } - - //test continue - var flag5: bool = true; - while (flag5) { - flag5 = false; - { - continue; - } - assert false, "continue failed"; - } - - print "done"; -} - diff --git a/scripts/fib.toy b/scripts/fib.toy deleted file mode 100644 index 96c93f1..0000000 --- a/scripts/fib.toy +++ /dev/null @@ -1,15 +0,0 @@ -//TODO: Not yet functional - -//example of the fibonacci sequence -fn fib(n: int) { - if (n < 2) return n; - return fib(n-1) + fib(n-2); -} - -//TODO: type coercion syntax hasn't been decided on yet, but it will be needed -for (var i = 1; i <= 10; i++) { - print i .. ":" .. fib(i); -} - -//Note to my future self: yes, the base case in 'fib()' is 'n < 2', stop second guessing yourself! -//Note to my past self: don't tell me what to do! \ No newline at end of file diff --git a/scripts/fizzbuzz.toy b/scripts/fizzbuzz.toy deleted file mode 100644 index 3441d17..0000000 --- a/scripts/fizzbuzz.toy +++ /dev/null @@ -1,24 +0,0 @@ -//standard example, using 'while' instead of 'for', because it's not ready yet - -var counter: int = 0; - -while (++counter <= 100) { - var result: string = ""; - - if (counter % 3 == 0) { - result = result .. "fizz"; - } - - if (counter % 5 == 0) { - result = result .. "buzz"; - } - - //finally - if (result != "") { - print result; - } - else { - print counter; - } -} - diff --git a/scripts/funky.toy b/scripts/funky.toy deleted file mode 100644 index 0786824..0000000 --- a/scripts/funky.toy +++ /dev/null @@ -1,20 +0,0 @@ -//TODO: functions are untested -fn makeCounter() { - var counter: int = 0; - - fn increment() { - return ++counter; - } - - return increment; -} - -var tally = makeCounter(); - -while (true) { - var result = tally(); - - if (result >= 10_000_000) { - break; - } -} diff --git a/scripts/leapyear.toy b/scripts/leapyear.toy deleted file mode 100644 index 812f831..0000000 --- a/scripts/leapyear.toy +++ /dev/null @@ -1,8 +0,0 @@ -//TODO: Not yet functional - -//find the leap years -fn isLeapYear(n: int) { - if (n % 400 == 0) return true; - if (n % 100 == 0) return false; - return n % 4 == 0; -} diff --git a/source/toy_scope.c b/source/toy_scope.c index 900ecff..e78d01f 100644 --- a/source/toy_scope.c +++ b/source/toy_scope.c @@ -26,8 +26,10 @@ static void decrementRefCount(Toy_Scope* scope) { if (iter->refCount == 0 && iter->data != NULL) { //free the scope entries when this scope is no longer needed for (unsigned int i = 0; i < iter->capacity; i++) { - Toy_freeString(&(iter->data[i].key)); - Toy_freeValue(iter->data[i].value); + if (iter->data[i].psl > 0) { + Toy_freeString(&(iter->data[i].key)); + Toy_freeValue(iter->data[i].value); + } } free(iter->data); } @@ -62,7 +64,7 @@ static Toy_ScopeEntry* lookupScopeEntryPtr(Toy_Scope* scope, Toy_String* key, un void probeAndInsert(Toy_Scope* scope, Toy_String* key, Toy_Value value, Toy_ValueType type, bool constant) { //make the entry unsigned int probe = Toy_hashString(key) % scope->capacity; - Toy_ScopeEntry entry = (Toy_ScopeEntry){ .key = *key, .value = value, .type = type, .constant = constant, .psl = 0 }; + Toy_ScopeEntry entry = (Toy_ScopeEntry){ .key = *key, .value = value, .type = type, .constant = constant, .psl = 1 }; //probe while (true) { @@ -134,8 +136,11 @@ Toy_Scope* Toy_pushScope(Toy_Bucket** bucketHandle, Toy_Scope* scope) { Toy_Scope* newScope = (Toy_Scope*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Scope)); newScope->next = scope; - newScope->data = adjustScopeEntries(NULL, TOY_SCOPE_INITIAL_CAPACITY); newScope->refCount = 0; + newScope->data = adjustScopeEntries(NULL, TOY_SCOPE_INITIAL_CAPACITY); + newScope->capacity = TOY_SCOPE_INITIAL_CAPACITY; + newScope->count = 0; + newScope->maxPsl = 0; incrementRefCount(newScope); diff --git a/source/toy_scope.h b/source/toy_scope.h index fee59c1..cf55fe0 100644 --- a/source/toy_scope.h +++ b/source/toy_scope.h @@ -11,18 +11,18 @@ typedef struct Toy_ScopeEntry { Toy_String key; Toy_Value value; Toy_ValueType type; + unsigned int psl; //psl '0' means empty bool constant; - unsigned int psl; } Toy_ScopeEntry; //holds a table-like collection of variables TODO: check bitness typedef struct Toy_Scope { struct Toy_Scope* next; unsigned int refCount; + Toy_ScopeEntry* data; unsigned int capacity; unsigned int count; unsigned int maxPsl; - Toy_ScopeEntry* data; } Toy_Scope; //handle deep scopes - the scope is stored in the bucket, not the table diff --git a/source/toy_string.c b/source/toy_string.c index c324b37..8fdef6e 100644 --- a/source/toy_string.c +++ b/source/toy_string.c @@ -67,6 +67,7 @@ Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_S } void Toy_freeString(Toy_String* str) { + assert(str->info.refCount > 0 && "Can't free a string with refcount 0"); //memory is freed when the bucket is decrementRefCount(str); }