def add_condition(self, compiler, condition): assert self.var is not None event = self.var # TODO type checking etc path = i.VirtualString('.'.join(condition.path)) assert type(condition.value.value) == str, condition.value value = i.VirtualString(condition.value.value) compiler.top.preamble.add(i.AddEventCondition(event, path, value))
def __copy(self, this, other): if isinstance(other, LiteralString): this.value._str = i.VirtualString(other.value) else: assert other.type is self, other assert other.value._str is not None this.value._str = other.value._str
def allocate(self, compiler, namehint): var = compiler.create_var(namehint, self.ir_type) path = i.VirtualString('.maybe') subvar = i.NBTSubPath(var, path, self._wrap_type()) valvar = compiler.define(namehint + '_valvar', subvar) # There is support for valvar being elided e.g. when we are # an NBT property of a struct, use that property as valvar. # But we don't test for this yet return MaybeTypeInstance(var, valvar, self.get_func_members(), self.get_func_properties())
def _as_string(self, container): if isinstance(container, LiteralString): return i.VirtualString(container.value) if isinstance(container.type, IRType): # Allow strings wrapped in _IRType v = container.type.as_ir_variable(container.value) if isinstance(v, (i.VirtualString, i.MutableString)): return v assert False, container else: assert container.type is self, container assert isinstance(container.value, StringInstance), container assert container.value._str is not None, container return container.value._str
def _create_func(self): func = self.compiler.define_function('_internal/entity_ptr') func.preamble.add(i.PureInsn()) retvar = func.preamble.define(i.ReturnVarInsn(i.VarType.i32)) entry = func.create_block('entry') set_uid = func.create_block('set_uid') end = func.create_block('end') self.uid_obj = self.compiler.global_def( '__uid', i.DefineObjective(i.VirtualString('__uid'), None)) sender = entry.define(i.CreateSelector(i.SelectorType.SENDER)) self_uid = entry.define(i.CreateEntityLocalAccess( self.uid_obj, sender)) next_uid = entry.define( i.CreateEntityLocalAccess( self.uid_obj, self.compiler.top.lookup('_global_entity'))) entry.add(i.RangeBr(self_uid, 0, 0, set_uid, end)) set_uid.add(i.SetScore(self_uid, next_uid)) set_uid.add(i.AddScore(next_uid, 1)) set_uid.add(i.Branch(end)) end.add(i.SetScore(retvar, self_uid)) end.add(i.Return()) func.end() return func
def create_sub_var(subname, var_type): path = i.VirtualString('.' + subname) insn = i.NBTSubPath(this, path, var_type) return compiler.define(namehint + '_' + subname, insn)
def objective(): return i.DefineObjective(i.VirtualString('__uid'), None)
def apply_to_selector(self, selector, compiler): compiler.add_insn( i.SetSelector(selector, self.sel_key, i.VirtualString(self.sel_val)))
def __init__(self, compiler, epos, idx): self.compiler = compiler assert not epos.has_offset(), "TODO" self.epos = epos self.path = i.VirtualString('Pos[%d]' % idx) self.idx = idx
def allocate(self, compiler, namehint): # TODO: May want to allow changing the objective name objdef = i.DefineObjective(i.VirtualString(namehint), None) objective = compiler.global_def(namehint, objdef) return EntityLocalInstance(self._l_type, objective)
def __init__(self, compiler, ptr, idx): self.compiler = compiler self.ptr = ptr self.path = i.VirtualString('Pos[%d]' % idx) self.idx = idx
def ctor(self, compiler, name): self._name = name event = self.create(i.VirtualString(name)) vname = name.split(':')[-1] self.var = compiler.global_def(vname, event)
def _gen_setter(block, indexvar, indexval, arr, value): path = i.VirtualString('[%d]' % indexval) path_var = block._func.preamble.define( i.NBTSubPath(arr, path, value.type)) block.add(i.SetScore(path_var, value))
def as_ir_variable(self, instance): # e.g. LiteralString if type(instance) == str: return i.VirtualString(instance) assert instance._str is not None, "String has no value" return instance._str
def __create_wrap_holders(self, compiler, realvar, types): for n, t in enumerate(types): path = i.VirtualString('.v%d' % n) subpath = i.NBTSubPath(realvar, path, t) holder = compiler.define('maybehold_%d' % n, subpath) yield holder