BCOMP.RIG 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- Program BCOMP.RIG in directory \RIGAL
  2. -- Compiles program written in Prof.Blikle small language
  3. -- to PASCAL code.
  4. #MAIN
  5. $LIST:=#CALL_PAS(3 'EX1.TXT');
  6. $TREE_CODE:=#analyse_program($LIST);
  7. #GEN_PROGRAM($TREE_CODE);
  8. ##
  9. %INCLUDE ANALYSE.RIG
  10. #GEN_PROGRAM
  11. /#GEN_PROLOG() /
  12. (. (* #GEN_STMT *) .)
  13. / #GEN_EPILOG() / ##
  14. #GEN_PROLOG
  15. /OPEN G 'ex1.pas';
  16. G<<' Program Main;';
  17. G<<' const tt=101;ff=102;err=103;';
  18. G<<' var state:record w,v,x,y,z:integer end;';
  19. G<<' function PLUS (a,b:integer) :integer;';
  20. G<<' begin if (a>100) or (b>100) then Plus:=err ';
  21. G<<' else if a+b >100 then Plus:=err ';
  22. G<<' else Plus:=a+b; end; ';
  23. G<<' function LESS (a,b:integer) :integer;';
  24. G<<' begin if (a>100) or (b>100) then Less:=err ';
  25. G<<' else if a<b then Less:=tt ';
  26. G<<' else Less:=ff; end; ';
  27. G<<' Procedure Val(a:integer);';
  28. G<<' begin case a of ';
  29. G<<' 101 : writeln (''tt''); ';
  30. G<<' 102 : writeln (''ff''); ';
  31. G<<' 103 : writeln (''err''); ';
  32. G<<' else writeln (a);end;end; ';
  33. G<<' BEGIN WITH STATE DO BEGIN ';
  34. G<<' w:=1;v:=1;x:=1;y:=1;z:=1; '/
  35. ##
  36. #GEN_EPILOG
  37. / G<<'Writeln ('' Post-DUMP : '');';
  38. G<<'Write (''w='');Val(w);';
  39. G<<'Write (''v='');Val(v);';
  40. G<<'Write (''x='');Val(x);';
  41. G<<'Write (''y='');Val(y);';
  42. G<<'Write (''z='');Val(z);';
  43. G<<'readln;end; ';
  44. G<<'END.'/
  45. ##
  46. #GEN_STMT
  47. 'assignment_op' ::
  48. <. left_part : $Id
  49. / G<< $Id ':=' /,
  50. right_part : #GEN_EXPR
  51. / G<] ';' / .>
  52. ;;
  53. 'while_op'::
  54. <. condition :
  55. / G<<' WHILE ' /
  56. #GEN_EXPR
  57. / G<] '=tt DO BEGIN '/,
  58. body : (. (* #GEN_STMT *) .)
  59. / G<<'END;' / ,
  60. condition :
  61. / G<< ' IF ' /
  62. #GEN_EXPR
  63. / G<] ' =err THEN x:=err; ' /
  64. .>
  65. ##
  66. #GEN_EXPR
  67. $N / G<] $N / ;;
  68. $Id / G<] $Id / ;;
  69. '+' :: <. arg1:
  70. / G<] ' PLUS( '/
  71. #GEN_EXPR,
  72. arg2:
  73. / G<] ',' /
  74. #GEN_EXPR
  75. / G<] ')' / .>
  76. ;;
  77. '<' :: <. arg1:
  78. / G<] ' LESS( '/
  79. #GEN_EXPR,
  80. arg2:
  81. / G<] ',' /
  82. #GEN_EXPR
  83. / G<] ')' / .>
  84. ##