Browse Source

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

bonjour-mdns-experiment
Tony Garnock-Jones 4 years ago
parent
commit
43b5987e8b
  1. 1
      TODO.md
  2. 10
      packages/syntax/src/generators.js
  3. 1
      packages/syntax/src/index.js
  4. 18
      packages/syntax/src/parser.js
  5. 10
      packages/syntax/src/plugin.js
  6. 12
      packages/syntax/src/types.js

1
TODO.md

@ -5,6 +5,7 @@ @@ -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`

10
packages/syntax/src/generators.js

@ -32,6 +32,16 @@ export function SpawnStatement(node) { @@ -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);
}

1
packages/syntax/src/index.js

@ -89,6 +89,7 @@ Array.prototype.push.apply(Types.TYPES, Object.keys(Types.DEPRECATED_KEYS)); @@ -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.

18
packages/syntax/src/parser.js

@ -202,10 +202,24 @@ export default class SyndicateParser extends _original_Parser { @@ -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");
node.initialAssertions.push(this.parseExpression());
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");

10
packages/syntax/src/plugin.js

@ -348,6 +348,16 @@ export default declare((api, options) => { @@ -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) {

12
packages/syntax/src/types.js

@ -25,9 +25,9 @@ import defineType, { @@ -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", { @@ -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"),
},

Loading…
Cancel
Save