Skip to content

Commit

Permalink
Merge 8959cc8 into 3970942
Browse files Browse the repository at this point in the history
  • Loading branch information
mohayonao committed Jul 5, 2014
2 parents 3970942 + 8959cc8 commit aca4b06
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 28 deletions.
5 changes: 0 additions & 5 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ window.onload = function() {
var beginTime, elapsedTime;

code = getCode(editor).trim();

while (/^\([\s\S]+\)$/.test(code)) {
code = code.slice(1, -1);
}

beginTime = now();

result = eval.call(null, SCScript.compile(code));
Expand Down
3 changes: 0 additions & 3 deletions demo/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ $(function() {
}

function update(code, mode) {
while (/^\([\s\S]+\)$/.test(code)) {
code = code.slice(1, -1);
}
if (prev[0] !== code || prev[1] !== mode) {
try {
switch (mode) {
Expand Down
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.67",
"version": "0.0.68",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
15 changes: 15 additions & 0 deletions src/sc/lang/compiler/codegen/block-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function(sc) {
"use strict";

require("./codegen");

var CodeGen = sc.lang.compiler.CodeGen;

CodeGen.addGenerateMethod("BlockExpression", function(node) {
var body = this.withFunction([], function() {
return this.generateStatements(node.body);
});

return [ "(", body, ")()" ];
});
})(sc);
46 changes: 46 additions & 0 deletions src/sc/lang/compiler/codegen/block-expr_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function() {
"use strict";

require("./");

var Syntax = sc.lang.compiler.Syntax;
var Token = sc.lang.compiler.Token;

describe("sc.lang.compiler.CodeGen", function() {
describe("BlockExpression", function() {
sc.test.codegen().each([
{
code: "(var a = 10; a)",
expected: "(function() { var $a; $a = $.Integer(10); return $a; })()",
ast: {
type: Syntax.BlockExpression,
body: [
{
type: Syntax.VariableDeclaration,
kind: "var",
declarations: [
{
type: Syntax.VariableDeclarator,
id: {
type: Syntax.Identifier,
name: "a"
},
init: {
type: Syntax.Literal,
value: "10",
valueType: Token.IntegerLiteral
}
}
]
},
{
type: Syntax.Identifier,
name: "a"
}
]
}
},
]);
});
});
})();
14 changes: 14 additions & 0 deletions src/sc/lang/compiler/codegen/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@
return result;
};

CodeGen.prototype.generateStatements = function(elements) {
var lastIndex = elements.length - 1;

return elements.map(function(item, i) {
var stmt = this.generate(item);

if (i === lastIndex) {
stmt = [ "return ", stmt ];
}

return [ stmt, ";" ];
}, this);
};

CodeGen.prototype.useTemporaryVariable = function(func) {
var result;
var tempName = "_ref" + this.state.tempVarId;
Expand Down
8 changes: 8 additions & 0 deletions src/sc/lang/compiler/codegen/codegen_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
test = toCompiledString(test);
expect(test).to.equal("[]");
});
it("generateStatements", function() {
var codegen = new CodeGen();

var test;
test = codegen.generateStatements([ "a", "b", "c" ]);
test = toCompiledString(test);
expect(test).to.equal("$a;$b;return $c;");
});
it("useTemporaryVariable", function() {
var codegen = new CodeGen();

Expand Down
1 change: 1 addition & 0 deletions src/sc/lang/compiler/codegen/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("./codegen");
require("./assignment-expr");
require("./binop-expr");
require("./block-expr");
require("./call-expr");
require("./envir-expr");
require("./event-expr");
Expand Down
16 changes: 1 addition & 15 deletions src/sc/lang/compiler/codegen/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

var body = this.withFunction([ "" ], function() { // "" compiled as $
return generateStatements(this, node.body);
return this.generateStatements(node.body);
});

var result = [ "(", body, ")" ];
Expand All @@ -22,18 +22,4 @@

return result;
});

function generateStatements(that, elements) {
var lastIndex = elements.length - 1;

return elements.map(function(item, i) {
var stmt = that.generate(item);

if (i === lastIndex) {
stmt = [ "return ", stmt ];
}

return [ stmt, ";" ];
});
}
})(sc);
1 change: 1 addition & 0 deletions src/sc/lang/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
Syntax: {
AssignmentExpression: "AssignmentExpression",
BlockExpression: "BlockExpression",
BinaryExpression: "BinaryExpression",
CallExpression: "CallExpression",
FunctionExpression: "FunctionExpression",
Expand Down
6 changes: 6 additions & 0 deletions src/sc/lang/compiler/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
}
return node;
},
createBlockExpression: function(body) {
return {
type: Syntax.BlockExpression,
body: body
};
},
createCallExpression: function(callee, method, args, stamp) {
return {
type: Syntax.CallExpression,
Expand Down
24 changes: 24 additions & 0 deletions src/sc/lang/compiler/parser/block-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(function(sc) {
"use strict";

require("./parser");

var Node = sc.lang.compiler.Node;
var Parser = sc.lang.compiler.Parser;

Parser.addParseMethod("BlockExpression", function() {
var marker = this.createMarker();

this.expect("(");

var expr = this.withScope(function() {
return Node.createBlockExpression(
this.parseFunctionBody()
);
});

this.expect(")");

return marker.update().apply(expr);
});
})(sc);
69 changes: 69 additions & 0 deletions src/sc/lang/compiler/parser/block-expr_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(function() {
"use strict";

require("./");

var Syntax = sc.lang.compiler.Syntax;
var Token = sc.lang.compiler.Token;
var Message = sc.lang.compiler.Message;
var strlib = sc.libs.strlib;

describe("sc.lang.compiler.Parser", function() {
describe("parseBlockExpression", function() {
sc.test.compile(this.title).each({
"(var a = 10;)": sc.test.OK,
"var a = 10;": strlib.format(Message.UnexpectedKeyword, "var"),
});
sc.test.parse(this.title).each({
"(var a = 10;)": {
type: Syntax.BlockExpression,
body: [
{
type: Syntax.VariableDeclaration,
kind: "var",
declarations: [
{
type: Syntax.VariableDeclarator,
id: {
type: Syntax.Identifier,
name: "a",
range: [ 5, 6 ],
loc: {
start: { line: 1, column: 5 },
end: { line: 1, column: 6 }
}
},
init: {
type: Syntax.Literal,
value: "10",
valueType: Token.IntegerLiteral,
range: [ 9, 11 ],
loc: {
start: { line: 1, column: 9 },
end: { line: 1, column: 11 }
}
},
range: [ 5, 11 ],
loc: {
start: { line: 1, column: 5 },
end: { line: 1, column: 11 }
}
}
],
range: [ 1, 11 ],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 11 }
},
}
],
range: [ 0, 13 ],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 13 }
}
}
});
});
});
})();
1 change: 1 addition & 0 deletions src/sc/lang/compiler/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("./parser");
require("./assignment-expr");
require("./binop-expr");
require("./block-expr");
require("./braces");
require("./call-expr");
require("./envir-expr");
Expand Down
5 changes: 5 additions & 0 deletions src/sc/lang/compiler/parser/parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
return this.parseEventExpression();
};
}
if (this.match("var")) {
return function() {
return this.parseBlockExpression();
};
}
if (this.match("..")) {
return function() {
return this.parseSeriesExpression();
Expand Down
2 changes: 1 addition & 1 deletion src/sc/lang/compiler/parser/parentheses_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"(0;1..10)": sc.test.OK, // SeriesExpression
"(1;2)": sc.test.OK, // Expressions
"(1)": sc.test.OK, // Expression
"(var a;)": sc.test.OK, // BlockExpression
"[1]": strlib.format(Message.UnexpectedToken, "["),
"(var a;)": Message.UnexpectedKeyword,
});
sc.test.parse(this.title).each({
"()": {
Expand Down
9 changes: 9 additions & 0 deletions src/sc/lang/compiler/parser/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require("./parser");

var Syntax = sc.lang.compiler.Syntax;
var Node = sc.lang.compiler.Node;
var Parser = sc.lang.compiler.Parser;

Expand All @@ -15,6 +16,14 @@
);
});

