Skip to content

Commit

Permalink
Add tests for bitops; tweak docstrings a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Apr 8, 2021
1 parent c965f85 commit 0ad2691
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.9.0 / ???

* Add `--use-bit-lib` flag to allow bitwise operations to work in LuaJIT
* Add `macro-searchers` table for finding macros similarly to `package.searchers`
* Support `&as` inside pattern matches
* Include stack trace for errors during macroexpansion
Expand Down
6 changes: 3 additions & 3 deletions reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,9 @@ this `begin` or `progn`.
* `lshift`, `rshift`, `band`, `bor`, `bxor`, `bnot`: bitwise operations

These all work as you would expect, with a few caveats. The bitwise operators
are only availible in Lua 5.3+, unless you use the `--use-bit-lib` flag, which
lets them be used in LuaJIT. The integer division operator (`//`) is only
availible in Lua 5.3+.
are only availible in Lua 5.3+, unless you use the `--use-bit-lib` flag or
the `useBitLib` flag in the options table, which lets them be used in
LuaJIT. The integer division operator (`//`) is only availible in Lua 5.3+.

They all take any number of arguments, as long as that number is fixed
at compile-time. For instance, `(= 2 2 (unpack [2 5]))` will evaluate
Expand Down
56 changes: 28 additions & 28 deletions src/fennel/specials.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -786,29 +786,26 @@ Method name doesn't have to be known at compile-time; if it is, use
(define-arithmetic-special "//" nil :1)

(fn bitop-special [native-name lib-name zero-arity unary-prefix ast scope parent]
(local len (length ast))
(if (= len 1)
(do
(compiler.assert zero-arity "Expected more than 0 arguments." ast))
(let [operands []
padded-native-name (.. " " native-name " ")
prefixed-lib-name (.. "bit." lib-name)]
(for [i 2 len]
(let [subexprs (compiler.compile1 (. ast i) scope parent
{:nval (if (not= i len) 1)})]
(utils.map subexprs tostring operands)))
(if (= (length operands) 1)
(if utils.root.options.useBitLib
(.. prefixed-lib-name "(" unary-prefix ", " (. operands 1) ")")
(.. "(" unary-prefix padded-native-name (. operands 1) ")"))
(if utils.root.options.useBitLib
(.. prefixed-lib-name "(" (table.concat operands ", ") ")")
(.. "(" (table.concat operands padded-native-name) ")"))))))
(if (= (length ast) 1)
(compiler.assert zero-arity "Expected more than 0 arguments." ast)
(let [len (length ast)
operands []
padded-native-name (.. " " native-name " ")
prefixed-lib-name (.. "bit." lib-name)]
(for [i 2 len]
(let [subexprs (compiler.compile1 (. ast i) scope parent
{:nval (if (not= i len) 1)})]
(utils.map subexprs tostring operands)))
(if (= (length operands) 1)
(if utils.root.options.useBitLib
(.. prefixed-lib-name "(" unary-prefix ", " (. operands 1) ")")
(.. "(" unary-prefix padded-native-name (. operands 1) ")"))
(if utils.root.options.useBitLib
(.. prefixed-lib-name "(" (table.concat operands ", ") ")")
(.. "(" (table.concat operands padded-native-name) ")"))))))

(fn define-bitop-special [name zero-arity unary-prefix native]
(tset SPECIALS name (partial bitop-special native name zero-arity unary-prefix))
(doc-special name [:a :b "..."]
"Bitwise operator; works the same as Lua but accepts more arguments."))
(tset SPECIALS name (partial bitop-special native name zero-arity unary-prefix)))

(define-bitop-special :lshift nil :1 "<<")
(define-bitop-special :rshift nil :1 ">>")
Expand All @@ -817,18 +814,21 @@ Method name doesn't have to be known at compile-time; if it is, use
(define-bitop-special :bxor :0 :0 "~")

(doc-special :lshift [:x :n]
"Bitwise logical left shift of x by n bits; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
"Bitwise logical left shift of x by n bits.
Only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")

(doc-special :rshift [:x :n]
"Bitwise logical right shift of x by n bits; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
"Bitwise logical right shift of x by n bits.
Only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")

(doc-special :band [:x1 :x2]
"Bitwise AND of arguments; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
(doc-special :band [:x1 :x2 "..."] "Bitwise AND of any number of arguments.
Only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")

(doc-special :bor [:x1 :x2] "Bitwise OR of arguments; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
(doc-special :bor [:x1 :x2 "..."] "Bitwise OR of any number of arguments.
Only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")

(doc-special :bxor [:x1 :x2]
"Bitwise XOR of arguments; only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")
(doc-special :bxor [:x1 :x2 "..."] "Bitwise XOR of any number of arguments.
Only works in Lua 5.3+ or LuaJIT with the --use-bit-lib flag.")

(define-arithmetic-special :or :false)
(define-arithmetic-special :and :true)
Expand Down
28 changes: 28 additions & 0 deletions test/bit.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(local l (require :test.luaunit))
(local fennel (require :fennel))

(fn == [test expected]
(let [opts (if (rawget _G :jit)
{:useBitLib true}
(and (not= _VERSION "Lua 5.1") (not= _VERSION "Lua 5.2"))
{})]
;; skip the test on PUC 5.1 and 5.2
(when opts
(l.assertEquals (fennel.eval test opts) expected))))

(fn test-shifts []
(== "(lshift 33 2)" 132)
(== "(lshift 1)" 2)
(== "(rshift 33 2)" 8)
(let [(ok? msg) (pcall fennel.compileString "(lshift)")]
(l.assertFalse ok?)
(l.assertStrContains msg "Expected more than 0 arguments")))

(fn test-ops []
(== "(band 22 13)" 4)
(== "(bor 1 2 4 8)" 15)
(== "(bxor 1)" 1)
(== "(band)" 0))

{: test-shifts
: test-ops}
3 changes: 2 additions & 1 deletion test/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ end

if(#arg == 0) then
testall({"core", "mangling", "quoting", "misc", "docstring", "fennelview",
"parser", "failures", "repl", "cli", "macro", "linter", "loops"})
"parser", "failures", "repl", "cli", "macro", "linter",
"loops", "bit"})
else
testall(arg)
end
Expand Down

0 comments on commit 0ad2691

Please sign in to comment.