Skip to content

Commit

Permalink
Merge pull request #80 from mohayonao/code-cleanup
Browse files Browse the repository at this point in the history
cleanup code
  • Loading branch information
mohayonao committed Jun 16, 2014
2 parents 4ddc81f + 13522dd commit 60be1c4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scscript",
"version": "0.0.57",
"version": "0.0.58",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
81 changes: 44 additions & 37 deletions src/sc/lang/bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,62 +114,69 @@

Bytecode.prototype.runAsRoutine = function(args) {
var result;
var code, length, iter;
var skip;

this.setParent(bytecode.current);

bytecode.current = this;

if (this._child) {
result = this._child.runAsRoutine(args);
if (this.state === sc.STATE_SUSPENDED) {
skip = true;
if (this.state !== sc.STATE_SUSPENDED) {
result = null;
}
}

if (!skip) {
code = this._code;
length = this._length;
iter = this._iter;

this.state = sc.STATE_RUNNING;
this.result = null;
while (this._index < length) {
if (iter && this._index === 0) {
args = iter.next();
if (args === null) {
this.state = sc.STATE_SUSPENDED;
this._index = length;
break;
}
}
if (iter && !iter.hasNext) {
iter = null;
}
if (!result) {
result = this._runAsRoutine(args);
}

result = code[this._index].apply(this, args);
bytecode.current = this._parent;

this._index += 1;
if (this._index >= length) {
if (iter) {
this._index = 0;
} else {
this.state = sc.STATE_SUSPENDED;
}
}
this.advance();

return this.result ? $.Nil() : result;
};

Bytecode.prototype._runAsRoutine = function(args) {
var result;
var code, length, iter;

if (this.state !== sc.STATE_RUNNING) {
code = this._code;
length = this._length;
iter = this._iter;

this.state = sc.STATE_RUNNING;
this.result = null;
while (this._index < length) {
if (iter && this._index === 0) {
args = iter.next();
if (args === null) {
this.state = sc.STATE_SUSPENDED;
this._index = length;
break;
}
}
}
if (iter && !iter.hasNext) {
iter = null;
}

bytecode.current = this._parent;
result = code[this._index].apply(this, args);

this.advance();
this._index += 1;
if (this._index >= length) {
if (iter) {
this._index = 0;
} else {
this.state = sc.STATE_SUSPENDED;
}
}

return this.result ? $.Nil() : result;
if (this.state !== sc.STATE_RUNNING) {
break;
}
}

return result;
};

Bytecode.prototype.advance = function() {
Expand Down
159 changes: 86 additions & 73 deletions src/sc/lang/compiler/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,40 +389,53 @@
return node;
};


SCParser.prototype.parseSimpleAssignmentExpression = function() {
var node, left, right, token, methodName, marker;
var node;

node = left = this.parsePartialExpression();
node = this.parsePartialExpression();

if (this.match("=")) {
if (node.type === Syntax.CallExpression) {
marker = Marker.create(this.lexer, left);

token = this.lex();
right = this.parseAssignmentExpression();
methodName = left.method.name + "_";
left.method.name = methodName;
left.args.list = node.args.list.concat(right);
if (left.stamp !== "[") {
left.stamp = "=";
}
node = marker.update().apply(left, true);
node = this._parseSimpleAssignmentCallExpression(node);
} else {
if (!isLeftHandSide(left)) {
this.throwError(left, Message.InvalidLHSInAssignment);
}

token = this.lex();
right = this.parseAssignmentExpression();
node = Node.createAssignmentExpression(
token.value, left, right
);
node = this._parseSimpleAssignmentExpression(node);
}
}

return node;
};

SCParser.prototype._parseSimpleAssignmentCallExpression = function(node) {
var right, token, methodName, marker;

marker = Marker.create(this.lexer, node);

token = this.lex();
right = this.parseAssignmentExpression();
methodName = node.method.name + "_";
node.method.name = methodName;
node.args.list = node.args.list.concat(right);
if (node.stamp !== "[") {
node.stamp = "=";
}

return marker.update().apply(node, true);
};

SCParser.prototype._parseSimpleAssignmentExpression = function(node) {
var right, token;

if (!isLeftHandSide(node)) {
this.throwError(node, Message.InvalidLHSInAssignment);
}

token = this.lex();
right = this.parseAssignmentExpression();

return Node.createAssignmentExpression(token.value, node, right);
};

SCParser.prototype.parseDestructuringAssignmentLeft = function() {
var params = { list: [] }, element;

Expand Down Expand Up @@ -622,7 +635,7 @@
var blocklist, stamp;

marker = Marker.create(this.lexer);
expr = this.parsePrimaryExpression(node);
expr = this.parseEnvironmentExpression(node);

blocklist = false;

Expand Down Expand Up @@ -659,23 +672,12 @@

SCParser.prototype.parseLeftHandSideParenthesis = function(expr) {
if (isClassName(expr)) {
return this.parseLeftHandSideClassNew(expr);
return this.parseLeftHandSideAbbreviatedMethodCall(expr, "new", "(");
}

return this.parseLeftHandSideMethodCall(expr);
};

SCParser.prototype.parseLeftHandSideClassNew = function(expr) {
var method, args;

method = Node.createIdentifier("new");
method = Marker.create(this.lexer).apply(method);

args = this.parseCallArgument();

return Node.createCallExpression(expr, method, args, "(");
};

SCParser.prototype.parseLeftHandSideMethodCall = function(expr) {
var method, args, lookahead;

Expand All @@ -702,6 +704,17 @@
return Node.createCallExpression(expr, method, args, "(");
};

SCParser.prototype.parseLeftHandSideAbbreviatedMethodCall = function(expr, methodName, stamp) {
var method, args;

method = Node.createIdentifier(methodName);
method = Marker.create(this.lexer).apply(method);

args = this.parseCallArgument();

return Node.createCallExpression(expr, method, args, stamp);
};

SCParser.prototype.parseLeftHandSideClosedBrace = function(expr) {
this.lex();
if (!this.match("{")) {
Expand Down Expand Up @@ -800,7 +813,7 @@

if (this.match("(")) {
// expr.()
return this.parseLeftHandSideDotValue(expr);
return this.parseLeftHandSideAbbreviatedMethodCall(expr, "value", ".");
} else if (this.match("[")) {
// expr.[0]
return this.parseLeftHandSideDotBracket(expr);
Expand All @@ -817,17 +830,6 @@
return Node.createCallExpression(expr, method, { list: [] });
};

SCParser.prototype.parseLeftHandSideDotValue = function(expr) {
var method, args;

method = Node.createIdentifier("value");
method = Marker.create(this.lexer).apply(method);

args = this.parseCallArgument();

return Node.createCallExpression(expr, method, args, ".");
};

SCParser.prototype.parseLeftHandSideDotBracket = function(expr) {
var method, marker;

Expand Down Expand Up @@ -1031,8 +1033,8 @@
return marker.update().apply(expr);
};

SCParser.prototype.parsePrimaryExpression = function(node) {
var expr, stamp;
SCParser.prototype.parseEnvironmentExpression = function(node) {
var expr;
var marker;

if (node) {
Expand All @@ -1049,30 +1051,41 @@
}
expr = Node.createEnvironmentExpresion(expr);
} else {
stamp = this.matchAny([ "(", "{", "[", "#" ]) || this.lookahead.type;
switch (stamp) {
case "(":
expr = this.parseParentheses();
break;
case "{":
expr = this.parseBraces();
break;
case "[":
expr = this.parseListInitialiser();
break;
case Token.Keyword:
expr = this.parsePrimaryKeywordExpression();
break;
case Token.Identifier:
expr = this.parsePrimaryIdentifier();
break;
case Token.StringLiteral:
expr = this.parsePrimaryStringExpression();
break;
default:
expr = this.parseArgumentableValue(stamp);
break;
}
expr = this.parsePrimaryExpression();
}

return marker.update().apply(expr);
};

SCParser.prototype.parsePrimaryExpression = function() {
var expr, stamp;
var marker;

marker = Marker.create(this.lexer);
stamp = this.matchAny([ "(", "{", "[", "#" ]) || this.lookahead.type;

switch (stamp) {
case "(":
expr = this.parseParentheses();
break;
case "{":
expr = this.parseBraces();
break;
case "[":
expr = this.parseListInitialiser();
break;
case Token.Keyword:
expr = this.parsePrimaryKeywordExpression();
break;
case Token.Identifier:
expr = this.parsePrimaryIdentifier();
break;
case Token.StringLiteral:
expr = this.parsePrimaryStringExpression();
break;
default:
expr = this.parseArgumentableValue(stamp);
break;
}

return marker.update().apply(expr);
Expand Down

0 comments on commit 60be1c4

Please sign in to comment.