概要
windows11に、sketchup6を入れてみた。
rubyで、3Dを書く。
練習問題やってみた。
練習問題
webdialogでForthインタープリタを実行せよ。
写真
サンプルコード
class Forth
$stack = []
def add
top1 = $stack.pop()
top2 = $stack.pop()
v = top2 + top1
$stack.push(v)
end
def sub
top1 = $stack.pop()
top2 = $stack.pop()
v = top2 - top1
$stack.push(v)
end
def mul
top1 = $stack.pop()
top2 = $stack.pop()
v = top2 * top1
$stack.push(v)
end
def div
top1 = $stack.pop()
top2 = $stack.pop()
v = top2 / top1
$stack.push(v)
end
def dup
top1 = $stack.pop()
$stack.push(top1)
$stack.push(top1)
end
def drop
top1 = $stack.pop()
end
def get
top1 = $stack.pop()
puts top1
end
def push(v)
$stack.push(Integer(v))
end
def run(str)
s = str.split(' ')
for i in s do
if i == "+"
add()
elsif i == "-"
sub()
elsif i == "*"
mul()
elsif i == "/"
div()
elsif i == "dup"
dup()
elsif i == "drop"
drop()
elsif i == "."
get()
else
push(i)
end
end
end
def run(str)
s = str.split(' ')
for i in s do
if i == "+"
add()
elsif i == "-"
sub()
elsif i == "*"
mul()
elsif i == "/"
div()
elsif i == "dup"
dup()
elsif i == "drop"
drop()
elsif i == "."
get()
else
push(i)
end
end
end
def eval(str)
res = ""
s = str.split(' ')
for i in s do
if i == "+"
add()
elsif i == "-"
sub()
elsif i == "*"
mul()
elsif i == "/"
div()
elsif i == "dup"
dup()
elsif i == "drop"
drop()
elsif i == "."
top1 = $stack.pop()
res = top1.to_s
else
push(i)
end
end
res
end
def initialize
#run("9 9 - 9 9 / .")# 1
#run("9 9 / 9 9 / + .")# 2
#run("9 9 + 9 + 9 / .")# 3
#run("9 9 9 + 9 / dup + .")# 4
#run("9 9 9 + 9 / dup + - .")# 5
#run("9 dup 9 + 9 + 9 / - .")# 6
#run("9 9 9 + 9 / - .")# 7
#run("9 9 9 drop 9 / - .")# 8
#run("9 9 - 9 * 9 + .")# 9
#run("9 9 / 9 dup 9 / + .")# 10
#run("9 9 9 + 9 / + .")# 11
#run("9 dup 9 9 + + 9 / + .")# 12
#run("9 9 9 + 9 / dup + + .")# 13
#run("9 dup 9 9 + 9 / dup + - + .")# 14
#run("9 dup dup 9 + 9 + 9 / - + .")# 15
end
end
#Forth.new
#f = Forth.new;puts f.eval "9 9 - 9 9 / ."
def testforth
wd = UI::WebDialog.new("Ruby ", true, 'Console', 200, 200)
wd.set_html <<-EOS
<html>
<head>
</head>
<body>
<textarea id="out" cols=60 rows=5></textarea>
<br>
<input type="text" id="src" size="58" value="" ><a href="skp:run">run</a>
</body>
</html>
EOS
wd.add_action_callback('run') do |d, a|
command = wd.get_element_value('src')
puts command
res = command + " => "
f = Forth.new
res += f.eval command
wd.execute_script("document.getElementById('out').value ='#{res}';")
end
wd.show
end
以上。