From 63e34924663df8ff06da3c737252e74aba40e422 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Thu, 25 Apr 2024 03:12:16 +0900 Subject: [PATCH] Fix > and < not working --- src/ts/parser.ts | 8 ++++++++ src/ts/process/infunctions.ts | 27 +++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ts/parser.ts b/src/ts/parser.ts index 71160b0b..050784b7 100644 --- a/src/ts/parser.ts +++ b/src/ts/parser.ts @@ -1233,6 +1233,10 @@ export function risuChatParser(da:string, arg:{ break } case '<':{ + if(stackType[nested.length] === 1){ + nested[0] += da[pointer] + break + } nested.unshift('') stackType[nested.length] = 2 break @@ -1319,6 +1323,10 @@ export function risuChatParser(da:string, arg:{ break } case '>':{ + if(stackType[nested.length] === 1){ + nested[0] += da[pointer] + break + } if(nested.length === 1 || stackType[nested.length] !== 2){ break } diff --git a/src/ts/process/infunctions.ts b/src/ts/process/infunctions.ts index c8ea1a9a..5b491368 100644 --- a/src/ts/process/infunctions.ts +++ b/src/ts/process/infunctions.ts @@ -14,12 +14,12 @@ function toRPN(expression:string) { }; expression = expression.replace(/\s+/g, ''); - let expression2 = expression.split(/([\+\-\*\/\^\%])/).filter(token => token); + let expression2 = expression.split(/([\+\-\*\/\^\%\>\<])/).filter(token => token); expression2.forEach(token => { if (parseFloat(token) || token === '0') { outputQueue += token + ' '; - } else if ('+-*/^%'.includes(token)) { + } else if ('+-*/^%><'.includes(token)) { while (operatorStack.length > 0 && ((operators[token].associativity === 'Left' && operators[token].precedence <= operators[operatorStack[operatorStack.length - 1]].precedence) || @@ -63,8 +63,27 @@ function calculateRPN(expression:string) { return stack.pop(); } -export function calcString(args:string) { - const expression = toRPN(args); +function executeRPNCalculation(text:string) { + const expression = toRPN(text); const evaluated = calculateRPN(expression); return evaluated } + +export function calcString(text:string) { + let depthText:string[] = [''] + + for(let i = 0; i < text.length; i++) { + if(text[i] === '(') { + depthText.push('') + } + else if(text[i] === ')' && depthText.length > 1) { + let result = executeRPNCalculation(depthText.pop()) + depthText[depthText.length - 1] += result + } + else { + depthText[depthText.length - 1] += text[i] + } + } + + return executeRPNCalculation(depthText.join('')) +}