コンパイラ基礎2 | spin on the RITZ

コンパイラ基礎2

下のようなプログラムをコンパイルする

階乗を求めるプログラム

function f(x)
var c;
begin
    if x=0 then c:=1;
    if x>0 then c:=x*f(x-1);
    return c
end.

main
var a,b;
begin a:=3;b:=f(a);write b
end.
【実行結果】
***** compilation result *****
  0 jump 21
  1 increment 1
  2 load 1, -1
  3 loadn 0
  4 =
  5 jumpc 8
  6 loadn 1
  7 store 1, 2
  8 load 1, -1
  9 loadn 0
 10 >
 11 jumpc 19
 12 load 1, -1
 13 load 1, -1
 14 loadn 1
 15 -
 16 call 1, 1
 17 *
 18 store 1, 2
 19 load 1, 2
 20 return 1, 1
 21 increment 2
 22 loadn 3
 23 store 0, 2
 24 load 0, 2
 25 call 1, 1
 26 store 0, 3
 27 load 0, 3
 28 write
 29 halt
***** execution result *****
6


一応本通りの結果にはなったけど、ちょっと自分で書いてみようとするとエラーが出たりするんだよなぁ~

インタープリタが正しく動いてないのか、それともプログラムが間違っているのか


よ~く見直す必要があるざんす


擬似ニーモニックが出たり、なかなか面白い。



修正しなきゃならんけど、700行近いコード&互いに関連した関数群で成り立ってるので、ちょっと根性要りそう。

まぁ、700程度で苦労してたらダメなんだろうけど(苦笑)



気分転換にPerlでもやるかな~