• Facebook
  • Twitter
  • Reddit
  • StumbleUpon
  • Digg
  • email

'''
Module for operations in Gcode
 
add=        a   +   b
sub=        a   -   b
neg=        --  a
div=        a   /   b
intdiv=     a   //  b
mul=        a   *   b
mod=        a   %   b
xor=        int(a)  ^   int(b)
and=        int(a)  &   int(b) 
or =        int(a)  |   int(b)  
invert=     ~   int(a)
equal=      bit wise a = b
lrot=       rotates int(a) by int(b) left
lrot=       rotates int(a) by int(b) right
boolor=     a   ||  b (a or b)
booland=    a   &&  b (a and b)
boolequal=  a   ==  b 
boolnot=    ~~ a (not a)
 
 
'''
 
 
 
import sys
 
from Gcodefunction import Gcodefunction, Gcodeoperator
 
equal= lambda a, b:~int(a)^int(b)
equal.__name__= 'equal'
 
lrot= lambda a, b: int(int(a) << int(b) % 32) | (int(a) >> (32 - int(b) % 32)) & -1     
lrot.__name__= 'lrot'
rrot= lambda a, b: int(int(a) >> int(b) % 32) | (int(a) << (32 - int(b) % 32)) & -1    
rrot.__name__= 'rrot'
 
operations= dict(
invert= Gcodeoperator(lambda a: ~int(a) , 1, '~', 12, '=> ~i\n\tbitwise invert\nsame as !i'),
invert2= Gcodeoperator(lambda a: ~int(a) , 1, '!', 12, '=> !i\n\tbitwise invert\nsame as ~i'),
neg= Gcodeoperator(lambda a: -a , 1, '--', 12, '=> 0 - x \n\tnegate'),
 
div= Gcodeoperator(lambda a, b: float(a)/b , 2, '/', 11, '=> x / y\n\tdivide'),
intdiv= Gcodeoperator(lambda a, b: a//b , 2, '//', 11, '=> x // y \n\tdivide, => integer'),
mul= Gcodeoperator(lambda a, b: a*b , 2, '*', 11, '=> x * y \n\tmultiply'),
mod= Gcodeoperator(lambda a, b: a%b , 2, '%', 11, '=> x % y\n\tmodulo (whats left from //)'),
 
add= Gcodeoperator(lambda a, b: a+b , 2, '+', 10, '=> x + y \n\tplus'),
sub= Gcodeoperator(lambda a, b: a-b , 2, '-', 10, '=> x - y\n\tminus never write only -x!! must be --x'),
 
lrot= Gcodeoperator(lrot, 2, '<<', 9, '=> x << y\n\trotate the bits of x left'),
rrot= Gcodeoperator(rrot, 2, '>>', 9, '=> x >> y\n\trotate the bits of x right'),
 
greater= Gcodeoperator(lambda a, b: a>b , 2, '>', 8, '=> x > y (true or false)\n\tx greater y'),
greaterequal= Gcodeoperator(lambda a, b: a>=b , 2, '>=', 8, '=> x >= y (true or false)\n\tx greater y or x equal y'),
less= Gcodeoperator(lambda a, b: a<b , 2, '<', 8, '=> x < y (true or false)\n\tx lower y'),
lessequal= Gcodeoperator(lambda a, b: a<=b , 2, '<=', 8, '=> x <= y (true or false)\n\tx lower y or x equal y'),
 
_boolequal= Gcodeoperator(lambda a, b: a == b , 2, '==', 7, '=> x == y (true or false)\n\tx is equal to y'),
_boolnonequal= Gcodeoperator(lambda a, b: a != b , 2, '!=', 7, '=> x != y (true or false)\n\tx is not equal to y'),
 
_and= Gcodeoperator(lambda a, b: int(a) & int(b) , 2, '&', 6, '=> x & y\n\tbitwise and'),
_or = Gcodeoperator(lambda a, b: int(a) | int(b)  , 2, '|', 5, '=> x | y \n\tbitwise or'),
xor= Gcodeoperator(lambda a, b: int(a) ^ int(b) , 2, '^', 4, '=> x ^ y\n\tbitwise exclusive or'),
equal= Gcodeoperator(equal, 2, '=', 3, '=> x = y \n\tbitwise equal'),
 
_boolnot= Gcodeoperator(lambda a: not a , 1, '~~', 2, '=> a == false\n\tlogic not'),
 
_booland= Gcodeoperator(lambda a, b: a and b , 2, '&&', 1, '=> a && b\n\tlogic and\n\ta and b will be executed'),
_boolor= Gcodeoperator(lambda a, b: a or b , 2, '||', 0, '=> a || b\n\tlogic or\n\ta and b will be executed'),
_boolxor= Gcodeoperator(lambda a, b: a and not b or b and not a , 2, '^^', -1, '=> a xor b\n\tlogic exclusive or\n\ta and b will be executed'),
ret= Gcodeoperator(lambda a, b: a or b , 2, '\\', -1.e2000, '=> a \ b\n\ta will be executed and b will be executed\n\t=> b'),
)
operators= dict([(v.name, v) for v in operations.values()]) # sign : function
 
for k, v in operations.items():
    if k.startswith('_'):
        operations[k[1:]]= v
        vars()[k.upper()[1:]]= k
    else:
        vars()[k.upper()]= k
 
del k,v