add conditionals

This commit is contained in:
jane 2020-11-10 17:59:59 -05:00
parent 3116435878
commit d445996bdb
2 changed files with 50 additions and 22 deletions

View file

@ -71,6 +71,7 @@ def socket_loop():
pattern_data = sock.recv(length)
logger.debug(pattern_data)
pattern.parse(pattern_data.decode())
this.color_state = 7
def loop():

View file

@ -9,6 +9,11 @@ this.encoded = None
this.pattern = None
this.values = {
"stack": 0,
"stack2": 0,
"stack3": 0,
"stack4": 0,
"stack5": 0,
"stack6": 0,
"r": 0,
"g": 0,
"b": 0,
@ -17,52 +22,70 @@ this.values = {
}
def constant(target, arg):
return arg
def constant(target, arg, index):
return (arg, index)
def add(target, arg):
return target + arg
def add(target, arg, index):
return (target + arg, index)
def sub(target, arg):
return target - arg
def sub(target, arg, index):
return (target - arg, index)
def mult(target, arg):
return target * arg
def mult(target, arg, index):
return (target * arg, index)
def div(target, arg):
return target / arg
def div(target, arg, index):
return (target / arg, index)
def mod(target, arg):
def mod(target, arg, index):
if arg <= 0:
return target
return target % arg
return (target % arg, index)
def rand(target, arg):
return random.randrange(0, 255)
def _apply(target, arg, func):
def jnz(target, arg, index):
if target != 0:
return (target, arg)
else:
return (target, index)
def jez(target, arg, index):
if target == 0:
return (target, arg)
else:
return (target, index)
def _apply(index, target, arg, func):
if type(arg) is int:
return func(target, arg)
return func(target, arg, index)
elif type(arg) is str and arg in this.values:
return func(target, this.values[arg])
return func(target, this.values[arg], index)
def apply(targets, args, func):
def apply(index, targets, args, func):
for target in range(len(targets)):
if this.values[targets[target]['channel']] != None:
if target < len(args):
this.values[targets[target]['channel']] = _apply(
this.values[targets[target]['channel']], args[target], func)
val, jump = _apply(
index, this.values[targets[target]['channel']], args[target], func)
if val != this.values[targets[target]['channel']]:
this.values[targets[target]['channel']] = val
else:
this.values[targets[target]['channel']] = _apply(
this.values[targets[target]['channel']], 0, func)
val, jump = _apply(
index, this.values[targets[target]['channel']], 0, func)
if val != this.values[targets[target]['channel']]:
this.values[targets[target]['channel']] = val
logger.debug("{} : {}".format(
targets[target]['channel'],
this.values[targets[target]['channel']]
@ -76,7 +99,9 @@ this.instructions = {
"MULTIPLY": mult,
"DIVIDE": div,
"MODULO": mod,
"RANDOM": rand
"RANDOM": rand,
"JNZ": jnz,
"JEZ": jez
}
@ -96,7 +121,9 @@ def pat(index, tick, previous_values):
if 'args' in this.pattern[i]['instruction']:
args = this.pattern[i]['instruction']['args']
if this.instructions[name] != None:
apply(targets, args, this.instructions[name])
jump = apply(index, targets, args, this.instructions[name])
if jump != i and jump < len(this.pattern) - 1:
i = jump - 1
r = this.values["r"]
g = this.values["g"]
b = this.values["b"]