-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import parse from './parse.js'; | ||
|
||
let indent = '', newline = '\n', pad = '', comments = false, _i = 0 | ||
|
||
export default function print(tree, options = {}) { | ||
if (typeof tree === 'string') tree = parse(tree); | ||
|
||
({ indent, newline, pad, comments } = options); | ||
newline ||= '' | ||
pad ||= '' | ||
indent ||= '' | ||
|
||
let out = typeof tree[0] === 'string' ? printNode(tree) : tree.map(node => printNode(node)).join(newline) | ||
|
||
return out | ||
} | ||
|
||
const flats = ['param', 'local', 'global', 'result', 'export'] | ||
|
||
function printNode(node, level = 0) { | ||
if (!Array.isArray(node)) return node + '' | ||
|
||
let content = node[0] | ||
|
||
for (let i = 1; i < node.length; i++) { | ||
// new node doesn't need space separator, eg. [x,[y]] -> `x(y)` | ||
if (Array.isArray(node[i])) { | ||
// inline nodes like (param x)(param y) | ||
// (func (export "xxx")..., but not (func (export "a")(param "b")... | ||
if ( | ||
flats.includes(node[i][0]) && | ||
(!Array.isArray(node[i - 1]) || node[i][0] === node[i - 1][0]) | ||
) { | ||
if (!Array.isArray(node[i - 1])) content += ` ` | ||
} else { | ||
content += newline | ||
if (node[i]) content += indent.repeat(level + 1) | ||
} | ||
|
||
content += printNode(node[i], level + 1) | ||
} | ||
else { | ||
content += ` ` | ||
content += node[i] | ||
} | ||
} | ||
return `(${content})` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
import './parse.js' | ||
import './compile.js' | ||
import './print.js' | ||
|
||
// stub fetch for local purpose | ||
const isNode = typeof global !== 'undefined' && globalThis === global | ||
if (isNode) { | ||
let { readFileSync } = await import('fs') | ||
globalThis.fetch = async path => { | ||
path = `.${path}` | ||
const data = readFileSync(path, 'utf8') | ||
return { text() { return data } } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import t, { is, ok, same } from 'tst' | ||
import print from '../src/print.js' | ||
import { wat2wasm, file } from './compile.js' | ||
|
||
t.only('print: basics', () => { | ||
const tree = [ | ||
'func', ['export', '"double"'], ['param', 'f64', 'f32'], ['param', '$x', 'i32'], ['result', 'f64'], | ||
['f64.mul', ['local.get', 0], ['f64.const', 2]] | ||
] | ||
|
||
// minify | ||
const min = print(tree, { | ||
indent: false, | ||
newline: false, | ||
pad: false, | ||
comments: false | ||
}) | ||
wat2wasm(min) | ||
is(min, `(func (export "double")(param f64 f32)(param $x i32)(result f64)(f64.mul(local.get 0)(f64.const 2)))`) | ||
|
||
// pretty-print | ||
const pretty = print(tree, { | ||
indent: ' ', // indentation characters | ||
newline: '\n', // new line charactes | ||
pad: '', // pad start of each line with a string | ||
comments: true // keep comments | ||
}) | ||
wat2wasm(pretty) | ||
is(pretty, | ||
`(func (export "double") | ||
(param f64 f32)(param $x i32) | ||
(result f64) | ||
(f64.mul | ||
(local.get 0) | ||
(f64.const 2)))`) | ||
|
||
is( | ||
print(`(import "Math" "random" (func $random (result f32)))`), | ||
`(import \"Math\" \"random\"(func $random (result f32)))` | ||
) | ||
}) | ||
|
||
t.only('print: dino', async t => { | ||
let src = await file('./example/dino.wat') | ||
const dino = print(src) | ||
console.log(dino) | ||
|
||
wat2wasm(dino) | ||
}) |