From db5622bccd9c52870d7885e463cb39636a0076ed Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 1 Aug 2024 18:00:11 -0400 Subject: [PATCH] new random seed for every run --- src/ludus.janet | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/ludus.janet b/src/ludus.janet index 65bb702..a4d4361 100644 --- a/src/ludus.janet +++ b/src/ludus.janet @@ -12,45 +12,72 @@ (import /src/json :as j) (defn ludus [source] + # if we can't load prelude, bail (when (= :error prelude/pkg) (error "could not load prelude")) + + # get us a clean working slate (def ctx @{:^parent prelude/ctx}) (def errors @[]) (var result @"") (def console @"") + + # capture all `print`s (setdyn :out console) + + # an output table + # this will change: the shape of our output + # at the moment, there's only one stack of turtle graphics + # we will be getting more (def out @{:errors errors :result result :io @{ :stdout @{:proto [:text-stream "0.1.0"] :data console} :turtle @{:proto [:turtle-graphics "0.1.0"] :data @[]}}}) + + ### start the program + # first, scanning (def scanned (s/scan source)) (when (any? (scanned :errors)) (each err (scanned :errors) (e/scan-error err)) (break (-> out j/encode string))) + # then, parsing (def parsed (p/parse scanned)) (when (any? (parsed :errors)) (each err (parsed :errors) (e/parse-error err)) (break (-> out j/encode string))) + # then, validation (def validated (v/valid parsed ctx)) (when (any? (validated :errors)) (each err (validated :errors) (e/validation-error err)) (break (-> out j/encode string))) + # and, finally, try interpreting the program (try (do + # we need to do this every run or we get the very same sequence of "random" numbers every time we run a program (math/seedrandom (os/cryptorand 8)) (set result (i/interpret (parsed :ast) ctx))) ([err] (e/runtime-error err) (break (-> out j/encode string)))) + + # stop capturing output (setdyn :out stdout) + + # update our output table with our output (set (out :result) (b/show result)) (set (((out :io) :turtle) :data) (get-in prelude/pkg [:turtle_commands :^value])) + + # run the "postlude": any Ludus code that needs to run after each program + # right now this is just resetting the boxes that hold turtle commands and state (try (i/interpret prelude/post/ast ctx) ([err] (e/runtime-error err))) + + # json-encode our output table, and convert it from a buffer to a string (which we require for playing nice with WASM/C) (-> out j/encode string)) +#### REPL (comment # (do # (def start (os/clock))