From 584fb115b68dcf2b2c01e867cd8fcd963d387c95 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 28 Nov 2022 16:48:45 +0000 Subject: [PATCH] Fixed the awful rule110 implementation --- repl/lib_standard.c | 3 +- scripts/rule110.toy | 97 ++++++++++++++++++-------------------------- source/interpreter.c | 3 +- source/toy_common.h | 1 + 4 files changed, 44 insertions(+), 60 deletions(-) diff --git a/repl/lib_standard.c b/repl/lib_standard.c index 52f339e..3224e37 100644 --- a/repl/lib_standard.c +++ b/repl/lib_standard.c @@ -1,9 +1,8 @@ #include "lib_standard.h" +#include "toy_common.h" #include "memory.h" -#include - static int nativeClock(Interpreter* interpreter, LiteralArray* arguments) { //no arguments if (arguments->count != 0) { diff --git a/scripts/rule110.toy b/scripts/rule110.toy index 5977122..c8b18cc 100644 --- a/scripts/rule110.toy +++ b/scripts/rule110.toy @@ -1,66 +1,49 @@ -var size: int const = 100; +//number of iterations +var SIZE: int const = 100; -var prev = []; -for (var i = 0; i < size; i++) { - prev.push(false); +//lookup table +var lookup = [ + "*": [ + "*": [ + "*": " ", + " ": "*" + ], + " ": [ + "*": "*", + " ": " " + ] +], " ": [ + "*": [ + "*": "*", + " ": "*" + ], + " ": [ + "*": "*", + " ": " " + ] +]]; + +//initial line to build from +var prev: string = ""; +for (var i = 0; i < SIZE -1; i++) { + prev += " "; } +prev += "*"; //initial +print prev; -prev.set(size - 1, true); - - -fn calc(p, i) { - if (p[i-1] && p[i] && p[i+1]) { - return false; - } - - if (p[i-1] && p[i] && !p[i+1]) { - return true; - } - - if (p[i-1] && !p[i] && p[i+1]) { - return true; - } - - if (p[i-1] && !p[i] && !p[i+1]) { - return false; - } - - if (!p[i-1] && p[i] && p[i+1]) { - return true; - } - - if (!p[i-1] && p[i] && !p[i+1]) { - return true; - } - - if (!p[i-1] && !p[i] && p[i+1]) { - return true; - } - - if (!p[i-1] && !p[i] && !p[i+1]) { - return false; - } -} - - - +//run for (var iteration = 0; iteration < 100; iteration++) { - var line = [false]; - for (var i = 1; i < size-1; i++) { - line.push(calc(prev, i)); - } - line.push(false); + //left + var output = (lookup[" "][prev[0]][prev[1]]); - var output = ""; - for (var i = 0; i < line.length(); i++) { - if (line[i]) { - output += "*"; - } - else { - output += " "; - } + //middle + for (var i = 1; i < SIZE-1; i++) { + output += (lookup[prev[i-1]][prev[i]][prev[i+1]]); } + //right + output += (lookup[prev[SIZE-2]][prev[SIZE-1]][" "]); + print output; - prev = line; + prev = output; } diff --git a/source/interpreter.c b/source/interpreter.c index 1ca1425..50ab415 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -1592,7 +1592,8 @@ static bool execIndex(Interpreter* interpreter, bool assignIntermediate) { } if (!IS_ARRAY(compound) && !IS_DICTIONARY(compound) && !IS_STRING(compound)) { - interpreter->errorOutput("Unknown compound found in indexing notation\n"); + interpreter->errorOutput("Unknown compound found in indexing notation: "); + printLiteralCustom(compound, interpreter->errorOutput); freeLiteral(third); freeLiteral(second); freeLiteral(first); diff --git a/source/toy_common.h b/source/toy_common.h index bee8170..f0e2951 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -9,6 +9,7 @@ #define TOY_VERSION_PATCH 5 #define TOY_VERSION_BUILD __DATE__ " " __TIME__ +//NOTE: I don't know why the time headers are here, need to try moving them back to the correct spots again //platform exports/imports #if defined(__linux__) #define TOY_API extern