wslでelixir その144

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題やってみた。

# 練習問題
Livebookで、俺言語のfizzbuzzを実行するインタープリターを書け。

# サンプルコード

```
defmodule Sim2 do
	def start_link do
		Agent.start_link(fn ->
			[a: 0, b: 0, c: 0, pc: 0]
		end, name: __MODULE__)
	end
  def a(s) do
		Agent.update(__MODULE__, fn v ->
	    ex = "[" <> s <> "]"
			{[d], _} = Code.eval_string(ex, v)
			v = List.keyreplace(v, :a, 0, {:a, d})
			List.keyreplace(v, :pc, 0, {:pc, 777})
		end)
  end
  def b(s) do
		Agent.update(__MODULE__, fn v ->
	    ex = "[" <> s <> "]"
			{[d], _} = Code.eval_string(ex, v)
			v = List.keyreplace(v, :b, 0, {:b, d})
			List.keyreplace(v, :pc, 0, {:pc, 777})
		end)
  end
  def print(s) do
		Agent.update(__MODULE__, fn v ->
	    ex = "[" <> s <> "]"
			{[d], _} = Code.eval_string(ex, v)
			IO.write(d)
      IO.write(' ')
			List.keyreplace(v, :pc, 0, {:pc, 777})
		end)
  end
 	def r() do
		IO.puts("")
	end
  def loop(s) do
		Agent.update(__MODULE__, fn v ->
		    if String.match?(s, ~r/\*/) do
	        s = String.replace(s, "@", "==")
	        p = String.split(s, "*")
			    g = Enum.at(p, 0)
			    ex = "[" <> g <> "]"
    			{[d], _} = Code.eval_string(ex, v)
    			if d == true do
    			    x = String.to_integer(Enum.at(p, 1))
    			    List.keyreplace(v, :pc, 0, {:pc, x})
    			else
    			    List.keyreplace(v, :pc, 0, {:pc, 777})
    			end
		    else
    			x = String.to_integer(s)
			    List.keyreplace(v, :pc, 0, {:pc, x})
		    end
		end)
  end
	def pc() do
		Agent.get(__MODULE__, fn v ->
        v[:pc]
    end)
  end
  def test() do
		Agent.get(__MODULE__, fn v ->
         v
    end)
  end
end

defmodule Main2 do
    def src(n) do
        str = """
10 a=0
20 a=a+1
30 #=a>100*0
40 b=rem(a,15)
50 #=b@0*120
60 b=rem(a,5)
70 #=b@0*150
80 b=rem(a,3)
90 #=b@0*170
100 ?=a
110 #=20
120 ?="fizzbuzz"
130 r
140 #=20
150 ?="buzz"
160 #=20
170 ?="fizz"
180 #=20
"""
      Enum.map(String.split(str, "\n", trim: true), fn l ->
		    s = String.split(l, " ")
		    cond do
	  		String.to_integer(Enum.at(s, 0)) == n ->
		  		l
			  true ->
				  nil
			  end
	    end)
    end
    def run(0) do
    end
    def run(pc) do
        m = src(pc)
        m = Enum.reject(m, fn(x) -> 
	        x == nil 
        end)
        Enum.map(m, fn l ->
          b = String.replace(l, "=", " ")
          s = String.split(b, " ")
	        cond do
		      Enum.at(s, 1) == "?" ->
	    	    Sim2.print(Enum.at(s, 2))
		      Enum.at(s, 1) == "#" ->
		   	    Sim2.loop(Enum.at(s, 2))
		      Enum.at(s, 1) == "a" ->
			      Sim2.a(Enum.at(s, 2))
		      Enum.at(s, 1) == "b" ->
			      Sim2.b(Enum.at(s, 2))
          Enum.at(s, 1) == "r" ->
		        Sim2.r()   
		      true ->
			      IO.puts ""
		      end
		      cond do
		      Sim2.pc == 777 ->
		        run(pc + 10)
		      true ->
		        run(Sim2.pc)
		      end
    	end)
	end
end

Sim2.start_link
Main2.run(10)

```

# 

```
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 
16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 
31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 
46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 
61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 
76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 
91 92 fizz 94 buzz fizz 97 98 fizz buzz 
```

以上。