mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Functions are working, tests incomplete
This required a massive cross-cutting rework to the scope system, multiple subtle bugfixes and relearning of the parser internals, but it does appear that functions are working correctly. A few caveats: for now, parameters are always constant, regardless of type, return values can't be specified, and some script tests have been written. Most importantly, a key feature is working: closures.
This commit is contained in:
22
tests/scripts/test_closures.toy
Normal file
22
tests/scripts/test_closures.toy
Normal file
@@ -0,0 +1,22 @@
|
||||
//closures
|
||||
fn makeCounter() {
|
||||
var counter: int = 0;
|
||||
|
||||
fn increment() {
|
||||
return ++counter;
|
||||
}
|
||||
|
||||
return increment;
|
||||
}
|
||||
|
||||
var tally = makeCounter();
|
||||
|
||||
while (true) {
|
||||
var result = tally();
|
||||
|
||||
print result;
|
||||
|
||||
if (result >= 10) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
20
tests/scripts/test_fibonacci.toy
Normal file
20
tests/scripts/test_fibonacci.toy
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
fn fib(n) {
|
||||
if (n < 2) return n;
|
||||
return fib(n-1) + fib(n-2);
|
||||
}
|
||||
|
||||
assert fib(1) == 1;
|
||||
assert fib(2) == 1;
|
||||
assert fib(3) == 2;
|
||||
assert fib(4) == 3;
|
||||
assert fib(5) == 5;
|
||||
assert fib(6) == 8;
|
||||
assert fib(7) == 13;
|
||||
assert fib(8) == 21;
|
||||
assert fib(9) == 34;
|
||||
assert fib(10) == 55;
|
||||
assert fib(11) == 89;
|
||||
assert fib(12) == 144;
|
||||
|
||||
print "Fibonacci passed";
|
||||
9
tests/units/test_function.c
Normal file
9
tests/units/test_function.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void) {
|
||||
printf(TOY_CC_WARN "Test not yet implemented: %s\n" TOY_CC_RESET, __FILE__);
|
||||
return -1;
|
||||
}
|
||||
@@ -41,7 +41,7 @@ int test_setup_and_teardown(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//check the bytecode was loaded correctly
|
||||
if (
|
||||
@@ -86,7 +86,7 @@ int test_simple_execution(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -132,7 +132,7 @@ int test_opcode_not_equal(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -188,7 +188,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -232,7 +232,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -277,7 +277,7 @@ int test_keyword_assert(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -326,7 +326,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -371,7 +371,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -416,7 +416,7 @@ int test_keyword_print(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -465,7 +465,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -510,7 +510,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -554,7 +554,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -599,7 +599,7 @@ int test_keyword_ifThenElse(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -647,7 +647,7 @@ int test_scope(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -695,7 +695,7 @@ int test_scope(Toy_Bucket** bucketHandle) {
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bytecode, false);
|
||||
Toy_bindVM(&vm, bytecode, NULL);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
@@ -737,7 +737,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
|
||||
|
||||
//run 1
|
||||
unsigned char* bytecode1 = makeCodeFromSource(bucketHandle, "print \"Hello world!\";");
|
||||
Toy_bindVM(&vm, bytecode1, false);
|
||||
Toy_bindVM(&vm, bytecode1, NULL);
|
||||
|
||||
Toy_runVM(&vm);
|
||||
Toy_resetVM(&vm, true);
|
||||
@@ -757,7 +757,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
|
||||
|
||||
//run 2
|
||||
unsigned char* bytecode2 = makeCodeFromSource(bucketHandle, "print \"Hello world!\";");
|
||||
Toy_bindVM(&vm, bytecode2, true); //preserve during repeated calls
|
||||
Toy_bindVM(&vm, bytecode2, NULL); //preserve during repeated calls
|
||||
|
||||
Toy_runVM(&vm);
|
||||
Toy_resetVM(&vm, true);
|
||||
@@ -777,7 +777,7 @@ int test_vm_reuse(Toy_Bucket** bucketHandle) {
|
||||
|
||||
//run 3
|
||||
unsigned char* bytecode3 = makeCodeFromSource(bucketHandle, "print \"Hello world!\";");
|
||||
Toy_bindVM(&vm, bytecode3, true); //preserve during repeated calls
|
||||
Toy_bindVM(&vm, bytecode3, NULL); //preserve during repeated calls
|
||||
|
||||
Toy_runVM(&vm);
|
||||
Toy_resetVM(&vm, true);
|
||||
|
||||
Reference in New Issue
Block a user