Skip to content

Commit 1a71842

Browse files
committed
Refactor instructions into separate files
1 parent 1e665d8 commit 1a71842

File tree

19 files changed

+2594
-2357
lines changed

19 files changed

+2594
-2357
lines changed

cmd_ir/core.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
from .core_types import FunctionLike, PosUtilEntity
77

8+
from .variables import (Variable, VarType, ParameterVariable, ReturnVariable,
9+
LocalStackVariable, LocalVariable, NbtOffsetVariable)
10+
811
class FuncWriter(metaclass=abc.ABCMeta):
912

1013
@abc.abstractmethod
@@ -229,7 +232,6 @@ def transform_scope(self, func):
229232
return changed
230233

231234
def reset_var_usage(self):
232-
from .variables import Variable
233235
for var in self.scope.values():
234236
if isinstance(var, Variable):
235237
var.reset_usage()
@@ -250,7 +252,6 @@ def _create_func(self, name):
250252

251253
def create_global(self, namehint, vartype):
252254
def create(name):
253-
from .instructions import DefineGlobal
254255
return self.preamble.add(DefineGlobal(vartype))
255256
return self.uniq(namehint, create)
256257

@@ -315,7 +316,6 @@ def writeout(self, writer, temp_gen):
315316

316317
def validate_args(self, args, retvars):
317318
assert self.finished
318-
from .variables import Variable, VarType
319319
if self.params:
320320
assert args is not None
321321
assert len(args) == len(self.params)
@@ -345,11 +345,10 @@ def extern_visibility(self):
345345

346346
class ExternFunction(VisibleFunction):
347347

348-
def __init__(self, global_name):
348+
def __init__(self, global_name, params=None, returns=None):
349349
self._gname = global_name
350-
# TODO
351-
self.params = []
352-
self.returns = []
350+
self.params = params or []
351+
self.returns = returns or []
353352

354353
@property
355354
def finished(self):
@@ -373,7 +372,13 @@ def is_empty(self):
373372
return False
374373

375374
def serialize(self):
376-
return 'extern function %s\n' % self._gname
375+
def serialize(typelist):
376+
if not typelist:
377+
return 'NULL'
378+
return '(%s)' % ', '.join(t.name for t in typelist)
379+
return 'extern function %s %s %s\n' % (self._gname,
380+
serialize(self.params),
381+
serialize(self.returns))
377382

378383
class IRFunction(VisibleFunction, VariableHolder):
379384

@@ -466,7 +471,6 @@ def create_block(self, namehint):
466471

467472
def create_var(self, namehint, vartype):
468473
def create(name):
469-
from .instructions import DefineVariable
470474
return self.preamble.add(DefineVariable(vartype))
471475
return self.uniq(namehint, create)
472476

@@ -490,7 +494,6 @@ def writeout(self, writer, temp_gen):
490494

491495
def get_registers(self):
492496
assert self._varsfinalized
493-
from .variables import Variable
494497
return [var for var in self.scope.values() \
495498
if isinstance(var, Variable) and var._direct_ref() \
496499
and not var.is_entity_local]
@@ -499,8 +502,6 @@ def is_closed(self):
499502
return all(b.is_terminated() for b in self.blocks)
500503

501504
def configure_parameters(self, hasownstackframe):
502-
from .variables import ParameterVariable, ReturnVariable, \
503-
LocalStackVariable
504505
# Linked to InvokeInsn
505506
offset = 0
506507
rets = []
@@ -534,8 +535,6 @@ def variables_finalized(self):
534535
self._varsfinalized = True
535536

