a programming language for extremely concise web assembly modules
warning: this compiler is very alpha and error messages aren't the best, but it works and language is simple!
clojure
; main.w
(extern console_log [message])
(pub defn main []
(console_log "Hello World!"))
Wasp depends on git
and rust
. Make sure you have them installed before beginning.
console
cargo install wasp
wasp init myproject
cd myproject
wasp build
python3 -m http.server
Open up http://localhost:8000 and look in console. At this point we will have a web assembly module that has access to the standard libraries functions. More to come in this area!
If you don't have need for the standard library (or want to write your own!). This is also an option.
console
wasp init myproject --no-std
At this point we will have a web assembly module with a single exported main function and nothing else.
If you think your standard library is out of date, just run wasp vendor
Wasp is an extremely basic language and standard library. Everything is a linked list.
clojure
(cons 42 nil) ; returns the memory location of cons
clojure
(head (cons 42 nil) ; return the head value 42
clojure
(tail (cons 42 nil) ; returns the memory location of tail
clojure
(cons 1 (cons 2 (cons 3 nil)) ; returns a linked list
clojure
(# cons 1 2 3) ; short hand for (cons 1 (cons 2 (cons 3 nil)))
Using web-dom we can easily draw something to screen. Loops in wasp work differently than other languages, bbserve how this example uses recursion to rebind variables.
```clojure (extern globalgetwindow []) (extern windowgetdocument [window]) (extern documentqueryselector [document query]) (extern htmlcanvasgetcontext [element context]) (extern drawingsetfillstyle [canvas color]) (extern drawingfill_rect [canvas x y w h])
(def colors ("black" "grey" "red"))
(pub defn main [] (let [window (globalgetwindow) document (windowgetdocument window) canvas (documentqueryselector document "#screen") ctx (htmlcanvasgetcontext canvas "2d")] (loop [x 0] (if (< x 3) (do (drawingsetfillstyle ctx (memnum (+ colors (* SIZENUM x)))) (drawingfill_rect ctx (* x 10) (* x 10) 50 50 ) (recur [x (+ x 1)])))))) ```
See it working here
It's often important for a web assembly modules to have some sort of global data that can be changed. For instance in a game we might have a high score.
```clojure (def high_score (0) )
(defn runmygame
...
(memnum highscore (+ (memnum highscore) 100)
...)
```
warning: this may change but it works
Code dependencies are kept in a special folder called vendor
which is populated by specific checkouts of git repositories.
For example a project.wasp
containing:
bar git@github.com:richardanaya/bar.git@specific-bar
would result in these commands (roughly)
rm -rf vendor
mkdir vendor
git clone git@github.com:richardanaya/bar.git@specific-bar vendor/bar
when wasp vendor
is called
Now, when wasp compiles your code, it does a few things.
project.wasp
, one folder at a time all files ending in .w are loaded from each vendor/<dependency-name>
and its subfolders.vendor
are loadedPlease try to use non conflicting names in meantime while this is fleshed out.
"hello world!"
)":hello_world"
)true
false
)(another_global 1 true :hey (:more-data)
). Use this for embedding raw data into your application memory on startup.
Note that numbers in this sequence are 64-bit float, but non-numbers are 32-bit integers.(mem_num x:integer y) - set 64-bit float value at memory location x to value y
(memheapstart) - get number that represents the start of the heap
These oprators work pretty much how you'd expect if you've used C
"test_"+name
when compiled in debug and removed when built with wasp build --release
. The function is
comes from the standard library.clojure
(deftest addition
(is (= 4 (+ 2 2)) "2 + 2 should be 4")
(is (= 7 (+ 3 4)) "3 + 4 should be 7"))
See it working here
Wasp prefers to keep as little in the core functionality as possible, letting the standard library evolve faster and more independent community driven manner. This project currently follows a principle that if a feature can be implemented with our primitive functions, don't include it in the core compiled language and let the standard library implement it. Also that no heap based concepts be added to the core language.