parser.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. #include "std.h"
  2. #include "parser.h"
  3. #include "config.h"
  4. static const double PI=3.1415926535897932384626433832795;
  5. using namespace CG;
  6. void Parser::fail( const char *fmt,... ){
  7. char buf[256];
  8. va_list args;
  9. va_start( args,fmt );
  10. vsprintf( buf,fmt,args );
  11. source_info=toker->sourceInfo();
  12. ::fail( buf );
  13. }
  14. int Parser::curr(){
  15. return toker->curr();
  16. }
  17. int Parser::next(){
  18. return toker->next();
  19. }
  20. string Parser::text(){
  21. return toker->text();
  22. }
  23. bstring Parser::wtext(){
  24. if( toker->curr()==T_STRINGCONST ) return unescapeString( toker->wtext() );
  25. return toker->wtext();
  26. }
  27. void Parser::exp( int n ){
  28. fail( "Expecting %s but encountered %s",Toker::toString(n).c_str(),Toker::toString(curr()).c_str() );
  29. }
  30. void Parser::exp( string t ){
  31. fail( "Expecting %s but encountered %s",t.c_str(),Toker::toString(curr()).c_str() );
  32. }
  33. string Parser::parse( int n ){
  34. if( curr()!=n ) exp(n);
  35. string t=text();
  36. next();return t;
  37. }
  38. bool Parser::cparse( int n ){
  39. if( curr()!=n ) return false;
  40. next();return true;
  41. }
  42. bool Parser::pub(){
  43. return pub_ && block==mainFun;
  44. }
  45. void Parser::emitDebugInfo(){
  46. if( !block->debug_on ) return;
  47. DebugInfoStm *t=new DebugInfoStm;
  48. t->source_info=source_info;
  49. block->emit(t);
  50. }
  51. void Parser::emit( Stm *t,bool debugInfo ){
  52. if( debugInfo ) emitDebugInfo();
  53. t->source_info=source_info;
  54. block->emit(t);
  55. }
  56. void Parser::decl( Decl *d ){
  57. block->decl(d);
  58. if( pub() ) publish( d );
  59. }
  60. Type *Parser::parseLitType( Type *ty ){
  61. switch( curr() ){
  62. case '%':next();return cparse('%') ? Type::int64 : Type::int32;
  63. case '#':next();return cparse('#') ? Type::float64 : Type::float32;
  64. case '!':next();return Type::float64;
  65. case ':':next();break;
  66. default:return ty;
  67. }
  68. switch( curr() ){
  69. case T_BYTE:next();return Type::int8;
  70. case T_SHORT:next();return Type::int16;
  71. case T_INT:next();return Type::int32;
  72. case T_LONG:next();return Type::int64;
  73. case T_FLOAT:next();return Type::float32;
  74. case T_DOUBLE:next();return Type::float64;
  75. }
  76. fail( "Expecting literal type" );
  77. return 0;
  78. }
  79. Val *Parser::parseLitVal(){
  80. Val *v;
  81. Type *ty;
  82. switch( curr() ){
  83. case T_INTCONST:
  84. v=new Val( toint(text()) );
  85. ty=Type::int32;
  86. break;
  87. case T_FLOATCONST:
  88. v=new Val( tofloat(text()) );
  89. ty=Type::float32;
  90. break;
  91. default:
  92. exp( "integer or floating point literal value" );
  93. }
  94. next();
  95. ty=parseLitType( ty );
  96. return v->cast( ty );
  97. }
  98. CGExp *Parser::parseLitExp( Type *ty ){
  99. int sign=0;
  100. for(;;){
  101. if( cparse('+') ){
  102. }else if( cparse('-') ){
  103. sign=sign ? -sign : -1;
  104. }else{
  105. break;
  106. }
  107. }
  108. Val *v;
  109. switch( curr() ){
  110. case T_INTCONST:{
  111. int64 n=toint(text());
  112. if( sign<0 ) n=-n;
  113. v=new Val( n );
  114. next();
  115. break;
  116. }
  117. case T_FLOATCONST:{
  118. double n=tofloat(text());
  119. if( sign<0 ) n=-n;
  120. v=new Val( n );
  121. next();
  122. break;
  123. }
  124. case T_STRINGCONST:{
  125. if( sign ) exp( "Numeric or string literal value" );
  126. v=new Val( parseBString() );
  127. break;
  128. }
  129. default:
  130. exp( "Numeric or string literal value" );
  131. }
  132. ty=parseLitType( ty );
  133. v=v->cast( ty );
  134. return v->cg_exp;
  135. }
  136. string Parser::parseString(){
  137. string t=parse(T_STRINGCONST);
  138. return t.substr(1,t.size()-2);
  139. }
  140. bstring Parser::parseBString(){
  141. if( curr()!=T_STRINGCONST ) exp(T_STRINGCONST);
  142. bstring t=wtext();next();
  143. return t.substr(1,t.size()-2);
  144. }
  145. string Parser::parseIdent(){
  146. if( !import_nest ) return parse(T_IDENT);
  147. switch( curr() ){
  148. case T_OBJECT:case T_STRING:case T_NEW:case T_DELETE:case T_IDENT:
  149. break;
  150. default:
  151. fail( "Invalid import identifier: %s",Toker::toString(curr()).c_str() );
  152. }
  153. string id=text();
  154. next();
  155. return id;
  156. }
  157. string Parser::parseClassName(){
  158. string id=parseIdent();
  159. while( cparse('.') ) id+='.'+parseIdent();
  160. return id;
  161. }
  162. string Parser::parseModuleName(){
  163. string id=parseIdent();
  164. while( cparse('.') ) id+='.'+parseIdent();
  165. return tolower(id);
  166. }
  167. static void ass( bool t ){
  168. if( !t ) fail( "Error in import file" );
  169. }
  170. CGExp *Parser::parseCGExp(){
  171. int sgn=1;
  172. if( cparse('-') ) sgn=-1;
  173. if( curr()==T_INTCONST || curr()==T_FLOATCONST ){
  174. Val *v=parseLitVal();
  175. CGLit *t=v->cg_exp->lit();
  176. if( t->type==CG_INT64 ) return lit( sgn * t->int_value );
  177. if( t->type==CG_FLOAT64 ) return lit( sgn * t->float_value );
  178. if( t->type==CG_INT32 ) return lit( sgn * int(t->int_value ) );
  179. if( t->type==CG_FLOAT32 ) return lit( sgn * float(t->float_value) );
  180. assert(0);
  181. }else if( curr()==T_STRINGCONST ){
  182. return sym(parseString(),CG_IMPORT);
  183. }else if( cparse('$') ){
  184. return (new Val(parseBString()))->cg_exp;
  185. }
  186. string id=parseIdent();
  187. if( id=="nan" ){
  188. double zero=0.0;
  189. if( cparse('#') ) return lit( float(0.0/zero) );
  190. if( cparse('!') ) return lit( double(0.0/zero) );
  191. fail( "Nan error" );
  192. }
  193. if( id=="inf" ){
  194. double zero=0.0;
  195. if( cparse('#') ) return lit( float(sgn/zero) );
  196. if( cparse('!') ) return lit( double(sgn/zero) );
  197. fail( "Inf error" );
  198. }
  199. int ty=CG_INT32;
  200. if( cparse(':') ){
  201. string t=parseIdent();
  202. switch(t[0]){
  203. case 'b':ty=CG_INT8;break;
  204. case 's':ty=CG_INT16;break;
  205. case 'i':ty=CG_INT32;break;
  206. case 'l':ty=CG_INT64;break;
  207. case 'p':ty=CG_PTR;break;
  208. case 'f':ty=CG_FLOAT32;break;
  209. case 'd':ty=CG_FLOAT64;break;
  210. default:fail( "Unrecognized intermediate code data type" );
  211. }
  212. }
  213. parse('(');
  214. CGExp *exp=0;
  215. if( id=="mem" ){
  216. exp=parseCGExp();
  217. int n=0;
  218. if( curr()!=')' ){
  219. parse(',');
  220. n=parseLitVal()->cg_exp->lit()->int_value;
  221. }
  222. exp=CG::mem(ty,exp,n);
  223. }else{
  224. fail( "Unrecognized intermediate code expression" );
  225. }
  226. parse(')');
  227. return exp;
  228. }
  229. Decl *Parser::parseImportDecl(){
  230. string id=parseIdent();
  231. Type *ty=parseType();
  232. CGExp *cg=cparse(T_EQ) ? parseCGExp() : 0;
  233. return new Decl( id,ty,cg );
  234. }
  235. int Parser::parseCallConv(){
  236. if( curr()!=T_STRINGCONST ) return default_call_conv;
  237. string t=tolower(parseString());
  238. if( t=="c" ) return CG_CDECL;
  239. if( t=="blitz" ) return CG_CDECL;
  240. if( t=="os" ) t=env_platform;
  241. if( t=="macos" ) return CG_CDECL;
  242. if( t=="linux" ) return CG_CDECL;
  243. if( t=="win32" ) return CG_STDCALL;
  244. fail( "Unrecognized calling convention '%s'",t.c_str() );
  245. return 0;
  246. }
  247. void Parser::parseExtern(){
  248. next();
  249. ++extern_nest;
  250. int call_conv=default_call_conv;
  251. default_call_conv=parseCallConv();
  252. while( !cparse(T_ENDEXTERN) ){
  253. source_info=toker->sourceInfo();
  254. if( cparse('\n') ){
  255. }else if( curr()==T_CONST ){
  256. parseConstDecls();
  257. }else if( cparse(T_GLOBAL) ){
  258. do{
  259. string id=parseIdent();
  260. Type *ty=parseRefType();
  261. CGExp *cg=cparse(T_EQ) ? parseCGExp() : 0;
  262. emit( new ExternDeclStm(T_GLOBAL,id,ty,cg,pub()),false );
  263. }while( cparse(',') );
  264. }else if( cparse(T_FUNCTION) ){
  265. do{
  266. string id=parseIdent();
  267. ExpSeq *defs=new ExpSeq();
  268. fun_defaults=defs;
  269. FunType *ty=parseType()->funType();
  270. fun_defaults=0;
  271. if( !ty ) exp( "function type" );
  272. CGExp *cg=cparse(T_EQ) ? parseCGExp() : 0;
  273. Decl *d=new FunDecl( id,ty,cg,block,defs );
  274. decl(d);
  275. }while( cparse(',') );
  276. }else if( cparse(T_TYPE) ){
  277. string id=parseIdent(),super_id;
  278. if( cparse(T_EXTENDS) ) super_id=parseIdent();
  279. ClassType *class_ty=new ClassType( super_id,block,ClassType::EXTERN );
  280. while( !cparse(T_ENDTYPE) ){
  281. if( cparse('\n') ){
  282. }else if( cparse(T_FIELD) ){
  283. do{
  284. string id=parseIdent();
  285. Type *ty=parseRefType();
  286. class_ty->fields.push_back( new Decl(id,ty,0) );
  287. }while( cparse(',') );
  288. }else if( cparse(T_METHOD) ){
  289. string id=parseIdent();
  290. ExpSeq *defs=new ExpSeq();
  291. fun_defaults=defs;
  292. FunType *ty=parseType()->funType();
  293. fun_defaults=0;
  294. if( !ty ) exp( "method type" );
  295. ty->attrs|=FunType::METHOD;
  296. CGExp *cg=cparse(T_EQ) ? parseCGExp() : 0;
  297. Decl *d=new FunDecl( id,ty,cg,block,defs );
  298. class_ty->methods.push_back( d );
  299. /*
  300. string id=parseIdent();
  301. FunType *ty=parseType()->funType();
  302. if( !ty ) exp( "method type" );
  303. ty->attrs|=FunType::METHOD;
  304. class_ty->methods.push_back( new Decl(id,ty,0) );
  305. */
  306. }else{
  307. exp( "field or method declaration" );
  308. }
  309. }
  310. decl( new Decl(id,class_ty,lit0) );
  311. }else{
  312. fail( "Syntax error in extern block - expecting Const, Global, Function or Type declaration" );
  313. }
  314. }
  315. default_call_conv=call_conv;
  316. --extern_nest;
  317. }
  318. Type *Parser::parseBaseType(){
  319. Scope *scope=block;
  320. if( import_module ) scope=import_module;
  321. switch( curr() ){
  322. case '!':next();return Type::float64;
  323. case '$':next();return Type::stringObject;
  324. case '@':if( next()!='@' ) return Type::int8;
  325. next();return Type::int16;
  326. case '%':if( next()!='%' ) return Type::int32;
  327. next();return Type::int64;
  328. case '#':if( next()!='#' ) return Type::float32;
  329. next();return Type::float64;
  330. case T_CSTRING:
  331. next();return Type::c_string;
  332. case T_WSTRING:
  333. next();return Type::w_string;
  334. case ':':
  335. switch( next() ){
  336. case T_BYTE:next();return Type::int8;
  337. case T_SHORT:next();return Type::int16;
  338. case T_INT:next();return Type::int32;
  339. case T_LONG:next();return Type::int64;
  340. case T_FLOAT:next();return Type::float32;
  341. case T_DOUBLE:next();return Type::float64;
  342. case T_OBJECT:next();return Type::objectObject;
  343. case T_STRING:next();return Type::stringObject;
  344. case T_IDENT:return new ObjectType( parseClassName(),scope );
  345. }
  346. exp(T_IDENT);
  347. case '^':
  348. if( import_nest ){
  349. next();
  350. string super_name=cparse(T_NULL) ? "" : parseClassName();
  351. parse('{');
  352. ClassType *ty=new ClassType( super_name,scope );
  353. while( !cparse('}') ){
  354. if( cparse('\n') ){
  355. }else if( curr()=='+' || curr()=='-' ){
  356. int tok=curr();next();
  357. Decl *d=parseImportDecl();
  358. FunType *f=d->val->type->funType();ass( !!f );
  359. if( tok=='-' ) f->attrs|=FunType::METHOD;
  360. ty->methods.push_back(d);
  361. }else if( cparse('.') ){
  362. Decl *d=parseImportDecl();
  363. ass( !d->val->cg_exp );
  364. ty->fields.push_back(d);
  365. }else{
  366. Decl *d=parseImportDecl();
  367. ass( !!d->val->cg_exp );
  368. ty->decls.push_back(d);
  369. }
  370. }
  371. if( curr()==T_IDENT ){
  372. string id=tolower(parseIdent());
  373. if( id.find('a')!=string::npos ) ty->attrs|=ClassType::ABSTRACT;
  374. if( id.find('f')!=string::npos ) ty->attrs|=ClassType::FINAL;
  375. if( id.find('e')!=string::npos ) ty->attrs|=ClassType::EXTERN;
  376. }
  377. return ty;
  378. }
  379. break;
  380. }
  381. return Type::int32;
  382. }
  383. FunType *Parser::parseFunType( Type *ty ){
  384. parse('(');
  385. FunType *fun=new FunType(ty);
  386. fun->call_conv=default_call_conv;
  387. if( !cparse(')') ){
  388. do{
  389. CGExp *cg=0;
  390. string id=parseIdent();
  391. ExpSeq *tmp_defaults=fun_defaults;
  392. fun_defaults=0;
  393. Type *ty=parseType();
  394. fun_defaults=tmp_defaults;
  395. if( cparse(T_VAR) ) ty=new VarType(ty);
  396. if( fun_defaults ){
  397. if( cparse(T_EQ) ) fun_defaults->push_back( parseExp() );
  398. else fun_defaults->push_back(0);
  399. }else if( cparse(T_EQ) ){
  400. if( !import_nest ){
  401. fail( "Function argument defaults can only be specified in function declaration" );
  402. // cg=parseLitExp( ty );
  403. }else{
  404. cg=parseCGExp();
  405. }
  406. }
  407. fun->args.push_back( new Decl(id,ty,cg) );
  408. }while( cparse(',') );
  409. parse(')');
  410. }
  411. if( import_nest ){
  412. if( curr()==T_IDENT ){
  413. string id=tolower(parseIdent());
  414. if( id.find('a')!=string::npos ) fun->attrs|=FunType::ABSTRACT;
  415. if( id.find('f')!=string::npos ) fun->attrs|=FunType::FINAL;
  416. if( id.find('s')!=string::npos ) fun->call_conv=CG_STDCALL;
  417. }
  418. }else if( curr()==T_STRINGCONST ){
  419. fun->call_conv=parseCallConv();
  420. }
  421. return fun;
  422. }
  423. int Parser::arrayDeclDims( string t ){
  424. assert( t.size()>1 && t[0]=='[' && t[t.size()-1]==']' );
  425. int n=1;
  426. for( int i=1;i<t.size()-1;++i ){
  427. if( t[i]==',' ) ++n;
  428. }
  429. return n;
  430. }
  431. Type *Parser::parseType(){
  432. Type *ty;
  433. if( strictMode>1 && !import_nest ){
  434. FunType *fun;
  435. switch( curr() ){
  436. case '(':
  437. fun=parseFunType( Type::int32 );
  438. fun->attrs|=FunType::VOIDFUN;
  439. ty=fun;
  440. break;
  441. case '%':case ':':
  442. ty=parseBaseType();
  443. break;
  444. default:
  445. ty=parseBaseType();
  446. if( ty==Type::int32 ) fail( "Missing type specifier" );
  447. }
  448. }else{
  449. ty=parseBaseType();
  450. }
  451. for(;;){
  452. if( curr()=='(' ){
  453. if( fun_defaults ){
  454. for( int i=0;i<fun_defaults->size();++i ){
  455. if( (*fun_defaults)[i] ){
  456. fail( "Illegal default function parameter" );
  457. }
  458. }
  459. fun_defaults->clear();
  460. }
  461. ty=parseFunType(ty);
  462. continue;
  463. }
  464. if( ty->cstringType() || ty->wstringType() ) break;
  465. if( curr()==T_ARRAYDECL ){
  466. if( !import_nest ) ty=new RefType(ty);
  467. ArrayType *arr=new ArrayType( ty,arrayDeclDims( text() ) );//text().size()-1 );
  468. next();
  469. ty=arr;
  470. }else if( import_nest && cparse('&') ){
  471. ty=new RefType(ty);
  472. }else if( import_nest && cparse('*') ){
  473. ty=new PtrType(ty);
  474. }else if( cparse(T_PTR) ){
  475. ty=new PtrType( ty );
  476. }else{
  477. break;
  478. }
  479. }
  480. return ty;
  481. }
  482. RefType *Parser::parseRefType(){
  483. Type *ty=parseType();
  484. if( ty->cstringType() || ty->wstringType() ) fail( "Illegal variable type" );
  485. return new RefType( ty );
  486. }
  487. Exp *Parser::parseCastExp( Type *ty ){
  488. for(;;){
  489. if( curr()==T_ARRAYDECL ){
  490. ty=new ArrayType( new RefType( ty ),arrayDeclDims( text() ) );
  491. next();
  492. }else if( cparse(T_PTR) ){
  493. ty=new PtrType(ty);
  494. }else{
  495. break;
  496. }
  497. }
  498. if( curr()=='(' ){
  499. return parsePostExp( new CastExp( ty,parsePriExp() ) );
  500. }
  501. return new CastExp( ty,parsePreExp() );
  502. }
  503. Exp *Parser::parseIdentExp(){
  504. string id=parseIdent();
  505. Type *ty=0;
  506. switch( curr() ){
  507. case '@':case '%':case '#':case '!':case '$':case ':':ty=parseBaseType();break;
  508. }
  509. return new IdentExp(id,ty);
  510. }
  511. ArrayExp *Parser::parseArrayExp( Type *ty ){
  512. parse('[');
  513. ArrayExp *exp=new ArrayExp(ty);
  514. do{
  515. exp->dims.push_back( parseExp() );
  516. }while(cparse(','));
  517. parse(']');
  518. return exp;
  519. }
  520. Exp *Parser::parseArrayDataExp(){
  521. parse('[');
  522. if( cparse(']') ) return new NullExp();
  523. ArrayDataExp *e=new ArrayDataExp();
  524. do{
  525. e->exps.push_back( parseExp() );
  526. }while( cparse(',') );
  527. parse(']');
  528. return e;
  529. }
  530. Exp *Parser::parseNewExp(){
  531. Type *t=0;
  532. switch( curr() ){
  533. case T_BYTE:t=Type::int8;break;
  534. case T_SHORT:t=Type::int16;break;
  535. case T_INT:t=Type::int32;break;
  536. case T_LONG:t=Type::int64;break;
  537. case T_FLOAT:t=Type::float32;break;
  538. case T_DOUBLE:t=Type::float64;break;
  539. case T_STRING:t=Type::stringObject;break;
  540. case T_OBJECT:t=Type::objectObject;break;
  541. }
  542. if( t ){
  543. next();
  544. }else{
  545. Exp *e=parsePriExp();
  546. if( curr()!=T_ARRAYDECL && curr()!=T_PTR && curr()!='[' ){
  547. return new NewExp(e);
  548. }
  549. IdentExp *id=dynamic_cast<IdentExp*>(e);
  550. if( !id ) fail( "Expecting element type" );
  551. t=new ObjectType( id->ident,block );
  552. }
  553. for(;;){
  554. if( curr()==T_ARRAYDECL ){
  555. t=new ArrayType( t,arrayDeclDims( text() ) );
  556. next();
  557. }else if( cparse(T_PTR) ){
  558. t=new PtrType( t );
  559. }else{
  560. break;
  561. }
  562. }
  563. parse('[');
  564. ArrayExp *exp=new ArrayExp(t);
  565. do{
  566. exp->dims.push_back( parseExp() );
  567. }while(cparse(','));
  568. parse(']');
  569. return exp;
  570. }
  571. Exp *Parser::parsePriExp(){
  572. int t;
  573. Type *ty;
  574. Exp *e=0,*lhs,*rhs;
  575. //Yuck! This extremely nasty hack allows for 'bracketless' fun invocation by statements...
  576. if( primary ){
  577. e=primary;
  578. primary=0;
  579. return e;
  580. }
  581. switch( int op=curr() ){
  582. case '(':
  583. next();
  584. e=parseExp();
  585. parse(')');
  586. return e;
  587. case '[':
  588. return parseArrayDataExp();
  589. case T_IDENT:
  590. return parseIdentExp();
  591. case T_INTCONST:case T_FLOATCONST:
  592. return new ValExp( parseLitVal() );
  593. case T_STRINGCONST:
  594. return new ValExp( new Val(parseBString()) );
  595. case T_TRUE:
  596. next();return new ValExp( new Val(1) );
  597. case T_FALSE:
  598. next();return new ValExp( new Val(0) );
  599. case T_PI:
  600. next();return new ValExp( new Val((double)PI) );
  601. case T_NULL:
  602. next();return new NullExp();
  603. case T_SELF:
  604. next();return new SelfExp();
  605. case T_SUPER:
  606. next();return new SuperExp();
  607. case T_NEW:
  608. next();return parseNewExp();
  609. case T_LEN:
  610. case T_CHR:
  611. case T_ASC:
  612. case T_INCBINPTR:
  613. case T_INCBINLEN:
  614. ty=0;
  615. switch( next() ){
  616. case '@':case '%':case '#':case '!':case '$':case ':':
  617. if( op!=T_VARPTR ) ty=parseBaseType();
  618. break;
  619. }
  620. return new IntrinsicExp( op,ty,parsePriExp() );
  621. case '.':
  622. next();return new GlobalExp( parseIdent() );
  623. case T_MIN:case T_MAX:
  624. t=curr();next();
  625. parse('(');lhs=parseExp();parse(',');rhs=parseExp();parse(')');
  626. return new ArithExp( t,lhs,rhs );
  627. }
  628. exp( "expression" );
  629. return 0;
  630. }
  631. Exp *Parser::parsePostExp( Exp *t ){
  632. if( !t ) t=parsePriExp();
  633. for(;;){
  634. if( cparse('.') ){
  635. t=new MemberExp( t,parsePriExp() );
  636. }else if( cparse('(') ){
  637. InvokeExp *i=new InvokeExp(t);
  638. if( !cparse(')') ){
  639. do{
  640. if( curr()==',' ) i->seq.push_back(0);
  641. else i->seq.push_back(parseExp());
  642. }while( cparse(',') );
  643. parse(')');
  644. }
  645. t=i;
  646. }else if( cparse('[') ){
  647. int c=curr();
  648. if( c==T_DOTDOT ){
  649. if( next()==']' ) t=new SliceExp(c,t,0,0);
  650. else t=new SliceExp(c,t,0,parseExp());
  651. }else{
  652. Exp *e=parseExp();
  653. int c=curr();
  654. if( c==T_DOTDOT ){
  655. if( next()==']' ) t=new SliceExp(c,t,e,0);
  656. else t=new SliceExp(c,t,e,parseExp());
  657. }else{
  658. IndexExp *i=new IndexExp(t);
  659. i->seq.push_back(e);
  660. while( cparse(',') ) i->seq.push_back(parseExp());
  661. t=i;
  662. }
  663. }
  664. parse(']');
  665. }else if( curr()==T_ARRAYDECL || curr()==T_PTR ){
  666. IdentExp *e=dynamic_cast<IdentExp*>(t);
  667. if( !e ) fail( "Expecting identifier" );
  668. ObjectType *ty=new ObjectType( e->ident,block );
  669. t=parseCastExp(ty);
  670. }else{
  671. return t;
  672. }
  673. }
  674. }
  675. Exp *Parser::parsePreExp(){
  676. Type *ty;
  677. if( primary ) return parsePostExp();
  678. switch( int op=curr() ){
  679. case T_NOT:
  680. next();return new NotExp( parsePreExp() );
  681. case '-':case '+':case '~':case T_ABS:case T_SGN:
  682. next();return new UnaryExp( op,parsePreExp() );
  683. case T_SIZEOF:
  684. case T_VARPTR:
  685. ty=0;
  686. switch( next() ){
  687. case '@':case '%':case '#':case '!':case '$':case ':':
  688. if( op!=T_VARPTR ) ty=parseBaseType();
  689. break;
  690. }
  691. return new IntrinsicExp( op,ty,parsePostExp() );
  692. case T_BYTE:
  693. next();return parseCastExp( Type::int8 );
  694. case T_SHORT:
  695. next();return parseCastExp( Type::int16 );
  696. case T_INT:
  697. next();return parseCastExp( Type::int32 );
  698. case T_LONG:
  699. next();return parseCastExp( Type::int64 );
  700. case T_FLOAT:
  701. next();return parseCastExp( Type::float32 );
  702. case T_DOUBLE:
  703. next();return parseCastExp( Type::float64 );
  704. case T_STRING:
  705. if( next()=='.' ) return parsePostExp( new ValExp(Type::stringClass) );
  706. return parseCastExp( Type::stringObject );
  707. case T_OBJECT:
  708. if( next()=='.' ) return parsePostExp( new ValExp(Type::objectClass) );
  709. return parseCastExp( Type::objectObject );
  710. }
  711. return parsePostExp();
  712. }
  713. Exp *Parser::parsePowExp(){
  714. Exp *t=parsePreExp();
  715. for(;;){
  716. switch( int op=curr() ){
  717. case '^':
  718. next();
  719. t=new ArithExp( op,t,parsePreExp() );
  720. break;
  721. default:
  722. return t;
  723. }
  724. }
  725. }
  726. Exp *Parser::parseFactExp(){
  727. Exp *t=parsePowExp();
  728. for(;;){
  729. switch( int op=curr() ){
  730. case '*':case '/':case T_MOD:
  731. next();
  732. t=new ArithExp( op,t,parsePowExp() );
  733. break;
  734. case T_SHL:case T_SHR:case T_SAR:
  735. next();
  736. t=new BitwiseExp( op,t,parsePowExp() );
  737. break;
  738. default:
  739. return t;
  740. }
  741. }
  742. }
  743. Exp *Parser::parseTermExp(){
  744. Exp *t=parseFactExp();
  745. for(;;){
  746. switch( int op=curr() ){
  747. case '+':case '-':
  748. next();
  749. t=new ArithExp( op,t,parseFactExp() );
  750. break;
  751. default:
  752. return t;
  753. }
  754. }
  755. }
  756. Exp *Parser::parseBitwiseExp(){
  757. Exp *t=parseTermExp();
  758. for(;;){
  759. switch( int op=curr() ){
  760. case '&':case '|':case '~':
  761. next();
  762. t=new BitwiseExp( op,t,parseTermExp() );
  763. break;
  764. default:
  765. return t;
  766. }
  767. }
  768. }
  769. Exp *Parser::parseCmpExp(){
  770. Exp *t=parseBitwiseExp();
  771. for(;;){
  772. switch( int op=curr() ){
  773. case T_LT:case T_EQ:case T_GT:case T_LE:case T_GE:case T_NE:
  774. next();
  775. t=new CmpExp( op,t,parseBitwiseExp() );
  776. break;
  777. default:
  778. return t;
  779. }
  780. }
  781. }
  782. Exp *Parser::parseShortCircExp(){
  783. Exp *t=parseCmpExp();
  784. for(;;){
  785. switch( int op=curr() ){
  786. case T_AND:case T_OR:
  787. next();
  788. t=new ShortCircExp( op,t,parseCmpExp() );
  789. break;
  790. default:
  791. return t;
  792. }
  793. }
  794. }
  795. Exp *Parser::parseExp(){
  796. Exp *e=parseShortCircExp();
  797. return e;
  798. }
  799. string Parser::parseMetaData(){
  800. if( !cparse('{') ) return "";
  801. string meta;
  802. while( curr()==T_IDENT ){
  803. string id=parseIdent(),t;
  804. if( cparse( T_EQ ) ){
  805. switch( curr() ){
  806. case T_INTCONST:
  807. case T_FLOATCONST:
  808. case T_STRINGCONST:
  809. t=text();
  810. next();
  811. break;
  812. default:
  813. fail( "Meta data must be literal constant" );
  814. }
  815. }else{
  816. t="1";
  817. }
  818. if( meta.size() ) meta+=" ";
  819. meta+=id+"="+t;
  820. }
  821. parse( '}' );
  822. return meta;
  823. }
  824. void Parser::addMetaData( const vector<Decl*> &decls ){
  825. string meta=parseMetaData();
  826. for( vector<Decl*>::const_iterator it=decls.begin();it!=decls.end();++it ){
  827. (*it)->setMetaData( meta );
  828. }
  829. }
  830. Decl *Parser::parseInitDecl( Exp **init ){
  831. string id=parseIdent();
  832. Type *ty=parseRefType();
  833. Exp *_init=0;
  834. if( cparse(T_EQ) ){
  835. _init=parseExp();
  836. }else if( cparse('[') ){
  837. ArrayExp *exp=new ArrayExp(ty);
  838. do{
  839. exp->dims.push_back( parseExp() );
  840. }while( cparse(',') );
  841. parse(']');
  842. ty=new RefType( new ArrayType(ty,exp->dims.size()) );
  843. _init=exp;
  844. }else{
  845. _init=new NullExp();
  846. }
  847. *init=_init;
  848. return new Decl( id,ty,0 );
  849. }
  850. void Parser::parseLocalDecls(){
  851. next();
  852. emitDebugInfo();
  853. vector<Decl*> decls;
  854. do{
  855. Exp *e;
  856. Decl *d=parseInitDecl( &e );
  857. decls.push_back( d );
  858. emit( new LocalDeclStm(d->ident,d->val->type,e),false );
  859. }while( cparse(',') );
  860. addMetaData( decls );
  861. }
  862. void Parser::parseGlobalDecls(){
  863. next();
  864. emitDebugInfo();
  865. vector<Decl*> decls;
  866. do{
  867. Exp *e;
  868. Decl *d=parseInitDecl( &e );
  869. decls.push_back( d );
  870. emit( new GlobalDeclStm(d->ident,d->val->type,e,pub()),false );
  871. }while( cparse(',') );
  872. addMetaData( decls );
  873. }
  874. void Parser::parseConstDecls(){
  875. next();
  876. vector<Decl*> decls;
  877. do{
  878. string id=parseIdent();
  879. Type *ty=parseType();
  880. if( !cparse(T_EQ) ) fail( "Constants must be initialized" );
  881. Decl *d=new ConstDecl( id,ty,block,parseExp() );
  882. decls.push_back( d );
  883. decl( d );
  884. }while( cparse(',') );
  885. addMetaData( decls );
  886. }
  887. void Parser::parseTypeDecl(){
  888. next();
  889. string id=parseIdent();
  890. string super_name=cparse(T_EXTENDS) ? parseClassName() : "Object";
  891. int attrs=0;
  892. if( cparse(T_FINAL) ){
  893. attrs=ClassType::FINAL;
  894. }else if( cparse(T_ABSTRACT) ){
  895. attrs=ClassType::ABSTRACT;
  896. }
  897. string meta=parseMetaData();
  898. if( !pub() ) attrs|=ClassType::PRIVATE;
  899. ClassType *class_type=new ClassType( super_name,block,attrs );
  900. ClassBlock *class_block=new ClassBlock( block,id,class_type );
  901. class_block->class_decl->setMetaData( meta );
  902. block=class_block;
  903. while( !cparse(T_ENDTYPE) ){
  904. source_info=toker->sourceInfo();
  905. switch( curr() ){
  906. case '\n':case ';':next();break;
  907. case T_FIELD:parseFieldDecls();break;
  908. case T_CONST:parseConstDecls();break;
  909. case T_GLOBAL:parseGlobalDecls();break;
  910. case T_METHOD:parseFunDecl( FunType::METHOD );break;
  911. case T_FUNCTION:parseFunDecl( 0 );break;
  912. default:fail( "Syntax error in user defined type declaration" );
  913. }
  914. }
  915. block=block->outer;
  916. }
  917. void Parser::parseFieldDecls(){
  918. next();
  919. ClassBlock *class_block=dynamic_cast<ClassBlock*>(block);
  920. if( !class_block ) fail( "Field declarations must appear within a Type declaration" );
  921. ClassType *class_type=class_block->type;
  922. Block *t_block=block;
  923. block=class_block->field_ctors;
  924. emitDebugInfo();
  925. vector<Decl*> decls;
  926. do{
  927. Exp *e;
  928. Decl *d=parseInitDecl( &e );
  929. emit( new FieldDeclStm(d->ident,d->val->type,e),false );
  930. class_type->fields.push_back( d );
  931. decls.push_back( d );
  932. }while( cparse(',') );
  933. addMetaData( decls );
  934. block=t_block;
  935. }
  936. void Parser::parseFunDecl( int attrs ){
  937. next();
  938. bool method=!!(attrs & FunType::METHOD);
  939. ClassBlock *class_block=dynamic_cast<ClassBlock*>(block);
  940. ClassType *class_type=class_block ? class_block->type : 0;
  941. if( method && !class_block ) fail( "Methods must appear within a type" );
  942. string id;
  943. Block *fun_block=0;
  944. if( cparse(T_NEW) ){
  945. id="New";
  946. if( !method ) fail( "New must be a method" );
  947. fun_block=class_block->ctor_new;
  948. if( !fun_block ) fail( "New method already defined" );
  949. class_block->ctor_new=0;
  950. }else if( cparse(T_DELETE) ){
  951. id="Delete";
  952. if( !method ) fail( "Delete must be a method" );
  953. class_block->makeDtor();
  954. fun_block=class_block->dtor_delete;
  955. if( !fun_block ) fail( "Delete method already defined" );
  956. class_block->dtor_delete=0;
  957. }else{
  958. id=parseIdent();
  959. }
  960. ExpSeq *defs=new ExpSeq();
  961. fun_defaults=defs;
  962. FunType *ty=parseType()->funType();
  963. fun_defaults=0;
  964. if( !ty ) fail( "Expecting function type" );
  965. if( ty->return_type->cstringType() || ty->return_type->wstringType() ) fail( "Illegal function return type" );
  966. int i;
  967. for( i=0;i<ty->args.size();++i ){
  968. Type *t=ty->args[i]->val->type;
  969. if( t->cstringType() || t->wstringType() ) fail( "Illegal function parameter type" );
  970. }
  971. bool noDebug=false;
  972. if( cparse( T_NODEBUG ) ) noDebug=true;
  973. if( cparse( T_FINAL ) ){
  974. if( !class_block ){
  975. fail( "Final cannot be used with global functions" );
  976. }
  977. attrs|=FunType::FINAL;
  978. }else if( cparse( T_ABSTRACT ) ){
  979. if( !class_block ){
  980. fail( "Abstract cannot be used with global functions" );
  981. }
  982. if( class_type->attrs & ClassType::FINAL ){
  983. fail( "Abstract methods cannot appear in final types" );
  984. }
  985. attrs|=FunType::ABSTRACT;
  986. }else if( method && (class_type->attrs & ClassType::FINAL) ){
  987. attrs|=FunType::FINAL;
  988. }
  989. if( cparse( T_NODEBUG ) ) noDebug=true;
  990. ty->attrs|=attrs;
  991. string meta=parseMetaData();
  992. if( attrs & FunType::ABSTRACT ){
  993. FunDecl *decl=new FunDecl(id,ty,sym("brl_blitz_NullMethodError",CG_IMPORT),block,defs);
  994. class_type->methods.push_back( decl );
  995. decl->setMetaData( meta );
  996. return;
  997. }
  998. if( !fun_block ){
  999. bool pub=method || class_block || this->pub();
  1000. FunBlock *t=new FunBlock( block,id,ty,pub,defs );
  1001. t->fun_decl->setMetaData( meta );
  1002. fun_block=t;
  1003. }
  1004. if( noDebug ) fun_block->debug_on=false;
  1005. parseStms( fun_block,method ? T_ENDMETHOD : T_ENDFUNCTION );
  1006. }
  1007. static bool isExpOp( int t ){
  1008. switch( t ){
  1009. case '&':case '|':case '~':
  1010. case '+':case '-':case '*':case '/':case T_MOD:case '^':
  1011. case T_LT:case T_GT:case T_LE:case T_GE:case T_EQ:case T_NE:
  1012. case T_AND:case T_OR:case T_SHL:case T_SHR:case T_SAR:
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. void Parser::parseExpStm(){
  1018. Exp *e=parsePreExp();
  1019. if( cparse(T_EQ) ){
  1020. emit( new AssignStm(e,parseExp()),true );
  1021. return;
  1022. }
  1023. int op=0;
  1024. switch( curr() ){
  1025. case T_ADDASSIGN:case T_SUBASSIGN:case T_MULASSIGN:case T_DIVASSIGN:case T_MODASSIGN:
  1026. case T_ORASSIGN:case T_ANDASSIGN:case T_XORASSIGN:case T_SHLASSIGN:case T_SHRASSIGN:case T_SARASSIGN:
  1027. op=curr();next();break;
  1028. }
  1029. if( op ){
  1030. emit( new OpAssignStm(op,e,parseExp()),true );
  1031. return;
  1032. }
  1033. if( dynamic_cast<NewExp*>(e) ){
  1034. emit( new EvalStm(e),true );
  1035. return;
  1036. }
  1037. InvokeExp *t=dynamic_cast<InvokeExp*>(e);
  1038. bool more=false;
  1039. if( !t ){
  1040. t=new InvokeExp(e);
  1041. more=curr()!=';' && curr()!='\n' && curr()!=EOF;
  1042. }else if( t->seq.size()==1 ){
  1043. if( isExpOp(curr()) ){
  1044. primary=t->seq[0];
  1045. t->seq[0]=parseExp();
  1046. }
  1047. more=cparse(',');
  1048. }
  1049. if( more ){
  1050. do{
  1051. if( curr()==',' ) t->seq.push_back(0);
  1052. else t->seq.push_back( parseExp() );
  1053. }while( cparse(',') );
  1054. }
  1055. emit( new EvalStm(t),true );
  1056. }
  1057. void Parser::parseIfStm( int t_term ){
  1058. source_info=toker->sourceInfo();
  1059. next();
  1060. Exp *e=parseExp();
  1061. cparse( T_THEN );
  1062. int term=t_term ? t_term : (curr()=='\n' ? T_ENDIF : '\n');
  1063. IfStm *stm=new IfStm( e,0,0 );
  1064. emit( stm,true );
  1065. int t;
  1066. block=stm->then_block=new Block( block );
  1067. for(;;){
  1068. t=curr();
  1069. if( t==term || t==T_ELSE || t==T_ELSEIF ) break;
  1070. parseStm();
  1071. }
  1072. block=block->outer;
  1073. if( t!=term ){
  1074. block=stm->else_block=new Block( block );
  1075. if( t==T_ELSE ){
  1076. if( next()==T_IF ){
  1077. parseIfStm( term );
  1078. }else{
  1079. while( curr()!=term ) parseStm();
  1080. }
  1081. }else{
  1082. //t==T_ELSEIF
  1083. parseIfStm( term );
  1084. }
  1085. block=block->outer;
  1086. }
  1087. if( !t_term && term==T_ENDIF ) next();
  1088. }
  1089. void Parser::parseLoopCtrlStm(){
  1090. int toke=curr();
  1091. string lab;
  1092. if( next()==T_IDENT ) lab=parseIdent();
  1093. emit( new LoopCtrlStm( toke,lab ),true );
  1094. }
  1095. void Parser::parseForStm(){
  1096. next();
  1097. LoopBlock *loop_block=new LoopBlock( block,loopLabel );
  1098. loopLabel="";
  1099. Exp *var;
  1100. if( cparse(T_LOCAL) ){
  1101. string id=parseIdent();
  1102. Type *ty=parseRefType();
  1103. var=new LocalDeclExp( id,ty,loop_block );
  1104. }else{
  1105. var=parsePostExp();
  1106. }
  1107. if( !cparse(T_EQ) ) exp( "assignment" );
  1108. if( cparse(T_EACHIN) ){
  1109. Exp *coll=parseExp();
  1110. emit( new ForEachStm( var,coll,loop_block ),true );
  1111. parseStms( loop_block,T_NEXT );
  1112. return;
  1113. }
  1114. Exp *init=parseExp();
  1115. bool until;
  1116. if( cparse(T_TO) ) until=false;
  1117. else if( cparse(T_UNTIL) ) until=true;
  1118. else fail( "Expecting 'To' or 'Until'" );
  1119. Exp *to=parseExp();
  1120. Exp *step=cparse(T_STEP) ? parseExp() : 0;
  1121. emit( new ForStm( var,init,to,step,loop_block,until ),true );
  1122. parseStms( loop_block,T_NEXT );
  1123. }
  1124. void Parser::parseWhileStm(){
  1125. next();
  1126. LoopBlock *loop_block=new LoopBlock( block,loopLabel );
  1127. loopLabel="";
  1128. emit( new WhileStm( parseExp(),loop_block),true );
  1129. parseStms( loop_block,T_WEND );
  1130. }
  1131. void Parser::parseRepeatStm(){
  1132. next();
  1133. LoopBlock *loop_block=new LoopBlock( block,loopLabel );
  1134. loopLabel="";
  1135. block=loop_block;
  1136. while( curr()!=T_UNTIL && curr()!=T_FOREVER ){
  1137. parseStm();
  1138. }
  1139. source_info=toker->sourceInfo();
  1140. int c=curr();next();
  1141. block=block->outer;
  1142. Exp *e=(c==T_UNTIL) ? parseExp() : 0;
  1143. emit( new RepeatStm(e,loop_block),true );
  1144. }
  1145. void Parser::parseTryStm(){
  1146. next();
  1147. TryStm *try_stm=new TryStm( new Block(block) );
  1148. emit( try_stm,true );
  1149. block=try_stm->block;
  1150. while( curr()!=T_CATCH && curr()!=T_ENDTRY ){
  1151. parseStm();
  1152. }
  1153. block=block->outer;
  1154. while( cparse(T_CATCH) ){
  1155. TryCatch *t=new TryCatch( new Block(block) );
  1156. try_stm->catches.push_back( t );
  1157. t->ident=parseIdent();
  1158. t->type=parseType();
  1159. block=t->block;
  1160. while( curr()!=T_CATCH && curr()!=T_ENDTRY ){
  1161. parseStm();
  1162. }
  1163. block=block->outer;
  1164. }
  1165. parse( T_ENDTRY );
  1166. }
  1167. void Parser::parseThrowStm(){
  1168. next();
  1169. emit( new ThrowStm(parseExp()),true );
  1170. }
  1171. void Parser::parseSelectStm(){
  1172. next();
  1173. SelectStm *sel=new SelectStm( parseExp() );
  1174. emit( sel,true );
  1175. while( cparse('\n') ){}
  1176. source_info=toker->sourceInfo();
  1177. while( cparse(T_CASE) ){
  1178. SelCase *t_case=new SelCase( new Block( block ) );
  1179. t_case->source_info=source_info;
  1180. sel->cases.push_back( t_case );
  1181. do{
  1182. t_case->exps.push_back( parseExp() );
  1183. }while( cparse(',') );
  1184. block=t_case->block;
  1185. while( curr()!=T_CASE && curr()!=T__DEFAULT && curr()!=T_ENDSELECT ){
  1186. parseStm();
  1187. }
  1188. source_info=toker->sourceInfo();
  1189. block=block->outer;
  1190. }
  1191. if( cparse(T__DEFAULT) ){
  1192. sel->_default=new Block( block );
  1193. block=sel->_default;
  1194. while( curr()!=T_ENDSELECT ){
  1195. parseStm();
  1196. }
  1197. block=block->outer;
  1198. }
  1199. parse( T_ENDSELECT );
  1200. }
  1201. void Parser::parseReturnStm(){
  1202. next();
  1203. Exp *exp=0;
  1204. if( curr()!=';' && curr()!='\n' && curr()!=EOF ) exp=parseExp();
  1205. emit( new ReturnStm(exp),true );
  1206. }
  1207. void Parser::parseDeleteStm(){
  1208. next();
  1209. Exp *exp=parseExp();
  1210. emit( new DeleteStm(exp),true );
  1211. }
  1212. void Parser::parseAccess(){
  1213. switch( curr() ){
  1214. case T_PUBLIC:pub_=true;break;
  1215. case T_PRIVATE:pub_=false;break;
  1216. default:assert(0);
  1217. }
  1218. next();
  1219. }
  1220. void Parser::parseLabelStm(){
  1221. next();
  1222. string id=parseIdent();
  1223. if( strictMode ){
  1224. while( cparse('\n') ){}
  1225. switch( curr() ){
  1226. case T_FOR:case T_WHILE:case T_REPEAT:case T_DEFDATA:
  1227. break;
  1228. default:
  1229. fail( "Labels must appear before a loop or DefData statement" );
  1230. }
  1231. if( curr()!=T_DEFDATA ){
  1232. loopLabel=id;
  1233. return;
  1234. }
  1235. }
  1236. if( block->fun_block->labels.count(id) ) fail( "Duplicate label '%s'",id.c_str() );
  1237. CGSym *goto_sym=CG::sym();
  1238. CGSym *restore_sym=CG::sym();
  1239. LabelStm *stm=new LabelStm(goto_sym,restore_sym);
  1240. block->fun_block->labels.insert( make_pair(tolower(id),stm) );
  1241. emit( stm,true );
  1242. }
  1243. void Parser::parseGotoStm(){
  1244. next();
  1245. emit( new GotoStm( parseIdent() ),true );
  1246. }
  1247. void Parser::parseAssertStm(){
  1248. next();
  1249. Exp *e=parseExp(),*m;
  1250. if( cparse(',') || cparse(T_ELSE) ) m=parseExp();
  1251. else m=new ValExp( new Val(tobstring("Assert failed")) );
  1252. emit( new AssertStm(e,m),true );
  1253. }
  1254. void Parser::includeFile( string file ){
  1255. file=realpath(file);
  1256. Toker *t=toker;
  1257. toker=new Toker( file );
  1258. string cd=getcwd();
  1259. setcwd( getdir(file) );
  1260. while( curr()!=EOF ){
  1261. parseStm();
  1262. }
  1263. setcwd( cd );
  1264. toker=t;
  1265. }
  1266. void Parser::parseDataStm(){
  1267. next();
  1268. DataStm *t=new DataStm();
  1269. emit( t,false );
  1270. do{
  1271. t->exps.push_back( parseExp() );
  1272. }while( cparse(',') );
  1273. }
  1274. void Parser::parseReadStm(){
  1275. next();
  1276. ReadStm *t=new ReadStm();
  1277. emit( t,true );
  1278. do{
  1279. t->exps.push_back( parseExp() );
  1280. }while( cparse(',') );
  1281. }
  1282. void Parser::parseRestoreStm(){
  1283. next();
  1284. emit( new RestoreStm( parseIdent() ),true );
  1285. }
  1286. void Parser::parseStm(){
  1287. source_info=toker->sourceInfo();
  1288. switch( curr() ){
  1289. case ';':
  1290. case '\n':
  1291. next();
  1292. break;
  1293. case '#':
  1294. parseLabelStm();
  1295. break;
  1296. case T_DEFDATA:
  1297. parseDataStm();
  1298. break;
  1299. case T_READDATA:
  1300. parseReadStm();
  1301. break;
  1302. case T_RESTOREDATA:
  1303. parseRestoreStm();
  1304. break;
  1305. case T_GOTO:
  1306. parseGotoStm();
  1307. break;
  1308. case T_TRY:
  1309. parseTryStm();
  1310. break;
  1311. case T_THROW:
  1312. parseThrowStm();
  1313. break;
  1314. case T_IF:
  1315. //case T_ELSEIF:
  1316. parseIfStm(0);
  1317. break;
  1318. case T_GLOBAL:
  1319. parseGlobalDecls();
  1320. break;
  1321. case T_FUNCTION:
  1322. parseFunDecl( 0 );
  1323. break;
  1324. case T_EXIT:case T_CONTINUE:
  1325. parseLoopCtrlStm();
  1326. break;
  1327. case T_FOR:
  1328. parseForStm();
  1329. break;
  1330. case T_WHILE:
  1331. parseWhileStm();
  1332. break;
  1333. case T_REPEAT:
  1334. parseRepeatStm();
  1335. break;
  1336. case T_SELECT:
  1337. parseSelectStm();
  1338. break;
  1339. case T_RETURN:
  1340. parseReturnStm();
  1341. break;
  1342. case T_ASSERT:
  1343. parseAssertStm();
  1344. break;
  1345. case T_END:
  1346. next();emit( new EndStm(),true );
  1347. break;
  1348. case T_RELEASE:
  1349. next();emit( new ReleaseStm( parseExp() ),true );
  1350. break;
  1351. case T_LOCAL:
  1352. parseLocalDecls();
  1353. break;
  1354. case T_CONST:
  1355. parseConstDecls();
  1356. break;
  1357. case T_TYPE:
  1358. parseTypeDecl();
  1359. break;
  1360. case T_INCLUDE:
  1361. next();includeFile( parseString() );
  1362. break;
  1363. case T_INCBIN:
  1364. next();emit( new IncbinStm( parseString() ),false );
  1365. break;
  1366. case T_EXTERN:
  1367. parseExtern();
  1368. break;
  1369. case T_PUBLIC:case T_PRIVATE:
  1370. parseAccess();
  1371. break;
  1372. case T_MODULE:case T_MODULEINFO:case T_IMPORT:case T_STRICT:
  1373. fail( "'%s' must appear at top of file",toker->text().c_str() );
  1374. case T_FIELD:
  1375. fail( "Field declaration outside of user defined type" );
  1376. case T_METHOD:
  1377. fail( "Method declaration outside of user defined type" );
  1378. case T_ELSE:
  1379. fail( "'Else' without matching 'If'" );
  1380. case T_ENDIF:
  1381. fail( "'%s' without matching 'If'",toker->text().c_str() );
  1382. case T_NEXT:
  1383. fail( "'Next' without matching 'For'" );
  1384. case T_WEND:
  1385. fail( "'%s' without matching 'While'",toker->text().c_str() );
  1386. case T_UNTIL:
  1387. fail( "'Until' without matching 'Repeat'" );
  1388. case T_FOREVER:
  1389. fail( "'Forever' without matching 'Repeat'" );
  1390. case T_CASE:
  1391. fail( "'Case' without matching 'Select'" );
  1392. case T__DEFAULT:
  1393. fail( "'Default' without matching 'Select'" );
  1394. case T_CATCH:
  1395. fail( "'Catch' without matching 'Try'" );
  1396. case T_ENDTRY:
  1397. fail( "'%s' without matching 'Try'",toker->text().c_str() );
  1398. case T_ENDTYPE:
  1399. fail( "'%s' without matching 'Type'",toker->text().c_str() );
  1400. case T_ENDEXTERN:
  1401. fail( "'%s' without matching 'Extern'",toker->text().c_str() );
  1402. case T_ENDMETHOD:
  1403. fail( "'%s' without matching 'Method'",toker->text().c_str() );
  1404. case T_ENDFUNCTION:
  1405. fail( "'%s' without matching 'Function'",toker->text().c_str() );
  1406. case T_ENDSELECT:
  1407. fail( "'%s' without matching 'Select'",toker->text().c_str() );
  1408. default:
  1409. parseExpStm();
  1410. }
  1411. }
  1412. void Parser::parseStms( Block *t,int term ){
  1413. Block *t_block=block;
  1414. block=t;
  1415. while( curr()!=term ){
  1416. parseStm();
  1417. }
  1418. next();
  1419. block=t_block;
  1420. }
  1421. void Parser::importFile( string file,ModuleType *mod ){
  1422. Toker *t=toker;
  1423. toker=new Toker( file );
  1424. ModuleType *m=import_module;
  1425. import_module=mod;
  1426. ++import_nest;
  1427. while( curr()!=EOF ){
  1428. source_info=toker->sourceInfo();
  1429. if( cparse(';') ){
  1430. }else if( cparse('\n') ){
  1431. }else if( cparse( T_IMPORT ) ){
  1432. parseImport();
  1433. }else if( cparse( T_MODULEINFO ) ){
  1434. parseString();
  1435. }else{
  1436. Decl *d=parseImportDecl();
  1437. if( import_module ){
  1438. import_module->decls.push_back(d);
  1439. }else{
  1440. mainFun->decl(d);
  1441. moduleExports.push_back(d);
  1442. }
  1443. }
  1444. }
  1445. --import_nest;
  1446. toker->close();
  1447. import_module=m;
  1448. toker=t;
  1449. }
  1450. void Parser::importModule( string mod ){
  1451. string mung="@"+mod;
  1452. if( rootScope.find(mung) ) return;
  1453. vector<string> ids;
  1454. splitModule( mod,ids );
  1455. ModuleType *mod_ty;
  1456. DeclSeq *decls=&mainFun->decls;
  1457. int k;
  1458. for( k=0;k<ids.size();++k ){
  1459. string id=ids[k];
  1460. if( Val *v=decls->find(id) ){
  1461. mod_ty=v->type->moduleType();
  1462. if( !mod_ty ) fail( "Unable to import module '%s'",mod.c_str() );
  1463. }else{
  1464. mod_ty=new ModuleType();
  1465. decls->push_back( new Decl(id,mod_ty,lit0) );
  1466. }
  1467. decls=&mod_ty->decls;
  1468. }
  1469. rootScope.push_back( new Decl(mung,mod_ty,lit0) );
  1470. importFile( moduleInterface(mod),mod_ty );
  1471. if( !import_module ){
  1472. string t="import "+mod;
  1473. if( !import_nest ){
  1474. objectImports.push_back(t);
  1475. emit( new ImportStm(sym(mungModuleEntry(mod),CG_IMPORT)),false );
  1476. }
  1477. moduleImports.push_back(t);
  1478. }
  1479. }
  1480. void Parser::importSource( string file ){
  1481. string ext=tolower(getext(file)),path=realpath(file);
  1482. if( ext=="bmx" ){
  1483. if( !importedSources.insert( tolower(path) ).second ) return;
  1484. if( !ftime(path) ) fail( "File '%s' not found",path.c_str() );
  1485. string cd=getcwd();
  1486. setcwd( getdir(path) );
  1487. importFile( ".bmx/"+stripdir(path)+config_mung+".i",0 );
  1488. setcwd( cd );
  1489. if( !import_nest ){
  1490. string t="import \""+file+'\"';
  1491. objectImports.push_back( t );
  1492. emit( new ImportStm( sym(mungObjectEntry(file),CG_IMPORT) ),false );
  1493. }
  1494. }else if( ext=="o" || ext=="a" || ext=="lib" ){
  1495. if( !import_module ){
  1496. if( !ftime(path) ) fail( "File '%s' not found",path.c_str() );
  1497. string t="import \""+file+'\"';
  1498. if( !import_nest ) objectImports.push_back( t );
  1499. moduleImports.push_back(t);
  1500. }
  1501. }else if( ext=="c" || ext=="m" || ext=="cpp" || ext=="cxx" || ext=="cc" || ext=="mm" || ext=="s" || ext=="asm" ){
  1502. if( !ftime(path) ) fail( "File '%s' not found",path.c_str() );
  1503. }else{
  1504. fail( "Unrecognized import file type for import '%s'",path.c_str() );
  1505. }
  1506. }
  1507. void Parser::parseImport(){
  1508. if( curr()==T_IDENT ){
  1509. importModule( parseModuleName() );
  1510. }else if( curr()==T_STRINGCONST ){
  1511. string file=parseString();
  1512. if( stripall(file)=="*" ){
  1513. }else if( file[0]=='-' ){
  1514. if( !import_module ){
  1515. string t="import \""+file+'\"';
  1516. if( !import_nest ) objectImports.push_back( t );
  1517. moduleImports.push_back(t);
  1518. }
  1519. return;
  1520. }else{
  1521. importSource( file );
  1522. }
  1523. }else{
  1524. fail( "Expecting module name or import file" );
  1525. }
  1526. }
  1527. Parser::Parser(){
  1528. }
  1529. void Parser::parse(){
  1530. pub_=true;
  1531. primary=0;
  1532. fun_defaults=0;
  1533. default_call_conv=CG_CDECL;
  1534. import_nest=0;
  1535. extern_nest=0;
  1536. import_module=0;
  1537. Type::createTypes();
  1538. setcwd( getdir(opt_infile) );
  1539. toker=new Toker( opt_infile );
  1540. mainFun=new FunBlock();
  1541. block=mainFun;
  1542. ModuleType *mod_ty=new ModuleType();
  1543. mainFun->decls.push_back( new Decl("brl",mod_ty,lit0) );
  1544. mod_ty->decls.push_back( new Decl("blitz",Type::blitzModule,lit0) );
  1545. importFile( env_blitzpath+"/mod/brl.mod/blitz.mod/blitz_classes.i",Type::blitzModule );
  1546. *Type::objectClass=*Type::blitzModule->find("Object");
  1547. *Type::stringClass=*Type::blitzModule->find("String");
  1548. *Type::arrayClass=*Type::blitzModule->find("Array");
  1549. if( opt_module=="brl.blitz" ){
  1550. if( stripall(opt_infile)=="blitz" ){
  1551. rootScope.push_back( new Decl("@brl.blitz",Type::blitzModule,lit0) );
  1552. }
  1553. }else{
  1554. importModule( "brl.blitz" );
  1555. }
  1556. //if( stripall(opt_infile)!="blitz" ) importModule( "brl.blitz" );
  1557. //else rootScope.push_back( new Decl("@brl.blitz",Type::blitzModule,lit0) );
  1558. while( curr()!=EOF ){
  1559. source_info=toker->sourceInfo();
  1560. if( cparse('\n') ){
  1561. }else if( cparse( T_STRICT ) ){
  1562. if( strictMode ) fail( "Strict or SuperStrict already specified" );
  1563. strictMode=1;
  1564. }else if( cparse( T_SUPERSTRICT ) ){
  1565. if( strictMode ) fail( "Strict or SuperStrict already specified" );
  1566. strictMode=2;
  1567. }else if( cparse( T_NODEBUG ) ){
  1568. opt_debug=false;
  1569. opt_release=true;
  1570. block->debug_on=false;
  1571. }else if( cparse( T_MODULE ) ){
  1572. string name=parseModuleName();
  1573. if( name!=opt_module ) fail( "Module does not match commandline module" );
  1574. if( opt_apptype.size() ) fail( "Modules cannot be built as applications" );
  1575. }else if( cparse( T_MODULEINFO ) ){
  1576. if( !opt_module.size() ) fail( "ModuleInfo can only be used with modules" );
  1577. moduleInfos.push_back( parseString() );
  1578. }else if( cparse( T_FRAMEWORK ) ){
  1579. string framework=parseModuleName();
  1580. if( opt_framework.size() && opt_framework!=framework ) fail( "Framework does not match commandline framework" );
  1581. opt_framework=framework;
  1582. }else if( cparse( T_IMPORT ) ){
  1583. parseImport();
  1584. }else{
  1585. break;
  1586. }
  1587. }
  1588. if( opt_module.size() ){
  1589. }else if( opt_framework.size() ){
  1590. importModule( opt_framework );
  1591. }else{
  1592. vector<string> mods;
  1593. enumModules( "",mods );
  1594. for( int k=0;k<mods.size();++k ){
  1595. if( mods[k].find( "brl." )==0 || mods[k].find( "pub." )==0 ){
  1596. importModule( mods[k] );
  1597. }
  1598. }
  1599. }
  1600. emit( new EvalClassBlocksStm(),false );
  1601. while( curr()!=EOF ){
  1602. parseStm();
  1603. }
  1604. toker->close();
  1605. }