Commits

Victor Kotseruba committed 79d37a0

@if, @else, @else if should work, but them are very ugly

Comments (0)

Files changed (1)

         return functools.partial(getattr(ast, name),
                                  col_offset=0,
                                  lineno=self.owner.lineno)
+
+
+class AstElse(object):
     
+    def __init__(self, if_node):
+        self.if_node = if_node
+    
+    @property
+    def body(self):
+        return self.if_node.orelse
 
 
 
     def reset(self):
         self.level = 0
         self.parents = []
+        self.if_nodes = []
         self.__id = 0
         self.lineno = 0
         self.ctx = self.ast.Module(body=[
         elif line.startswith('@while '):
             self.enter_ctx(self.handle_while(line[1:]))
         
+        elif line.startswith('@if '):
+            if_node = self.handle_if(line[4:])
+            self.if_nodes += [None] * (self.level - len(self.if_nodes) + 1)
+            self.if_nodes[self.level] = if_node
+            self.enter_ctx(if_node)
+        
+        elif line == '@else':
+            self.enter_ctx(self.handle_else())
+        
+        elif line.startswith('@else if '):
+            self.enter_ctx(self.handle_elif(line[9:]))
+        
         elif line.startswith('.') or line.startswith('#') or\
                             level < self.get_level(next):
             self.enter_ctx(self.handle_rule(line))
         return loop
     
     
+    def handle_if(self, data):
+        test = ast.parse(_convert_s(data)).body[0].value
+        condition = self.ast.If(test=test, body=[], orelse=[])
+        self._push(condition)
+        return condition
+    
+    
+    def handle_else(self):
+        return AstElse(self.if_nodes[self.level])
+    
+    
+    def handle_elif(self, data):
+        test = ast.parse(_convert_s(data)).body[0].value
+        condition = self.ast.If(test=test, body=[], orelse=[])
+        self.if_nodes[self.level].orelse.append(condition)
+        return condition
+    
+    
     def handle_prop(self, data):
         name, value, expr = self.parse_prop(data)
         if expr:
             func=self.ast.Name(id=name, ctx=self.ast.Load()),
             args=[parent], keywords=[], starargs=None, kwargs=None
         )))
-        
         return func_def