Advertisement
Guest User

wtf

a guest
May 27th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 41.51 KB | None | 0 0
  1. program compiler;
  2. uses crt, memory;
  3.  
  4. type
  5.     pNodeList = ^tNodeList;
  6.     tNodeList = record
  7.         value: String[30];
  8.         token: String[20];
  9.         base: String[20];
  10.         lineCount : integer;
  11.         idx : integer;
  12.         next: pNodeList;
  13.     end;
  14.  
  15.     LinkedList = record
  16.         first: pNodeList;
  17.         last: pNodeList;
  18.         size: Integer;
  19.     end;
  20.  
  21. var
  22.     list:LinkedList;
  23.     it:Integer;
  24.     itLL:pNodeList;
  25.     node:pNodeList;
  26.     main: String;
  27.     Outfile   : Text;
  28.     error_count : integer;
  29.     globalVar:LinkedList;
  30.     localVar:LinkedList;
  31.     funcList:LinkedList;
  32.     fileList:LinkedList;
  33.     recordList:LinkedList;
  34.     recordVarList:LinkedList;
  35.     valName : string;
  36.     valType : string;
  37.     count : integer;
  38.     jsfile : Text;
  39.     varBase : string;
  40.     Userfile : Text;
  41.     line : String;
  42.     str : String;
  43.     token : String;
  44.     readlnCount : integer;
  45.     c:char;
  46.     noFuncFlag : integer;
  47.     gl : string;
  48.  
  49.  
  50. procedure newLL(var list: LinkedList);  forward;
  51. procedure addToLL(var list: LinkedList; inVal:string;token: String); forward;
  52. procedure addToLL2(var list: LinkedList; inVal:string;token: String; base: String); forward;
  53. procedure addToLL3(lineCount: integer; idx: integer; var list: LinkedList; inVal:string; token: String); forward;
  54. procedure nextLL(var tNode: pNodeList); forward;
  55. procedure getLL(var list: LinkedList; var tNode: pNodeList; name : string); forward;
  56.  
  57.  
  58. procedure readfile(var list: LinkedList); forward;
  59.  
  60. procedure start; forward;
  61.  
  62. procedure js(s: string); forward;
  63. procedure jsln(s: string); forward;
  64. procedure error; forward;
  65. procedure errorPr; forward;
  66. procedure next; forward;
  67. function cur: string; forward;
  68. function val: string; forward;
  69. procedure semicolon_; forward;
  70. function value(par: string) : string; forward;
  71. procedure type_; forward;
  72. procedure var_; forward;
  73. function factor_(z: integer):string; forward;
  74. function term_(z: integer):string; forward;
  75. function expr_(z: integer):string; forward;
  76. function bfactor_(z: integer):string; forward;
  77. function bterm_(z: integer):string; forward;
  78. function bexpr_(z: integer):string; forward;
  79. procedure writeln_; forward;
  80. procedure write_; forward;
  81. procedure function_; forward;
  82. procedure procedure_; forward;
  83. procedure initLocVar; forward;
  84. procedure for_; forward;
  85. procedure while_; forward;
  86. procedure repeat_; forward;
  87. procedure header_; forward;
  88. procedure assign_; forward;
  89. procedure line_; forward;
  90. function index_:string; forward;
  91. procedure else_(z: integer); forward;
  92. procedure if_(z: integer); forward;
  93. function function_arg_:string; forward;
  94. procedure main_; forward;
  95. procedure program_; forward;
  96. procedure lowcase(var str : string); forward;
  97.  
  98. procedure openfile; forward;
  99. procedure closefile; forward;
  100.  
  101. procedure assignfile_; forward;
  102. procedure resetfile_; forward;
  103. procedure closefile_; forward;
  104. procedure rewritefile_; forward;
  105.  
  106.  
  107.  
  108. procedure newLL(var list: LinkedList);
  109.     begin
  110.         list.size:=0;
  111.         list.first := nil;
  112.         list.last := nil;
  113.     end;
  114.  
  115. procedure addToLL(var list: LinkedList; inVal:string; token: String);
  116.     var newNode:pNodeList;
  117.     begin
  118.         if (list.size = 0) then begin
  119.             {new(list.first);}
  120.             list.first := memalloc (SizeOf(tNodeList));
  121.             list.first^.value:=inVal;
  122.             list.first^.token:=token;
  123.             list.first^.base:='';
  124.             list.first^.next := nil;
  125.             list.last:=list.first;
  126.         end else begin
  127.             {new(newNode);}
  128.             newNode := memalloc (SizeOf(tNodeList));
  129.             newNode^.value := inVal;
  130.             newNode^.token := token;
  131.             newNode^.next := nil;
  132.             newNode^.base:='';
  133.             list.last^.next := newNode;
  134.             list.last := newNode;
  135.         end;
  136.  
  137.         list.size := list.size + 1;
  138.  
  139.     end;
  140.    
  141. procedure addToLL3(lineCount: integer; idx: integer; var list: LinkedList; inVal:string; token: String);
  142.     var newNode:pNodeList;
  143.     begin
  144.         if (list.size = 0) then begin
  145.             {new(list.first);}
  146.             list.first := memalloc (SizeOf(tNodeList));
  147.             list.first^.value:=inVal;
  148.             list.first^.token:=token;
  149.             list.first^.base:='';
  150.             list.first^.lineCount:=lineCount;
  151.             list.first^.idx:=idx;
  152.             list.first^.next := nil;
  153.             list.last:=list.first;
  154.         end else begin
  155.             {new(newNode);}
  156.             newNode := memalloc (SizeOf(tNodeList));
  157.             newNode^.value := inVal;
  158.             newNode^.token := token;
  159.             newNode^.next := nil;
  160.             newNode^.base:='';
  161.             newNode^.lineCount:=lineCount;
  162.             newNode^.idx:=idx;
  163.             list.last^.next := newNode;
  164.             list.last := newNode;
  165.         end;
  166.  
  167.         list.size := list.size + 1;
  168.  
  169.     end;
  170.  
  171. procedure addToLL2(var list: LinkedList; inVal:string;token: String; base: String);
  172.     var newNode:pNodeList;
  173.     begin
  174.         if list.size = 0 then begin
  175.             {new(list.first);}
  176.             list.first := memalloc (SizeOf(tNodeList));
  177.             list.first^.value:=inVal;
  178.             list.first^.token:=token;
  179.             list.first^.base:=base;
  180.             list.first^.next := nil;
  181.             list.last:=list.first;
  182.         end else begin
  183.             {new(newNode);}
  184.             newNode := memalloc (SizeOf(tNodeList));
  185.             newNode^.value := inVal;
  186.             newNode^.token := token;
  187.             newNode^.base:=base;
  188.             newNode^.next := nil;
  189.             list.last^.next := newNode;
  190.             list.last := list.last^.next;
  191.         end;
  192.  
  193.         list.size := list.size + 1;
  194.  
  195.     end;
  196.  
  197. procedure nextLL(var tNode: pNodeList);
  198.     begin
  199.         if tNode <> nil then begin
  200.             tNode:=tNode^.next;
  201.         end else begin
  202.             writeln(outfile, '$e next from null!!!');
  203.         end;
  204.     end;
  205.  
  206. procedure getLL(var list: LinkedList; var tNode: pNodeList; name : string);
  207.     var node2:pNodeList;
  208.     begin
  209.         tNode := nil;
  210.         if list.size = 0 then begin
  211.             tNode := nil;
  212.         end else begin
  213.             node2 := list.first;
  214.             while (node2 <> nil) do begin
  215.                 if (node2^.value = name) then begin
  216.                     {tNode := node2;}
  217.                     tNode := memalloc (SizeOf(tNodeList));
  218.                     tNode^.value := node2^.value;
  219.                     tNode^.token := node2^.token;
  220.                     tNode^.base:=node2^.base;
  221.                     tNode^.next := nil;
  222.                 end;
  223.                 node2:=node2^.next;
  224.             end;
  225.         end;
  226.     end;
  227.  
  228.  
  229. procedure lowcase(var str : string);
  230. var i : Integer;
  231. begin
  232.     for i:=1 to length(str) do begin
  233.         if (ord(str[i]) >= ord('A')) and (ord(str[i]) <= ord('Z')) then begin
  234.            str[i] :=  chr(ord(str[i]) - ord('A') + ord('a'));
  235.         end;
  236.    end;
  237. end;
  238.  
  239.  
  240. procedure readfile(var list: LinkedList);
  241. var lineCount : integer;
  242.     idxCount : integer;
  243. begin
  244.  
  245.     Assign(Userfile, 'in.pas');
  246.     Reset(Userfile);
  247.    
  248.     lineCount := 0;
  249.     idxCount := 0;
  250.  
  251.     Repeat
  252.         Readln(Userfile,line);
  253.         lineCount := lineCount + 1;
  254.  
  255.         lowcase(line);
  256.  
  257.  
  258.         {addToLL(list, line, '---------------------');}
  259.         str:='';
  260.         token:='';
  261.  
  262.         it := 1;
  263.         while it <= length(line) do begin
  264.             c := line[it];
  265.  
  266.             if c = '{' then begin
  267.                 addToLL3(lineCount, idxCount, list, c, 'brace_{');
  268.                 idxCount := idxCount + 1;
  269.             end else if c = '}' then begin
  270.                 addToLL3(lineCount, idxCount, list, c, 'brace_}');
  271.                 idxCount := idxCount + 1;
  272.             end else if c = '(' then begin
  273.                 addToLL3(lineCount, idxCount, list, c, 'brace_(');
  274.                 idxCount := idxCount + 1;
  275.             end else if c = ')' then begin
  276.                 addToLL3(lineCount, idxCount, list, c, 'brace_)');
  277.                 idxCount := idxCount + 1;
  278.             end else if c = '[' then begin
  279.                 addToLL3(lineCount, idxCount, list, c, 'brace_[');
  280.                 idxCount := idxCount + 1;
  281.             end else if c = ']' then begin
  282.                 addToLL3(lineCount, idxCount, list, c, 'brace_]');
  283.                 idxCount := idxCount + 1;
  284.             end else if c = '+' then begin
  285.                 addToLL3(lineCount, idxCount, list, c, 'plus');
  286.                 idxCount := idxCount + 1;
  287.             end else if c = '-' then begin
  288.                 addToLL3(lineCount, idxCount, list, c, 'minus');
  289.                 idxCount := idxCount + 1;
  290.             end else if c = '*' then begin
  291.                 addToLL3(lineCount, idxCount, list, c, 'mul');
  292.                 idxCount := idxCount + 1;
  293.             end else if c = '/' then begin
  294.                 addToLL3(lineCount, idxCount, list, c, 'divide');
  295.             end else if c = ';' then begin
  296.                 addToLL3(lineCount, idxCount, list, c, 'semicolon');
  297.                 idxCount := idxCount + 1;
  298.             end else if c = ',' then begin
  299.                 addToLL3(lineCount, idxCount, list, c, 'comma');
  300.                 idxCount := idxCount + 1;
  301.             end else if c = '.' then begin
  302.                 addToLL3(lineCount, idxCount, list, c, 'dot');
  303.                 idxCount := idxCount + 1;
  304.             end else if c = '^' then begin
  305.                 addToLL3(lineCount, idxCount, list, c, 'pointer');
  306.                 idxCount := idxCount + 1;
  307.             end else if c = '=' then begin
  308.                     addToLL3(lineCount, idxCount, list, c, '=');
  309.                     idxCount := idxCount + 1;
  310.             end else if c = '<' then begin
  311.                 if line[it+1]='>' then begin
  312.                     addToLL3(lineCount, idxCount, list, '<>', '<>');
  313.                     idxCount := idxCount + 1;
  314.                     it := it + 1;
  315.                 end else if line[it+1]='=' then begin
  316.                     addToLL3(lineCount, idxCount, list, '<=', '<=');
  317.                     idxCount := idxCount + 1;
  318.                     it := it + 1;
  319.                 end else begin
  320.                     addToLL3(lineCount, idxCount, list, c, '<');
  321.                     idxCount := idxCount + 1;
  322.                 end;
  323.             end else if c = '>' then begin
  324.                 if line[it+1]='=' then begin
  325.                     addToLL3(lineCount, idxCount, list, '>=', '>=');
  326.                     idxCount := idxCount + 1;
  327.                     it := it + 1;
  328.                 end else begin
  329.                     addToLL3(lineCount, idxCount, list, c, '>');
  330.                     idxCount := idxCount + 1;
  331.                 end;
  332.             end else if c = chr(39) then begin
  333.                 it := it + 1;
  334.                 while (it <= length(line)) and (line[it] <> chr(39)) do begin
  335.                     if line[it] = chr(34) then begin
  336.                         str := str + chr(92);
  337.                     end;
  338.                     if line[it] = chr(92) then begin
  339.                         str := str + chr(92);
  340.                     end;
  341.                     str := str + line[it];
  342.                     it := it + 1;
  343.                 end;
  344.                 addToLL3(lineCount, idxCount, list, str, 'str');
  345.                 idxCount := idxCount + 1;
  346.                 str := '';
  347.             end else if c = ':' then begin
  348.                 if line[it+1]='=' then begin
  349.                     addToLL3(lineCount, idxCount, list, ':=', 'assign');
  350.                     idxCount := idxCount + 1;
  351.                     it := it + 1;
  352.                 end else begin
  353.                     addToLL3(lineCount, idxCount, list, c, 'colon');
  354.                     idxCount := idxCount + 1;
  355.                 end;
  356.             end else if (ord(c) >= ord('0')) and (ord(c) <= ord('9')) then begin
  357.                 while (it <= length(line)) and (ord(line[it]) >= ord('0')) and (ord(line[it]) <= ord('9'))  do begin
  358.                     str := str + line[it];
  359.                     it := it + 1;
  360.                 end;
  361.                 addToLL3(lineCount, idxCount, list, str, 'number');
  362.                 idxCount := idxCount + 1;
  363.                 str := '';
  364.                 Continue;
  365.             end else if ((ord(c) >= ord('a')) and (ord(c) <= ord('z'))) or (c = '_') then begin
  366.                 token := 'var';
  367.                 while (it <= length(line)) and (((ord(line[it]) >= ord('0')) and (ord(line[it]) <= ord('9')))
  368.                     or ((ord(line[it]) >= ord('a')) and (ord(line[it]) <= ord('z'))) or (line[it] = '_')) do begin
  369.                     str := str + line[it];
  370.                     it := it + 1;
  371.                 end;
  372.  
  373.                 if str = 'begin' then begin
  374.                     addToLL3(lineCount, idxCount, list, str, 'begin');
  375.                     idxCount := idxCount + 1;
  376.                 end else if str = 'end' then begin
  377.                     addToLL3(lineCount, idxCount, list, str, 'end');
  378.                 end else if str = 'if' then begin
  379.                     addToLL3(lineCount, idxCount, list, str, 'if');
  380.                 end else if str = 'and' then begin
  381.                     addToLL3(lineCount, idxCount, list, str, 'and');
  382.                 end else if str = 'or' then begin
  383.                     addToLL3(lineCount, idxCount, list, str, 'or');
  384.                 end else if str = 'for' then begin
  385.                     addToLL3(lineCount, idxCount, list, str, 'for');
  386.                 end else if str = 'to' then begin
  387.                     addToLL3(lineCount, idxCount, list, str, 'to');
  388.                 end else if str = 'do' then begin
  389.                     addToLL3(lineCount, idxCount, list, str, 'do');
  390.                 end else if str = 'while' then begin
  391.                     addToLL3(lineCount, idxCount, list, str, 'while');
  392.                 end else if str = 'repeat' then begin
  393.                     addToLL3(lineCount, idxCount, list, str, 'repeat');
  394.                 end else if str = 'until' then begin
  395.                     addToLL3(lineCount, idxCount, list, str, 'until');
  396.                 end else if str = 'unit' then begin
  397.                     addToLL3(lineCount, idxCount, list, str, 'unit');
  398.                 end else if str = 'program' then begin
  399.                     addToLL3(lineCount, idxCount, list, str, 'program');
  400.                 end else if str = 'uses' then begin
  401.                     addToLL3(lineCount, idxCount, list, str, 'uses');
  402.                 end else if str = 'type' then begin
  403.                     addToLL3(lineCount, idxCount, list, str, 'type');
  404.                 end else if str = 'string' then begin
  405.                     addToLL3(lineCount, idxCount, list, str, 'string');
  406.                 end else if str = 'integer' then begin
  407.                     addToLL3(lineCount, idxCount, list, str, 'integer');
  408.                 end else if str = 'char' then begin
  409.                     addToLL3(lineCount, idxCount, list, str, 'char');
  410.                 end else if str = 'procedure' then begin
  411.                     addToLL3(lineCount, idxCount, list, str, 'procedure');
  412.                 end else if str = 'function' then begin
  413.                     addToLL3(lineCount, idxCount, list, str, 'function');
  414.                 end else if str = 'writeln' then begin
  415.                     addToLL3(lineCount, idxCount, list, str, 'writeln');
  416.                 end else if str = 'write' then begin
  417.                     addToLL3(lineCount, idxCount, list, str, 'write');
  418.                 end else if str = 'readln' then begin
  419.                     addToLL3(lineCount, idxCount, list, str, 'readln');
  420.                 end else if str = 'record' then begin
  421.                     addToLL3(lineCount, idxCount, list, str, 'record');
  422.                 end else if str = 'interface' then begin
  423.                     addToLL3(lineCount, idxCount, list, str, 'interface');
  424.                 end else if str = 'implementation' then begin
  425.                     addToLL3(lineCount, idxCount, list, str, 'implementation');
  426.                 end else if str = 'var' then begin
  427.                     addToLL3(lineCount, idxCount, list, str, 'var');
  428.                 end else if str = 'then' then begin
  429.                     addToLL3(lineCount, idxCount, list, str, 'then');
  430.                 end else if str = 'else' then begin
  431.                     addToLL3(lineCount, idxCount, list, str, 'else');
  432.                 end else if str = 'case' then begin
  433.                     addToLL3(lineCount, idxCount, list, str, 'case');
  434.                 end else if str = 'assign' then begin
  435.                     addToLL3(lineCount, idxCount, list, str, 'assign');
  436.                 end else if str = 'reset' then begin
  437.                     addToLL3(lineCount, idxCount, list, str, 'reset');
  438.                 end else if str = 'rewrite' then begin
  439.                     addToLL3(lineCount, idxCount, list, str, 'rewrite');
  440.                 end else if str = 'text' then begin
  441.                     addToLL3(lineCount, idxCount, list, str, 'text');
  442.                 end else if str = 'clrscr' then begin
  443.                     addToLL3(lineCount, idxCount, list, str, 'clrscr');
  444.                 end else if str = 'continue' then begin
  445.                     addToLL3(lineCount, idxCount, list, str, 'continue');
  446.                 end else if str = 'close' then begin
  447.                     addToLL3(lineCount, idxCount, list, str, 'close');
  448.                 end else if str = 'forward' then begin
  449.                     addToLL3(lineCount, idxCount, list, str, 'forward');
  450.                 end else begin
  451.                     addToLL3(lineCount, idxCount, list, str, 'variable');
  452.                 end;
  453.                
  454.                
  455.                 idxCount := idxCount + 1;
  456.  
  457.                 str := '';
  458.                 Continue;
  459.             end;
  460.            
  461.             it := it + 1;
  462.         end;
  463.  
  464.  
  465.     Until Eof(Userfile);
  466.  
  467.     Close(Userfile);
  468. end;
  469.  
  470.  
  471. procedure js(s: string);
  472. begin
  473.     write(jsfile, s);
  474. end;
  475.  
  476. procedure jsln(s: string);
  477. begin
  478.     writeln(jsfile, s);
  479. end;
  480.  
  481. procedure error;
  482. begin
  483.     error_count := error_count + 1;
  484.     if node <> nil then begin
  485.         writeln(Outfile, '$e ',val,' {line: ',node^.lineCount,' idx: ',node^.idx,'}');
  486.     end else begin
  487.         writeln(outfile, '$e error null');
  488.     end;
  489.     nextLL(node);
  490. end;
  491.  
  492. procedure errorPr;
  493. begin
  494.     error_count := error_count + 1;
  495.     if node <> nil then begin
  496.         writeln(Outfile, '$e ',val,' {line: ',node^.lineCount,' idx: ',node^.idx,'}');
  497.     end else begin
  498.         writeln(outfile, '$e error null');
  499.     end;
  500. end;
  501.  
  502. procedure next;
  503. begin
  504.     writeln(Outfile, '> ', val);
  505.     nextLL(node);
  506. end;
  507.  
  508. function cur: string;
  509. begin
  510.     if node <> nil then begin
  511.         cur := node^.token;
  512.     end else begin
  513.         cur := '$cur';
  514.     end;
  515. end;
  516.  
  517. function val: string;
  518. begin
  519.     if node <> nil then begin
  520.         val := node^.value;
  521.     end else begin
  522.         val := '$val';
  523.     end;   
  524. end;
  525.  
  526.  
  527. procedure semicolon_;
  528. begin
  529.     if cur = 'semicolon' then begin
  530.         next;
  531.     end else begin
  532.         write(outfile, 'error semicolon ');
  533.         errorPr;
  534.     end;
  535. end;
  536.  
  537.  
  538. function index_ : string;
  539. var res : string;
  540. begin
  541.     res := '';
  542.     if (node^.next <> nil) and (node^.next^.token = 'brace_[') then begin
  543.         next;
  544.         next;
  545.         res := '[(' + expr_(0) + ') - 1]';
  546.  
  547.     end;
  548.     index_ := res;
  549. end;
  550.  
  551.  
  552. function value(par: string) : string;
  553. var
  554.     tNode : pNodeList;
  555.     res : string;
  556.     after : string;
  557. begin
  558.     after := '.val';
  559.  
  560.    
  561.     if par <> 'assign' then begin
  562.         tNode := nil;
  563.         getLL(funcList, tNode, val);
  564.         if tNode <> nil then begin
  565.             after := '.f';
  566.             varBase := gl + '.funcs.';
  567.         end;
  568.     end;
  569.  
  570.    
  571.     if cur = 'str' then begin
  572.         res := ('"' + val + '"');
  573.     end else if cur = 'number' then begin
  574.         res := val;
  575.     end else if cur = 'variable' then begin
  576.         if val = 'memalloc' then begin
  577.             res := '{}';
  578.             while (node^.next <> nil) and (node^.next^.token <> 'semicolon') do begin
  579.                 next;
  580.             end;
  581.         end else if val = 'ord' then begin
  582.             next;
  583.             next;
  584.             res := '((';
  585.             res := res + expr_(0);
  586.             res := res + ').charCodeAt(0))';
  587.         end else if val = 'length' then begin
  588.             next;
  589.             next;
  590.             res := '(';
  591.             res := res + expr_(0);
  592.             res := res + ').length';
  593.         end else if val = 'chr' then begin
  594.             next;
  595.             next;
  596.             res := 'String.fromCharCode(';
  597.             res := res + expr_(0);
  598.             res := res + ')';
  599.         end else if (val = 'line') and (node^.next^.token = 'brace_(') then begin
  600.             next;
  601.             next;
  602.             res := 'line(';
  603.             res := res + expr_(0) + ',';
  604.             next;
  605.             res := res + expr_(0) + ',';
  606.             next;
  607.             res := res + expr_(0) + ',';
  608.             next;
  609.             res := res + expr_(0);
  610.             next;
  611.             res := res + ');';
  612.         end else if val = 'eof' then begin
  613.             next;
  614.             next;
  615.             res := 'eof(';
  616.             res := res + gl + '.' + val;
  617.             res := res + ')';
  618.             next;
  619.         end else if val = 'nil' then begin
  620.             res := 'null';
  621.         end else begin
  622.             tNode := nil;
  623.             getLL(localVar, tNode, val);
  624.             if ((localVar.size = 0) or (tNode = nil)) and (after <> '.f') then begin
  625.                 varBase := gl + '.';
  626.             end;
  627.             if (tNode <> nil) and (after <> '.f') then begin
  628.                 varBase := tNode^.base;
  629.                 after := '.val';
  630.                 noFuncFlag := 1;
  631.             end;
  632.             res := (varBase + val + after);
  633.         end;
  634.     end else begin
  635.         res := '';
  636.     end;
  637.  
  638.     res := res + index_;
  639.  
  640.     while (node^.next^.token = 'dot') or (node^.next^.token = 'pointer') do begin
  641.         noFuncFlag := 1;
  642.         res := res + index_;
  643.         if node^.next^.token = 'pointer' then begin
  644.             next;
  645.         end;
  646.         res := res + '.';
  647.         next;
  648.         next;
  649.         res := res + val;
  650.     end;
  651.    
  652.     value := res;
  653. end;
  654.  
  655. procedure type_;
  656. var tmp : integer;
  657.     valName : string;
  658. begin
  659.     tmp := 0;
  660.     while cur = 'variable' do begin
  661.         tmp := tmp + 1;
  662.         js(val);
  663.         valName := val;
  664.         next;
  665.         if cur = '=' then begin
  666.             jsln(': { f : function(obj) { ');
  667.             next;
  668.             if cur = 'pointer' then begin
  669.                 addToLL(recordList, valName, 'pas.' + main + '.');
  670.                 next;
  671.                 jsln('var tmp = {};');
  672.                 jsln('tmp.val = null;');
  673.                 jsln('return  tmp;');
  674.                 jsln('}},');
  675.                 next;
  676.                 next;
  677.                 continue;
  678.             end;
  679.             if cur = 'record' then begin
  680.                 next;
  681.             end else begin
  682.                 error;
  683.             end;
  684.             addToLL(recordList, valName, 'pas.' + main + '.');
  685.             jsln('obj.val = {};');
  686.             while cur = 'variable' do begin
  687.                 {jsln('obj.val.' + val + '= тут пустой объект ;');}
  688.                 js('obj.val.' + val + '=');
  689.                 valName := val;
  690.                 next;
  691.                 if cur = 'colon' then begin
  692.                     next;
  693.                     valType := val;
  694.                     if cur = 'integer' then begin
  695.                         jsln('0;');
  696.                         next;
  697.                         semicolon_;
  698.                     end else if (cur = 'string') or (cur = 'char') then begin
  699.                         jsln('"";');
  700.                         next;
  701.                         semicolon_;
  702.                     end else begin
  703.                         jsln('null;');
  704.                         next;
  705.                         semicolon_;
  706.                         writeln(outfile, 'error type_ undif type');
  707.                     end;
  708.                 end else begin
  709.                     write(outfile, 'error type_ no colon');
  710.                     error;
  711.                 end;
  712.             end;
  713.         end else begin
  714.             write(outfile, 'error type_ no colon');
  715.             error;
  716.         end;
  717.         jsln('return obj;}');
  718.         jsln('},');
  719.         if cur = 'end' then begin
  720.             next;
  721.             semicolon_;
  722.         end else begin         
  723.             write(outfile, 'error type_ no end of record');
  724.             error;
  725.         end;
  726.     end;
  727.     if tmp = 0 then begin
  728.         write(outfile, 'error type_ no variable');
  729.         error;
  730.     end;
  731. end;
  732.  
  733. procedure var_;
  734. var
  735.     tmp : integer;
  736.     valName : string;
  737.     valType : string;
  738.     tNode : pNodeList;
  739. begin
  740.     tmp := 0;
  741.     while cur = 'variable' do begin
  742.         tmp := tmp + 1;
  743.         js(val);
  744.         valName := val;
  745.         next;
  746.         if cur = 'colon' then begin
  747.             js(':');
  748.             next;
  749.             valType := val;
  750.             addToLL2(globalVar, valName, valType, 'pas.' + main + '.');
  751.             if cur = 'integer' then begin
  752.                 js('{val:0}');
  753.                 jsln(',');
  754.                 next;
  755.                 semicolon_;
  756.             end else if (cur = 'string') or (cur = 'char') then begin
  757.                 js('{val:""}');
  758.                 jsln(',');
  759.                 next;
  760.                 semicolon_;
  761.             end else if (cur = 'text') then begin
  762.                 js('{val:{}}');
  763.                 addToLL(fileList, valName, 'text');
  764.                 jsln(',');
  765.                 next;
  766.                 semicolon_;
  767.             end else if cur = 'variable' then begin
  768.                 tNode := nil;
  769.                 getLL(recordList, tNode, val);
  770.                 if tNode <> nil then begin
  771.                     addToLL(recordVarList, valName, val);
  772.                     js('{val:{}}');
  773.                     next;
  774.                     semicolon_;
  775.                     jsln(',');
  776.                 end else begin
  777.                     write(outfile, 'error var_ variable type undif');
  778.                     error;
  779.                 end;
  780.             end else begin
  781.                 write(outfile, 'error var_ undif type');
  782.                 error;
  783.             end;
  784.         end else begin
  785.             write(outfile, 'error var_ no colon');
  786.             error;
  787.         end;
  788.     end;
  789.     if tmp = 0 then begin
  790.         write(outfile, 'error var_ no variable');
  791.         error;
  792.     end;
  793. end;
  794.  
  795. function factor_ (z: integer): string;
  796. var tNode : pNodeList;
  797.     res : string;
  798. begin
  799.     res := '';
  800.     noFuncFlag := 0;
  801.     if (cur = 'number') or (cur = 'variable') or (cur = 'str') then begin
  802.         res := res + (value(''));
  803.         if cur = 'variable' then begin         
  804.             tNode := nil;
  805.             getLL(funcList, tNode, val);
  806.             next;
  807.             if (tNode <> nil) and (noFuncFlag = 0) then begin
  808.                 if cur = 'brace_(' then begin
  809.                     res := res + function_arg_;
  810.                 end else begin
  811.                     res := res + '()';
  812.                 end;
  813.                 res := res + '.val';
  814.             end;
  815.         end else begin
  816.             next;
  817.         end;
  818.     end else if cur = 'brace_(' then begin
  819.         res := res + ('(');
  820.         next;
  821.         res := res + expr_(0);
  822.         if cur = 'brace_)' then begin
  823.             res := res + (')');
  824.             next;
  825.         end else begin
  826.             write(outfile, 'error factor_ brace_)');
  827.             error;
  828.         end;
  829.     end;
  830.     factor_ := res;
  831. end;
  832.  
  833.  
  834. function term_(z: integer):string;
  835. var res : string;
  836. begin
  837.     res := '';
  838.     res := res + factor_(0);
  839.  
  840.     if (node <> nil) and ((cur = 'divide') or (cur = 'mul')) then begin
  841.         res := res + (val);
  842.         next;
  843.         res := res + term_(0);
  844.     end;
  845.     term_ := res;
  846. end;
  847.  
  848. function expr_(z: integer):string;
  849. var res : string;
  850. begin
  851.     res := '';
  852.     res := res + term_(0);
  853.  
  854.     if (node <> nil) and ((cur = 'minus') or (cur = 'plus')) then begin
  855.         res := res + (val);
  856.         next;
  857.         res := res + expr_(0);
  858.     end;
  859.     expr_ := res;
  860. end;
  861.  
  862. function bfactor_(z: integer):string;
  863. var
  864.     res : string;
  865. begin
  866.     res := '';
  867.     if (cur = 'number') or (cur = 'variable') or (cur = 'str') then begin
  868.         res := res + factor_(0);
  869.     end else if cur = 'brace_(' then begin
  870.         res := res + ('(');
  871.         next;
  872.         res := res + bexpr_(0);
  873.         if cur = 'brace_)' then begin
  874.             res := res + (')');
  875.             next;
  876.         end else begin
  877.             write(outfile, 'error factor_ brace_)');
  878.             error;
  879.         end;
  880.     end;
  881.     bfactor_ := res;
  882. end;
  883.  
  884.  
  885. function bterm_(z: integer):string;
  886. var
  887.     res : string;
  888. begin
  889.     res := '';
  890.     res := res + bfactor_(0);
  891.  
  892.     if (node <> nil) and ((cur = '<') or (cur = '<=') or (cur = '>')
  893.         or (cur = '>=') or (cur = '<>') or (cur = '=')) then begin
  894.         if cur = '<>' then begin
  895.             res := res +  ('!=');
  896.         end else if cur = '=' then begin
  897.             res := res + ('==');
  898.         end else begin
  899.             res := res + (val);
  900.         end;
  901.         next;
  902.         res := res + bterm_(0);
  903.     end;
  904.     bterm_ := res;
  905. end;
  906.  
  907. function bexpr_(z: integer):string;
  908. var
  909.     res : string;
  910. begin
  911.     res := '';
  912.     res := res + bterm_(0);
  913.  
  914.     if (node <> nil) and ((cur = 'and') or (cur = 'or')) then begin
  915.         if cur = 'and' then begin
  916.             res := res + ('&&');
  917.         end else begin
  918.             res := res + ('||');
  919.         end;
  920.         next;
  921.         res := res + bexpr_(0);
  922.     end;
  923.     bexpr_ := res;
  924. end;
  925.  
  926. procedure writeln_;
  927. var
  928.     tNode : pNodeList;
  929. begin
  930.    
  931.     if cur = 'brace_(' then begin
  932.         next;
  933.        
  934.         tNode := nil;
  935.         getLL(fileList, tNode, val);
  936.         if tNode <> nil then begin
  937.             js('writelnfile(' + gl + '.' + val + ', (');
  938.             next;
  939.             next;
  940.         end else begin     
  941.             js('writeln((');
  942.         end;
  943.         js(expr_(0));
  944.         while cur = 'comma' do begin
  945.             js('+');
  946.             next;
  947.             js(expr_(0));
  948.         end;
  949.         if cur = 'brace_)' then begin
  950.             jsln('));');
  951.             next;
  952.             semicolon_;
  953.         end else begin
  954.             write(outfile, 'error writeln brace_)');
  955.             error;
  956.         end;
  957.     end else begin
  958.         write(outfile, 'error writeln brace_(');
  959.         error;
  960.     end;
  961. end;
  962.  
  963. procedure write_;
  964. var
  965.     tNode : pNodeList;
  966. begin
  967.     if cur = 'brace_(' then begin
  968.         next;
  969.        
  970.         tNode := nil;
  971.         getLL(fileList, tNode, val);
  972.         if tNode <> nil then begin
  973.             js('writefile(' + gl + '.' + val + ', (');
  974.             next;
  975.             next;
  976.         end else begin     
  977.             js('write((');
  978.         end;
  979.        
  980.         js(expr_(0));
  981.         while cur = 'comma' do begin
  982.             js('+');
  983.             next;
  984.             js(expr_(0));
  985.         end;
  986.         if cur = 'brace_)' then begin
  987.             jsln('));');
  988.             next;
  989.             semicolon_;
  990.         end else begin
  991.             write(outfile, 'error write brace_)');
  992.             error;
  993.         end;
  994.     end else begin
  995.         write(outfile, 'error write brace_(');
  996.         error;
  997.     end;
  998. end;
  999.  
  1000. procedure initLocVar;
  1001. var tNode : pNodeList;
  1002. begin
  1003.     if cur = 'integer' then begin
  1004.         js('{val:0}');
  1005.     end else if (cur = 'string') or (cur = 'char') then begin
  1006.         js('{val:""}');
  1007.     end else if cur = 'variable' then begin
  1008.         tNode := nil;
  1009.         getLL(recordList, tNode, val);
  1010.         if tNode <> nil then begin
  1011.             js(gl + '.' + val + '.f({})');
  1012.         end else begin
  1013.             write(outfile, 'error var_ variable type undif but {}');
  1014.             js('{}');
  1015.         end;
  1016.     end else begin
  1017.         write(outfile, 'error var_ undif type');
  1018.         error;
  1019.     end;
  1020. end;
  1021.  
  1022. procedure function_;
  1023. var funcName : string;
  1024.     cloneList:LinkedList;
  1025.     flag : integer;
  1026.     itClone : pNodeList;
  1027. begin
  1028.     newLL(localVar);
  1029.     newLL(cloneList);
  1030.     funcName := val;
  1031.     addToLL(funcList, funcName, '');
  1032.     if cur = 'variable' then begin
  1033.         js(val);
  1034.         js(': { f :function(');
  1035.         next;
  1036.         if cur = 'brace_(' then begin
  1037.             next;
  1038.             while cur <> 'brace_)' do begin
  1039.                 flag := 0;
  1040.                 if cur = 'var' then begin
  1041.                     flag := 1;
  1042.                     next;
  1043.                 end;
  1044.                 if cur = 'variable' then begin
  1045.                     js(val);
  1046.                     valName := val;
  1047.                     if flag = 0 then begin
  1048.                         addToLL(cloneList, valName, '');
  1049.                     end;
  1050.                     next;
  1051.                     if cur = 'colon' then begin
  1052.                         next;
  1053.                         if (cur = 'integer') or (cur = 'string')
  1054.                             or (cur = 'variable') then begin
  1055.                             valType := val;
  1056.                             addToLL2(localVar, valName, valType, '');
  1057.                             next;
  1058.                             if cur = 'semicolon' then begin
  1059.                                 next;
  1060.                                 js(',');
  1061.                             end;
  1062.                         end else begin
  1063.                             write(outfile, 'error function_ arg no type');
  1064.                             error;
  1065.                         end;
  1066.                     end else begin
  1067.                         write(outfile, 'error function_ arg no colon');
  1068.                         error;
  1069.                     end;
  1070.                 end else begin
  1071.                     write(outfile, 'error function_ arg no variable');
  1072.                     error;
  1073.                 end;
  1074.             end;
  1075.             next;
  1076.         end;
  1077.         jsln(') {');
  1078.         if cur = 'colon' then begin
  1079.             next;
  1080.             if (cur = 'integer') or (cur = 'string')
  1081.                 or (cur = 'variable') then begin
  1082.                 addToLL2(localVar, funcName, val, '');
  1083.                 jsln('var ' + funcName + '={};');
  1084.                 next;
  1085.                 semicolon_;
  1086.             end else begin
  1087.                 write(outfile, 'error function_ no ret type');
  1088.                 error;
  1089.             end;
  1090.         end else begin
  1091.             write(outfile, 'error function_ no ret type colon');
  1092.             error;
  1093.         end;
  1094.  
  1095.         if cloneList.size <> 0 then begin
  1096.             itClone := cloneList.first;
  1097.             while itClone <> nil do begin
  1098.                 jsln(itClone^.value + ' = {val: ' + itClone^.value + '.val};');
  1099.                 nextLL(itClone);
  1100.             end;
  1101.         end;
  1102.  
  1103.         if cur = 'var' then begin
  1104.             next;
  1105.             count := 0;
  1106.             while cur = 'variable' do begin
  1107.                 count := count + 1;
  1108.                 js('var ' + val);
  1109.  
  1110.                 valName := val;
  1111.                 next;
  1112.                 if cur = 'colon' then begin
  1113.                     js(' = ');
  1114.                     next;
  1115.                     valType := val;
  1116.                     addToLL2(localVar, valName, valType, '');
  1117.                     initLocVar;
  1118.                     jsln(';');
  1119.                     next;
  1120.                     semicolon_;
  1121.                 end else begin
  1122.                     write(outfile, 'error type_ no colon');
  1123.                     error;
  1124.                 end;
  1125.             end;
  1126.             if count = 0 then begin
  1127.                 write(outfile, 'error type_ no variable');
  1128.                 error;
  1129.             end;
  1130.         end;
  1131.         if cur = 'begin' then begin
  1132.             next;
  1133.  
  1134.             varBase := '';
  1135.  
  1136.             while (node <> nil) and (cur <> 'end') do begin
  1137.                 line_;
  1138.             end;
  1139.  
  1140.             if node = nil then begin               
  1141.                 write(outfile, 'error func node nil');
  1142.                 error;
  1143.             end else if cur = 'end' then begin
  1144.                 next;
  1145.                 semicolon_;
  1146.             end;
  1147.             jsln('return ' + funcName + ';');
  1148.         end;
  1149.         jsln('}}, //func');
  1150.     end else begin
  1151.         write(outfile, 'error function_ no name');
  1152.         error;
  1153.     end;
  1154.     localVar.size := 0;
  1155. end;
  1156.  
  1157. procedure procedure_;
  1158. var    
  1159.     funcName : string;
  1160.     cloneList:LinkedList;
  1161.     flag : integer;
  1162.     itClone : pNodeList;
  1163. begin
  1164.     newLL(localVar);
  1165.     newLL(cloneList);
  1166.     funcName := val;
  1167.     addToLL(funcList, funcName, '');
  1168.     if cur = 'variable' then begin
  1169.         js(val);
  1170.         js(': { f :function(');
  1171.         next;
  1172.         if cur = 'brace_(' then begin
  1173.             next;
  1174.             while cur <> 'brace_)' do begin
  1175.                 flag := 0;
  1176.                 if cur = 'var' then begin
  1177.                     flag := 1;
  1178.                     next;
  1179.                 end;
  1180.                 if cur = 'variable' then begin
  1181.                     js(val);
  1182.                     valName := val;
  1183.                     if flag = 0 then begin
  1184.                         addToLL(cloneList, valName, '');
  1185.                     end;
  1186.                     next;
  1187.                     if cur = 'colon' then begin
  1188.                         next;
  1189.                         if (cur = 'integer') or (cur = 'string')
  1190.                             or (cur = 'variable') then begin
  1191.                             valType := val;
  1192.                             addToLL2(localVar, valName, valType, '');
  1193.                             next;
  1194.                             if cur = 'semicolon' then begin
  1195.                                 next;
  1196.                                 js(',');
  1197.                             end;
  1198.                         end else begin
  1199.                             write(outfile, 'error function_ arg no type');
  1200.                             error;
  1201.                         end;
  1202.                     end else begin
  1203.                         write(outfile, 'error function_ arg no colon');
  1204.                         error;
  1205.                     end;
  1206.                 end else begin
  1207.                     write(outfile, 'error function_ arg no variable');
  1208.                     error;
  1209.                 end;
  1210.             end;
  1211.             next;
  1212.         end;
  1213.         jsln(') {');
  1214.         semicolon_;
  1215.  
  1216.         if cloneList.size <> 0 then begin
  1217.             itClone := cloneList.first;
  1218.             while itClone <> nil do begin
  1219.                 jsln(itClone^.value + ' = {val: ' + itClone^.value + '.val};');
  1220.                 nextLL(itClone);
  1221.             end;
  1222.         end;
  1223.  
  1224.         if cur = 'var' then begin
  1225.             next;
  1226.             count := 0;
  1227.             while cur = 'variable' do begin
  1228.                 count := count + 1;
  1229.                 js('var ' + val);
  1230.  
  1231.                 valName := val;
  1232.                 next;
  1233.                 if cur = 'colon' then begin
  1234.                     js(' = ');
  1235.                     next;
  1236.                     valType := val;
  1237.                     addToLL2(localVar, valName, valType, '');
  1238.                     initLocVar;
  1239.                     jsln(';');
  1240.                     next;
  1241.                     semicolon_;
  1242.                 end else begin
  1243.                     write(outfile, 'error type_ no colon');
  1244.                     error;
  1245.                 end;
  1246.             end;
  1247.             if count = 0 then begin
  1248.                 write(outfile, 'error type_ no variable');
  1249.                 error;
  1250.             end;
  1251.         end;
  1252.        
  1253.         if cur = 'begin' then begin
  1254.             next;
  1255.  
  1256.             varBase := '';
  1257.  
  1258.             while (node <> nil) and (cur <> 'end') do begin
  1259.                 line_;
  1260.             end;
  1261.  
  1262.             if node = nil then begin               
  1263.                 write(outfile, 'error func node nil');
  1264.                 error;
  1265.             end else if cur = 'end' then begin
  1266.                 next;
  1267.                 semicolon_;
  1268.             end;           
  1269.         end;
  1270.         jsln('}}, // proc');
  1271.     end else begin
  1272.         write(outfile, 'error function_ no name');
  1273.         error;
  1274.     end;
  1275.     localVar.size := 0;
  1276. end;
  1277.  
  1278. procedure uses_;
  1279. begin
  1280.     while cur <> 'semicolon' do begin
  1281.         next;
  1282.     end;
  1283.     semicolon_;
  1284. end;
  1285.  
  1286. procedure header_;
  1287. begin
  1288.     if cur = 'type' then begin
  1289.         next;
  1290.         type_;
  1291.         header_;
  1292.     end else if cur = 'var' then begin
  1293.         next;
  1294.         var_;
  1295.         header_;
  1296.     end else if cur = 'uses' then begin
  1297.         next;
  1298.         uses_;
  1299.         header_;
  1300.     end else if cur = 'function' then begin
  1301.         next;
  1302.         function_;
  1303.         header_;
  1304.     end else if cur = 'procedure' then begin
  1305.         next;
  1306.         procedure_;
  1307.         header_;
  1308.     end else if cur = 'forward' then begin
  1309.         next;
  1310.         semicolon_;
  1311.         header_;
  1312.     end;
  1313. end;
  1314.  
  1315. procedure assign_;
  1316. begin
  1317.     if cur = 'assign' then begin
  1318.         js(' = (');
  1319.         next;
  1320.         js(expr_(0));
  1321.         jsln(');');
  1322.     end else begin     
  1323.         write(outfile, 'error assign');
  1324.         error;
  1325.     end;
  1326. end;
  1327.  
  1328. procedure assignfile_;
  1329. begin
  1330.     if cur = 'brace_(' then begin
  1331.         next;
  1332.         js('assignfile(' + gl + '.' + val + ', ');
  1333.         next;
  1334.         next;
  1335.         jsln('"' + val + '");');
  1336.         next;
  1337.         next;
  1338.         semicolon_;
  1339.     end else begin     
  1340.         write(outfile, 'error assignfile_ brace (');
  1341.         error;
  1342.     end;   
  1343. end;
  1344.  
  1345. procedure resetfile_;
  1346. begin
  1347.     if cur = 'brace_(' then begin
  1348.         next;
  1349.         jsln('resetfile(' + gl + '.' + val + ');');
  1350.         next;
  1351.         next;
  1352.         semicolon_;
  1353.     end else begin
  1354.         write(outfile, 'error assignfile_ brace (');
  1355.         error;
  1356.     end;   
  1357. end;
  1358.  
  1359. procedure closefile_;
  1360. begin
  1361.     if cur = 'brace_(' then begin
  1362.         next;
  1363.         jsln('closefile(' + gl + '.' + val + ');');
  1364.         next;
  1365.         next;
  1366.         semicolon_;
  1367.     end else begin
  1368.         write(outfile, 'error assignfile_ brace (');
  1369.         error;
  1370.     end;   
  1371. end;
  1372.  
  1373. procedure rewritefile_;
  1374. begin
  1375.     if cur = 'brace_(' then begin
  1376.         next;
  1377.         jsln('rewritefile(' + gl + '.' + val + ');');
  1378.         next;
  1379.         next;
  1380.         semicolon_;
  1381.     end else begin
  1382.         write(outfile, 'error assignfile_ brace (');
  1383.         error;
  1384.     end;   
  1385. end;
  1386.  
  1387. procedure while_;
  1388. begin
  1389.     js('while (');
  1390.     js(bexpr_(0));
  1391.     jsln(') {');
  1392.     if cur = 'do' then begin
  1393.         next;
  1394.         if cur = 'begin' then begin
  1395.             next;
  1396.             while (node <> nil) and (cur <> 'end') do begin
  1397.                 line_;
  1398.             end;
  1399.         end;
  1400.         if cur = 'end' then begin
  1401.             jsln('}  // while');   
  1402.             next;
  1403.             semicolon_;
  1404.         end else begin
  1405.             write(outfile, 'error while_ not end');
  1406.             error;
  1407.         end;
  1408.     end else begin
  1409.         write(outfile, 'error while_ not do');
  1410.         error;
  1411.     end;
  1412.    
  1413. end;
  1414.  
  1415.  
  1416. procedure repeat_;
  1417. begin
  1418.     jsln('do {  // repeat');
  1419.  
  1420.  
  1421.     while (node <> nil) and (cur <> 'until') do begin
  1422.         line_;
  1423.     end;
  1424.     next;
  1425.  
  1426.     js('} while (!(');
  1427.     js(bexpr_(0));
  1428.     semicolon_;
  1429.     jsln('));');
  1430.  
  1431. end;
  1432.  
  1433. procedure for_;
  1434. var itName : string;
  1435. begin
  1436.     js('for (');
  1437.  
  1438.     if cur = 'variable' then begin
  1439.         js(value('') + ' = ');
  1440.         itName := value('');
  1441.         next;
  1442.         if cur = 'assign' then begin
  1443.             next;
  1444.             if (cur = 'number') or (cur = 'variable') then begin
  1445.                 js(value(''));
  1446.                 next;
  1447.                 if cur = 'to' then begin
  1448.                     next;
  1449.                     if (cur = 'number') or (cur = 'variable') then begin
  1450.                         jsln('; ' + itName + ' <= ' + value('') + '; ' + itName + '++) {');
  1451.                         next;
  1452.                         if cur = 'do' then begin
  1453.                             next;
  1454.                             if cur = 'begin' then begin
  1455.                                 next;
  1456.                                 while (node <> nil) and (cur <> 'end') do begin
  1457.                                     line_;
  1458.                                 end;
  1459.                             end else begin
  1460.                                 line_;
  1461.                             end;
  1462.                             if cur = 'end' then begin
  1463.                                 next;
  1464.                                 semicolon_;
  1465.                             end else begin
  1466.                                 write(outfile, 'error for not end');
  1467.                                 error;
  1468.                             end;
  1469.                         end else begin
  1470.                             write(outfile, 'error for not do');
  1471.                             error;
  1472.                         end;
  1473.                     end;
  1474.                 end else begin
  1475.                     write(outfile, 'error for not assign');
  1476.                     error;
  1477.                 end;
  1478.             end;
  1479.         end else begin
  1480.             write(outfile, 'error for not assign');
  1481.         end;
  1482.     end else begin
  1483.         write(outfile, 'error for not variable');
  1484.         error;
  1485.     end;
  1486.     jsln('} // for');
  1487.     jsln(itName + '--;');
  1488. end;
  1489.  
  1490. procedure if_(z: Integer);
  1491. begin
  1492.     js('if (');
  1493.     js(bexpr_(0));
  1494.     jsln(') { //if');
  1495.     if cur = 'then' then begin
  1496.         next;
  1497.         if cur = 'begin' then begin
  1498.             next;
  1499.             while (node <> nil) and (cur <> 'end') do begin
  1500.                 line_;
  1501.             end;
  1502.         end;
  1503.         if cur = 'end' then begin
  1504.             jsln('} //if');
  1505.             next;
  1506.         end;
  1507.         if cur = 'semicolon' then begin
  1508.             next;
  1509.         end;       
  1510.         if cur = 'else' then begin
  1511.             next;
  1512.             else_(0);
  1513.         end;
  1514.     end else begin     
  1515.         write(outfile, 'error if_ not then');
  1516.         error;
  1517.     end;
  1518. end;
  1519.  
  1520. procedure else_(z:Integer);
  1521. begin
  1522.     js(' else ');
  1523.    
  1524.     if cur = 'if' then begin
  1525.         next;
  1526.         if_(0);
  1527.     end else if cur = 'begin' then begin
  1528.         jsln(' { // else');
  1529.         next;
  1530.         while (node <> nil) and (cur <> 'end') do begin
  1531.             line_;
  1532.         end;
  1533.         if cur = 'end' then begin
  1534.             next;
  1535.             jsln('} // else');
  1536.         end;
  1537.     end;   
  1538.     if cur = 'semicolon' then begin
  1539.         next;
  1540.     end;
  1541.     if cur = 'else' then begin
  1542.         next;
  1543.         else_(0);
  1544.     end;
  1545.    
  1546. end;
  1547.  
  1548. procedure readln_;
  1549. var
  1550.     tNode : pNodeList;
  1551. begin
  1552.     if cur = 'brace_(' then begin
  1553.         next;
  1554.         if cur = 'variable' then begin
  1555.             tNode := nil;
  1556.             getLL(fileList, tNode, val);
  1557.             if tNode <> nil then begin
  1558.                 js('readlnfile(' + gl + '.' + val + ', (');
  1559.                 next;
  1560.                 next;
  1561.                
  1562.                 tNode := nil;
  1563.                 getLL(localVar, tNode, val);
  1564.                 if (localVar.size = 0) or (tNode = nil) then begin
  1565.                     varBase := gl + '.';
  1566.                 end else begin
  1567.                     varBase := tNode^.base;
  1568.                 end;
  1569.                 jsln(varBase + val + '));');
  1570.                 next;
  1571.                 next;
  1572.             end else begin         
  1573.                 js('waitReadln(');
  1574.                
  1575.                 tNode := nil;
  1576.                 getLL(localVar, tNode, val);
  1577.                 if (localVar.size = 0) or (tNode = nil) then begin
  1578.                     varBase := gl + '.';
  1579.                 end else begin
  1580.                     varBase := tNode^.base;
  1581.                 end;
  1582.                 js(varBase + val);
  1583.                 jsln(', function(){');
  1584.                 readlnCount := readlnCount + 1;
  1585.                 next;
  1586.                 next;
  1587.             end;
  1588.         end else begin
  1589.             writeln(outfile, 'error readln_ no variable arg');
  1590.         end;
  1591.        
  1592.     end else begin
  1593.         jsln('waitReadln({}, function(){');
  1594.         readlnCount := readlnCount + 1;
  1595.     end;
  1596.     next;
  1597. end;
  1598.  
  1599. function function_arg_ : string;
  1600. var
  1601.     tNode : pNodeList;
  1602.     res : string;
  1603. begin
  1604.     res := '';
  1605.     res := res + ('(');
  1606.     next;
  1607.     while cur <> 'brace_)' do begin
  1608.         if (cur = 'variable') and
  1609.             ((node^.next^.token = 'comma') or (node^.next^.token = 'brace_)')) then begin
  1610.             tNode := nil;
  1611.             getLL(localVar, tNode, val);
  1612.             if (localVar.size = 0) or (tNode = nil) then begin
  1613.                 varBase := gl + '.';
  1614.             end else begin
  1615.                 varBase := tNode^.base;
  1616.             end;
  1617.            
  1618.             tNode := nil;
  1619.             getLL(funcList, tNode, val);
  1620.             if (tNode <> nil) then begin
  1621.                 res := res + (gl + '.funcs.' + val + '.f()');
  1622.             end else begin         
  1623.                 res := res + (varBase + val);
  1624.             end;
  1625.             next;
  1626.             if cur = 'comma' then begin
  1627.                 res := res + (',');
  1628.                 next;
  1629.             end;
  1630.         end else if (cur = 'variable') or (cur = 'str') or (cur = 'number') then begin
  1631.             res := res + ('{val:');
  1632.             res := res + (expr_(0));
  1633.             res := res + ('}');
  1634.             if cur = 'comma' then begin
  1635.                 res := res + (',');
  1636.                 next;
  1637.             end;
  1638.         end else begin         
  1639.             write(outfile, 'error line_ no func arg');
  1640.             error;
  1641.         end;
  1642.     end;
  1643.     res := res + (')');
  1644.     next;
  1645.     function_arg_ := res;
  1646. end;
  1647.  
  1648. procedure line_;
  1649. begin
  1650.     if cur = 'variable' then begin
  1651.         if(node^.next^.token = 'assign') then begin
  1652.             js(value('assign'));       
  1653.         end else begin
  1654.             js(value('line')); 
  1655.         end;
  1656.         next;
  1657.         if cur = 'assign' then begin
  1658.             assign_;
  1659.             semicolon_;
  1660.         end else if (cur = 'semicolon') then begin
  1661.             jsln('();');
  1662.             semicolon_;
  1663.         end else if cur = 'brace_(' then begin
  1664.             js(function_arg_);
  1665.             jsln(';');
  1666.             semicolon_;
  1667.         end;
  1668.     end else if cur = 'writeln' then begin
  1669.         next;
  1670.         writeln_;
  1671.     end else if cur = 'write' then begin
  1672.         next;
  1673.         write_;
  1674.     end else if cur = 'while' then begin
  1675.         next;
  1676.         while_;
  1677.     end else if cur = 'repeat' then begin
  1678.         next;
  1679.         repeat_;
  1680.     end else if cur = 'clrscr' then begin
  1681.         next;
  1682.         jsln('clrscr();');
  1683.         semicolon_;
  1684.     end else if cur = 'readln' then begin
  1685.         next;
  1686.         readln_;
  1687.     end else if cur = 'for' then begin
  1688.         next;
  1689.         for_;
  1690.     end else if cur = 'if' then begin
  1691.         next;
  1692.         if_(0);
  1693.     end else if cur = 'assign' then begin
  1694.         next;
  1695.         assignfile_;
  1696.     end else if cur = 'reset' then begin
  1697.         next;
  1698.         resetfile_;
  1699.     end else if cur = 'rewrite' then begin
  1700.         next;
  1701.         rewritefile_;
  1702.     end else if cur = 'continue' then begin
  1703.         next;
  1704.         jsln('continue;');
  1705.         semicolon_;
  1706.     end else if cur = 'close' then begin
  1707.         next;
  1708.         closefile_;
  1709.     end else if cur = 'forward' then begin
  1710.         next;
  1711.         semicolon_;
  1712.     end else if cur = 'brace_{' then begin
  1713.         js(' /* ');
  1714.         next;
  1715.         while (node <> nil) and (cur <> 'brace_}') do begin
  1716.             js(val);
  1717.             next;
  1718.         end;
  1719.         jsln(' */');
  1720.         next;
  1721.     end else begin     
  1722.         write(outfile, '? line_' + val);
  1723.         error;
  1724.     end;
  1725. end;
  1726.  
  1727.  
  1728.  
  1729. procedure main_;
  1730. var
  1731.     tNode : pNodeList;
  1732.     readlnIt : integer;
  1733. begin
  1734.     if cur = 'begin' then begin
  1735.         varBase := 'this.';
  1736.         jsln('$main:function() {');
  1737.         next;
  1738.  
  1739.         tNode := funcList.first;
  1740.         while (tNode <> nil) do begin
  1741.             jsln(gl + '.funcs.' + tNode^.value + ' = pas.' + main + '.' + tNode^.value + ';');
  1742.             nextLL(tNode);
  1743.         end;
  1744.  
  1745.         tNode := recordVarList.first;
  1746.         while (tNode <> nil) do begin
  1747.             jsln(gl + '.' + tNode^.value + '.val = ' + gl + '.' + tNode^.token + '.f({});');
  1748.             nextLL(tNode);
  1749.         end;
  1750.  
  1751.         while (node <> nil) and (cur <> 'end') do begin
  1752.             line_;
  1753.         end;
  1754.        
  1755.         for readlnIt := 1 to readlnCount do begin
  1756.             jsln('});  // readln');
  1757.         end;
  1758.  
  1759.         if node = nil then begin
  1760.             write(outfile, 'error main node nil');
  1761.             error;
  1762.         end else if cur = 'end' then begin
  1763.             next;
  1764.             if cur <> 'dot' then begin
  1765.                 write(outfile, 'error main no dot in finish');
  1766.                 error;
  1767.             end else begin
  1768.                 next;
  1769.             end;
  1770.         end else begin
  1771.             write(outfile, 'error main no end in finish');
  1772.             error;
  1773.         end;
  1774.  
  1775.         jsln('}');
  1776.     end else begin
  1777.         write(outfile, 'error main no start with begin');
  1778.         error;
  1779.     end;
  1780. end;
  1781.  
  1782.  
  1783. procedure program_;
  1784. begin
  1785.     if cur = 'program' then begin
  1786.         next;
  1787.         if cur = 'variable' then begin
  1788.             main := val;
  1789.             gl := '$gl_' + main;
  1790.             jsln('pas.' + val + ' = {');
  1791.             next;
  1792.         end else begin
  1793.             write(outfile, 'error program no program name');
  1794.             error;
  1795.         end;
  1796.         semicolon_;
  1797.         header_;
  1798.         main_;
  1799.         jsln('}');
  1800.         jsln(gl + ' = pas.' + main + ';');
  1801.         jsln(gl + '.funcs = {};');
  1802.     end else begin
  1803.         write(outfile, 'error program_');
  1804.         error;
  1805.     end;
  1806.     if node <> nil then begin
  1807.         write(outfile, 'error program node is not nil');
  1808.         error;
  1809.     end;
  1810. end;
  1811.  
  1812.  
  1813. procedure start;
  1814. begin
  1815.  
  1816.     varBase := '';
  1817.  
  1818.     newLL(funcList);
  1819.     newLL(fileList);
  1820.     newLL(localVar);
  1821.     newLL(globalVar);
  1822.     newLL(recordList);
  1823.     newLL(recordVarList);
  1824.    
  1825.     noFuncFlag := 0;
  1826.  
  1827.    
  1828.    
  1829.     program_;
  1830.  
  1831. end;
  1832.  
  1833.  
  1834. procedure openfile;
  1835. begin
  1836.     Assign(jsfile, 'js.txt');
  1837.     Rewrite(jsfile);
  1838. end;
  1839.  
  1840. procedure closefile;
  1841. begin
  1842.     Close(jsfile);
  1843. end;
  1844.  
  1845.  
  1846. begin
  1847.     clrscr;
  1848.    
  1849.     {writeln(sizeof(tnodelist));}
  1850.     error_count := 0;
  1851.     readlnCount := 0;
  1852.  
  1853.     newLL(list);
  1854.  
  1855.     writeln('start');
  1856.  
  1857.     readfile(list);
  1858.  
  1859.     itLL := list.first;
  1860.  
  1861.     Assign(Outfile, 'out.txt');
  1862.     Rewrite(Outfile);
  1863.  
  1864.  
  1865.  
  1866.     while itLL <> nil do begin
  1867.         writeln(Outfile, itLL^.value,' -> ', itLL^.token);
  1868.         nextLL(itLL);
  1869.     end;
  1870.  
  1871.  
  1872.     openfile;
  1873.  
  1874.     jsln('pas = {}');
  1875.     node := list.first;
  1876.  
  1877.  
  1878.  
  1879.     start;
  1880.  
  1881.  
  1882.     jsln('function main() {');
  1883.     jsln('initConsole();');
  1884.     js('pas.');
  1885.     js(main);
  1886.     jsln('.$main();');
  1887.     jsln('}');
  1888.    
  1889.    
  1890.     writeln(Outfile, 'errors: ', error_count);
  1891.    
  1892.    
  1893.     Close(Outfile);
  1894.     closefile;
  1895.  
  1896.    
  1897.     writeln('errors: ', error_count);
  1898.     writeln('finish');
  1899.     readln;
  1900. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement