EberonContextExpression.ob 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. MODULE EberonContextExpression;
  2. IMPORT
  3. Cast,
  4. Context, ContextExpression, ContextHierarchy,
  5. EberonContextDesignator, EberonContextProcedure,
  6. EberonMap, EberonOperator, EberonString, EberonTypePromotion,
  7. Errors, Expression, LanguageContext,
  8. JS,
  9. Object, Record, Types;
  10. TYPE
  11. ExpressionNode* = RECORD(ContextExpression.ExpressionHandler)
  12. condition, first, second: Expression.PType;
  13. END;
  14. RelationExpression* = RECORD(ContextExpression.ExpressionNode)
  15. PROCEDURE RelationExpression(parent: ContextExpression.PExpressionHandler);
  16. PROCEDURE handleTypePromotion(promotion: EberonTypePromotion.PCombined);
  17. currentTypePromotion: EberonTypePromotion.PCombined;
  18. END;
  19. SimpleExpression* = RECORD(ContextExpression.SimpleExpression)
  20. PROCEDURE handleLogicalOr();
  21. typePromotion: EberonTypePromotion.PCombined;
  22. currentPromotion: EberonTypePromotion.PMaybe;
  23. orHandled: BOOLEAN;
  24. END;
  25. Term* = RECORD(ContextExpression.Term)
  26. PROCEDURE handleLogicalAnd();
  27. typePromotion: EberonTypePromotion.PCombined;
  28. currentPromotion: EberonTypePromotion.PMaybe;
  29. andHandled: BOOLEAN;
  30. END;
  31. Factor* = RECORD(ContextExpression.Factor)
  32. END;
  33. AddOperator* = RECORD(ContextExpression.AddOperator)
  34. END;
  35. MulOperator* = RECORD(ContextExpression.MulOperator)
  36. END;
  37. RelationOps = RECORD(ContextExpression.RelationOps)
  38. END;
  39. BeginTypePromotionAndMsg = RECORD(ContextHierarchy.Message)
  40. result: EberonTypePromotion.PCombined;
  41. END;
  42. CurrentTypePromotionMsg = RECORD(ContextHierarchy.Message)
  43. result: EberonTypePromotion.PMaybe;
  44. END;
  45. VAR
  46. relationOps: POINTER TO RelationOps;
  47. PROCEDURE hierarchyDepth(t: Record.Type): INTEGER;
  48. BEGIN
  49. result <- 0;
  50. base <- t.base;
  51. WHILE base # NIL DO
  52. INC(result);
  53. base := base.base;
  54. END;
  55. RETURN result;
  56. END;
  57. PROCEDURE getNthBase(t: Record.PType; n: INTEGER): Record.PType;
  58. BEGIN
  59. result <- t;
  60. i <- n;
  61. WHILE i # 0 DO
  62. result := result.base;
  63. DEC(i);
  64. END;
  65. RETURN result;
  66. END;
  67. PROCEDURE findCommonBase(t1, t2: Record.PType): Record.PType;
  68. VAR
  69. result: Record.PType;
  70. BEGIN
  71. depth1 <- hierarchyDepth(t1^);
  72. depth2 <- hierarchyDepth(t2^);
  73. commonBase1 <- t1;
  74. commonBase2 <- t2;
  75. IF depth1 > depth2 THEN
  76. commonBase1 := getNthBase(commonBase1, depth1 - depth2);
  77. ELSE
  78. commonBase2 := getNthBase(commonBase2, depth2 - depth1);
  79. END;
  80. WHILE commonBase1 # commonBase2 DO
  81. commonBase1 := commonBase1.base;
  82. commonBase2 := commonBase2.base;
  83. END;
  84. RETURN commonBase1;
  85. END;
  86. PROCEDURE ExpressionNode.handleExpression(e: Expression.PType);
  87. BEGIN
  88. IF SELF.condition = NIL THEN
  89. SELF.condition := e;
  90. ELSIF SELF.first = NIL THEN
  91. SELF.first := e;
  92. ELSE
  93. SELF.second := e;
  94. END;
  95. END;
  96. PROCEDURE ExpressionNode.endParse(): BOOLEAN;
  97. VAR
  98. resultType: Types.PType;
  99. op: LanguageContext.PCastOp;
  100. BEGIN
  101. result <- SELF.first;
  102. IF result = NIL THEN
  103. result := SELF.condition;
  104. ELSE
  105. firstType <- SELF.first.type();
  106. secondType <- SELF.second.type();
  107. IF (firstType IS Record.PType) & (secondType IS Record.PType) THEN
  108. resultType := findCommonBase(firstType, secondType);
  109. ELSIF (firstType IS Record.PPointer) & (secondType IS Record.PPointer) THEN
  110. resultType := findCommonBase(Record.pointerBase(firstType^), Record.pointerBase(secondType^));
  111. END;
  112. IF resultType = NIL THEN
  113. IF SELF.root().language().types.implicitCast(firstType, secondType, FALSE, op) # Cast.errNo THEN
  114. Errors.raise("incompatible types in ternary operator: '"
  115. + firstType.description() + "' and '" + secondType.description() + "'");
  116. END;
  117. resultType := firstType;
  118. END;
  119. result := Expression.makeSimple(SELF.condition.code() + " ? "
  120. + SELF.first.code() + " : "
  121. + SELF.second.code(),
  122. resultType);
  123. END;
  124. SELF.parent()(ContextExpression.PExpressionHandler).handleExpression(result);
  125. RETURN TRUE;
  126. END;
  127. PROCEDURE RelationExpression.RelationExpression(parent: ContextExpression.PExpressionHandler)
  128. | SUPER(parent, relationOps);
  129. END;
  130. PROCEDURE RelationExpression.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
  131. VAR
  132. result: Object.PType;
  133. BEGIN
  134. IF msg IS EberonContextDesignator.TransferPromotedTypesMsg THEN
  135. ELSE
  136. result := SUPER(msg);
  137. END;
  138. RETURN result;
  139. END;
  140. PROCEDURE RelationExpression.handleTypePromotion(promotion: EberonTypePromotion.PCombined);
  141. BEGIN
  142. SELF.currentTypePromotion := promotion;
  143. END;
  144. PROCEDURE RelationExpression.handleLiteral(s: STRING);
  145. BEGIN
  146. IF SELF.currentTypePromotion # NIL THEN
  147. SELF.currentTypePromotion.clear();
  148. END;
  149. SUPER(s);
  150. END;
  151. PROCEDURE RelationOps.in(left, right: Types.PType; cx: ContextHierarchy.Node): ContextExpression.BinaryOperatorCx;
  152. VAR
  153. result: ContextExpression.BinaryOperatorCx;
  154. BEGIN
  155. IF right IS EberonMap.PType THEN
  156. EberonContextDesignator.checkMapKeyType(left);
  157. result := EberonOperator.inMap;
  158. ELSE
  159. result := SUPER(left, right, cx);
  160. END;
  161. RETURN result;
  162. END;
  163. PROCEDURE RelationExpression.endParse(): BOOLEAN;
  164. BEGIN
  165. IF SELF.currentTypePromotion # NIL THEN
  166. void <- SELF.parent().handleMessage(
  167. NEW EberonContextDesignator.TransferPromotedTypesMsg(SELF.currentTypePromotion)^);
  168. END;
  169. RETURN SUPER();
  170. END;
  171. PROCEDURE SimpleExpression.handleLogicalOr();
  172. BEGIN
  173. IF SELF.typePromotion # NIL THEN
  174. SELF.currentPromotion := SELF.typePromotion.next();
  175. ELSE
  176. SELF.orHandled := TRUE;
  177. END;
  178. END;
  179. PROCEDURE setSimpleExpressionTypePromotion(VAR e: SimpleExpression): EberonTypePromotion.PMaybe;
  180. BEGIN
  181. IF e.currentPromotion = NIL THEN
  182. msg <- EberonContextProcedure.BeginTypePromotionOrMsg();
  183. void <- e.parent().handleMessage(msg);
  184. e.typePromotion := msg.result;
  185. IF e.typePromotion # NIL THEN
  186. IF e.orHandled THEN
  187. unused <- e.typePromotion.next();
  188. END;
  189. e.currentPromotion := e.typePromotion.next();
  190. END;
  191. END;
  192. RETURN e.currentPromotion;
  193. END;
  194. PROCEDURE SimpleExpression.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
  195. VAR
  196. result: Object.PType;
  197. BEGIN
  198. IF msg IS BeginTypePromotionAndMsg THEN
  199. p <- setSimpleExpressionTypePromotion(SELF);
  200. IF p # NIL THEN
  201. msg.result := p.makeAnd();
  202. END;
  203. ELSE
  204. result := SUPER(msg);
  205. END;
  206. RETURN result;
  207. END;
  208. PROCEDURE SimpleExpression.endParse(): BOOLEAN;
  209. BEGIN
  210. IF SELF.typePromotion # NIL THEN
  211. SELF.parent()^(RelationExpression).handleTypePromotion(SELF.typePromotion);
  212. END;
  213. RETURN SUPER();
  214. END;
  215. PROCEDURE setTermTypePromotion(VAR term: Term): EberonTypePromotion.PMaybe;
  216. BEGIN
  217. IF term.currentPromotion = NIL THEN
  218. msg <- BeginTypePromotionAndMsg();
  219. void <- term.parent().handleMessage(msg);
  220. term.typePromotion := msg.result;
  221. IF term.typePromotion # NIL THEN
  222. IF term.andHandled THEN
  223. unused <- term.typePromotion.next();
  224. END;
  225. term.currentPromotion := term.typePromotion.next();
  226. END;
  227. END;
  228. RETURN term.currentPromotion;
  229. END;
  230. PROCEDURE Term.handleMessage(VAR msg: ContextHierarchy.Message): Object.PType;
  231. VAR
  232. result: Object.PType;
  233. BEGIN
  234. IF msg IS EberonContextDesignator.PromoteTypeMsg THEN
  235. promoted <- msg.info;
  236. p <- setTermTypePromotion(SELF);
  237. IF p # NIL THEN
  238. p.promote(promoted, msg.type);
  239. END;
  240. ELSIF msg IS EberonContextProcedure.BeginTypePromotionOrMsg THEN
  241. p <- setTermTypePromotion(SELF);
  242. IF p # NIL THEN
  243. msg.result := p.makeOr();
  244. END;
  245. ELSIF msg IS CurrentTypePromotionMsg THEN
  246. msg.result := setTermTypePromotion(SELF);
  247. ELSE
  248. result := SUPER(msg);
  249. END;
  250. RETURN result;
  251. END;
  252. PROCEDURE Term.handleLogicalAnd();
  253. BEGIN
  254. IF SELF.typePromotion # NIL THEN
  255. SELF.currentPromotion := SELF.typePromotion.next();
  256. ELSE
  257. SELF.andHandled := TRUE;
  258. END;
  259. END;
  260. PROCEDURE Factor.handleLogicalNot();
  261. BEGIN
  262. SUPER();
  263. msg <- CurrentTypePromotionMsg();
  264. void <- SELF.handleMessage(msg);
  265. p <- msg.result;
  266. IF p # NIL THEN
  267. p.invert();
  268. END;
  269. END;
  270. PROCEDURE AddOperator.doMatchPlusOperator(type: Types.PType): ContextExpression.BinaryOperator;
  271. VAR
  272. result: ContextExpression.BinaryOperator;
  273. BEGIN
  274. IF (type = EberonString.string) OR (type IS Types.PString) THEN
  275. result := EberonOperator.addStr;
  276. ELSE
  277. result := SUPER(type);
  278. END;
  279. RETURN result;
  280. END;
  281. PROCEDURE AddOperator.doExpectPlusOperator(): STRING;
  282. RETURN "numeric type or SET or STRING";
  283. END;
  284. PROCEDURE AddOperator.endParse(): BOOLEAN;
  285. BEGIN
  286. SELF.parent()^(SimpleExpression).handleLogicalOr();
  287. RETURN TRUE;
  288. END;
  289. PROCEDURE MulOperator.endParse(): BOOLEAN;
  290. BEGIN
  291. SELF.parent()^(Term).handleLogicalAnd();
  292. RETURN TRUE;
  293. END;
  294. PROCEDURE RelationOps.eq(type: Types.PType): ContextExpression.BinaryOperatorCx;
  295. VAR
  296. result: ContextExpression.BinaryOperatorCx;
  297. BEGIN
  298. IF type = EberonString.string THEN
  299. result := EberonOperator.equalStr;
  300. ELSE
  301. result := SUPER(type);
  302. END;
  303. RETURN result;
  304. END;
  305. PROCEDURE RelationOps.notEq(type: Types.PType): ContextExpression.BinaryOperatorCx;
  306. VAR
  307. result: ContextExpression.BinaryOperatorCx;
  308. BEGIN
  309. IF type = EberonString.string THEN
  310. result := EberonOperator.notEqualStr;
  311. ELSE
  312. result := SUPER(type);
  313. END;
  314. RETURN result;
  315. END;
  316. PROCEDURE RelationOps.less(type: Types.PType): ContextExpression.BinaryOperatorCx;
  317. VAR
  318. result: ContextExpression.BinaryOperatorCx;
  319. BEGIN
  320. IF type = EberonString.string THEN
  321. result := EberonOperator.lessStr;
  322. ELSE
  323. result := SUPER(type);
  324. END;
  325. RETURN result;
  326. END;
  327. PROCEDURE RelationOps.greater(type: Types.PType): ContextExpression.BinaryOperatorCx;
  328. VAR
  329. result: ContextExpression.BinaryOperatorCx;
  330. BEGIN
  331. IF type = EberonString.string THEN
  332. result := EberonOperator.greaterStr;
  333. ELSE
  334. result := SUPER(type);
  335. END;
  336. RETURN result;
  337. END;
  338. PROCEDURE RelationOps.lessEq(type: Types.PType): ContextExpression.BinaryOperatorCx;
  339. VAR
  340. result: ContextExpression.BinaryOperatorCx;
  341. BEGIN
  342. IF type = EberonString.string THEN
  343. result := EberonOperator.lessEqualStr;
  344. ELSE
  345. result := SUPER(type);
  346. END;
  347. RETURN result;
  348. END;
  349. PROCEDURE RelationOps.greaterEq(type: Types.PType): ContextExpression.BinaryOperatorCx;
  350. VAR
  351. result: ContextExpression.BinaryOperatorCx;
  352. BEGIN
  353. IF type = EberonString.string THEN
  354. result := EberonOperator.greaterEqualStr;
  355. ELSE
  356. result := SUPER(type);
  357. END;
  358. RETURN result;
  359. END;
  360. PROCEDURE RelationOps.is(cx: ContextHierarchy.Node): ContextExpression.BinaryOperatorCx;
  361. VAR
  362. impl: ContextExpression.BinaryOperatorCx;
  363. r: ContextExpression.BinaryOperatorCx;
  364. PROCEDURE is(left, right: Expression.PType; lcx: LanguageContext.PType): Expression.PType;
  365. BEGIN
  366. d <- left.designator();
  367. IF d # NIL THEN
  368. v <- d.info();
  369. IF v IS EberonTypePromotion.PVariable THEN
  370. msg <- EberonContextDesignator.PromoteTypeMsg(
  371. v,
  372. ContextExpression.unwrapType(right.designator().info()));
  373. void <- cx.handleMessage(msg);
  374. END;
  375. END;
  376. RETURN impl(left, right, lcx);
  377. END;
  378. BEGIN
  379. impl := SUPER(cx);
  380. JS.do("r = is"); (*allow closure*)
  381. RETURN r;
  382. END;
  383. PROCEDURE RelationOps.coalesceType(leftType, rightType: Types.PType): Types.PType;
  384. VAR
  385. result: Types.PType;
  386. BEGIN
  387. IF (((leftType = EberonString.string) & (rightType IS Types.PString))
  388. OR ((rightType = EberonString.string) & (leftType IS Types.PString))) THEN
  389. result := EberonString.string;
  390. ELSE
  391. result := SUPER(leftType, rightType);
  392. END;
  393. RETURN result;
  394. END;
  395. BEGIN
  396. NEW(relationOps);
  397. END EberonContextExpression.