From 0d7e4db6614688cc9193e130fbecad1f6883d448 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 6 Feb 2023 04:47:30 +0000 Subject: [PATCH] Fixed repl bug --- repl/repl_main.c | 3 +++ repl/repl_tools.c | 3 +++ scripts/fib-memo.toy | 8 ++++---- scripts/fib.toy | 7 ++----- scripts/rule110.toy | 4 ++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/repl/repl_main.c b/repl/repl_main.c index 8013c96..8104392 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -152,6 +152,9 @@ int main(int argc, const char* argv[]) { if (Toy_commandLine.compilefile && Toy_commandLine.outfile) { size_t size = 0; char* source = Toy_readFile(Toy_commandLine.compilefile, &size); + if (!source) { + return 1; + } unsigned char* tb = Toy_compileString(source, &size); if (!tb) { return 1; diff --git a/repl/repl_tools.c b/repl/repl_tools.c index 1b8269d..cce3bfc 100644 --- a/repl/repl_tools.c +++ b/repl/repl_tools.c @@ -145,6 +145,9 @@ void Toy_runSource(char* source) { void Toy_runSourceFile(char* fname) { size_t size = 0; //not used char* source = Toy_readFile(fname, &size); + if (!source) { + return; + } Toy_runSource(source); free((void*)source); } diff --git a/scripts/fib-memo.toy b/scripts/fib-memo.toy index 5749eed..b41a5ae 100644 --- a/scripts/fib-memo.toy +++ b/scripts/fib-memo.toy @@ -2,9 +2,9 @@ var memo: [int : int] = [:]; fn fib(n : int) { - if (n < 2) { - return n; - } + if (n < 2) { + return n; + } var result = memo[n]; if (result == null) { @@ -12,7 +12,7 @@ fn fib(n : int) { memo[n] = result; } - return result; + return result; } for (var i = 0; i < 40; i++) { diff --git a/scripts/fib.toy b/scripts/fib.toy index 0c3e342..ef59c88 100644 --- a/scripts/fib.toy +++ b/scripts/fib.toy @@ -1,9 +1,6 @@ fn fib(n : int) { - if (n < 2) { - return n; - } - - return fib(n-1) + fib(n-2); + if (n < 2) return n; + return fib(n-1) + fib(n-2); } for (var i = 0; i < 20; i++) { diff --git a/scripts/rule110.toy b/scripts/rule110.toy index c8b18cc..d7e8419 100644 --- a/scripts/rule110.toy +++ b/scripts/rule110.toy @@ -1,5 +1,5 @@ //number of iterations -var SIZE: int const = 100; +var SIZE: int const = 260; //lookup table var lookup = [ @@ -32,7 +32,7 @@ prev += "*"; //initial print prev; //run -for (var iteration = 0; iteration < 100; iteration++) { +for (var iteration = 0; iteration < SIZE -1; iteration++) { //left var output = (lookup[" "][prev[0]][prev[1]]);