Grouping works correctly
This commit is contained in:
parent
dbb8a5161e
commit
432316d640
@ -23,7 +23,6 @@ function parse(tokens) {
|
|||||||
ast = addLevels(ast);
|
ast = addLevels(ast);
|
||||||
|
|
||||||
// Start grouping by precedence.
|
// Start grouping by precedence.
|
||||||
|
|
||||||
// Precedence 16.
|
// Precedence 16.
|
||||||
ast = grouping(ast);
|
ast = grouping(ast);
|
||||||
// Precedence 15.
|
// Precedence 15.
|
||||||
@ -103,24 +102,28 @@ function getDeepestLevel(tokens) {
|
|||||||
function grouping(tokens) {
|
function grouping(tokens) {
|
||||||
// Get the deepest level.
|
// Get the deepest level.
|
||||||
let deepestLevel = getDeepestLevel(tokens);
|
let deepestLevel = getDeepestLevel(tokens);
|
||||||
// Loop through for each level.
|
let groupBuffer;
|
||||||
|
let opening;
|
||||||
|
// Group the deepest levels first.
|
||||||
for (let currentLevel = deepestLevel; currentLevel > 0; currentLevel--) {
|
for (let currentLevel = deepestLevel; currentLevel > 0; currentLevel--) {
|
||||||
let groupBuffer = [];
|
groupBuffer = []; // Overwrite groupBuffer.
|
||||||
for (let j = 0; j < tokens.length; j++) {
|
opening = null; // Overwrite opening.
|
||||||
let nextTokenLevel = 0;
|
for (let i = 0; i < tokens.length; i++) {
|
||||||
if (typeof tokens[j + 1] != 'undefined')
|
if (tokens[i].level == currentLevel) {
|
||||||
nextTokenLevel = tokens[j + 1].level;
|
if (groupBuffer.length == 0)
|
||||||
if (tokens[j].level == currentLevel) {
|
opening = tokens[i];
|
||||||
groupBuffer.push(tokens[j]); // Add the token to the groupBuffer.
|
groupBuffer.push(tokens[i]);
|
||||||
if (tokens[j].level > nextTokenLevel) {
|
if (typeof tokens[i + 1] == 'undefined' || (tokens[i].type == 'delimiter' && tokens[i].subtype == 'right' && tokens[i].value == opening.value)) { // The end of the tokens.
|
||||||
let g = new Group(groupBuffer[0].value, groupBuffer);
|
let g = new Group(groupBuffer[0].value, groupBuffer);
|
||||||
g.index = g.tokens[0].index;
|
g.index = g.tokens[0].index;
|
||||||
g.level = g.tokens[0].level - 1; // -1 because the group is on the level below.
|
g.level = g.tokens[0].level - 1; // -1 because the group is on the level below.
|
||||||
tokens.splice(g.tokens[0].index, g.tokens.length, g);
|
let length = g.tokens.length; // Keep track of how many tokens there are before removing the delimiters.
|
||||||
j = g.tokens[0].index;
|
g.tokens = g.tokens.splice(1, g.tokens.length - 2); // Remove the delimiters in g.tokens.
|
||||||
// Remove the delimiters in g.tokens.
|
i -= length - 1; // Reset the counter.
|
||||||
g.tokens = g.tokens.splice(1, groupBuffer.length - 2);
|
tokens.splice(i, length, g); // Replace the tokens with the new group.
|
||||||
|
// Reset groupBuffer and opening.
|
||||||
groupBuffer = [];
|
groupBuffer = [];
|
||||||
|
opening = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,7 +140,6 @@ function grouping(tokens) {
|
|||||||
*/
|
*/
|
||||||
// TODO: Member access
|
// TODO: Member access
|
||||||
function memberAccess(ast) {
|
function memberAccess(ast) {
|
||||||
console.log(ast);
|
|
||||||
for (let i = 0; i < ast.length; i++) {
|
for (let i = 0; i < ast.length; i++) {
|
||||||
if (ast[i].type == 'group')
|
if (ast[i].type == 'group')
|
||||||
ast[i].tokens = memberAccess(ast[i].tokens); // Recursively order the groups.
|
ast[i].tokens = memberAccess(ast[i].tokens); // Recursively order the groups.
|
||||||
@ -243,7 +245,6 @@ function keywords(ast) {
|
|||||||
*/
|
*/
|
||||||
function functionCreation(ast) {
|
function functionCreation(ast) {
|
||||||
// Function call is Parenthesis Group, Brace Group.
|
// Function call is Parenthesis Group, Brace Group.
|
||||||
console.log(ast);
|
|
||||||
for (let i = 0; i < ast.length; i++) {
|
for (let i = 0; i < ast.length; i++) {
|
||||||
if (ast[i].type == 'group')
|
if (ast[i].type == 'group')
|
||||||
ast[i].tokens = functionCreation(ast[i].tokens); // Recursively order the groups.
|
ast[i].tokens = functionCreation(ast[i].tokens); // Recursively order the groups.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user