Skip to content

Commit

Permalink
Interpreter demo (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Jan 19, 2025
1 parent e8f4cc4 commit 4708e24
Show file tree
Hide file tree
Showing 23 changed files with 194 additions and 28 deletions.
4 changes: 4 additions & 0 deletions doc/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export default defineConfig({
},
],
},
{
label: "Interpreter demo",
link: "/demo",
},
{
label: "Limitations",
link: "/limitations",
Expand Down
5 changes: 5 additions & 0 deletions doc/src/application/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runWorker } from "../application/run-worker.js";
import Worker from "./run/worker.js?worker";

export const run = async (source: string): Promise<Uint8Array<ArrayBuffer>> =>
runWorker(() => new Worker(), source);
8 changes: 8 additions & 0 deletions doc/src/application/run/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import init, { run } from "@raviqqe/stak";
import { handleWorkerMessage } from "../handle-worker-message";

const heapSize: number = 2 ** 20;

handleWorkerMessage(init, (input: string) =>
run(input, new Uint8Array(), heapSize),
);
2 changes: 1 addition & 1 deletion doc/src/components/Button.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.container {
composes: boxShadow from "../system.css";
composes: boxShadow from "../system.module.css";
display: flex;
align-items: center;
gap: 0.5rem;
Expand Down
10 changes: 10 additions & 0 deletions doc/src/components/CompilerDemo.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import { DemoForm } from "./CompilerDemo/DemoForm.jsx";
import { DemoIo } from "./CompilerDemo/DemoIo.jsx";
import Demo from "./Demo.astro";
---

<Demo>
<DemoForm client:visible />
<DemoIo client:visible />
</Demo>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useStore } from "@nanostores/solid";
import { Boxes, CirclePlay } from "lucide-solid";
import type { JSX } from "solid-js";
import * as store from "../stores/demo.js";
import { Button } from "./Button.jsx";
import { ButtonGroup } from "./ButtonGroup.jsx";
import * as store from "../../stores/compiler-demo.js";
import { Button } from "../Button.jsx";
import { ButtonGroup } from "../ButtonGroup.jsx";
import { ErrorMessage } from "../ErrorMessage.jsx";
import { Field } from "../Field.jsx";
import { Label } from "../Label.jsx";
import { TextArea } from "../TextArea.jsx";
import styles from "./DemoForm.module.css";
import { ErrorMessage } from "./ErrorMessage.jsx";
import { Field } from "./Field.jsx";
import { Label } from "./Label.jsx";
import { TextArea } from "./TextArea.jsx";

