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