parse.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. #include "pch.h"
  2. #include "ast.h"
  3. #include "main.h"
  4. //////////////////////////////////////////////////////////////////////////////
  5. //
  6. // UnitValue
  7. //
  8. //////////////////////////////////////////////////////////////////////////////
  9. class UnitValue : public Value {
  10. public:
  11. UnitValue()
  12. {
  13. }
  14. ZString GetString()
  15. {
  16. return "value";
  17. }
  18. };
  19. //////////////////////////////////////////////////////////////////////////////
  20. //
  21. // NumberValue
  22. //
  23. //////////////////////////////////////////////////////////////////////////////
  24. class NumberValue : public Value {
  25. private:
  26. float m_value;
  27. public:
  28. NumberValue(float value) :
  29. m_value(value)
  30. {
  31. }
  32. float GetValue()
  33. {
  34. return m_value;
  35. }
  36. ZString GetString()
  37. {
  38. return m_value;
  39. }
  40. TRef<Type> GetType()
  41. {
  42. return new BaseType("number");
  43. }
  44. static TRef<Type> StaticGetType()
  45. {
  46. return new BaseType("number");
  47. }
  48. };
  49. //////////////////////////////////////////////////////////////////////////////
  50. //
  51. // BooleanValue
  52. //
  53. //////////////////////////////////////////////////////////////////////////////
  54. class BooleanValue : public Value {
  55. private:
  56. bool m_value;
  57. public:
  58. BooleanValue(bool value) :
  59. m_value(value)
  60. {
  61. }
  62. bool GetValue()
  63. {
  64. return m_value;
  65. }
  66. ZString GetString()
  67. {
  68. return m_value;
  69. }
  70. TRef<Type> GetType()
  71. {
  72. return new BaseType("boolean");
  73. }
  74. static TRef<Type> StaticGetType()
  75. {
  76. return new BaseType("boolean");
  77. }
  78. };
  79. //////////////////////////////////////////////////////////////////////////////
  80. //
  81. // StringValue
  82. //
  83. //////////////////////////////////////////////////////////////////////////////
  84. class StringValue : public Value {
  85. private:
  86. ZString m_value;
  87. public:
  88. StringValue(const ZString& value) :
  89. m_value(value)
  90. {
  91. }
  92. const ZString& GetValue()
  93. {
  94. return m_value;
  95. }
  96. ZString GetString()
  97. {
  98. return "\"" + m_value + "\"";
  99. }
  100. TRef<Type> GetType()
  101. {
  102. return new BaseType("string");
  103. }
  104. static TRef<Type> StaticGetType()
  105. {
  106. return new BaseType("string");
  107. }
  108. };
  109. //////////////////////////////////////////////////////////////////////////////
  110. //
  111. // PairValue
  112. //
  113. //////////////////////////////////////////////////////////////////////////////
  114. class PairValue : public Value {
  115. private:
  116. TRef<Delay> m_pdelayFirst;
  117. TRef<Delay> m_pdelaySecond;
  118. public:
  119. PairValue(Delay* pdelayFirst, Delay* pdelaySecond) :
  120. m_pdelayFirst(pdelayFirst),
  121. m_pdelaySecond(pdelaySecond)
  122. {
  123. }
  124. Delay* GetFirst()
  125. {
  126. return m_pdelayFirst;
  127. }
  128. Delay* GetSecond()
  129. {
  130. return m_pdelaySecond;
  131. }
  132. ZString GetString()
  133. {
  134. return
  135. m_pdelayFirst->Evaluate()->GetString()
  136. + ", "
  137. + m_pdelaySecond->Evaluate()->GetString();
  138. }
  139. TRef<Type> GetType()
  140. {
  141. return
  142. new PairType(
  143. m_pdelayFirst->GetType(),
  144. m_pdelaySecond->GetType()
  145. );
  146. }
  147. static TRef<Type> StaticGetType()
  148. {
  149. return
  150. new PairType(
  151. new PolymorphicType(),
  152. new PolymorphicType()
  153. );
  154. }
  155. };
  156. //////////////////////////////////////////////////////////////////////////////
  157. //
  158. // ConsValue
  159. //
  160. //////////////////////////////////////////////////////////////////////////////
  161. class ConsValue : public Value {
  162. private:
  163. TRef<Delay> m_pdelayHead;
  164. TRef<Delay> m_pdelayTail;
  165. public:
  166. ConsValue(Delay* pdelayHead, Delay* pdelayTail) :
  167. m_pdelayHead(pdelayHead),
  168. m_pdelayTail(pdelayTail)
  169. {
  170. }
  171. Delay* GetHead()
  172. {
  173. return m_pdelayHead;
  174. }
  175. Delay* GetTail()
  176. {
  177. return m_pdelayTail;
  178. }
  179. ZString GetString()
  180. {
  181. if (m_pdelayHead == NULL) {
  182. return "[]";
  183. } else {
  184. ZString str = "[";
  185. TRef<ConsValue> pcons = this;
  186. while (true) {
  187. str += pcons->m_pdelayHead->Evaluate()->GetString();
  188. CastTo(pcons, pcons->m_pdelayTail->Evaluate());
  189. if (pcons->m_pdelayHead == NULL) {
  190. return str + "]";
  191. } else {
  192. str += ", ";
  193. }
  194. }
  195. }
  196. }
  197. TRef<Type> GetType()
  198. {
  199. if (m_pdelayHead == NULL) {
  200. return ConsValue::StaticGetType();
  201. } else {
  202. return new ListType(m_pdelayHead->GetType());
  203. }
  204. }
  205. static TRef<Type> StaticGetType()
  206. {
  207. return new ListType(new PolymorphicType());
  208. }
  209. };
  210. //////////////////////////////////////////////////////////////////////////////
  211. //
  212. // BooleanExpression
  213. //
  214. //////////////////////////////////////////////////////////////////////////////
  215. class BooleanExpression : public Expression {
  216. public:
  217. TRef<Delay> Evaluate(Environment* penv, bool b)
  218. {
  219. return CreateDelay(penv, CreateValueExpression(new BooleanValue(b)));
  220. }
  221. };
  222. //////////////////////////////////////////////////////////////////////////////
  223. //
  224. // function x -> NotExpression;
  225. //
  226. //////////////////////////////////////////////////////////////////////////////
  227. class NotExpression : public BooleanExpression {
  228. public:
  229. TRef<Type> GetType(Environment* penv)
  230. {
  231. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(BooleanValue::StaticGetType()));
  232. return BooleanValue::StaticGetType();
  233. }
  234. TRef<Delay> Evaluate(Environment* penv)
  235. {
  236. TRef<BooleanValue> pvalue1; CastTo(pvalue1, penv->GetArgument(1)->Evaluate());
  237. return BooleanExpression::Evaluate(penv, !pvalue1->GetValue());
  238. }
  239. };
  240. //////////////////////////////////////////////////////////////////////////////
  241. //
  242. // function x y -> EqualExpression;
  243. //
  244. //////////////////////////////////////////////////////////////////////////////
  245. class EqualNumberExpression : public BooleanExpression {
  246. public:
  247. TRef<Type> GetType(Environment* penv)
  248. {
  249. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  250. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  251. return BooleanValue::StaticGetType();
  252. }
  253. TRef<Delay> Evaluate(Environment* penv)
  254. {
  255. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  256. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  257. return BooleanExpression::Evaluate(penv, pvalue1->GetValue() == pvalue2->GetValue());
  258. }
  259. };
  260. //////////////////////////////////////////////////////////////////////////////
  261. //
  262. // function x y -> MultiplyExpression;
  263. //
  264. //////////////////////////////////////////////////////////////////////////////
  265. class MultiplyExpression : public Expression {
  266. public:
  267. TRef<Type> GetType(Environment* penv)
  268. {
  269. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  270. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  271. return NumberValue::StaticGetType();
  272. }
  273. TRef<Delay> Evaluate(Environment* penv)
  274. {
  275. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  276. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  277. return
  278. CreateDelay(
  279. NULL,
  280. CreateValueExpression(
  281. new NumberValue(pvalue1->GetValue() * pvalue2->GetValue())
  282. )
  283. );
  284. }
  285. };
  286. //////////////////////////////////////////////////////////////////////////////
  287. //
  288. // function x y -> DivideExpression;
  289. //
  290. //////////////////////////////////////////////////////////////////////////////
  291. class DivideExpression : public Expression {
  292. public:
  293. TRef<Type> GetType(Environment* penv)
  294. {
  295. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  296. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  297. return NumberValue::StaticGetType();
  298. }
  299. TRef<Delay> Evaluate(Environment* penv)
  300. {
  301. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  302. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  303. return
  304. CreateDelay(
  305. NULL,
  306. CreateValueExpression(
  307. new NumberValue(pvalue1->GetValue() / pvalue2->GetValue())
  308. )
  309. );
  310. }
  311. };
  312. //////////////////////////////////////////////////////////////////////////////
  313. //
  314. // function x y -> ModExpression;
  315. //
  316. //////////////////////////////////////////////////////////////////////////////
  317. class ModExpression : public Expression {
  318. public:
  319. TRef<Type> GetType(Environment* penv)
  320. {
  321. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  322. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  323. return NumberValue::StaticGetType();
  324. }
  325. TRef<Delay> Evaluate(Environment* penv)
  326. {
  327. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  328. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  329. return
  330. CreateDelay(
  331. NULL,
  332. CreateValueExpression(
  333. new NumberValue(mod(pvalue1->GetValue(), pvalue2->GetValue()))
  334. )
  335. );
  336. }
  337. };
  338. //////////////////////////////////////////////////////////////////////////////
  339. //
  340. // function x y -> AddExpression;
  341. //
  342. //////////////////////////////////////////////////////////////////////////////
  343. class AddExpression : public Expression {
  344. public:
  345. TRef<Type> GetType(Environment* penv)
  346. {
  347. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  348. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  349. return NumberValue::StaticGetType();
  350. }
  351. TRef<Delay> Evaluate(Environment* penv)
  352. {
  353. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  354. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  355. return
  356. CreateDelay(
  357. NULL,
  358. CreateValueExpression(
  359. new NumberValue(pvalue1->GetValue() + pvalue2->GetValue())
  360. )
  361. );
  362. }
  363. };
  364. //////////////////////////////////////////////////////////////////////////////
  365. //
  366. // function x y -> SubtractExpression;
  367. //
  368. //////////////////////////////////////////////////////////////////////////////
  369. class SubtractExpression : public Expression {
  370. public:
  371. TRef<Type> GetType(Environment* penv)
  372. {
  373. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(NumberValue::StaticGetType()));
  374. ZVerify(penv->GetArgument(2)->GetType()->IsMatch(NumberValue::StaticGetType()));
  375. return NumberValue::StaticGetType();
  376. }
  377. TRef<Delay> Evaluate(Environment* penv)
  378. {
  379. TRef<NumberValue> pvalue1; CastTo(pvalue1, penv->GetArgument(2)->Evaluate());
  380. TRef<NumberValue> pvalue2; CastTo(pvalue2, penv->GetArgument(1)->Evaluate());
  381. return
  382. CreateDelay(
  383. NULL,
  384. CreateValueExpression(
  385. new NumberValue(pvalue1->GetValue() - pvalue2->GetValue())
  386. )
  387. );
  388. }
  389. };
  390. //////////////////////////////////////////////////////////////////////////////
  391. //
  392. // function condition then else -> IfExpression;
  393. //
  394. //////////////////////////////////////////////////////////////////////////////
  395. class IfExpression : public Expression {
  396. public:
  397. TRef<Type> GetType(Environment* penv)
  398. {
  399. ZVerify(penv->GetArgument(1)->GetType()->IsMatch(penv->GetArgument(2)->GetType()));
  400. ZVerify(penv->GetArgument(3)->GetType()->IsMatch(BooleanValue::StaticGetType()));
  401. return penv->GetArgument(1)->GetType();
  402. }
  403. TRef<Delay> Evaluate(Environment* penv)
  404. {
  405. TRef<BooleanValue> pbool;
  406. CastTo(pbool, penv->GetArgument(3)->Evaluate());
  407. if (pbool->GetValue()) {
  408. return penv->GetArgument(2);
  409. } else {
  410. return penv->GetArgument(1);
  411. }
  412. }
  413. };
  414. //////////////////////////////////////////////////////////////////////////////
  415. //
  416. // function x -> FirstExpression;
  417. //
  418. //////////////////////////////////////////////////////////////////////////////
  419. class FirstExpression : public Expression {
  420. public:
  421. TRef<Type> GetType(Environment* penv)
  422. {
  423. TRef<Type> ptype = penv->GetArgument(1)->GetType();
  424. ZVerify(ptype->IsMatch(PairValue::StaticGetType()));
  425. return ptype->GetPairType()->GetFirstType();
  426. }
  427. TRef<Delay> Evaluate(Environment* penv)
  428. {
  429. TRef<PairValue> ppair; CastTo(ppair, penv->GetArgument(1)->Evaluate());
  430. return ppair->GetFirst();
  431. }
  432. };
  433. //////////////////////////////////////////////////////////////////////////////
  434. //
  435. // function x -> SecondExpression;
  436. //
  437. //////////////////////////////////////////////////////////////////////////////
  438. class SecondExpression : public Expression {
  439. public:
  440. TRef<Type> GetType(Environment* penv)
  441. {
  442. TRef<Type> ptype = penv->GetArgument(1)->GetType();
  443. ZVerify(ptype->IsMatch(PairValue::StaticGetType()));
  444. return ptype->GetPairType()->GetSecondType();
  445. }
  446. TRef<Delay> Evaluate(Environment* penv)
  447. {
  448. TRef<PairValue> ppair; CastTo(ppair, penv->GetArgument(1)->Evaluate());
  449. return ppair->GetSecond();
  450. }
  451. };
  452. //////////////////////////////////////////////////////////////////////////////
  453. //
  454. // function x y -> PairExpression;
  455. //
  456. //////////////////////////////////////////////////////////////////////////////
  457. class PairExpression : public Expression {
  458. public:
  459. TRef<Type> GetType(Environment* penv)
  460. {
  461. return
  462. new PairType(
  463. penv->GetArgument(2)->GetType(),
  464. penv->GetArgument(1)->GetType()
  465. );
  466. }
  467. TRef<Delay> Evaluate(Environment* penv)
  468. {
  469. return
  470. CreateDelay(
  471. NULL,
  472. CreateValueExpression(
  473. new PairValue(
  474. penv->GetArgument(2),
  475. penv->GetArgument(1)
  476. )
  477. )
  478. );
  479. }
  480. };
  481. //////////////////////////////////////////////////////////////////////////////
  482. //
  483. // function cons -> EmptyExpression;
  484. //
  485. //////////////////////////////////////////////////////////////////////////////
  486. class EmptyExpression : public BooleanExpression {
  487. public:
  488. TRef<Type> GetType(Environment* penv)
  489. {
  490. TRef<Type> ptype = penv->GetArgument(1)->GetType();
  491. ZVerify(ptype->IsMatch(ConsValue::StaticGetType()));
  492. return BooleanValue::StaticGetType();
  493. }
  494. TRef<Delay> Evaluate(Environment* penv)
  495. {
  496. TRef<ConsValue> pcons; CastTo(pcons, penv->GetArgument(1)->Evaluate());
  497. return BooleanExpression::Evaluate(penv, pcons->GetHead() == NULL);
  498. }
  499. };
  500. //////////////////////////////////////////////////////////////////////////////
  501. //
  502. // function x -> HeadExpression;
  503. //
  504. //////////////////////////////////////////////////////////////////////////////
  505. class HeadExpression : public Expression {
  506. public:
  507. TRef<Type> GetType(Environment* penv)
  508. {
  509. TRef<Type> ptype = penv->GetArgument(1)->GetType();
  510. ZVerify(ptype->IsMatch(ConsValue::StaticGetType()));
  511. return ptype->GetListType()->GetType();
  512. }
  513. TRef<Delay> Evaluate(Environment* penv)
  514. {
  515. TRef<ConsValue> pcons; CastTo(pcons, penv->GetArgument(1)->Evaluate());
  516. return pcons->GetHead();
  517. }
  518. };
  519. //////////////////////////////////////////////////////////////////////////////
  520. //
  521. // function x -> TailExpression;
  522. //
  523. //////////////////////////////////////////////////////////////////////////////
  524. class TailExpression : public Expression {
  525. public:
  526. TRef<Type> GetType(Environment* penv)
  527. {
  528. TRef<Type> ptype = penv->GetArgument(1)->GetType();
  529. ZVerify(ptype->IsMatch(ConsValue::StaticGetType()));
  530. return ptype;
  531. }
  532. TRef<Delay> Evaluate(Environment* penv)
  533. {
  534. TRef<ConsValue> pcons; CastTo(pcons, penv->GetArgument(1)->Evaluate());
  535. return pcons->GetTail();
  536. }
  537. };
  538. //////////////////////////////////////////////////////////////////////////////
  539. //
  540. // function x y -> ConsExpression;
  541. //
  542. //////////////////////////////////////////////////////////////////////////////
  543. class ConsExpression : public Expression {
  544. public:
  545. TRef<Type> GetType(Environment* penv)
  546. {
  547. TRef<Type> ptypeHead = penv->GetArgument(2)->GetType();
  548. TRef<Type> ptypeTail = penv->GetArgument(1)->GetType();
  549. ZVerify(ptypeTail->IsMatch(ConsValue::StaticGetType()));
  550. ZVerify(ptypeHead->IsMatch(ptypeTail->GetListType()->GetType()));
  551. return ptypeTail;
  552. }
  553. TRef<Delay> Evaluate(Environment* penv)
  554. {
  555. return
  556. CreateDelay(
  557. NULL,
  558. CreateValueExpression(
  559. new ConsValue(
  560. penv->GetArgument(2),
  561. penv->GetArgument(1)
  562. )
  563. )
  564. );
  565. }
  566. };
  567. //////////////////////////////////////////////////////////////////////////////
  568. //
  569. // MDLParser
  570. //
  571. //////////////////////////////////////////////////////////////////////////////
  572. class MDLParser : public TextTokenImpl {
  573. public:
  574. int Comma ;
  575. int SemiColon ;
  576. int LeftParen ;
  577. int RightParen ;
  578. int LeftBracket ;
  579. int RightBracket ;
  580. int Arrow ;
  581. int Equals ;
  582. int NotEquals ;
  583. int Less ;
  584. int LessEquals ;
  585. int Greater ;
  586. int GreaterEquals;
  587. int Plus ;
  588. int Minus ;
  589. int Multiply ;
  590. int Divide ;
  591. int Cons ;
  592. int Period ;
  593. int Let ;
  594. int In ;
  595. int Function ;
  596. int If ;
  597. int Then ;
  598. int Else ;
  599. TRef<Expression> m_pexprEqual;
  600. TRef<Expression> m_pexprMultiply;
  601. TRef<Expression> m_pexprDivide;
  602. TRef<Expression> m_pexprAdd;
  603. TRef<Expression> m_pexprSubtract;
  604. TRef<Expression> m_pexprCons;
  605. TRef<Expression> m_pexprPair;
  606. TRef<Expression> m_pexprEmptyList;
  607. TRef<Expression> m_pexprIf;
  608. MDLParser(PCC pcc, int length) :
  609. TextTokenImpl(pcc, length)
  610. {
  611. Comma = AddToken("','");
  612. SemiColon = AddToken("';'");
  613. LeftParen = AddToken("'('");
  614. RightParen = AddToken("')'");
  615. LeftBracket = AddToken("'['");
  616. RightBracket = AddToken("']'");
  617. Arrow = AddToken("'->'");
  618. Equals = AddToken("'='");
  619. NotEquals = AddToken("'!='");
  620. Less = AddToken("'<'");
  621. LessEquals = AddToken("'<='");
  622. Greater = AddToken("'>'");
  623. GreaterEquals= AddToken("'>='");
  624. Plus = AddToken("'+'");
  625. Minus = AddToken("'-'");
  626. Multiply = AddToken("'*'");
  627. Divide = AddToken("'/'");
  628. Cons = AddToken("'::'");
  629. Period = AddToken("'.'");
  630. Let = AddSymbol("let");
  631. In = AddSymbol("in");
  632. Function = AddSymbol("function");
  633. If = AddSymbol("if");
  634. Then = AddSymbol("then");
  635. Else = AddSymbol("else");
  636. Next();
  637. m_pexprEqual = CreateSymbolExpression("equal");
  638. m_pexprMultiply = CreateSymbolExpression("mul");
  639. m_pexprDivide = CreateSymbolExpression("div");
  640. m_pexprAdd = CreateSymbolExpression("add");
  641. m_pexprSubtract = CreateSymbolExpression("sub");
  642. m_pexprCons = CreateSymbolExpression("cons");
  643. m_pexprPair = CreateSymbolExpression("pair");
  644. m_pexprEmptyList = CreateSymbolExpression("emptyList");
  645. m_pexprIf = CreateSymbolExpression("if");
  646. }
  647. int ParseToken(PCC& pcc)
  648. {
  649. switch (pcc[0]) {
  650. case '[': pcc++; return LeftBracket;
  651. case ']': pcc++; return RightBracket;
  652. case '(': pcc++; return LeftParen;
  653. case ')': pcc++; return RightParen;
  654. case ',': pcc++; return Comma;
  655. case ';': pcc++; return SemiColon;
  656. case '=': pcc++; return Equals;
  657. case '+': pcc++; return Plus;
  658. case '*': pcc++; return Multiply;
  659. case '/': pcc++; return Divide;
  660. case '^': pcc++; return Period;
  661. case ':':
  662. if (pcc[1] == ':') {
  663. pcc += 2;
  664. return Cons;
  665. }
  666. case '!':
  667. if (pcc[1] == '=') {
  668. pcc += 2;
  669. return NotEquals;
  670. }
  671. return Null;
  672. case '-':
  673. if (pcc[1] == '>') {
  674. pcc += 2;
  675. return Arrow;
  676. } else {
  677. pcc++;
  678. return Minus;
  679. }
  680. case '<':
  681. if (pcc[0] == '=') {
  682. pcc += 2;
  683. return LessEquals;
  684. } else {
  685. pcc++;
  686. return Less;
  687. }
  688. case '>':
  689. if (pcc[0] == '=') {
  690. pcc += 2;
  691. return GreaterEquals;
  692. } else {
  693. pcc++;
  694. return Greater;
  695. }
  696. }
  697. return 0;
  698. }
  699. //////////////////////////////////////////////////////////////////////////////
  700. //
  701. // Parser
  702. //
  703. //////////////////////////////////////////////////////////////////////////////
  704. bool ReadFunction(TRef<Expression>& pexprOut, int tokenID)
  705. {
  706. ZString str;
  707. TRef<Expression> pexpr;
  708. if (IsSymbol(str, true)) {
  709. if (Is(tokenID, false)) {
  710. if (!ReadExpression(pexpr, true, true)) {
  711. return false;
  712. }
  713. } else if (!ReadFunction(pexpr, tokenID)) {
  714. return false;
  715. }
  716. pexprOut = CreateFunctionExpression(str, pexpr);
  717. return true;
  718. }
  719. return false;
  720. }
  721. bool ReadLet(TRef<Expression>& pexpr)
  722. {
  723. TRef<Environment> penv;
  724. if (ReadDefinitions(penv, NULL, true)) {
  725. if (Is(In, true)) {
  726. TRef<Expression> pexprIn;
  727. if (ReadExpression(pexprIn, true, true)) {
  728. pexpr = CreateLetExpression(penv, pexprIn);
  729. return true;
  730. }
  731. }
  732. }
  733. return false;
  734. }
  735. bool ReadIf(TRef<Expression>& pexpr)
  736. {
  737. TRef<Expression> pexprCondition;
  738. if (ReadExpression(pexprCondition, true, true)) {
  739. if (Is(Then, true)) {
  740. TRef<Expression> pexprThen;
  741. if (ReadExpression(pexprThen, true, true)) {
  742. if (Is(Else, true)) {
  743. TRef<Expression> pexprElse;
  744. if (ReadExpression(pexprElse, true, true)) {
  745. pexpr =
  746. CreateApplyExpression(
  747. CreateApplyExpression(
  748. CreateApplyExpression(
  749. m_pexprIf,
  750. pexprCondition
  751. ),
  752. pexprThen
  753. ),
  754. pexprElse
  755. );
  756. return true;
  757. }
  758. }
  759. }
  760. }
  761. }
  762. return false;
  763. }
  764. bool ReadList(TRef<Expression>& pexpr)
  765. {
  766. if (Is(RightBracket, false)) {
  767. pexpr = m_pexprEmptyList;
  768. return true;
  769. } else if (ReadExpression(pexpr, false, true)) {
  770. if (Is(Comma, false)) {
  771. TRef<Expression> pexpr2;
  772. if (ReadList(pexpr2)) {
  773. pexpr =
  774. CreateApplyExpression(
  775. CreateApplyExpression(
  776. m_pexprCons,
  777. pexpr
  778. ),
  779. pexpr2
  780. );
  781. return true;
  782. }
  783. } else if (Is(RightBracket, true)) {
  784. pexpr =
  785. CreateApplyExpression(
  786. CreateApplyExpression(
  787. m_pexprCons,
  788. pexpr
  789. ),
  790. m_pexprEmptyList
  791. );
  792. return true;
  793. }
  794. }
  795. return false;
  796. }
  797. bool ReadTerminal(TRef<Expression>& pexpr, bool bError)
  798. {
  799. ZString str;
  800. float value;
  801. if (IsSymbol(str, false)) {
  802. pexpr = CreateSymbolExpression(str);
  803. return true;
  804. } else if (IsNumber(value, false)) {
  805. pexpr = CreateValueExpression(new NumberValue(value));
  806. return true;
  807. } else if (IsString(str, false)) {
  808. pexpr = CreateValueExpression(new StringValue(str));
  809. return true;
  810. } else if (Is(LeftParen, false)) {
  811. if (ReadExpression(pexpr, true, false)) {
  812. if (Is(RightParen, true)) {
  813. return true;
  814. }
  815. } else if (Is(RightParen, true)) {
  816. pexpr = CreateValueExpression(new UnitValue());
  817. return true;
  818. }
  819. } else if (Is(Let, false)) {
  820. return ReadLet(pexpr);
  821. } else if (Is(If, false)) {
  822. return ReadIf(pexpr);
  823. } else if (Is(Function, false)) {
  824. return ReadFunction(pexpr, Arrow);
  825. } else if (Is(LeftBracket, false)) {
  826. return ReadList(pexpr);
  827. }
  828. if (bError) {
  829. SetError("Expected Terminal");
  830. }
  831. return bError;
  832. }
  833. // a.b.c ==> c (b a)
  834. bool ReadMember(TRef<Expression>& pexpr, bool bError)
  835. {
  836. if (ReadTerminal(pexpr, bError)) {
  837. while (true) {
  838. if (Is(Period, false)) {
  839. TRef<Expression> pexpr2;
  840. if (ReadTerminal(pexpr2, true)) {
  841. pexpr = CreateApplyExpression(pexpr2, pexpr);
  842. continue;
  843. }
  844. }
  845. return true;
  846. }
  847. }
  848. return false;
  849. }
  850. bool ReadMultiply(TRef<Expression>& pexpr, bool bError)
  851. {
  852. if (ReadMember(pexpr, bError)) {
  853. while (true) {
  854. if (Is(Multiply, false)) {
  855. pexpr =
  856. CreateApplyExpression(
  857. m_pexprMultiply,
  858. pexpr
  859. );
  860. TRef<Expression> pexpr2;
  861. if (ReadMember(pexpr2, false)) {
  862. pexpr = CreateApplyExpression(pexpr, pexpr2);
  863. continue;
  864. }
  865. } else if (Is(Divide, false)) {
  866. pexpr =
  867. CreateApplyExpression(
  868. m_pexprDivide,
  869. pexpr
  870. );
  871. TRef<Expression> pexpr2;
  872. if (ReadMember(pexpr2, false)) {
  873. pexpr = CreateApplyExpression(pexpr, pexpr2);
  874. continue;
  875. }
  876. }
  877. return true;
  878. }
  879. }
  880. return false;
  881. }
  882. bool ReadAddition(TRef<Expression>& pexpr, bool bError)
  883. {
  884. if (ReadMultiply(pexpr, bError)) {
  885. while (true) {
  886. if (Is(Plus, false)) {
  887. pexpr =
  888. CreateApplyExpression(
  889. m_pexprAdd,
  890. pexpr
  891. );
  892. TRef<Expression> pexpr2;
  893. if (ReadMultiply(pexpr2, false)) {
  894. pexpr = CreateApplyExpression(pexpr, pexpr2);
  895. continue;
  896. }
  897. } else if (Is(Minus, false)) {
  898. pexpr =
  899. CreateApplyExpression(
  900. m_pexprSubtract,
  901. pexpr
  902. );
  903. TRef<Expression> pexpr2;
  904. if (ReadMultiply(pexpr2, false)) {
  905. pexpr = CreateApplyExpression(pexpr, pexpr2);
  906. continue;
  907. }
  908. }
  909. return true;
  910. }
  911. }
  912. return false;
  913. }
  914. bool ReadApply(TRef<Expression>& pexpr, bool bError)
  915. {
  916. if (ReadAddition(pexpr, bError)) {
  917. while (true) {
  918. TRef<Expression> pexpr2;
  919. if (ReadAddition(pexpr2, false)) {
  920. pexpr = CreateApplyExpression(pexpr, pexpr2);
  921. continue;
  922. }
  923. return true;
  924. }
  925. }
  926. return false;
  927. }
  928. bool ReadCons(TRef<Expression>& pexpr, bool bError)
  929. {
  930. if (ReadApply(pexpr, bError)) {
  931. if (Is(Cons, false)) {
  932. pexpr =
  933. CreateApplyExpression(
  934. m_pexprCons,
  935. pexpr
  936. );
  937. TRef<Expression> pexpr2;
  938. if (ReadCons(pexpr2, true)) {
  939. pexpr = CreateApplyExpression(pexpr, pexpr2);
  940. return true;
  941. }
  942. return false;
  943. }
  944. return true;
  945. }
  946. return false;
  947. }
  948. bool ReadPair(TRef<Expression>& pexpr, bool bAllowPairs, bool bError)
  949. {
  950. if (bAllowPairs) {
  951. if (ReadCons(pexpr, bError)) {
  952. if (Is(Comma, false)) {
  953. pexpr =
  954. CreateApplyExpression(
  955. m_pexprPair,
  956. pexpr
  957. );
  958. TRef<Expression> pexpr2;
  959. if (ReadPair(pexpr2, true, true)) {
  960. pexpr = CreateApplyExpression(pexpr, pexpr2);
  961. return true;
  962. }
  963. return false;
  964. }
  965. return true;
  966. }
  967. return false;
  968. } else {
  969. return ReadCons(pexpr, bError);
  970. }
  971. }
  972. bool ReadExpression(TRef<Expression>& pexpr, bool bAllowPairs, bool bError)
  973. {
  974. return ReadPair(pexpr, bAllowPairs, bError);
  975. }
  976. bool ReadDefinitions(TRef<Environment>& penvOut, Environment* penvIn, bool bError)
  977. {
  978. TRef<Environment> penvTop = new Environment(NULL, "file", NULL);
  979. penvOut = penvIn;
  980. while (true) {
  981. ZString str;
  982. if (IsSymbol(str, false)) {
  983. TRef<Expression> pexpr;
  984. if (Is(Equals, false)) {
  985. if (ReadExpression(pexpr, true, true)) {
  986. if (Is(SemiColon, true)) {
  987. penvOut =
  988. new Environment(
  989. penvOut,
  990. str,
  991. CreateDelay(
  992. penvTop,
  993. pexpr
  994. )
  995. );
  996. continue;
  997. }
  998. }
  999. } else if (ReadFunction(pexpr, Equals)) {
  1000. if (Is(SemiColon, true)) {
  1001. penvOut =
  1002. new Environment(
  1003. penvOut,
  1004. str,
  1005. CreateDelay(
  1006. penvTop,
  1007. pexpr
  1008. )
  1009. );
  1010. continue;
  1011. }
  1012. }
  1013. return false;
  1014. }
  1015. if (penvOut != penvIn) {
  1016. penvTop->SetNext(penvOut);
  1017. penvOut = penvTop;
  1018. return true;
  1019. }
  1020. return false;
  1021. }
  1022. }
  1023. };
  1024. //////////////////////////////////////////////////////////////////////////////
  1025. //
  1026. // Application
  1027. //
  1028. //////////////////////////////////////////////////////////////////////////////
  1029. class MDLApp : public Win32App {
  1030. private:
  1031. TRef<Environment> m_penv;
  1032. public:
  1033. MDLApp()
  1034. {
  1035. AddValue("emptyList", new ConsValue(NULL, NULL));
  1036. AddValue("true", new BooleanValue(true));
  1037. AddValue("false", new BooleanValue(false));
  1038. AddFunction("not", new NotExpression(), "x");
  1039. AddFunction("equal", new EqualNumberExpression(), "x", "y");
  1040. AddFunction("mul", new MultiplyExpression(), "x", "y");
  1041. AddFunction("div", new DivideExpression(), "x", "y");
  1042. AddFunction("mod", new ModExpression(), "x", "y");
  1043. AddFunction("add", new AddExpression(), "x", "y");
  1044. AddFunction("sub", new SubtractExpression(), "x", "y");
  1045. AddFunction("cons", new ConsExpression(), "head", "tail");
  1046. AddFunction("head", new HeadExpression(), "cons");
  1047. AddFunction("tail", new TailExpression(), "cons");
  1048. AddFunction("empty", new EmptyExpression(), "cons");
  1049. AddFunction("pair", new PairExpression(), "first", "second");
  1050. AddFunction("first", new FirstExpression(), "first");
  1051. AddFunction("second", new SecondExpression(), "second");
  1052. AddFunction("if", new IfExpression(), "condition", "then", "else");
  1053. }
  1054. void AddValue(const ZString& strName, Value* pvalue)
  1055. {
  1056. m_penv =
  1057. new Environment(
  1058. m_penv,
  1059. strName,
  1060. CreateDelay(
  1061. NULL,
  1062. CreateValueExpression(
  1063. pvalue
  1064. )
  1065. )
  1066. );
  1067. }
  1068. void AddFunction(const ZString& strName, Expression* pexpr, const ZString& str1)
  1069. {
  1070. m_penv =
  1071. new Environment(
  1072. m_penv,
  1073. strName,
  1074. CreateDelay(
  1075. NULL,
  1076. CreateFunctionExpression(
  1077. str1,
  1078. pexpr
  1079. )
  1080. )
  1081. );
  1082. }
  1083. void AddFunction(const ZString& strName, Expression* pexpr, const ZString& str1, const ZString& str2)
  1084. {
  1085. m_penv =
  1086. new Environment(
  1087. m_penv,
  1088. strName,
  1089. CreateDelay(
  1090. NULL,
  1091. CreateFunctionExpression(
  1092. str1,
  1093. CreateFunctionExpression(
  1094. str2,
  1095. pexpr
  1096. )
  1097. )
  1098. )
  1099. );
  1100. }
  1101. void AddFunction(const ZString& strName, Expression* pexpr, const ZString& str1, const ZString& str2, const ZString& str3)
  1102. {
  1103. m_penv =
  1104. new Environment(
  1105. m_penv,
  1106. strName,
  1107. CreateDelay(
  1108. NULL,
  1109. CreateFunctionExpression(
  1110. str1,
  1111. CreateFunctionExpression(
  1112. str2,
  1113. CreateFunctionExpression(
  1114. str3,
  1115. pexpr
  1116. )
  1117. )
  1118. )
  1119. )
  1120. );
  1121. }
  1122. void Output(const ZString& str)
  1123. {
  1124. OutputDebugStringA((PCC)str);
  1125. }
  1126. void Open(const ZString& str)
  1127. {
  1128. TRef<ZFile> pfile = new ZFile(str);
  1129. if (pfile) {
  1130. MDLParser parser((PCC)pfile->GetPointer(), pfile->GetLength());
  1131. TRef<Environment> penv;
  1132. if (parser.ReadDefinitions(penv, m_penv, false)) {
  1133. TRef<Delay> pdelay = penv->Find("result");
  1134. TRef<Type> ptype = pdelay->GetType();
  1135. TRef<Value> pvalue = pdelay->Evaluate();
  1136. int id = 'a';
  1137. Output(ptype->GetString(id) + "\n");
  1138. Output(pvalue->GetString() + "\n");
  1139. } else if (parser.Error()) {
  1140. // !!! there was an error in the file
  1141. }
  1142. }
  1143. }
  1144. HRESULT Initialize(const ZString& strCommandLine)
  1145. {
  1146. PCC pcc = strCommandLine;
  1147. CommandLineToken token(pcc, strCommandLine.GetLength());
  1148. while (token.MoreTokens()) {
  1149. ZString strInput;
  1150. ZString strOutput;
  1151. if (token.Is(token.Minus, false)) {
  1152. ZString str;
  1153. if (token.IsSymbol(str, true)) {
  1154. }
  1155. } else if (token.IsPath(strInput, true)) {
  1156. Open(strInput);
  1157. return S_FALSE;
  1158. }
  1159. break;
  1160. }
  1161. printf("Usage: MDL filename\n");
  1162. return S_FALSE;
  1163. }
  1164. } g_app;