paiza.ioでelixir その205

# 概要
paiza.ioでelixirやってみた。
練習問題やってみた。

# 練習問題
アセンブラを書け。
九九をコンパイルせよ。

# サンプルコード

```
defmodule Va do
	use Agent
	def start_link() do
		Agent.start_link(fn ->
			0
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def set(x) do
		Agent.update(__MODULE__, fn v ->
			x
		end)
	end
end
defmodule Src do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
            ""
		end, name: __MODULE__)
	end
	def set(x) do
		Agent.update(__MODULE__, fn v ->
	        x
		end)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def getn(n) do
		Agent.get(__MODULE__, fn v ->
	    	Enum.at(String.split(v, "\n", trim: true), n - 1)
	    end)
    end
    def getl("end") do
        0
    end
	def getl(a) do
		Agent.get(__MODULE__, fn v ->
            Enum.reduce(String.split(v, "\n", trim: true), 1, fn l, n ->
	    	    if String.starts_with?(l, a) do
	    	        Va.set(n)
	    	    end
	    	    n = n + 1
	        end)
	        Va.get - 1
	    end)
    end
end
defmodule Main do
    def val(nil) do
        0
    end
    def val(s) do
        if String.starts_with?(s, "'") do
            String.at(s, 1)
            |> String.to_charlist
            |> List.first
        else
            String.to_integer(s)
        end
    end
    def add(n) do
	    IO.puts "rom[#{n}] <= 12'hf_00;"
	end
	def sub(n) do
	    IO.puts "rom[#{n}] <= 12'hf_01;"
	end
	def mul(n) do
	    IO.puts "rom[#{n}] <= 12'hf_02;"
	end
	def div(n) do
	    IO.puts "rom[#{n}] <= 12'hf_03;"
	end
	def rem(n) do
	    IO.puts "rom[#{n}] <= 12'hf_03;"
	end
	def eq(n) do
	    IO.puts "rom[#{n}] <= 12'hf_03;"
	end
	def rnd(n) do
	    IO.puts "rom[#{n}] <= 12'hf_07;"
	end
	def dup(n) do
	    IO.puts "rom[#{n}] <= 12'h8_00;"
	end
	def drop(n) do
	    IO.puts "rom[#{n}] <= 12'h9_00;"
	end
	def gt(n) do
	    IO.puts "rom[#{n}] <= 12'hf_06;"
	end
	def out3(n) do
	    IO.puts "rom[#{n}] <= 12'h5_03;"
	end
	def out2(n) do
	    IO.puts "rom[#{n}] <= 12'h5_02;"
	end
	def out1(n) do
	    IO.puts "rom[#{n}] <= 12'h5_01;"
	end
	def out(n) do
	    IO.puts "rom[#{n}] <= 12'h5_00;"
	end
	def push(n, s) do
	    i = val(s)
	    j = Integer.floor_div(i, 16)
	    i = rem(i, 16)
	    i = Integer.to_string(i, 16)
	    j = Integer.to_string(j, 16)
	    IO.puts "rom[#{n}] <= 12'h2_#{j}#{i};"
	end	
	def if_(n, s) do
	    i = Src.getl(s)
	    j = Integer.floor_div(i, 16)
	    i = rem(i, 16)
	    i = Integer.to_string(i, 16)
	    j = Integer.to_string(j, 16)
	    IO.puts "rom[#{n}] <= 12'h6_#{j}#{i};"
	end
	def jmp(n, s) do
	    i = Src.getl(s)
	    j = Integer.floor_div(i, 16)
	    i = rem(i, 16)
	    i = Integer.to_string(i, 16)
	    j = Integer.to_string(j, 16)
	    IO.puts "rom[#{n}] <= 12'h1_#{j}#{i};"
	end
	def set(n, s) do
	    cond do
	    s == "a" ->
	        IO.puts "rom[#{n}] <= 12'h4_00;"
	    s == "b" ->
	        IO.puts "rom[#{n}] <= 12'h4_01;"
	    s == "c" ->
	        IO.puts "rom[#{n}] <= 12'h4_02;"
	    end
	end
	def get(n, s) do
	    cond do
	    s == "a" ->
	        IO.puts "rom[#{n}] <= 12'h3_00;"
	    s == "b" ->
	        IO.puts "rom[#{n}] <= 12'h3_01;"
	    s == "c" ->
	        IO.puts "rom[#{n}] <= 12'h3_02;"
	    end
	end
    def asm(str) do
        Enum.reduce(String.split(str, "\n"), 0, fn st, n ->
            IO.puts "//#{st}"
            s = String.split(st, "  ")
            cond do
	        Enum.at(s, 1) == "+" ->
		        add(n)
    	    Enum.at(s, 1) == "-" ->
	    	    sub(n)
	        Enum.at(s, 1) == "*" ->
		        mul(n)
    	    Enum.at(s, 1) == "/" ->
	    	    div(n)
    	    Enum.at(s, 1) == "%" ->
	    	    rem(n)
    	    Enum.at(s, 1) == "=" ->
	    	    eq(n)
    	    Enum.at(s, 1) == "rnd" ->
	    	    rnd(n)
    	    Enum.at(s, 1) == "out3" ->
	    	    out3(n)
    	    Enum.at(s, 1) == "out2" ->
	    	    out2(n)
    	    Enum.at(s, 1) == "out1" ->
	    	    out1(n)
    	    Enum.at(s, 1) == "out" ->
	    	    out(n)
	        Enum.at(s, 1) == "dup" ->
		        dup(n)
    	    Enum.at(s, 1) == "drop" ->
	    	    drop(n)
	        Enum.at(s, 1) == ">" ->
		        gt(n)
	        Enum.at(s, 1) == "jmp" ->
		        jmp(n, Enum.at(s, 2))
	        Enum.at(s, 1) == "if" ->
		        if_(n, Enum.at(s, 2))
	        Enum.at(s, 1) == "set" ->
		        set(n, Enum.at(s, 2))
    	    Enum.at(s, 1) == "get" ->
	    	    get(n, Enum.at(s, 2))
    	    true ->
                push(n, Enum.at(s, 1))
            end
            n = n + 1
        end)
    end
    def start(str) do
        Src.set(str)
        asm(str)
    end
end

Va.start_link
Src.start_link

str = """
  1
  set  a
  1
  set  b
loop  get  a
  get  b
  *
  out3
  out2
  out1
  drop
  ' '
  out
  get  a
  1
  +
  set  a
  10
  get  a
  >
  if  loop
  10
  out
  get  b
  1
  +
  set  b
  1
  set  a
  10
  get  b
  >
  if  loop
lup  jmp  lup
"""
Main.start(str)


```

