Skip to content

Commit aa3b58a

Browse files
Copy (#60)
1 parent 6c0c339 commit aa3b58a

File tree

128 files changed

+12529
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+12529
-10
lines changed

.github/workflows/wasm-calc-haskell.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
contents: "read"
4040
strategy:
4141
matrix:
42-
version: [9, 10, 11, 12]
42+
version: [9, 10, 11, 12, 13]
4343
steps:
4444
- name: Checkout 🛎️
4545
uses: actions/checkout@v4

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,34 @@ run-build-drawing-demo-11:
5151

5252
# run with `make watch version=9` to run tests / ghci for wasm-calc9
5353
.PHONY: watch
54-
version = 12
54+
version = 13
5555
watch:
5656
ghciwatch --watch wasm-calc$(version) \
5757
--command "cabal repl wasm-calc$(version)-tests" \
5858
--test-ghci 'main'
5959

6060
# run with `make run version=8` to run wasm-calc8
6161
.PHONY: run
62-
version = 12
62+
version = 13
6363
run:
6464
cabal run wasm-calc$(version) -- repl
6565

6666
# run with `make test version=7` to run wasm-calc7 tests
6767
.PHONY: test
68-
version = 12
68+
version = 13
6969
test:
7070
cabal run wasm-calc$(version):tests
7171

7272
# run with `make format-all-files version=7` to format all static `.calc` files for wasm-calc7
7373
.PHONY: format-all-files
74-
version = 12
74+
version = 13
7575
STATIC_FILES = "./wasm-calc$(version)/test/static/"
7676
format-all-files:
7777
find $(STATIC_FILES) -maxdepth 1 -type f -exec cabal run wasm-calc$(version) -- format {} \;
7878

7979
# run with `make format-all-files version=7` to build all static `.calc` files for wasm-calc7
8080
.PHONY: build-file
81-
version = 12
81+
version = 13
8282
file = noalloc
8383
STATIC_FILES = ./wasm-calc$(version)/test/static
8484
build-file:
@@ -87,7 +87,7 @@ build-file:
8787
# run with `make build-malloc version=9` to build and diff malloc.calc for
8888
# wasm-calc9
8989
.PHONY: build-malloc
90-
version = 12
90+
version = 13
9191
build-malloc:
9292
nix run .#wasm-calc$(version) build wasm-calc$(version)/static/malloc.calc > wasm-calc$(version)/static/malloc-new.wasm
9393
diff wasm-calc$(version)/static/malloc-new.wasm wasm-calc$(version)/static/malloc.wasm

cabal.project

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ packages:
1010
wasm-calc9/wasm-calc9.cabal,
1111
wasm-calc10/wasm-calc10.cabal,
1212
wasm-calc11/wasm-calc11.cabal,
13-
wasm-calc12/wasm-calc12.cabal
13+
wasm-calc12/wasm-calc12.cabal,
14+
wasm-calc13/wasm-calc13.cabal
1415

1516
with-compiler: ghc-9.6.5
1617

flake.nix

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
wasm-calc10 = makeCalc "wasm-calc10" ./wasm-calc10;
6969
wasm-calc11 = makeCalc "wasm-calc11" ./wasm-calc11;
7070
wasm-calc12 = makeCalc "wasm-calc12" ./wasm-calc12;
71+
wasm-calc13 = makeCalc "wasm-calc13" ./wasm-calc13;
7172

7273
in
7374
{
@@ -76,15 +77,17 @@
7677
inherit wasm-calc1 wasm-calc2 wasm-calc3
7778
wasm-calc4 wasm-calc5 wasm-calc6
7879
wasm-calc7 wasm-calc8 wasm-calc9
79-
wasm-calc10 wasm-calc11 wasm-calc12;
80+
wasm-calc10 wasm-calc11 wasm-calc12
81+
wasm-calc13;
8082
};
8183

8284
# sneaky way to 'build all'
8385
checks = {
8486
inherit wasm-calc1 wasm-calc2 wasm-calc3
8587
wasm-calc4 wasm-calc5 wasm-calc6
8688
wasm-calc7 wasm-calc8 wasm-calc9
87-
wasm-calc10 wasm-calc11 wasm-calc12;
89+
wasm-calc10 wasm-calc11 wasm-calc12
90+
wasm-calc13;
8891
};
8992

9093
defaultPackage = self.packages.${system}.wasm-calc12;

wasm-calc13/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist-newstyle
2+
.direnv

wasm-calc13/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for nix-basic
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.

wasm-calc13/app/Main.hs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Main where
2+
3+
import qualified Calc
4+
import Control.Applicative
5+
import Data.Text (Text)
6+
import qualified Data.Text as T
7+
import qualified Options.Applicative as Opt
8+
import System.IO
9+
10+
data AppAction
11+
= Repl
12+
| Build Text -- given an input path, turn it into a Wasm module or explode with an error
13+
| Format Text -- given an input path, format and write new file
14+
15+
parseAppAction :: Opt.Parser AppAction
16+
parseAppAction =
17+
Opt.hsubparser
18+
( Opt.command
19+
"repl"
20+
( Opt.info
21+
(pure Repl)
22+
(Opt.progDesc "Start new calc repl")
23+
)
24+
<> Opt.command
25+
"build"
26+
( Opt.info
27+
(Build <$> filePathParse)
28+
(Opt.progDesc "Given a file path, either compile it into a wasm module or return an error")
29+
)
30+
<> Opt.command
31+
"format"
32+
( Opt.info
33+
(Format <$> filePathParse)
34+
(Opt.progDesc "Given a file path, parse and save a formatted file")
35+
)
36+
)
37+
38+
filePathParse :: Opt.Parser Text
39+
filePathParse =
40+
Opt.argument
41+
Opt.str
42+
(Opt.metavar "<file path>")
43+
44+
optionsParse :: Opt.Parser AppAction
45+
optionsParse = parseAppAction
46+
47+
helpfulPreferences :: Opt.ParserPrefs
48+
helpfulPreferences =
49+
Opt.defaultPrefs
50+
{ Opt.prefShowHelpOnError = True,
51+
Opt.prefShowHelpOnEmpty = True
52+
}
53+
54+
main :: IO ()
55+
main = do
56+
hSetBuffering stdout LineBuffering
57+
hSetBuffering stderr LineBuffering
58+
action <-
59+
Opt.customExecParser
60+
helpfulPreferences
61+
(Opt.info (optionsParse <**> Opt.helper) Opt.fullDesc)
62+
case action of
63+
Repl -> Calc.repl
64+
Build filePath -> Calc.build (T.unpack filePath)
65+
Format filePath -> Calc.prettyPrint (T.unpack filePath)

wasm-calc13/demo/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ignore compiler wasm files
2+
*.wasm

wasm-calc13/demo/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# demo
2+
3+
This is a small demo that runs in the browser, passing a `draw` function into a
4+
WASM module.
5+
6+
To open it in a browser, run `serve .` and navigate to
7+
`localhost:3000/draw.html`.
8+
9+
To change the file and see results, run `watchexec -w ./**/*.calc make
10+
run-build-drawing-demo-7`. This will watch all `.calc` files and recompile on file changes.
11+
12+
You will need to reload the browser after each change.

wasm-calc13/demo/draw.calc

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
global mut index: Int64 = 1
2+
3+
import imports.draw as draw(
4+
x: Int64, y: Int64, r: Int64, g: Int64, b: Int64
5+
) -> Void
6+
7+
function min(floor: Int64, value: Int64) -> Int64 {
8+
if value > floor then value else floor
9+
}
10+
11+
function max(ceiling: Int64, value: Int64) -> Int64 {
12+
if value < ceiling then value else ceiling
13+
}
14+
15+
function clamp(
16+
floor: Int64, ceiling: Int64, value: Int64
17+
) -> Int64 { min(floor, max(ceiling, value))}
18+
19+
function drawBounded(
20+
x: Int64, y: Int64, color: (Int64,Int64,Int64)
21+
) -> (Int64,Int64,Int64) {
22+
let maxWidth: Int64 = 600;
23+
let maxHeight: Int64 = 600;
24+
let (r, g, b) = color;
25+
draw(
26+
clamp(0, maxWidth, x), clamp(0, maxHeight, y), r, g, b
27+
);
28+
(r, g, b)
29+
}
30+
31+
function cycle(color: (Int64,Int64,Int64)) -> (Int64,
32+
Int64,
33+
Int64) { let (r, g, b) = color; (g, b, r)}
34+
35+
function initial(index: Int64) -> (Int64,Int64,Int64) {
36+
let r = clamp(0, 255, index * 2);
37+
let g = clamp(0, 255, 255 - r);
38+
let b = clamp(0, 255, r * 3);
39+
(r, g, b)
40+
}
41+
42+
export function test() -> Void {
43+
let color = drawBounded(
44+
index * 2, index * 3, initial(index)
45+
);
46+
let color2 = drawBounded(
47+
100 - index, index * 3, cycle(color)
48+
);
49+
let color3 = drawBounded(
50+
10 + index * 3, 50 - index * 2, cycle(color2)
51+
);
52+
drawBounded(index * 4, 200 - index * 3, cycle(color3));
53+
if index < 200 then
54+
set(index, index + 1)
55+
else
56+
set(index, 0)
57+
}

wasm-calc13/demo/draw.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!doctype html>
2+
3+
<html>
4+
5+
<head>
6+
<meta charset="utf-8">
7+
<title>WASM test</title>
8+
</head>
9+
10+
<body>
11+
<script>
12+
const drawDot = (x,y,r,g,b) => {
13+
const canvas = document.getElementById("canvas");
14+
if (canvas.getContext) {
15+
const ctx = canvas.getContext("2d");
16+
ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")"
17+
ctx.fillRect(x, y, x + 1, y + 1);
18+
}
19+
}
20+
21+
const importObject = {
22+
imports: {
23+
draw: (bigX,bigY,bigR,bigG,bigB) => {
24+
drawDot(Number(bigX), Number(bigY),Number(bigR),Number(bigG), Number(bigB))
25+
}
26+
}
27+
};
28+
29+
fetch("draw.wasm").then(response =>
30+
response.arrayBuffer()
31+
).then(bytes =>
32+
WebAssembly.instantiate(bytes, importObject)
33+
).then(result => {
34+
setInterval(() => {
35+
result.instance.exports.test();
36+
},1);
37+
});
38+
</script>
39+
<canvas id="canvas" width=600 height=600/>
40+
</body>
41+
42+
</html>

wasm-calc13/src/Calc.hs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Calc
2+
( module Calc.Types,
3+
module Calc.Build,
4+
module Calc.Parser,
5+
module Calc.ExprUtils,
6+
module Calc.Repl,
7+
module Calc.Wasm,
8+
module Calc.PrettyPrint,
9+
)
10+
where
11+
12+
import Calc.Build
13+
import Calc.ExprUtils
14+
import Calc.Parser
15+
import Calc.PrettyPrint
16+
import Calc.Repl
17+
import Calc.Types
18+
import Calc.Wasm

0 commit comments

Comments
 (0)