76 lines
2.2 KiB
Python
Raw Normal View History

2012-01-28 14:52:09 +00:00
"""
"""
2012-02-19 20:38:19 +00:00
from .node import Node
2012-02-25 17:08:08 +00:00
from lesscpy.lessc import utility
2012-02-25 17:34:23 +00:00
from lesscpy.lessc import color
2012-02-25 17:08:08 +00:00
2012-02-19 20:38:19 +00:00
class Expression(Node):
2012-02-25 17:08:08 +00:00
def parse(self, scope):
""" Parse Node
@param list: current scope
"""
assert(len(self.tokens) == 3)
expr = [t.parse(scope) if hasattr(t, 'parse')
else t
for t in self.tokens]
expr = [self.neg(t, scope) for t in expr]
A, O, B = [e[0]
if type(e) is tuple
else e
for e in expr]
2012-02-25 17:34:23 +00:00
try:
a, ua = utility.analyze_number(A, 'Illegal element in expression')
b, ub = utility.analyze_number(B, 'Illegal element in expression')
except SyntaxError:
return ' '.join([str(A), str(O), str(B)])
2012-02-25 17:08:08 +00:00
if(a is False or b is False):
2012-02-25 17:34:23 +00:00
return ' '.join([str(A), str(O), str(B)])
2012-02-25 17:08:08 +00:00
if ua == 'color' or ub == 'color':
2012-02-25 17:34:23 +00:00
return color.LessColor().process((A, O, B))
2012-02-25 17:08:08 +00:00
out = self.operate(a, b, O)
if type(a) is int and type(b) is int:
out = int(out)
return self.with_units(out, ua, ub)
def neg(self, t, scope):
"""
"""
if t and type(t) is list and t[0] == '-':
v = t[1]
if len(t) > 1 and hasattr(t[1], 'parse'):
v = t[1].parse(scope)
if type(v) is str:
return '-' + v
return -v
return t
def with_units(self, v, ua, ub):
"""
"""
2012-02-25 17:34:23 +00:00
if not v: return v
if ua or ub:
if ua and ub:
if ua == ub:
return str(v) + ua
else:
raise SyntaxError("Error in expression %s != %s" % (ua, ub))
elif ua:
2012-02-25 17:08:08 +00:00
return str(v) + ua
2012-02-25 17:34:23 +00:00
elif ub:
return str(v) + ub
2012-02-25 17:08:08 +00:00
return v
def operate(self, a, b, o):
"""
"""
operation = {
'+': '__add__',
'-': '__sub__',
'*': '__mul__',
'/': '__truediv__'
}.get(o[0])
v = getattr(a, operation)(b)
if v is NotImplemented:
v = getattr(b, operation)(a)
return v