From 65dca0533a0afbf63c6a333331a9c71b4a3ae7e6 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 29 Nov 2020 12:12:52 +0900 Subject: [PATCH] basic:abandoning the idea of (:) -- executor cannot index things statement-wise, only line-wise; major re-write should be possible but NOT TODAY --- assets/99.bas | 6 ++++-- assets/basic.js | 28 +++++++++++----------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/assets/99.bas b/assets/99.bas index 3d195a1..c5b8f28 100644 --- a/assets/99.bas +++ b/assets/99.bas @@ -1,7 +1,9 @@ 1 FOR I = 99 TO 1 -2 (MODE = 1):(GOSUB 12) +2 MODE = 1 +3 GOSUB 12 4 PRINT(I+" bottle"+BOTTLES$+" of beer on the wall, "+i+" bottle"+BOTTLES$+" of beer.") -5 (MODE = 2):(GOSUB 12) +5 MODE = 2 +6 GOSUB 12 7 PRINT("Take one down and pass it around, "+(i-1)+" bottle"+BOTTLES$+" of beer on the wall.") 8 NEXT 9 PRINT "No more bottles of beer on the wall, no more bottles of beer." diff --git a/assets/basic.js b/assets/basic.js index 8dd53a8..2fdbd01 100644 --- a/assets/basic.js +++ b/assets/basic.js @@ -421,9 +421,6 @@ if no arg text were given (e.g. "10 NEXT"), args will have zero length return lh.concat(rh); }); }, -":" : function(lnum, args) { // functional sequence - // TODO alternative method of impl: make syntax tree to have two or more consequent command "roots" and execute from there -}, "+" : function(lnum, args) { // addition, string concat return twoArg(lnum, args, function(lh, rh) { return lh + rh; }); }, @@ -532,14 +529,14 @@ if no arg text were given (e.g. "10 NEXT"), args will have zero length return oneArgNum(lnum, args, function(lh) { if (lh < 0) throw lang.syntaxfehler(lnum, lh); bStatus.gosubStack.push(lnum + 1); - println(lnum+" GOSUB into "+lh); + //println(lnum+" GOSUB into "+lh); return lh; }); }, "RETURN" : function(lnum, args) { var r = bStatus.gosubStack.pop(); if (r === undefined) throw lang.nowhereToReturn(lnum); - println(lnum+" RETURN to "+r); + //println(lnum+" RETURN to "+r); return r; }, "CLEAR" : function(lnum, args) { @@ -710,7 +707,7 @@ if no arg text were given (e.g. "10 NEXT"), args will have zero length }; Object.freeze(bStatus.builtin); let bF = {}; -bF._1os = {"!":1,"~":1,"#":1,"<":1,"=":1,">":1,"*":1,"+":1,"-":1,"/":1,"^":1,":":1}; +bF._1os = {"!":1,"~":1,"#":1,"<":1,"=":1,">":1,"*":1,"+":1,"-":1,"/":1,"^":1}; bF._2os = {"<":1,"=":1,">":1}; bF._uos = {"+":1,"-":1}; bF._isNum = function(code) { @@ -764,7 +761,6 @@ bF._opPrc = { "!":15,"~":15, // array CONS and PUSH "#": 16, // array concat "=":999, - ":":9999, // instructions separator }; bF._opRh = {"^":1,"=":1,"!":1}; bF._keywords = { @@ -1156,10 +1152,6 @@ bF._parserElaboration = function(lnum, tokens, states) { k += 1; } }; -/** - * @returns BasicAST - */ -bF._parseTokens = function(lnum, tokens, states, recDepth) { // DO NOT PERFORM SEMANTIC ANALYSIS HERE // at this point you can't (and shouldn't) distinguish whether or not defuns/variables are previously declared @@ -1224,6 +1216,10 @@ f Line 10 (function) for input "DEFUN sinc(x) = sin(x) / x" */ +/** + * @returns BasicAST + */ +bF._parseTokens = function(lnum, tokens, states, recDepth) { function isSemanticLiteral(token, state) { return "]" == token || ")" == token || @@ -1255,10 +1251,6 @@ for input "DEFUN sinc(x) = sin(x) / x" treeHead.astDepth = recDepth; treeHead.astLnum = lnum; - // TODO ability to parse arbitrary parentheses - // test string: print((minus(plus(3,2),times(8,7)))) - // ^ ^ these extra parens break your parser - // LITERAL if (tokens.length == 1 && (isSemanticLiteral(tokens[0], states[0]))) { // special case where there were only one word @@ -1272,7 +1264,7 @@ for input "DEFUN sinc(x) = sin(x) / x" } // else, screw it else { - throw lang.syntaxfehler(lnum); + throw lang.syntaxfehler(lnum, "TRAP_LITERALLY_LITERAL"); } } @@ -1305,7 +1297,7 @@ for input "DEFUN sinc(x) = sin(x) / x" } // generate tree - if (indexThen === undefined) throw lang.syntaxfehler(lnum); + if (indexThen === undefined) throw lang.syntaxfehler(lnum, "IF without THEN"); treeHead.astValue = "if"; treeHead.astType = "function"; @@ -1374,6 +1366,8 @@ for input "DEFUN sinc(x) = sin(x) / x" } } + // == AUTOPAREN == + // TODO do it properly by counting number of arguments and whatnot if (parenDepth != 0) throw lang.syntaxfehler(lnum, lang.unmatchedBrackets); if (_debugSyntaxAnalysis) serial.println("Paren position: "+parenStart+", "+parenEnd);