Skip to content

Commit

Permalink
refactor: introduce a Loop node type (#105)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rather than a `While` node with a virtual condition
and null for other properties, this introduces a new `Loop` node that
only has a body.
  • Loading branch information
eventualbuddha authored Dec 29, 2016
1 parent 316b7e3 commit 3447059
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
19 changes: 13 additions & 6 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import lex, { SourceType } from 'coffee-lex';
import locationsEqual from './util/locationsEqual';
import locationContainingNodes from './util/locationContainingNodes';
import locationWithLastPosition from './util/locationWithLastPosition';
import makeNode, { Bool, RegexFlags } from './nodes';
import makeNode, { RegexFlags } from './nodes';
import mapAny from './mappers/mapAny';
import mapLiteral from './mappers/mapLiteral';
import mergeLocations from './util/mergeLocations';
Expand Down Expand Up @@ -653,16 +653,23 @@ function convert(context) {
}

case 'While': {
const result = makeNode(context, 'While', locationContainingNodes(node, node.condition, node.body), {
let start = linesAndColumns.indexForLocation({ line: node.locationData.first_line, column: node.locationData.first_column });
let tokens = context.sourceTokens;
let startTokenIndex = tokens.indexOfTokenContainingSourceIndex(start);
let startTokenType = tokens.tokenAtIndex(startTokenIndex).type;

if (startTokenType === SourceType.LOOP) {
return makeNode(context, 'Loop', locationContainingNodes(node, node.body), {
body: convertChild(node.body)
});
}

return makeNode(context, 'While', locationContainingNodes(node, node.condition, node.body), {
condition: convertChild(node.condition),
guard: convertChild(node.guard),
body: convertChild(node.body),
isUntil: node.condition.inverted === true
});
if (result.raw.indexOf('loop') === 0) {
result.condition = Bool.true();
}
return result;
}

case 'Existence':
Expand Down
48 changes: 28 additions & 20 deletions test/examples/loop/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,55 @@
"type": "Program",
"line": 1,
"column": 1,
"range": [ 0, 8 ],
"range": [
0,
8
],
"raw": "loop\n a",
"body": {
"type": "Block",
"line": 1,
"column": 1,
"range": [ 0, 8 ],
"raw": "loop\n a",
"range": [
0,
8
],
"statements": [
{
"type": "While",
"type": "Loop",
"line": 1,
"column": 1,
"range": [ 0, 8 ],
"raw": "loop\n a",
"isUntil": false,
"guard": null,
"range": [
0,
8
],
"body": {
"type": "Block",
"line": 2,
"column": 3,
"range": [ 7, 8 ],
"raw": "a",
"inline": false,
"range": [
7,
8
],
"statements": [
{
"type": "Identifier",
"line": 2,
"column": 3,
"range": [ 7, 8 ],
"range": [
7,
8
],
"raw": "a",
"data": "a"
}
]
],
"raw": "a",
"inline": false
},
"condition": {
"type": "Bool",
"data": true,
"virtual": true
}
"raw": "loop\n a"
}
]
],
"raw": "loop\n a"
}
}
}

0 comments on commit 3447059

Please sign in to comment.