std.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "std.h"
  2. #include "block.h"
  3. int strictMode;
  4. FunBlock *mainFun; //main function
  5. DeclSeq rootScope;
  6. DeclSeq objectExports; //exports from this object file
  7. DeclSeq moduleExports; //exports from this and imported object files
  8. vector<string> objectImports; //imports to this object file
  9. vector<string> moduleImports; //imports to this object file
  10. vector<string> moduleInfos;
  11. string globalIdent;
  12. set<string> importedSources; //source files already imported
  13. string fixIdent( string id ){
  14. int k;
  15. for( k=0;k<id.size();++k ){
  16. if( !isalnum(id[k]) && id[k]!='_' ) id[k]='_';
  17. }
  18. return id;
  19. }
  20. void publish( Decl *d ){
  21. objectExports.push_back(d);
  22. moduleExports.push_back(d);
  23. }
  24. Val *findGlobal( string id ){
  25. int k;
  26. Val *v=0;
  27. string mod;
  28. for( k=rootScope.size()-1;k>=0;--k ){
  29. if( Val *t=rootScope[k]->val->find(id) ){
  30. if( v ){
  31. fail( "Duplicate identifier '%s' in modules '%s' and '%s'",id.c_str(),mod.substr(1).c_str(),rootScope[k]->ident.substr(1).c_str() );
  32. dupid( id );
  33. }
  34. mod=rootScope[k]->ident;
  35. v=t;
  36. }
  37. }
  38. if( v ) globalIdent=mod.substr(1)+"."+id;
  39. return v;
  40. }
  41. CGDat *genDebugStm( string t ){
  42. CGDat *d=CG::dat();
  43. int i1=t.find(';');
  44. if( i1!=string::npos ){
  45. int i2=t.find(';',i1+1);
  46. if( i2!=string::npos ){
  47. string f=t.substr(0,i1);
  48. string l=t.substr(i1+1,i2-i1-1);
  49. string c=t.substr(i2+1);
  50. fixpath( f );
  51. if( !f.find(env_blitzpath+"/") ) f="$BMXPATH"+f.substr(env_blitzpath.size());
  52. d->push_back( genCString(f) );
  53. d->push_back( CG::lit(int(toint(l))) );
  54. d->push_back( CG::lit(int(toint(c))) );
  55. return d;
  56. }
  57. }
  58. return 0;
  59. }
  60. CGDat *genCString( string t ){
  61. static map<string,CGDat*> c_strings;
  62. map<string,CGDat*>::iterator it=c_strings.find(t);
  63. if( it!=c_strings.end() ) return it->second;
  64. CGDat *d=CG::dat();
  65. d->push_back( CG::lit(tobstring(t),CG_CSTRING) );
  66. c_strings.insert( make_pair(t,d) );
  67. return d;
  68. }
  69. CGDat *genBBString( bstring t ){
  70. static map<bstring,CGDat*> bb_strings;
  71. map<bstring,CGDat*>::iterator it=bb_strings.find(t);
  72. if( it!=bb_strings.end() ) return it->second;
  73. CGDat *d=CG::dat();
  74. d->push_back( CG::sym("bbStringClass",CG_IMPORT) );
  75. d->push_back( CG::lit(0x7fffffff) );
  76. d->push_back( CG::lit(t,CG_BSTRING) );
  77. bb_strings.insert( make_pair(t,d) );
  78. return d;
  79. }
  80. CGDat *genBBString2( bstring t ){
  81. static map<bstring,CGDat*> bb_strings2;
  82. map<bstring,CGDat*>::iterator it=bb_strings2.find(t);
  83. if( it!=bb_strings2.end() ) return it->second;
  84. CGDat *d=CG::dat();
  85. d->push_back( CG::sym("bbStringClass",CG_IMPORT) );
  86. d->push_back( CG::lit(0x7ffffffe) );
  87. d->push_back( CG::lit(t,CG_BSTRING) );
  88. bb_strings2.insert( make_pair(t,d) );
  89. return d;
  90. }
  91. string mungGlobal( string decl_id ){
  92. return global_mung+decl_id;
  93. }
  94. string mungMember( string class_id,string decl_id ){
  95. return "_"+global_mung+class_id+"_"+decl_id;
  96. }
  97. string mungObjectEntry( string path ){
  98. path=tolower(realpath(path));
  99. string dir=stripall(getdir(path));
  100. return "__bb_"+fixIdent(dir)+"_"+fixIdent(stripall(path));
  101. }
  102. string mungModuleEntry( string mod ){
  103. string id=moduleIdent(mod);
  104. //#if DEMO_VERSION
  105. // if( id!="appstub" ) return "__bb_"+id+"_"+id+"_";
  106. //#endif
  107. return "__bb_"+id+"_"+id;
  108. }
  109. void dupid( string id,const char *fmt ){
  110. fail( fmt,id.c_str() );
  111. }
  112. void badid( string id,const char *fmt ){
  113. fail( fmt,id.c_str() );
  114. }
  115. void badty( string id,const char *fmt ){
  116. fail( fmt,id.c_str() );
  117. }
  118. void badmod( string id,const char *fmt ){
  119. fail( fmt,id.c_str() );
  120. }
  121. static void escErr(){
  122. fail( "Bad escape sequence in string" );
  123. }
  124. bstring escapeString( bstring t ){
  125. bstring r;
  126. r.reserve( t.size() );
  127. int i=0;
  128. while( i<t.size() ){
  129. int c=t[i++],esc;
  130. switch( c ){
  131. case '~':
  132. esc='~';
  133. break;
  134. case '\0':
  135. esc='0';
  136. break;
  137. case '\t':
  138. esc='t';
  139. break;
  140. case '\r':
  141. esc='r';
  142. break;
  143. case '\n':
  144. esc='n';
  145. break;
  146. case '\"':
  147. esc='q';
  148. break;
  149. default:
  150. if( c>=32 && c<127 ){
  151. r+=bchar_t(c);
  152. }else{
  153. char buf[32];
  154. sprintf( buf,"~%i~",c );
  155. r+=tobstring(buf);
  156. }
  157. continue;
  158. }
  159. r+=bchar_t('~');
  160. r+=bchar_t(esc);
  161. }
  162. return r;
  163. }
  164. bstring unescapeString( bstring t ){
  165. bstring r;
  166. r.reserve( t.size() );
  167. int i=0;
  168. while( i<t.size() ){
  169. int c=t[i++];
  170. if( c!='~' ){
  171. r+=bchar_t(c);
  172. continue;
  173. }
  174. if( i==t.size() ) escErr();
  175. c=t[i++];
  176. switch( c ){
  177. case '~':
  178. r+=bchar_t('~');
  179. break;
  180. case '0':
  181. r+=bchar_t('\0');
  182. break;
  183. case 't':
  184. r+=bchar_t('\t');
  185. break;
  186. case 'r':
  187. r+=bchar_t('\r');
  188. break;
  189. case 'n':
  190. r+=bchar_t('\n');
  191. break;
  192. case 'q':
  193. r+=bchar_t('\"');
  194. break;
  195. default:
  196. if( c>='1' && c<='9' ){
  197. int n=0;
  198. while( c>='0' && c<='9' ){
  199. n=n*10+(c-'0');
  200. if( i==t.size() ) escErr();
  201. c=t[i++];
  202. }
  203. if( c!='~' ) escErr();
  204. r+=bchar_t(n);
  205. }else{
  206. escErr();
  207. }
  208. }
  209. }
  210. return r;
  211. }