export const DemoForm = (): JSX.Element => {
const source = useStore(store.source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}

.output {
composes: textArea from "../system.css";
composes: textArea from "../../system.module.css";
overflow: auto;
flex: 1;
margin: 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useStore } from "@nanostores/solid";
import type { JSX } from "solid-js";
import * as store from "../stores/demo.js";
import * as store from "../../stores/compiler-demo.js";
import { ErrorMessage } from "../ErrorMessage.jsx";
import { Field } from "../Field.jsx";
import { Label } from "../Label.jsx";
import { Link } from "../Link.jsx";
import { TextArea } from "../TextArea.jsx";
import styles from "./DemoIo.module.css";
import { ErrorMessage } from "./ErrorMessage.jsx";
import { Field } from "./Field.jsx";
import { Label } from "./Label.jsx";
import { Link } from "./Link.jsx";
import { TextArea } from "./TextArea.jsx";

export const DemoIo = (): JSX.Element => {
const input = useStore(store.input);
Expand Down
8 changes: 3 additions & 5 deletions doc/src/components/Demo.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
---
import classNames from "classnames";
import styles from "./Demo.module.css";
import { DemoForm } from "./DemoForm.jsx";
import { DemoIo } from "./DemoIo.jsx";
---

<div class={styles.container}>
<DemoForm client:visible />
<DemoIo client:visible />
<div class={classNames(styles.root, "not-content")}>
<slot />
</div>
3 changes: 1 addition & 2 deletions doc/src/components/Demo.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.container {
.root {
display: flex;
gap: 1.5rem;
height: 100%;

@media (max-width: 42rem) {
flex-direction: column;
Expand Down
10 changes: 10 additions & 0 deletions doc/src/components/InterpreterDemo.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import Demo from "./Demo.astro";
import { DemoForm } from "./InterpreterDemo/DemoForm.jsx";
import { DemoOutput } from "./InterpreterDemo/DemoOutput.jsx";
---

<Demo>
<DemoForm client:visible />
<DemoOutput client:visible />
</Demo>
13 changes: 13 additions & 0 deletions doc/src/components/InterpreterDemo/DemoForm.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.root {
display: flex;
flex-direction: column;
min-height: 24rem;

> * {
flex: 1;
}
}

.textArea {
flex: 1;
}
25 changes: 25 additions & 0 deletions doc/src/components/InterpreterDemo/DemoForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useStore } from "@nanostores/solid";
import type { JSX } from "solid-js";
import * as store from "../../stores/interpreter-demo.js";
import { Field } from "../Field.jsx";
import { Label } from "../Label.jsx";
import { TextArea } from "../TextArea.jsx";
import styles from "./DemoForm.module.css";

export const DemoForm = (): JSX.Element => {
const source = useStore(store.source);

return (
<form class={styles.root}>
<Field>
<Label for="source">Program</Label>
<TextArea
class={styles.textArea}
id="source"
onChange={(source) => store.source.set(source)}
value={source()}
/>
</Field>
</form>
);
};
18 changes: 18 additions & 0 deletions doc/src/components/InterpreterDemo/DemoOutput.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.root {
display: flex;
flex-direction: column;

> * {
flex: 1;
}
}

.output {
composes: textArea from "../../system.module.css";
flex: 1;
margin: 0;

> pre {
margin: 0;
}
}
24 changes: 24 additions & 0 deletions doc/src/components/InterpreterDemo/DemoOutput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useStore } from "@nanostores/solid";
import type { JSX } from "solid-js";
import * as store from "../../stores/interpreter-demo.js";
import { ErrorMessage } from "../ErrorMessage.js";
import { Field } from "../Field.jsx";
import { Label } from "../Label.jsx";
import styles from "./DemoOutput.module.css";

export const DemoOutput = (): JSX.Element => {
const output = useStore(store.output);
const error = useStore(store.error);

return (
<div class={styles.root}>
<Field>
<Label for="output">Output</Label>
<pre class={styles.output} id="output">
{output()}
</pre>
{error() && <ErrorMessage>{error()?.message}</ErrorMessage>}
</Field>
</div>
);
};
2 changes: 1 addition & 1 deletion doc/src/components/TextArea.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.container {
composes: textArea from "../system.css";
composes: textArea from "../system.module.css";
overflow: auto;
resize: none;
font-size: inherit;
Expand Down
6 changes: 4 additions & 2 deletions doc/src/components/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import classNames from "classnames";
import type { JSX } from "solid-js";
import styles from "./TextArea.module.css";

interface Props {
class?: string;
id?: string;
onChange: (value: string) => void;
style?: JSX.CSSProperties;
Expand All @@ -10,9 +12,9 @@ interface Props {

export const TextArea = (props: Props): JSX.Element => (
<textarea
class={styles.container}
class={classNames(styles.container, props.class)}
id={props.id}
onChange={(event) => props.onChange(event.currentTarget.value)}
onInput={(event) => props.onChange(event.currentTarget.value)}
style={props.style}
value={props.value}
>
Expand Down
10 changes: 10 additions & 0 deletions doc/src/content/docs/demo.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Interpreter demo
description: Stak Scheme interpreter demo running on web browsers.
---

import InterpreterDemo from "../../components/InterpreterDemo.astro";

Stak Scheme can run on many platforms including web browsers. The following is the demo of Stak Scheme built in WASM running on a web page.

<InterpreterDemo />
10 changes: 7 additions & 3 deletions doc/src/pages/demo.astro → doc/src/pages/compiler-demo.astro
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
import DemoComponent from "../components/Demo.astro";
import CompilerDemoComponent from "../components/CompilerDemo.astro";
---

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href={`${import.meta.env.BASE_URL}/icon.svg`} />
<title>Stak Scheme demo</title>
<title>Stak Scheme compiler demo</title>
</head>
<body>
<style>
Expand Down Expand Up @@ -40,8 +40,12 @@ import DemoComponent from "../components/Demo.astro";
padding: 1.5rem;
margin: 0;
background-color: var(--primary-background-color);

> * {
height: 100%;
}
}
</style>
<DemoComponent />
<CompilerDemoComponent />
</body>
</html>
File renamed without changes.
35 changes: 35 additions & 0 deletions doc/src/stores/interpreter-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { atom, computed, task } from "nanostores";
import { run as runProgram } from "../application/run.js";

export const source = atom(
`
(import (scheme base) (scheme write))
(define (fibonacci x)
(if (<= x 1)
1
(+ (fibonacci (- x 1)) (fibonacci (- x 2)))))
(display "Answer: ")
(write (fibonacci 10))
(newline)
`.trim(),
);

const run = computed(source, (source) =>
task(async () => {
try {
return await runProgram(source);
} catch (error) {
return error as Error;
}
}),
);

export const output = computed(run, (output) =>
output instanceof Error ? null : new TextDecoder().decode(output),
);

export const error = computed(run, (error) =>
error instanceof Error ? error : null,
);
1 change: 1 addition & 0 deletions doc/src/system.css → doc/src/system.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

.textArea {
composes: boxShadow;
font-family: monospace;
padding: 1rem;
box-sizing: border-box;
overflow: auto;
Expand Down

0 comments on commit 4708e24

Please sign in to comment.