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

robloxでassistant その29

Posted at

概要

robloxでassistantやってみた。
練習問題やってみた。

練習問題

インタープリタを書け。
zundokoを表示せよ。

方針

  • 俺cpuアセンブラで書く。
  • stack machine
  • zundoko表示
0
set_a
:loop
rnd
1
>
if :skip1
;zun
out
get_a
1
+
set_a
get_a
3
>
if :skip2
jp :loop
:skip1
;doko
out
0
set_a
jp :loop
:skip2
;doko ki yo si !
out
end

rndは、乱数。
;fizzbuzzは、stackに積む。
%は、剰余
jp :loopは、:loopに飛ぶ。
set_aは、レジスタaに、セットする。
get_aは、レジスタaから読み込む。
:loopは、ラベル
if :loopは、真なら、ラベル:loopに飛ぶ。
dupは、コピー、複製を作る。
outは、数値表示。
数値は、stackに積む。
+-/*は、加減乗除。
endは、終了。

サンプルコード


local player = game.Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local gui = PlayerGui:WaitForChild("ScreenGui")


gui.TextButton.MouseButton1Click:Connect(function()
	local src = gui.TextBox.Text
	local stack = {}
	function stack.push(item)
		table.insert(stack, item) 
	end
	function stack.pop()
		return table.remove(stack)
	end
	local l
	local t1
	local t2
	local v
	local a = 0
	local s = 0
	local h = {}
	for l in string.gmatch(src, "[^\n]+") do 
		s += 1
		table.insert(h, l)
	end
	local pc = 1
	local j
	repeat
		l = h[pc]
		if string.sub(l, 1, 1) == ";" then
			stack.push(string.sub(l, 2, 16))
			pc += 1
		elseif string.sub(l, 1, 1) == ":" then
			pc += 1
		elseif l == "rnd" then
			v = math.random(1, 2)
			stack.push(v)
			pc += 1
		elseif l == "dup" then
			t1 = stack.pop()
			stack.push(t1)
			stack.push(t1)
			pc += 1
		elseif l == "drop" then
			t1 = stack.pop()
			pc += 1
		elseif l == "set_a" then
			t1 = stack.pop()
			a = t1
			pc += 1
		elseif l == "get_a" then
			stack.push(a)
			pc += 1
		elseif l == "out" then
			t1 = stack.pop()
			print(t1)
			pc += 1
		elseif l == "%" then
			t1 = stack.pop()
			t2 = stack.pop()
			v = t2 % t1
			--print(v)
			stack.push(v)
			pc += 1
		elseif l == "+" then
			t1 = stack.pop()
			t2 = stack.pop()
			v = t2 + t1
			stack.push(v)
			pc += 1
		elseif l == "-" then
			t1 = stack.pop()
			t2 = stack.pop()
			v = t2 - t1
			stack.push(v)
			pc += 1
		elseif l == "/" then
			t1 = stack.pop()
			t2 = stack.pop()
			v = t2 / t1
			stack.push(v)
			pc += 1
		elseif l == "*" then
			t1 = stack.pop()
			t2 = stack.pop()
			v = t2 * t1
			stack.push(v)
			pc += 1
		elseif l == ">" then
			t1 = tonumber(stack.pop())
			t2 = tonumber(stack.pop())
			if t2 > t1 then
				stack.push(1)
			else
				stack.push(0)
			end
			pc += 1
		elseif l == "<" then
			t1 = tonumber(stack.pop())
			t2 = tonumber(stack.pop())
			if t2 < t1 then
				stack.push(1)
			else
				stack.push(0)
			end
			pc += 1
		elseif l == "end" then
			pc = 300
		elseif string.sub(l, 1, 2) == "if" then
			local p = string.sub(l, 4, 9)
			t1 = stack.pop()
			t1 = tonumber(t1)
			if t1 > 0 then
				for j = 1, s do
					if h[j] == p then
						pc = j
						--print(pc)
					end
				end
			else
				pc += 1
			end			
		elseif string.sub(l, 1, 2) == "jp" then
			local p = string.sub(l, 4, 9)
			for j = 1, s do
				if h[j] == p then
					pc = j
				end
			end
		else
			stack.push(l)
			pc += 1
		end
	until pc > 200
	print("ok")
end)





実行結果


  10:49:52.640  zun  -  クライアント - LocalScript:57
  10:49:52.641   ▶ doko (x2)  -  クライアント - LocalScript:57
  10:49:52.641  zun  -  クライアント - LocalScript:57
  10:49:52.641  doko  -  クライアント - LocalScript:57
  10:49:52.641   ▶ zun (x3)  -  クライアント - LocalScript:57
  10:49:52.642  doko  -  クライアント - LocalScript:57
  10:49:52.642  zun  -  クライアント - LocalScript:57
  10:49:52.642  doko  -  クライアント - LocalScript:57
  10:49:52.643  zun  -  クライアント - LocalScript:57
  10:49:52.643   ▶ doko (x6)  -  クライアント - LocalScript:57
  10:49:52.644   ▶ zun (x3)  -  クライアント - LocalScript:57
  10:49:52.645  doko  -  クライアント - LocalScript:57
  10:49:52.645   ▶ zun (x2)  -  クライアント - LocalScript:57
  10:49:52.645  doko  -  クライアント - LocalScript:57
  10:49:52.645  zun  -  クライアント - LocalScript:57
  10:49:52.646  doko  -  クライアント - LocalScript:57
  10:49:52.646   ▶ zun (x4)  -  クライアント - LocalScript:57
  10:49:52.647  doko ki yo si !  -  クライアント - LocalScript:57
  10:49:52.647  ok  -  クライアント - LocalScript:136

以上。

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