Advertisement
minh_tran_782

PROGRAMMING CODE: NAME IN OOP

Mar 21st, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. class StaticCheck(Visitor):
  2.  
  3.     def visitProgram(self,ctx:Program,o:object):
  4.         for decl in ctx.decl:
  5.             self.visit(decl,o)
  6.  
  7.     def visitClassDecl(self,ctx:ClassDecl,o:object):
  8.         o=[]
  9.         for mem in ctx.mem:
  10.             self.visit(mem,o)
  11.  
  12.     def visitVarDecl(self,ctx:VarDecl,o:object):
  13.         if ctx.name in o:
  14.             raise RedeclaredField(ctx.name)
  15.         o+=[ctx.name]
  16.  
  17.     def visitFuncDecl(self,ctx:FuncDecl,o:object):
  18.         if ctx.name in o:
  19.             raise RedeclaredMethod(ctx.name)
  20.         o+=[ctx.name]
  21.     def visitIntType(self,ctx:IntType,o:object):pass
  22.  
  23.     def visitFloatType(self,ctx:FloatType,o:object):pass
  24.  
  25.     def visitClassType(self,ctx:ClassType,o:object):pass
  26.  
  27.     def visitIntLit(self,ctx:IntLit,o:object):pass
  28.  
  29.     def visitId(self,ctx:Id,o:object):pass
  30.  
  31.     def visitFieldAccess(self,ctx:FieldAccess,o:object):pass
  32. ##################################
  33. class GetEnv(Visitor):
  34.     def visitProgram(self,ctx:Program,o:object):
  35.         o = []
  36.         for decl in ctx.decl:
  37.             self.visit(decl, o)
  38.         return o
  39.        
  40.     def visitClassDecl(self,ctx:ClassDecl,o:object):
  41.         env = []
  42.         for mem in ctx.mem:
  43.             self.visit(mem, env)
  44.         o += [(ctx.name, ctx.parent, env)]
  45.        
  46.  
  47.     def visitVarDecl(self,ctx:VarDecl,o:object):
  48.         if ctx.name in o:
  49.             raise RedeclaredField(ctx.name)
  50.         o += [(ctx.name, ctx.typ)]
  51.        
  52.     def visitFuncDecl(self,ctx:FuncDecl,o:object):
  53.         if ctx.name in o:
  54.             raise RedeclaredMethod(ctx.name)
  55.         o += [(ctx.name, None)]
  56.  
  57. class StaticCheck(Visitor):
  58.     def visitProgram(self,ctx:Program,o:object):
  59.         env = GetEnv().visit(ctx, o)
  60.         # print(env)
  61.         for decl in ctx.decl:
  62.             self.visit(decl, env)
  63.  
  64.     def visitClassDecl(self,ctx:ClassDecl,o:object):
  65.         for mem in ctx.mem:
  66.             if type(mem) is FuncDecl:
  67.                 self.visit(mem, o)
  68.  
  69.     def visitFuncDecl(self,ctx:FuncDecl,o:object):
  70.         local = ctx.param + ctx.body[0]
  71.         listOfClass = o
  72.        
  73.         for expr in ctx.body[1]:
  74.             self.visit(expr, (listOfClass, local))
  75.  
  76.     def visitId(self,ctx:Id,o:object):
  77.         typ = None
  78.         for x in o[1]:
  79.             if x.name == ctx.name:
  80.                 typ = x.typ
  81.         if not typ:
  82.             raise UndeclaredIdentifier(ctx.name)
  83.            
  84.         return typ
  85.  
  86.     def visitFieldAccess(self,ctx:FieldAccess,o:object):
  87.         typ = self.visit(ctx.exp, o)
  88.         if type(typ) is ClassType:
  89.             c = None
  90.             for x in o[0]:
  91.                 if typ.name == x[0]:
  92.                     c = x
  93.                    
  94.             if not c: raise UndeclaredClass(typ.name)
  95.            
  96.             for mem in c[2]:
  97.                 if mem[0] == ctx.field:
  98.                     return
  99.             raise UndeclaredField(ctx.field)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement