/* 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 library consists of the logical operations included in the standard library of LiveCode Builder.
*/

module com.livecode.logic

use com.livecode.foreign

//foreign handler MCLogicEvalAnd(in Left as CBool, in Right as CBool, out Value as CBool) returns nothing binds to "<builtin>"
//foreign handler MCLogicEvalOr(in Left as CBool, in Right as CBool, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCLogicEvalNot(in Operand as CBool, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCLogicEvalIsEqualTo(in Left as CBool, in Right as CBool, out Value as CBool) returns nothing binds to "<builtin>"
public foreign handler MCLogicEvalIsNotEqualTo(in Left as CBool, in Right as CBool, out Value as CBool) returns nothing binds to "<builtin>"

public foreign handler MCLogicEvalBoolFormattedAsString(in Target as CBool, out Value as String) returns nothing binds to "<builtin>"
public foreign handler MCLogicEvalStringParsedAsBool(in Target as String, out Value as optional Boolean) returns nothing binds to "<builtin>"
public foreign handler MCLogicExecFormatBoolAsString(in Target as CBool) returns String binds to "<builtin>"
public foreign handler MCLogicExecParseStringAsBool(in Target as String) returns optional Boolean binds to "<builtin>"

--

/*
Summary:    Returns the boolean-logical value of the expression '<Left> and <Right>'

Left:       An expression that evaluates to a boolean value
Right:      An expression that evaluates to a boolean value

Returns: 	If the left hand expression evaluates to false, the value of the expression is false.
            Otherwise, the operator evaluates to the value of the right hand expression.
            Note that the right hand expression is only evaluated if the left hand expression is true.
*/

//syntax LogicOr is left binary operator of precedence 4
//    <Left: Expression> "or" <Right: Expression>
//begin
//    EvalOr(Left, Right, output)
//end syntax

/*
Summary:    Returns the boolean-logical value of the expression '<Left> or <Right>'

Left:       An expression that evaluates to a boolean value
Right:      An expression that evaluates to a boolean value

Returns: 	If the left hand expression evaluates to true, the value of the expression is true.
            Otherwise, the operator evaluates to the value of the right hand expression.
            Note that the right hand expression is only evaluated if the left hand expression is false.
*/

//syntax LogicAnd is left binary operator of precedence 3
//    <Left: Expression> "and" <Right: Expression>
//begin
//    EvalAnd(Left, Right, output)
//end syntax

/*
Summary:    Returns the boolean-logical value of the expression 'not <Operand>'

Operand:    An expression that evaluates to a boolean value

Returns: 	If the operand expression evaluates to true, the value of the expression is false. 
            Otherwise, the value of the expression is true.

Example:
	variable tBool is bool
	put not not not not true into tBool is true
	
Tags: Logic
	
*/

syntax LogicNot is prefix operator with precedence 6
    "not" <Operand: Expression>
begin
    MCLogicEvalNot(Operand, output)
end syntax

--

/*
Summary:    Determines whether <Left> and <Right> are equal or not.

Left:       An expression which evaluates to a boolean value.
Right:      An expression which evaluates to a boolean value.


Returns: 	Returns true if <Left> evaluates to the same value as <Right>.

Example:
	variable tBool is bool
	put true is true into tBool -- tBool is true
	
Tags: Logic
*/

syntax BooleanIsEqualToBoolean is neutral binary operator with precedence 7
    <Left: Expression> "is" <Right: Expression>
begin
    MCLogicEvalIsEqualTo(Left, Right, output)
end syntax

/*
Summary:    Determines whether <Left> and <Right> are equal or not.

Left:       An expression which evaluates to a boolean value.
Right:      An expression which evaluates to a boolean value.


Returns: 	Returns true if <Left> evaluates to the opposite boolean value as <Right>.

Example:
	variable tBool is bool
	put true is not false into tBool -- tBool is true
	
Tags: Logic
*/

syntax BooleanIsNotEqualToBoolean is neutral binary operator with precedence 7
    <Left: Expression> "is not" <Right: Expression>
begin
    MCLogicEvalIsNotEqualTo(Left, Right, output)
end syntax

/*
Summary:	Formats a boolean value as a string

Operand:	An expression that evaluates to a boolean value.

Returns:	"true" or "false"

Example:
	variable tString as String
	variable tBool as Boolean
	put (false is not not true) is false into tBool
	put tBool formatted as string into tString -- tString contains "true"

Description:
Use <BooleanFormattedAsString> when you want to manipulate a boolean value as text.
*/

syntax BooleanFormattedAsString is postfix operator with precedence 1
    <Operand: Expression> "formatted" "as" "string"
begin
    MCLogicEvalBoolFormattedAsString(Operand, output)
end syntax

/*
Summary:	Formats a boolean value as a string

Operand:	An expression that evaluates to a boolean value.

The result:	"true" or "false"

Example:
	variable tVar as String
	parse 1 = 0 as String
	put the result into tVar
	put "e" into char 3 of tVar -- tVar is "tree"

Description:
Use <FormatBooleanAsString> when you want to manipulate a boolean value as text.
*/

syntax FormatBooleanAsString is statement
    "format" <Operand: Expression> "as" "string"
begin
    MCLogicExecFormatBoolAsString(Operand)
end syntax

/*
Summary:	Parses a string as a boolean value

Operand:	An expression that evaluates to a string.

Returns:	true or false

Example:
	variable tString as String
	variable tBool as optional Boolean
	put "false" into tString
	put tString parsed as boolean into tBool -- tBool contains false
	
Example:
	variable tBool as optional Boolean
	put "flase" parsed as boolean into tBool -- tBool is nothing
Description:
Use <StringParsedAsBoolean> when you want to determine if a string contains "true" or "false"

>*Note:* Only the strings "true" and "false" will parse to give a boolean value. Any other input will cause an error to be thrown.
*/
syntax StringParsedAsBoolean is postfix operator with precedence 1
    <Operand: Expression> "parsed" "as" "boolean"
begin
    MCLogicEvalStringParsedAsBool(Operand, output)
end syntax

/*
Summary:	Parses a string as a boolean value

Operand:	An expression that evaluates to a string.

Returns:	true or false

Example:
	variable tString as String
	variable tBool as optional Boolean
	combine ["f", "a", "l", "s" "e"] with "" into tString
	put tString parsed as boolean into tBool -- tBool contains false

Example:
	variable tResult as String
	parse "sdfsdf" as Boolean
	if the result is nothing then
		put "not a valid boolean" into tResult
	end if

Description:
Use <StringParsedAsBoolean> when you want to determine if a string contains "true" or "false"

>*Note:* Only the strings "true" and "false" will parse to give a boolean value. Any other input will cause an error to be thrown.
*/
syntax ParseStringAsBoolean is statement
    "parse" <Operand: Expression> "as" "boolean"
begin
    MCLogicExecParseStringAsBool(Operand)
end syntax

end module
