Skip to content

Commit

Permalink
Add new Orb.Instruction.Const
Browse files Browse the repository at this point in the history
Dedicated to const, which makes it easier to work with and transform. I’ll remove the old paths through Orb.Instruction next
  • Loading branch information
RoyalIcing committed Jun 29, 2024
1 parent 8a84ed6 commit f6acec2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/orb/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,8 @@ defmodule Orb.Constants do

defimpl Orb.ToWat do
def to_wat(%Orb.Constants.NulTerminatedString{memory_offset: memory_offset}, indent) do
[
indent,
"(i32.const ",
to_string(memory_offset),
")"
]
Orb.Instruction.Const.new(:i32, memory_offset)
|> Orb.ToWat.to_wat(indent)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/orb/instruction/const.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Orb.Instruction.Const do
@moduledoc false
defstruct [:push_type, :value]

def new(push_type, value) when push_type in ~w(i32 i64 f32 f64)a do
%__MODULE__{push_type: push_type, value: value}
end

defimpl Orb.ToWat do
def to_wat(%Orb.Instruction.Const{push_type: push_type, value: value}, indent) do
[
indent,
["(", Atom.to_string(push_type), ".const "],
to_string(value),
")"
]
end
end
end

0 comments on commit f6acec2

Please sign in to comment.