BCOMP.RIG 2.2 KB

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