still fixing so that demo code would work

This commit is contained in:
minjaesong
2020-12-21 22:18:10 +09:00
parent 8600207e34
commit 666c882b5e
2 changed files with 41 additions and 21 deletions

2
.gitignore vendored
View File

@@ -35,6 +35,8 @@ tmp_*
## Vim temp files ## Vim temp files
*.swp *.swp
*.swo *.swo
## Kate temp swap file
*.kate-swp
# Eclipse MAT heap files # Eclipse MAT heap files
*.hprof *.hprof

View File

@@ -46,6 +46,8 @@ function isNumable(s) {
return s !== undefined && (typeof s.trim == "function" && s.trim() !== "" || s.trim == undefined) && !isNaN(s); return s !== undefined && (typeof s.trim == "function" && s.trim() !== "" || s.trim == undefined) && !isNaN(s);
} }
function cloneObject(o) = JSON.parse(JSON.stringify(o));
class ParserError extends Error { class ParserError extends Error {
constructor(...args) { constructor(...args) {
super(...args); super(...args);
@@ -271,7 +273,8 @@ let resolve = function(variable) {
else else
throw Error("BasicIntpError: unknown variable with type "+variable.troType+", with value "+variable.troValue); throw Error("BasicIntpError: unknown variable with type "+variable.troType+", with value "+variable.troValue);
} }
let curryDefun = function(exprTree, value) { let curryDefun = function(inputTree, value) {
let exprTree = cloneObject(inputTree);
bF._recurseApplyAST(exprTree, it => { bF._recurseApplyAST(exprTree, it => {
if (it.astType == "defun_args") { if (it.astType == "defun_args") {
// apply arg0 into the tree // apply arg0 into the tree
@@ -286,13 +289,6 @@ let curryDefun = function(exprTree, value) {
} }
}); });
if (DBGON) {
serial.println("[BASIC.curryDefun] currying value: "+value);
serial.println(Object.entries(value));
serial.println("[BASIC.curryDefun] curried expression tree:");
serial.println(astToString(exprTree));
}
return exprTree; return exprTree;
} }
let argCheckErr = function(lnum, o) { let argCheckErr = function(lnum, o) {
@@ -399,7 +395,7 @@ let _basicConsts = {
}; };
Object.freeze(_basicConsts); Object.freeze(_basicConsts);
let initBvars = function() { let initBvars = function() {
return JSON.parse(JSON.stringify(_basicConsts)); return cloneObject(_basicConsts);
} }
let ForGen = function(s,e,t) { let ForGen = function(s,e,t) {
this.start = s; this.start = s;
@@ -501,7 +497,7 @@ bStatus.getArrayIndexFun = function(lnum, stmtnum, arrayName, array) {
bStatus.getDefunThunk = function(lnum, stmtnum, exprTree) { bStatus.getDefunThunk = function(lnum, stmtnum, exprTree) {
if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`); if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`);
let tree = JSON.parse(JSON.stringify(exprTree)); // ALWAYS create new tree instance! let tree = cloneObject(exprTree); // ALWAYS create new tree instance!
return function(lnum, stmtnum, args, seps) { return function(lnum, stmtnum, args, seps) {
if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`); if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`);
@@ -1230,9 +1226,17 @@ if no arg text were given (e.g. "10 NEXT"), args will have zero length
if (DBGON) { if (DBGON) {
serial.println("[BASIC.CURRY] currying "+args[0].troValue+" with "+arg0); serial.println("[BASIC.CURRY] currying "+args[0].troValue+" with "+arg0);
serial.println("args[1] = "+Object.entries(args[1])); serial.println("args[1] = "+Object.entries(args[1]));
serial.println("[BASIC.CURRY] before currying:");
serial.println(astToString(fn))
} }
curryDefun(fn, arg0); curryDefun(fn, arg0);
if (DBGON) {
serial.println("[BASIC.CURRY] after currying:");
serial.println(astToString(fn));
}
return fn; return fn;
}); });
}, },
@@ -2584,9 +2588,9 @@ bF._troNOP = function(lnum, stmtnum) { return new SyntaxTreeReturnObj("null", un
bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) { bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`); if (lnum === undefined || stmtnum === undefined) throw Error(`Line or statement number is undefined: (${lnum},${stmtnum})`);
let _debugExec = false; let _debugExec = true;
let _debugPrintCurrentLine = false; let _debugPrintCurrentLine = true;
let recWedge = "> ".repeat(recDepth); let recWedge = ">".repeat(recDepth) + " ";
if (_debugExec || _debugPrintCurrentLine) serial.println(recWedge+"@@ EXECUTE @@"); if (_debugExec || _debugPrintCurrentLine) serial.println(recWedge+"@@ EXECUTE @@");
if (_debugPrintCurrentLine && recDepth == 0) { if (_debugPrintCurrentLine && recDepth == 0) {
@@ -2594,7 +2598,6 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
serial.println(astToString(syntaxTree)); serial.println(astToString(syntaxTree));
} }
if (syntaxTree == undefined) return bF._troNOP(lnum, stmtnum); if (syntaxTree == undefined) return bF._troNOP(lnum, stmtnum);
else if (syntaxTree.astValue == undefined) { // empty meaningless parens else if (syntaxTree.astValue == undefined) { // empty meaningless parens
if (syntaxTree.astLeaves.length > 1) throw Error("WTF"); if (syntaxTree.astLeaves.length > 1) throw Error("WTF");
@@ -2613,9 +2616,9 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
if (_debugExec) { if (_debugExec) {
serial.println(recWedge+"testedval:"); serial.println(recWedge+"testedval:");
serial.println(recWedge+"type="+testedval.astType); serial.println(recWedge+"type="+testedval.troValue.astType);
serial.println(recWedge+"value="+testedval.astValue); serial.println(recWedge+"value="+testedval.troValue.astValue);
serial.println(recWedge+"nextLine="+testedval.astNextLine); serial.println(recWedge+"nextLine="+testedval.troValue.astNextLine);
} }
try { try {
@@ -2710,7 +2713,9 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
if (func === undefined) { if (func === undefined) {
var someVar = bStatus.vars[funcName]; var someVar = bStatus.vars[funcName];
//println(lnum+" _executeSyntaxTree: "+Object.entries(someVar)); if (DBGON) {
serial.println(lnum+" _executeSyntaxTree: "+Object.entries(someVar));
}
if (someVar === undefined) { if (someVar === undefined) {
throw lang.syntaxfehler(lnum, funcName + " is undefined"); throw lang.syntaxfehler(lnum, funcName + " is undefined");
@@ -2745,15 +2750,28 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
} }
} }
} }
// array indexing in the tree (caused by indexing array within recursive DEFUN)
else if (syntaxTree.astType == "array" && syntaxTree.astLeaves[0] !== undefined) {
let indexer = bStatus.getArrayIndexFun(lnum, stmtnum, "substituted array", syntaxTree.astValue);
let indices = syntaxTree.astLeaves.map(it=>bF._executeSyntaxTree(lnum, stmtnum, it, recDepth + 1));
let retVal = indexer(lnum, stmtnum, indices);
if (_debugExec) serial.println(recWedge+`indexing substituted array(${Object.entries(indices)}) = ${Object.entries(retVal)}`);
return new SyntaxTreeReturnObj(
JStoBASICtype(retVal),
retVal,
[lnum, stmtnum + 1]
);
}
else if (syntaxTree.astType == "num") { else if (syntaxTree.astType == "num") {
if (_debugExec) serial.println(recWedge+"num"); if (_debugExec) serial.println(recWedge+"num "+((syntaxTree.astValue)*1));
return new SyntaxTreeReturnObj(syntaxTree.astType, (syntaxTree.astValue)*1, [lnum, stmtnum + 1]); return new SyntaxTreeReturnObj(syntaxTree.astType, (syntaxTree.astValue)*1, [lnum, stmtnum + 1]);
} }
else if (syntaxTree.astType == "lit" || literalTypes.includes(syntaxTree.astType)) { else if (syntaxTree.astType == "lit" || literalTypes.includes(syntaxTree.astType)) {
if (_debugExec) serial.println(recWedge+"literal|string|bool|array"); if (_debugExec) serial.println(recWedge+"literal with astType: "+syntaxTree.astType+", astValue: "+syntaxTree.astValue);
return new SyntaxTreeReturnObj(syntaxTree.astType, syntaxTree.astValue, [lnum, stmtnum + 1]); return new SyntaxTreeReturnObj(syntaxTree.astType, syntaxTree.astValue, [lnum, stmtnum + 1]);
} }
else if (syntaxTree.astType == "null") { else if (syntaxTree.astType == "null") {
if (_debugExec) serial.println(recWedge+"null")
return bF._executeSyntaxTree(lnum, stmtnum, syntaxTree.astLeaves[0], recDepth + 1); return bF._executeSyntaxTree(lnum, stmtnum, syntaxTree.astLeaves[0], recDepth + 1);
} }
else { else {
@@ -2761,7 +2779,7 @@ bF._executeSyntaxTree = function(lnum, stmtnum, syntaxTree, recDepth) {
serial.println(recWedge+astToString(syntaxTree)); serial.println(recWedge+astToString(syntaxTree));
throw Error("Parse error"); throw Error("Parse error");
} }
}; }; // END OF bF._executeSyntaxTree
// @return ARRAY of BasicAST // @return ARRAY of BasicAST
bF._interpretLine = function(lnum, cmd) { bF._interpretLine = function(lnum, cmd) {
let _debugprintHighestLevel = false; let _debugprintHighestLevel = false;