wazum
wazum copied to clipboard
idea: ability to construct nodes with their dependencies provided later
Some output generators do things like this:
output.append('(func $', func.name) // the function is not complete yet
// ...
for (const param of func.parameters) output.append(' (param $', param.name, ' ', param.type!.primitive, ')')
// ...
if (func.returnType) output.append(' (result ', func.returnType.primitive, ')', NL)
// ...
output.append('(', type.$type, '.', operator, NL)
output.indent()
output.append('(local.get $', exp.leftOperand.$cstNode?.text, ')')
output.append('(local.get $', exp.rightOperand.$cstNode?.text, ')')
output.append(NL, ')')
// ...
output.append(NL, ')') // the function is complete
Here the function header is added, then parameters and return type are added, then the body is added.
With current API the function inputs are required (same with binaryen).
For example, the call to w.func
expects all the thing the function needs up front. Trying to convert from the procedural string-based code can be tricky, needs some refactoring.
Would it be nice if the parts of a node can be added after creation? f.e.
const func = w.func()
func.addParam(param)
func.addParam(param)
func.addParam(param)
func.setReturn(return)
This would mean that the state of the function would be, for some time, incomplete. Not sure if you want to have incomplete types (f.e. null
for return type if it isn't yet specified, etc). But it might sequential patterns easier. (I'm no expert in this area, learning language design, playing with techniques.)