2
0

pbase.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Contains some helper routines for the parser
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pbase;
  19. interface
  20. uses
  21. cobjects,globals,scanner,symtable,systems,verbose;
  22. const
  23. { forward types should only be possible inside }
  24. { a TYPE statement, this crashed the compiler }
  25. { when trying to dispose local symbols }
  26. typecanbeforward : boolean = false;
  27. { true, if we are after an assignement }
  28. afterassignment : boolean = false;
  29. { sspecial for handling procedure vars }
  30. getprocvar : boolean = false;
  31. getprocvardef : pprocvardef = nil;
  32. var
  33. { contains the current token to be processes }
  34. token : ttoken;
  35. { size of data segment, set by proc_unit or proc_program }
  36. datasize : longint;
  37. { for operators }
  38. optoken : ttoken;
  39. opsym : pvarsym;
  40. { symtable were unit references are stored }
  41. refsymtable : psymtable;
  42. { true, if only routine headers should be }
  43. { parsed }
  44. parse_only : boolean;
  45. { true, if we are in a except block }
  46. in_except_block : boolean;
  47. { consumes token i, if the current token is unequal i }
  48. { a syntax error is written }
  49. procedure consume(i : ttoken);
  50. { consumes all tokens til atoken (for error recovering }
  51. procedure consume_all_until(atoken : ttoken);
  52. { consumes tokens while they are semicolons }
  53. procedure emptystats;
  54. { reads a list of identifiers into a string container }
  55. function idlist : pstringcontainer;
  56. { inserts the symbols of sc in st with def as definition }
  57. { sc is disposed }
  58. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  59. implementation
  60. { consumes token i, if the current token is unequal i }
  61. { a syntax error is written }
  62. procedure consume(i : ttoken);
  63. { generates a syntax error message }
  64. procedure syntaxerror(const s : string);
  65. begin
  66. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  67. end;
  68. { This is changed since I changed the order of token
  69. in cobjects.pas for operator overloading !!!! }
  70. { ttoken = (PLUS,MINUS,STAR,SLASH,EQUAL,GT,LT,LTE,GTE,SYMDIF,CARET,ASSIGNMENT,
  71. LECKKLAMMER,RECKKLAMMER,
  72. POINT,COMMA,LKLAMMER,RKLAMMER,COLON,SEMICOLON,
  73. KLAMMERAFFE,UNEQUAL,POINTPOINT,
  74. ID,REALNUMBER,_EOF,INTCONST,CSTRING,CCHAR,DOUBLEADDR,}
  75. const tokens : array[PLUS..DOUBLEADDR] of string[12] = (
  76. '+','-','*','/','=','>','<','>=','<=','is','as','in',
  77. '><','^',':=','<>','[',']','.',',','(',')',':',';',
  78. '@','..',
  79. 'identifier','const real.','end of file',
  80. 'ord const','const string','const char','@@');
  81. var
  82. j : integer;
  83. begin
  84. if token<>i then
  85. begin
  86. if i<_AND then
  87. syntaxerror(tokens[i])
  88. else
  89. begin
  90. { um die ProgrammgrӇe klein zu halten, }
  91. { wird f�r ein Schl�sselwort-Token der }
  92. { "Text" in der Schl�sselworttabelle }
  93. { des Scanners nachgeschaut }
  94. for j:=1 to anz_keywords do
  95. if keyword_token[j]=i then
  96. syntaxerror(keyword[j])
  97. end;
  98. end
  99. else
  100. token:=yylex;
  101. end;
  102. procedure consume_all_until(atoken : ttoken);
  103. begin
  104. while (token<>atoken) and (token<>_EOF) do
  105. consume(token);
  106. { this will create an error if the token is _EOF }
  107. if token<>atoken then
  108. consume(atoken);
  109. { this error is fatal as we have read the whole file }
  110. Message(scan_f_end_of_file);
  111. end;
  112. procedure emptystats;
  113. begin
  114. while token=SEMICOLON do
  115. consume(SEMICOLON);
  116. end;
  117. { reads a list of identifiers into a string container }
  118. function idlist : pstringcontainer;
  119. var
  120. sc : pstringcontainer;
  121. begin
  122. sc:=new(pstringcontainer,init);
  123. repeat
  124. sc^.insert(pattern);
  125. consume(ID);
  126. if token=COMMA then consume(COMMA)
  127. else break
  128. until false;
  129. idlist:=sc;
  130. end;
  131. { inserts the symbols of sc in st with def as definition }
  132. { sc is disposed }
  133. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  134. var
  135. s : string;
  136. begin
  137. s:=sc^.get;
  138. while s<>'' do
  139. begin
  140. st^.insert(new(pvarsym,init(s,def)));
  141. { static data fields are inserted in the globalsymtable }
  142. if (st^.symtabletype=objectsymtable) and
  143. ((current_object_option and sp_static)<>0) then
  144. begin
  145. s:=lowercase(st^.name^)+'_'+s;
  146. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  147. end;
  148. s:=sc^.get;
  149. end;
  150. dispose(sc,done);
  151. end;
  152. end.
  153. {
  154. $Log$
  155. Revision 1.1 1998-03-25 11:18:14 root
  156. Initial revision
  157. Revision 1.9 1998/03/10 01:17:23 peter
  158. * all files have the same header
  159. * messages are fully implemented, EXTDEBUG uses Comment()
  160. + AG... files for the Assembler generation
  161. Revision 1.8 1998/03/06 00:52:40 peter
  162. * replaced all old messages from errore.msg, only ExtDebug and some
  163. Comment() calls are left
  164. * fixed options.pas
  165. Revision 1.7 1998/03/02 01:48:59 peter
  166. * renamed target_DOS to target_GO32V1
  167. + new verbose system, merged old errors and verbose units into one new
  168. verbose.pas, so errors.pas is obsolete
  169. Revision 1.6 1998/02/16 12:51:38 michael
  170. + Implemented linker object
  171. Revision 1.5 1998/02/13 10:35:22 daniel
  172. * Made Motorola version compilable.
  173. * Fixed optimizer
  174. Revision 1.4 1998/02/12 11:50:24 daniel
  175. Yes! Finally! After three retries, my patch!
  176. Changes:
  177. Complete rewrite of psub.pas.
  178. Added support for DLL's.
  179. Compiler requires less memory.
  180. Platform units for each platform.
  181. Revision 1.3 1998/01/13 17:13:08 michael
  182. * File time handling and file searching is now done in an OS-independent way,
  183. using the new file treating functions in globals.pas.
  184. Revision 1.2 1998/01/09 09:09:58 michael
  185. + Initial implementation, second try
  186. }