Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dead code and console logs. Closes #108 #109

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 3 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,39 +278,6 @@

Snap2Js.parse.context = function(element) {
return AST.Node.from(element);
let receiver = null;
const receiverNode = element.childNamed('receiver');
if (receiverNode && receiverNode.children.length) { // create the context
receiver = receiverNode.children[0];
if (receiver.tag === 'ref') receiver = receiver.target;
this.parse(receiver);
}

// Add the execution code
const [inputs, variables, fnBody] = element.children;
// FIXME: How is "variables" used???
// Could this be used to store variables captured from the closure?
const type = fnBody.tag === 'script' ? 'reifyScript' : 'reifyReporter';
const node = new BuiltIn(null, type); // TODO: Make these custom types?
const fnNode = this.createAstNode(fnBody);
if (fnNode) {
node.addChild(fnNode);
} else {
node.addChild(new Block());
}
const inputNodes = inputs.children.map(item => this.createAstNode(item.contents));
const inputList = new BuiltIn(null, 'list'); // TODO: Make custom types?
inputNodes.forEach(node => inputList.addChild(node));
node.addChild(inputList);

node.simplify();

const receiverName = receiver && receiver.tag === 'sprite' ?
receiver.attributes.name : null;
const context = new BoundFunction(receiverName);
context.addChild(node);

return context;
};

Snap2Js.transpile = function(xml, pretty=false, options) {
Expand Down Expand Up @@ -431,40 +398,18 @@

let body = this.generateCodeFromState(this.state);

this.writeToDebugFile(body, elements[0].tag === 'context'); // REMOVE
let fn = new Function('__ENV', body);

return fn;
};

Snap2Js.writeToDebugFile = function(body, isContext) {
const content = [
'const fn = function (__ENV) {',
body,
'};',
'async function test(){',
isContext ?
'const f = await fn(require(\'.\').newContext());\nconsole.log(await f());' :
'console.log(await fn(require(\'.\').newContext()))',
'};',
'test();',
].join('\n');
fs.writeFileSync('code.js', content);
};

Snap2Js._flatten = function(node) {
let children = [];

for (let i = 0; i < node.children.length; i++) {
let child = node.children[i];
if (child.tag === 'l' && child.children.length === 1) {
//if (child.children.length === 1) {
children.push(this._flatten(child.children[0]));
//} else if (child.children.length) {
//console.log('children', children);
//children = children.concat(child.children
//.map(child => this._flatten(child)));
//}
children.push(this._flatten(child.children[0]));
} else if (child.tag === 'autolambda') {
children.push(this._flatten(child.children[0]));
} else {
Expand Down Expand Up @@ -523,9 +468,6 @@
.map((reference, i) => this.getReferencedValue(reference, i))
.reduce((l1, l2) => l1.concat(l2), []);

// Define the REFERENCES object?
//this.state.references.unshift();

this.state.references
.filter(reference => reference instanceof AST.Node)
.forEach(reference => reference.prepare(customBlockDefs, allowWarp));
Expand All @@ -546,7 +488,7 @@
// Sanitize all user entered values
const compileCustomBlock = block => {
block.name = utils.sanitize(block.name);
block.code = Snap2Js.generateCode(block.ast);
block.code = this.generateCode(block.ast);
};

this.state.customBlocks.forEach(compileCustomBlock);
Expand All @@ -569,8 +511,7 @@
};

Snap2Js.generateCode = function(node) {
if (!node.code) console.log('node is', node);
return node.code(Snap2Js._backend) || '';
return node.code(this._backend) || '';
};

Snap2Js._backend = {};
Expand Down
8 changes: 0 additions & 8 deletions src/backend-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ const callStatementWithArgs = function() {
return callFnWithArgs.apply(null, arguments) + '\n';
};

const callRawStatementWithArgs = function() {
return callRawFnWithArgs.apply(null, arguments) + '\n';
};

const newPromise = (value='') => `SPromise.resolve(${value})`;

module.exports = {
callRawFnWithArgs,
callFnWithArgs,
callRawStatementWithArgs,
callStatementWithArgs,
newPromise,
};
2 changes: 0 additions & 2 deletions src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const CALLER = '__SELF';
const callRawFnWithArgs = require('./backend-helpers').callRawFnWithArgs;
const callFnWithArgs = require('./backend-helpers').callFnWithArgs;
const callStatementWithArgs = require('./backend-helpers').callStatementWithArgs;
const callRawStatementWithArgs = require('./backend-helpers').callRawStatementWithArgs;
const newPromise = require('./backend-helpers').newPromise;

const backend = {};

Expand Down
13 changes: 0 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ const sanitize = function(text) {
return text;
};

const isWarping = function(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'doWarp') {
return true;
}
parent = parent.parent;
}

return false;
};

const defer = function () {
const deferred = {resolve: null, reject: null};
deferred.promise = new Promise((resolve, reject) => {
Expand All @@ -73,6 +61,5 @@ module.exports = {
parseSpec: parseSpec,
inputNames: inputNames,
sanitize,
isWarping,
defer,
};