예제 #1
0
 def define_const(self, key, value, lf):
     if self.contains_key(key):
         raise lib.NameException(
             "Name '{}' is already defined in this scope, in {}, at line {}"
             .format(key, lf[1], lf[0]))
     else:
         self.constants[key] = value
예제 #2
0
 def get_class(self, class_name):
     v = self._inner_get(class_name)
     if v is NULLPTR:
         raise lib.NameException("Class or module '{}' is not defined".format(class_name))
     elif type(v).__name__ == "Function":
         return v.outer_scope.outer.get_class(class_name)
     return v
예제 #3
0
 def define_var(self, key, value, lf):
     if self._local_contains(key):
         raise lib.NameException(
             "Name '{}' is already defined in this scope, in '{}', at line {}"
             .format(key, lf[1], lf[0]))
     else:
         self.variables[key] = value
예제 #4
0
 def assign_const(self, key, value, lf) -> bool:
     if key in self.constants:
         if self.constants[key] is UNDEFINED:
             self.constants[key] = value
             return True
         else:
             raise lib.NameException("Assignment to constant '{}' is not allowed, in '{}', at line {}"
                                     .format(key, lf[1], lf[0]))
     else:
         out = self.outer
         while out:
             if key in out.constants:
                 if out.constants[key] is UNDEFINED:
                     out.constants[key] = value
                     return True
                 else:
                     raise lib.NameException("Assignment to constant '{}' is not allowed, in '{}', at line {}"
                                             .format(key, lf[1], lf[0]))
             out = out.outer
     return False
예제 #5
0
 def assign(self, key, value, lf):
     if key in self.variables:
         self.variables[key] = value
     else:
         out = self.outer
         while out:
             if key in out.variables:
                 out.variables[key] = value
                 return
             out = out.outer
         if not self.assign_const(key, value, lf) and not self.assign_namespace(key, value):
             raise lib.NameException("Name '{}' is not defined, in '{}', at line {}"
                                     .format(key, lf[1], lf[0]))
예제 #6
0
    def get_heap(self, class_name: str):
        """
        Returns the heap-variable corresponding to the key 'class_name'.

        This method will return the instance if the value stored in heap is a pointer.

        :param class_name:
        :return: the heap-variable corresponding to the key 'class_name'. Instance will be returned if the
        value stored in heap is a pointer.
        """
        obj = self._inner_get_heap(class_name)
        if obj is NULLPTR:
            raise lib.NameException("Global name '{}' is not defined".format(class_name))
        return obj
예제 #7
0
    def get(self, key: str, line_file: tuple):
        """
        Returns the value of that key.

        If the value is a pointer, then returns the instance pointed by the pointer instead.

        :param key:
        :param line_file:
        :return: the value corresponding to the key. Instance will be returned if the value is a pointer.
        """
        v = self._inner_get(key)
        # print(key + str(v))
        if v is NULLPTR:
            raise lib.NameException("Name '{}' is not defined, in file '{}', at line {}"
                                    .format(key, line_file[1], line_file[0]))
        return v
예제 #8
0
 def add_heap(self, k, v):
     if k in self.heap:
         raise lib.NameException("Global name '{}' has already defined".format(k))
     else:
         self.heap[k] = v