From 9239eac78a52208dfd6ef7409610c47a7db77df3 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Wed, 24 May 2023 07:43:40 +0200 Subject: [PATCH] Fixing several function calls with no argument errors. + Functions max and min now throw an error if no arguments are provided. + Objects called now throw an error if no argument is provided. + Fixing tokenizer's seek error reporting system (leads to very rare types of errors). + Removing expression simplifications that could make the app freeze when no arguments are provided to a function. --- .../ad5001/LogarithmPlotter/js/expr-eval.js | 13 ++++++++--- .../LogarithmPlotter/js/parsing/common.js | 2 +- .../eu/ad5001/LogarithmPlotter/js/utils.js | 22 +++++++++---------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/expr-eval.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/expr-eval.js index 0df2661..6417e83 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/expr-eval.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/expr-eval.js @@ -241,7 +241,10 @@ function evaluate(tokens, expr, values) { nstack.push(f.apply(undefined, args)); } else if(f.execute) { // Objects & expressions execution - nstack.push(f.execute.apply(f, args)); + if(args.length >= 1) + nstack.push(f.execute.apply(f, args)); + else + throw new Error(qsTranslate('error', 'In order to be executed, object %1 must have at least one argument.').arg(f)) } else { throw new Error(qsTranslate('error', '%1 cannot be executed.').arg(f)); } @@ -1614,16 +1617,20 @@ function arrayIndex(array, index) { function max(array) { if (arguments.length === 1 && Array.isArray(array)) { return Math.max.apply(Math, array); - } else { + } else if(arguments.length >= 1) { return Math.max.apply(Math, arguments); + } else { + throw new EvalError(qsTranslate('error', 'Function %1 must have at least one argument.').arg('max')) } } function min(array) { if (arguments.length === 1 && Array.isArray(array)) { return Math.min.apply(Math, array); - } else { + } else if(arguments.length >= 1) { return Math.min.apply(Math, arguments); + } else { + throw new EvalError(qsTranslate('error', 'Function %1 must have at least one argument.').arg('min')) } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/common.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/common.js index 4e7f6fe..dab0969 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/common.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parsing/common.js @@ -36,7 +36,7 @@ class InputExpression { if(!this.atEnd() && this.peek() == char) { this.position++; } else { - this.raise("Unexpected character " + peek() + ". Expected character " + char); + this.raise("Unexpected character " + this.peek() + ". Expected character " + char); } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/utils.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/utils.js index ee53afa..9121661 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/utils.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/utils.js @@ -216,16 +216,16 @@ function simplifyExpression(str) { } ], // Simple simplifications - [/(\s|^|\()0(\.0+)? \* (\([^)(]+\))/g, '$10'], - [/(\s|^|\()0(\.0+)? \* ([^)(+-]+)/g, '$10'], - [/(\([^)(]\)) \* 0(\.0+)?(\s|$|\))/g, '0$3'], - [/([^)(+-]) \* 0(\.0+)?(\s|$|\))/g, '0$3'], - [/(\s|^|\()1(\.0+)? (\*|\/) /g, '$1'], - [/(\s|^|\()0(\.0+)? (\+|\-) /g, '$1'], - [/ (\*|\/) 1(\.0+)?(\s|$|\))/g, '$3'], - [/ (\+|\-) 0(\.0+)?(\s|$|\))/g, '$3'], - [/(^| |\() /g, '$1'], - [/ ($|\))/g, '$1'], + // [/(\s|^|\()0(\.0+)? \* (\([^)(]+\))/g, '$10'], + // [/(\s|^|\()0(\.0+)? \* ([^)(+-]+)/g, '$10'], + // [/(\([^)(]\)) \* 0(\.0+)?(\s|$|\))/g, '0$3'], + // [/([^)(+-]) \* 0(\.0+)?(\s|$|\))/g, '0$3'], + // [/(\s|^|\()1(\.0+)? (\*|\/) /g, '$1'], + // [/(\s|^|\()0(\.0+)? (\+|\-) /g, '$1'], + // [/ (\*|\/) 1(\.0+)?(\s|$|\))/g, '$3'], + // [/ (\+|\-) 0(\.0+)?(\s|$|\))/g, '$3'], + // [/(^| |\() /g, '$1'], + // [/ ($|\))/g, '$1'], ] // Replacements @@ -271,7 +271,7 @@ function makeExpressionReadable(str) { }] ] - str = simplifyExpression(str) + // str = simplifyExpression(str) // Replacements for(var replacement of replacements) while(replacement[0].test(str))