expressp.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /* ------------------------------------------------------------------------- */
  2. /* "expressp" : The expression parser */
  3. /* */
  4. /* Part of Inform 6.33 */
  5. /* copyright (c) Graham Nelson 1993 - 2014 */
  6. /* */
  7. /* ------------------------------------------------------------------------- */
  8. #include "header.h"
  9. /* --- Interface to lexer -------------------------------------------------- */
  10. static char separators_to_operators[103];
  11. static char conditionals_to_operators[7];
  12. static char token_type_allowable[301];
  13. #define NOT_AN_OPERATOR (char) 0x7e
  14. static void make_lexical_interface_tables(void)
  15. { int i;
  16. for (i=0;i<103;i++)
  17. separators_to_operators[i] = NOT_AN_OPERATOR;
  18. for (i=0;i<NUM_OPERATORS;i++)
  19. if (operators[i].token_type == SEP_TT)
  20. separators_to_operators[operators[i].token_value] = i;
  21. for (i=0;i<7;i++) /* 7 being the size of keyword_group "conditions" */
  22. conditionals_to_operators[i] = NOT_AN_OPERATOR;
  23. for (i=0;i<NUM_OPERATORS;i++)
  24. if (operators[i].token_type == CND_TT)
  25. conditionals_to_operators[operators[i].token_value] = i;
  26. for (i=0;i<301;i++) token_type_allowable[i] = 0;
  27. token_type_allowable[VARIABLE_TT] = 1;
  28. token_type_allowable[SYSFUN_TT] = 1;
  29. token_type_allowable[DQ_TT] = 1;
  30. token_type_allowable[DICTWORD_TT] = 1;
  31. token_type_allowable[SUBOPEN_TT] = 1;
  32. token_type_allowable[SUBCLOSE_TT] = 1;
  33. token_type_allowable[SMALL_NUMBER_TT] = 1;
  34. token_type_allowable[LARGE_NUMBER_TT] = 1;
  35. token_type_allowable[ACTION_TT] = 1;
  36. token_type_allowable[SYSTEM_CONSTANT_TT] = 1;
  37. token_type_allowable[OP_TT] = 1;
  38. }
  39. static token_data current_token, previous_token, heldback_token;
  40. static int comma_allowed, arrow_allowed, superclass_allowed,
  41. bare_prop_allowed,
  42. array_init_ambiguity, action_ambiguity,
  43. etoken_count, inserting_token, bracket_level;
  44. extern int *variable_usage;
  45. int system_function_usage[32];
  46. static int get_next_etoken(void)
  47. { int v, symbol, mark_symbol_as_used = FALSE,
  48. initial_bracket_level = bracket_level;
  49. etoken_count++;
  50. if (inserting_token)
  51. { current_token = heldback_token;
  52. inserting_token = FALSE;
  53. }
  54. else
  55. { get_next_token();
  56. current_token.text = token_text;
  57. current_token.value = token_value;
  58. current_token.type = token_type;
  59. current_token.marker = 0;
  60. current_token.symtype = 0;
  61. current_token.symflags = -1;
  62. }
  63. switch(current_token.type)
  64. { case LOCAL_VARIABLE_TT:
  65. current_token.type = VARIABLE_TT;
  66. variable_usage[current_token.value] = TRUE;
  67. break;
  68. case DQ_TT:
  69. current_token.marker = STRING_MV;
  70. break;
  71. case SQ_TT:
  72. { int32 unicode = text_to_unicode(token_text);
  73. if (token_text[textual_form_length] == 0)
  74. {
  75. if (!glulx_mode) {
  76. current_token.value = unicode_to_zscii(unicode);
  77. if (current_token.value == 5)
  78. { unicode_char_error("Character can be printed \
  79. but not used as a value:", unicode);
  80. current_token.value = '?';
  81. }
  82. if (current_token.value >= 0x100)
  83. current_token.type = LARGE_NUMBER_TT;
  84. else current_token.type = SMALL_NUMBER_TT;
  85. }
  86. else {
  87. current_token.value = unicode;
  88. if (current_token.value >= 0x8000
  89. || current_token.value < -0x8000)
  90. current_token.type = LARGE_NUMBER_TT;
  91. else current_token.type = SMALL_NUMBER_TT;
  92. }
  93. }
  94. else
  95. { current_token.type = DICTWORD_TT;
  96. current_token.marker = DWORD_MV;
  97. }
  98. }
  99. break;
  100. case SYMBOL_TT:
  101. ReceiveSymbol:
  102. symbol = current_token.value;
  103. mark_symbol_as_used = TRUE;
  104. v = svals[symbol];
  105. current_token.symtype = stypes[symbol];
  106. current_token.symflags = sflags[symbol];
  107. switch(stypes[symbol])
  108. { case ROUTINE_T:
  109. current_token.marker = IROUTINE_MV;
  110. break;
  111. case GLOBAL_VARIABLE_T:
  112. current_token.marker = VARIABLE_MV;
  113. break;
  114. case OBJECT_T:
  115. case CLASS_T:
  116. /* All objects must be backpatched in Glulx. */
  117. if (module_switch || glulx_mode)
  118. current_token.marker = OBJECT_MV;
  119. break;
  120. case ARRAY_T:
  121. current_token.marker = ARRAY_MV;
  122. break;
  123. case INDIVIDUAL_PROPERTY_T:
  124. if (module_switch) current_token.marker = IDENT_MV;
  125. break;
  126. case CONSTANT_T:
  127. if (sflags[symbol] & (UNKNOWN_SFLAG + CHANGE_SFLAG))
  128. { current_token.marker = SYMBOL_MV;
  129. if (module_switch) import_symbol(symbol);
  130. v = symbol;
  131. }
  132. else current_token.marker = 0;
  133. break;
  134. case LABEL_T:
  135. error_named("Label name used as value:", token_text);
  136. break;
  137. default:
  138. current_token.marker = 0;
  139. break;
  140. }
  141. if (sflags[symbol] & SYSTEM_SFLAG)
  142. current_token.marker = 0;
  143. current_token.value = v;
  144. if (!glulx_mode) {
  145. if (((current_token.marker != 0)
  146. && (current_token.marker != VARIABLE_MV))
  147. || (v < 0) || (v > 255))
  148. current_token.type = LARGE_NUMBER_TT;
  149. else current_token.type = SMALL_NUMBER_TT;
  150. }
  151. else {
  152. if (((current_token.marker != 0)
  153. && (current_token.marker != VARIABLE_MV))
  154. || (v < -0x8000) || (v >= 0x8000))
  155. current_token.type = LARGE_NUMBER_TT;
  156. else current_token.type = SMALL_NUMBER_TT;
  157. }
  158. if (stypes[symbol] == GLOBAL_VARIABLE_T)
  159. { current_token.type = VARIABLE_TT;
  160. variable_usage[current_token.value] = TRUE;
  161. }
  162. break;
  163. case NUMBER_TT:
  164. if (!glulx_mode) {
  165. if (current_token.value >= 256)
  166. current_token.type = LARGE_NUMBER_TT;
  167. else
  168. current_token.type = SMALL_NUMBER_TT;
  169. }
  170. else {
  171. if (current_token.value < -0x8000
  172. || current_token.value >= 0x8000)
  173. current_token.type = LARGE_NUMBER_TT;
  174. else
  175. current_token.type = SMALL_NUMBER_TT;
  176. }
  177. break;
  178. case SEP_TT:
  179. switch(current_token.value)
  180. { case ARROW_SEP:
  181. if (!arrow_allowed)
  182. current_token.type = ENDEXP_TT;
  183. break;
  184. case COMMA_SEP:
  185. if ((bracket_level==0) && (!comma_allowed))
  186. current_token.type = ENDEXP_TT;
  187. break;
  188. case SUPERCLASS_SEP:
  189. if ((bracket_level==0) && (!superclass_allowed))
  190. current_token.type = ENDEXP_TT;
  191. break;
  192. case GREATER_SEP:
  193. get_next_token();
  194. if ((token_type == SEP_TT)
  195. &&((token_value == SEMICOLON_SEP)
  196. || (token_value == GREATER_SEP)))
  197. current_token.type = ENDEXP_TT;
  198. put_token_back();
  199. break;
  200. case OPENB_SEP:
  201. bracket_level++;
  202. if (expr_trace_level>=3)
  203. { printf("Previous token type = %d\n",previous_token.type);
  204. printf("Previous token val = %d\n",previous_token.value);
  205. }
  206. if ((previous_token.type == OP_TT)
  207. || (previous_token.type == SUBOPEN_TT)
  208. || (previous_token.type == ENDEXP_TT)
  209. || (array_init_ambiguity)
  210. || ((bracket_level == 1) && (action_ambiguity)))
  211. current_token.type = SUBOPEN_TT;
  212. else
  213. { inserting_token = TRUE;
  214. heldback_token = current_token;
  215. current_token.text = "<call>";
  216. bracket_level--;
  217. }
  218. break;
  219. case CLOSEB_SEP:
  220. bracket_level--;
  221. if (bracket_level < 0)
  222. current_token.type = ENDEXP_TT;
  223. else current_token.type = SUBCLOSE_TT;
  224. break;
  225. case SEMICOLON_SEP:
  226. current_token.type = ENDEXP_TT; break;
  227. case MINUS_SEP:
  228. if ((previous_token.type == OP_TT)
  229. || (previous_token.type == SUBOPEN_TT)
  230. || (previous_token.type == ENDEXP_TT))
  231. current_token.value = UNARY_MINUS_SEP; break;
  232. case INC_SEP:
  233. if ((previous_token.type == VARIABLE_TT)
  234. || (previous_token.type == SUBCLOSE_TT)
  235. || (previous_token.type == LARGE_NUMBER_TT)
  236. || (previous_token.type == SMALL_NUMBER_TT))
  237. current_token.value = POST_INC_SEP; break;
  238. case DEC_SEP:
  239. if ((previous_token.type == VARIABLE_TT)
  240. || (previous_token.type == SUBCLOSE_TT)
  241. || (previous_token.type == LARGE_NUMBER_TT)
  242. || (previous_token.type == SMALL_NUMBER_TT))
  243. current_token.value = POST_DEC_SEP; break;
  244. case HASHHASH_SEP:
  245. token_text = current_token.text + 2;
  246. ActionUsedAsConstant:
  247. current_token.type = ACTION_TT;
  248. current_token.text = token_text;
  249. current_token.value = 0;
  250. current_token.marker = ACTION_MV;
  251. break;
  252. case HASHADOLLAR_SEP:
  253. obsolete_warning("'#a$Act' is now superseded by '##Act'");
  254. token_text = current_token.text + 3;
  255. goto ActionUsedAsConstant;
  256. case HASHGDOLLAR_SEP:
  257. /* This form generates the position of a global variable
  258. in the global variables array. So Glob is the same as
  259. #globals_array --> #g$Glob */
  260. current_token.text += 3;
  261. current_token.type = SYMBOL_TT;
  262. symbol = symbol_index(current_token.text, -1);
  263. if (stypes[symbol] != GLOBAL_VARIABLE_T) {
  264. ebf_error(
  265. "global variable name after '#g$'",
  266. current_token.text);
  267. current_token.value = 0;
  268. current_token.type = SMALL_NUMBER_TT;
  269. current_token.marker = 0;
  270. break;
  271. }
  272. mark_symbol_as_used = TRUE;
  273. current_token.value = svals[symbol] - MAX_LOCAL_VARIABLES;
  274. current_token.marker = 0;
  275. if (!glulx_mode) {
  276. if (current_token.value >= 0x100)
  277. current_token.type = LARGE_NUMBER_TT;
  278. else current_token.type = SMALL_NUMBER_TT;
  279. }
  280. else {
  281. if (current_token.value >= 0x8000
  282. || current_token.value < -0x8000)
  283. current_token.type = LARGE_NUMBER_TT;
  284. else current_token.type = SMALL_NUMBER_TT;
  285. }
  286. break;
  287. case HASHNDOLLAR_SEP:
  288. /* This form is still needed for constants like #n$a (the
  289. dictionary address of the word "a"), since 'a' means
  290. the ASCII value of 'a' */
  291. if (strlen(token_text) > 4)
  292. obsolete_warning(
  293. "'#n$word' is now superseded by ''word''");
  294. current_token.type = DICTWORD_TT;
  295. current_token.value = 0;
  296. current_token.text = token_text + 3;
  297. current_token.marker = DWORD_MV;
  298. break;
  299. case HASHRDOLLAR_SEP:
  300. /* This form -- #r$Routinename, to return the routine's */
  301. /* packed address -- is needed far less often in Inform 6, */
  302. /* where just giving the name Routine returns the packed */
  303. /* address. But it's used in a lot of Inform 5 code. */
  304. obsolete_warning(
  305. "'#r$Routine' can now be written just 'Routine'");
  306. current_token.text += 3;
  307. current_token.type = SYMBOL_TT;
  308. current_token.value = symbol_index(current_token.text, -1);
  309. goto ReceiveSymbol;
  310. case HASHWDOLLAR_SEP:
  311. error("The obsolete '#w$word' construct has been removed");
  312. break;
  313. case HASH_SEP:
  314. system_constants.enabled = TRUE;
  315. get_next_token();
  316. system_constants.enabled = FALSE;
  317. if (token_type != SYSTEM_CONSTANT_TT)
  318. { ebf_error(
  319. "'r$', 'n$', 'g$' or internal Inform constant name after '#'",
  320. token_text);
  321. break;
  322. }
  323. else
  324. { current_token.type = token_type;
  325. current_token.value = token_value;
  326. current_token.text = token_text;
  327. current_token.marker = INCON_MV;
  328. }
  329. break;
  330. }
  331. break;
  332. case CND_TT:
  333. v = conditionals_to_operators[current_token.value];
  334. if (v != NOT_AN_OPERATOR)
  335. { current_token.type = OP_TT; current_token.value = v;
  336. }
  337. break;
  338. }
  339. if (current_token.type == SEP_TT)
  340. { v = separators_to_operators[current_token.value];
  341. if (v != NOT_AN_OPERATOR)
  342. { if ((veneer_mode)
  343. || ((v!=MESSAGE_OP) && (v!=MPROP_NUM_OP) && (v!=MPROP_NUM_OP)))
  344. { current_token.type = OP_TT; current_token.value = v;
  345. if (array_init_ambiguity &&
  346. ((v==MINUS_OP) || (v==UNARY_MINUS_OP)) &&
  347. (initial_bracket_level == 0) &&
  348. (etoken_count != 1))
  349. warning("Without bracketing, the minus sign '-' is ambiguous");
  350. }
  351. }
  352. }
  353. /* A feature of Inform making it annoyingly hard to parse left-to-right
  354. is that there is no clear delimiter for expressions; that is, the
  355. legal syntax often includes sequences of expressions with no
  356. intervening markers such as commas. We therefore need to use some
  357. internal context to determine whether an end is in sight... */
  358. if (token_type_allowable[current_token.type]==0)
  359. { if (expr_trace_level >= 3)
  360. { printf("Discarding as not allowable: '%s' ", current_token.text);
  361. describe_token(current_token);
  362. printf("\n");
  363. }
  364. current_token.type = ENDEXP_TT;
  365. }
  366. else
  367. if ((!((initial_bracket_level > 0)
  368. || (previous_token.type == ENDEXP_TT)
  369. || ((previous_token.type == OP_TT)
  370. && (operators[previous_token.value].usage != POST_U))
  371. || (previous_token.type == SYSFUN_TT)))
  372. && ((current_token.type != OP_TT)
  373. || (operators[current_token.value].usage == PRE_U)))
  374. { if (expr_trace_level >= 3)
  375. { printf("Discarding as no longer part: '%s' ", current_token.text);
  376. describe_token(current_token);
  377. printf("\n");
  378. }
  379. current_token.type = ENDEXP_TT;
  380. }
  381. else
  382. { if (mark_symbol_as_used) sflags[symbol] |= USED_SFLAG;
  383. if (expr_trace_level >= 3)
  384. { printf("Expr token = '%s' ", current_token.text);
  385. describe_token(current_token);
  386. printf("\n");
  387. }
  388. }
  389. if ((previous_token.type == ENDEXP_TT)
  390. && (current_token.type == ENDEXP_TT)) return FALSE;
  391. previous_token = current_token;
  392. return TRUE;
  393. }
  394. /* --- Operator precedences ------------------------------------------------ */
  395. #define LOWER_P 101
  396. #define EQUAL_P 102
  397. #define GREATER_P 103
  398. #define e1 1 /* Missing operand error */
  399. #define e2 2 /* Unexpected close bracket */
  400. #define e3 3 /* Missing operator error */
  401. #define e4 4 /* Expression ends with an open bracket */
  402. #define e5 5 /* Associativity illegal error */
  403. const int prec_table[] = {
  404. /* a .......... ( ) end op term */
  405. /* b ( */ LOWER_P, e3, LOWER_P, LOWER_P, e3,
  406. /* . ) */ EQUAL_P, GREATER_P, e2, GREATER_P, GREATER_P,
  407. /* . end */ e4, GREATER_P, e1, GREATER_P, GREATER_P,
  408. /* . op */ LOWER_P, GREATER_P, LOWER_P, -1, GREATER_P,
  409. /* . term */ LOWER_P, e3, LOWER_P, LOWER_P, e3
  410. };
  411. static int find_prec(token_data a, token_data b)
  412. {
  413. /* We are comparing the precedence of tokens a and b
  414. (where a occurs to the left of b). If the expression is correct,
  415. the only possible values are GREATER_P, LOWER_P or EQUAL_P;
  416. if it is malformed then one of e1 to e5 results.
  417. Note that this routine is not symmetrical and that the relation
  418. is not trichotomous.
  419. If a and b are equal (and aren't brackets), then
  420. a LOWER_P a if a right-associative
  421. a GREATER_P a if a left-associative
  422. */
  423. int i, j, l1, l2;
  424. switch(a.type)
  425. { case SUBOPEN_TT: i=0; break;
  426. case SUBCLOSE_TT: i=1; break;
  427. case ENDEXP_TT: i=2; break;
  428. case OP_TT: i=3; break;
  429. default: i=4; break;
  430. }
  431. switch(b.type)
  432. { case SUBOPEN_TT: i+=0; break;
  433. case SUBCLOSE_TT: i+=5; break;
  434. case ENDEXP_TT: i+=10; break;
  435. case OP_TT: i+=15; break;
  436. default: i+=20; break;
  437. }
  438. j = prec_table[i]; if (j != -1) return j;
  439. l1 = operators[a.value].precedence;
  440. l2 = operators[b.value].precedence;
  441. if (operators[b.value].usage == PRE_U) return LOWER_P;
  442. if (operators[a.value].usage == POST_U) return GREATER_P;
  443. /* Anomalous rule to resolve the function call precedence, which is
  444. different on the right from on the left, e.g., in:
  445. alpha.beta(gamma)
  446. beta(gamma).alpha
  447. */
  448. if ((l1 == 11) && (l2 > 11)) return GREATER_P;
  449. if (l1 < l2) return LOWER_P;
  450. if (l1 > l2) return GREATER_P;
  451. switch(operators[a.value].associativity)
  452. { case L_A: return GREATER_P;
  453. case R_A: return LOWER_P;
  454. case 0: return e5;
  455. }
  456. return GREATER_P;
  457. }
  458. /* --- Converting token to operand ----------------------------------------- */
  459. /* Must match the switch statement below */
  460. int z_system_constant_list[] =
  461. { adjectives_table_SC,
  462. actions_table_SC,
  463. classes_table_SC,
  464. identifiers_table_SC,
  465. preactions_table_SC,
  466. largest_object_SC,
  467. strings_offset_SC,
  468. code_offset_SC,
  469. actual_largest_object_SC,
  470. static_memory_offset_SC,
  471. array_names_offset_SC,
  472. readable_memory_offset_SC,
  473. cpv__start_SC,
  474. cpv__end_SC,
  475. ipv__start_SC,
  476. ipv__end_SC,
  477. array__start_SC,
  478. array__end_SC,
  479. highest_attribute_number_SC,
  480. attribute_names_array_SC,
  481. highest_property_number_SC,
  482. property_names_array_SC,
  483. highest_action_number_SC,
  484. action_names_array_SC,
  485. highest_fake_action_number_SC,
  486. fake_action_names_array_SC,
  487. highest_routine_number_SC,
  488. routine_names_array_SC,
  489. routines_array_SC,
  490. routine_flags_array_SC,
  491. highest_global_number_SC,
  492. global_names_array_SC,
  493. globals_array_SC,
  494. global_flags_array_SC,
  495. highest_array_number_SC,
  496. array_names_array_SC,
  497. array_flags_array_SC,
  498. highest_constant_number_SC,
  499. constant_names_array_SC,
  500. highest_class_number_SC,
  501. class_objects_array_SC,
  502. highest_object_number_SC,
  503. -1 };
  504. static int32 value_of_system_constant_z(int t)
  505. { switch(t)
  506. { case adjectives_table_SC:
  507. return adjectives_offset;
  508. case actions_table_SC:
  509. return actions_offset;
  510. case classes_table_SC:
  511. return class_numbers_offset;
  512. case identifiers_table_SC:
  513. return identifier_names_offset;
  514. case preactions_table_SC:
  515. return preactions_offset;
  516. case largest_object_SC:
  517. return 256 + no_objects - 1;
  518. case strings_offset_SC:
  519. return strings_offset/scale_factor;
  520. case code_offset_SC:
  521. return code_offset/scale_factor;
  522. case actual_largest_object_SC:
  523. return no_objects;
  524. case static_memory_offset_SC:
  525. return static_memory_offset;
  526. case array_names_offset_SC:
  527. return array_names_offset;
  528. case readable_memory_offset_SC:
  529. return Write_Code_At;
  530. case cpv__start_SC:
  531. return prop_values_offset;
  532. case cpv__end_SC:
  533. return class_numbers_offset;
  534. case ipv__start_SC:
  535. return individuals_offset;
  536. case ipv__end_SC:
  537. return variables_offset;
  538. case array__start_SC:
  539. return variables_offset + (MAX_GLOBAL_VARIABLES*WORDSIZE);
  540. case array__end_SC:
  541. return static_memory_offset;
  542. case highest_attribute_number_SC:
  543. return no_attributes-1;
  544. case attribute_names_array_SC:
  545. return attribute_names_offset;
  546. case highest_property_number_SC:
  547. return no_individual_properties-1;
  548. case property_names_array_SC:
  549. return identifier_names_offset + 2;
  550. case highest_action_number_SC:
  551. return no_actions-1;
  552. case action_names_array_SC:
  553. return action_names_offset;
  554. case highest_fake_action_number_SC:
  555. return ((grammar_version_number==1)?256:4096) + no_fake_actions-1;
  556. case fake_action_names_array_SC:
  557. return fake_action_names_offset;
  558. case highest_routine_number_SC:
  559. return no_named_routines-1;
  560. case routine_names_array_SC:
  561. return routine_names_offset;
  562. case routines_array_SC:
  563. return routines_array_offset;
  564. case routine_flags_array_SC:
  565. return routine_flags_array_offset;
  566. case highest_global_number_SC:
  567. return 16 + no_globals-1;
  568. case global_names_array_SC:
  569. return global_names_offset;
  570. case globals_array_SC:
  571. return variables_offset;
  572. case global_flags_array_SC:
  573. return global_flags_array_offset;
  574. case highest_array_number_SC:
  575. return no_arrays-1;
  576. case array_names_array_SC:
  577. return array_names_offset;
  578. case array_flags_array_SC:
  579. return array_flags_array_offset;
  580. case highest_constant_number_SC:
  581. return no_named_constants-1;
  582. case constant_names_array_SC:
  583. return constant_names_offset;
  584. case highest_class_number_SC:
  585. return no_classes-1;
  586. case class_objects_array_SC:
  587. return class_numbers_offset;
  588. case highest_object_number_SC:
  589. return no_objects-1;
  590. }
  591. error_named("System constant not implemented in Z-code",
  592. system_constants.keywords[t]);
  593. return(0);
  594. }
  595. /* Must match the switch statement below */
  596. int glulx_system_constant_list[] =
  597. { classes_table_SC,
  598. identifiers_table_SC,
  599. array_names_offset_SC,
  600. cpv__start_SC,
  601. cpv__end_SC,
  602. dictionary_table_SC,
  603. dynam_string_table_SC,
  604. grammar_table_SC,
  605. actions_table_SC,
  606. globals_array_SC,
  607. -1 };
  608. static int32 value_of_system_constant_g(int t)
  609. {
  610. switch (t) {
  611. case classes_table_SC:
  612. return Write_RAM_At + class_numbers_offset;
  613. case identifiers_table_SC:
  614. return Write_RAM_At + identifier_names_offset;
  615. case array_names_offset_SC:
  616. return Write_RAM_At + array_names_offset;
  617. case cpv__start_SC:
  618. return prop_defaults_offset;
  619. case cpv__end_SC:
  620. return Write_RAM_At + class_numbers_offset;
  621. case dictionary_table_SC:
  622. return dictionary_offset;
  623. case dynam_string_table_SC:
  624. return abbreviations_offset;
  625. case grammar_table_SC:
  626. return grammar_table_offset;
  627. case actions_table_SC:
  628. return actions_offset;
  629. case globals_array_SC:
  630. return variables_offset;
  631. }
  632. error_named("System constant not implemented in Glulx",
  633. system_constants.keywords[t]);
  634. return 0;
  635. }
  636. extern int32 value_of_system_constant(int t)
  637. {
  638. if (!glulx_mode)
  639. return value_of_system_constant_z(t);
  640. else
  641. return value_of_system_constant_g(t);
  642. }
  643. static int evaluate_term(token_data t, assembly_operand *o)
  644. {
  645. /* If the given token is a constant, evaluate it into the operand.
  646. For now, the identifiers are considered variables.
  647. Returns FALSE if it fails to understand type. */
  648. int32 v;
  649. o->marker = t.marker;
  650. o->symtype = t.symtype;
  651. o->symflags = t.symflags;
  652. switch(t.type)
  653. { case LARGE_NUMBER_TT:
  654. v = t.value;
  655. if (!glulx_mode) {
  656. if (v < 0) v = v + 0x10000;
  657. o->type = LONG_CONSTANT_OT;
  658. o->value = v;
  659. }
  660. else {
  661. o->value = v;
  662. o->type = CONSTANT_OT;
  663. }
  664. return(TRUE);
  665. case SMALL_NUMBER_TT:
  666. v = t.value;
  667. if (!glulx_mode) {
  668. if (v < 0) v = v + 0x10000;
  669. o->type = SHORT_CONSTANT_OT;
  670. o->value = v;
  671. }
  672. else {
  673. o->value = v;
  674. set_constant_ot(o);
  675. }
  676. return(TRUE);
  677. case DICTWORD_TT:
  678. /* Find the dictionary address, adding to dictionary if absent */
  679. if (!glulx_mode)
  680. o->type = LONG_CONSTANT_OT;
  681. else
  682. o->type = CONSTANT_OT;
  683. o->value = dictionary_add(t.text, 0x80, 0, 0);
  684. return(TRUE);
  685. case DQ_TT:
  686. /* Create as a static string */
  687. if (!glulx_mode)
  688. o->type = LONG_CONSTANT_OT;
  689. else
  690. o->type = CONSTANT_OT;
  691. o->value = compile_string(t.text, FALSE, FALSE);
  692. return(TRUE);
  693. case VARIABLE_TT:
  694. if (!glulx_mode) {
  695. o->type = VARIABLE_OT;
  696. }
  697. else {
  698. if (t.value >= MAX_LOCAL_VARIABLES) {
  699. o->type = GLOBALVAR_OT;
  700. }
  701. else {
  702. /* This includes "local variable zero", which is really
  703. the stack-pointer magic variable. */
  704. o->type = LOCALVAR_OT;
  705. }
  706. }
  707. o->value = t.value;
  708. return(TRUE);
  709. case SYSFUN_TT:
  710. if (!glulx_mode) {
  711. o->type = VARIABLE_OT;
  712. o->value = t.value + 256;
  713. }
  714. else {
  715. o->type = SYSFUN_OT;
  716. o->value = t.value;
  717. }
  718. system_function_usage[t.value] = 1;
  719. return(TRUE);
  720. case ACTION_TT:
  721. *o = action_of_name(t.text);
  722. return(TRUE);
  723. case SYSTEM_CONSTANT_TT:
  724. /* Certain system constants depend only on the
  725. version number and need no backpatching, as they
  726. are known in advance. We can therefore evaluate
  727. them immediately. */
  728. if (!glulx_mode) {
  729. o->type = LONG_CONSTANT_OT;
  730. switch(t.value)
  731. {
  732. case version_number_SC:
  733. o->type = SHORT_CONSTANT_OT;
  734. o->marker = 0;
  735. v = version_number; break;
  736. case dict_par1_SC:
  737. o->type = SHORT_CONSTANT_OT;
  738. o->marker = 0;
  739. v = (version_number==3)?4:6; break;
  740. case dict_par2_SC:
  741. o->type = SHORT_CONSTANT_OT;
  742. o->marker = 0;
  743. v = (version_number==3)?5:7; break;
  744. case dict_par3_SC:
  745. o->type = SHORT_CONSTANT_OT;
  746. o->marker = 0;
  747. v = (version_number==3)?6:8; break;
  748. case lowest_attribute_number_SC:
  749. case lowest_action_number_SC:
  750. case lowest_routine_number_SC:
  751. case lowest_array_number_SC:
  752. case lowest_constant_number_SC:
  753. case lowest_class_number_SC:
  754. o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 0; break;
  755. case lowest_object_number_SC:
  756. case lowest_property_number_SC:
  757. o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 1; break;
  758. case lowest_global_number_SC:
  759. o->type = SHORT_CONSTANT_OT; o->marker = 0; v = 16; break;
  760. case lowest_fake_action_number_SC:
  761. o->type = LONG_CONSTANT_OT; o->marker = 0;
  762. v = ((grammar_version_number==1)?256:4096); break;
  763. case oddeven_packing_SC:
  764. o->type = SHORT_CONSTANT_OT; o->marker = 0;
  765. v = oddeven_packing_switch; break;
  766. default:
  767. v = t.value;
  768. o->marker = INCON_MV;
  769. break;
  770. }
  771. o->value = v;
  772. }
  773. else {
  774. o->type = CONSTANT_OT;
  775. switch(t.value)
  776. {
  777. /* The three dict_par flags point at the lower byte
  778. of the flag field, because the library is written
  779. to expect one-byte fields, even though the compiler
  780. generates a dictionary with room for two. */
  781. case dict_par1_SC:
  782. o->type = BYTECONSTANT_OT;
  783. o->marker = 0;
  784. v = DICT_ENTRY_FLAG_POS+1;
  785. break;
  786. case dict_par2_SC:
  787. o->type = BYTECONSTANT_OT;
  788. o->marker = 0;
  789. v = DICT_ENTRY_FLAG_POS+3;
  790. break;
  791. case dict_par3_SC:
  792. o->type = BYTECONSTANT_OT;
  793. o->marker = 0;
  794. v = DICT_ENTRY_FLAG_POS+5;
  795. break;
  796. /* ###fix: need to fill more of these in! */
  797. default:
  798. v = t.value;
  799. o->marker = INCON_MV;
  800. break;
  801. }
  802. o->value = v;
  803. }
  804. return(TRUE);
  805. default:
  806. return(FALSE);
  807. }
  808. }
  809. /* --- Emitter ------------------------------------------------------------- */
  810. expression_tree_node *ET;
  811. static int ET_used;
  812. extern void clear_expression_space(void)
  813. { ET_used = 0;
  814. }
  815. static assembly_operand *emitter_stack;
  816. static int *emitter_markers;
  817. static int *emitter_bracket_counts;
  818. #define FUNCTION_VALUE_MARKER 1
  819. #define ARGUMENT_VALUE_MARKER 2
  820. #define OR_VALUE_MARKER 3
  821. static int emitter_sp;
  822. static int is_property_t(int symbol_type)
  823. { return ((symbol_type == PROPERTY_T) || (symbol_type == INDIVIDUAL_PROPERTY_T));
  824. }
  825. static void mark_top_of_emitter_stack(int marker, token_data t)
  826. { if (emitter_sp < 1)
  827. { compiler_error("SR error: Attempt to add a marker to the top of an empty emitter stack");
  828. return;
  829. }
  830. if (expr_trace_level >= 2)
  831. { printf("Marking top of emitter stack (which is ");
  832. print_operand(emitter_stack[emitter_sp-1]);
  833. printf(") as ");
  834. switch(marker)
  835. {
  836. case FUNCTION_VALUE_MARKER:
  837. printf("FUNCTION");
  838. break;
  839. case ARGUMENT_VALUE_MARKER:
  840. printf("ARGUMENT");
  841. break;
  842. case OR_VALUE_MARKER:
  843. printf("OR_VALUE");
  844. break;
  845. default:
  846. printf("UNKNOWN");
  847. break;
  848. }
  849. printf("\n");
  850. }
  851. if (emitter_markers[emitter_sp-1])
  852. { if (marker == ARGUMENT_VALUE_MARKER)
  853. {
  854. warning("Ignoring spurious leading comma");
  855. return;
  856. }
  857. error_named("Missing operand for", t.text);
  858. if (emitter_sp == MAX_EXPRESSION_NODES)
  859. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  860. emitter_markers[emitter_sp] = 0;
  861. emitter_bracket_counts[emitter_sp] = 0;
  862. emitter_stack[emitter_sp] = zero_operand;
  863. emitter_sp++;
  864. }
  865. emitter_markers[emitter_sp-1] = marker;
  866. }
  867. static void add_bracket_layer_to_emitter_stack(int depth)
  868. { /* There's no point in tracking bracket layers that don't fence off any values. */
  869. if (emitter_sp < depth + 1) return;
  870. if (expr_trace_level >= 2)
  871. printf("Adding bracket layer\n");
  872. ++emitter_bracket_counts[emitter_sp-depth-1];
  873. }
  874. static void remove_bracket_layer_from_emitter_stack()
  875. { /* Bracket layers that don't fence off any values will not have been tracked. */
  876. if (emitter_sp < 2) return;
  877. if (expr_trace_level >= 2)
  878. printf("Removing bracket layer\n");
  879. if (emitter_bracket_counts[emitter_sp-2] <= 0)
  880. { compiler_error("SR error: Attempt to remove a nonexistent bracket layer from the emitter stack");
  881. return;
  882. }
  883. --emitter_bracket_counts[emitter_sp-2];
  884. }
  885. static void emit_token(token_data t)
  886. { assembly_operand o1, o2; int arity, stack_size, i;
  887. int op_node_number, operand_node_number, previous_node_number;
  888. int32 x;
  889. if (expr_trace_level >= 2)
  890. { printf("Output: %-19s%21s ", t.text, "");
  891. for (i=0; i<emitter_sp; i++)
  892. { print_operand(emitter_stack[i]); printf(" ");
  893. if (emitter_markers[i] == FUNCTION_VALUE_MARKER) printf(":FUNCTION ");
  894. if (emitter_markers[i] == ARGUMENT_VALUE_MARKER) printf(":ARGUMENT ");
  895. if (emitter_markers[i] == OR_VALUE_MARKER) printf(":OR ");
  896. if (emitter_bracket_counts[i]) printf(":BRACKETS(%d) ", emitter_bracket_counts[i]);
  897. }
  898. printf("\n");
  899. }
  900. if (t.type == SUBOPEN_TT) return;
  901. stack_size = 0;
  902. while ((stack_size < emitter_sp) &&
  903. !emitter_markers[emitter_sp-stack_size-1] &&
  904. !emitter_bracket_counts[emitter_sp-stack_size-1])
  905. stack_size++;
  906. if (t.type == SUBCLOSE_TT)
  907. { if (stack_size < emitter_sp && emitter_bracket_counts[emitter_sp-stack_size-1])
  908. { if (stack_size == 0)
  909. { error("No expression between brackets '(' and ')'");
  910. emitter_stack[emitter_sp] = zero_operand;
  911. emitter_markers[emitter_sp] = 0;
  912. emitter_bracket_counts[emitter_sp] = 0;
  913. ++emitter_sp;
  914. }
  915. else if (stack_size < 1)
  916. compiler_error("SR error: emitter stack empty in subexpression");
  917. else if (stack_size > 1)
  918. compiler_error("SR error: emitter stack overfull in subexpression");
  919. remove_bracket_layer_from_emitter_stack();
  920. }
  921. return;
  922. }
  923. if (t.type != OP_TT)
  924. { emitter_markers[emitter_sp] = 0;
  925. emitter_bracket_counts[emitter_sp] = 0;
  926. if (emitter_sp == MAX_EXPRESSION_NODES)
  927. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  928. if (!evaluate_term(t, &(emitter_stack[emitter_sp++])))
  929. compiler_error_named("Emit token error:", t.text);
  930. return;
  931. }
  932. /* A comma is argument-separating if it follows an argument (or a function
  933. call, since we ignore spurious leading commas in function argument lists)
  934. with no intervening brackets. Function calls are variadic, so we don't
  935. apply argument-separating commas. */
  936. if (t.value == COMMA_OP &&
  937. stack_size < emitter_sp &&
  938. (emitter_markers[emitter_sp-stack_size-1] == ARGUMENT_VALUE_MARKER ||
  939. emitter_markers[emitter_sp-stack_size-1] == FUNCTION_VALUE_MARKER) &&
  940. !emitter_bracket_counts[emitter_sp-stack_size-1])
  941. { if (expr_trace_level >= 2)
  942. printf("Treating comma as argument-separating\n");
  943. return;
  944. }
  945. if (t.value == OR_OP)
  946. return;
  947. arity = 1;
  948. if (t.value == FCALL_OP)
  949. { if (expr_trace_level >= 3)
  950. { printf("FCALL_OP finds marker stack: ");
  951. for (x=0; x<emitter_sp; x++) printf("%d ", emitter_markers[x]);
  952. printf("\n");
  953. }
  954. if (emitter_markers[emitter_sp-1] == ARGUMENT_VALUE_MARKER)
  955. warning("Ignoring spurious trailing comma");
  956. while (emitter_markers[emitter_sp-arity] != FUNCTION_VALUE_MARKER)
  957. {
  958. if ((glulx_mode &&
  959. emitter_stack[emitter_sp-arity].type == SYSFUN_OT) ||
  960. (!glulx_mode &&
  961. emitter_stack[emitter_sp-arity].type == VARIABLE_OT &&
  962. emitter_stack[emitter_sp-arity].value >= 256 &&
  963. emitter_stack[emitter_sp-arity].value < 288))
  964. { int index = emitter_stack[emitter_sp-arity].value;
  965. if(!glulx_mode)
  966. index -= 256;
  967. if(index > 0 && index < NUMBER_SYSTEM_FUNCTIONS)
  968. error_named("System function name used as a value:", system_functions.keywords[index]);
  969. else
  970. compiler_error("Found unnamed system function used as a value");
  971. emitter_stack[emitter_sp-arity] = zero_operand;
  972. }
  973. ++arity;
  974. }
  975. }
  976. else
  977. { arity = 1;
  978. if (operators[t.value].usage == IN_U) arity = 2;
  979. if (operators[t.value].precedence == 3)
  980. { arity = 2;
  981. x = emitter_sp-1;
  982. if(!emitter_markers[x] && !emitter_bracket_counts[x])
  983. { for (--x; emitter_markers[x] == OR_VALUE_MARKER && !emitter_bracket_counts[x]; --x)
  984. { ++arity;
  985. ++stack_size;
  986. }
  987. for (;x >= 0 && !emitter_markers[x] && !emitter_bracket_counts[x]; --x)
  988. ++stack_size;
  989. }
  990. }
  991. if (arity > stack_size)
  992. { error_named("Missing operand for", t.text);
  993. while (arity > stack_size)
  994. { if (emitter_sp == MAX_EXPRESSION_NODES)
  995. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  996. emitter_markers[emitter_sp] = 0;
  997. emitter_bracket_counts[emitter_sp] = 0;
  998. emitter_stack[emitter_sp] = zero_operand;
  999. emitter_sp++;
  1000. stack_size++;
  1001. }
  1002. }
  1003. }
  1004. /* pseudo-typecheck in 6.30 */
  1005. for (i = 1; i <= arity; i++)
  1006. {
  1007. o1 = emitter_stack[emitter_sp - i];
  1008. if (is_property_t(o1.symtype) ) {
  1009. switch(t.value)
  1010. {
  1011. case FCALL_OP:
  1012. case SETEQUALS_OP: case NOTEQUAL_OP:
  1013. case CONDEQUALS_OP:
  1014. case PROVIDES_OP: case NOTPROVIDES_OP:
  1015. case PROP_ADD_OP: case PROP_NUM_OP:
  1016. case SUPERCLASS_OP:
  1017. case MPROP_ADD_OP: case MESSAGE_OP:
  1018. case PROPERTY_OP:
  1019. if (i < arity) break;
  1020. case GE_OP: case LE_OP:
  1021. if ((i < arity) && (o1.symflags & STAR_SFLAG)) break;
  1022. default:
  1023. warning("Property name in expression is not qualified by object");
  1024. }
  1025. } /* if (is_property_t */
  1026. }
  1027. switch(arity)
  1028. { case 1:
  1029. o1 = emitter_stack[emitter_sp - 1];
  1030. if ((o1.marker == 0) && is_constant_ot(o1.type))
  1031. { switch(t.value)
  1032. { case UNARY_MINUS_OP: x = -o1.value; goto FoldConstant;
  1033. case ARTNOT_OP:
  1034. if (!glulx_mode)
  1035. x = (~o1.value) & 0xffff;
  1036. else
  1037. x = (~o1.value) & 0xffffffff;
  1038. goto FoldConstant;
  1039. case LOGNOT_OP:
  1040. if (o1.value != 0) x=0; else x=1;
  1041. goto FoldConstant;
  1042. }
  1043. }
  1044. break;
  1045. case 2:
  1046. o1 = emitter_stack[emitter_sp - 2];
  1047. o2 = emitter_stack[emitter_sp - 1];
  1048. if ((o1.marker == 0) && (o2.marker == 0)
  1049. && is_constant_ot(o1.type) && is_constant_ot(o2.type))
  1050. {
  1051. int32 ov1, ov2;
  1052. if (glulx_mode)
  1053. { ov1 = o1.value;
  1054. ov2 = o2.value;
  1055. }
  1056. else
  1057. { ov1 = (o1.value >= 0x8000) ? (o1.value - 0x10000) : o1.value;
  1058. ov2 = (o2.value >= 0x8000) ? (o2.value - 0x10000) : o2.value;
  1059. }
  1060. switch(t.value)
  1061. {
  1062. case PLUS_OP: x = ov1 + ov2; goto FoldConstantC;
  1063. case MINUS_OP: x = ov1 - ov2; goto FoldConstantC;
  1064. case TIMES_OP: x = ov1 * ov2; goto FoldConstantC;
  1065. case DIVIDE_OP:
  1066. case REMAINDER_OP:
  1067. if (ov2 == 0)
  1068. error("Division of constant by zero");
  1069. else
  1070. if (t.value == DIVIDE_OP) {
  1071. if (ov2 < 0) {
  1072. ov1 = -ov1;
  1073. ov2 = -ov2;
  1074. }
  1075. if (ov1 >= 0)
  1076. x = ov1 / ov2;
  1077. else
  1078. x = -((-ov1) / ov2);
  1079. }
  1080. else {
  1081. if (ov2 < 0) {
  1082. ov2 = -ov2;
  1083. }
  1084. if (ov1 >= 0)
  1085. x = ov1 % ov2;
  1086. else
  1087. x = -((-ov1) % ov2);
  1088. }
  1089. goto FoldConstant;
  1090. case ARTAND_OP: x = o1.value & o2.value; goto FoldConstant;
  1091. case ARTOR_OP: x = o1.value | o2.value; goto FoldConstant;
  1092. case CONDEQUALS_OP:
  1093. if (o1.value == o2.value) x = 1; else x = 0;
  1094. goto FoldConstant;
  1095. case NOTEQUAL_OP:
  1096. if (o1.value != o2.value) x = 1; else x = 0;
  1097. goto FoldConstant;
  1098. case GE_OP:
  1099. if (o1.value >= o2.value) x = 1; else x = 0;
  1100. goto FoldConstant;
  1101. case GREATER_OP:
  1102. if (o1.value > o2.value) x = 1; else x = 0;
  1103. goto FoldConstant;
  1104. case LE_OP:
  1105. if (o1.value <= o2.value) x = 1; else x = 0;
  1106. goto FoldConstant;
  1107. case LESS_OP:
  1108. if (o1.value < o2.value) x = 1; else x = 0;
  1109. goto FoldConstant;
  1110. case LOGAND_OP:
  1111. if ((o1.value != 0) && (o2.value != 0)) x=1; else x=0;
  1112. goto FoldConstant;
  1113. case LOGOR_OP:
  1114. if ((o1.value != 0) || (o2.value != 0)) x=1; else x=0;
  1115. goto FoldConstant;
  1116. }
  1117. }
  1118. }
  1119. op_node_number = ET_used++;
  1120. if (op_node_number == MAX_EXPRESSION_NODES)
  1121. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1122. ET[op_node_number].operator_number = t.value;
  1123. ET[op_node_number].up = -1;
  1124. ET[op_node_number].down = -1;
  1125. ET[op_node_number].right = -1;
  1126. /* This statement is redundant, but prevents compilers from wrongly
  1127. issuing a "used before it was assigned a value" error: */
  1128. previous_node_number = 0;
  1129. for (i = emitter_sp-arity; i != emitter_sp; i++)
  1130. {
  1131. if (expr_trace_level >= 3)
  1132. printf("i=%d, emitter_sp=%d, arity=%d, ETU=%d\n",
  1133. i, emitter_sp, arity, ET_used);
  1134. if (emitter_stack[i].type == EXPRESSION_OT)
  1135. operand_node_number = emitter_stack[i].value;
  1136. else
  1137. { operand_node_number = ET_used++;
  1138. if (operand_node_number == MAX_EXPRESSION_NODES)
  1139. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1140. ET[operand_node_number].down = -1;
  1141. ET[operand_node_number].value = emitter_stack[i];
  1142. }
  1143. ET[operand_node_number].up = op_node_number;
  1144. ET[operand_node_number].right = -1;
  1145. if (i == emitter_sp - arity)
  1146. { ET[op_node_number].down = operand_node_number;
  1147. }
  1148. else
  1149. { ET[previous_node_number].right = operand_node_number;
  1150. }
  1151. previous_node_number = operand_node_number;
  1152. }
  1153. emitter_sp = emitter_sp - arity + 1;
  1154. emitter_stack[emitter_sp - 1].type = EXPRESSION_OT;
  1155. emitter_stack[emitter_sp - 1].value = op_node_number;
  1156. emitter_stack[emitter_sp - 1].marker = 0;
  1157. emitter_markers[emitter_sp - 1] = 0;
  1158. emitter_bracket_counts[emitter_sp - 1] = 0;
  1159. /* Remove the marker for the brackets implied by operator precedence */
  1160. remove_bracket_layer_from_emitter_stack();
  1161. return;
  1162. FoldConstantC:
  1163. /* In Glulx, skip this test; we can't check out-of-range errors
  1164. for 32-bit arithmetic. */
  1165. if (!glulx_mode && ((x<-32768) || (x > 32767)))
  1166. { char folding_error[40];
  1167. int32 ov1 = (o1.value >= 0x8000) ? (o1.value - 0x10000) : o1.value;
  1168. int32 ov2 = (o2.value >= 0x8000) ? (o2.value - 0x10000) : o2.value;
  1169. switch(t.value)
  1170. {
  1171. case PLUS_OP:
  1172. sprintf(folding_error, "%d + %d = %d", ov1, ov2, x);
  1173. break;
  1174. case MINUS_OP:
  1175. sprintf(folding_error, "%d - %d = %d", ov1, ov2, x);
  1176. break;
  1177. case TIMES_OP:
  1178. sprintf(folding_error, "%d * %d = %d", ov1, ov2, x);
  1179. break;
  1180. }
  1181. error_named("Signed arithmetic on compile-time constants overflowed \
  1182. the range -32768 to +32767:", folding_error);
  1183. }
  1184. FoldConstant:
  1185. if (!glulx_mode) {
  1186. while (x < 0) x = x + 0x10000;
  1187. x = x & 0xffff;
  1188. }
  1189. else {
  1190. x = x & 0xffffffff;
  1191. }
  1192. emitter_sp = emitter_sp - arity + 1;
  1193. if (!glulx_mode) {
  1194. if (x<256)
  1195. emitter_stack[emitter_sp - 1].type = SHORT_CONSTANT_OT;
  1196. else emitter_stack[emitter_sp - 1].type = LONG_CONSTANT_OT;
  1197. }
  1198. else {
  1199. if (x == 0)
  1200. emitter_stack[emitter_sp - 1].type = ZEROCONSTANT_OT;
  1201. else if (x >= -128 && x <= 127)
  1202. emitter_stack[emitter_sp - 1].type = BYTECONSTANT_OT;
  1203. else if (x >= -32768 && x <= 32767)
  1204. emitter_stack[emitter_sp - 1].type = HALFCONSTANT_OT;
  1205. else
  1206. emitter_stack[emitter_sp - 1].type = CONSTANT_OT;
  1207. }
  1208. emitter_stack[emitter_sp - 1].value = x;
  1209. emitter_stack[emitter_sp - 1].marker = 0;
  1210. emitter_markers[emitter_sp - 1] = 0;
  1211. emitter_bracket_counts[emitter_sp - 1] = 0;
  1212. if (expr_trace_level >= 2)
  1213. { printf("Folding constant to: ");
  1214. print_operand(emitter_stack[emitter_sp - 1]);
  1215. printf("\n");
  1216. }
  1217. /* Remove the marker for the brackets implied by operator precedence */
  1218. remove_bracket_layer_from_emitter_stack();
  1219. return;
  1220. }
  1221. /* --- Pretty printing ----------------------------------------------------- */
  1222. static void show_node(int n, int depth, int annotate)
  1223. { int j;
  1224. for (j=0; j<2*depth+2; j++) printf(" ");
  1225. if (ET[n].down == -1)
  1226. { print_operand(ET[n].value);
  1227. if (annotate && (ET[n].value.marker != 0))
  1228. printf(" [%s]", describe_mv(ET[n].value.marker));
  1229. printf("\n");
  1230. }
  1231. else
  1232. { printf("%s ", operators[ET[n].operator_number].description);
  1233. j = operators[ET[n].operator_number].precedence;
  1234. if ((annotate) && ((j==2) || (j==3)))
  1235. { printf(" %d|%d ", ET[n].true_label, ET[n].false_label);
  1236. if (ET[n].label_after != -1) printf(" def %d after ",
  1237. ET[n].label_after);
  1238. if (ET[n].to_expression) printf(" con to expr ");
  1239. }
  1240. printf("\n");
  1241. show_node(ET[n].down, depth+1, annotate);
  1242. }
  1243. if (ET[n].right != -1) show_node(ET[n].right, depth, annotate);
  1244. }
  1245. extern void show_tree(assembly_operand AO, int annotate)
  1246. { if (AO.type == EXPRESSION_OT) show_node(AO.value, 0, annotate);
  1247. else
  1248. { printf("Constant: "); print_operand(AO);
  1249. if (annotate && (AO.marker != 0))
  1250. printf(" [%s]", describe_mv(AO.marker));
  1251. printf("\n");
  1252. }
  1253. }
  1254. /* --- Lvalue transformations ---------------------------------------------- */
  1255. /* This only gets called in Z-code, since Glulx doesn't distinguish
  1256. individual property operators from general ones. */
  1257. static void check_property_operator(int from_node)
  1258. { int below = ET[from_node].down;
  1259. int opnum = ET[from_node].operator_number;
  1260. ASSERT_ZCODE();
  1261. if (veneer_mode) return;
  1262. if ((below != -1) && (ET[below].right != -1))
  1263. { int n = ET[below].right, flag = FALSE;
  1264. if ((ET[n].down == -1)
  1265. && ((ET[n].value.type == LONG_CONSTANT_OT)
  1266. || (ET[n].value.type == SHORT_CONSTANT_OT))
  1267. && ((ET[n].value.value > 0) && (ET[n].value.value < 64))
  1268. && ((!module_switch) || (ET[n].value.marker == 0)))
  1269. flag = TRUE;
  1270. if (!flag)
  1271. { switch(opnum)
  1272. { case PROPERTY_OP: opnum = MESSAGE_OP; break;
  1273. case PROP_ADD_OP: opnum = MPROP_ADD_OP; break;
  1274. case PROP_NUM_OP: opnum = MPROP_NUM_OP; break;
  1275. }
  1276. }
  1277. ET[from_node].operator_number = opnum;
  1278. }
  1279. if (below != -1)
  1280. check_property_operator(below);
  1281. if (ET[from_node].right != -1)
  1282. check_property_operator(ET[from_node].right);
  1283. }
  1284. static void check_lvalues(int from_node)
  1285. { int below = ET[from_node].down;
  1286. int opnum = ET[from_node].operator_number, opnum_below;
  1287. int lvalue_form, i, j;
  1288. if (below != -1)
  1289. {
  1290. if ((opnum == FCALL_OP) && (ET[below].down != -1))
  1291. { opnum_below = ET[below].operator_number;
  1292. if ((opnum_below == PROPERTY_OP) || (opnum_below == MESSAGE_OP))
  1293. { i = ET[ET[from_node].down].right;
  1294. ET[from_node].down = ET[below].down;
  1295. ET[ET[below].down].up = from_node;
  1296. ET[ET[ET[below].down].right].up = from_node;
  1297. ET[ET[ET[below].down].right].right = i;
  1298. opnum = PROP_CALL_OP;
  1299. ET[from_node].operator_number = opnum;
  1300. }
  1301. }
  1302. if (operators[opnum].requires_lvalue)
  1303. { opnum_below = ET[below].operator_number;
  1304. if (ET[below].down == -1)
  1305. { if (!is_variable_ot(ET[below].value.type))
  1306. { error("'=' applied to undeclared variable");
  1307. goto LvalueError;
  1308. }
  1309. }
  1310. else
  1311. { lvalue_form=0;
  1312. switch(opnum)
  1313. { case SETEQUALS_OP:
  1314. switch(opnum_below)
  1315. { case ARROW_OP: lvalue_form = ARROW_SETEQUALS_OP; break;
  1316. case DARROW_OP: lvalue_form = DARROW_SETEQUALS_OP; break;
  1317. case MESSAGE_OP: lvalue_form = MESSAGE_SETEQUALS_OP; break;
  1318. case PROPERTY_OP: lvalue_form = PROPERTY_SETEQUALS_OP; break;
  1319. }
  1320. break;
  1321. case INC_OP:
  1322. switch(opnum_below)
  1323. { case ARROW_OP: lvalue_form = ARROW_INC_OP; break;
  1324. case DARROW_OP: lvalue_form = DARROW_INC_OP; break;
  1325. case MESSAGE_OP: lvalue_form = MESSAGE_INC_OP; break;
  1326. case PROPERTY_OP: lvalue_form = PROPERTY_INC_OP; break;
  1327. }
  1328. break;
  1329. case POST_INC_OP:
  1330. switch(opnum_below)
  1331. { case ARROW_OP: lvalue_form = ARROW_POST_INC_OP; break;
  1332. case DARROW_OP: lvalue_form = DARROW_POST_INC_OP; break;
  1333. case MESSAGE_OP: lvalue_form = MESSAGE_POST_INC_OP; break;
  1334. case PROPERTY_OP: lvalue_form = PROPERTY_POST_INC_OP; break;
  1335. }
  1336. break;
  1337. case DEC_OP:
  1338. switch(opnum_below)
  1339. { case ARROW_OP: lvalue_form = ARROW_DEC_OP; break;
  1340. case DARROW_OP: lvalue_form = DARROW_DEC_OP; break;
  1341. case MESSAGE_OP: lvalue_form = MESSAGE_DEC_OP; break;
  1342. case PROPERTY_OP: lvalue_form = PROPERTY_DEC_OP; break;
  1343. }
  1344. break;
  1345. case POST_DEC_OP:
  1346. switch(opnum_below)
  1347. { case ARROW_OP: lvalue_form = ARROW_POST_DEC_OP; break;
  1348. case DARROW_OP: lvalue_form = DARROW_POST_DEC_OP; break;
  1349. case MESSAGE_OP: lvalue_form = MESSAGE_POST_DEC_OP; break;
  1350. case PROPERTY_OP: lvalue_form = PROPERTY_POST_DEC_OP; break;
  1351. }
  1352. break;
  1353. }
  1354. if (lvalue_form == 0)
  1355. { error_named("'=' applied to",
  1356. (char *) operators[opnum_below].description);
  1357. goto LvalueError;
  1358. }
  1359. /* Transform from_node from_node
  1360. | \ | \\\ \
  1361. below value to value
  1362. | \\\
  1363. */
  1364. ET[from_node].operator_number = lvalue_form;
  1365. i = ET[below].down;
  1366. ET[from_node].down = i;
  1367. while (i != -1)
  1368. { ET[i].up = from_node;
  1369. j = i;
  1370. i = ET[i].right;
  1371. }
  1372. ET[j].right = ET[below].right;
  1373. }
  1374. }
  1375. check_lvalues(below);
  1376. }
  1377. if (ET[from_node].right != -1)
  1378. check_lvalues(ET[from_node].right);
  1379. return;
  1380. LvalueError:
  1381. ET[from_node].down = -1;
  1382. ET[from_node].value = zero_operand;
  1383. if (ET[from_node].right != -1)
  1384. check_lvalues(ET[from_node].right);
  1385. }
  1386. /* --- Tree surgery for conditionals --------------------------------------- */
  1387. static void negate_condition(int n)
  1388. { int i;
  1389. if (ET[n].right != -1) negate_condition(ET[n].right);
  1390. if (ET[n].down == -1) return;
  1391. i = operators[ET[n].operator_number].negation;
  1392. if (i!=0) ET[n].operator_number = i;
  1393. if (operators[i].precedence==2) negate_condition(ET[n].down);
  1394. }
  1395. static void delete_negations(int n, int context)
  1396. {
  1397. /* Recursively apply
  1398. ~~(x && y) = ~~x || ~~y
  1399. ~~(x || y) = ~~x && ~~y
  1400. ~~(x == y) = x ~= y
  1401. (etc) to delete the ~~ operator from the tree. Since this is
  1402. depth first, the ~~ being deleted has no ~~s beneath it, which
  1403. is important to make "negate_condition" work. */
  1404. int i;
  1405. if (ET[n].right != -1) delete_negations(ET[n].right, context);
  1406. if (ET[n].down == -1) return;
  1407. delete_negations(ET[n].down, context);
  1408. if (ET[n].operator_number == LOGNOT_OP)
  1409. { negate_condition(ET[n].down);
  1410. ET[n].operator_number
  1411. = ET[ET[n].down].operator_number;
  1412. ET[n].down = ET[ET[n].down].down;
  1413. i = ET[n].down;
  1414. while(i != -1) { ET[i].up = n; i = ET[i].right; }
  1415. }
  1416. }
  1417. static void insert_exp_to_cond(int n, int context)
  1418. {
  1419. /* Insert a ~= test when an expression is used as a condition.
  1420. Check for possible confusion over = and ==, e.g. "if (a = 1) ..." */
  1421. int new, i;
  1422. if (ET[n].right != -1) insert_exp_to_cond(ET[n].right, context);
  1423. if (ET[n].down == -1)
  1424. { if (context==CONDITION_CONTEXT)
  1425. { new = ET_used++;
  1426. if (new == MAX_EXPRESSION_NODES)
  1427. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1428. ET[new] = ET[n];
  1429. ET[n].down = new; ET[n].operator_number = NONZERO_OP;
  1430. ET[new].up = n; ET[new].right = -1;
  1431. }
  1432. return;
  1433. }
  1434. switch(operators[ET[n].operator_number].precedence)
  1435. { case 3: /* Conditionals have level 3 */
  1436. context = QUANTITY_CONTEXT;
  1437. break;
  1438. case 2: /* Logical operators level 2 */
  1439. context = CONDITION_CONTEXT;
  1440. break;
  1441. case 1: /* Forms of '=' have level 1 */
  1442. if (context == CONDITION_CONTEXT)
  1443. warning("'=' used as condition: '==' intended?");
  1444. default:
  1445. if (context != CONDITION_CONTEXT) break;
  1446. new = ET_used++;
  1447. if (new == MAX_EXPRESSION_NODES)
  1448. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1449. ET[new] = ET[n];
  1450. ET[n].down = new; ET[n].operator_number = NONZERO_OP;
  1451. ET[new].up = n; ET[new].right = -1;
  1452. i = ET[new].down;
  1453. while (i!= -1) { ET[i].up = new; i = ET[i].right; }
  1454. context = QUANTITY_CONTEXT; n = new;
  1455. }
  1456. insert_exp_to_cond(ET[n].down, context);
  1457. }
  1458. static unsigned int etoken_num_children(int n)
  1459. {
  1460. int count = 0;
  1461. int i;
  1462. i = ET[n].down;
  1463. if (i == -1) { return 0; }
  1464. do {
  1465. count++;
  1466. i = ET[i].right;
  1467. } while (i!=-1);
  1468. return count;
  1469. }
  1470. static void func_args_on_stack(int n, int context)
  1471. {
  1472. /* Make sure that the arguments of every function-call expression
  1473. are stored to the stack. If any aren't (ie, if any arguments are
  1474. constants or variables), cover them with push operators.
  1475. (The very first argument does not need to be so treated, because
  1476. it's the function address, not a function argument. We also
  1477. skip the treatment for most system functions.) */
  1478. int new, pn, fnaddr, opnum;
  1479. ASSERT_GLULX();
  1480. if (ET[n].right != -1)
  1481. func_args_on_stack(ET[n].right, context);
  1482. if (ET[n].down == -1) {
  1483. pn = ET[n].up;
  1484. if (pn != -1) {
  1485. opnum = ET[pn].operator_number;
  1486. if (opnum == FCALL_OP
  1487. || opnum == MESSAGE_CALL_OP
  1488. || opnum == PROP_CALL_OP) {
  1489. /* If it's an FCALL, get the operand which contains the function
  1490. address (or system-function number) */
  1491. if (opnum == MESSAGE_CALL_OP
  1492. || opnum == PROP_CALL_OP
  1493. || ((fnaddr=ET[pn].down) != n
  1494. && (ET[fnaddr].value.type != SYSFUN_OT
  1495. || ET[fnaddr].value.value == INDIRECT_SYSF
  1496. || ET[fnaddr].value.value == GLK_SYSF))) {
  1497. if (etoken_num_children(pn) > (unsigned int)(opnum == FCALL_OP ? 4:3)) {
  1498. new = ET_used++;
  1499. if (new == MAX_EXPRESSION_NODES)
  1500. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1501. ET[new] = ET[n];
  1502. ET[n].down = new;
  1503. ET[n].operator_number = PUSH_OP;
  1504. ET[new].up = n;
  1505. ET[new].right = -1;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. return;
  1511. }
  1512. func_args_on_stack(ET[n].down, context);
  1513. }
  1514. static assembly_operand check_conditions(assembly_operand AO, int context)
  1515. { int n;
  1516. if (AO.type != EXPRESSION_OT)
  1517. { if (context != CONDITION_CONTEXT) return AO;
  1518. n = ET_used++;
  1519. if (n == MAX_EXPRESSION_NODES)
  1520. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1521. ET[n].down = -1;
  1522. ET[n].up = -1;
  1523. ET[n].right = -1;
  1524. ET[n].value = AO;
  1525. AO.type = EXPRESSION_OT;
  1526. AO.value = n;
  1527. AO.marker = 0;
  1528. }
  1529. insert_exp_to_cond(AO.value, context);
  1530. delete_negations(AO.value, context);
  1531. if (glulx_mode)
  1532. func_args_on_stack(AO.value, context);
  1533. return AO;
  1534. }
  1535. /* --- Shift-reduce parser ------------------------------------------------- */
  1536. static int sr_sp;
  1537. static token_data *sr_stack;
  1538. extern assembly_operand parse_expression(int context)
  1539. {
  1540. /* Parses an expression, evaluating it as a constant if possible.
  1541. Possible contexts are:
  1542. VOID_CONTEXT the expression is used as a statement, so that
  1543. its value will be thrown away and it only
  1544. needs to exist for any resulting side-effects
  1545. (function calls and assignments)
  1546. CONDITION_CONTEXT the result must be a condition
  1547. CONSTANT_CONTEXT there is required to be a constant result
  1548. (so that, for instance, comma becomes illegal)
  1549. QUANTITY_CONTEXT the default: a quantity is to be specified
  1550. ACTION_Q_CONTEXT like QUANTITY_CONTEXT, but postfixed brackets
  1551. at the top level do not indicate function call:
  1552. used for e.g.
  1553. <Insert button (random(pocket1, pocket2))>
  1554. RETURN_Q_CONTEXT like QUANTITY_CONTEXT, but a single property
  1555. name does not generate a warning
  1556. ASSEMBLY_CONTEXT a quantity which cannot use the '->' operator
  1557. (needed for assembly language to indicate
  1558. store destinations)
  1559. FORINIT_CONTEXT a quantity which cannot use an (unbracketed)
  1560. '::' operator
  1561. ARRAY_CONTEXT like CONSTANT_CONTEXT, but where an unbracketed
  1562. minus sign is ambiguous, and brackets always
  1563. indicate subexpressions, not function calls
  1564. Return value: an assembly operand.
  1565. If the type is OMITTED_OT, then the expression has no resulting value.
  1566. If the type is EXPRESSION_OT, then the value will need to be
  1567. calculated at run-time by code compiled from the expression tree
  1568. whose root node-number is the operand value.
  1569. Otherwise the assembly operand is the value of the expression, which
  1570. is constant and thus known at compile time.
  1571. If an error has occurred in the expression, which recovery from was
  1572. not possible, then the return is (short constant) 0. This should
  1573. minimise the chance of a cascade of further error messages.
  1574. */
  1575. token_data a, b, pop; int i;
  1576. assembly_operand AO;
  1577. superclass_allowed = (context != FORINIT_CONTEXT);
  1578. if (context == FORINIT_CONTEXT) context = VOID_CONTEXT;
  1579. comma_allowed = (context == VOID_CONTEXT);
  1580. arrow_allowed = (context != ASSEMBLY_CONTEXT);
  1581. bare_prop_allowed = (context == RETURN_Q_CONTEXT);
  1582. array_init_ambiguity = ((context == ARRAY_CONTEXT) ||
  1583. (context == ASSEMBLY_CONTEXT));
  1584. action_ambiguity = (context == ACTION_Q_CONTEXT);
  1585. if (context == ASSEMBLY_CONTEXT) context = QUANTITY_CONTEXT;
  1586. if (context == ACTION_Q_CONTEXT) context = QUANTITY_CONTEXT;
  1587. if (context == RETURN_Q_CONTEXT) context = QUANTITY_CONTEXT;
  1588. if (context == ARRAY_CONTEXT) context = CONSTANT_CONTEXT;
  1589. etoken_count = 0;
  1590. inserting_token = FALSE;
  1591. emitter_sp = 0;
  1592. bracket_level = 0;
  1593. previous_token.text = "$";
  1594. previous_token.type = ENDEXP_TT;
  1595. previous_token.value = 0;
  1596. sr_sp = 1;
  1597. sr_stack[0] = previous_token;
  1598. AO = zero_operand;
  1599. statements.enabled = FALSE;
  1600. directives.enabled = FALSE;
  1601. if (get_next_etoken() == FALSE)
  1602. { ebf_error("expression", token_text);
  1603. return AO;
  1604. }
  1605. do
  1606. { if (expr_trace_level >= 2)
  1607. { printf("Input: %-20s", current_token.text);
  1608. for (i=0; i<sr_sp; i++) printf("%s ", sr_stack[i].text);
  1609. printf("\n");
  1610. }
  1611. if (expr_trace_level >= 3) printf("ET_used = %d\n", ET_used);
  1612. if (sr_sp == 0)
  1613. { compiler_error("SR error: stack empty");
  1614. return(AO);
  1615. }
  1616. a = sr_stack[sr_sp-1]; b = current_token;
  1617. if ((a.type == ENDEXP_TT) && (b.type == ENDEXP_TT))
  1618. { if (emitter_sp == 0)
  1619. { compiler_error("SR error: emitter stack empty");
  1620. return AO;
  1621. }
  1622. if (emitter_sp > 1)
  1623. { compiler_error("SR error: emitter stack overfull");
  1624. return AO;
  1625. }
  1626. AO = emitter_stack[0];
  1627. if (AO.type == EXPRESSION_OT)
  1628. { if (expr_trace_level >= 3)
  1629. { printf("Tree before lvalue checking:\n");
  1630. show_tree(AO, FALSE);
  1631. }
  1632. if (!glulx_mode)
  1633. check_property_operator(AO.value);
  1634. check_lvalues(AO.value);
  1635. ET[AO.value].up = -1;
  1636. }
  1637. else {
  1638. if ((context != CONSTANT_CONTEXT) && is_property_t(AO.symtype)
  1639. && (arrow_allowed) && (!bare_prop_allowed))
  1640. warning("Bare property name found. \"self.prop\" intended?");
  1641. }
  1642. check_conditions(AO, context);
  1643. if (context == CONSTANT_CONTEXT)
  1644. if (!is_constant_ot(AO.type))
  1645. { AO = zero_operand;
  1646. ebf_error("constant", "<expression>");
  1647. }
  1648. put_token_back();
  1649. return(AO);
  1650. }
  1651. switch(find_prec(a,b))
  1652. {
  1653. case e5: /* Associativity error */
  1654. error_named("Brackets mandatory to clarify order of:",
  1655. a.text);
  1656. case LOWER_P:
  1657. case EQUAL_P:
  1658. if (sr_sp == MAX_EXPRESSION_NODES)
  1659. memoryerror("MAX_EXPRESSION_NODES", MAX_EXPRESSION_NODES);
  1660. sr_stack[sr_sp++] = b;
  1661. switch(b.type)
  1662. {
  1663. case SUBOPEN_TT:
  1664. if (sr_sp >= 2 && sr_stack[sr_sp-2].type == OP_TT && sr_stack[sr_sp-2].value == FCALL_OP)
  1665. mark_top_of_emitter_stack(FUNCTION_VALUE_MARKER, b);
  1666. else
  1667. add_bracket_layer_to_emitter_stack(0);
  1668. break;
  1669. case OP_TT:
  1670. switch(b.value){
  1671. case OR_OP:
  1672. if (sr_stack[sr_sp-2].type == OP_TT &&
  1673. operators[sr_stack[sr_sp-2].value].precedence == 3)
  1674. mark_top_of_emitter_stack(OR_VALUE_MARKER, b);
  1675. else
  1676. { error("'or' not between values to the right of a condition");
  1677. /* Convert to + for error recovery purposes */
  1678. sr_stack[sr_sp-1].value = PLUS_OP;
  1679. }
  1680. break;
  1681. case COMMA_OP:
  1682. {
  1683. /* A comma separates arguments only if the shallowest open bracket belongs to a function call. */
  1684. int shallowest_open_bracket_index = sr_sp - 2;
  1685. while (shallowest_open_bracket_index > 0 && sr_stack[shallowest_open_bracket_index].type != SUBOPEN_TT)
  1686. --shallowest_open_bracket_index;
  1687. if (shallowest_open_bracket_index > 0 &&
  1688. sr_stack[shallowest_open_bracket_index-1].type == OP_TT &&
  1689. sr_stack[shallowest_open_bracket_index-1].value == FCALL_OP)
  1690. { mark_top_of_emitter_stack(ARGUMENT_VALUE_MARKER, b);
  1691. break;
  1692. }
  1693. /* Non-argument-separating commas get treated like any other operator; we fall through to the default case. */
  1694. }
  1695. default:
  1696. {
  1697. /* Add a marker for the brackets implied by operator precedence */
  1698. int operands_on_left = (operators[b.value].usage == PRE_U) ? 0 : 1;
  1699. add_bracket_layer_to_emitter_stack(operands_on_left);
  1700. }
  1701. }
  1702. }
  1703. get_next_etoken();
  1704. break;
  1705. case GREATER_P:
  1706. do
  1707. { pop = sr_stack[sr_sp - 1];
  1708. emit_token(pop);
  1709. sr_sp--;
  1710. } while (find_prec(sr_stack[sr_sp-1], pop) != LOWER_P);
  1711. break;
  1712. case e1: /* Missing operand error */
  1713. error_named("Missing operand after", a.text);
  1714. put_token_back();
  1715. current_token.type = NUMBER_TT;
  1716. current_token.value = 0;
  1717. current_token.marker = 0;
  1718. current_token.text = "0";
  1719. break;
  1720. case e2: /* Unexpected close bracket */
  1721. error("Found '(' without matching ')'");
  1722. get_next_etoken();
  1723. break;
  1724. case e3: /* Missing operator error */
  1725. error("Missing operator: inserting '+'");
  1726. put_token_back();
  1727. current_token.type = OP_TT;
  1728. current_token.value = PLUS_OP;
  1729. current_token.marker = 0;
  1730. current_token.text = "+";
  1731. break;
  1732. case e4: /* Expression ends with an open bracket */
  1733. error("Found '(' without matching ')'");
  1734. sr_sp--;
  1735. break;
  1736. }
  1737. }
  1738. while (TRUE);
  1739. }
  1740. /* --- Test for simple ++ or -- usage: used to optimise "for" loop code ---- */
  1741. extern int test_for_incdec(assembly_operand AO)
  1742. { int s = 0;
  1743. if (AO.type != EXPRESSION_OT) return 0;
  1744. if (ET[AO.value].down == -1) return 0;
  1745. switch(ET[AO.value].operator_number)
  1746. { case INC_OP: s = 1; break;
  1747. case POST_INC_OP: s = 1; break;
  1748. case DEC_OP: s = -1; break;
  1749. case POST_DEC_OP: s = -1; break;
  1750. }
  1751. if (s==0) return 0;
  1752. if (ET[ET[AO.value].down].down != -1) return 0;
  1753. if (!is_variable_ot(ET[ET[AO.value].down].value.type)) return 0;
  1754. return s*(ET[ET[AO.value].down].value.value);
  1755. }
  1756. /* ========================================================================= */
  1757. /* Data structure management routines */
  1758. /* ------------------------------------------------------------------------- */
  1759. extern void init_expressp_vars(void)
  1760. { int i;
  1761. /* make_operands(); */
  1762. make_lexical_interface_tables();
  1763. for (i=0;i<32;i++) system_function_usage[i] = 0;
  1764. }
  1765. extern void expressp_begin_pass(void)
  1766. {
  1767. }
  1768. extern void expressp_allocate_arrays(void)
  1769. { ET = my_calloc(sizeof(expression_tree_node), MAX_EXPRESSION_NODES,
  1770. "expression parse trees");
  1771. emitter_markers = my_calloc(sizeof(int), MAX_EXPRESSION_NODES,
  1772. "emitter markers");
  1773. emitter_bracket_counts = my_calloc(sizeof(int), MAX_EXPRESSION_NODES,
  1774. "emitter bracket layer counts");
  1775. emitter_stack = my_calloc(sizeof(assembly_operand), MAX_EXPRESSION_NODES,
  1776. "emitter stack");
  1777. sr_stack = my_calloc(sizeof(token_data), MAX_EXPRESSION_NODES,
  1778. "shift-reduce parser stack");
  1779. }
  1780. extern void expressp_free_arrays(void)
  1781. { my_free(&ET, "expression parse trees");
  1782. my_free(&emitter_markers, "emitter markers");
  1783. my_free(&emitter_bracket_counts, "emitter bracket layer counts");
  1784. my_free(&emitter_stack, "emitter stack");
  1785. my_free(&sr_stack, "shift-reduce parser stack");
  1786. }
  1787. /* ========================================================================= */