Skip to content

Commit

Permalink
Merge pull request #27 from mohayonao/refactoring
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
mohayonao committed May 4, 2014
2 parents 5e3cd7e + 05a3cf3 commit 43d6111
Show file tree
Hide file tree
Showing 27 changed files with 12,175 additions and 12,015 deletions.
20,484 changes: 10,235 additions & 10,249 deletions build/scscript.js

Large diffs are not rendered by default.

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.18",
"version": "0.0.19",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
4 changes: 1 addition & 3 deletions src/sc/lang/classlib.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function(sc) {
"use strict";

require("./klass");
require("./klass-constructors");
require("./klass-utils");
require("./dollarSC");
require("./klass");
require("./iterator");
require("./fn");
require("../libs/random");
Expand Down
187 changes: 3 additions & 184 deletions src/sc/lang/compiler.js
Original file line number Diff line number Diff line change
@@ -1,184 +1,3 @@
(function(sc) {
"use strict";

require("./sc");

var compiler = {};

compiler.Token = {
CharLiteral: "Char",
EOF: "<EOF>",
FalseLiteral: "False",
FloatLiteral: "Float",
Identifier: "Identifier",
IntegerLiteral: "Integer",
Keyword: "Keyword",
Label: "Label",
NilLiteral: "Nil",
Punctuator: "Punctuator",
StringLiteral: "String",
SymbolLiteral: "Symbol",
TrueLiteral: "True"
};

compiler.Syntax = {
AssignmentExpression: "AssignmentExpression",
BinaryExpression: "BinaryExpression",
BlockExpression: "BlockExpression",
CallExpression: "CallExpression",
FunctionExpression: "FunctionExpression",
GlobalExpression: "GlobalExpression",
Identifier: "Identifier",
ListExpression: "ListExpression",
Label: "Label",
Literal: "Literal",
ObjectExpression: "ObjectExpression",
Program: "Program",
ThisExpression: "ThisExpression",
UnaryExpression: "UnaryExpression",
VariableDeclaration: "VariableDeclaration",
VariableDeclarator: "VariableDeclarator"
};

var Message = compiler.Message = {
ArgumentAlreadyDeclared: "argument '%0' already declared",
InvalidLHSInAssignment: "invalid left-hand side in assignment",
NotImplemented: "not implemented %0",
UnexpectedEOS: "unexpected end of input",
UnexpectedIdentifier: "unexpected identifier",
UnexpectedChar: "unexpected char",
UnexpectedLabel: "unexpected label",
UnexpectedNumber: "unexpected number",
UnexpectedString: "unexpected string",
UnexpectedSymbol: "unexpected symbol",
UnexpectedToken: "unexpected token %0",
VariableAlreadyDeclared: "variable '%0' already declared",
VariableNotDefined: "variable '%0' not defined"
};

compiler.Keywords = {
var: "keyword",
arg: "keyword",
const: "keyword",
this: "function",
thisThread: "function",
thisProcess: "function",
thisFunction: "function",
thisFunctionDef: "function",
};

var Scope = (function() {
function Scope(methods) {
var f = function(parent) {
this.parent = parent;
this.stack = [];
};

function F() {}
F.prototype = Scope;
f.prototype = new F();

Object.keys(methods).forEach(function(key) {
f.prototype[key] = methods[key];
});

return f;
}

Scope.add = function(type, id, scope) {
var peek = this.stack[this.stack.length - 1];
var vars, args, declared, stmt, indent;

if (scope) {
vars = scope.vars;
args = scope.args;
declared = scope.declared;
stmt = scope.stmt;
indent = scope.indent;
} else {
vars = peek.vars;
args = peek.args;
declared = peek.declared;
stmt = peek.stmt;
indent = peek.indent;
}

if (args[id]) {
this.parent.throwError({}, Message.ArgumentAlreadyDeclared, id);
}

if (vars[id] && id.charAt(0) !== "_") {
this.parent.throwError({}, Message.VariableAlreadyDeclared, id);
}

switch (type) {
case "var":
if (!vars[id]) {
this.add_delegate(stmt, id, indent, peek, scope);
vars[id] = true;
delete declared[id];
}
break;
case "arg":
args[id] = true;
delete declared[id];
break;
}
};

Scope.add_delegate = function() {
};

Scope.end = function() {
this.stack.pop();
};

Scope.getDeclaredVariable = function() {
var peek = this.stack[this.stack.length - 1];
var declared = {};

if (peek) {
Array.prototype.concat.apply([], [
peek.declared, peek.args, peek.vars
].map(Object.keys)).forEach(function(key) {
declared[key] = true;
});
}

return declared;
};

Scope.find = function(id) {
var peek = this.stack[this.stack.length - 1];
return peek.vars[id] || peek.args[id] || peek.declared[id];
};

Scope.peek = function() {
return this.stack[this.stack.length - 1];
};

return Scope;
})();

compiler.Scope = Scope;

sc.lang.compiler = compiler;

var SCScript = sc.SCScript;

SCScript.tokenize = function(source, opts) {
opts = opts || /* istanbul ignore next */ {};
opts.tokens = true;
return sc.lang.parser.parse(source, opts).tokens || /* istanbul ignore next */ [];
};

SCScript.parse = function(source, opts) {
return sc.lang.parser.parse(source, opts);
};

SCScript.compile = function(source, opts) {
var ast = SCScript.parse(source, opts);
return sc.lang.codegen.compile(ast, opts);
};

})(sc);
require("./compiler/compiler");
require("./compiler/parser");
require("./compiler/codegen");
4 changes: 2 additions & 2 deletions src/sc/lang/codegen.js → src/sc/lang/compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use strict";

require("./sc");
require("./parser");
require("./scope");

var codegen = {};
var Syntax = sc.lang.compiler.Syntax;
Expand Down Expand Up @@ -218,7 +218,7 @@
CodeGen.prototype.throwError = function(obj, messageFormat) {
var args, message;

args = Array.prototype.slice.call(arguments, 1);
args = Array.prototype.slice.call(arguments, 2);
message = messageFormat.replace(/%(\d)/g, function(whole, index) {
return args[index];
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
var codegen = sc.lang.codegen;
var Syntax = sc.lang.compiler.Syntax;
var Token = sc.lang.compiler.Message;
var SCScript = sc.SCScript;

describe("sc.lang.codegen", function() {
function s(str) {
Expand Down Expand Up @@ -54,8 +53,18 @@
});
describe("compile", function() {
it("with bare", function() {
var source = "nil";
var test = esprima.parse(SCScript.compile(source, { bare: true }));
var ast = {
type: Syntax.Program,
body: [
{
type: Syntax.Literal,
value: "null",
valueType: Token.NilLiteral
}
]
};
var source = codegen.compile(ast, { bare: true });
var test = esprima.parse(source);
var compiled = esprima.parse(
"(function($this, $SC) { return $SC.Nil(); })"
);
Expand Down
89 changes: 89 additions & 0 deletions src/sc/lang/compiler/compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
(function(sc) {
"use strict";

require("./sc");

var compiler = {};

compiler.Token = {
CharLiteral: "Char",
EOF: "<EOF>",
FalseLiteral: "False",
FloatLiteral: "Float",
Identifier: "Identifier",
IntegerLiteral: "Integer",
Keyword: "Keyword",
Label: "Label",
NilLiteral: "Nil",
Punctuator: "Punctuator",
StringLiteral: "String",
SymbolLiteral: "Symbol",
TrueLiteral: "True"
};

compiler.Syntax = {
AssignmentExpression: "AssignmentExpression",
BinaryExpression: "BinaryExpression",
BlockExpression: "BlockExpression",
CallExpression: "CallExpression",
FunctionExpression: "FunctionExpression",
GlobalExpression: "GlobalExpression",
Identifier: "Identifier",
ListExpression: "ListExpression",
Label: "Label",
Literal: "Literal",
ObjectExpression: "ObjectExpression",
Program: "Program",
ThisExpression: "ThisExpression",
UnaryExpression: "UnaryExpression",
VariableDeclaration: "VariableDeclaration",
VariableDeclarator: "VariableDeclarator"
};

compiler.Message = {
ArgumentAlreadyDeclared: "argument '%0' already declared",
InvalidLHSInAssignment: "invalid left-hand side in assignment",
NotImplemented: "not implemented %0",
UnexpectedEOS: "unexpected end of input",
UnexpectedIdentifier: "unexpected identifier",
UnexpectedChar: "unexpected char",
UnexpectedLabel: "unexpected label",
UnexpectedNumber: "unexpected number",
UnexpectedString: "unexpected string",
UnexpectedSymbol: "unexpected symbol",
UnexpectedToken: "unexpected token %0",
VariableAlreadyDeclared: "variable '%0' already declared",
VariableNotDefined: "variable '%0' not defined"
};

compiler.Keywords = {
var: "keyword",
arg: "keyword",
const: "keyword",
this: "function",
thisThread: "function",
thisProcess: "function",
thisFunction: "function",
thisFunctionDef: "function",
};

sc.lang.compiler = compiler;

var SCScript = sc.SCScript;

SCScript.tokenize = function(source, opts) {
opts = opts || /* istanbul ignore next */ {};
opts.tokens = true;
return sc.lang.parser.parse(source, opts).tokens || /* istanbul ignore next */ [];
};

SCScript.parse = function(source, opts) {
return sc.lang.parser.parse(source, opts);
};

SCScript.compile = function(source, opts) {
var ast = SCScript.parse(source, opts);
return sc.lang.codegen.compile(ast, opts);
};

})(sc);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function(sc) {
"use strict";

require("../lang/compiler");
require("./compiler");

var Token = sc.lang.compiler.Token;
var Syntax = sc.lang.compiler.Syntax;
Expand Down Expand Up @@ -5563,10 +5563,10 @@
method: {
type: Syntax.Identifier,
name: "new",
range: [ 7, 7 ],
range: [ 9, 9 ],
loc: {
start: { line: 1, column: 7 },
end : { line: 1, column: 7 }
start: { line: 1, column: 9 },
end : { line: 1, column: 9 }
}
},
args: {
Expand Down Expand Up @@ -5769,10 +5769,10 @@
method: {
type: Syntax.Identifier,
name: "newFrom",
range: [ 5, 5 ],
range: [ 6, 6 ],
loc: {
start: { line: 1, column: 5 },
end : { line: 1, column: 5 }
start: { line: 1, column: 6 },
end : { line: 1, column: 6 }
}
},
args: {
Expand Down Expand Up @@ -5882,10 +5882,10 @@
method: {
type: Syntax.Identifier,
name: "newFrom",
range: [ 5, 5 ],
range: [ 6, 6 ],
loc: {
start: { line: 1, column: 5 },
end : { line: 1, column: 5 }
start: { line: 1, column: 6 },
end : { line: 1, column: 6 }
}
},
args: {
Expand Down
Loading

0 comments on commit 43d6111

Please sign in to comment.