Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
absorbs = (Expression, Variable, Tuple)
def disp(self, vm, msgs=None):
if isinstance(msgs, (tuple, list)):
vm.io.disp(', '.join(str(vm.disp_round(x)) for x in msgs))
else:
vm.io.disp(vm.disp_round(msgs))
class Output(Function):
def run(self, vm):
assert len(self.arg) == 3
row, col, msg = vm.get(self.arg)
vm.io.output(row, col, vm.disp_round(msg))
class Prompt(Token):
absorbs = (Expression, Variable, Tuple)
def run(self, vm):
if not self.arg:
raise ExecutionError('%s used without arguments')
if isinstance(self.arg, Tuple):
for var in self.arg.contents:
self.prompt(vm, var)
else:
self.prompt(vm, self.arg)
def prompt(self, vm, var):
val = vm.io.input(var.token + '?')
var.set(vm, val)
def __repr__(self):
def run(self, vm):
arg = self.arg
if not arg:
raise ExecutionError('Input used without arguments')
if isinstance(arg, Tuple) and len(arg) == 1 or isinstance(arg, Variable):
self.prompt(vm, arg)
elif isinstance(arg, Tuple) and len(arg) == 2:
self.prompt(vm, arg.contents[1], vm.get(arg.contents[0]))
else:
raise ExecutionError('Input used with wrong number of arguments')
and not isinstance(self.stack[-1], Tuple):
expr = self.stack.pop()
tup = self.stack[-1]
tup.append(expr)
tup.sep()
elif self.stack and isinstance(self.stack[-1], Tuple):
self.stack[-1].sep()
elif self.stack:
raise ParseError('comma encountered with an unclosed non-tuple expression on the stack')
else:
if self.lines[-1]:
token = self.lines[-1].pop()
else:
self.error('Encountered comma, but cannot find anything to put in the tuple')
tup = Tuple()
tup.append(token)
self.stack.append(tup)
tup.sep()
if isinstance(self.stack[-1], FunctionArgs):
self.stack.append(Expression())
self.inc()
continue
elif '0' <= char <= '9' or char == '.'\
or isinstance(self.token(sub=True, inc=False), tokens.Minus) and self.number(test=True):
result = tokens.Value(self.number())
elif char in u'l∟' and self.more(self.pos+1) and self.source[self.pos+1] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789':
result = self.list()
elif char.isalpha():
result = self.token()
result = stack
stack.finish()
self.inc()
break
elif char != stack.end:
self.error('tried to end \'%s\' with: "%s" (expecting "%s")' % (stack, char, stack.end))
else:
stacks.append(stack)
else:
stacks.append(stack)
else:
self.error('encountered "%s" but we have no expression on the stack to terminate' % char)
elif char == ',':
if len(self.stack) > 1 and isinstance(self.stack[-2], Tuple)\
and not isinstance(self.stack[-1], Tuple):
expr = self.stack.pop()
tup = self.stack[-1]
tup.append(expr)
tup.sep()
elif self.stack and isinstance(self.stack[-1], Tuple):
self.stack[-1].sep()
elif self.stack:
raise ParseError('comma encountered with an unclosed non-tuple expression on the stack')
else:
if self.lines[-1]:
token = self.lines[-1].pop()
else:
self.error('Encountered comma, but cannot find anything to put in the tuple')
tup = Tuple()
tup.append(token)
else:
if expr:
new.append(expr)
expr = None
new.append(token)
if expr:
new.append(expr)
if new:
# implied expressions need to be added to tuples in their entirety, instead of just their last element
pops = []
for i in xrange(0, len(new)-1):
e, t = new[i], new[i+1]
if isinstance(e, Expression) and isinstance(t, Tuple):
pops.append(i)
e.append(t.contents[0].flatten())
t.contents[0] = e
for p in reversed(sorted(pops)):
new.pop(p)
# tokens with the absorb mechanic can steal the next token from the line if it matches a list of types
last = new[0]
pops = []
for i in xrange(1, len(new)):
token = new[i]
if isinstance(token, last.absorbs):
if isinstance(token, BaseExpression):
token = token.flatten()