diff --git a/Paramood/bridge/formatting.py b/Paramood/bridge/formatting.py index 3b38a86..59fb416 100644 --- a/Paramood/bridge/formatting.py +++ b/Paramood/bridge/formatting.py @@ -1,11 +1,10 @@ -from ..core.expressions import SCALAR, UNITS, TextValue +from ..core.expressions import TextValue, unit_definition def display_quantity(quantity, unit): if isinstance(quantity, TextValue): return quantity.value - if not unit and quantity.dimension == SCALAR: - return quantity.value - if unit not in UNITS or UNITS[unit][1] != quantity.dimension: - raise ValueError('Unsupported output unit') - return quantity.value / UNITS[unit][0] + factor, dimension, offset = unit_definition(unit) + if dimension != quantity.dimension: + raise ValueError('Incompatible output unit') + return (quantity.value - offset) / factor diff --git a/Paramood/bridge/protocol.py b/Paramood/bridge/protocol.py index f17ca3c..37f3727 100644 --- a/Paramood/bridge/protocol.py +++ b/Paramood/bridge/protocol.py @@ -3,7 +3,6 @@ from ..core.expressions import ( ExpressionError, LocalEvaluationUnsupported, - NondeterministicExpression, ) @@ -66,9 +65,7 @@ def dispatch(adapter, message): } except ExpressionError as exc: code = ( - 'NONDETERMINISTIC_EXPRESSION' - if isinstance(exc, NondeterministicExpression) - else 'LOCAL_EVALUATION_UNSUPPORTED' + 'LOCAL_EVALUATION_UNSUPPORTED' if isinstance(exc, LocalEvaluationUnsupported) else 'INVALID_EXPRESSION' ) diff --git a/Paramood/core/builtins.py b/Paramood/core/builtins.py index 5d558e9..5a4730b 100644 --- a/Paramood/core/builtins.py +++ b/Paramood/core/builtins.py @@ -8,4 +8,6 @@ ) FUNCTIONS = {item['name']: item for item in CATALOG['functions']} CONSTANT_NAMES = {item['name'] for item in CATALOG['constants']} -UNIT_NAMES = set(CATALOG['units']) +UNIT_NAMES = set(CATALOG['units']) | { + alias for unit in CATALOG['unitDefinitions'] for alias in unit['aliases'] +} diff --git a/Paramood/core/expression_catalog.json b/Paramood/core/expression_catalog.json index 262c116..1095cc3 100644 --- a/Paramood/core/expression_catalog.json +++ b/Paramood/core/expression_catalog.json @@ -261,12 +261,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "floor(-1.5)", "restriction": "" }, @@ -279,12 +279,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "ceil(1.5)", "restriction": "" }, @@ -297,12 +297,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "round(-1.5)", "restriction": "Halfway values round away from zero." }, @@ -315,8 +315,8 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, @@ -459,7 +459,7 @@ "arity": 0, "returns": "unitless", "example": "random()", - "restriction": "Changes when evaluated. Exact preview and batch application are unavailable; Quick Calculator uses native Fusion evaluation." + "restriction": "Preview shows a sample. Fusion evaluates the original expression again when applied or recomputed; the resulting value can change." }, { "name": "if", @@ -489,7 +489,7 @@ "arity": 3, "returns": "same as branches", "example": "if(2 mm < 3 mm; 10 mm; 20 mm)", - "restriction": "Three arguments; both branches must have matching types and dimensions." + "restriction": "Fusion evaluates only the selected branch. Unitless numeric branches can adopt the other branch dimension." } ], "constants": [ @@ -640,5 +640,1173 @@ "and", "or", "not" + ], + "unitDefinitions": [ + { + "name": "mm", + "dimension": [ + 1, + 0 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cm", + "dimension": [ + 1, + 0 + ], + "factor": 10.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "m", + "dimension": [ + 1, + 0 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hm", + "dimension": [ + 1, + 0 + ], + "factor": 100000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "micron", + "dimension": [ + 1, + 0 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "in", + "dimension": [ + 1, + 0 + ], + "factor": 25.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ft", + "dimension": [ + 1, + 0 + ], + "factor": 304.8, + "offset": 0.0, + "aliases": [] + }, + { + "name": "yd", + "dimension": [ + 1, + 0 + ], + "factor": 914.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mi", + "dimension": [ + 1, + 0 + ], + "factor": 1609344.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "nauticalMile", + "dimension": [ + 1, + 0 + ], + "factor": 1852000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mil", + "dimension": [ + 1, + 0 + ], + "factor": 0.025400000000000002, + "offset": 0.0, + "aliases": [] + }, + { + "name": "rad", + "dimension": [ + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "deg", + "dimension": [ + 0, + 1 + ], + "factor": 0.017453292519943295, + "offset": 0.0, + "aliases": [] + }, + { + "name": "grad", + "dimension": [ + 0, + 1 + ], + "factor": 0.015707963267948967, + "offset": 0.0, + "aliases": [] + }, + { + "name": "s", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "min", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 60.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hr", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 3600.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "acre", + "dimension": [ + 2, + 0 + ], + "factor": 4046856422.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "circular_mil", + "dimension": [ + 2, + 0 + ], + "factor": 0.0005067074791, + "offset": 0.0, + "aliases": [] + }, + { + "name": "rpm", + "dimension": [ + 0, + 1, + -1 + ], + "factor": 0.10471975511965977, + "offset": 0.0, + "aliases": [] + }, + { + "name": "farad", + "dimension": [ + -2, + 0, + 4, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "coulomb", + "dimension": [ + 0, + 0, + 1, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "S", + "dimension": [ + -2, + 0, + 3, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mho", + "dimension": [ + -2, + 0, + 3, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "dol", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "A", + "dimension": [ + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "J", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "erg", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 0.1, + "offset": 0.0, + "aliases": [] + }, + { + "name": "calorie", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 4184000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Btu", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 1055055852.62, + "offset": 0.0, + "aliases": [] + }, + { + "name": "V", + "dimension": [ + 2, + 0, + -3, + 1, + -1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ccs", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [ + "CCS" + ] + }, + { + "name": "cis", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 16387.064, + "offset": 0.0, + "aliases": [ + "CIS" + ] + }, + { + "name": "cfm", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 471947.44320000004, + "offset": 0.0, + "aliases": [ + "CFM" + ] + }, + { + "name": "cmh", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 277777.77777777775, + "offset": 0.0, + "aliases": [ + "CMH" + ] + }, + { + "name": "gpm", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 63090.19639999999, + "offset": 0.0, + "aliases": [ + "GPM" + ] + }, + { + "name": "lph", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 277.77777777777777, + "offset": 0.0, + "aliases": [ + "LPH" + ] + }, + { + "name": "N", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "dyne", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 0.01, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lbforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 4448.221615260501, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ounceforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 278.01385095, + "offset": 0.0, + "aliases": [] + }, + { + "name": "tonforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 8896443.230521, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Hz", + "dimension": [ + 0, + 0, + -1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lx", + "dimension": [ + -2, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "H", + "dimension": [ + 2, + 0, + -2, + 1, + -2 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cd", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "EV", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lm", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Wb", + "dimension": [ + 2, + 0, + -2, + 1, + -1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "maxwell", + "dimension": [ + 2, + 0, + -2, + 1, + -1 + ], + "factor": 0.01, + "offset": 0.0, + "aliases": [] + }, + { + "name": "T", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gamma", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 1e-09, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gauss", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 0.0001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "oersted", + "dimension": [ + -1, + 0, + 0, + 0, + 1 + ], + "factor": 0.07957747200000001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "g", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "kg", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "slug", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 14.5939029372, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lbmass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.45359237, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ouncemass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.028349523125, + "offset": 0.0, + "aliases": [] + }, + { + "name": "tonmass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 907.18474, + "offset": 0.0, + "aliases": [] + }, + { + "name": "pcs", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "W", + "dimension": [ + 2, + 0, + -3, + 1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hp", + "dimension": [ + 2, + 0, + -3, + 1 + ], + "factor": 745699871.582, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Pa", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "psi", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 6.894757293178, + "offset": 0.0, + "aliases": [] + }, + { + "name": "psf", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.047880259, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ksi", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 6894.7572931780005, + "offset": 0.0, + "aliases": [] + }, + { + "name": "bar", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 100.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "atm", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 101.325, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.24908891, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ftH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 2.9890669200000004, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 9.806650000000001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mmHg", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.133322387415, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inHg", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 3.386388, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ohm", + "dimension": [ + 2, + 0, + -3, + 1, + -2 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "sr", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mps", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "fps", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 304.8, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mph", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 447.04, + "offset": 0.0, + "aliases": [] + }, + { + "name": "knots", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 514.4444444444445, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mmpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 0.016666666666666666, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 16.666666666666668, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 0.42333333333333334, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ftpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 5.08, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mole", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "K", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "C", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 273.15, + "aliases": [] + }, + { + "name": "F", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 0.5555555555555429, + "offset": 255.3722222222222, + "aliases": [] + }, + { + "name": "R", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 0.5555555555555556, + "offset": 0.0, + "aliases": [] + }, + { + "name": "poise", + "dimension": [ + -1, + 0, + -1, + 1 + ], + "factor": 0.0001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "l", + "dimension": [ + 3, + 0 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gal", + "dimension": [ + 3, + 0 + ], + "factor": 3785411.784, + "offset": 0.0, + "aliases": [] + }, + { + "name": "qt", + "dimension": [ + 3, + 0 + ], + "factor": 946352.946, + "offset": 0.0, + "aliases": [] + }, + { + "name": "pt", + "dimension": [ + 3, + 0 + ], + "factor": 473176.473, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cup", + "dimension": [ + 3, + 0 + ], + "factor": 236588.2365, + "offset": 0.0, + "aliases": [] + }, + { + "name": "fl_oz", + "dimension": [ + 3, + 0 + ], + "factor": 29573.5295625, + "offset": 0.0, + "aliases": [] + } ] } diff --git a/Paramood/core/expressions.py b/Paramood/core/expressions.py index 741af8d..d5a9bab 100644 --- a/Paramood/core/expressions.py +++ b/Paramood/core/expressions.py @@ -9,15 +9,15 @@ import ast import math import re -from dataclasses import dataclass +from dataclasses import dataclass, replace from typing import ClassVar, Mapping, Tuple from itertools import zip_longest -from .builtins import FUNCTIONS, CONSTANT_NAMES, UNIT_NAMES +from .builtins import CATALOG, FUNCTIONS, CONSTANT_NAMES, UNIT_NAMES from .text import decode_literal, map_code, normalize_literals, segments, split_definitions -Dimension = Tuple[int, ...] # powers of length, angle, and optional time +Dimension = Tuple[float, ...] # powers of length, angle, and optional time SCALAR = (0, 0) LENGTH = (1, 0) ANGLE = (0, 1) @@ -25,24 +25,16 @@ MAX_TEXT_LENGTH = 65536 MAX_BATCH_TEXT_LENGTH = 262144 UNITS = { - 'mm': (1.0, LENGTH), - 'cm': (10.0, LENGTH), - 'm': (1000.0, LENGTH), - 'in': (25.4, LENGTH), - 'ft': (304.8, LENGTH), - 'deg': (math.pi / 180, ANGLE), - 'rad': (1.0, ANGLE), - 'n': (1.0, SCALAR), - 'hm': (100000.0, LENGTH), - 'micron': (0.001, LENGTH), - 'yd': (914.4, LENGTH), - 'mi': (1609344.0, LENGTH), - 'nauticalMile': (1852000.0, LENGTH), - 'mil': (0.0254, LENGTH), - 'grad': (math.pi / 200, ANGLE), - 's': (1.0, (0, 0, 1)), - 'min': (60.0, (0, 0, 1)), - 'hr': (3600.0, (0, 0, 1)), + item['name']: (item['factor'], tuple(item['dimension'])) for item in CATALOG['unitDefinitions'] +} +for item in CATALOG['unitDefinitions']: + for alias in item['aliases']: + UNITS[alias] = (item['factor'], tuple(item['dimension'])) +UNITS['n'] = (1.0, SCALAR) +UNIT_OFFSETS = { + name: item['offset'] + for item in CATALOG['unitDefinitions'] + for name in [item['name'], *item['aliases']] } _UNIT_PATTERN = '|'.join(re.escape(u) for u in sorted(UNIT_NAMES | {'n'}, key=len, reverse=True)) _LITERAL = re.compile( @@ -76,29 +68,61 @@ class LocalEvaluationUnsupported(ExpressionError): """Valid Fusion syntax whose result cannot be established by the local evaluator.""" -class NondeterministicExpression(LocalEvaluationUnsupported): - """A stable preview cannot be promised for an expression containing random().""" - - def _dimension(values): - result = list(values) + result = [round(v) if math.isclose(v, round(v), abs_tol=1e-7) else round(v, 8) for v in values] while len(result) > 2 and result[-1] == 0: result.pop() return tuple(result) def dimension_unit(dimension, length_unit='mm'): - """Serialize length/angle/time dimensions without inferring physical types.""" - if len(dimension) > 3: - raise LocalEvaluationUnsupported('Unsupported physical dimension') + """Serialize native dimensions; all base units come from the Fusion catalog.""" + bases = (length_unit, 'rad', 's', 'kg', 'A', 'K', 'mole', 'cd', 'dol', 'pcs', 'sr') + if len(dimension) > len(bases): + raise LocalEvaluationUnsupported('Unknown Fusion base dimension') numerator, denominator = [], [] - for unit, power in zip((length_unit, 'rad', 's'), dimension): + for unit, power in zip(bases, dimension): if power: magnitude = abs(power) - part = unit if magnitude == 1 else unit + '^' + str(magnitude) + part = unit if magnitude == 1 else unit + '^' + format(magnitude, '.8g') (numerator if power > 0 else denominator).append(part) + if not numerator and denominator: + # Fusion unit strings need a unit numerator, not the numeric token "1". + numerator.append(length_unit) + denominator.insert(0, length_unit) top = '*'.join(numerator) - return (top or '1') + '/' + '/'.join(denominator) if denominator else top + return top + '/' + '/'.join(denominator) if denominator else top + + +def unit_definition(unit): + if not unit: + return 1.0, SCALAR, 0.0 + if unit in UNITS: + factor, dim = UNITS[unit] + return factor, dim, UNIT_OFFSETS.get(unit, 0.0) + quantity = evaluate('1 ' + unit, {}) + if isinstance(quantity, TextValue): + raise ExpressionError('Expected a numeric unit') + return quantity.value, quantity.dimension, 0.0 + + +def stochastic_expression(expression, parameters=None): + tree = _tree(expression) + return any( + isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == 'random' + for n in ast.walk(tree) + ) or any( + getattr((parameters or {}).get(name), 'stochastic', False) for name in _dependencies(tree) + ) + + +def mark_stochastic(expressions, dependencies): + names = {name for name, expr in expressions.items() if stochastic_expression(expr)} + while True: + expanded = names | {name for name, deps in dependencies.items() if deps & names} + if expanded == names: + return names + names = expanded def _assignment(item: str) -> tuple[str, str, str]: @@ -131,11 +155,13 @@ def replace(match): class Quantity: value: float dimension: Dimension = SCALAR + stochastic: bool = False @dataclass(frozen=True) class TextValue: value: str + stochastic: bool = False dimension: ClassVar[str] = TEXT @@ -174,8 +200,6 @@ def code(raw): lambda m: '(' + m.group(1) + '*__unit__(1,"' + m.group(2) + '")**(' + m.group(3) + '))', raw, ) - if re.search(r'\b(' + _UNIT_PATTERN + r')\s*\*\*\s*[A-Za-z_(]', raw): - raise LocalEvaluationUnsupported('Use pow() for nonliteral unit exponents') raw = _LITERAL.sub(lambda m: '__unit__(' + m.group(1) + ',"' + m.group(2) + '")', raw) return re.sub( r'(?<=[\w)])\s+(' + _UNIT_PATTERN + r')\b(?!\s*\()', @@ -211,7 +235,10 @@ def evaluate( """Evaluate supported operations, rejecting unknown units/functions and dimensions.""" if len(expression) > 10000: raise ExpressionError('Expression is too long') - tree = _tree(normalize_expression(expression, parameters)) + expression = normalize_expression(expression, parameters) + if hasattr(native_function, 'evaluate'): + return native_function.evaluate(expression, parameters) + tree = _tree(expression) if sum(1 for _ in ast.walk(tree)) > 2000: raise ExpressionError('Expression is too complex') for call in ast.walk(tree): @@ -222,14 +249,6 @@ def evaluate( ): if len(call.args) != FUNCTIONS[call.func.id]['arity'] or call.keywords: raise ExpressionError('Invalid function arguments') - # Reject random even in an unselected conditional branch: recompute is unstable. - if not allow_nondeterministic and any( - isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == 'random' - for n in ast.walk(tree) - ): - raise NondeterministicExpression( - 'random() has no stable preview. Use Quick Calculator for a native sample.' - ) def logical(node): if isinstance(node, ast.BoolOp) and isinstance(node.op, (ast.And, ast.Or)): @@ -261,13 +280,9 @@ def power(a, b): if abs(exponent) > 10000: raise ExpressionError('Exponent is too large') dim = [round(d * exponent, 8) for d in a.dimension] - if any(not math.isclose(d, round(d), abs_tol=1e-8) for d in dim): - raise LocalEvaluationUnsupported( - 'Fractional resulting dimensions require native Fusion evaluation' - ) if a.value < 0 and exponent != int(exponent): raise ExpressionError('Power must have a real result') - return Quantity(math.pow(a.value, exponent), _dimension(round(d) for d in dim)) + return Quantity(math.pow(a.value, exponent), _dimension(dim)) def calc(node: ast.AST) -> Quantity | TextValue: if isinstance(node, ast.Constant) and isinstance(node.value, str): @@ -359,7 +374,7 @@ def calc(node: ast.AST) -> Quantity | TextValue: if q.dimension != SCALAR: raise ExpressionError('Unit requires a dimensionless number') factor, dim = UNITS[unit] - return Quantity(q.value * factor, dim) + return Quantity(q.value * factor + UNIT_OFFSETS.get(unit, 0), dim) if name not in _FUNCTIONS: raise ExpressionError('Unknown function: ' + name) args = [calc(arg) for arg in node.args] @@ -368,9 +383,11 @@ def calc(node: ast.AST) -> Quantity | TextValue: if len(args) != FUNCTIONS.get(name, {}).get('arity'): raise ExpressionError('Invalid function arguments') if name == 'random': - if native_function and allow_nondeterministic: + if native_function: return native_function(name, args, SCALAR) - raise NondeterministicExpression('random() requires native Fusion evaluation') + raise LocalEvaluationUnsupported( + 'Native Fusion evaluation is required for random()' + ) q = args[0] if name == 'pow': return power(q, args[1]) @@ -379,9 +396,9 @@ def calc(node: ast.AST) -> Quantity | TextValue: raise ExpressionError('Incompatible dimensions') return Quantity((min if name == 'min' else max)(a.value for a in args), q.dimension) if name == 'sqrt': - if any(d % 2 for d in q.dimension) or q.value < 0: + if q.value < 0: raise ExpressionError('Invalid sqrt dimension or value') - return Quantity(math.sqrt(q.value), _dimension(d // 2 for d in q.dimension)) + return Quantity(math.sqrt(q.value), _dimension(d / 2 for d in q.dimension)) if name in {'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh'}: if q.dimension == SCALAR: if native_function: @@ -416,7 +433,7 @@ def calc(node: ast.AST) -> Quantity | TextValue: raise ExpressionError('Result must be finite') if isinstance(result, TextValue) and len(result.value) > MAX_TEXT_LENGTH: raise ExpressionError('Text result exceeds 65536 characters') - return result + return replace(result, stochastic=stochastic_expression(expression, parameters)) def _increment_unit( @@ -452,7 +469,7 @@ def resolve(name: str) -> None: return default_unit if dimension == ANGLE: return 'deg' - raise ExpressionError('Unsupported resulting dimension') + return dimension_unit(dimension, default_unit) def _fold_assignments( @@ -472,8 +489,18 @@ def _fold_assignments( def numeric_literal(name: str, expression: str) -> str: if not _NUMERIC.fullmatch(expression): return expression + if name in existing and isinstance(existing[name], TextValue): + raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) # A scalar's empty display unit is meaningful, not a missing length unit. - unit = 'n' if name in existing and existing[name].dimension == SCALAR else default_unit + unit = ( + ( + 'n' + if existing[name].dimension == SCALAR + else units.get(name) or dimension_unit(existing[name].dimension, default_unit) + ) + if name in existing + else default_unit + ) return expression + ' ' + unit initial: dict[str, str] = {} @@ -503,7 +530,9 @@ def numeric_literal(name: str, expression: str) -> str: if operator in '+-' and _NUMERIC.fullmatch(expression): unit = units.get(name) if unit is None: - unit = _increment_unit(previous, {**initial, **raw}, existing, default_unit) + unit = _increment_unit( + previous, {**initial, **raw}, existing, default_unit, native_function + ) if unit: expression += ' ' + unit expression = replacement + operator + '(' + expression + ')' @@ -557,8 +586,6 @@ def visit(name: str) -> None: text_size += len(value.value) if text_size > MAX_BATCH_TEXT_LENGTH: raise ExpressionError('Combined text results exceed 262144 characters') - if value.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported resulting dimension') if name in existing and existing[name].dimension != value.dimension: raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) fusion_expr = map_code(expr, lambda code: re.sub(r'(?<=\d)\s*n\b', '', code)) @@ -566,7 +593,13 @@ def visit(name: str) -> None: default_unit if value.dimension == LENGTH else ( - 'deg' if value.dimension == ANGLE else ('Text' if value.dimension == TEXT else '') + 'deg' + if value.dimension == ANGLE + else ( + 'Text' + if value.dimension == TEXT + else dimension_unit(value.dimension, default_unit) + ) ) ) if name not in existing: @@ -707,13 +740,11 @@ def plan_batch_snapshot( original_exprs: dict[str, str] = {} original_graph: dict[str, set[str]] = {} for name, item in snapshot.items(): - if item.quantity.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported snapshot dimension: ' + name) if isinstance(item.quantity, TextValue): if item.unit != 'Text': raise ExpressionError('Invalid text snapshot unit: ' + name) elif item.unit: - if item.unit not in UNITS or UNITS[item.unit][1] != item.quantity.dimension: + if unit_definition(item.unit)[1] != item.quantity.dimension: raise ExpressionError('Unsupported snapshot unit: ' + name) elif item.quantity.dimension != SCALAR: raise ExpressionError('Missing snapshot unit: ' + name) @@ -742,7 +773,9 @@ def plan_batch_snapshot( else not isinstance(expected, TextValue) and math.isclose(actual.value, expected.value, rel_tol=1e-10, abs_tol=1e-10) ) - if actual.dimension != expected.dimension or not same_value: + if actual.dimension != expected.dimension or ( + not same_value and not actual.stochastic and not expected.stochastic + ): raise ExpressionError('Inconsistent snapshot value: ' + name) raw = _fold_assignments( text, @@ -781,8 +814,6 @@ def visit(name: str) -> None: if name in raw: expr = raw[name] value = evaluate(expr, values, native_function) - if value.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported resulting dimension') if name in snapshot and snapshot[name].quantity.dimension != value.dimension: raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) unit = ( @@ -791,7 +822,11 @@ def visit(name: str) -> None: else ( 'deg' if value.dimension == ANGLE - else ('Text' if value.dimension == TEXT else '') + else ( + 'Text' + if value.dimension == TEXT + else dimension_unit(value.dimension, default_unit) + ) ) ) if name not in snapshot: diff --git a/Paramood/core/text.py b/Paramood/core/text.py index 6f28b17..9f8e8a5 100644 --- a/Paramood/core/text.py +++ b/Paramood/core/text.py @@ -1,7 +1,40 @@ """Quote-aware Fusion string syntax; never evaluate arbitrary Python input.""" +def normalize_unit_symbols(source): + """Quotes following a number are native inch/foot suffixes, not text starts.""" + result, quote, escaped, previous = [], '', False, '' + for i, char in enumerate(source): + if quote: + result.append(char) + if escaped: + escaped = False + elif char == '\\': + escaped = True + elif char == quote: + quote = '' + previous = char + continue + if char in '\'"' and previous and (previous.isdigit() or previous == ')'): + result.append(' in' if char == '"' else ' ft') + elif char in '\'"': + quote = char + result.append(char) + elif ( + char in 'µ°' + and not (i + 1 < len(source) and source[i + 1].isalpha()) + and not previous.isalpha() + ): + result.append(' micron' if char == 'µ' else ' deg') + else: + result.append(char) + if not char.isspace(): + previous = char + return ''.join(result) + + def segments(source): + source = normalize_unit_symbols(source) start, i = 0, 0 while i < len(source): if source[i] not in '\'"': diff --git a/Paramood/fusion/adapter.py b/Paramood/fusion/adapter.py index 6d08b53..f09df43 100644 --- a/Paramood/fusion/adapter.py +++ b/Paramood/fusion/adapter.py @@ -5,21 +5,22 @@ import math import re import uuid +from dataclasses import replace import adsk.core import adsk.fusion from ..core.expressions import ( ANGLE, - ExpressionError, LENGTH, SCALAR, TextValue, - UNITS, ParameterSnapshot, Quantity, dependency_closure, dimension_unit, + unit_definition, + mark_stochastic, evaluate, normalize_expression, plan_batch, @@ -29,6 +30,7 @@ from ..bridge.protocol import BridgeError from ..bridge.formatting import display_quantity from .variants import VariantsMixin +from .expression_engine import NativeExpressionEngine from ..core.text import map_code, normalize_literals, split_definitions, text_literal @@ -52,19 +54,14 @@ def _value_expression(parameter): raise ValueError('Non-finite parameter value') if not unit: return repr(value) + 'n' - if unit in UNITS and UNITS[unit][1] == LENGTH: - base = value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE: - base = value - else: - raise ValueError('Unsupported parameter unit') - factor = UNITS[unit][0] - converted = base / factor + factor, dimension, offset = unit_definition(unit) + base = value * 10.0 ** dimension[0] + converted = (base - offset) / factor if not math.isfinite(converted): raise ValueError('Non-finite converted value') if abs(converted) < 1e12: integer = round(converted) - if integer * factor == base: + if integer * factor + offset == base: converted = float(integer) return repr(converted) + ' ' + unit @@ -145,21 +142,27 @@ def _quantities(self, design): for parameter in design.allParameters: if _is_text(parameter): result[parameter.name] = TextValue(parameter.textValue) - continue - unit = parameter.unit - if unit not in UNITS and unit != '': - continue - try: - value = parameter.value - if unit == '': - result[parameter.name] = Quantity(value, SCALAR) - elif UNITS[unit][1] == LENGTH: - result[parameter.name] = Quantity(value * 10.0, LENGTH) - elif UNITS[unit][1] == ANGLE: - result[parameter.name] = Quantity(value, ANGLE) - except Exception: - continue - return result + else: + _, dimension, _ = unit_definition(parameter.unit) + result[parameter.name] = Quantity(parameter.value * 10.0 ** dimension[0], dimension) + dynamic = self._stochastic_parameters(design) + return {name: replace(value, stochastic=name in dynamic) for name, value in result.items()} + + @staticmethod + def _stochastic_parameters(design): + expressions = {p.name: p.expression for p in design.allParameters} + dependencies = {} + for parameter in design.allParameters: + # API dependency lists are preferred. Lightweight offline fixtures have none. + native = getattr(parameter, 'dependencyParameters', None) + if native is not None: + dependencies[parameter.name] = {p.name for p in native} + else: + dependencies[parameter.name] = ( + set(re.findall(r'[A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9]*', parameter.expression)) + & expressions.keys() + ) + return mark_stochastic(expressions, dependencies) def _dependency_snapshot(self, design): """Read a strict graph for future update planning; it does not authorize writes.""" @@ -199,14 +202,9 @@ def names_of(items): dimension, value = 'text', parameter.textValue if not isinstance(value, str) or unit != 'Text': raise ValueError('Invalid text parameter: ' + name) - elif unit == '': - dimension, value = SCALAR, parameter.value - elif unit in UNITS and UNITS[unit][1] == LENGTH: - dimension, value = LENGTH, parameter.value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE: - dimension, value = ANGLE, parameter.value else: - raise ValueError('Unsupported parameter unit: ' + name) + _, dimension, _ = unit_definition(unit) + value = parameter.value * 10.0 ** dimension[0] if not text and not math.isfinite(value): raise ValueError('Non-finite parameter value: ' + name) dependencies[name] = names_of(parameter.dependencyParameters) @@ -227,7 +225,11 @@ def names_of(items): for name in by_name: if actual_dependents[name] != expected_dependents[name]: raise ValueError('Inconsistent parameter dependency graph: ' + name) - return result + dynamic = self._stochastic_parameters(design) + return { + name: replace(item, quantity=replace(item.quantity, stochastic=name in dynamic)) + for name, item in result.items() + } except Exception as exc: raise BridgeError( 'INCOMPLETE_SNAPSHOT', 'Cannot verify parameter dependencies: ' + str(exc) @@ -371,6 +373,7 @@ def state(self): def _snapshot_token(self, design, text): parameters = [] + dynamic = self._stochastic_parameters(design) for param in design.allParameters: try: value = param.textValue if _is_text(param) else param.value @@ -378,7 +381,9 @@ def _snapshot_token(self, design, text): value = ( None # Text/unsupported parameter, expression is still part of the snapshot. ) - parameters.append((param.name, param.expression, param.unit, value)) + parameters.append( + (param.name, param.expression, param.unit, None if param.name in dynamic else value) + ) payload = [ self._document_id, text, @@ -405,12 +410,45 @@ def _editable_parameter(self, design, name): else None ) + def _normalize_unit_aliases(self, text, design): + if not callable(getattr(design.fusionUnitsManager, 'formatUnits', None)): + return text + known = {p.name for p in design.allParameters} + known.update( + match.group(1).replace('.', '_') + for match in re.finditer( + r'(?:^|[;\n])\s*([A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9.]*)\s*[+*/-]?=', text + ) + ) + cache = {} + + def normalize(code): + def alias(match): + name = match.group() + if name in known or name[0].isdigit() or name.startswith('.') or '.' in name: + return name + if name not in cache: + try: + cache[name] = design.fusionUnitsManager.formatUnits(name) or name + except RuntimeError: + cache[name] = name + return cache[name] + + return re.sub( + r'(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|[A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9]*(?:\.[A-Za-zА-Яа-яЁё_0-9]+)*', + alias, + code, + ) + + return map_code(text, normalize) + def _plan(self, text): design = self._design() if not design: raise BridgeError('NO_DESIGN', 'Open a Fusion design first.') if not isinstance(text, str) or len(text) > 10000: raise BridgeError('BAD_REQUEST', 'Expression is missing or too long.') + text = self._normalize_unit_aliases(text, design) names = [ re.sub(r'[+*/-]$', '', item.split('=', 1)[0].strip()).strip().replace('.', '_') for item in split_definitions(text) @@ -523,44 +561,39 @@ def _execute_new_batch(self, design, ops): return {'created': created_names, 'updated': updated_names} def _native_function(self, design): - """Evaluate preference-sensitive calls only on an explicit bridge request.""" - - def evaluate_call(name, arguments, dimension): - # This callback currently handles unitless arguments and unitless results. - # It never creates temporary parameters or changes the active document. - if dimension != SCALAR or any(arg.dimension != SCALAR for arg in arguments): - raise ExpressionError('Unsupported native function dimension') - expression = name + '(' + ';'.join(format(arg.value, '.17g') for arg in arguments) + ')' - manager = design.fusionUnitsManager - if not manager.isValidExpression(expression, ''): - raise ExpressionError('Fusion cannot validate function: ' + name) - value = manager.evaluateExpression(expression, '') - if not math.isfinite(value): - raise ExpressionError('Fusion returned a non-finite result') - return Quantity(value, dimension) - - return evaluate_call + manager = design.fusionUnitsManager + return ( + NativeExpressionEngine(manager, self._quantities(design)) + if hasattr(manager, 'evaluateExpression') + else None + ) def _verify_value(self, parameter, op): unit = parameter.unit if isinstance(op.quantity, TextValue): - if not _is_text(parameter) or parameter.textValue != op.quantity.value: + if not _is_text(parameter) or ( + not op.quantity.stochastic and parameter.textValue != op.quantity.value + ): raise BridgeError( 'RESULT_MISMATCH', 'Fusion text differs from preview for ' + op.name + '.' ) return if _is_text(parameter): raise BridgeError('TYPE_MISMATCH', 'Cannot assign a number to a text parameter.') - if unit == '' and op.quantity.dimension == SCALAR: - actual = parameter.value - elif unit in UNITS and UNITS[unit][1] == LENGTH and op.quantity.dimension == LENGTH: - actual = parameter.value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE and op.quantity.dimension == ANGLE: - actual = parameter.value - else: + _, dimension, _ = unit_definition(unit) + if dimension != op.quantity.dimension: raise BridgeError( 'RESULT_MISMATCH', 'Fusion returned an unexpected unit for ' + op.name + '.' ) + actual = parameter.value * 10.0 ** dimension[0] + if not math.isfinite(actual): + raise BridgeError( + 'RESULT_MISMATCH', 'Fusion returned a non-finite value for ' + op.name + '.' + ) + if op.quantity.stochastic: + # The expression and type are verified; the sampled preview value is + # deliberately not compared with a fresh native random evaluation. + return if not math.isclose(actual, op.quantity.value, rel_tol=1e-9, abs_tol=1e-9): raise BridgeError( 'RESULT_MISMATCH', 'Fusion result differs from preview for ' + op.name + '.' @@ -787,6 +820,10 @@ def request(self, action, payload): 'value': display_quantity(op.quantity, op.unit), 'unit': op.unit, 'status': op.status, + 'estimated': op.quantity.stochastic, + 'message': 'Sample value. Fusion will evaluate this expression again when applied.' + if op.quantity.stochastic + else None, } for op in ops ], @@ -818,6 +855,10 @@ def request(self, action, payload): 'value': display_quantity(op.quantity, op.unit), 'unit': op.unit, 'status': op.status, + 'estimated': op.quantity.stochastic, + 'message': 'Sample value. Fusion will evaluate this expression again when applied.' + if op.quantity.stochastic + else None, 'previous': str(display_quantity(op.previous, op.unit)) if op.previous else None, @@ -876,7 +917,9 @@ def request(self, action, payload): if not isinstance(expression, str) or len(expression) > 10000: raise BridgeError('BAD_REQUEST', 'Expression is missing or too long.') quantities = self._quantities(design) - expression = normalize_expression(expression, quantities) + expression = self._normalize_unit_aliases( + normalize_expression(expression, quantities), design + ) quantity = evaluate( expression, quantities, self._native_function(design), allow_nondeterministic=True ) @@ -891,9 +934,7 @@ def request(self, action, payload): dimension_unit(quantity.dimension, design.fusionUnitsManager.defaultLengthUnits), ) manager = design.fusionUnitsManager - if not manager.isValidExpression(expression, unit): - raise BridgeError('INVALID_EXPRESSION', 'Fusion cannot validate this expression.') - value = manager.evaluateExpression(expression, unit) + value = quantity.value / (10.0 ** quantity.dimension[0]) formatted = manager.formatValue(value, unit) if unit else str(value) display_value = ( value diff --git a/Paramood/fusion/expression_engine.py b/Paramood/fusion/expression_engine.py new file mode 100644 index 0000000..f5e0573 --- /dev/null +++ b/Paramood/fusion/expression_engine.py @@ -0,0 +1,211 @@ +"""Native numeric evaluation with symbolic unit inference and no model mutations. + +Fusion evaluates the original formula. The AST only supplies a result-unit hint +and safely substitutes values of parameters that have not been created yet. +""" + +import ast +import math +import re +from itertools import zip_longest + +from ..core.expressions import ( + ANGLE, + SCALAR, + TEXT, + Quantity, + TextValue, + ExpressionError, + _dimension, + _tree, + dimension_unit, + unit_definition, + stochastic_expression, + evaluate as offline_evaluate, +) +from ..core.text import map_code, text_literal + + +class NativeExpressionEngine: + def __init__(self, manager, baseline=None): + self.manager = manager + self.baseline = baseline or {} + + def __call__(self, name, arguments, dimension): + source = name + '(' + ';'.join(self.literal(arg) for arg in arguments) + ')' + return self.numeric(source, dimension) + + @staticmethod + def literal(quantity): + if isinstance(quantity, TextValue): + return text_literal(quantity.value) + unit = dimension_unit(quantity.dimension) + return '(' + format(quantity.value, '.17g') + (' ' + unit if unit else '') + ')' + + def numeric(self, source, dimension): + unit = dimension_unit(dimension) + if not self.manager.isValidExpression(source, unit): + raise ExpressionError('Fusion cannot validate this expression.') + value = self.manager.evaluateExpression(source, unit) + # Fusion's internal length is cm; the planner retains mm, including derived units. + value *= 10.0 ** dimension[0] + if not math.isfinite(value): + raise ExpressionError('Fusion returned a non-finite result') + return Quantity(value, dimension) + + def evaluate(self, expression, parameters): + tree = _tree(expression) + stochastic = stochastic_expression(expression, parameters) + + def render(node): + if isinstance(node, ast.Constant): + return text_literal(node.value) if isinstance(node.value, str) else repr(node.value) + if isinstance(node, ast.Name): + return ( + self.literal(parameters[node.id]) + if node.id in parameters and parameters[node.id] != self.baseline.get(node.id) + else node.id + ) + if isinstance(node, ast.UnaryOp): + op = {ast.USub: '-', ast.UAdd: '+', ast.Not: 'not '}.get(type(node.op)) + if op is not None: + return '(' + op + render(node.operand) + ')' + if isinstance(node, ast.BinOp): + op = { + ast.Add: '+', + ast.Sub: '-', + ast.Mult: '*', + ast.Div: '/', + ast.Pow: '^', + ast.Mod: '%', + }.get(type(node.op)) + if op: + return '(' + render(node.left) + op + render(node.right) + ')' + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and not node.keywords: + if node.func.id == '__unit__': + return '(' + render(node.args[0]) + ' ' + node.args[1].value + ')' + name = 'if' if node.func.id == '__if__' else node.func.id + return name + '(' + ';'.join(render(a) for a in node.args) + ')' + if isinstance(node, ast.Compare) and len(node.ops) == 1: + op = { + ast.Lt: '<', + ast.LtE: '<=', + ast.Gt: '>', + ast.GtE: '>=', + ast.Eq: '==', + ast.NotEq: '<>', + }.get(type(node.ops[0])) + if op: + return '(' + render(node.left) + op + render(node.comparators[0]) + ')' + if isinstance(node, ast.BoolOp): + op = ' and ' if isinstance(node.op, ast.And) else ' or ' + return '(' + op.join(render(a) for a in node.values) + ')' + raise ExpressionError('Invalid expression syntax') + + def dimension(node): + if isinstance(node, ast.Constant): + return TEXT if isinstance(node.value, str) else SCALAR + if isinstance(node, ast.Name): + if node.id in parameters: + return parameters[node.id].dimension + if node.id in {'PI', 'E'}: + return SCALAR + if node.id == 'Gravity': + return (1, 0, -2) + if node.id == 'SpeedOfLight': + return (1, 0, -1) + return unit_definition(self.manager.formatUnits(node.id))[1] + if isinstance(node, ast.UnaryOp): + return dimension(node.operand) + if isinstance(node, ast.BinOp): + a, b = dimension(node.left), dimension(node.right) + if TEXT in (a, b): + return TEXT + if isinstance(node.op, (ast.Add, ast.Sub, ast.Mod)): + return b if a == SCALAR else a + if isinstance(node.op, ast.Pow): + exponent = self.numeric(render(node.right), SCALAR).value + return _dimension(d * exponent for d in a) + sign = -1 if isinstance(node.op, ast.Div) else 1 + return _dimension(x + sign * y for x, y in zip_longest(a, b, fillvalue=0)) + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): + name = node.func.id + if name == '__unit__': + return unit_definition(node.args[1].value)[1] + if name in { + 'sin', + 'cos', + 'tan', + 'sinh', + 'cosh', + 'tanh', + 'sign', + 'random', + 'exp', + 'ln', + 'log', + }: + return SCALAR + if name in {'asin', 'acos', 'atan', 'asinh', 'acosh', 'atanh'}: + return ANGLE + if name == '__if__' and len(node.args) == 3: + branches = [] + for branch in node.args[1:]: + try: + branches.append(dimension(branch)) + except (ExpressionError, RuntimeError): + # Fusion may never evaluate this branch. The complete + # native formula is still validated below before sampling. + continue + return next((d for d in branches if d != SCALAR), SCALAR) + if name == 'pow' and len(node.args) == 2: + exponent = self.numeric(render(node.args[1]), SCALAR).value + return _dimension(d * exponent for d in dimension(node.args[0])) + if name == 'sqrt' and len(node.args) == 1: + return _dimension(d / 2 for d in dimension(node.args[0])) + if node.args: + dims = [dimension(a) for a in node.args] + return next((d for d in dims if d != SCALAR), SCALAR) + return SCALAR + return SCALAR + + inferred = dimension(tree.body) + if inferred == TEXT: + # Text references/concatenation are pure. Native numeric conditions are + # resolved lazily so an unused invalid numeric branch does not veto text. + def text_value(node): + if isinstance(node, ast.Name) and node.id in parameters: + return parameters[node.id] + if ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Name) + and node.func.id == '__if__' + ): + test = 'if(' + render(node.args[0]) + ';1;0)' + branch = node.args[1] if self.numeric(test, SCALAR).value else node.args[2] + return text_value(branch) + if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): + return TextValue(text_value(node.left).value + text_value(node.right).value) + return offline_evaluate(render(node), {}) + + value = text_value(tree.body) + return TextValue(value.value, stochastic) + + # Preserve the original Fusion syntax and literal unit binding. Substitute + # only references, never function names, units, constants, or quoted text. + identifiers = re.compile(r'(? d.from === from); + const diagnostic = diagnostics.find((d) => d.from === from && d.severity !== 'info'); ranges.push( Decoration.mark({ class: `token-${kind}${diagnostic ? ' token-error' : ''}`, diff --git a/frontend/src/expression-syntax.test.ts b/frontend/src/expression-syntax.test.ts index 69b2549..2a119c9 100644 --- a/frontend/src/expression-syntax.test.ts +++ b/frontend/src/expression-syntax.test.ts @@ -30,3 +30,11 @@ it('preserves escaped quotes while scanning command delimiters', () => { "'a\\'this' + old", ); }); + +it('treats native inch and foot suffixes as units while preserving quoted text', () => { + expect(hasDefinitionSeparator('size=1";other=2')).toBe(true); + expect(hasDefinitionSeparator("size=1';other=2")).toBe(true); + expect(inString('size=1"')).toBe(false); + expect(inString("size=1'")).toBe(false); + expect(inString('caption="1')).toBe(true); +}); diff --git a/frontend/src/expression-syntax.ts b/frontend/src/expression-syntax.ts index 7aa86ea..d2db8d1 100644 --- a/frontend/src/expression-syntax.ts +++ b/frontend/src/expression-syntax.ts @@ -1,7 +1,9 @@ +import { expressionTokens, normalizeUnitSymbols } from '../../src/expression-language'; /** Separators and identifiers inside quoted values are content, not commands. */ export function mapExpressionCode(text: string, transform: (code: string) => string): string { - return text.replace(/'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|[^'"]+/gs, (part) => - part.startsWith("'") || part.startsWith('"') ? part : transform(part), + return normalizeUnitSymbols(text).replace( + /'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|[^'"]+/gs, + (part) => (part.startsWith("'") || part.startsWith('"') ? part : transform(part)), ); } export function hasAssignment(text: string): boolean { @@ -37,14 +39,6 @@ export function hasDefinitionSeparator(text: string): boolean { return found; } export function inString(text: string): boolean { - let quote = ''; - for (let i = 0; i < text.length; i++) { - if (quote && text[i] === '\\') { - i++; - continue; - } - if (quote === text[i]) quote = ''; - else if (!quote && (text[i] === "'" || text[i] === '"')) quote = text[i]; - } - return !!quote; + const last = expressionTokens(text).at(-1); + return !!last && last.kind === 'string' && !/^(['"])(?:\\.|(?!\1).)*\1$/s.test(last.value); } diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 35ab2f1..472fc12 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -683,11 +683,16 @@ export function App() {