From 9851b8647d124a3d317eab613e859ee63fc62ca6 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sun, 25 Jun 2023 21:12:06 +0900 Subject: [PATCH] [test] infunctions --- package.json | 1 + pnpm-lock.yaml | 7 +++ src/ts/process/infunctions.ts | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/ts/process/infunctions.ts diff --git a/package.json b/package.json index 45581920..90b8acfb 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "@adobe/css-tools": "4.3.0-beta.2", "@dqbd/tiktoken": "^1.0.4", + "@mlc-ai/web-tokenizers": "^0.1.0", "@tauri-apps/api": "1.3.0", "@xenova/transformers": "^2.1.1", "blueimp-md5": "^2.19.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ba8e828..7af74a4a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,9 @@ dependencies: '@dqbd/tiktoken': specifier: ^1.0.4 version: 1.0.4 + '@mlc-ai/web-tokenizers': + specifier: ^0.1.0 + version: 0.1.0 '@tauri-apps/api': specifier: 1.3.0 version: 1.3.0 @@ -402,6 +405,10 @@ packages: '@jridgewell/sourcemap-codec': 1.4.14 dev: true + /@mlc-ai/web-tokenizers@0.1.0: + resolution: {integrity: sha512-whiQ+40ohtAFoFOGcje1Io7BMr434Wh3hM3nBCWlJMpXxL5Rlig/AH9wjyUPsytKwWTEe7RoYPyXSbFw5Vs6Tw==} + dev: false + /@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.2: resolution: {integrity: sha512-9bfjwDxIDWmmOKusUcqdS4Rw+SETlp9Dy39Xui9BEGEk19dDwH0jhipwFzEff/pFg95NKymc6TOTbRKcWeRqyQ==} cpu: [arm64] diff --git a/src/ts/process/infunctions.ts b/src/ts/process/infunctions.ts new file mode 100644 index 00000000..4c88d446 --- /dev/null +++ b/src/ts/process/infunctions.ts @@ -0,0 +1,84 @@ + +function toRPN(expression:string) { + let outputQueue = ''; + let operatorStack = []; + let operators = { + '+': {precedence: 2, associativity: 'Left'}, + '-': {precedence: 2, associativity: 'Left'}, + '*': {precedence: 3, associativity: 'Left'}, + '/': {precedence: 3, associativity: 'Left'} + }; + + expression = expression.replace(/\s+/g, ''); + let expression2 = expression.split(/([\+\-\*\/])/).filter(token => token); + + expression2.forEach(token => { + if (parseFloat(token) || token === '0') { + outputQueue += token + ' '; + } else if ('+-*/'.includes(token)) { + while (operatorStack.length > 0 && + ((operators[token].associativity === 'Left' && + operators[token].precedence <= operators[operatorStack[operatorStack.length - 1]].precedence) || + (operators[token].associativity === 'Right' && + operators[token].precedence < operators[operatorStack[operatorStack.length - 1]].precedence))) { + outputQueue += operatorStack.pop() + ' '; + } + + operatorStack.push(token); + } + }); + + while (operatorStack.length > 0) { + outputQueue += operatorStack.pop() + ' '; + } + + return outputQueue.trim(); +} + +function calculateRPN(expression:string) { + let stack = []; + + expression.split(' ').forEach(token => { + if (parseFloat(token) || token === '0') { + stack.push(parseFloat(token)); + } else { + let [b, a] = [stack.pop(), stack.pop()]; + switch (token) { + case '+': stack.push(a + b); break; + case '-': stack.push(a - b); break; + case '*': stack.push(a * b); break; + case '/': stack.push(a / b); break; + } + } + }); + + return stack.pop(); +} +function parseAndExecuteFunctions(s: string): string { + const functionRegex = /\$(\w+)\((.*?)\)/g; + + let match; + while ((match = functionRegex.exec(s)) !== null) { + let [fullMatch, funcName, args] = match; + + let result = ''; + switch (funcName) { + case 'calc': + result = calc(parseAndExecuteFunctions(args)); + break; + default: + break; + } + + s = s.replace(fullMatch, result.toString()); + functionRegex.lastIndex = 0; // Reset the regex + } + + return s; +} + +function calc(args:string) { + const expression = toRPN(args); + const evaluated = calculateRPN(expression); + return evaluated +}