pbase.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. files,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. type
  33. tblock_type = (bt_general,bt_type,bt_const);
  34. var
  35. { contains the current token to be processes }
  36. token : ttoken;
  37. {$ifdef UseTokenInfo}
  38. tokeninfo : ptokeninfo;
  39. {$endif UseTokenInfo}
  40. { size of data segment, set by proc_unit or proc_program }
  41. datasize : longint;
  42. { for operators }
  43. optoken : ttoken;
  44. opsym : pvarsym;
  45. { symtable were unit references are stored }
  46. refsymtable : psymtable;
  47. { true, if only routine headers should be }
  48. { parsed }
  49. parse_only : boolean;
  50. { true, if we are in a except block }
  51. in_except_block : boolean;
  52. { type of currently parsed block }
  53. { isn't full implemented (FK) }
  54. block_type : tblock_type;
  55. { true, if we should ignore an equal in const x : 1..2=2 }
  56. ignore_equal : boolean;
  57. { consumes token i, if the current token is unequal i }
  58. { a syntax error is written }
  59. procedure consume(i : ttoken);
  60. { consumes all tokens til atoken (for error recovering }
  61. procedure consume_all_until(atoken : ttoken);
  62. { consumes tokens while they are semicolons }
  63. procedure emptystats;
  64. { reads a list of identifiers into a string container }
  65. function idlist : pstringcontainer;
  66. { inserts the symbols of sc in st with def as definition }
  67. { sc is disposed }
  68. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  69. implementation
  70. { consumes token i, if the current token is unequal i }
  71. { a syntax error is written }
  72. procedure consume(i : ttoken);
  73. { generates a syntax error message }
  74. procedure syntaxerror(const s : string);
  75. begin
  76. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  77. end;
  78. { This is changed since I changed the order of token
  79. in cobjects.pas for operator overloading !!!! }
  80. { ttoken = (PLUS,MINUS,STAR,SLASH,EQUAL,GT,
  81. LT,LTE,GTE,SYMDIF,STARSTAR,ASSIGNMENT,CARET,
  82. LECKKLAMMER,RECKKLAMMER,
  83. POINT,COMMA,LKLAMMER,RKLAMMER,COLON,SEMICOLON,
  84. KLAMMERAFFE,UNEQUAL,POINTPOINT,
  85. ID,REALNUMBER,_EOF,INTCONST,CSTRING,CCHAR,DOUBLEADDR,}
  86. const tokens : array[PLUS..DOUBLEADDR] of string[12] = (
  87. '+','-','*','/','=','>','<','>=','<=','is','as','in',
  88. '><','**',':=','^','<>','[',']','.',',','(',')',':',';',
  89. '@','..',
  90. 'identifier','const real.','end of file',
  91. 'ord const','const string','const char','@@');
  92. var
  93. j : integer;
  94. begin
  95. {$ifndef UseTokenInfo}
  96. if token<>i then
  97. begin
  98. if i<_AND then
  99. syntaxerror(tokens[i])
  100. else
  101. begin
  102. { um die ProgrammgrӇe klein zu halten, }
  103. { wird f�r ein Schl�sselwort-Token der }
  104. { "Text" in der Schl�sselworttabelle }
  105. { des Scanners nachgeschaut }
  106. for j:=1 to anz_keywords do
  107. if keyword_token[j]=i then
  108. syntaxerror(keyword[j])
  109. end;
  110. end
  111. else
  112. token:=yylex;
  113. {$else UseTokenInfo}
  114. if token<>i then
  115. begin
  116. if i<_AND then
  117. syntaxerror(tokens[i])
  118. else
  119. begin
  120. { um die ProgrammgrӇe klein zu halten, }
  121. { wird f�r ein Schl�sselwort-Token der }
  122. { "Text" in der Schl�sselworttabelle }
  123. { des Scanners nachgeschaut }
  124. for j:=1 to anz_keywords do
  125. if keyword_token[j]=i then
  126. syntaxerror(keyword[j])
  127. end;
  128. end
  129. else
  130. begin
  131. if assigned(tokeninfo) then
  132. dispose(tokeninfo);
  133. tokeninfo:=yylex;
  134. token:=tokeninfo^.token;
  135. end;
  136. {$endif UseTokenInfo}
  137. end;
  138. procedure consume_all_until(atoken : ttoken);
  139. begin
  140. {$ifndef UseTokenInfo}
  141. while (token<>atoken) and (token<>_EOF) do
  142. consume(token);
  143. { this will create an error if the token is _EOF }
  144. if token<>atoken then
  145. consume(atoken);
  146. {$else UseTokenInfo}
  147. while (token<>atoken) and (token<>_EOF) do
  148. consume(token);
  149. { this will create an error if the token is _EOF }
  150. if token<>atoken then
  151. consume(atoken);
  152. {$endif UseTokenInfo}
  153. { this error is fatal as we have read the whole file }
  154. Message(scan_f_end_of_file);
  155. end;
  156. procedure emptystats;
  157. begin
  158. while token=SEMICOLON do
  159. consume(SEMICOLON);
  160. end;
  161. { reads a list of identifiers into a string container }
  162. function idlist : pstringcontainer;
  163. var
  164. sc : pstringcontainer;
  165. begin
  166. sc:=new(pstringcontainer,init);
  167. repeat
  168. {$ifndef UseTokenInfo}
  169. sc^.insert(pattern);
  170. {$else UseTokenInfo}
  171. sc^.insert_with_tokeninfo(pattern,
  172. tokeninfo^.fi);
  173. {$endif UseTokenInfo}
  174. consume(ID);
  175. if token=COMMA then consume(COMMA)
  176. else break
  177. until false;
  178. idlist:=sc;
  179. end;
  180. { inserts the symbols of sc in st with def as definition }
  181. { sc is disposed }
  182. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  183. var
  184. s : string;
  185. {$ifdef UseTokenInfo}
  186. filepos : tfileposinfo;
  187. ss : pvarsym;
  188. {$endif UseTokenInfo}
  189. begin
  190. {$ifdef UseTokenInfo}
  191. s:=sc^.get_with_tokeninfo(filepos);
  192. {$else UseTokenInfo}
  193. s:=sc^.get;
  194. {$endif UseTokenInfo}
  195. while s<>'' do
  196. begin
  197. {$ifndef UseTokenInfo}
  198. st^.insert(new(pvarsym,init(s,def)));
  199. {$else UseTokenInfo}
  200. ss:=new(pvarsym,init(s,def));
  201. ss^.line_no:=filepos.line;
  202. st^.insert(ss);
  203. {$endif UseTokenInfo}
  204. { static data fields are inserted in the globalsymtable }
  205. if (st^.symtabletype=objectsymtable) and
  206. ((current_object_option and sp_static)<>0) then
  207. begin
  208. s:=lowercase(st^.name^)+'_'+s;
  209. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  210. end;
  211. {$ifdef UseTokenInfo}
  212. s:=sc^.get_with_tokeninfo(filepos);
  213. {$else UseTokenInfo}
  214. s:=sc^.get;
  215. {$endif UseTokenInfo}
  216. end;
  217. dispose(sc,done);
  218. end;
  219. end.
  220. {
  221. $Log$
  222. Revision 1.4 1998-04-30 15:59:41 pierre
  223. * GDB works again better :
  224. correct type info in one pass
  225. + UseTokenInfo for better source position
  226. * fixed one remaining bug in scanner for line counts
  227. * several little fixes
  228. Revision 1.3 1998/04/29 10:33:57 pierre
  229. + added some code for ansistring (not complete nor working yet)
  230. * corrected operator overloading
  231. * corrected nasm output
  232. + started inline procedures
  233. + added starstarn : use ** for exponentiation (^ gave problems)
  234. + started UseTokenInfo cond to get accurate positions
  235. Revision 1.2 1998/04/07 22:45:05 florian
  236. * bug0092, bug0115 and bug0121 fixed
  237. + packed object/class/array
  238. Revision 1.1.1.1 1998/03/25 11:18:14 root
  239. * Restored version
  240. Revision 1.9 1998/03/10 01:17:23 peter
  241. * all files have the same header
  242. * messages are fully implemented, EXTDEBUG uses Comment()
  243. + AG... files for the Assembler generation
  244. Revision 1.8 1998/03/06 00:52:40 peter
  245. * replaced all old messages from errore.msg, only ExtDebug and some
  246. Comment() calls are left
  247. * fixed options.pas
  248. Revision 1.7 1998/03/02 01:48:59 peter
  249. * renamed target_DOS to target_GO32V1
  250. + new verbose system, merged old errors and verbose units into one new
  251. verbose.pas, so errors.pas is obsolete
  252. Revision 1.6 1998/02/16 12:51:38 michael
  253. + Implemented linker object
  254. Revision 1.5 1998/02/13 10:35:22 daniel
  255. * Made Motorola version compilable.
  256. * Fixed optimizer
  257. Revision 1.4 1998/02/12 11:50:24 daniel
  258. Yes! Finally! After three retries, my patch!
  259. Changes:
  260. Complete rewrite of psub.pas.
  261. Added support for DLL's.
  262. Compiler requires less memory.
  263. Platform units for each platform.
  264. Revision 1.3 1998/01/13 17:13:08 michael
  265. * File time handling and file searching is now done in an OS-independent way,
  266. using the new file treating functions in globals.pas.
  267. Revision 1.2 1998/01/09 09:09:58 michael
  268. + Initial implementation, second try
  269. }