def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: iHdlExpr """ if isinstance(o, HdlValueId): d = { "__class__": o.__class__.__name__, "val": str(o), } elif is_str(o) or o is None: d = o elif isinstance(o, HdlValueInt): d = self.visit_HdlValueInt(o) elif isinstance(o, HdlOp): d = self.visit_HdlOp(o) elif o is HdlAll or\ o is HdlTypeAuto or\ o is HdlOthers or\ o is HdlTypeType: d = { "__class__": o.__class__.__name__, } elif isinstance(o, (list, tuple)): return [self.visit_iHdlExpr(o2) for o2 in o] else: raise NotImplementedError("Unexpected object of type " + str(type(o))) return d
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) elif is_str(o): w('"%s"' % o) elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) elif isinstance(o, (list, tuple)): with_nl = len(o) > 3 if isinstance(o, list): begin = "{" end = "}" else: begin = "(" end = ")" w(begin) for elem in o: self.visit_iHdlExpr(elem) if with_nl: w(", \n") else: w(", ") w(end) elif isinstance(o, HdlOp): self.visit_HdlOp(o) elif o is None: w("nullptr") else: raise NotImplementedError(o.__class__, o) return True
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: True, the flag used to mark that the ; should be added if this is a statement """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) elif is_str(o): w('"%s"' % o) elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) elif isinstance(o, HdlOp): self.visit_HdlOp(o) elif o is HdlAll: w("*") elif o is HdlTypeAuto: pass elif o is None: w("null") elif isinstance(o, float): w(str(o)) else: raise NotImplementedError(o.__class__, o) return True
def _parse_hdlConvertor_json(j): # handle primitive types if j is None: return j elif isinstance(j, (int, float)) or is_str(j): return j elif isinstance(j, list): return [_parse_hdlConvertor_json(_j) for _j in j] # load a hdlAst object cls = j["__class__"] cls = KNOWN_NODES[cls] consumed = {"__class__", } argc = cls.__init__.__code__.co_argcount if argc == 1: o = cls() else: # load argumets for __init__ argv = [] # 1st is self arg_names = cls.__init__.__code__.co_varnames[1:argc] for a in arg_names: v = j.get(a, None) if a == "fn": v = getattr(HdlOpType, v) else: v = _parse_hdlConvertor_json(v) argv.append(v) consumed.add(a) o = cls(*argv) if len(consumed) != len(j): # load rest of the properties which were not in __init__ params for k, v in j.items(): if k in consumed: continue # there are few cases where object class is not specified specified # explicitely we have to handle them first if k == "position": _v = CodePosition() ( _v.start_line, _v.start_column, _v.stop_line, _v.stop_column ) = v elif k == "direction": _v = getattr(HdlDirection, v) elif k == "join_t": _v = getattr(HdlStmBlockJoinType, v) else: _v = _parse_hdlConvertor_json(v) setattr(o, k, _v) return o
def get_object_and_scope_by_name(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() actual = self while actual is not None: o = actual.get(name, _INVALID) if o is not _INVALID: return (actual, o) else: actual = actual.parent raise KeyError(name)
def level_push(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() i = self.children.get(name, None) if i is not None: # there is already a child with such a name return i assert name in self, ( name, "name not registered for any object in this scope") i = self.__class__(self, name, self.ignorecase) self.children[name] = i return i
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) return elif is_str(o): w('"%s"' % o) return elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) return elif isinstance(o, (list, tuple)): with_nl = len(o) > 3 w("(") for elem in o: self.visit_iHdlExpr(elem) if with_nl: w(", \n") else: w(", ") w(")") return elif isinstance(o, HdlOp): self.visit_HdlOp(o) return elif o is None: w("None") return elif isinstance(o, dict): w("{") with Indent(self.out): for last, (k, v) in iter_with_last( sorted(o.items(), key=lambda x: x[0])): self.visit_iHdlExpr(k) w(": ") self.visit_iHdlExpr(v) if not last: w(",\n") else: w("\n") w("}") return raise NotImplementedError(o.__class__, o)
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: iHdlExpr """ if isinstance(o, HdlValueId): o = add_io_prefix(o) if not self._stm_dst: return hdl_getattr(o, "val") return o elif o is None or isinstance(o, HdlValueInt) or is_str(o): return o elif isinstance(o, list): return [self.visit_iHdlExpr(_o) for _o in o] elif isinstance(o, HdlOp): return self.visit_HdlOp(o) else: raise NotImplementedError("Do not know how to convert %s" % (o))
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) return elif is_str(o): w('"%s"' % o) return elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) return elif isinstance(o, HdlOp): self.visit_HdlOp(o) return else: raise NotImplementedError( "Do not know how to convert %r" % (o))
def checked_name(self, suggested_name, obj): """ Get a non occupied name in current scope if name is occupied or name ends with _ the new name is generated. :return: str """ assert is_str(suggested_name), suggested_name if not suggested_name.endswith("_"): try: self.register_name(suggested_name, obj) return suggested_name except NameOccupiedErr: suggested_name += "_" actual = self while actual is not None: try: cntrVal = actual.cntrsForPrefixNames[suggested_name] break except KeyError: actual = actual.parent if actual is not None: # some parrent of self already have such a prefix counter usableName = actual.__incrPrefixCntr( suggested_name, cntrVal) else: # parrents and self does not have such a prefix counter # delete potentially existing prefix conterrs from children # and add prefix counter to self cntrVal = self.__discard_prefix_cntrs_from_children(suggested_name) usableName = self.__incrPrefixCntr( suggested_name, cntrVal) # setup for me and propagate to children self.register_name(usableName, obj) return usableName
def get_child(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() return self.children[name]
def __init__(self, val, obj=None): assert is_str(val), val self.val = val self.obj = obj