diff options
author | jaseg <git@jaseg.de> | 2021-12-28 21:40:22 +0100 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2021-12-28 21:40:22 +0100 |
commit | 63e1eae8d81cb7940d3547511488f8ec4acd4d1c (patch) | |
tree | c0d2ddf93d8637d0df600a320cbf9d1387860163 /gerbonara/gerber/aperture_macros/expression.py | |
parent | 25dd65fac05a43ef75fe75049d5b79a73a207fc0 (diff) | |
download | gerbonara-63e1eae8d81cb7940d3547511488f8ec4acd4d1c.tar.gz gerbonara-63e1eae8d81cb7940d3547511488f8ec4acd4d1c.tar.bz2 gerbonara-63e1eae8d81cb7940d3547511488f8ec4acd4d1c.zip |
WIP
Diffstat (limited to 'gerbonara/gerber/aperture_macros/expression.py')
-rw-r--r-- | gerbonara/gerber/aperture_macros/expression.py | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/gerbonara/gerber/aperture_macros/expression.py b/gerbonara/gerber/aperture_macros/expression.py index 74fbd90..ddd8d53 100644 --- a/gerbonara/gerber/aperture_macros/expression.py +++ b/gerbonara/gerber/aperture_macros/expression.py @@ -8,6 +8,10 @@ import re import ast +def expr(obj): + return obj if isinstance(obj, Expression) else ConstantExpression(obj) + + class Expression(object): @property def value(self): @@ -28,6 +32,35 @@ class Expression(object): raise IndexError(f'Cannot fully resolve expression due to unresolved variables: {expr} with variables {variable_binding}') return expr.value + def __add__(self, other): + return OperatorExpression(operator.add, self, expr(other)).optimized() + + def __radd__(self, other): + return expr(other) + self + + def __sub__(self, other): + return OperatorExpression(operator.sub, self, expr(other)).optimized() + + def __rsub__(self, other): + return expr(other) - self + + def __mul__(self, other): + return OperatorExpression(operator.mul, self, expr(other)).optimized() + + def __rmul__(self, other): + return expr(other) * self + + def __truediv__(self, other): + return OperatorExpression(operator.truediv, self, expr(other)).optimized() + + def __rtruediv__(self, other): + return expr(other) / self + + def __neg__(self): + return 0 - self + + def __pos__(self): + return self class UnitExpression(Expression): def __init__(self, expr, unit): @@ -50,10 +83,10 @@ class UnitExpression(Expression): return self._expr elif unit == 'mm': - return OperatorExpression.mul(self._expr, MILLIMETERS_PER_INCH) + return self._expr * MILLIMETERS_PER_INCH elif unit == 'inch': - return OperatorExpression.div(self._expr, MILLIMETERS_PER_INCH) + return self._expr / MILLIMETERS_PER_INCH) else: raise ValueError('invalid unit, must be "inch" or "mm".') |