ability to parse infix operators but not unary

This commit is contained in:
minjaesong
2020-06-11 07:53:36 +09:00
parent 655f955fc5
commit 51060b346b
2 changed files with 71 additions and 50 deletions

View File

@@ -523,27 +523,22 @@ basicFunctions._parserElaboration = function(lnum, tokens, states) {
k += 1; k += 1;
} }
}; };
basicFunctions._parserLukasiewiczation = function(lnum, tokens, states) { basicFunctions._unaryToBinary = function(lnum, tokens, states) {
// for the test input string of: // turn some + and - into unary ops
// cin(tan(getch() MOD 5),4+sin(32 AND 7))+cin(-2) // + 2
// 5 * + 2
// + 7 - - 4
// //
// // ( 0 + 2 )
// cin(tan(getch() MOD 5),4+sin(32 AND 7))+cin(-2) // 5 * ( 0 + 2 )
// cin(tan(getch() MOD 5),4+sin(32 AND 7)) cin(-2) // ( 0 + 7 ) - ( 0 - 4 )
// tan(getch() MOD 5) 4+sin(32 AND 7) -2
// getch() MOD 5 4 sin(32 AND 7) 2
// getch() 5 32 AND 7
// 32 7
// 32 7
// getch() 5 and(32,7)
// MOD(getch(),5) 4 sin(and(32,7)) 2
// tan(MOD(getch(),5)) plus(4,sin(and(32,7))) unaryMinus(2)
// cin(tan(MOD(getch(),5)),plus(4,sin(and(32,7)))) cin(unaryMinus(2))
// plus(cin(tan(MOD(getch(),5)),plus(4,sin(and(32,7)))),cin(unaryMinus(2)))
var _debugprintLuka = true; var _debugprintLuka = true;
if (_debugprintLuka) println("@@ LUKASIEWICZATION @@") if (_debugprintLuka) println("@@ UNARY-TO-BINARY @@")
}; };
basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) { basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
// DO NOT PERFORM SEMANTIC ANALYSIS HERE // DO NOT PERFORM SEMANTIC ANALYSIS HERE
@@ -582,7 +577,10 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
if (tokens.length != states.length) throw "InternalError: size of tokens and states does not match (line: "+lnum+", recursion depth: "+recDepth+")"; if (tokens.length != states.length) throw "InternalError: size of tokens and states does not match (line: "+lnum+", recursion depth: "+recDepth+")";
if (tokens.length == 0) { if (tokens.length == 0) {
if (_debugSyntaxAnalysis) println("*empty tokens*"); if (_debugSyntaxAnalysis) println("*empty tokens*");
return new BasicAST(); var retTreeHead = new BasicAST();
retTreeHead.depth = recDepth;
retTreeHead.lnum = lnum;
return retTreeHead;
} }
var k; var k;
@@ -600,12 +598,18 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
throw "TODO"; throw "TODO";
} }
// LEAF: is this a literal? // LEAF: is this a literal?
else if (recDepth > 0 && ("quote" == states[0] || "number" == states[0])) { else if (tokens.length == 1 && ("quote" == states[0] || "number" == states[0] || "bool" == states[0])) {
treeHead.value = tokens[0]; if (_debugSyntaxAnalysis) println("literal/number: "+tokens[0]);
treeHead.value = ("quote" == states[0]) ? tokens[0] : tokens[0].toUpperCase();
treeHead.type = "literal"; treeHead.type = "literal";
} }
// is this a function/operators? // is this a function/operators?
else { else {
// scan for operators with highest precedence, use rightmost one if multiple were found
var topmostOp;
var topmostOpPrc = 0;
var operatorPos = -1;
// find and mark position of separators and parentheses // find and mark position of separators and parentheses
// properly deal with the nested function calls // properly deal with the nested function calls
var parenDepth = 0; var parenDepth = 0;
@@ -626,10 +630,26 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
if (parenDepth == 1 && states[k] == "sep") { if (parenDepth == 1 && states[k] == "sep") {
separators.push(k); separators.push(k);
} }
if (parenDepth == 0 && states[k] == "operator" && basicFunctions._operatorPrecedence[tokens[k].toUpperCase()] >= topmostOpPrc) {
topmostOp = tokens[k].toUpperCase();
topmostOpPrc = basicFunctions._operatorPrecedence[tokens[k]];
operatorPos = k;
}
} }
if (parenDepth != 0) throw "Unmatched brackets"; if (parenDepth != 0) throw "Unmatched brackets";
// if there is an operator, split using it
if (topmostOp !== undefined) {
if (_debugSyntaxAnalysis) println("operator: "+topmostOp+", pos: "+operatorPos);
treeHead.value = topmostOp;
treeHead.type = "operator";
treeHead.leaves[0] = basicFunctions._parseTokens(lnum, tokens.slice(0, operatorPos), states.slice(0, operatorPos), recDepth + 1);
treeHead.leaves[1] = basicFunctions._parseTokens(lnum, tokens.slice(operatorPos + 1, tokens.length), states.slice(operatorPos + 1, tokens.length), recDepth + 1);
}
else {
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;
treeHead.type = (currentFunction === undefined) ? "null" : "function"; treeHead.type = (currentFunction === undefined) ? "null" : "function";
@@ -666,6 +686,7 @@ basicFunctions._parseTokens = function(lnum, tokens, states, recDepth) {
} }
treeHead.leaves = leaves;//.filter(function(__v) { return __v !== undefined; }); treeHead.leaves = leaves;//.filter(function(__v) { return __v !== undefined; });
} }
}
return treeHead; return treeHead;
@@ -693,7 +714,7 @@ basicFunctions._interpretLine = function(lnum, cmd) {
// ŁUKASIEWICZATION : turn infix notation into polish notation // ŁUKASIEWICZATION : turn infix notation into polish notation
basicFunctions._parserLukasiewiczation(lnum, tokens, states); basicFunctions._unaryToBinary(lnum, tokens, states);
if (_debugprintHighestLevel) println(tokens.join("~")); if (_debugprintHighestLevel) println(tokens.join("~"));

View File

@@ -29,7 +29,7 @@ class VMGUI(val appConfig: LwjglApplicationConfiguration) : ApplicationAdapter()
override fun create() { override fun create() {
super.create() super.create()
gpu = GraphicsAdapter(vm, lcdMode = false) gpu = GraphicsAdapter(vm, lcdMode = true)
vm.peripheralTable[1] = PeripheralEntry( vm.peripheralTable[1] = PeripheralEntry(
VM.PERITYPE_TERM, VM.PERITYPE_TERM,