概要
robloxでassistantやってみた。
練習問題やってみた。
練習問題
スクリプトだけで、インタープリタを書け。
手順
- ReplicatedFirstに、LocalScriptを追加。
- スクリプトを書く。
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local gui = Instance.new("ScreenGui")
gui.Parent = PlayerGui
local NewTextBox = Instance.new("TextBox")
NewTextBox.Parent = gui
NewTextBox.Size = UDim2.new(0.2, 0, 0.6, 0)
NewTextBox.Position = UDim2.new(0.2, 0, 0.1, 0)
NewTextBox.BackgroundColor3 = Color3.fromRGB(200, 200, 200)
NewTextBox.TextColor3 = Color3.fromRGB(255, 255, 255)
NewTextBox.Font = "Arial"
NewTextBox.TextWrapped = true
NewTextBox.TextXAlignment = Enum.TextXAlignment.Left
NewTextBox.TextSize = 20
NewTextBox.ClearTextOnFocus = false
NewTextBox.MultiLine = true
NewTextBox.TextEditable = true
NewTextBox.BorderSizePixel = 0
NewTextBox.Text = "0\nset_a\n:loop\n1\nget_a\n+\ndup\nset_a\nout\n10\nget_a\n>\nif :loop\nend\n"
NewTextBox.PlaceholderText = "Create Box"
NewTextBox.PlaceholderColor3 = Color3.fromRGB(255, 255, 255)
NewTextBox.Active = true
local button = Instance.new("TextButton")
button.Name = "button"
button.BorderSizePixel = 0
button.TextSize = 20
button.TextColor3 = Color3.new(1, 0.2, 0.4)
button.Size = UDim2.new(0.1, 0, 0.1, 0)
button.Position = UDim2.new(0.5, 0, 0.8, 0)
button.SizeConstraint = Enum.SizeConstraint.RelativeYY
button.Text = "run"
button.Parent = gui
button.Active = true
button.MouseButton1Down:Connect(function()
local src = NewTextBox.Text
--print(src)
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
--print(l)
s += 1
table.insert(h, l)
end
local pc = 1
local j
repeat
l = h[pc]
--print(pc)
if l == nil then
pc = 200
elseif string.sub(l, 1, 1) == ":" then
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
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 == "end" then
pc = 200
elseif string.sub(l, 1, 2) == "if" then
local p = string.sub(l, 4, 8)
t1 = tonumber(stack.pop())
if t1 == 1 then
for j = 1, s do
if h[j] == p then
pc = j
--print(pc)
end
end
--pc = 3
else
pc += 1
end
else
stack.push(l)
pc += 1
end
until pc > 100
print("ok")
end)
写真
以上。