Commits

Victor Kotseruba  committed ac664a4

math works

  • Participants
  • Parent commits f7b7578

Comments (0)

Files changed (2)

 
 class Expression(object):
 
+    number_re = re.compile(r'(\d+(?:\.\d+)?(?:em|px|%|pt))')
+    variable_re = re.compile(r'(?P<name>!\w+)')
+    
     def __init__(self, expr):
         self.expr = expr        
+        
+    def evaluate(self, context):
+        expr = self.expr
+        def replace_variable(name):
+            name = name.groupdict()['name']
+            name = name.strip('!')
+            if name in context:
+                return str(context[name])
+            raise Exception('variable "%s" not found' % name)
+        expr = self.variable_re.sub(replace_variable, expr)
+        expr = self.number_re.sub(r"Number('\1')", expr)
+        return str(eval(expr))
     
-    def evaluate(self, context):
-        data = {}
-        for d in reversed(context.dicts):
-            data.update(d)
-        return 'eval(%s, %r)' % (self.expr, data)
-
     def __repr__(self):
-        return 'e[%s]' % self.expr
+        return self.expr
 
 
 class Number(object):
     
-    number_re = re.compile(r'^(?P<value>\d+(?:\.\d+)?)(?P<points>\w+)$')
+    number_re = re.compile(r'^(?P<value>\d+(?:\.\d+)?)(?P<points>em|px|%|pt)$')
     
     def __init__(self, expr):
         data = self.number_re.match(expr).groupdict()
                         value = color.full_color
                     elif self.colors == 'rgb':
                         value = color.rgb_color
-                except Color.UnknownType:
+                except Color.UnknownType, TypeError:
                     pass
             properties.append(self.property_template % (prop.key, value))
         if properties:
 
-=font-size(!size)
-  font-size = !size
+=box-font(!size)
+  font-size = !size / 5
 
+=square(!width)
+  width = !width
+  height = !width
+  padding = !width / 10
+  margin = !width / 20
+  +box-font(!width)
 
-=heading-font(!size)
-  +font-size(10px)
-
-h1
-  color: white
-  +heading-font(20px)
+.box
+  +square(100px)