Skip to content

Commit

Permalink
Support value comparison for defines
Browse files Browse the repository at this point in the history
  • Loading branch information
drslump committed Dec 20, 2013
1 parent ef0e87c commit 5292cc6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/Boo.Lang.Extensions/Macros/IfdefMacro.boo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region license
#region license
// Copyright (c) 2009 Rodrigo B. de Oliveira ([email protected])
// All rights reserved.
//
Expand All @@ -25,8 +25,8 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion




namespace Boo.Lang.Extensions

import Boo.Lang.Compiler
Expand Down Expand Up @@ -59,6 +59,25 @@ class IfdefMacro(AbstractAstMacro):
return Parameters.Defines.ContainsKey(condition.Name)

private def EvaluateBinary(condition as BinaryExpression):
if condition.Operator in (BinaryOperatorType.Equality, BinaryOperatorType.Inequality):
lft = condition.Left.ToString()

if not Parameters.Defines.ContainsKey(lft):
return false

rgt as string
if condition.Right isa ReferenceExpression:
rgt = (condition.Right as ReferenceExpression).Name
elif condition.Right isa StringLiteralExpression:
rgt = (condition.Right as StringLiteralExpression).Value
else:
rgt = condition.Right.ToString()

if condition.Operator == BinaryOperatorType.Equality:
return Parameters.Defines[lft] == rgt
else:
return Parameters.Defines[lft] != rgt

if condition.Operator == BinaryOperatorType.Or:
return Evaluate(condition.Left) or Evaluate(condition.Right)
if condition.Operator == BinaryOperatorType.And:
Expand Down
18 changes: 15 additions & 3 deletions tests/testcases/macros/ifdef-1.boo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ FOO or BAR
DEFINES: BAZ
not FOO
not BAR
DEFINES: FOO=foo, QUX=qux
FOO
FOO or BAR
not BAR
FOO == 'foo'
(QUX == qux) or (QUX == 10)
"""
import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
Expand All @@ -24,7 +30,12 @@ def compileWithDefines(module as Module, *defines as (string)):
compiler.Parameters.Pipeline = Pipelines.CompileToMemory()
compiler.Parameters.References.Add(System.Reflection.Assembly.GetExecutingAssembly())
for define in defines:
compiler.Parameters.Defines.Add(define, null)
pair = define.Split("=".ToCharArray(), 2)
if len(pair) == 2:
compiler.Parameters.Defines.Add(pair[0], pair[1])
else:
compiler.Parameters.Defines.Add(pair[0], null)

result = compiler.Run(CompileUnit(module.CloneNode()))
assert len(result.Errors) == 0, result.Errors.ToString(true)
return result.GeneratedAssembly
Expand All @@ -48,12 +59,13 @@ module = [|
printIfdef FOO or BAR
printIfdef not FOO
printIfdef not BAR

printIfdef FOO == 'foo'
printIfdef QUX == qux or QUX == 10
|]


runWithDefines module, "FOO"
runWithDefines module, "BAR"
runWithDefines module, "FOO", "BAR"
runWithDefines module, "BAZ"
runWithDefines module, "FOO=foo", "QUX=qux"

0 comments on commit 5292cc6

Please sign in to comment.