Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 2 additions & 3 deletions tools/example-generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions www/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -120,7 +120,7 @@ function App() {
<SelectValue placeholder='Select example' />
</SelectTrigger>
<SelectContent>
{Object.keys(EXAMPLES).map((key) => (
{Object.keys(examples).map((key) => (
<SelectItem key={key} value={key}>
{key}
</SelectItem>
Expand Down
68 changes: 68 additions & 0 deletions www/src/assets/examples/hof.val
Original file line number Diff line number Diff line change
@@ -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]))
8 changes: 3 additions & 5 deletions www/src/lib/examples.ts
Original file line number Diff line number Diff line change
@@ -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;