pbase.pas 9.8 KB

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