Design: WAT syntax as a valid JavaScript subset

Created on 16 Mar 2019  路  4Comments  路  Source: WebAssembly/design

To permit comma separated S-expressions within the text format to allow the WAT syntax to be a valid parse-time JavaScript subset syntax like JSON is to JavaScript.

This would allow you to write the following example:

(func $swap (param i32 i32) (result i32 i32) (get_local 1) (get_local 0))

As the following:

(func, $swap, (param, i32, i32), (result, i32, i32), (get_local, 1) (get_local, 0))

One application of this is being able to write valid WAT syntax in JavaScript functions

function add (a, b) {
   (i32.const, a)
   (i32.const, b)
   (i32.const, add)
}

That you can add.toString() to extract valid WAT source code, that can make it easier to interleave WAT source code within JavaScript without the need for template literals when prototyping.

Most helpful comment

I'm really not sure we want to have JS-compatible WAST. Mostly because from a parser perpective it would be really hard to tell if you are writing WAST or JS.

I would rather use JS's template literals:

function add (a, b) {
   wast`
     (i32.const, a)
     (i32.const, b)
     (i32.const, add)
   `
}

All 4 comments

S-expressions make me think of Lisp and because of that I would expect ' to mean quote and , to mean unquote.

This is an interesting idea, but I'm not sure we'll want to modify the text format to support it.

I'm really not sure we want to have JS-compatible WAST. Mostly because from a parser perpective it would be really hard to tell if you are writing WAST or JS.

I would rather use JS's template literals:

function add (a, b) {
   wast`
     (i32.const, a)
     (i32.const, b)
     (i32.const, add)
   `
}

You could knock up a WAT DSL in CoffeeScript very easily. Just to offer another option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nikhedonia picture nikhedonia  路  7Comments

beriberikix picture beriberikix  路  7Comments

dpw picture dpw  路  3Comments

artem-v-shamsutdinov picture artem-v-shamsutdinov  路  6Comments

bobOnGitHub picture bobOnGitHub  路  6Comments