2
0

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.

概要

paiza.ioでelixirやってみた。
俺言語のインタープリタ、書いてみた。
練習問題やってみた。

練習問題

fizzbuzzを書け。

投入したソースコード

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*140
80 b=rem(a,3)
90 #=b@0*160
100 ?=a
110 #=20
120 ?="fizzbuzz"
130 #=20
140 ?="buzz"
150 #=20
160 ?="fizz"
170 #=20

サンプルコード

defmodule Sim 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.puts(d)
			List.keyreplace(v, :pc, 0, {:pc, 777})
		end)
    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 Main 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*140
80 b=rem(a,3)
90 #=b@0*160
100 ?=a
110 #=20
120 ?="fizzbuzz"
130 #=20
140 ?="buzz"
150 #=20
160 ?="fizz"
170 #=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 ->
            #IO.puts(l)
            b = String.replace(l, "=", " ")
            s = String.split(b, " ")
	        cond do
		    Enum.at(s, 1) == "?" ->
	    	    Sim.print(Enum.at(s, 2))
		    Enum.at(s, 1) == "#" ->
		   	    Sim.loop(Enum.at(s, 2))
		    Enum.at(s, 1) == "a" ->
			    Sim.a(Enum.at(s, 2))
		    Enum.at(s, 1) == "b" ->
			    Sim.b(Enum.at(s, 2))
		    true ->
			    IO.puts ""
		    end
        	#IO.inspect(Sim.test)
		    cond do
		    Sim.pc == 777 ->
		        run(pc + 10)
		    true ->
		        run(Sim.pc)
		    end
    	end)
	end
end

Sim.start_link
Main.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

成果物

以上。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?