536537
def add_entry_exit(self):
537-
from .variables import LocalVariable, NbtOffsetVariable
538-
from .instructions import PushNewStackFrame, PopStack, Branch
539538
# sorted from head to tail of stack
540539
vars = sorted([var.var for var in self.scope.values() \
541540
if isinstance(var, LocalVariable) \
@@ -555,7 +554,6 @@ def add_entry_exit(self):
555554
return vars
556555

557556
def add_advancement_revoke(self, event):
558-
from .instructions import RevokeEventAdvancement
559557
self._entryblock.add(RevokeEventAdvancement(self))
560558

561559
def serialize(self):
@@ -614,7 +612,6 @@ def add(self, insn):
614612
if not self.force and self.insns and self.insns[-1].terminator():
615613
assert False, "Block %s is terminated by %s. Tried adding %s" % (
616614
self, self.insns[-1], insn)
617-
from .instructions import Return, Branch
618615
if isinstance(insn, Return):
619616
return super().add(Branch(self._func._exitblock))
620617
return super().add(insn)
@@ -666,3 +663,9 @@ def is_empty(self):
666663
def reset(self):
667664
if self._clear:
668665
super().reset()
666+
667+
# Load these after everything has been defined - otherwise it is a recursive
668+
# dependency
669+
from .instructions import (DefineGlobal, DefineVariable, PushNewStackFrame,
670+
PopStack, Branch, RevokeEventAdvancement, Return,
671+
Branch)

cmd_ir/core_types.py

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22

3-
from commands import *
3+
import commands as c
44

55
class InsnArg:
66

@@ -39,7 +39,7 @@ class EntityLocal(NativeType):
3939

4040
def __init__(self, name):
4141
self.name = name
42-
self.obj_ref = ObjectiveRef(name)
42+
self.obj_ref = c.ObjectiveRef(name)
4343

4444
class VirtualString(NativeType):
4545

@@ -65,7 +65,7 @@ def __init__(self, name):
6565
self.player = name
6666

6767
def as_resolve(self):
68-
return NameRef(self.player)
68+
return c.NameRef(self.player)
6969

7070
class SelectorType(InsnArg):
7171

@@ -96,8 +96,7 @@ def _init_from_parser(cls, name):
9696
SelectorType.NEAREST_PLAYER = SelectorType('p', 1)
9797
SelectorType.RANDOM_PLAYER = SelectorType('r', 1)
9898

99-
# Name conflict with commands.Selector
100-
class SelectorTy(EntitySelection):
99+
class Selector(EntitySelection):
101100

102101
def __new__(cls, type):
103102
if type.max_limit == 1:
@@ -114,24 +113,24 @@ def set(self, key, value):
114113
self.simple_args[key] = value
115114

116115
def set_score_range(self, objective, min, max):
117-
self.other_args.append(SelRange(objective, min, max))
116+
self.other_args.append(c.SelRange(objective, min, max))
118117

119118
def set_nbt(self, path, value):
120119
parts = path.split('.') if path else []
121-
self.other_args.append(SelNbt(parts, value))
120+
self.other_args.append(c.SelNbt(parts, value))
122121

123122
@property
124123
def is_only_one(self):
125124
return 'limit' in self.simple_args and self.simple_args['limit'] == '1'
126125

127126
def as_resolve(self):
128-
args = SimpleSelectorArgs(self.simple_args)
127+
args = c.SimpleSelectorArgs(self.simple_args)
129128
for other in self.other_args:
130-
args = ComboSelectorArgs.new(args, other)
131-
return Selector(self.type.letter, args)
129+
args = c.ComboSelectorArgs.new(args, other)
130+
return c.Selector(self.type.letter, args)
132131

133132
# Same as Selector but is also an EntityRef so can be used in more places
134-
class SingleEntitySelector(SelectorTy, EntityRef):
133+
class SingleEntitySelector(Selector, EntityRef):
135134

136135
def __new__(cls, type):
137136
return object.__new__(cls)
@@ -143,7 +142,7 @@ def is_only_one(self):
143142
class PosUtilEntity(EntityRef):
144143

145144
def as_resolve(self):
146-
return PosUtil
145+
return c.PosUtil
147146

148147
class Position(NativeType):
149148

@@ -158,30 +157,30 @@ def internal(val):
158157
self._x, self._y, self._z = map(internal, (x, y, z))
159158

160159
def as_blockpos(self):
161-
return WorldPos(self._x, self._y, self._z, block_pos=True)
160+
return c.WorldPos(self._x, self._y, self._z, block_pos=True)
162161

163162
def as_worldpos(self):
164-
return WorldPos(self._x, self._y, self._z)
163+
return c.WorldPos(self._x, self._y, self._z)
165164

166165
class RelPosVal(NativeType):
167166

168167
def __init__(self, val):
169168
self.val = val
170-
self.as_coord = WorldRelCoord(val)
169+
self.as_coord = c.WorldRelCoord(val)
171170

172171
class AncPosVal(NativeType):
173172

174173
def __init__(self, val):
175174
self.val = val
176-
self.as_coord = AnchorRelCoord(val)
175+
self.as_coord = c.AnchorRelCoord(val)
177176

178177
class CmdFunction(NativeType, metaclass=abc.ABCMeta):
179178

180179
@abc.abstractmethod
181180
def as_cmd(self):
182181
pass
183182

184-
class BlockType(Resolvable, NativeType):
183+
class BlockType(c.Resolvable, NativeType):
185184

186185
def __init__(self, block_id):
187186
self.block_id = block_id
@@ -200,7 +199,7 @@ def resolve(self, scope):
200199
nbt = self.nbt.resolve(scope) if self.nbt is not None else ''
201200
return '%s%s%s' % (self.block_id, '[%s]' % props if props else '', nbt)
202201

203-
class ItemType(Resolvable, NativeType):
202+
class ItemType(c.Resolvable, NativeType):
204203

205204
def __init__(self, item_id):
206205
self.item_id = item_id
@@ -216,3 +215,79 @@ def resolve(self, scope):
216215
nbt = NBTCompound(self.nbt_props.items())
217216
props = nbt.resolve(scope)
218217
return '%s%s' % (self.item_id, props)
218+
219+
class TeamRef(NativeType):
220+
221+
def __init__(self, name):
222+
self.name = name
223+
self.ref = c.TeamName(name)
224+
225+
class TextColor(InsnArg):
226+
227+
__lookup = {}
228+
229+
def __init__(self, name):
230+
self.name = name
231+
self.__lookup[name] = self
232+
233+
@classmethod
234+
def _init_from_parser(cls, value):
235+
return cls.__lookup[value]
236+
237+
black = None
238+
dark_blue = None
239+
dark_green = None
240+
dark_aqua = None
241+
dark_red = None
242+
dark_purple = None
243+
gold = None
244+
gray = None
245+
dark_gray = None
246+
blue = None
247+
green = None
248+
aqua = None
249+
red = None
250+
light_purple = None
251+
yellow = None
252+
white = None
253+
reset = None
254+
255+
TextColor.black = TextColor('black')
256+
TextColor.dark_blue = TextColor('dark_blue')
257+
TextColor.dark_green = TextColor('dark_green')
258+
TextColor.dark_aqua = TextColor('dark_aqua')
259+
TextColor.dark_red = TextColor('dark_red')
260+
TextColor.dark_purple = TextColor('dark_purple')
261+
TextColor.gold = TextColor('gold')
262+
TextColor.gray = TextColor('gray')
263+
TextColor.dark_gray = TextColor('dark_gray')
264+
TextColor.blue = TextColor('blue')
265+
TextColor.green = TextColor('green')
266+
TextColor.aqua = TextColor('aqua')
267+
TextColor.red = TextColor('red')
268+
TextColor.light_purple = TextColor('light_purple')
269+
TextColor.yellow = TextColor('yellow')
270+
TextColor.white = TextColor('white')
271+
TextColor.reset = TextColor('reset')
272+
273+
class BossbarRef(NativeType):
274+
275+
def __init__(self, name):
276+
self.name = name
277+
self.ref = c.Bossbar(name)
278+
279+
PosType = (int, float, RelPosVal, AncPosVal)
280+
281+
def Opt(optype):
282+
return (type(None), optype)
283+
284+
class EventRef(NativeType):
285+
286+
def __init__(self, name):
287+
self.name = name
288+
self.conditions = {}
289+
290+
def add_condition(self, path, value):
291+
self.conditions[path] = value
292+
293+
RelPosType = (int, float, RelPosVal)

cmd_ir/grammar.lark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ preamble: "preamble" "{" instruction* "}"
44

55
?body: function | extern_func
66

7-
extern_func: "extern" "function" FUNC_NAME NEWLINE
7+
extern_func: "extern" "function" FUNC_NAME (typelist|NULL) (typelist|NULL) NEWLINE
88

99
function: "function" FUNC_NAME "{" function_body "}"
1010

@@ -25,6 +25,7 @@ operand: IDENT | atom | tuple
2525

2626
tuple: "(" atom ["," atom] ")"
2727

28+
typelist: "(" IDENT ["," IDENT] ")"
2829

2930
LABEL: CNAME ":"
3031
IDENT: CNAME

0 commit comments

Comments
 (0)