Skip to content
Tcl Source Code Artifact [bea3d2fd3b]
Login

Artifact [bea3d2fd3b]

Artifact bea3d2fd3b0ce3cb9f4da43b2712d2e524b8ddb8c54052e7b0d01f1b88b5cf42:


--- a/generic/tclCompExpr.c
+++ b/generic/tclCompExpr.c
@@ -2968,6 +2968,33 @@
     TclEmitInstInt4(INST_LOAD_FIELD_NUM, lv, envPtr);
 }
 
+/*
+ * The functions of tcl::mathfunc that only read numbers: $a(x) as their
+ * argument is read as a number (loadFieldNum), like an operand of "+".
+ */
+static int
+NumericMathFunc(
+    const char *name,
+    Tcl_Size len)
+{
+    static const char *const names[] = {
+	"abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "cosh",
+	"double", "entier", "exp", "floor", "fmod", "hypot", "int", "isqrt",
+	"log", "log10", "max", "min", "pow", "round", "sin", "sinh", "sqrt",
+	"tan", "tanh", "wide", "isfinite", "isinf", "isnan", "isnormal",
+	"issubnormal", "isunordered", "signbit", NULL
+    };
+    int i;
+
+    for (i = 0; names[i] != NULL; i++) {
+	if ((Tcl_Size) strlen(names[i]) == len
+		&& memcmp(names[i], name, (size_t) len) == 0) {
+	    return 1;
+	}
+    }
+    return 0;
+}
+
 static void
 CompileExprTree(
     Tcl_Interp *interp,
@@ -2997,6 +3024,9 @@
     Tcl_Size tupleNode[DECL_MAX];
     Tcl_Obj *const *tupleLit[DECL_MAX];
     int nTupleLit = 0;
+    /* FUNCTION nodes of numeric functions (NumericMathFunc) */
+    Tcl_Size numFnNode[DECL_MAX];
+    int nNumFn = 0;
 
     while (1) {
 	int next;
@@ -3042,6 +3072,9 @@
 		TclDStringAppendLiteral(&cmdName, "tcl::mathfunc::");
 		p = TclGetStringFromObj(*funcObjv, &length);
 		funcObjv++;
+		if (nNumFn < DECL_MAX && NumericMathFunc(p, length)) {
+		    numFnNode[nNumFn++] = nodePtr - nodes;
+		}
 		Tcl_DStringAppend(&cmdName, p, length);
 		TclEmitPush(TclRegisterLiteral(envPtr,
 			Tcl_DStringValue(&cmdName),
@@ -3419,6 +3452,27 @@
 		case LEFT_SHIFT: case RIGHT_SHIFT:
 		    NumericFieldLoad(envPtr, start);
 		    break;
+		case FUNCTION: case COMMA: case OPEN_PAREN: {
+		    /* an argument of a numeric function: up the commas to the
+		     * function node */
+		    OpNode *fn = nodePtr;
+		    int k;
+
+		    while ((fn->lexeme == COMMA || fn->lexeme == OPEN_PAREN)
+			    && fn->p.parent >= 0) {
+			fn = nodes + fn->p.parent;
+		    }
+		    if (fn->lexeme != FUNCTION) {
+			break;
+		    }
+		    for (k = 0; k < nNumFn; k++) {
+			if (numFnNode[k] == fn - nodes) {
+			    NumericFieldLoad(envPtr, start);
+			    break;
+			}
+		    }
+		    break;
+		}
 		}
 	    }
 	    break;