spawn :let ID = INIT :let ID = INIT ... { ... }

This commit is contained in:
Tony Garnock-Jones 2018-11-05 17:48:26 +00:00
parent 51b90071fb
commit 43b5987e8b
6 changed files with 47 additions and 5 deletions

View File

@ -5,6 +5,7 @@
- [DONE] `spawn*` or similar - looks like `spawn on start { ... }` will do the trick
- [DONE] activation
- [DONE] remove ground dataspace syntax
- [DONE] `spawn :let childVal = parentCalculation { ... }`
- `defer` statement
- `define/query`

View File

@ -32,6 +32,16 @@ export function SpawnStatement(node) {
this.space();
this.print(a, node);
}
for (let i = 0; i < node.parentIds.length; i++) {
this.space();
this.token(":let");
this.space();
this.print(node.parentIds[i], node);
this.space();
this.token("=");
this.space();
this.print(node.parentInits[i], node);
}
this.space();
this.printBlock(node.bootProc);
}

View File

@ -89,6 +89,7 @@ Array.prototype.push.apply(Types.TYPES, Object.keys(Types.DEPRECATED_KEYS));
//
Types.getBindingIdentifiers.keys.EventHandlerEndpoint = ["captureIds"];
Types.getBindingIdentifiers.keys.DuringStatement = ["captureIds"];
Types.getBindingIdentifiers.keys.SpawnStatement = ["parentIds"];
//---------------------------------------------------------------------------
// (3) Install our modified parser in place of the core parser.

View File

@ -202,10 +202,24 @@ export default class SyndicateParser extends _original_Parser {
node.name = this.parseExpression();
}
node.initialAssertions = [];
node.parentIds = [];
node.parentInits = [];
while (this.match(tt.colon)) {
this.next();
this.expectContextual("asserting");
if (this.isContextual("asserting")) {
this.next();
node.initialAssertions.push(this.parseExpression());
} else if (this.state.type === tt._let) {
this.next();
const id = this.parseBindingAtom();
this.checkLVal(id, true, undefined, "spawn :let declaration");
this.expect(tt.eq);
const init = this.parseMaybeAssign(false);
node.parentIds.push(id);
node.parentInits.push(init);
} else {
this.unexpected();
}
}
node.bootProc = this.parseSyntheticFunctionStatement();
return this.finishNode(node, "SpawnStatement");

View File

@ -348,6 +348,16 @@ export default declare((api, options) => {
SEQ: t.arrayExpression(node.initialAssertions)
}),
}));
if (node.parentIds.length > 0) {
const stmts = [];
for (let i = 0; i < node.parentIds.length; i++) {
const id = node.parentIds[i];
const init = node.parentInits[i];
stmts.push(template(`const ID = INIT;`)({ ID: id, INIT: init }));
}
stmts.push(path.node);
path.replaceWith(t.blockStatement(stmts, []));
}
},
FieldDeclarationStatement(path, state) {

View File

@ -25,9 +25,9 @@ import defineType, {
} from "@babel/types/lib/definitions/utils";
defineType("SpawnStatement", {
builder: ["name", "initialAssertions", "bootProc"],
visitor: ["name", "initialAssertions", "bootProc"],
aliases: ["Statement"],
builder: ["name", "initialAssertions", "parentIds", "parentInits", "bootProc"],
visitor: ["name", "initialAssertions", "parentIds", "parentInits", "bootProc"],
aliases: ["Statement", "Scopable"],
fields: {
name: {
validate: assertNodeType("Expression"),
@ -39,6 +39,12 @@ defineType("SpawnStatement", {
assertEach(assertNodeType("Expression")),
),
},
parentIds: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Identifier")),),
},
parentInits: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Expression")),),
},
bootProc: {
validate: assertNodeType("FunctionExpression"),
},