Skip to content

Commit

Permalink
Improve bitwise docs
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed May 23, 2024
1 parent d333a66 commit 4e9571d
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions lib/orb/numeric/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ defmodule Orb.Numeric.DSL do
end
end

@doc """
Bitwise OR operation for i32 or i64 integers.
## Examples
use Orb
defw bitwise_or_example(a: I32, b: I32), I32 do
a ||| b
end
"""
def left ||| right do
case Ops.extract_common_type(left, right) do
type when Ops.is_primitive_integer_type(type) ->
Expand All @@ -146,6 +158,18 @@ defmodule Orb.Numeric.DSL do
end
end

@doc """
Bitwise AND operation for i32 or i64 integers.
## Examples
use Orb
defw bitwise_and_example(a: I32, b: I32), I32 do
a &&& b
end
"""
def left &&& right do
case Ops.extract_common_type(left, right) do
type when Ops.is_primitive_integer_type(type) ->
Expand All @@ -156,6 +180,18 @@ defmodule Orb.Numeric.DSL do
end
end

@doc """
Left shift operation for i32 or i64 integers.
## Examples
use Orb
defw left_shift_by_4(a: I32), I32 do
a <<< 4
end
"""
def left <<< right do
case Ops.extract_common_type(left, right) do
type when Ops.is_primitive_integer_type(type) ->
Expand All @@ -166,24 +202,81 @@ defmodule Orb.Numeric.DSL do
end
end

@doc """
Logical NOT operation for i32 or i64 integers. Returns i32.
Zero is considered false, all other numbers are considered true.
It does not support short-circuiting.
## Examples
use Orb
defw not_example(a: I32), I32 do
not a
end
"""
def not value do
case Ops.typeof(value, :primitive) do
type when Ops.is_primitive_integer_type(type) ->
Orb.Instruction.new(type, :eqz, [value])

type when Ops.is_primitive_float_type(type) ->
raise ArgumentError, "Cannot NOT two expressions of type #{type}."
end
end

@doc """
Logical OR operation for i32 or i64 integers. Returns i32.
Zero is considered false, all other numbers are considered true.
It does not support short-circuiting.
## Examples
use Orb
defw or_example(a: I32, b: I32), I32 do
a or b
end
"""
def left or right do
case Ops.extract_common_type(left, right) do
type when Ops.is_primitive_integer_type(type) ->
Orb.Instruction.new(type, :or, [left, right])

type when Ops.is_primitive_float_type(type) ->
raise ArgumentError, "Cannot OR two expressions of type #{type}."
end
end

@doc """
Logical AND operation for i32 or i64 integers. Returns i32.
Zero is considered false, all other numbers are considered true.
It does not support short-circuiting.
## Examples
use Orb
defw and_example(a: I32, b: I32), I32 do
a and b
end
"""
def left and right do
case Ops.extract_common_type(left, right) do
type when Ops.is_primitive_integer_type(type) ->
Orb.Instruction.new(type, :and, [left, right])

type when Ops.is_primitive_float_type(type) ->
raise ArgumentError, "Cannot AND two expressions of type #{type}."
end
end
end

0 comments on commit 4e9571d

Please sign in to comment.