Skip to content

Commit 9b6efc6

Browse files
committed
Finished first pass on Runtime, fixed ESLint errors.
1 parent a09ed0b commit 9b6efc6

4 files changed

Lines changed: 31 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"eslint-config-mitmaro": "^2.0.0",
2323
"eslint-plugin-strict-newline": "^1.0.0",
2424
"istanbul": "^0.4.4",
25-
"mocha": "^2.5.3"
25+
"mocha": "^3.0.0"
2626
},
2727
"dependencies": {
2828
"async": "^2.0.1"

src/Parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class Parser {
139139
generateAST() {
140140
this.sources = [];
141141
const ast = this.generateRootNode();
142+
142143
this.getExpectedToken(TOKEN_STRUCTURE_EOF);
143144
ast.sources = this.sources;
144145
return ast;

src/Runner.js renamed to src/Runtime.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ const Parser = require('./Parser');
88
const Lexer = require('./Lexer');
99

1010
// credit: http://stackoverflow.com/a/3886106/124861
11-
function isFloat(n){
11+
function isFloat(n) {
1212
return Number(n) === n && n % 1 !== 0;
1313
}
1414

1515
function floatEqual(a, b, epsilon = Number.EPSILON) {
16-
if (a === b) {
17-
return true;
18-
}
16+
if (a === b) {
17+
return true;
18+
}
1919

20-
if (isNaN(a) || isNaN(b) || !isFinite(a) || !isFinite(b)) {
21-
return false;
22-
}
20+
if (isNaN(a) || isNaN(b) || !isFinite(a) || !isFinite(b)) {
21+
return false;
22+
}
23+
24+
const diff = Math.abs(a - b);
2325

24-
const diff = Math.abs(a - b);
25-
return diff < epsilon ? true : diff <= Math.max(Math.abs(a), Math.abs(b)) * epsilon;
26+
return diff < epsilon ? true : diff <= Math.max(Math.abs(a), Math.abs(b)) * epsilon;
2627
}
2728

2829
const {
@@ -66,6 +67,7 @@ class Runner {
6667

6768
for (const value of sources) {
6869
const path = value.type === PARSER_TYPE_VARIABLE ? this.getValueFromVariable(value.name) : value.value;
70+
6971
tasks[path] = fs.readFile.bind(fs, path);
7072
}
7173

@@ -96,10 +98,17 @@ class Runner {
9698
}
9799

98100
invokeInclude(statement) {
101+
let path;
99102

100-
if ()
101-
102-
this.invoke(ast);
103+
if (statement.value.type === PARSER_TYPE_VARIABLE) {
104+
path = this.getValueFromVariable(statement.value.name);
105+
}
106+
else {
107+
path = statement.value.value;
108+
}
109+
if (this.astCache[path]) {
110+
this.invokeStatements(this.astCache[path].statements);
111+
}
103112
}
104113

105114
invokeBranch(statement) {
@@ -108,6 +117,8 @@ class Runner {
108117
return this.invokeStatements(branch.consequent.statements);
109118
}
110119
}
120+
// no branch executed
121+
return null;
111122
}
112123

113124
evaluateExpression(expression) {
@@ -123,6 +134,7 @@ class Runner {
123134
else if (expression.type === PARSER_TYPE_VARIABLE) {
124135
return this.getValueFromVariable(expression.name);
125136
}
137+
throw new RuntimeError(`Unknown expression type: ${expression.type}`);
126138
}
127139

128140
evaluateBinaryExpression(expression) {
@@ -176,7 +188,7 @@ class Runner {
176188
if (isFloat(leftValue) && isFloat(rightValue)) {
177189
return floatEqual(leftValue, rightValue);
178190
}
179-
191+
// eslint-disable-next-line eqeqeq
180192
return strict ? leftValue === rightValue : leftValue == rightValue;
181193
}
182194

@@ -189,7 +201,7 @@ class Runner {
189201
}
190202

191203
getValueFromVariable(name) {
192-
if (!name in this.input) {
204+
if (!(name in this.input)) {
193205
throw new RuntimeError(`Unset variable: ${name}`);
194206
}
195207
return this.input[name];

test/Parser/Parser.generate.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Parser.generate', function() {
3636
nb.include(nb.value('foo'))
3737
];
3838

39-
expect(result.sources).to.deep.equal([nb.value('foo')]);
39+
expect(result.sources).to.deep.equal([ nb.value('foo') ]);
4040
expect(result.statements).to.deep.equal(expected);
4141
});
4242

@@ -46,14 +46,14 @@ describe('Parser.generate', function() {
4646
nb.include(nb.variable('foo'))
4747
];
4848

49-
expect(result.sources).to.deep.equal([nb.variable('foo')]);
49+
expect(result.sources).to.deep.equal([ nb.variable('foo') ]);
5050
expect(result.statements).to.deep.equal(expected);
5151
});
5252

5353
it('should set sources from inside if blocks', function() {
5454
const result = getParserResult('{{if bar}}{{include baz}}{{fi}}');
5555

56-
expect(result.sources).to.deep.equal([nb.variable('baz')]);
56+
expect(result.sources).to.deep.equal([ nb.variable('baz') ]);
5757
});
5858

5959
it('should parse with basic if statement', function() {

0 commit comments

Comments
 (0)