mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-12 07:44:03 +09:00
working parentheses parsing
This commit is contained in:
@@ -84,7 +84,7 @@ function BasicAST() {
|
|||||||
this.depth = 0;
|
this.depth = 0;
|
||||||
this.leaves = [];
|
this.leaves = [];
|
||||||
this.value = undefined;
|
this.value = undefined;
|
||||||
this.type = "null"; // literal, operator, variable, function
|
this.type = "null"; // literal, operator, variable, function, null
|
||||||
|
|
||||||
this.toString = function() {
|
this.toString = function() {
|
||||||
var sb = "";
|
var sb = "";
|
||||||
@@ -162,6 +162,10 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
|
|||||||
treeHead.depth = recDepth;
|
treeHead.depth = recDepth;
|
||||||
treeHead.lnum = lnum;
|
treeHead.lnum = lnum;
|
||||||
|
|
||||||
|
// TODO ability to parse arbitrary parentheses
|
||||||
|
// test string: print((minus(plus(3,2),times(8,7))))
|
||||||
|
// ^ ^ these extra parens break your parser
|
||||||
|
|
||||||
// IF statement
|
// IF statement
|
||||||
if ("IF" == tokens[0].toUpperCase()) {
|
if ("IF" == tokens[0].toUpperCase()) {
|
||||||
throw "TODO";
|
throw "TODO";
|
||||||
@@ -173,32 +177,57 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
|
|||||||
}
|
}
|
||||||
// is this a function?
|
// is this a function?
|
||||||
else {
|
else {
|
||||||
treeHead.value = headWord;
|
// find and mark position of separators and parentheses
|
||||||
treeHead.type = "function";
|
|
||||||
|
|
||||||
// find and mark position of separators
|
|
||||||
// properly deal with the nested function calls
|
// properly deal with the nested function calls
|
||||||
|
var currentFunction = (states[0] == "paren") ? undefined : tokens[0];
|
||||||
|
var parenDepth = 0;
|
||||||
|
var parenStart = 0;
|
||||||
|
var parenEnd = -1;
|
||||||
var separators = [];
|
var separators = [];
|
||||||
var sepInit = (tokens.length > 1 && "paren" == states[1]) ? 2 : 1;
|
|
||||||
var parenCount = 1;
|
|
||||||
for (k = sepInit; k < tokens.length; k++) {
|
|
||||||
if ("(" == tokens[k]) parenCount += 1;
|
|
||||||
else if (")" == tokens[k]) parenCount -= 1;
|
|
||||||
|
|
||||||
if (parenCount == 1 && states[k] == "sep") separators.push(k);
|
treeHead.value = currentFunction;
|
||||||
|
treeHead.type = (typeof currentFunction == "undefined") ? "null" : "function";
|
||||||
|
|
||||||
if (parenCount < 0) throw lang.syntaxfehler(lnum);
|
for (k = 0; k < tokens.length; k++) {
|
||||||
|
if (tokens[k] == "(") {
|
||||||
|
parenDepth += 1;
|
||||||
|
if (parenDepth == 1) parenStart = k;
|
||||||
|
}
|
||||||
|
else if (tokens[k] == ")") {
|
||||||
|
if (parenDepth == 1) parenEnd = k;
|
||||||
|
parenDepth -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parenDepth == 1 && states[k] == "sep") {
|
||||||
|
separators.push(k);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
separators.push(tokens.length - (sepInit == 1 ? 0 : 1));
|
|
||||||
|
|
||||||
println("sep ("+sepInit+")["+separators.join(",")+"]");
|
if (parenDepth != 0) throw "Unmatched brackets";
|
||||||
|
|
||||||
// parse and populate leaves
|
|
||||||
var leaves = [];
|
var leaves = [];
|
||||||
if (sepInit < separators[0]) {
|
|
||||||
for (k = 0; k < separators.length; k++) {
|
// if there is no paren
|
||||||
var subtkn = tokens.slice((k == 0) ? sepInit : separators[k - 1] + 1, separators[k]);
|
if (parenStart == 0 && parenEnd == -1 && tokens.length > 1) {
|
||||||
var substa = states.slice((k == 0) ? sepInit : separators[k - 1] + 1, separators[k]);
|
var subtkn = tokens.slice(1, tokens.length);
|
||||||
|
var substa = states.slice(1, tokens.length);
|
||||||
|
|
||||||
|
leaves.push(basicFunctions._parseTokens(lnum, subtkn, substa, recDepth + 1));
|
||||||
|
}
|
||||||
|
else if (parenEnd > parenStart) {
|
||||||
|
separators = [parenStart].concat(separators, [parenEnd]);
|
||||||
|
// recursively parse comma-separated arguments
|
||||||
|
|
||||||
|
// print ( plus ( 3 , 2 ) , times ( 8 , 7 ) )
|
||||||
|
// s ^ e
|
||||||
|
// separators = [1,8,15]
|
||||||
|
// plus ( 3 , 2 ) / times ( 8 , 7 )
|
||||||
|
// s ^ e s ^ e
|
||||||
|
// separators = [1,5] ; [1,5]
|
||||||
|
// 3 / 2 / 8 / 7
|
||||||
|
for (k = 1; k < separators.length; k++) {
|
||||||
|
var subtkn = tokens.slice(separators[k - 1] + 1, separators[k]);
|
||||||
|
var substa = states.slice(separators[k - 1] + 1, separators[k]);
|
||||||
|
|
||||||
leaves.push(basicFunctions._parseTokens(lnum, subtkn, substa, recDepth + 1));
|
leaves.push(basicFunctions._parseTokens(lnum, subtkn, substa, recDepth + 1));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user