Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Feb 10, 2024
1 parent 2580900 commit 88a585f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
24 changes: 24 additions & 0 deletions adana-playground/app.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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("");
Expand Down
30 changes: 30 additions & 0 deletions adana-playground/examples.js
Original file line number Diff line number Diff line change
@@ -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
}
`,
},
];
8 changes: 4 additions & 4 deletions adana-playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ <h1>Adana Playground</h1>
<form action="" id="scriptForm">
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-start">
<div class="d-flex">
<button type="submit" class="btn btn-outline-danger text-uppercase me-1">
Run
</button>
<button id="clearHeap" class="btn btn-outline-info text-uppercase">
Clear Heap
</button>
<select name="Examples" id="examples" class="form-select">
<option selected value="">Select an example</option>
</select>
</div>
</div>
<div class="card-body m-0 p-0">
Expand Down
4 changes: 4 additions & 0 deletions adana-playground/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
--bs-body-bg: black;
}

.form-select {
--bs-border-radius: 0;
}

.btn {
--bs-btn-border-radius: 0;
}
Expand Down
2 changes: 1 addition & 1 deletion adana-script/file_tests/test_fizzbuzz_else.adana
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 88a585f

Please sign in to comment.