# 実行結果

```
//  1
rom[0] <= 12'h2_01;
//  set  a
rom[1] <= 12'h4_00;
//  1
rom[2] <= 12'h2_01;
//  set  b
rom[3] <= 12'h4_01;
//loop  get  a
rom[4] <= 12'h3_00;
//  get  b
rom[5] <= 12'h3_01;
//  *
rom[6] <= 12'hf_02;
//  out3
rom[7] <= 12'h5_03;
//  out2
rom[8] <= 12'h5_02;
//  out1
rom[9] <= 12'h5_01;
//  drop
rom[10] <= 12'h9_00;
//  ' '
rom[11] <= 12'h2_20;
//  out
rom[12] <= 12'h5_00;
//  get  a
rom[13] <= 12'h3_00;
//  1
rom[14] <= 12'h2_01;
//  +
rom[15] <= 12'hf_00;
//  set  a
rom[16] <= 12'h4_00;
//  10
rom[17] <= 12'h2_0A;
//  get  a
rom[18] <= 12'h3_00;
//  >
rom[19] <= 12'hf_06;
//  if  loop
rom[20] <= 12'h6_04;
//  10
rom[21] <= 12'h2_0A;
//  out
rom[22] <= 12'h5_00;
//  get  b
rom[23] <= 12'h3_01;
//  1
rom[24] <= 12'h2_01;
//  +
rom[25] <= 12'hf_00;
//  set  b
rom[26] <= 12'h4_01;
//  1
rom[27] <= 12'h2_01;
//  set  a
rom[28] <= 12'h4_00;
//  10
rom[29] <= 12'h2_0A;
//  get  b
rom[30] <= 12'h3_01;
//  >
rom[31] <= 12'hf_06;
//  if  loop
rom[32] <= 12'h6_04;
//lup  jmp  lup
rom[33] <= 12'h1_21;
//
rom[34] <= 12'h2_00;

```

# 成果物

https://paiza.io/projects/HvTAu601rXIRxjhVOU9Dtw

以上。