mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-17 00:16:04 +09:00
even better working parser
This commit is contained in:
@@ -17,10 +17,17 @@ var cmdbufMemFootPrint = 0;
|
|||||||
var prompt = "Ok";
|
var prompt = "Ok";
|
||||||
|
|
||||||
var lang = {};
|
var lang = {};
|
||||||
lang.syntaxfehler = function(line) {
|
lang.badNumberFormat = "Bad number format";
|
||||||
|
lang.badOperatorFormat = "Bad number format";
|
||||||
|
lang.badFunctionCallFormat = "Bad function call format";
|
||||||
|
lang.unmatchedBrackets = "Unmatched brackets";
|
||||||
|
lang.syntaxfehler = function(line, reason) {
|
||||||
if (line === undefined)
|
if (line === undefined)
|
||||||
return "Syntax error";
|
return "Syntax error";
|
||||||
return "Syntax error in " + line;
|
else if (reason === undefined)
|
||||||
|
return "Syntax error in " + line;
|
||||||
|
else
|
||||||
|
return "Syntax error in " + line + ": " + reason;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getUsedMemSize() {
|
function getUsedMemSize() {
|
||||||
@@ -139,19 +146,18 @@ basicFunctions._isSeparator = function(code) {
|
|||||||
};
|
};
|
||||||
basicFunctions._operatorPrecedence = {
|
basicFunctions._operatorPrecedence = {
|
||||||
// function call in itself has highest precedence
|
// function call in itself has highest precedence
|
||||||
"NOT":13,
|
"^":2,
|
||||||
"^":12,
|
"*":3,"/":3,
|
||||||
"*":11,"/":11,
|
"MOD":4,
|
||||||
"MOD":10,
|
"+":5,"-":5,
|
||||||
"+":9,"-":9,
|
"<<":6,">>":6,
|
||||||
"<<":8,">>":8,
|
|
||||||
"==":7,"<>":7,"><":7,"<":7,">":7,"<=":7,"=<":7,">=":7,"=>":7,
|
"==":7,"<>":7,"><":7,"<":7,">":7,"<=":7,"=<":7,">=":7,"=>":7,
|
||||||
"BAND":6,
|
"BAND":8,
|
||||||
"BXOR":5,
|
"BXOR":9,
|
||||||
"BOR":4,
|
"BOR":10,
|
||||||
"AND":3,
|
"AND":11,
|
||||||
"OR":2,
|
"OR":12,
|
||||||
"=":1
|
"=":13
|
||||||
};
|
};
|
||||||
basicFunctions._isUnaryOp = function(word) {
|
basicFunctions._isUnaryOp = function(word) {
|
||||||
return 13 == basicFunctions._operatorPrecedence[word];
|
return 13 == basicFunctions._operatorPrecedence[word];
|
||||||
@@ -250,7 +256,7 @@ basicFunctions._tokenise = function(lnum, cmd) {
|
|||||||
mode = "number2";
|
mode = "number2";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw lang.syntaxfehler(lnum);
|
throw lang.syntaxfehler(lnum, lang.badNumberFormat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ("number2" == mode) {
|
else if ("number2" == mode) {
|
||||||
@@ -288,7 +294,7 @@ basicFunctions._tokenise = function(lnum, cmd) {
|
|||||||
mode = "operator2";
|
mode = "operator2";
|
||||||
}
|
}
|
||||||
else if (basicFunctions._isFirstOp(charCode)) {
|
else if (basicFunctions._isFirstOp(charCode)) {
|
||||||
throw lang.syntaxfehler(lnum);
|
throw lang.syntaxfehler(lnum, lang.badOperatorFormat);
|
||||||
}
|
}
|
||||||
if (basicFunctions._isNumber(charCode)) {
|
if (basicFunctions._isNumber(charCode)) {
|
||||||
tokens.push(sb); sb = "" + char; states.push(mode);
|
tokens.push(sb); sb = "" + char; states.push(mode);
|
||||||
@@ -317,7 +323,7 @@ basicFunctions._tokenise = function(lnum, cmd) {
|
|||||||
}
|
}
|
||||||
else if ("operator2" == mode) {
|
else if ("operator2" == mode) {
|
||||||
if (basicFunctions._isFirstOp(charCode)) {
|
if (basicFunctions._isFirstOp(charCode)) {
|
||||||
throw lang.syntaxfehler(lnum);
|
throw lang.syntaxfehler(lnum, lang.badOperatorFormat);
|
||||||
}
|
}
|
||||||
if (basicFunctions._isNumber(charCode)) {
|
if (basicFunctions._isNumber(charCode)) {
|
||||||
tokens.push(sb); sb = "" + char; states.push("operator");
|
tokens.push(sb); sb = "" + char; states.push("operator");
|
||||||
@@ -631,7 +637,7 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parenDepth != 0) throw "Unmatched brackets";
|
if (parenDepth != 0) throw lang.syntaxfehler(lnum, lang.unmatchedBrackets);
|
||||||
|
|
||||||
// BINARY_OP/UNARY_OP
|
// BINARY_OP/UNARY_OP
|
||||||
if (topmostOp !== undefined) {
|
if (topmostOp !== undefined) {
|
||||||
@@ -660,13 +666,14 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
|
|||||||
else {
|
else {
|
||||||
if (_debugSyntaxAnalysis) println("function call");
|
if (_debugSyntaxAnalysis) println("function call");
|
||||||
var currentFunction = (states[0] == "paren") ? undefined : tokens[0];
|
var currentFunction = (states[0] == "paren") ? undefined : tokens[0];
|
||||||
treeHead.value = currentFunction;
|
treeHead.value = ("-" == currentFunction) ? "UNARYMINUS" : ("+" == currentFunction) ? "UNARYPLUS" : currentFunction;
|
||||||
|
|
||||||
treeHead.type = (currentFunction === undefined) ? "null" : "function";
|
treeHead.type = (currentFunction === undefined) ? "null" : "function";
|
||||||
|
if (_debugSyntaxAnalysis) println("function name: "+treeHead.value);
|
||||||
|
|
||||||
var leaves = [];
|
var leaves = [];
|
||||||
|
|
||||||
// if there is no paren
|
// if there is no paren or paren does NOT start index 1
|
||||||
if (parenStart == 0 && parenEnd == -1 && tokens.length > 1) {
|
if (parenStart >= 2) {
|
||||||
var subtkn = tokens.slice(1, tokens.length);
|
var subtkn = tokens.slice(1, tokens.length);
|
||||||
var substa = states.slice(1, tokens.length);
|
var substa = states.slice(1, tokens.length);
|
||||||
|
|
||||||
@@ -695,11 +702,8 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
|
|||||||
leaves.push(basicFunctions._parseTokens(lnum, subtkn, substa, recDepth + 1));
|
leaves.push(basicFunctions._parseTokens(lnum, subtkn, substa, recDepth + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else throw lang.syntaxfehler(lnum, lang.badFunctionCallFormat);
|
||||||
treeHead.leaves = leaves;//.filter(function(__v) { return __v !== undefined; });
|
treeHead.leaves = leaves;//.filter(function(__v) { return __v !== undefined; });
|
||||||
|
|
||||||
// after-the-fact fix for some unary ops
|
|
||||||
if (treeHead.value == "-" && treeHead.leaves.length == 1) treeHead.value = "UNARYMINUS";
|
|
||||||
else if (treeHead.value == "+" && treeHead.leaves.length == 1) treeHead.value = "UNARYPLUS";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user