Browse Source

Add activate; remove ground dataspace

bonjour-mdns-experiment
Tony Garnock-Jones 4 years ago
parent
commit
9e84b67252
  1. 12
      TODO.md
  2. 18
      src/generators.js
  3. 27
      src/index.js
  4. 41
      src/parser.js
  5. 63
      src/plugin.js
  6. 26
      src/types.js

12
TODO.md

@ -0,0 +1,12 @@ @@ -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`

18
src/generators.js

@ -30,18 +30,6 @@ export function SpawnStatement(node) { @@ -30,18 +30,6 @@ export function SpawnStatement(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) {
this.word("field");
this.space();
@ -128,3 +116,9 @@ export function MessageSendStatement(node) { @@ -128,3 +116,9 @@ export function MessageSendStatement(node) {
this.print(node.body, node);
this.semicolon();
}
export function ActivationExpression(node) {
this.word("activate");
this.space();
this.print(node.moduleExpr, node);
}

27
src/index.js

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

41
src/parser.js

@ -19,6 +19,36 @@ @@ -19,6 +19,36 @@
import { _original_Parser, tokTypes as tt } from "@babel/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
parseStatementContent(declaration, topLevel) {
let previousError = null;
@ -57,17 +87,6 @@ export default class SyndicateParser extends _original_Parser { @@ -57,17 +87,6 @@ export default class SyndicateParser extends _original_Parser {
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")) {
this.next();
const node = this.startNode();

63
src/plugin.js

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

26
src/types.js

@ -39,21 +39,6 @@ defineType("SpawnStatement", { @@ -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", {
builder: ["member", "init"],
visitor: ["member", "init"],
@ -163,3 +148,14 @@ defineType("MessageSendStatement", { @@ -163,3 +148,14 @@ defineType("MessageSendStatement", {
},
},
});
defineType("ActivationExpression", {
builder: ["moduleExpr"],
visitor: ["moduleExpr"],
aliases: ["Expression"],
fields: {
moduleExpr: {
validate: assertNodeType("Expression"),
},
},
});

Loading…
Cancel
Save