Skip to content

Commit

Permalink
An idea for an alternative way to declare locals
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Dec 18, 2023
1 parent b21077e commit 37c8d4d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
12 changes: 11 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Used by "mix format"

locals_without_parens = [
i32: 1,
let: 1
]

[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
locals_without_parens: locals_without_parens,
export: [
locals_without_parens: locals_without_parens
]
]
31 changes: 21 additions & 10 deletions lib/orb/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ defmodule Orb.DSL do
# block = Macro.expand_once(block, __ENV__)

block_items =
case block do
{:__block__, _meta, block_items} -> block_items
single -> [single]
end
List.wrap(
case block do
{:__block__, _, items} -> items
single -> single
end
)

block_items = Macro.expand(block_items, env)
block_items = do_snippet(locals, block_items)
Expand All @@ -178,13 +180,12 @@ defmodule Orb.DSL do
block_items = unquote(block_items)

body_local_types =
for %{locals: statement_locals} <- block_items do
statement_locals |> IO.inspect(label: "statement_locals")
end
for(%{locals: locals} <- block_items, do: locals)
|> List.flatten()

# |> Keyword.new()
local_types = unquote(local_types) ++ body_local_types
local_types =
(unquote(local_types) ++ body_local_types)
|> Keyword.new()

%Orb.Func{
name:
Expand All @@ -195,7 +196,7 @@ defmodule Orb.DSL do
params: unquote(params),
result: unquote(result_type),
local_types: local_types,
body: block_items |> Orb.InstructionSequence.new(),
body: Orb.InstructionSequence.new(block_items),
exported_names: unquote(exported_names)
}
|> Orb.Func.narrow_if_needed()
Expand Down Expand Up @@ -320,6 +321,16 @@ defmodule Orb.DSL do
# TODO: should this be removed?
def i32(op) when op in Ops.i32(:all), do: {:i32, op}

def i32(locals) when is_list(locals) do
Orb.InstructionSequence.new(
:local_effect,
for {local, initial_value} when initial_value !== 0 <- locals do
Orb.Instruction.local_set(Orb.I32, local, initial_value)
end,
locals: for({local, _} <- locals, do: {local, Orb.I32})
)
end

def i64(n) when is_integer(n), do: Instruction.i64(:const, n)

@doc """
Expand Down
9 changes: 8 additions & 1 deletion test/loop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ defmodule LoopTest do
use Orb

defw sum1to10(), I32, sum: I32 do
# sum = 0
# let sum: i32(0)
# i32 sum: 0
# I32.def sum: 0
# sum :: I32 = 0
# f32(r: 0.0)

loop i <- 1..10 do
sum = sum + i
end
Expand Down Expand Up @@ -98,7 +105,7 @@ defmodule LoopTest do
)
"""

assert wasm_source = to_wat(Loop1To10)
assert wasm_source == to_wat(Loop1To10)
assert 55 = Wasm.call(Loop1To10, :sum1to10)
end
end

0 comments on commit 37c8d4d

Please sign in to comment.