Intcode assembler: make immediate mode the default

This commit is contained in:
Shadowfacts 2019-12-06 10:49:25 -05:00
parent 36ea4a177d
commit b933c30bea
1 changed files with 21 additions and 14 deletions

View File

@ -1,22 +1,23 @@
defmodule Assembler do defmodule Assembler do
@asm """ @asm """
in #res in res
clt res #8 #31 clt $res 8 cmpRes
jnz 31 #lessThan jnz $cmpRes lessThan
ceq res #8 #31 ceq $res 8 cmpRes
jnz 31 #equal jnz $cmpRes equal
out #1001 out 1001
hlt hlt
lessThan: lessThan:
out #999 out 999
hlt hlt
equal: equal:
out #1000 out 1000
hlt hlt
res: 0 res: 0
cmpRes: 0
""" """
def test do def test do
@asm @asm
@ -83,19 +84,25 @@ defmodule Assembler do
|> Enum.with_index() |> Enum.with_index()
|> Enum.map_reduce(0, fn {{param, type}, index}, modes -> |> Enum.map_reduce(0, fn {{param, type}, index}, modes ->
val = val =
case Regex.run(~r/^#?(\d+)$/, param) do case Regex.run(~r/^\$?(\d+)$/, param) do
[_, digits] -> [_, digits] ->
String.to_integer(digits) String.to_integer(digits)
_ -> _ ->
[_, name] = Regex.run(~r/^#?(\w+)$/, param) [_, name] = Regex.run(~r/^\$?(\w+)$/, param)
{:label, name} {:label, name}
end end
modes = modes =
if type == :read && String.starts_with?(param, "#") do case type do
modes + pow(10, index + 2) :read ->
if String.starts_with?(param, "$") do
modes
else else
modes + pow(10, index + 2)
end
_ ->
modes modes
end end