diff --git a/examples/hoc.val b/examples/hof.val similarity index 100% rename from examples/hoc.val rename to examples/hof.val diff --git a/tools/example-generator/main.py b/tools/example-generator/main.py index d8c1bcd..6cb9bcc 100644 --- a/tools/example-generator/main.py +++ b/tools/example-generator/main.py @@ -76,7 +76,7 @@ def generate(self): with open(self.output_file, 'w') as f: f.write(content) - print(f'Successfully generated JS dictionary at {self.output_file}') + print(f'Successfully generated code at {self.output_file}') print(f'Processed {len(val_files)} val files') def _generate_js_dictionary(self, imports, dictionary_entries): @@ -89,14 +89,13 @@ def _generate_js_dictionary(self, imports, dictionary_entries): lines.append(import_line) lines.append('') - lines.append('const EXAMPLES = {') + lines.append('export const examples = {') dictionary_text = ',\n'.join(dictionary_entries) lines.append(dictionary_text) lines.append('};') lines.append('') - lines.append('export default EXAMPLES;') return '\n'.join(lines) diff --git a/www/src/App.tsx b/www/src/App.tsx index e18f98e..6423376 100644 --- a/www/src/App.tsx +++ b/www/src/App.tsx @@ -20,7 +20,7 @@ import { ResizablePanel, ResizablePanelGroup, } from './components/ui/resizable'; -import EXAMPLES from './lib/examples'; +import { examples } from './lib/examples'; const STORAGE_KEY_CODE = 'val-editor-code'; const STORAGE_KEY_EXAMPLE = 'val-editor-example'; @@ -30,7 +30,7 @@ function App() { const [code, setCode] = useState(() => { const savedCode = localStorage.getItem(STORAGE_KEY_CODE); - return savedCode || EXAMPLES.factorial; + return savedCode || examples.factorial; }); const [currentExample, setCurrentExample] = useState(() => { @@ -84,7 +84,7 @@ function App() { const handleExampleChange = (value: string) => { setCurrentExample(value); - setCode(EXAMPLES[value as keyof typeof EXAMPLES]); + setCode(examples[value as keyof typeof examples]); }; if (!wasmLoaded) return null; @@ -120,7 +120,7 @@ function App() { - {Object.keys(EXAMPLES).map((key) => ( + {Object.keys(examples).map((key) => ( {key} diff --git a/www/src/assets/examples/hof.val b/www/src/assets/examples/hof.val new file mode 100644 index 0000000..af1daa7 --- /dev/null +++ b/www/src/assets/examples/hof.val @@ -0,0 +1,68 @@ +fn map(l, f) { + i = 0 + + result = [] + + while (i < len(l)) { + result = append(result, f(l[i])) + i = i + 1 + } + + return result +} + +fn double(x) { + return x * 2 +} + +fn even(x) { + return x % 2 == 0 +} + +fn filter(l, f) { + i = 0 + + result = [] + + while (i < len(l)) { + if (f(l[i])) { + result = append(result, l[i]) + } + + i = i + 1 + } + + return result +} + +fn reduce(l, f, initial) { + i = 0 + + result = initial + + while (i < len(l)) { + result = f(result, l[i]) + i = i + 1 + } + + return result +} + +fn sum(a, b) { + return a + b +} + +fn max(a, b) { + if (a > b) { + return a + } else { + return b + } +} + +l = [1, 2, 3, 4, 5] + +println(map(l, double)) +println(filter(l, even)) +println(reduce(l, sum, 0)) +println(reduce(l, max, l[0])) diff --git a/www/src/lib/examples.ts b/www/src/lib/examples.ts index 5f786f8..7f2fd28 100644 --- a/www/src/lib/examples.ts +++ b/www/src/lib/examples.ts @@ -1,20 +1,18 @@ // This file is generated by `example-generator`. Do not edit manually. import factorial from '../assets/examples/factorial.val?raw'; import fibonacci from '../assets/examples/fibonacci.val?raw'; -import hoc from '../assets/examples/hoc.val?raw'; +import hof from '../assets/examples/hof.val?raw'; import loop from '../assets/examples/loop.val?raw'; import newton from '../assets/examples/newton.val?raw'; import primes from '../assets/examples/primes.val?raw'; import strings from '../assets/examples/strings.val?raw'; -const EXAMPLES = { +export const examples = { factorial: factorial, fibonacci: fibonacci, - hoc: hoc, + hof: hof, loop: loop, newton: newton, primes: primes, strings: strings, }; - -export default EXAMPLES;