/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
 
 This file is part of LiveCode.
 
 LiveCode is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License v3 as published by the Free
 Software Foundation.
 
 LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
 along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

/*  
This module specifies the bitwise operations on integers included in the standard library of LiveCode Builder.
*/

module com.livecode.bitwise

use com.livecode.foreign

public foreign handler MCBitwiseEvalBitwiseAnd(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseOr(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseNot(in Operand as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseXor(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"

public foreign handler MCBitwiseEvalBitwiseShiftLeft(in Target as LCInt, in Shift as LCUInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCBitwiseEvalBitwiseShiftRight(in Target as LCInt, in Shift as LCUInt, out Value as LCInt) returns nothing binds to "<builtin>"

--

/*
Summary:        Performs a bitwise AND operation on the binary representations of <Left> and <Right>.

Left:           An expression which evaluates to an integer.
Right:          An expression which evaluates to an integer.

Returns: 	The integer whose binary representation is the result of the bitwise AND operation.

Example:
	variable tVar as Number
	put 3 bitwise and 6 into tVar -- tVar contains 2
	
Description:
Each bit of <Left> bitwise and <Right> is 1 if and only if both the corresponding bit of the binary representation of <Left> and that of <Right> is 1. Otherwise it is 0.

Tags: Bitwise operations

*/

syntax BitwiseAnd is left binary operator with precedence 2
    <Left: Expression> "bitwise and" <Right: Expression>
begin
    MCBitwiseEvalBitwiseAnd(Left, Right, output)
end syntax

/*
Summary:        Performs a bitwise OR operation on the binary representations of <Left> and <Right>.

Left:           An expression which evaluates to an integer.
Right:          An expression which evaluates to an integer.

Returns: 	The integer whose binary representation is the result of the bitwise OR operation.
                

Example:
	variable tVar as Number
	put 3 bitwise or 6 into tVar -- tVar contains 7
	
Description:
Each bit of <Left> bitwise or <Right> is 0 if and only if both the corresponding bit of the binary representation of <Left> and that of <Right> is 0. Otherwise it is 1.

Tags: Bitwise operations
*/

syntax BitwiseOr is left binary operator with precedence 4
    <Left: Expression> "bitwise or" <Right: Expression>
begin
    MCBitwiseEvalBitwiseOr(Left, Right, output)
end syntax

/*
Summary:        Performs a bitwise XOR operation on the binary representations of <Left> and <Right>.

Left:           An expression which evaluates to an integer.
Right:          An expression which evaluates to an integer.

Returns: 	The integer whose binary representation is the result of the bitwise XOR operation.

Example:
	variable tVar as Number
	put 3 bitwise xor 6 into tVar -- tVar contains 5
	
Description:
Each bit of <Left> bitwise xor <Right> is 1 if and only if exactly one of the corresponding bits of the binary representation of <Left> and that of <Right> is 1. Otherwise it is 0.

Tags: Bitwise operations
*/

syntax BitwiseXor is left binary operator with precedence 3
    <Left: Expression> "bitwise xor" <Right: Expression>
begin
    MCBitwiseEvalBitwiseXor(Left, Right, output)
end syntax

/*
Summary:        Performs a bitwise NOT operation on the binary representation of <Operand>.

Operand:        An expression which evaluates to an integer.

Returns: 	The integer whose binary representation is the result of the bitwise NOT operation.

Example:
	variable tVar as Number
	put bitwise not -5 into tVar -- tVar contains 4

Description:
Bitwise not returns the complement of <Operand> as a signed two's
complement integer, i.e. equivalent to -(x + 1).

Tags: Bitwise operations
*/

syntax BitwiseNot is prefix operator with precedence 1
    "bitwise" "not" <Operand: Expression>
begin
    MCBitwiseEvalBitwiseNot(Operand, output)
end syntax

--

/*
Summary:	Shifts the bits of <Operand> left.

Operand:        An expression which evaluates to an integer.
Shift:          An expression which evaluates to an integer.

Returns:	The result of bit-shifting <Operand> left by <Shift> places.

Example:
	variable tVar
	put 7 shifted left by 2 bitwise into tVar -- tVar contains 28

Description:
Shifts the bits of <Operand> left. Shifting the bits of <Operand> left
by x is equivalent to multiplying by 2^x.

Tags: Bitwise operations
*/

syntax BitwiseShiftLeft is postfix operator with precedence 1
    <Operand: Expression> "shifted" "left" "by" <Shift: Expression> "bitwise"
begin
    MCBitwiseEvalBitwiseShiftLeft(Operand, Shift, output)
end syntax

/*
Summary:	Shifts the bits of <Operand> right.

Operand:        An expression which evaluates to an integer.
Shift:          An expression which evaluates to an integer.

Returns:	The result of bit-shifting <Operand> right by <Shift> places.

Example:
	variable tVar
	put 7 shifted right by 2 bitwise into tVar -- tVar contains 1

Description:
Shifts the bits of <Operand> right. Shifting the bits of <Operand>
right by x is equivalent to dividing by 2^x (rounding down)

Tags: Bitwise operations
*/

syntax BitwiseShiftRight is postfix operator with precedence 1
    <Operand: Expression> "shifted" "right" "by" <Shift: Expression> "bitwise"
begin
    MCBitwiseEvalBitwiseShiftRight(Operand, Shift, output)
end syntax

--

//syntax BitwiseRotate is postfix operator with precedence 1
//    <Operand: Expression> "rotated" "by" <Shift: Expression> "bitwise"
//begin
//    EvalBitwiseRotate(Operand, Shift, output)
//end syntax

end module
