Commits

Gigih Aji Ibrahim  committed 6f9dd7d

add HashScopeExpr

  • Participants
  • Parent commits cdf03b0

Comments (0)

Files changed (3)

File src/ast3/expr.py

         super(CaseExpr, self).__init__(loc)
         self.setAttr(condExpr=condExpr, thenBlock=thenBlock)
         self.setOrder('condExpr', 'thenBlock')
+
+class HashScopeExpr(Expr):
+    def __init__(self, loc, expr):
+        super(Expr, self).__init__(loc)
+        self.setAttr(expr=expr)
+        self.setOrder('expr')

File src/parser4.py

         ## unary expr
         self.prefix(['not', '+', '-'], self.pUnaryExpr)
 
+        ## sigil expr
+        self.prefix('#', self.pHashScopeExpr)
+
         ## binary expr
         self.infixRight('or',                   30, self.pBinaryExpr)
         self.infixRight('and',                  40, self.pBinaryExpr)
         self.expect('#DEDENT')
         return MatchExpr(self.getLoc(token), left, cases, elseBlock)
 
+
+    def pHashScopeExpr(self, token):
+        expr = self.pExpr()
+        if not isinstance(expr, NameExpr):
+            raise ParseError(expr.loc, 'Expected NameExpr for HashScope instead of {}'.format(expr))
+
+        return HashScopeExpr(self.getLoc(token), expr)
+
+        
     def pType(self):
         nextToken    = self.peekNext()
         if nextToken.type == '#ID': return self.pNameType()

File src/step/typechecker.py

         node.symbol = self.search(node.name)
         if node.symbol is None:
             curSymbol  = self.getSymbol()
-            if isinstance(curSymbol, SymbolScope.Block):
+            if isA(curSymbol, SymbolScope.Block, Def):
                 curSymbol  = self.searchByType(Def)
                 symbolType = 'function'
             else:
         
         ## type is first exprStmt
         node.type = node.thenBlock[0].expr.type
-        
+
+
+    def vHashScopeExpr(self, node):
+        ## must be inside call expr arguments
+        if not isinstance(node.parent, CallExpr):
+            raise SemanticError(node.loc, "HashScopeExpr only valid inside call expression argument.")
+
+        ## inject scope to NameExpr child
+        index = node.parent.args.index(node)
+        param = list(node.parent.symbol.paramByName.values())[index]
+
+        left  = NameExpr(node.loc, param.type.symbol.name)
+
+        return self.rewrite(node, NameExpr, left=left, name=node.expr.name)