From a55988cb338e6b2f2a9a163c18cbe0622ec013bc Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Tue, 19 Jul 2016 08:41:03 -0400 Subject: [PATCH] @static && || syntax --- base/osutils.jl | 9 ++++++--- test/choosetests.jl | 2 +- test/misc.jl | 18 ------------------ test/osutils.jl | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 test/osutils.jl diff --git a/base/osutils.jl b/base/osutils.jl index d858e84ad4b88..4cf45a7dc9e11 100644 --- a/base/osutils.jl +++ b/base/osutils.jl @@ -56,17 +56,20 @@ Partially evaluates an expression at parse time. For example, `@static is_windows() ? foo : bar` will evaluate `is_windows()` and insert either `foo` or `bar` into the expression. This is useful in cases where a construct would be invalid on other platforms, such as a `ccall` to a non-existent function. +`@static if is_apple() foo end` and `@static foo <&&,||> bar` are also valid syntax. """ macro static(ex) if isa(ex, Expr) - if ex.head === :if + if ex.head === :if || ex.head === :&& || ex.head === :|| cond = eval(current_module(), ex.args[1]) - if cond + if xor(cond, ex.head === :||) return esc(ex.args[2]) elseif length(ex.args) == 3 return esc(ex.args[3]) - else + elseif ex.head === :if return nothing + else + return cond end end end diff --git a/test/choosetests.jl b/test/choosetests.jl index 5a377a54d1371..46310500bd2b4 100644 --- a/test/choosetests.jl +++ b/test/choosetests.jl @@ -33,7 +33,7 @@ function choosetests(choices = []) "markdown", "base64", "serialize", "misc", "threads", "enums", "cmdlineargs", "i18n", "workspace", "libdl", "int", "checked", "intset", "floatfuncs", "compile", "parallel", "inline", - "boundscheck", "error", "ambiguous", "cartesian", "asmvariant", + "boundscheck", "error", "ambiguous", "cartesian", "asmvariant", "osutils", "channels" ] profile_skipped = false diff --git a/test/misc.jl b/test/misc.jl index dc087d5fd2d2a..7598c6d3f3eea 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -207,24 +207,6 @@ end @test isa(ex, ErrorException) && ex.msg == "cannot assign variables in other modules" end -@test !Base.is_unix(:Windows) -@test !Base.is_linux(:Windows) -@test Base.is_linux(:Linux) -@test Base.is_windows(:Windows) -@test Base.is_windows(:NT) -@test !Base.is_windows(:Darwin) -@test Base.is_apple(:Darwin) -@test Base.is_apple(:Apple) -@test !Base.is_apple(:Windows) -@test Base.is_unix(:Darwin) -@test Base.is_unix(:FreeBSD) -@test_throws ArgumentError Base.is_unix(:BeOS) -if !is_windows() - @test Sys.windows_version() === (0, 0) -else - @test (Sys.windows_version()::Tuple{Int,Int})[1] > 0 -end - # Issue 14173 module Tmp14173 export A diff --git a/test/osutils.jl b/test/osutils.jl new file mode 100644 index 0000000000000..e75ded56a1eaf --- /dev/null +++ b/test/osutils.jl @@ -0,0 +1,26 @@ +@test !Base.is_unix(:Windows) +@test !Base.is_linux(:Windows) +@test Base.is_linux(:Linux) +@test Base.is_windows(:Windows) +@test Base.is_windows(:NT) +@test !Base.is_windows(:Darwin) +@test Base.is_apple(:Darwin) +@test Base.is_apple(:Apple) +@test !Base.is_apple(:Windows) +@test Base.is_unix(:Darwin) +@test Base.is_unix(:FreeBSD) +@test_throws ArgumentError Base.is_unix(:BeOS) +if !is_windows() + @test Sys.windows_version() === (0, 0) +else + @test (Sys.windows_version()::Tuple{Int,Int})[1] > 0 +end + +@test (@static true ? 1 : 2) === 1 +@test (@static false ? 1 : 2) === 2 +@test (@static if true 1 end) === 1 +@test (@static if false 1 end) === nothing +@test (@static true && 1) === 1 +@test (@static false && 1) === false +@test (@static true || 1) === true +@test (@static false || 1) === 1