Add activate; remove ground dataspace

This commit is contained in:
Tony Garnock-Jones 2018-11-01 23:35:11 +00:00
parent 3576433ed1
commit 9e84b67252
6 changed files with 112 additions and 75 deletions

12
TODO.md Normal file
View File

@ -0,0 +1,12 @@
- `during`
- `define/query`
- [DONE] activation
- web pack
- drivers
- timer
- alert
- `defer` statement
- [DONE] remove ground dataspace syntax
- some kind of `stop facet` statement
- put a Symbol on the fields blob?
- `react`

View File

@ -30,18 +30,6 @@ export function SpawnStatement(node) {
this.printBlock(node); this.printBlock(node);
} }
export function GroundDataspaceStatement(node) {
this.word("ground");
this.space();
this.word("dataspace");
this.space();
if (this.id) {
this.print(node.id, node);
this.space();
}
this.print(node.body, node);
}
export function FieldDeclarationStatement(node) { export function FieldDeclarationStatement(node) {
this.word("field"); this.word("field");
this.space(); this.space();
@ -128,3 +116,9 @@ export function MessageSendStatement(node) {
this.print(node.body, node); this.print(node.body, node);
this.semicolon(); this.semicolon();
} }
export function ActivationExpression(node) {
this.word("activate");
this.space();
this.print(node.moduleExpr, node);
}

View File

@ -21,22 +21,27 @@
// syntactic extensions to JS. // syntactic extensions to JS.
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// (0) Replace the `isStatement` function with a non-hard-coded // (0) Replace the `isStatement` and `isExpression` functions with
// version that checks the contents of FLIPPED_ALIAS_KEYS at the time // non-hard-coded versions that check the contents of
// of the call. // FLIPPED_ALIAS_KEYS at the time of each call.
// //
// This allows our later extensions to be picked up correctly. // This allows our later extensions to be picked up correctly.
// //
var Validators = require("@babel/types/lib/validators/generated"); var Validators = require("@babel/types/lib/validators/generated");
var shallowEqual = require("@babel/types/lib/utils/shallowEqual"); var shallowEqual = require("@babel/types/lib/utils/shallowEqual");
var _isStatement = Validators.isStatement;
Validators.isStatement = function (node, opts) { function _isX(X, previous) {
if (node && Types.FLIPPED_ALIAS_KEYS["Statement"].indexOf(node.type) !== -1) { return (node, opts) => {
return typeof opts === "undefined" || shallowEqual.default(node, opts); if (node && Types.FLIPPED_ALIAS_KEYS[X].indexOf(node.type) !== -1) {
} else { return typeof opts === "undefined" || shallowEqual.default(node, opts);
return _isStatement(node, opts); } else {
} return previous(node, opts);
}; }
};
}
Validators.isStatement = _isX("Statement", Validators.isStatement);
Validators.isExpression = _isX("Expression", Validators.isExpression);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// (1) Load the core parser in modifiable form. // (1) Load the core parser in modifiable form.

View File

@ -19,6 +19,36 @@
import { _original_Parser, tokTypes as tt } from "@babel/parser"; import { _original_Parser, tokTypes as tt } from "@babel/parser";
export default class SyndicateParser extends _original_Parser { export default class SyndicateParser extends _original_Parser {
// Overrides ExpressionParser.parseMaybeAssign
parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
let previousError = null;
if (this.isContextual("activate")) {
let result = this.withBacktracking(
() => {
this.next();
const node = this.startNode();
node.moduleExpr = this.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
return this.finishNode(node, "ActivationExpression");
},
(err) => {
previousError = err;
return null;
});
if (result) return result;
}
try {
return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
} catch (err) {
if (err instanceof SyntaxError && previousError && previousError.pos >= err.pos) {
throw previousError;
} else {
throw err;
}
}
}
// Overrides StatementParser.parseStatementContent // Overrides StatementParser.parseStatementContent
parseStatementContent(declaration, topLevel) { parseStatementContent(declaration, topLevel) {
let previousError = null; let previousError = null;
@ -57,17 +87,6 @@ export default class SyndicateParser extends _original_Parser {
return this.finishNode(node, "FieldDeclarationStatement"); return this.finishNode(node, "FieldDeclarationStatement");
} }
if (this.isContextual("ground")) {
this.next();
this.expectContextual("dataspace");
const node = this.startNode();
if (this.match(tt.name)) {
node.id = this.parseIdentifier();
}
node.body = this.parseStatement();
return this.finishNode(node, "GroundDataspaceStatement");
}
if (this.isContextual("spawn")) { if (this.isContextual("spawn")) {
this.next(); this.next();
const node = this.startNode(); const node = this.startNode();

View File

@ -24,7 +24,7 @@ import traverse from "@babel/traverse";
function maybeTerminalWrap(state, terminal, ast) { function maybeTerminalWrap(state, terminal, ast) {
if (terminal) { if (terminal) {
return template(`DATASPACE.currentFacet().stop(() => { AST })`)({ return template(`DATASPACE._currentFacet.stop(() => { AST })`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
AST: ast AST: ast
}); });
@ -172,7 +172,7 @@ function translateEndpoint(state, path, expectedEvt) {
let _evt = path.scope.generateUidIdentifier("evt"); let _evt = path.scope.generateUidIdentifier("evt");
let _vs = path.scope.generateUidIdentifier("vs"); let _vs = path.scope.generateUidIdentifier("vs");
path.replaceWith(template( path.replaceWith(template(
`DATASPACE.currentFacet().addEndpoint(function () { `DATASPACE._currentFacet.addEndpoint(function () {
let HANDLER = { let HANDLER = {
skeleton: SKELETON, skeleton: SKELETON,
constPaths: CONSTPATHS, constPaths: CONSTPATHS,
@ -181,7 +181,7 @@ function translateEndpoint(state, path, expectedEvt) {
callback: DATASPACE.wrap((EVT, VS) => { callback: DATASPACE.wrap((EVT, VS) => {
if (EVT === EXPECTED) { if (EVT === EXPECTED) {
INITS; INITS;
DATASPACE.currentFacet().actor.scheduleScript(() => { DATASPACE._currentFacet.actor.scheduleScript(() => {
BODY; BODY;
}); });
} }
@ -218,6 +218,7 @@ export default declare((api, options) => {
visitor: { visitor: {
Program(path, state) { Program(path, state) {
let savedGlobalFacetUid = path.scope.generateUidIdentifier("savedGlobalFacet");
state.ImmutableID = path.scope.generateUidIdentifier("Immutable"); state.ImmutableID = path.scope.generateUidIdentifier("Immutable");
state.SyndicateID = path.scope.generateUidIdentifier("Syndicate"); state.SyndicateID = path.scope.generateUidIdentifier("Syndicate");
state.DataspaceID = path.scope.generateUidIdentifier("Dataspace"); state.DataspaceID = path.scope.generateUidIdentifier("Dataspace");
@ -229,12 +230,30 @@ export default declare((api, options) => {
const IMMUTABLE = SYNDICATE.Immutable; const IMMUTABLE = SYNDICATE.Immutable;
const DATASPACE = SYNDICATE.Dataspace; const DATASPACE = SYNDICATE.Dataspace;
const SKELETON = SYNDICATE.Skeleton; const SKELETON = SYNDICATE.Skeleton;
const STRUCT = SYNDICATE.Struct;`)({ const STRUCT = SYNDICATE.Struct;
let SAVEDGLOBALFACET = DATASPACE._currentFacet;
DATASPACE._currentFacet = new SYNDICATE._Dataspace.ActionCollector();`)({
IMMUTABLE: state.ImmutableID, IMMUTABLE: state.ImmutableID,
SYNDICATE: state.SyndicateID, SYNDICATE: state.SyndicateID,
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
SKELETON: state.SkeletonID, SKELETON: state.SkeletonID,
STRUCT: state.StructID, STRUCT: state.StructID,
SAVEDGLOBALFACET: savedGlobalFacetUid,
}));
path.pushContainer(
'body',
template(`module.exports[DATASPACE.BootSteps] = {
module: module,
steps: DATASPACE._currentFacet.actions
};
DATASPACE._currentFacet = SAVEDGLOBALFACET;
SAVEDGLOBALFACET = null;
if (require.main === module) {
SYNDICATE.bootModule(module);
}`)({
DATASPACE: state.DataspaceID,
SYNDICATE: state.SyndicateID,
SAVEDGLOBALFACET: savedGlobalFacetUid,
})); }));
}, },
@ -247,22 +266,6 @@ export default declare((api, options) => {
})); }));
}, },
GroundDataspaceStatement(path, state) {
const { node } = path;
if (node.id) {
path.replaceWith(template(`let G = (new SYNDICATE.Ground.Ground(function () { BODY }).start());`)({
G: node.id,
SYNDICATE: state.SyndicateID,
BODY: node.body
}));
} else {
path.replaceWith(template(`(new SYNDICATE.Ground.Ground(function () { BODY }).start());`)({
SYNDICATE: state.SyndicateID,
BODY: node.body
}));
}
},
FieldDeclarationStatement(path, state) { FieldDeclarationStatement(path, state) {
const { node } = path; const { node } = path;
let prop = node.member.computed let prop = node.member.computed
@ -279,7 +282,7 @@ export default declare((api, options) => {
AssertionEndpointStatement(path, state) { AssertionEndpointStatement(path, state) {
const { node } = path; const { node } = path;
if (node.test) { if (node.test) {
path.replaceWith(template(`DATASPACE.currentFacet().addEndpoint(function () { path.replaceWith(template(`DATASPACE._currentFacet.addEndpoint(function () {
return (TEST) ? [TEMPLATE, null] : [void 0, null]; return (TEST) ? [TEMPLATE, null] : [void 0, null];
});`)({ });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
@ -287,7 +290,7 @@ export default declare((api, options) => {
TEMPLATE: node.template, TEMPLATE: node.template,
})); }));
} else { } else {
path.replaceWith(template(`DATASPACE.currentFacet().addEndpoint(function () { path.replaceWith(template(`DATASPACE._currentFacet.addEndpoint(function () {
return [TEMPLATE, null]; return [TEMPLATE, null];
});`)({ });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
@ -298,7 +301,7 @@ export default declare((api, options) => {
DataflowStatement(path, state) { DataflowStatement(path, state) {
const { node } = path; const { node } = path;
path.replaceWith(template(`DATASPACE.currentFacet().addDataflow(function () { BODY });`)({ path.replaceWith(template(`DATASPACE._currentFacet.addDataflow(function () { BODY });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
BODY: node.body, BODY: node.body,
})); }));
@ -308,7 +311,7 @@ export default declare((api, options) => {
const { node } = path; const { node } = path;
switch (node.triggerType) { switch (node.triggerType) {
case "dataflow": case "dataflow":
path.replaceWith(template(`DATASPACE.currentFacet().addDataflow(function () { path.replaceWith(template(`DATASPACE._currentFacet.addDataflow(function () {
if (PATTERN) { BODY } if (PATTERN) { BODY }
});`)({ });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
@ -336,14 +339,14 @@ export default declare((api, options) => {
PseudoEventHandler(path, state) { PseudoEventHandler(path, state) {
const { node } = path; const { node } = path;
if (node.triggerType === "start") { if (node.triggerType === "start") {
path.replaceWith(template(`DATASPACE.currentFacet().actor.scheduleScript(() => { path.replaceWith(template(`DATASPACE._currentFacet.actor.scheduleScript(() => {
BODY; BODY;
});`)({ });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
BODY: node.body, BODY: node.body,
})); }));
} else { } else {
path.replaceWith(template(`DATASPACE.currentFacet().addStopScript(function () { path.replaceWith(template(`DATASPACE._currentFacet.addStopScript(function () {
BODY; BODY;
});`)({ });`)({
DATASPACE: state.DataspaceID, DATASPACE: state.DataspaceID,
@ -369,6 +372,14 @@ export default declare((api, options) => {
BODY: node.body BODY: node.body
})); }));
}, },
ActivationExpression(path, state) {
const { node } = path;
path.replaceWith(template.expression(`DATASPACE.activate(MODULE)`)({
DATASPACE: state.DataspaceID,
MODULE: node.moduleExpr,
}));
},
}, },
}; };
}); });

View File

@ -39,21 +39,6 @@ defineType("SpawnStatement", {
}, },
}); });
defineType("GroundDataspaceStatement", {
builder: ["id", "body"],
visitor: ["id", "body"],
aliases: ["Statement"],
fields: {
id: {
validate: assertNodeType("Identifier"),
optional: true,
},
body: {
validate: assertNodeType("Statement"),
},
},
});
defineType("FieldDeclarationStatement", { defineType("FieldDeclarationStatement", {
builder: ["member", "init"], builder: ["member", "init"],
visitor: ["member", "init"], visitor: ["member", "init"],
@ -163,3 +148,14 @@ defineType("MessageSendStatement", {
}, },
}, },
}); });
defineType("ActivationExpression", {
builder: ["moduleExpr"],
visitor: ["moduleExpr"],
aliases: ["Expression"],
fields: {
moduleExpr: {
validate: assertNodeType("Expression"),
},
},
});