if (hasSingleBlockExpression(node)) {
node.body = node.body[0].body;
}

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

function hasSingleBlockExpression(node) {
return node.body.length === 1 && node.body[0].type === Syntax.BlockExpression;
}
})(sc);
38 changes: 38 additions & 0 deletions src/sc/lang/compiler/parser/program_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@
start: { line: 1, column: 0 },
end: { line: 1, column: 6 },
}
},
"(var a;)": {
type: Syntax.Program,
body: [
{
type: Syntax.VariableDeclaration,
declarations: [
{
type: Syntax.VariableDeclarator,
id: {
type: Syntax.Identifier,
name: "a",
range: [ 5, 6 ],
loc: {
start: { line: 1, column: 5 },
end: { line: 1, column: 6 },
}
},
range: [ 5, 6 ],
loc: {
start: { line: 1, column: 5 },
end: { line: 1, column: 6 },
}
}
],
kind: "var",
range: [ 1, 6 ],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 6 },
}
}
],
range: [ 0, 8 ],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 8 },
}
}
});
});
Expand Down
3 changes: 0 additions & 3 deletions src/sc/node-cli/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ function toString(obj) {
var replOptions = {
prompt: "scsc> ",
eval: function(input, context, filename, callback) {
while (/^\([\s\S]+\)$/.test(input)) {
input = input.slice(1, -1).trim();
}
try {
var js = SCScript.compile(input, { loc: true, range: true });
var result;
Expand Down

0 comments on commit aca4b06

Please sign in to comment.