├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── compiler.rkt ├── default.nix ├── foo.rkt ├── index.html ├── server.rkt └── wacket.rkt /.gitignore: -------------------------------------------------------------------------------- 1 | foo.wat 2 | foo.wasm -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Wacket Developers 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | foo.wasm: foo.wat 2 | wat2wasm foo.wat 3 | 4 | foo.wat: foo.rkt wacket.rkt 5 | ./compiler.rkt < foo.rkt > foo.wat 6 | 7 | .PHONY: clean 8 | 9 | clean: 10 | rm -f foo.wat foo.wasm 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Tsoding](https://img.shields.io/badge/twitch.tv-tsoding-purple?logo=twitch&style=for-the-badge)](https://www.twitch.tv/tsoding) 2 | # wacket 3 | 4 | Racket to WebAssembly "compiler". 5 | 6 | ## Quick Start 7 | 8 | 1. Install [wabt][wabt] 9 | 2. Install [racket][racket] 10 | 3. `$ make` 11 | 4. `$ ./server.rkt` 12 | 5. `$ http://localhost:3001/index.html` 13 | 8. Change math expression in the `foo.rkt` file and `make` again. 14 | 15 | [wabt]: https://github.com/WebAssembly/wabt 16 | [racket]: https://racket-lang.org/ 17 | -------------------------------------------------------------------------------- /compiler.rkt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env racket 2 | #lang racket 3 | 4 | (require racket/pretty) 5 | (require "wacket.rkt") 6 | 7 | ;;; TODO(#6): compiler only compiles first expresion from stdin 8 | (pretty-write (wat-module 9 | (wat-compile (read)))) 10 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | with import {}; { 2 | WacketEnv = stdenv.mkDerivation { 3 | name = "WacketEnv"; 4 | buildInputs = [ racket wabt ]; 5 | }; 6 | } 7 | -------------------------------------------------------------------------------- /foo.rkt: -------------------------------------------------------------------------------- 1 | (define (foo a b) 2 | (- (+ 2 2) 1)) 3 | 4 | (define (bar) 5 | (+ 2 3)) 6 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Try WebAssembly 4 | 5 | 6 |
7 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /server.rkt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env racket 2 | #lang racket 3 | 4 | (require web-server/servlet-env 5 | web-server/http 6 | web-server/dispatchers/dispatch) 7 | 8 | (serve/servlet (lambda (_) (next-dispatcher)) 9 | #:servlet-path "/" 10 | #:extra-files-paths (list "./") 11 | #:port 3001 12 | #:launch-browser? #f) 13 | -------------------------------------------------------------------------------- /wacket.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | 3 | (provide wat-compile 4 | wat-module 5 | wat-export-func) 6 | 7 | (define (wat-module expr) 8 | `(module ,expr)) 9 | 10 | (define (wat-export-func name body) 11 | `(func (export ,name) (result i32) 12 | ,@body)) 13 | 14 | (define (wat-number x) 15 | `(i32.const ,x)) 16 | 17 | (define (wat-compile-op op xs) 18 | (append 19 | (append* (for/list ([x xs]) (wat-compile x))) 20 | (for/list ([_ (in-range (sub1 (length xs)))]) 21 | op))) 22 | 23 | (define (wat-compile-func-args args) 24 | (for/list ([_ args]) 25 | ;; TODO(#7): all of the arguments are hardcoded to i32 26 | '(param i32))) 27 | 28 | (define (wat-compile-func name args body) 29 | ;; TODO(#8): all compiled functions are exported 30 | `(func (export ,(symbol->string name)) 31 | ,@(wat-compile-func-args args) 32 | ;; TODO(#9): function result is hardcoded to i32 33 | (result i32) 34 | ,@(append* 35 | (for/list ([x body]) 36 | (wat-compile x))))) 37 | 38 | (define (wat-compile expr) 39 | (match expr 40 | [(list (quote +) xs ...) 41 | (wat-compile-op 'i32.add xs)] 42 | [(list (quote -) xs ...) 43 | (wat-compile-op 'i32.sub xs)] 44 | [(list (quote *) xs ...) 45 | (wat-compile-op 'i32.mul xs)] 46 | [(list (quote /) xs ...) 47 | (wat-compile-op 'i32.div_s)] 48 | [(list (quote define) (list name args ...) body ...) 49 | (wat-compile-func name args body)] 50 | [x #:when (integer? x) (list (wat-number x))] 51 | ;; TODO(#10): compilation of local function variables is not support 52 | [unsupported (error "Cannot compile expression:" unsupported)])) 53 | 54 | --------------------------------------------------------------------------------