From 88a585f53358126a52d6ab804d9b59e4eedfe9c9 Mon Sep 17 00:00:00 2001 From: Nordine Bittich Date: Sat, 10 Feb 2024 12:48:45 +0100 Subject: [PATCH] examples --- adana-playground/app.js | 24 +++++++++++++++ adana-playground/examples.js | 30 +++++++++++++++++++ adana-playground/index.html | 8 ++--- adana-playground/style.css | 4 +++ .../file_tests/test_fizzbuzz_else.adana | 2 +- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 adana-playground/examples.js diff --git a/adana-playground/app.js b/adana-playground/app.js index a86b07a..f9c804e 100644 --- a/adana-playground/app.js +++ b/adana-playground/app.js @@ -1,15 +1,20 @@ import init, { compute_as_string as compute } from "./pkg/adana_script_wasm.js"; +import { EXAMPLES } from "./examples.js"; async function run() { const form = document.querySelector("form"); form.classList.add("d-none"); + await init(); + const memory = new WebAssembly.Memory({ initial: 32, // 2mb maximum: 512, // 32mb shared: true, }); + const ctx = new Uint8Array(memory.buffer); + form.classList.remove("d-none"); const text_area = document.querySelector("#code"); @@ -27,10 +32,29 @@ async function run() { } }; + const select = document.querySelector("#examples"); + for (const example of EXAMPLES) { + const option = document.createElement("option"); + option.innerText = example.label; + option.value = example.key; + select.appendChild(option); + } + + select.addEventListener("change", function (e) { + const key = e.target.value; + const example = EXAMPLES.find((e) => e.key === key); + if (example) { + text_area.value = example.script; + } + }); + form.addEventListener("submit", (e) => { e.preventDefault(); logs = []; const data = new FormData(e.target); + for (let i = 0; i < ctx.length; i++) { + ctx[i] = undefined; + } let res = compute(data.get("code") || "", ctx); console.log(res); // NORDINE out.value = logs.join(""); diff --git a/adana-playground/examples.js b/adana-playground/examples.js new file mode 100644 index 0000000..332c9fb --- /dev/null +++ b/adana-playground/examples.js @@ -0,0 +1,30 @@ +export const EXAMPLES = [ + { + key: "fizz_buzz", + label: "Fizz Buzz", + script: `num = 100 +count = 0 +text = "" + while count == 0 || count <= num { + if count % 3 == 0 && count % 5 == 0 { + # Fizz Buzz + text = count + " = FizzBuzz" + println(text) + + } else if count % 5 == 0 { + # buzz + text = count + " = Buzz" # oops + println(text) + } else if count % 3 == 0 { + #fizz + text = count + " = Fizz" + println(text) + + } else { + println(count + " neither fizz nor buzz") + } + count += 1 +} +`, + }, +]; diff --git a/adana-playground/index.html b/adana-playground/index.html index b157a78..a583776 100644 --- a/adana-playground/index.html +++ b/adana-playground/index.html @@ -28,13 +28,13 @@

Adana Playground

-
+
- +
diff --git a/adana-playground/style.css b/adana-playground/style.css index c8781ed..b8f7614 100644 --- a/adana-playground/style.css +++ b/adana-playground/style.css @@ -2,6 +2,10 @@ --bs-body-bg: black; } +.form-select { + --bs-border-radius: 0; +} + .btn { --bs-btn-border-radius: 0; } diff --git a/adana-script/file_tests/test_fizzbuzz_else.adana b/adana-script/file_tests/test_fizzbuzz_else.adana index 446da1b..305e173 100644 --- a/adana-script/file_tests/test_fizzbuzz_else.adana +++ b/adana-script/file_tests/test_fizzbuzz_else.adana @@ -2,7 +2,7 @@ count = -1 text = "" while count <= 100 { - count = count + 1 + count += 1 if count % 3 == 0 && count % 5 == 0 { # Fizz Buzz text = count + " = FizzBuzz"