Last active
June 24, 2019 04:21
-
-
Save edvakf/9afd138fec7167627eba8101e2090347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#control flows | |
#br inside block -> forward jump | |
#br inside loop -> backward jump | |
#if | |
ret = nil | |
catch(:a) do | |
if condition | |
ret = ... | |
throw :a # br 0 | |
else | |
end | |
end | |
# または、breakできるifを定義 | |
def _if(condition, &block) | |
if condition | |
yield block | |
end | |
end | |
ret = nil | |
_if condition do | |
ret = ... | |
break # br 0 | |
end | |
# あるいは | |
ret,level = | |
_if condition do | |
break 返り値,0 # br 0 | |
end | |
# break ret1,level-1 if level != 0 # トップレベルでは書かない | |
# ここで気づいたが、_ifだとelseがでてきたときに面倒そう | |
# 面倒だし、普通にifを使えば良さそう。ifの中のbr 0はサポートしない | |
#block | |
ret = nil | |
catch(:a) do | |
ret = ... | |
next # br 0 | |
ret = ... | |
end | |
# または | |
ret = | |
while true | |
break 返り値 # br 0 | |
break 返り値 # end | |
end | |
#loop | |
while true | |
ret = ... | |
next # br 0 | |
ret = ... | |
break | |
end | |
# または | |
ret = | |
while true | |
next # br 0 | |
break 返り値 # end | |
end | |
#block loop (advanced) | |
ret = nil | |
while true | |
ret = ... | |
break # br 1 | |
ret = ... | |
next # br 0 | |
end | |
# または | |
ret = | |
while true | |
break 返り値 # br 1 | |
next # br 0 | |
break 返り値 # end | |
end | |
# または | |
ret0 = | |
while true | |
ret1,level = | |
while true | |
next # br 0 | |
break 返り値,1 # br 1 | |
break 返り値,0 # end | |
end | |
break ret1,level-1 if level != 0 | |
end | |
#block if | |
ret0 = nil | |
catch(:b) do | |
ret1 = nil | |
catch(:a) do | |
if condition | |
ret1 = ... | |
throw :b # br 0 | |
ret1 = ... | |
throw :b # br 1 | |
else | |
end | |
ret1 = ... | |
end | |
ret0 = ... | |
end | |
# または | |
ret0,level = | |
while true | |
ret1,level = | |
_if condition do | |
break 返り値,0 # br 0 | |
break 返り値,1 # br 1 | |
end | |
break ret1,level-1 if level != 0 | |
break 返り値,0 # end | |
end | |
# next ret1,level-1 if level != 0 # トップレベルでは書かない | |
# または | |
ret0,level = | |
while true | |
ret1,level = | |
if condition do | |
break ret0の返り値,1 # br 1 (br 0はサポートしない) | |
end | |
break ret1,level-1 if level != 0 | |
end | |
#loop if | |
ret0 = nil | |
while true | |
ret0 = ... | |
next # br 0 | |
ret1 = nil | |
catch(:a) do | |
if condition | |
ret0 = ... | |
next # br 1 | |
ret1 = ... | |
throw :a # br 0 | |
else | |
end | |
end | |
ret = ... | |
break | |
end | |
# または | |
ret0,level = | |
while true | |
next # br 0 | |
ret1,level = | |
_if condition do | |
break 返り値,0 # br 0 | |
break 返り値,1 # br 1 | |
end | |
next ret1,level-1 if level != 0 | |
break 返り値,0 # end | |
end | |
# break ret1,level-1 if level != 0 # トップレベルでは書かない |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Controls | |
def _if(condition, then_proc, &else_block) | |
if condition | |
then_proc.call | |
elsif else_block | |
yield else_block | |
else | |
-1 | |
end | |
end | |
def _loop(&block) | |
while true | |
depth = yield block | |
next if depth == 0 | |
return depth | |
end | |
end | |
def _block(&block) | |
yield block | |
end | |
def test1 | |
a = 1 | |
depth = _block{ | |
depth = _loop{ | |
depth = _if(a == 0, ->{ | |
a = 2 | |
next 1 # continue | |
-1 # end | |
}){ # else | |
a = 3 | |
-1 # end | |
} | |
next depth - 1 if depth > 0 | |
-1 # end | |
} | |
next depth - 1 if depth > 0 | |
-1 # end | |
} | |
# assert(depth = 0) | |
#p a | |
a | |
end | |
def sum(p0) | |
#v0 = 0 | |
#l0 = v0 | |
sum = 0 | |
#v1 = 1 | |
#l1 = v1 | |
i = 1 | |
depth = _block{ | |
depth = _loop{ | |
#v2 = l1 | |
#v3 = p0 | |
#v4 = v2 > v3 | |
#if v4 | |
if i > p0 | |
#break 1 # br_if | |
next 1 | |
end | |
#v5 = l0 | |
#v6 = l1 | |
#v7 = v5 + v6 | |
#l0 = v7 | |
sum = sum + i | |
#v8 = l1 | |
#v9 = 1 | |
#v10 = v8 + v9 | |
#l1 = v10 | |
i = i + 1 | |
p 'hoge' | |
#break 0 # continue | |
next 0 | |
-1 | |
} | |
p 'fuga' | |
next depth - 1 if depth > 0 | |
-1 | |
} | |
#v11 = l0 | |
#v11 | |
sum | |
end | |
def fact(p0) | |
v0 = p0 | |
v1 = 0 | |
v2 = v0 == v1 | |
v3 = nil | |
depth = _if(v2, ->{ | |
v4 = 1 | |
v3 = v4 | |
-1 | |
}){ # else | |
v5 = p0 | |
v6 = p0 | |
v7 = 1 | |
v8 = v6 - v7 | |
v9 = fact(v8) | |
v10 = v5 * v9 | |
-1 | |
} | |
v10 | |
end | |
def test02() | |
v0 = nil | |
depth = _block{ | |
v1 = 1 | |
v2 = 0 | |
v3 = v2 == 0 | |
if v3 | |
v0 = v1 | |
next 0 | |
end | |
v0 = v0 | |
-1 | |
} | |
end | |
end | |
c = Controls.new | |
#1_000.times do | |
#c.test1 | |
#end | |
#p c.sum(10) | |
p c.fact(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment