racc で xvcd を生成することを試し始めた (その4)
if ... elseif ... else end
の構文もサポートしたものを以下においた。
http://homepage2.nifty.com/youichi_kato/src.html
- racc で xvcd
zzz-2007-06-07.tgz(2007-06-07)
javascript 風言語から xfy/xvcd のコードを生成する
走らせ方は racc が動作する状態で、 $ make とすればよい。
このソースは http://ruby256.hp.infoseek.co.jp/ から 魔道編のサンプルプログラム
として download したものをベースにして作成している。
コード全部をここに引用したかったが、ちょっと量がおおくなったので、tar+gzip したものの公開とする。
kato$ wc *
12 24 138 Makefile
28 55 444 c.intp
339 652 6030 node.rb
509 1515 11887 parser.rb
230 518 5219 parser.y
60 121 719 src
以下には入力ソースと出力結果を引用する。
kato$cat src
//==== assign ====
message = 'funcall ok'
message = true
message = false
message = message
message = foo(a,b,"x")
message = foo(boo())
message = foo(boo(a))
message = foo(boo(a), zoo(b))//==== function-call ====
puts( "abc" )
puts( func0() )
puts( a )//==== if then end =====
if "$a=''"
x = "ONE"
y = "TWO"
end//==== def func =====
def func0()
a="local"
puts('in func0()')
end
func0()def func1(a, b, c)
puts('in func1()')
end
func1("A", "B", "foo(a)")//==== if then else end =====
if "$a=''"
a="1"
else
a="2"
end//==== if then elseif end =====
if "a"
a="1"
elseif "b"
a="2"
elseif "c"
a="3"
else
a="4"
endif "a"
a="1"
elseif "b"
a="2"
else
a="3"
end# //==== End of File ====
出力結果
kato$make
ruby c.intp src && echo ok
<!-- ==== assign ==== -->
<instruction:variable name="message" select="'funcall ok'"/>
<instruction:variable name="message" select="'true'"/>
<instruction:variable name="message" select="'false'"/>
<instruction:variable name="message" select="$message"/>
<instruction:variable name="message" select="foo($a,$b,'x')"/>
<instruction:variable name="message" select="foo(boo())"/>
<instruction:variable name="message" select="foo(boo($a))"/>
<instruction:variable name="message" select="foo(boo($a),zoo($b))"/>
<!-- ==== function-call ==== -->
<instruction:message-box>
<xvcd:value-of select="'abc'"/>
</instruction:message-box>
<instruction:message-box>
<xvcd:value-of select="func0()"/>
</instruction:message-box>
<instruction:message-box>
<xvcd:value-of select="$a"/>
</instruction:message-box>
<!-- ==== if then end ===== -->
<instruction:if test="$a=''">
<instruction:variable name="x" select="'ONE'"/>
<instruction:variable name="y" select="'TWO'"/>
</instruction:if>
<!-- ==== def func ===== -->
<instruction:command name="func0">
<instruction:variable name="a" select="'local'"/>
<instruction:message-box>
<xvcd:value-of select="'in func0()'"/>
</instruction:message-box>
</instruction:command>
<instruction:call name="func0">
</instruction:call>
<instruction:command name="func1">
<instruction:with-param name="a"/>
<instruction:with-param name="b"/>
<instruction:with-param name="c"/>
<instruction:message-box>
<xvcd:value-of select="'in func1()'"/>
</instruction:message-box>
</instruction:command>
<instruction:call name="func1">
<instruction:with-param name="a" select="'A'"/>
<instruction:with-param name="b" select="'B'"/>
<instruction:with-param name="c" select="'foo(a)'"/>
</instruction:call>
<!-- ==== if then else end ===== -->
<instruction:choose>
<instruction:when test="$a=''">
<instruction:variable name="a" select="'1'"/>
</instruction:when>
<instruction:otherwise>
<instruction:variable name="a" select="'2'"/>
</instruction:otherwise>
</instruction:choose>
<!-- ==== if then elseif end ===== -->
<instruction:choose>
<instruction:when test="a">
<instruction:variable name="a" select="'1'"/>
</instruction:when>
<instruction:when test="44b">
<instruction:variable name="a" select="'2'"/>
</instruction:when>
<instruction:when test="46c">
<instruction:variable name="a" select="'3'"/>
</instruction:when>
<instruction:otherwise>
<instruction:variable name="a" select="'4'"/>
</instruction:otherwise>
</instruction:choose>
<instruction:choose>
<instruction:when test="a">
<instruction:variable name="a" select="'1'"/>
</instruction:when>
<instruction:when test="54b">
<instruction:variable name="a" select="'2'"/>
</instruction:when>
<instruction:otherwise>
<instruction:variable name="a" select="'3'"/>
</instruction:otherwise>
</instruction:choose>
ソースは、まだ ruby らしくない部分も多いし無駄もある。
それを解消すべく プログラミングRuby 第2版 言語編 という本も買った。
最近のコメント