Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
instruction = pysh_instruction.Pysh_Instruction(pysh_type[1:] + '_dec',
dec,
stack_types = [pysh_type])
return instruction
registered_instructions.register_instruction(decrementer('_integer'))
registered_instructions.register_instruction(decrementer('_float'))
def float_sin(state):
'''
'''
if len(state.stacks['_float']) > 0:
new_float = math.sin(state.stacks['_float'].stack_ref(0))
state.stacks['_float'].pop_item()
state.stacks['_float'].push_item(new_float)
float_sin_instruction = pysh_instruction.Pysh_Instruction('float_sin',
float_sin,
stack_types = ['_float'])
registered_instructions.register_instruction(float_sin_instruction)
def float_cos(state):
'''
'''
if len(state.stacks['_float']) > 0:
new_float = math.cos(state.stacks['_float'].stack_ref(0))
state.stacks['_float'].pop_item()
state.stacks['_float'].push_item(new_float)
float_cos_instruction = pysh_instruction.Pysh_Instruction('float_cos',
float_cos,
stack_types = ['_float'])
registered_instructions.register_instruction(float_cos_instruction)
ri.register_instruction(exec_do_range_intruction)
def code_do_count(state):
if not (len(state.stacks['_integer']) == 0 or state.stacks['_integer'].stack_ref(0) < 1 or len(state.stacks['_code']) == 0):
to_push = [0,
state.stacks['_integer'].stack_ref(0) - 1,
lambda: ri.get_instruction_by_name('_code_from_exec'),
state.stacks['_code'].stack_ref(0),
lambda: ri.get_instruction_by_name('code_do*range')]
state.stacks['_code'].pop_item()
state.stacks['_integer'].pop_item()
state.stacks['_exec'].push_item(to_push)
return state
code_do_count_intruction = pysh_instruction.Pysh_Instruction('code_do*count',
code_do_count,
stack_types = ['_exec', '_integer', '_code'])
ri.register_instruction(code_do_count_intruction)
def exec_do_count(state):
'''
differs from code.do*count only in the source of the code and the recursive call
'''
if not (len(state.stacks['_integer']) == 0 or state.stacks['_integer'].stack_ref(0) < 1 or len(state.stacks['_exec']) == 0):
to_push = [0,
state.stacks['_integer'].stack_ref(0) - 1,
lambda: ri.get_instruction_by_name('_exec_do*range'),
state.stacks['_exec'].stack_ref(0)]
state.stacks['_exec'].pop_item()
state.stacks['_integer'].pop_item()
def exec_do_count(state):
'''
differs from code.do*count only in the source of the code and the recursive call
'''
if not (len(state.stacks['_integer']) == 0 or state.stacks['_integer'].stack_ref(0) < 1 or len(state.stacks['_exec']) == 0):
to_push = [0,
state.stacks['_integer'].stack_ref(0) - 1,
lambda: ri.get_instruction_by_name('_exec_do*range'),
state.stacks['_exec'].stack_ref(0)]
state.stacks['_exec'].pop_item()
state.stacks['_integer'].pop_item()
state.stacks['_exec'].push_item(to_push)
return state
exec_do_count_intruction = pysh_instruction.Pysh_Instruction('exec_do*count',
exec_do_count,
stack_types = ['_exec', '_integer'],
parentheses = 1)
ri.register_instruction(exec_do_count_intruction)
def code_do_times(state):
if not (len(state.stacks['_integer']) == 0 or state.stacks['_integer'].stack_ref(0) < 1 or len(state.stacks['_code']) == 0):
to_push = [0,
state.stacks['_integer'].stack_ref(0) - 1,
lambda: ri.get_instruction_by_name('_code_from_exec'),
[lambda: ri.get_instruction_by_name('_integer_pop')] + u.ensure_list(state.stacks['_exec'].stack_ref(0)),
lambda: ri.get_instruction_by_name('_code_do*range')]
state.stacks['_code'].pop_item()
state.stacks['_integer'].pop_item()
state.stacks['_exec'].push_item(to_push)
def code_maker(pysh_type):
def f(state):
if len(state.stacks[pysh_type]) > 0:
new_code = state.stacks[pysh_type].stack_ref(0)
state.stacks[pysh_type].pop_item()
state.stacks['_code'].push_item(new_code)
return state
instruction = pysh_instruction.Pysh_Instruction('code_from' + pysh_type,
f,
stack_types = ['_code', pysh_type])
if pysh_type == '_exec':
instruction.parentheses = 1
return instruction
ri.register_instruction(code_maker('_boolean'))
def string_replace(state):
'''
In third string on stack, replaces all occurences of second string with first string
'''
if len(state.stacks['_string']) > 2:
replace_this = state.stacks['_string'].stack_ref(1)
with_this = state.stacks['_string'].stack_ref(0)
in_this = state.stacks['_string'].stack_ref(2)
new_string = in_this.replace(replace_this, with_this)
state.stacks['_string'].pop_item()
state.stacks['_string'].pop_item()
state.stacks['_string'].pop_item()
state.stacks['_string'].push_item(new_string)
return state
string_replace_instruction = pysh_instruction.Pysh_Instruction('string_replace',
string_replace,
stack_types = ['_string'])
registered_instructions.register_instruction(string_replace_instruction)
def greater_than_equal(pysh_type):
'''
Returns a function that pushes TRUE to the bool stack if the
second item on the stack is greater than, or equal, the first item.
'''
def gte(state):
if len(state.stacks[pysh_type])>1:
new_bool = state.stacks[pysh_type].stack_ref(1) > state.stacks[pysh_type].stack_ref(0)
state.stacks[pysh_type].pop_item()
state.stacks[pysh_type].pop_item()
state.stacks['_boolean'].push_item(new_bool)
instruction = pysh_instruction.Pysh_Instruction(pysh_type[1:] + '_gte',
gte,
stack_types = [pysh_type, '_boolean'])
return instruction
registered_instructions.register_instruction(greater_than_equal('_integer'))
'''
Holds information about a push instruction. Can be found in a
Push program.
'''
def __init__(self, name, func, stack_types = [], parentheses = 0):
self.name = name
self.func = func
self.stack_types = stack_types
self.parentheses = parentheses # Specifies parens group. (0, 1, 2, ... etc)
self.atom_type = INSTRUCTION_ATOM_TYPE
def __repr__(self):
return self.name + "_INSTR"
class Pysh_Input_Instruction(Pysh_Instruction):
'''
Subclass secific to storing information about input instructions
which are generated based on initial state for the _input stack.
'''
def __init__(self, name):
Pysh_Instruction.__init__(self, name, None)
self.name = name
self.func = name
self.stack_types = '_input'
self.atom_type = INPUT_INSTRUCTION_ATOM_TYPE
def __repr__(self):
return self.name + "_INPT_INSTR"
from __future__ import absolute_import, division, print_function, unicode_literals
from .. import pysh_state
from .. import pysh_instruction
from .. import pysh_utils
from . import registered_instructions
def string_from_integer(state):
if len(state.stacks['_integer']) > 0:
top_int = state.stacks['_integer'].stack_ref(0)
state.stacks['_integer'].pop_item()
state.stacks['_string'].push_item(str(top_int))
return state
string_from_integer_intruction = pysh_instruction.Pysh_Instruction('string_from_integer',
string_from_integer,
stack_types = ['_string', '_integer'])
registered_instructions.register_instruction(string_from_integer_intruction)
#
#string_from_integer
#Casts the top integer to a string and pushes the result onto the string stack.
#
def string_from_float(state):
if len(state.stacks['_float']) > 0:
top_float = state.stacks['_float'].stack_ref(0)
state.stacks['_float'].pop_item()
state.stacks['_string'].push_item(str(top_float))
return state
string_from_float_intruction = pysh_instruction.Pysh_Instruction('string_from_float',
state.stacks['_boolean'].push_item(result)
return state
boolean_or_intruction = pysh_instruction.Pysh_Instruction('boolean_or',
boolean_or,
stack_types = ['_boolean'])
registered_instructions.register_instruction(boolean_or_intruction)
def boolean_not(state):
if len(state.stacks['_boolean']) > 1:
result = not state.stacks['_boolean'].stack_ref(0)
state.stacks['_boolean'].pop_item()
state.stacks['_boolean'].push_item(result)
return state
boolean_not_intruction = pysh_instruction.Pysh_Instruction('boolean_not',
boolean_not,
stack_types = ['_boolean'])
registered_instructions.register_instruction(boolean_not_intruction)
def boolean_xor(state):
if len(state.stacks['_boolean']) > 1:
result = not (state.stacks['_boolean'].stack_ref(0) == state.stacks['_boolean'].stack_ref(1))
state.stacks['_boolean'].pop_item()
state.stacks['_boolean'].pop_item()
state.stacks['_boolean'].push_item(result)
return state
boolean_xor_intruction = pysh_instruction.Pysh_Instruction('boolean_xor',
boolean_xor,
stack_types = ['_boolean'])
if len(state.stacks['_float']) > 0:
new_int = int(state.stacks['_float'].stack_ref(0))
state.stacks['_float'].pop_item()
state.stacks['_integer'].push_item(new_int)
int_from_float_instrc = pysh_instruction.Pysh_Instruction('integer_from_float',
integer_from_float,
stack_types = ['_integer', '_float'])
registered_instructions.register_instruction(int_from_float_instrc)
def integer_from_boolean(state):
if len(state.stacks['_boolean']) > 0:
new_int = int(state.stacks['_boolean'].stack_ref(0))
state.stacks['_boolean'].pop_item()
state.stacks['_integer'].push_item(new_int)
int_from_boolean_instrc = pysh_instruction.Pysh_Instruction('integer_from_boolean',
integer_from_boolean,
stack_types = ['_integer', '_boolean'])
registered_instructions.register_instruction(int_from_boolean_instrc)
def float_from_integer(state):
if len(state.stacks['_integer']) > 0:
new_float = float(state.stacks['_integer'].stack_ref(0))
state.stacks['_integer'].pop_item()
state.stacks['_float'].push_item(new_float)
float_from_int_instrc = pysh_instruction.Pysh_Instruction('float_from_integer',
float_from_integer,
stack_types = ['_float', '_integer'])
registered_instructions.register_instruction(float_from_int_instrc)