1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslでelixir その145

Last updated at Posted at 2024-03-12

概要

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

練習問題

Livebookで、俺言語のzundokoを実行するインタープリターを書け。

サンプルコード



defmodule Main3 do
    def src(n) do
        str = """
10 a=0
20 b=:rand.uniform(2)
30 #=b<2*80
40 ?="zun"
50 a=a+1
60 #=a>3*110
70 #=20
80 ?="doko"
90 a=0
100 #=20
110 ?="doko"
120 ?="kiyosi!!"
130 #=0
"""
      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))
		      true ->
			      IO.puts ""
		      end
		      cond do
		      Sim2.pc == 777 ->
		        run(pc + 10)
		      true ->
		        run(Sim2.pc)
		      end
    	end)
	end
end

Sim2.start_link
Main3.run(10)

実行結果

doko zun doko zun doko zun doko doko zun zun zun doko doko zun doko doko zun zun zun zun doko kiyosi!! 

以上。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?