<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fleurer &#187; fsvm</title>
	<atom:link href="http://www.fleurer-lee.com/tag/fsvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fleurer-lee.com</link>
	<description>rage and love, story of my life.</description>
	<lastBuildDate>Thu, 02 Sep 2010 11:07:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fleurer’s Stack VM</title>
		<link>http://www.fleurer-lee.com/2010/02/03/fleurers-stack-vm/</link>
		<comments>http://www.fleurer-lee.com/2010/02/03/fleurers-stack-vm/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 05:09:18 +0000</pubDate>
		<dc:creator>fleurer</dc:creator>
				<category><![CDATA[备忘]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[fsvm]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://www.fleurer-lee.com/?p=645706</guid>
		<description><![CDATA[http://github.com/Fleurer/fsvm
尝试用C写的堆栈机，好像烂尾了 &#8211; -!
只是个运行时，无视了语法分析。随便写的东西也没什么规划，编写的时候就郁闷不知道哪部分该归分析器那部分该归vm，也不知道现有的部分能不能真用到解释器上，于是华丽地烂尾～只实现了20来条指令，可以递归可以闭包，不过不能算乘法&#8230;囧，很简陋啦~ 
好像是第一次写C，不会make就先凑合了rake &#8211; -！ 对C不熟悉，满地的Segmentation Fault对我们只会用printf调试的菜鸟太残酷了&#8230;用了那个保守式的gc库Boehm GC，面对满地的malloc而无free不清楚泄漏起来会怎样&#8230;囧
本来是对C++那套OO无爱，想单用struct和函数也可以OO么。于是用了C，然后就后悔了：我不想重新实现hashmap之类的东西，C++那stl多好&#8230;T_T。倒也找到了个C的泛型库khash，不过宏终究不如模板来的好看&#8230;现在想来，信息学奥赛acm中用C的那些同学做题的时候都是自己实现一遍各个数据结构么？
拿段伪代码：

def main:
     sum&#40;10&#41;
&#160;
def sum&#40;i&#41;:
     if &#40;i==0&#41; : return 0;
     else: return&#40;i+sum&#40;i-1&#41;&#41;;

放到fsvm下大约是这样：

int test_rec&#40;&#41;&#123;
    Op op_main&#91;&#93;=&#123;
        OP_PUSH_NUM, 10, 
        OP_PUSH_CONST, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/Fleurer/fsvm">http://github.com/Fleurer/fsvm</a><br />
尝试用C写的堆栈机，好像烂尾了 &#8211; -!</p>
<p>只是个运行时，无视了语法分析。随便写的东西也没什么规划，编写的时候就郁闷不知道哪部分该归分析器那部分该归vm，也不知道现有的部分能不能真用到解释器上，于是华丽地烂尾～只实现了20来条指令，可以递归可以闭包，不过不能算乘法&#8230;囧，很简陋啦~ </p>
<p>好像是第一次写C，不会make就先凑合了rake &#8211; -！ 对C不熟悉，满地的Segmentation Fault对我们只会用printf调试的菜鸟太残酷了&#8230;用了那个保守式的gc库Boehm GC，面对满地的malloc而无free不清楚泄漏起来会怎样&#8230;囧</p>
<p>本来是对C++那套OO无爱，想单用struct和函数也可以OO么。于是用了C，然后就后悔了：我不想重新实现hashmap之类的东西，C++那stl多好&#8230;T_T。倒也找到了个C的泛型库khash，不过宏终究不如模板来的好看&#8230;现在想来，信息学奥赛acm中用C的那些同学做题的时候都是自己实现一遍各个数据结构么？</p>
<p>拿段伪代码：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> main:
     <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:
     <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>i==<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> : <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
     <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #ff7700;font-weight:bold;">return</span><span style="color: black;">&#40;</span>i+<span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>i-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>放到fsvm下大约是这样：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> test_rec<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Op op_main<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>
        OP_PUSH_NUM<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> 
        OP_PUSH_CONST<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//&quot;sum&quot;  </span>
        OP_PUSH_VAR<span style="color: #339933;">,</span> 
        OP_CALL<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>
        OP_RET
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    Op op_sum<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>
        OP_PUSH_CONST<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//&quot;i&quot;</span>
        OP_PUSH_VAR<span style="color: #339933;">,</span> 
        OP_POP_TMP<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//store i</span>
        OP_PUSH_TMP<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//push i</span>
        OP_PUSH_NUM<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> 
        OP_EQ<span style="color: #339933;">,</span>
        OP_NOT<span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// i!=0?</span>
        OP_BRANCH<span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> 
            OP_PUSH_NUM<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
            OP_RET<span style="color: #339933;">,</span>
        <span style="color: #666666; font-style: italic;">//else</span>
            OP_PUSH_TMP<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> 
            OP_PUSH_NUM<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//1</span>
            OP_SUB<span style="color: #339933;">,</span> 
            OP_PUSH_CONST<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//&quot;sum&quot;</span>
            OP_PUSH_VAR<span style="color: #339933;">,</span> 
            OP_CALL<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">//sum(tmp[0]-1)</span>
            OP_PUSH_TMP<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> 
            OP_PRINT_STACK<span style="color: #339933;">,</span> 
            OP_ADD<span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//tmp[0]+sum(tmp[0]-1)</span>
        OP_RET
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    Env <span style="color: #339933;">*</span>env<span style="color: #339933;">=</span>fnew_env<span style="color: #009900;">&#40;</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    Proto <span style="color: #339933;">*</span>p_main <span style="color: #339933;">=</span> fnew_proto<span style="color: #009900;">&#40;</span>op_main<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fset_const<span style="color: #009900;">&#40;</span>p_main<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> fstr<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sum&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Func<span style="color: #339933;">*</span> f_main<span style="color: #339933;">=</span>fnew_func<span style="color: #009900;">&#40;</span>p_main<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Obj o_main<span style="color: #339933;">=</span>ffunc<span style="color: #009900;">&#40;</span>f_main<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Proto<span style="color: #339933;">*</span> p_sum<span style="color: #339933;">=</span>fnew_proto<span style="color: #009900;">&#40;</span>op_sum<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fset_const<span style="color: #009900;">&#40;</span>p_sum<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> fstr<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;i&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fset_const<span style="color: #009900;">&#40;</span>p_sum<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> fstr<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sum&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Func<span style="color: #339933;">*</span> f_sum<span style="color: #339933;">=</span>fnew_func<span style="color: #009900;">&#40;</span>p_sum<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Obj o_sum<span style="color: #339933;">=</span>ffunc<span style="color: #009900;">&#40;</span>f_sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    fbind_var<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;sum&quot;</span><span style="color: #339933;">,</span> o_sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    fio_puts<span style="color: #009900;">&#40;</span>fcall<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> f_main<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>创建函数的那几个函数我自己也看着别扭&#8230;不过写C还是老实点好 >_<</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fleurer-lee.com/2010/02/03/fleurers-stack-vm/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
