[test] infunctions
This commit is contained in:
@@ -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",
|
||||
|
||||
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@@ -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]
|
||||
|
||||
84
src/ts/process/infunctions.ts
Normal file
84
src/ts/process/infunctions.ts
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user