Compare commits
770 Commits
master
...
autoparser
Author | SHA1 | Date |
---|---|---|
greg | fb26293157 | |
greg | c0289f238f | |
greg | 7afb9d47fc | |
greg | 2431a074b0 | |
greg | 3bfd251a68 | |
greg | a033c82d13 | |
greg | c176c1c918 | |
greg | aa40b985f3 | |
greg | 64a3705e35 | |
greg | 7e23e40a2f | |
greg | 4c88a7ada6 | |
greg | 367719d408 | |
greg | 2e80045750 | |
greg | 35c67f73c3 | |
greg | da9aa1e29d | |
greg | ca67f9b4fe | |
greg | 60cce3fe9c | |
greg | 4eb22f94d0 | |
greg | 355c8170a4 | |
greg | e3671a579d | |
greg | 08ca48b2ba | |
greg | fea9b9575b | |
greg | 276dad56d7 | |
greg | 695e733584 | |
greg | 9bfd751db6 | |
greg | b058e47d79 | |
greg | 5be53dc847 | |
greg | a0bea0d55a | |
greg | 9747374e8a | |
greg | 9ab1ca28f8 | |
greg | 66cd51a355 | |
greg | 1056be12e7 | |
greg | 48e7c0be03 | |
greg | 6e82d1207e | |
greg | f0e7c9906e | |
greg | 57c7858c87 | |
greg | a105c84943 | |
greg | 2b8d63d9cc | |
greg | c807c20292 | |
greg | a643c8a792 | |
greg | 69200048fa | |
greg | 55e372a670 | |
greg | c50626241e | |
greg | 232bec97a7 | |
greg | ce1d967f08 | |
greg | daa0062108 | |
greg | 3e7c7a50b4 | |
greg | 2574a1b9c0 | |
greg | c285ee182e | |
greg | f7659a5598 | |
greg | 1064d9993a | |
greg | 0e3320e183 | |
greg | 89a2be19f4 | |
greg | d9e96398a4 | |
greg | a564ffa1ce | |
greg | b3fff100d2 | |
greg | cfd6df7ba5 | |
greg | bb2e1ae27a | |
greg | 4333563d03 | |
greg | e7cabb2a79 | |
greg | 5da7c809b2 | |
greg | d229a57837 | |
greg | 0dd8861f83 | |
greg | 4ab900d601 | |
greg | 501b975fb6 | |
greg | 83315e97ac | |
greg | 6259a0808c | |
greg | 0c69476fd0 | |
greg | 1caccc6ae2 | |
greg | 23af2b1455 | |
greg | 61795b0331 | |
greg | a1b874c891 | |
greg | e7103b925b | |
greg | c35401da65 | |
greg | d51a9a73d7 | |
greg | fddd43b86e | |
greg | 12f55fa844 | |
greg | bb0fb716e4 | |
greg | 687d482853 | |
greg | 628eb28deb | |
greg | 4c8b4c8c71 | |
greg | c674148772 | |
greg | 7b4f69dce5 | |
greg | 98caf1cac3 | |
greg | 457799e0f7 | |
greg | 681d767855 | |
greg | ef4620e90a | |
greg | 1f2a4c706f | |
greg | a452bccd1c | |
greg | eca2218f6a | |
greg | 83aedb0efb | |
greg | 76841de784 | |
greg | c0574ff1ef | |
greg | faa5c6ab42 | |
greg | 9c2d2190b0 | |
greg | 21511f5120 | |
greg | 8bd399f97a | |
greg | 30a6d0929a | |
greg | bec8aedc22 | |
greg | 1b642c6321 | |
greg | 559306ffc8 | |
greg | 540ffde4bc | |
greg | fa8d46e3d7 | |
greg | 4598802999 | |
greg | 5ea83e2da6 | |
greg | 23c0f54042 | |
greg | 8618de313b | |
greg | cd23b23a91 | |
greg | e6475a1262 | |
greg | e6f81b28f9 | |
greg | c20d75faf1 | |
greg | 85aabed344 | |
greg | aa821e720a | |
greg | 0e25720927 | |
greg | c101610cde | |
greg | 1217f6e143 | |
greg | 6f41167402 | |
greg | cf0af7e0c9 | |
greg | a7fd515e7b | |
greg | c6509338d8 | |
greg | 192a6bf6e1 | |
greg | 7e8c4267c2 | |
greg | 25527bbdf0 | |
greg | 5e7aef1040 | |
greg | 3386fcc505 | |
greg | 18a839bb91 | |
greg | 92d641fca0 | |
greg | 9b4499c5ac | |
greg | 07e19cbfa2 | |
greg | 2016fcab41 | |
greg | 0e7b6f25b3 | |
greg | 7e7aa55d6e | |
greg | cc79565fb3 | |
greg | 5659bab684 | |
greg | 405f91a770 | |
greg | faed1d6f25 | |
greg | f6d047e3b8 | |
greg | fcd980f148 | |
greg | c3919daa66 | |
greg | f8152f68ad | |
greg | 9273773bf4 | |
greg | 844cef36c7 | |
greg | 0e17e45f3e | |
greg | 18d8ca7bd5 | |
greg | 2ee14bf740 | |
greg | 502497687a | |
greg | 107897ec97 | |
greg | 8534fb4118 | |
greg | a1e38aba8e | |
greg | 2f8ef99b08 | |
greg | 2bb55b6cca | |
greg | bcd70ff538 | |
greg | 728393671f | |
greg | 9c3e223e51 | |
greg | 2738119f17 | |
greg | 630ead289c | |
greg | 485e869c90 | |
greg | 9e8a3d1f08 | |
greg | b1da524a8f | |
greg | 787b6d51a4 | |
greg | 1dae4443cd | |
greg | 210a45c92e | |
greg | 815e0401f2 | |
greg | 753247ee83 | |
greg | 6223fc20f3 | |
greg | da928db351 | |
greg | 93d0cfe5b8 | |
greg | 687b28d1d1 | |
greg | b62f618256 | |
greg | f25b76ea11 | |
greg | 6b2736348d | |
greg | 69d5f38ea1 | |
greg | a6f8616839 | |
greg | cdcb55e3b8 | |
greg | 74ac26841f | |
greg | 8fd29b5090 | |
greg | 5ebc96daa7 | |
greg | 277e039251 | |
greg | 6e8f57e54f | |
greg | ae02391270 | |
greg | 9379485713 | |
greg | 910522537c | |
greg | 98e1a5235a | |
greg | e054c4b27f | |
greg | e3b0f4a51e | |
greg | 911f26e9c6 | |
greg | 677e3ae0a9 | |
greg | 9611770bb3 | |
greg | 4c256cb5f7 | |
greg | 688e1c7f5d | |
greg | 26c9c72bcc | |
greg | 2d614aa17a | |
greg | ecb2eb0f87 | |
greg | 4c4004d3ac | |
greg | 9b4a23c4f2 | |
greg | 936c168cef | |
greg | db835f42aa | |
greg | cd5fc36c37 | |
greg | d7a33c974e | |
greg | b2288206d2 | |
greg | d962e2c27a | |
greg | 4534c1d3d6 | |
greg | f79dc0b1e3 | |
greg | 4928fc0019 | |
greg | d735e45688 | |
greg | b4208b696d | |
greg | ff3dbbcbc6 | |
greg | e3261be8a0 | |
greg | f131105b50 | |
greg | 1089a33634 | |
greg | 6c60794485 | |
greg | 2f18529bcc | |
greg | c68e09d89d | |
greg | d9e8178a90 | |
greg | 57536e6399 | |
greg | 32e077c407 | |
greg | 83752a1c74 | |
greg | 33d0d49d30 | |
greg | 76a9367284 | |
greg | 66c7bbeb07 | |
greg | ed8359bcd7 | |
greg | 996f75e15c | |
greg | 30a54d997c | |
greg | 4bcbf1854a | |
greg | f2c6556c2a | |
greg | 9161e2751f | |
greg | 60fc9fd7e1 | |
greg | 3b249045aa | |
greg | ff0e14d9a9 | |
greg | 8fe535597e | |
greg | 4bb8f82579 | |
greg | 5cb8423ecc | |
greg | 4032707dc9 | |
greg | 1a8423535a | |
greg | 338981febe | |
greg | 6dff8b029e | |
greg | df877830d3 | |
greg | 40696b3cbd | |
greg | 40a82d7e25 | |
greg | c605f76059 | |
greg | a6d71821b9 | |
greg | c4f0331d1a | |
greg | b4054d7581 | |
greg | 74d3828c71 | |
greg | bb57da564d | |
greg | 3f9ae5fac3 | |
greg | 62edc7c996 | |
greg | e412fb9a89 | |
greg | 3a97401f61 | |
greg | 87cfe854ac | |
greg | 184a2ae03a | |
greg | 50ceb92d6b | |
greg | dd7736e32d | |
greg | 3025af3ded | |
greg | 24608362a5 | |
greg | c83df6fd84 | |
greg | 65f64ebcc2 | |
greg | 00ee802fbd | |
greg | c88d59401c | |
greg | 1aa4e3b942 | |
greg | abbbb34901 | |
greg | 3ff4a34aeb | |
greg | 7430aebe63 | |
greg | 9071846df3 | |
greg | 29d307ff53 | |
greg | 89482e5b5a | |
greg | 6435d5e958 | |
greg | f6536e7ebd | |
greg | c5feea4597 | |
greg | c5cb223168 | |
greg | d16a0c9380 | |
greg | daf9878020 | |
greg | f825c87397 | |
greg | 8d2a65b44e | |
greg | 6b9fee1aed | |
greg | d05f173dd3 | |
greg | e88a0f59b5 | |
greg | 90cf7db609 | |
greg | 1ae9dbcba7 | |
greg | 9214f36c04 | |
greg | 98169bd352 | |
greg | d60cf99ab5 | |
greg | bb93d29beb | |
greg | c20f93e18c | |
greg | f48adbd9bf | |
greg | 9ad506fc78 | |
greg | 4c81c36d67 | |
greg | 230f2dd7ff | |
greg | 1615269a7b | |
greg | 1a8ba8d8a2 | |
greg | d4aec19c71 | |
greg | 10b1864680 | |
greg | 4831a24853 | |
greg | 67ff21d408 | |
greg | 8b83d982c0 | |
greg | 6bff7aac0d | |
greg | 18fa160fed | |
greg | 7ac5846282 | |
greg | 88209d370d | |
greg | 0f9d2d76c4 | |
greg | 006fd7d411 | |
greg | e3b236a15d | |
greg | 68bbd62ab6 | |
greg | e47a2c7241 | |
greg | a8b77848b4 | |
greg | 839731f2d1 | |
greg | f51e1a3c47 | |
greg | fc350cd03e | |
greg | 8fe7c85b00 | |
greg | b920fae93b | |
greg | 1981b74d89 | |
greg | 077ab8ddb8 | |
greg | 9775bfc342 | |
greg | 505d23a327 | |
greg | 81c4566c2b | |
greg | 8be757beca | |
greg | 20c74953b5 | |
greg | d5c3227966 | |
greg | fbeb101e7f | |
greg | 18c761a5b5 | |
greg | 89cf101362 | |
greg | 66d10604ba | |
greg | 565461e1db | |
greg | 5ecd28d057 | |
greg | 6c5dbac406 | |
greg | 5dd1cd79ff | |
greg | dfc89e5060 | |
greg | 5871bf68de | |
greg | 34b569eb5f | |
greg | 3220f0aec7 | |
greg | 5810fb7961 | |
greg | 1a3076a949 | |
greg | 1d9e5edfba | |
greg | 555d2a7ba5 | |
greg | 291fb61c8d | |
greg | 685b579fdd | |
greg | f72e77cbb6 | |
greg | 4b5afef17e | |
greg | 5889998126 | |
greg | c19946bb6d | |
greg | d1301b30e6 | |
greg | f8287e42ce | |
greg | bd6bf2f4bb | |
greg | a6b336d84c | |
greg | deab74b992 | |
greg | 0755d42112 | |
greg | 117e0e38a8 | |
greg | 3f1de5f60d | |
greg | c52fd4c73d | |
greg | cac3ea86cf | |
greg | 92ece39d5e | |
greg | 14c09bb40c | |
greg | 0dabbc700b | |
greg | 741e5f7f9b | |
greg | cfefceabf9 | |
greg | ea08f8cab8 | |
greg | 7d1c07c481 | |
greg | 7831cb8d8a | |
greg | 16d9e3eb60 | |
greg | 737dad6438 | |
greg | 74f8c16599 | |
greg | a82f24a158 | |
greg | 6459ad28e8 | |
greg | 5e0c7e5a95 | |
greg | 57d4222746 | |
greg | 88d1896281 | |
greg | 7fe0a6589e | |
greg | ac5bdd7bcb | |
greg | 8bf5f40a2a | |
greg | 7e505dd88e | |
greg | f15427e5d9 | |
greg | 8230b115de | |
greg | a53135a897 | |
greg | f3c8474c93 | |
greg | 8dc8d15437 | |
greg | b5a6c5903e | |
greg | c97e58c2aa | |
greg | cb9b56f000 | |
greg | 55e1600b97 | |
greg | fb009497a4 | |
greg | 4b13fef734 | |
greg | 14ccf9f1be | |
greg | 7a6dfbbd0e | |
greg | bb3f85dd16 | |
greg | 3e66568ddd | |
greg | 3abe299361 | |
greg | 626b17cbd2 | |
greg | 192a7e611f | |
greg | d3febb201b | |
greg | f9fe81993f | |
greg | ff01d4b798 | |
greg | dd22ca0291 | |
greg | 801896bcc6 | |
greg | 6f8c89af37 | |
greg | f82e1e699c | |
greg | a0f3583ab1 | |
greg | e81d5e108b | |
greg | 5d15d60ab6 | |
greg | f0de3c3d12 | |
greg | 9dd8f90e3c | |
greg | e0f5f01e69 | |
greg | 424998c128 | |
greg | b93625819c | |
greg | f90bfb88ca | |
greg | 850b77541b | |
greg | dbf5886aad | |
greg | dd93adf5b7 | |
greg | d8df98ba01 | |
greg | 4da771036a | |
greg | 3911c45dde | |
greg | f3c3d4595e | |
greg | e4a42e7691 | |
greg | cc537f292d | |
greg | 840e093bc4 | |
greg | 815f2b8242 | |
greg | 34dba9cc4d | |
greg | 6e28ae68a0 | |
greg | 48b0b8d053 | |
greg | e0c49abe56 | |
greg | 65dc362a1d | |
greg | 8ff1c632c2 | |
greg | 039022bfc5 | |
greg | 387ec25cda | |
greg | ecf60198fa | |
greg | f83cece2b4 | |
greg | 8fd5fb5a0b | |
greg | 455fe2abe2 | |
greg | 902c85ccd7 | |
greg | 4ea600d55c | |
greg | 6dec35d460 | |
greg | cc855affbf | |
greg | a303aa2a5b | |
greg | 421a9a7e9b | |
greg | f37ab80163 | |
greg | 178434171e | |
greg | fd4610e175 | |
greg | 5103f03fa5 | |
greg | 1a4bf24ab1 | |
greg | 9d6bdf22da | |
greg | 8326a12c9c | |
greg | 5e474231da | |
greg | 1ac440c8df | |
greg | f5022a771c | |
greg | eaf86ea908 | |
greg | eb6354e55a | |
greg | 751c6f65bd | |
greg | 3e231b4715 | |
greg | e103ba221c | |
greg | d5f01a7b1f | |
greg | 1702163478 | |
greg | bdd6f75cf6 | |
greg | 2681dbc4f2 | |
greg | 9454fc6194 | |
greg | c8feaa9b57 | |
greg | 518414ffd5 | |
greg | 06a5de6e32 | |
greg | dd4816624c | |
greg | 748a85db02 | |
greg | 8f2d9b900b | |
greg | b9d1140264 | |
greg | 7188a7d33e | |
greg | 0c7099771f | |
greg | afec7e829c | |
greg | a6773d59bd | |
greg | d804efdc5e | |
greg | 0ace370fc2 | |
greg | 1f50fcc620 | |
greg | d7181afa91 | |
greg | 4eb7683f47 | |
greg | b04a8f0092 | |
greg | c50be58cd2 | |
greg | 5911a07f4f | |
greg | 26bc6e90f3 | |
greg | b0655d7cab | |
greg | a46ede9395 | |
greg | d9ab5a58cf | |
greg | 77297c7e06 | |
greg | d93b5c0a2e | |
greg | 0b9dc113d1 | |
greg | d6fc13f08d | |
greg | 825c271b17 | |
greg | 8c4f7e141a | |
greg | 12fbc51da1 | |
greg | db108ee434 | |
greg | 7ddb421ced | |
greg | 1631bb0a04 | |
greg | 5923cc2317 | |
greg | 1fa56800c5 | |
greg | 2b4d3e8516 | |
greg | 9b74527618 | |
greg | d23e5bff35 | |
greg | 3a4f5ae840 | |
greg | 298194c42d | |
greg | 23d2209d8b | |
greg | 4cf165b408 | |
greg | 154839979b | |
greg | 6741787852 | |
greg | 538f0b18f4 | |
greg | dc81d237c5 | |
greg | 8651839a66 | |
greg | f6e5ea250d | |
greg | 9801f53a17 | |
greg | db92292569 | |
greg | e1ce54aece | |
greg | c227ad656f | |
greg | b45d09e81a | |
greg | 761500b9d6 | |
greg | e888e82404 | |
greg | 328ec4ba87 | |
greg | 4a7b570603 | |
greg | 7eb48fb4ef | |
greg | 8ebf1b3056 | |
greg | 905431b33c | |
greg | 2996198eff | |
greg | 06771979df | |
greg | f158b6c712 | |
greg | ba8f67441f | |
greg | 872e9ce7ee | |
greg | 2722533efd | |
greg | edf342e65a | |
greg | 27d4c2ccbd | |
greg | 6794d22f1d | |
greg | 1858d26638 | |
greg | 84fbe73cf6 | |
greg | ad994c38ae | |
greg | 48343d3fad | |
greg | 4f8ff35d0f | |
greg | b210ad5e19 | |
greg | 7311d0311f | |
greg | 1b59c264b4 | |
greg | b2e453a9de | |
greg | 59226eb731 | |
greg | 297003c0b0 | |
greg | e5ee072b00 | |
greg | 9b62efc830 | |
greg | f626ca1427 | |
greg | 82c52ede48 | |
greg | 7f52b20d97 | |
greg | 12fee6158c | |
greg | 2d21de7cc3 | |
greg | 9cc9c5977d | |
greg | ed9d1312d1 | |
greg | 29d9e50311 | |
greg | e84550f3ec | |
greg | 3063de1242 | |
greg | e1d07b4e66 | |
greg | af45004afa | |
greg | d4d61ce5ad | |
greg | 743311d18a | |
greg | 17f9846bb9 | |
greg | fe8418edbe | |
greg | 1d8102b9fa | |
greg | aac3ca40fe | |
greg | c389b44bf8 | |
greg | db52f9b716 | |
greg | d3a743442b | |
greg | e46d840d96 | |
greg | 3d406f1dd2 | |
greg | fca307d3ab | |
greg | a65544356c | |
greg | 10d51cc29c | |
greg | 95b773de7f | |
greg | c032da712f | |
greg | cfb93d9dea | |
greg | 08798fb690 | |
greg | e0eef5e58f | |
greg | 2107e6344e | |
greg | 2ad65a1c5d | |
greg | a01b6c874e | |
greg | 907af38f44 | |
greg | 923566c4e9 | |
greg | 96c51a9b88 | |
greg | 8528c912bd | |
greg | 47f4c25020 | |
greg | 6ed67cd325 | |
greg | c18c1a639a | |
greg | d3f6bbabaa | |
greg | 5d0648b9c0 | |
greg | ba7a2e658f | |
greg | ee12e10ac6 | |
greg | 6a0b50f278 | |
greg | 6c44d295db | |
greg | cd69ebaa9d | |
greg | 3fe9ec95d5 | |
greg | 8b5d1ecd15 | |
greg | 272fa92052 | |
greg | 0c717c721e | |
greg | 64d560a1fc | |
greg | 8a92d5ffa8 | |
greg | 5ed0ed727b | |
greg | 5428810d2c | |
greg | b48c007bca | |
greg | 5aa4c404a5 | |
greg | 0e3aaa8b08 | |
greg | f33cfdadfe | |
greg | 231de69084 | |
greg | f014c1a9d9 | |
greg | c36bc3377e | |
greg | 5eba222679 | |
greg | dcf89aa429 | |
greg | 1ffbeb6472 | |
greg | f8a521fc9b | |
greg | 77f72806be | |
greg | dcde5d6018 | |
greg | 6bb227d052 | |
greg | 29d4cb53a4 | |
greg | 3915c1f035 | |
greg | b400796e4d | |
greg | 96e6a87f64 | |
greg | b1f9e5cefc | |
greg | be36d4697d | |
greg | ce8c511929 | |
greg | a8cafa8c64 | |
greg | 229e6ae733 | |
greg | e9dd0d9ae8 | |
greg | 15d4317191 | |
greg | 7114e446a4 | |
greg | 044f534ac5 | |
greg | d3207ad890 | |
greg | 19fffd5063 | |
greg | 785c916ece | |
greg | 5a9ebb188d | |
greg | 16e8d969be | |
greg | 70bf68d9bd | |
greg | f53c14535b | |
greg | 4f96abd7d9 | |
greg | fdaf4c302c | |
greg | 8ce53d7c72 | |
greg | 428d560e2a | |
greg | 80bc7ec089 | |
greg | e6591b80d9 | |
greg | e099f713ad | |
greg | 9b54256521 | |
greg | 087402ece6 | |
greg | 252b6e8bd9 | |
greg | b1163e2ae4 | |
greg | 032d01c9f5 | |
greg | c4ab1ed105 | |
greg | 9a257f08d7 | |
greg | 47d56a7b44 | |
greg | 1f7ae2e30f | |
greg | e3c8753a4d | |
greg | e1aa7ecb17 | |
greg | f09a6e14ba | |
greg | 31da25a66e | |
greg | fff9cb7d25 | |
greg | db1e188fdb | |
greg | 935185ed92 | |
greg | 0999cbe28e | |
greg | 674f70a428 | |
greg | b1b6672399 | |
greg | bd1c455dc8 | |
greg | b62ef43f07 | |
greg | 09b67dc3f7 | |
greg | a613fa73e5 | |
greg | 8c473c554e | |
greg | 3e04cbfa29 | |
greg | 570650cbfa | |
greg | 49be163181 | |
greg | b4f93acbd8 | |
greg | 8c65ae3214 | |
greg | e436533638 | |
greg | 4601a56867 | |
greg | 2f7a1850db | |
greg | 71aef379d3 | |
greg | 8662a3ba0e | |
greg | 5ca98c7d77 | |
greg | 13cde3106c | |
greg | 09d524c74a | |
greg | 61c36c4def | |
greg | bc4fbe4276 | |
greg | fc11ee753d | |
greg | dd2b4893a4 | |
greg | 51745fd800 | |
greg | 8c0ac19fa8 | |
greg | 971ab9ba21 | |
greg | 819fb3f58f | |
greg | 1c23329656 | |
greg | 6da20cbfaf | |
greg | f48451125e | |
greg | 06c3999430 | |
greg | 1af1589550 | |
greg | 32a90b8103 | |
greg | 186c900920 | |
greg | 509ab80b9c | |
greg | 247638c4db | |
greg | be98f8387e | |
greg | 841b38d5b1 | |
greg | 16dfbb27d5 | |
greg | 3af7e6a409 | |
greg | 123f388711 | |
greg | bb349cda5f | |
greg | caa331ecdc | |
greg | ae3a030ad8 | |
greg | ddb09b453d | |
greg | 9a4760d44f | |
greg | eb5ce2ef9e | |
greg | b080ea7c81 | |
greg | dffab8ae94 | |
greg | 48ee6c9a75 | |
greg | 58d399dace | |
greg | 57ea1bae30 | |
greg | e870d8172a | |
greg | 72b26755a7 | |
greg | 1416c9d444 | |
greg | 5c79563e53 | |
greg | c27c900e7f | |
greg | 50c5dbe96d | |
greg | be8c8b3343 | |
greg | b856023072 | |
greg | 868373f409 | |
greg | 582a7fd6dc | |
greg | a947ec3cb2 | |
greg | 79619025ea | |
greg | 56b338a6a8 | |
greg | 7d6f946e22 | |
greg | 0da7f7e3a1 | |
greg | 19a344fa77 | |
greg | 626a7f3861 | |
greg | 8e3a571d67 | |
greg | f88f115567 | |
greg | 08f1092b69 | |
greg | 6ddea790c0 | |
greg | 6897eb1283 | |
greg | 34fdf2be00 | |
greg | 4ef93fafb5 | |
greg | d2108f0f97 | |
greg | 2989ac338c | |
greg | 8b6d54aec2 | |
greg | cdb47bb3b9 | |
greg | 9d6dc5a5f2 | |
greg | 71d2428e57 | |
greg | 8f9bfbc5bd | |
greg | c9fdd5e83c | |
greg | 30eddf7737 | |
greg | 42719dc2f2 | |
greg | 8fcc850d77 | |
greg | f421918945 | |
greg | 0e4469fa58 | |
greg | edf100b583 | |
greg | 169e662049 | |
greg | 46999beabf | |
greg | 1342a76786 | |
greg | 05238bced3 | |
greg | a97cce184c | |
greg | 1ae61287c1 | |
greg | 329c521964 | |
greg | bfa16fd6fb | |
greg | 25f5188d8c | |
greg | 5213dd327f | |
greg | cea29094cd | |
greg | 67eafba97a | |
greg | 1059a88ee6 | |
greg | 429ace73bd | |
greg | 044e7a6a26 | |
greg | dbdae42c1b | |
greg | fc3dcf792d | |
greg | 02b34ca105 | |
greg | 1e9cd551a6 | |
greg | 9f4330889a | |
greg | 3058af4f05 | |
greg | 4f17d5a0dc | |
greg | 8e3774ffca | |
greg | c6059ada7d | |
greg | b5ee45f639 | |
greg | 04f53b6beb | |
greg | 2aaa600d53 | |
greg | c6a92728ee | |
greg | b2e23bed86 | |
greg | 3fdacf018e |
|
@ -1 +1,4 @@
|
||||||
|
Cargo.lock
|
||||||
target
|
target
|
||||||
|
.schala_repl
|
||||||
|
.schala_history
|
||||||
|
|
16
Cargo.toml
16
Cargo.toml
|
@ -1,12 +1,20 @@
|
||||||
[package]
|
[package]
|
||||||
name = "null_only_language"
|
name = "schala"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
simplerepl = { path = "../simplerepl" }
|
|
||||||
llvm-sys = "*"
|
llvm-sys = "*"
|
||||||
|
take_mut = "0.1.3"
|
||||||
|
itertools = "0.5.8"
|
||||||
|
lazy_static = "0.2.8"
|
||||||
|
maplit = "*"
|
||||||
|
colored = "1.5"
|
||||||
|
|
||||||
[dependencies.iron_llvm]
|
schala-lib = { path = "schala-lib" }
|
||||||
git = "https://github.com/jauhien/iron-llvm.git"
|
|
||||||
|
[build-dependencies]
|
||||||
|
includedir_codegen = "0.2.0"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
|
||||||
|
<program> := <statements> EOF
|
||||||
|
|
||||||
|
<statements> := <statement>
|
||||||
|
| <statement> SEP <statements>
|
||||||
|
|
||||||
|
<statement> := let <id> = <expr>
|
||||||
|
| <expr>
|
||||||
|
| <fn_block>
|
||||||
|
|
||||||
|
<fn_block> := fn <id> ( <arg_list> ) <statements> end
|
||||||
|
|
||||||
|
<arg_list> := e
|
||||||
|
| <id>
|
||||||
|
| <id> , <arg_list>
|
||||||
|
|
||||||
|
<expr> := if <expr> then <statements> end
|
||||||
|
| if <expr> then <statements> else <statements> end
|
||||||
|
| while <expr> SEP <statements> end
|
||||||
|
| ( <expr> )
|
||||||
|
| <binop>
|
||||||
|
|
||||||
|
<binop> := <simple_expr>
|
||||||
|
| <simple_expr> <id> <binop>
|
||||||
|
|
||||||
|
<simple_expr> := <id>
|
||||||
|
| <number>
|
||||||
|
| <string>
|
||||||
|
|
||||||
|
|
4
README
4
README
|
@ -1,4 +0,0 @@
|
||||||
|
|
||||||
No-runtime-value-error-language
|
|
||||||
|
|
||||||
A language wth a largely-python-like where there are no value errors. Can call null like a function
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
|
||||||
|
# Schala - a programming language meta-interpreter
|
||||||
|
|
||||||
|
Schala is a Rust framework written to make it easy to
|
||||||
|
create and experiment with toy programming languages. It provides
|
||||||
|
a common REPL, and a trait `ProgrammingLanguage` with provisions
|
||||||
|
for tokenizing text, parsing tokens, evaluating an abstract syntax tree,
|
||||||
|
and other tasks that are common to all programming languages.
|
||||||
|
|
||||||
|
Schala is implemented as a Rust library `schala_lib`, which provides a
|
||||||
|
`schala_main` function. This function serves as the main loop of the REPL, if run
|
||||||
|
interactively, or otherwise reads and interprets programming language source
|
||||||
|
files. It expects as input a vector of `PLIGenerator`, which is a type representing
|
||||||
|
a closure that returns a boxed trait object that implements the `ProgrammingLanguage` trait,
|
||||||
|
and stores any persistent state relevant to that programming language. The ability
|
||||||
|
to share state between different programming languages is in the works.
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
Schala started out life as an experiment in writing a Javascript-like
|
||||||
|
programming language that would never encounter any kind of runtime value
|
||||||
|
error, but rather always return `null` under any kind of error condition. I had
|
||||||
|
seen one too many Javascript `Uncaught TypeError: Cannot read property ___ of
|
||||||
|
undefined` messages, and I was a bit frustrated. Plus I had always wanted to
|
||||||
|
write a programming langauge from scratch, and Rust is a fun language to
|
||||||
|
program in. Over time I became interested in playing around with other sorts
|
||||||
|
of programming languages as well, and wanted to make the process as general as
|
||||||
|
possible.
|
||||||
|
|
||||||
|
The name of the project comes from Schala the Princess of Zeal from the 1995
|
||||||
|
SNES RPG *Chrono Trigger*. I like classic JRPGs and enjoyed the thought of
|
||||||
|
creating a language name confusingly close to Scala. The naming scheme for
|
||||||
|
languages implemented with the Schala meta-interpreter is Chrono Trigger
|
||||||
|
characters.
|
||||||
|
|
||||||
|
Schala is incomplete alpha software and is not ready for public release.
|
||||||
|
|
||||||
|
## Languages implemented using the meta-interpreter
|
||||||
|
|
||||||
|
* The eponymous *Schala* language is an interpreted/compiled scripting langauge,
|
||||||
|
designed to be relatively simple, but with a reasonably sophisticated type
|
||||||
|
system.
|
||||||
|
|
||||||
|
* *Maaru* was the original Schala (since renamed to free up the name *Schala*
|
||||||
|
for the above language), a very simple dynamically-typed scripting language
|
||||||
|
such that all possible runtime errors result in null rather than program
|
||||||
|
failure.
|
||||||
|
|
||||||
|
* *Robo* is an experiment in creating a lazy, functional, strongly-typed language
|
||||||
|
much like Haskell
|
||||||
|
|
||||||
|
* *Rukka* is a straightforward LISP implementation
|
||||||
|
|
||||||
|
## Reference works
|
||||||
|
|
||||||
|
Here's a partial list of resources I've made use of in the process
|
||||||
|
of learning how to write a programming language.
|
||||||
|
|
||||||
|
### Type-checking
|
||||||
|
https://skillsmatter.com/skillscasts/10868-inside-the-rust-compiler
|
||||||
|
|
||||||
|
### Evaluation
|
||||||
|
*Understanding Computation*, Tom Stuart, O'Reilly 2013
|
||||||
|
|
||||||
|
*Basics of Compiler Design*, Torben Mogensen
|
||||||
|
|
||||||
|
### Parsing
|
||||||
|
http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
|
||||||
|
|
||||||
|
[Crafting Interpreters](http://www.craftinginterpreters.com/)
|
||||||
|
|
||||||
|
### LLVM
|
||||||
|
http://blog.ulysse.io/2016/07/03/llvm-getting-started.html
|
||||||
|
|
||||||
|
###Rust resources
|
||||||
|
https://thefullsnack.com/en/rust-for-the-web.html
|
||||||
|
https://rocket.rs/guide/getting-started/
|
|
@ -0,0 +1,46 @@
|
||||||
|
|
||||||
|
# TODO Items
|
||||||
|
|
||||||
|
* Share state between programming languages
|
||||||
|
|
||||||
|
* idea for Schala - scoped types - be able to define a quick enum type scoped to a function ro something, that only is meant to be used as a quick bespoke interface between two other things
|
||||||
|
|
||||||
|
* another idea, allow:
|
||||||
|
type enum {
|
||||||
|
type enum MySubVariant {
|
||||||
|
SubVariant1, SubVariant2, etc.
|
||||||
|
}
|
||||||
|
Variant1(MySubVariant),
|
||||||
|
Variant2(...),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* idea for Schala: both currying *and* default arguments!
|
||||||
|
ex. fn a(b: Int, c:Int, d:Int = 1) -> Int
|
||||||
|
a(1,2) : Int
|
||||||
|
a(1,2,d=2): Int
|
||||||
|
a(_,1,3) : Int -> Int
|
||||||
|
a(1,2, c=_): Int -> Int
|
||||||
|
a(_,_,_) : Int -> Int -> Int -> Int
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
- AST : maybe replace the Expression type with "Ascription(TypeName, Box<Expression>) nodes??
|
||||||
|
- parser: add a "debug" field to the Parser struct for all debug-related things
|
||||||
|
|
||||||
|
-scala-style html"dfasfsadf${}" string interpolations!
|
||||||
|
|
||||||
|
*Compiler passes architecture
|
||||||
|
|
||||||
|
-ProgrammingLanguageInterface defines a evaluate_in_repl() and evaluate_no_repl() functions
|
||||||
|
-these take in a vec of CompilerPasses
|
||||||
|
|
||||||
|
struct CompilerPass {
|
||||||
|
name: String,
|
||||||
|
run: fn(PrevPass) -> NextPass
|
||||||
|
}
|
||||||
|
|
||||||
|
-change "Type...." names in parser.rs to "Anno..." for non-collision with names in typechecking.rs
|
||||||
|
|
||||||
|
-get rid of code pertaining to compilation specifically, have a more generation notion of "execution type"
|
|
@ -0,0 +1,25 @@
|
||||||
|
[package]
|
||||||
|
name = "schala-lib"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
llvm-sys = "*"
|
||||||
|
take_mut = "0.1.3"
|
||||||
|
itertools = "0.5.8"
|
||||||
|
getopts = "*"
|
||||||
|
lazy_static = "0.2.8"
|
||||||
|
maplit = "*"
|
||||||
|
colored = "1.5"
|
||||||
|
serde = "1.0.15"
|
||||||
|
serde_derive = "1.0.15"
|
||||||
|
serde_json = "1.0.3"
|
||||||
|
rocket = "0.3.5"
|
||||||
|
rocket_codegen = "0.3.5"
|
||||||
|
rocket_contrib = "0.3.5"
|
||||||
|
phf = "0.7.12"
|
||||||
|
includedir = "0.2.0"
|
||||||
|
rustyline = "1.0.0"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
includedir_codegen = "0.2.0"
|
|
@ -0,0 +1,10 @@
|
||||||
|
extern crate includedir_codegen;
|
||||||
|
|
||||||
|
use includedir_codegen::Compression;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
includedir_codegen::start("WEBFILES")
|
||||||
|
.dir("../static", Compression::Gzip)
|
||||||
|
.build("static.rs")
|
||||||
|
.unwrap();
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
extern crate colored;
|
||||||
|
|
||||||
|
use self::colored::*;
|
||||||
|
|
||||||
|
pub struct LLVMCodeString(pub String);
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct EvalOptions {
|
||||||
|
pub debug_tokens: bool,
|
||||||
|
pub debug_parse: bool,
|
||||||
|
pub debug_type: bool,
|
||||||
|
pub debug_symbol_table: bool,
|
||||||
|
pub show_llvm_ir: bool,
|
||||||
|
pub trace_evaluation: bool,
|
||||||
|
pub compile: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct LanguageOutput {
|
||||||
|
output: String,
|
||||||
|
artifacts: Vec<TraceArtifact>,
|
||||||
|
failed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LanguageOutput {
|
||||||
|
pub fn add_artifact(&mut self, artifact: TraceArtifact) {
|
||||||
|
self.artifacts.push(artifact);
|
||||||
|
}
|
||||||
|
pub fn add_output(&mut self, output: String) {
|
||||||
|
self.output = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_string(&self) -> String {
|
||||||
|
let mut acc = String::new();
|
||||||
|
for line in self.artifacts.iter() {
|
||||||
|
acc.push_str(&line.debug_output.color(line.text_color).to_string());
|
||||||
|
acc.push_str(&"\n");
|
||||||
|
}
|
||||||
|
acc.push_str(&self.output);
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_to_screen(&self) {
|
||||||
|
for line in self.artifacts.iter() {
|
||||||
|
let color = line.text_color;
|
||||||
|
let stage = line.stage_name.color(color).to_string();
|
||||||
|
let output = line.debug_output.color(color).to_string();
|
||||||
|
println!("{}: {}", stage, output);
|
||||||
|
}
|
||||||
|
println!("{}", self.output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
//TODO I'll probably wanna implement this later
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CompilationOutput {
|
||||||
|
output: LLVMCodeString,
|
||||||
|
artifacts: Vec<TraceArtifact>,
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TraceArtifact {
|
||||||
|
stage_name: String,
|
||||||
|
debug_output: String,
|
||||||
|
text_color: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TraceArtifact {
|
||||||
|
pub fn new(stage: &str, debug: String) -> TraceArtifact {
|
||||||
|
let color = match stage {
|
||||||
|
"parse_trace" | "ast" => "red",
|
||||||
|
"tokens" => "green",
|
||||||
|
"type_check" => "magenta",
|
||||||
|
_ => "blue",
|
||||||
|
};
|
||||||
|
TraceArtifact { stage_name: stage.to_string(), debug_output: debug, text_color: color}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_parse_trace(trace: Vec<String>) -> TraceArtifact {
|
||||||
|
let mut output = String::new();
|
||||||
|
|
||||||
|
for t in trace {
|
||||||
|
output.push_str(&t);
|
||||||
|
output.push_str("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TraceArtifact { stage_name: "parse_trace".to_string(), debug_output: output, text_color: "red"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ProgrammingLanguageInterface {
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, eval_options: &EvalOptions) -> LanguageOutput;
|
||||||
|
fn evaluate_noninteractive(&mut self, input: &str, eval_options: &EvalOptions) -> LanguageOutput {
|
||||||
|
self.evaluate_in_repl(input, eval_options)
|
||||||
|
}
|
||||||
|
fn get_language_name(&self) -> String;
|
||||||
|
fn get_source_file_suffix(&self) -> String;
|
||||||
|
fn compile(&mut self, _input: &str) -> LLVMCodeString {
|
||||||
|
LLVMCodeString("".to_string())
|
||||||
|
}
|
||||||
|
fn can_compile(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,378 @@
|
||||||
|
#![feature(link_args)]
|
||||||
|
#![feature(advanced_slice_patterns, slice_patterns, box_patterns, box_syntax)]
|
||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_codegen)]
|
||||||
|
extern crate getopts;
|
||||||
|
extern crate rustyline;
|
||||||
|
extern crate itertools;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate maplit;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
extern crate serde_json;
|
||||||
|
extern crate rocket;
|
||||||
|
extern crate rocket_contrib;
|
||||||
|
extern crate includedir;
|
||||||
|
extern crate phf;
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::process::exit;
|
||||||
|
use std::default::Default;
|
||||||
|
|
||||||
|
use rustyline::error::ReadlineError;
|
||||||
|
use rustyline::Editor;
|
||||||
|
|
||||||
|
mod language;
|
||||||
|
mod webapp;
|
||||||
|
pub mod llvm_wrap;
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/static.rs"));
|
||||||
|
|
||||||
|
pub use language::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput, LLVMCodeString};
|
||||||
|
pub type PLIGenerator = Box<Fn() -> Box<ProgrammingLanguageInterface> + Send + Sync>;
|
||||||
|
|
||||||
|
pub fn schala_main(generators: Vec<PLIGenerator>) {
|
||||||
|
let languages: Vec<Box<ProgrammingLanguageInterface>> = generators.iter().map(|x| x()).collect();
|
||||||
|
|
||||||
|
let option_matches = program_options().parse(std::env::args()).unwrap_or_else(|e| {
|
||||||
|
println!("{:?}", e);
|
||||||
|
exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
if option_matches.opt_present("list-languages") {
|
||||||
|
for lang in languages {
|
||||||
|
println!("{}", lang.get_language_name());
|
||||||
|
}
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if option_matches.opt_present("help") {
|
||||||
|
println!("{}", program_options().usage("Schala metainterpreter"));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if option_matches.opt_present("webapp") {
|
||||||
|
webapp::web_main(generators);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
|
||||||
|
let initial_index: usize =
|
||||||
|
option_matches.opt_str("lang")
|
||||||
|
.and_then(|lang| { language_names.iter().position(|x| { x.to_lowercase() == lang.to_lowercase() }) })
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
let mut options = EvalOptions::default();
|
||||||
|
options.compile = match option_matches.opt_str("eval-style") {
|
||||||
|
Some(ref s) if s == "compile" => true,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
match option_matches.free[..] {
|
||||||
|
[] | [_] => {
|
||||||
|
let mut repl = Repl::new(languages, initial_index);
|
||||||
|
repl.options.show_llvm_ir = true; //TODO make this be configurable
|
||||||
|
repl.run();
|
||||||
|
}
|
||||||
|
[_, ref filename, _..] => {
|
||||||
|
|
||||||
|
run_noninteractive(filename, languages, options);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_noninteractive(filename: &str, languages: Vec<Box<ProgrammingLanguageInterface>>, options: EvalOptions) {
|
||||||
|
let path = Path::new(filename);
|
||||||
|
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| {
|
||||||
|
println!("Source file lacks extension");
|
||||||
|
exit(1);
|
||||||
|
});
|
||||||
|
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
println!("Extension .{} not recognized", ext);
|
||||||
|
exit(1);
|
||||||
|
}));
|
||||||
|
|
||||||
|
let mut source_file = File::open(path).unwrap();
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
source_file.read_to_string(&mut buffer).unwrap();
|
||||||
|
|
||||||
|
if options.compile {
|
||||||
|
if !language.can_compile() {
|
||||||
|
panic!("Trying to compile a non-compileable language");
|
||||||
|
} else {
|
||||||
|
let llvm_bytecode = language.compile(&buffer);
|
||||||
|
compilation_sequence(llvm_bytecode, filename);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let output = language.evaluate_in_repl(&buffer, &options);
|
||||||
|
// if output.has_error....
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Repl {
|
||||||
|
options: EvalOptions,
|
||||||
|
languages: Vec<Box<ProgrammingLanguageInterface>>,
|
||||||
|
current_language_index: usize,
|
||||||
|
interpreter_directive_sigil: char,
|
||||||
|
console: rustyline::Editor<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Repl {
|
||||||
|
fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl {
|
||||||
|
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
||||||
|
|
||||||
|
let console = Editor::<()>::new();
|
||||||
|
|
||||||
|
Repl {
|
||||||
|
options: Repl::get_options(),
|
||||||
|
languages: languages,
|
||||||
|
current_language_index: i,
|
||||||
|
interpreter_directive_sigil: '.',
|
||||||
|
console
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_options() -> EvalOptions {
|
||||||
|
File::open(".schala_repl")
|
||||||
|
.and_then(|mut file| {
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents)?;
|
||||||
|
Ok(contents)
|
||||||
|
})
|
||||||
|
.and_then(|contents| {
|
||||||
|
let options: EvalOptions = serde_json::from_str(&contents)?;
|
||||||
|
Ok(options)
|
||||||
|
}).unwrap_or(EvalOptions::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_options(&self) {
|
||||||
|
let ref options = self.options;
|
||||||
|
let read = File::create(".schala_repl")
|
||||||
|
.and_then(|mut file| {
|
||||||
|
let buf = serde_json::to_string(options).unwrap();
|
||||||
|
file.write_all(buf.as_bytes())
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Err(err) = read {
|
||||||
|
println!("Error saving .schala_repl file {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&mut self) {
|
||||||
|
println!("MetaInterpreter v 0.05");
|
||||||
|
|
||||||
|
self.console.get_history().load(".schala_history").unwrap_or(());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let language_name = self.languages[self.current_language_index].get_language_name();
|
||||||
|
let prompt_str = format!("{} >> ", language_name);
|
||||||
|
|
||||||
|
match self.console.readline(&prompt_str) {
|
||||||
|
Err(ReadlineError::Eof) | Err(ReadlineError::Interrupted) => break,
|
||||||
|
Err(e) => {
|
||||||
|
println!("Terminal read error: {}", e);
|
||||||
|
},
|
||||||
|
Ok(ref input) => {
|
||||||
|
self.console.add_history_entry(input);
|
||||||
|
if self.handle_interpreter_directive(input) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let output = self.input_handler(input);
|
||||||
|
println!("=> {}", output);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.console.get_history().save(".schala_history").unwrap_or(());
|
||||||
|
self.save_options();
|
||||||
|
println!("Exiting...");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn input_handler(&mut self, input: &str) -> String {
|
||||||
|
let ref mut language = self.languages[self.current_language_index];
|
||||||
|
let interpreter_output = language.evaluate_in_repl(input, &self.options);
|
||||||
|
interpreter_output.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
||||||
|
match input.chars().nth(0) {
|
||||||
|
Some(ch) if ch == self.interpreter_directive_sigil => (),
|
||||||
|
_ => return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut iter = input.chars();
|
||||||
|
iter.next();
|
||||||
|
let trimmed_sigil: &str = iter.as_str();
|
||||||
|
|
||||||
|
let commands: Vec<&str> = trimmed_sigil
|
||||||
|
.split_whitespace()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let cmd: &str = match commands.get(0).clone() {
|
||||||
|
None => return true,
|
||||||
|
Some(s) => s
|
||||||
|
};
|
||||||
|
|
||||||
|
match cmd {
|
||||||
|
"exit" | "quit" => {
|
||||||
|
self.save_options();
|
||||||
|
exit(0)
|
||||||
|
},
|
||||||
|
"help" => {
|
||||||
|
println!("Commands:");
|
||||||
|
println!("exit | quit");
|
||||||
|
println!("lang(uage) [go|show|next|previous]");
|
||||||
|
println!("set [show|hide] [tokens|parse|symbols|eval|llvm]");
|
||||||
|
}
|
||||||
|
"lang" | "language" => {
|
||||||
|
match commands.get(1) {
|
||||||
|
Some(&"show") => {
|
||||||
|
for (i, lang) in self.languages.iter().enumerate() {
|
||||||
|
if i == self.current_language_index {
|
||||||
|
println!("* {}", lang.get_language_name());
|
||||||
|
} else {
|
||||||
|
println!("{}", lang.get_language_name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(&"go") => {
|
||||||
|
match commands.get(2) {
|
||||||
|
None => println!("Must specify a language name"),
|
||||||
|
Some(&desired_name) => {
|
||||||
|
for (i, _) in self.languages.iter().enumerate() {
|
||||||
|
let lang_name = self.languages[i].get_language_name();
|
||||||
|
if lang_name.to_lowercase() == desired_name.to_lowercase() {
|
||||||
|
self.current_language_index = i;
|
||||||
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("Language {} not found", desired_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(&"next") => {
|
||||||
|
self.current_language_index = (self.current_language_index + 1) % self.languages.len();
|
||||||
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||||
|
}
|
||||||
|
Some(&"prev") | Some(&"previous") => {
|
||||||
|
self.current_language_index = if self.current_language_index == 0 { self.languages.len() - 1 } else { self.current_language_index - 1 };
|
||||||
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
||||||
|
},
|
||||||
|
Some(e) => println!("Bad `lang` argument: {}", e),
|
||||||
|
None => println!("`lang` - valid arguments `show`, `next`, `prev`|`previous`"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"set" => {
|
||||||
|
let show = match commands.get(1) {
|
||||||
|
Some(&"show") => true,
|
||||||
|
Some(&"hide") => false,
|
||||||
|
Some(e) => {
|
||||||
|
println!("Bad `set` argument: {}", e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("`set` - valid arguments `show {{option}}`, `hide {{option}}`");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match commands.get(2) {
|
||||||
|
Some(&"tokens") => self.options.debug_tokens = show,
|
||||||
|
Some(&"parse") => self.options.debug_parse = show,
|
||||||
|
Some(&"symbols") => self.options.debug_symbol_table = show,
|
||||||
|
Some(&"eval") => {
|
||||||
|
//let ref mut language = self.languages[self.current_language_index];
|
||||||
|
//language.set_option("trace_evaluation", show);
|
||||||
|
},
|
||||||
|
Some(&"llvm") => self.options.show_llvm_ir = show,
|
||||||
|
Some(e) => {
|
||||||
|
println!("Bad `show`/`hide` argument: {}", e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("`show`/`hide` requires an argument");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
e => println!("Unknown command: {}", e)
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compilation_sequence(llvm_code: LLVMCodeString, sourcefile: &str) {
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
let ll_filename = "out.ll";
|
||||||
|
let obj_filename = "out.o";
|
||||||
|
let q: Vec<&str> = sourcefile.split('.').collect();
|
||||||
|
let bin_filename = match &q[..] {
|
||||||
|
&[name, "maaru"] => name,
|
||||||
|
_ => panic!("Bad filename {}", sourcefile),
|
||||||
|
};
|
||||||
|
|
||||||
|
let LLVMCodeString(llvm_str) = llvm_code;
|
||||||
|
|
||||||
|
println!("Compilation process finished for {}", ll_filename);
|
||||||
|
File::create(ll_filename)
|
||||||
|
.and_then(|mut f| f.write_all(llvm_str.as_bytes()))
|
||||||
|
.expect("Error writing file");
|
||||||
|
|
||||||
|
let llc_output = Command::new("llc")
|
||||||
|
.args(&["-filetype=obj", ll_filename, "-o", obj_filename])
|
||||||
|
.output()
|
||||||
|
.expect("Failed to run llc");
|
||||||
|
|
||||||
|
|
||||||
|
if !llc_output.status.success() {
|
||||||
|
println!("{}", String::from_utf8_lossy(&llc_output.stderr));
|
||||||
|
}
|
||||||
|
|
||||||
|
let gcc_output = Command::new("gcc")
|
||||||
|
.args(&["-o", bin_filename, &obj_filename])
|
||||||
|
.output()
|
||||||
|
.expect("failed to run gcc");
|
||||||
|
|
||||||
|
if !gcc_output.status.success() {
|
||||||
|
println!("{}", String::from_utf8_lossy(&gcc_output.stdout));
|
||||||
|
println!("{}", String::from_utf8_lossy(&gcc_output.stderr));
|
||||||
|
}
|
||||||
|
|
||||||
|
for filename in [obj_filename].iter() {
|
||||||
|
Command::new("rm")
|
||||||
|
.arg(filename)
|
||||||
|
.output()
|
||||||
|
.expect(&format!("failed to run rm {}", filename));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn program_options() -> getopts::Options {
|
||||||
|
let mut options = getopts::Options::new();
|
||||||
|
options.optopt("s",
|
||||||
|
"eval-style",
|
||||||
|
"Specify whether to compile (if supported) or interpret the language. If not specified, the default is language-specific",
|
||||||
|
"[compile|interpret]"
|
||||||
|
);
|
||||||
|
options.optflag("",
|
||||||
|
"list-languages",
|
||||||
|
"Show a list of all supported languages");
|
||||||
|
options.optopt("l",
|
||||||
|
"lang",
|
||||||
|
"Start up REPL in a language",
|
||||||
|
"LANGUAGE");
|
||||||
|
options.optflag("h",
|
||||||
|
"help",
|
||||||
|
"Show help text");
|
||||||
|
options.optflag("w",
|
||||||
|
"webapp",
|
||||||
|
"Start up web interpreter");
|
||||||
|
options
|
||||||
|
}
|
|
@ -0,0 +1,279 @@
|
||||||
|
#![allow(non_snake_case)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
extern crate llvm_sys;
|
||||||
|
|
||||||
|
use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate};
|
||||||
|
use self::llvm_sys::prelude::*;
|
||||||
|
use self::llvm_sys::core;
|
||||||
|
use std::ptr;
|
||||||
|
use std::ffi::{CString, CStr};
|
||||||
|
use std::os::raw::c_char;
|
||||||
|
|
||||||
|
pub fn create_context() -> LLVMContextRef {
|
||||||
|
unsafe { core::LLVMContextCreate() }
|
||||||
|
}
|
||||||
|
pub fn module_create_with_name(name: &str) -> LLVMModuleRef {
|
||||||
|
unsafe {
|
||||||
|
let n = name.as_ptr() as *const _;
|
||||||
|
core::LLVMModuleCreateWithName(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn CreateBuilderInContext(context: LLVMContextRef) -> LLVMBuilderRef {
|
||||||
|
unsafe { core::LLVMCreateBuilderInContext(context) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn AppendBasicBlockInContext(context: LLVMContextRef,
|
||||||
|
function: LLVMValueRef,
|
||||||
|
name: &str)
|
||||||
|
-> LLVMBasicBlockRef {
|
||||||
|
let c_name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMAppendBasicBlockInContext(context, function, c_name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn AddFunction(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> LLVMValueRef {
|
||||||
|
let c_name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMAddFunction(module, c_name.as_ptr(), function_type) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn FunctionType(return_type: LLVMTypeRef,
|
||||||
|
mut param_types: Vec<LLVMTypeRef>,
|
||||||
|
is_var_rag: bool)
|
||||||
|
-> LLVMTypeRef {
|
||||||
|
let len = param_types.len();
|
||||||
|
unsafe {
|
||||||
|
let pointer = param_types.as_mut_ptr();
|
||||||
|
core::LLVMFunctionType(return_type,
|
||||||
|
pointer,
|
||||||
|
len as u32,
|
||||||
|
if is_var_rag { 1 } else { 0 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetNamedFunction(module: LLVMModuleRef,
|
||||||
|
name: &str) -> Option<LLVMValueRef> {
|
||||||
|
|
||||||
|
let c_name = CString::new(name).unwrap();
|
||||||
|
let ret = unsafe { core::LLVMGetNamedFunction(module, c_name.as_ptr()) };
|
||||||
|
|
||||||
|
if ret.is_null() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn VoidTypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
||||||
|
unsafe { core::LLVMVoidTypeInContext(context) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn DisposeBuilder(builder: LLVMBuilderRef) {
|
||||||
|
unsafe { core::LLVMDisposeBuilder(builder) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn DisposeModule(module: LLVMModuleRef) {
|
||||||
|
unsafe { core::LLVMDisposeModule(module) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ContextDispose(context: LLVMContextRef) {
|
||||||
|
unsafe { core::LLVMContextDispose(context) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn PositionBuilderAtEnd(builder: LLVMBuilderRef, basic_block: LLVMBasicBlockRef) {
|
||||||
|
unsafe { core::LLVMPositionBuilderAtEnd(builder, basic_block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildRet(builder: LLVMBuilderRef, val: LLVMValueRef) -> LLVMValueRef {
|
||||||
|
unsafe { core::LLVMBuildRet(builder, val) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildRetVoid(builder: LLVMBuilderRef) -> LLVMValueRef {
|
||||||
|
unsafe { core::LLVMBuildRetVoid(builder) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn DumpModule(module: LLVMModuleRef) {
|
||||||
|
unsafe { core::LLVMDumpModule(module) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn Int64TypeInContext(context: LLVMContextRef) -> LLVMTypeRef {
|
||||||
|
unsafe { core::LLVMInt64TypeInContext(context) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ConstInt(int_type: LLVMTypeRef, n: u64, sign_extend: bool) -> LLVMValueRef {
|
||||||
|
unsafe { core::LLVMConstInt(int_type, n, if sign_extend { 1 } else { 0 }) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildAdd(builder: LLVMBuilderRef,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
reg_name: &str)
|
||||||
|
-> LLVMValueRef {
|
||||||
|
let name = CString::new(reg_name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildAdd(builder, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildSub(builder: LLVMBuilderRef,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
reg_name: &str)
|
||||||
|
-> LLVMValueRef {
|
||||||
|
let name = CString::new(reg_name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildSub(builder, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildMul(builder: LLVMBuilderRef,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
reg_name: &str)
|
||||||
|
-> LLVMValueRef {
|
||||||
|
let name = CString::new(reg_name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildMul(builder, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildUDiv(builder: LLVMBuilderRef,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
reg_name: &str)
|
||||||
|
-> LLVMValueRef {
|
||||||
|
let name = CString::new(reg_name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildUDiv(builder, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildSRem(builder: LLVMBuilderRef,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
reg_name: &str)
|
||||||
|
-> LLVMValueRef {
|
||||||
|
let name = CString::new(reg_name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildSRem(builder, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildCondBr(builder: LLVMBuilderRef,
|
||||||
|
if_expr: LLVMValueRef,
|
||||||
|
then_expr: LLVMBasicBlockRef,
|
||||||
|
else_expr: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||||
|
|
||||||
|
|
||||||
|
unsafe { core::LLVMBuildCondBr(builder, if_expr, then_expr, else_expr) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildBr(builder: LLVMBuilderRef,
|
||||||
|
dest: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||||
|
unsafe { core::LLVMBuildBr(builder, dest) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetInsertBlock(builder: LLVMBuilderRef) -> LLVMBasicBlockRef {
|
||||||
|
unsafe { core::LLVMGetInsertBlock(builder) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildPhi(builder: LLVMBuilderRef, ty: LLVMTypeRef, name: &str) -> LLVMValueRef {
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildPhi(builder, ty, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn SetValueName(value: LLVMValueRef, name: &str) {
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe {
|
||||||
|
core::LLVMSetValueName(value, name.as_ptr())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetValueName(value: LLVMValueRef) -> String {
|
||||||
|
unsafe {
|
||||||
|
let name_ptr: *const c_char = core::LLVMGetValueName(value);
|
||||||
|
CStr::from_ptr(name_ptr).to_string_lossy().into_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetParams(function: LLVMValueRef) -> Vec<LLVMValueRef> {
|
||||||
|
let size = CountParams(function);
|
||||||
|
unsafe {
|
||||||
|
let mut container = Vec::with_capacity(size);
|
||||||
|
container.set_len(size);
|
||||||
|
core::LLVMGetParams(function, container.as_mut_ptr());
|
||||||
|
container
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn CountParams(function: LLVMValueRef) -> usize {
|
||||||
|
unsafe { core::LLVMCountParams(function) as usize }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildFCmp(builder: LLVMBuilderRef,
|
||||||
|
op: LLVMRealPredicate,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
name: &str) -> LLVMValueRef {
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildFCmp(builder, op, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildZExt(builder: LLVMBuilderRef,
|
||||||
|
val: LLVMValueRef,
|
||||||
|
dest_type: LLVMTypeRef,
|
||||||
|
name: &str) -> LLVMValueRef {
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildZExt(builder, val, dest_type, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildUIToFP(builder: LLVMBuilderRef,
|
||||||
|
val: LLVMValueRef,
|
||||||
|
dest_type: LLVMTypeRef,
|
||||||
|
name: &str) -> LLVMValueRef {
|
||||||
|
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildUIToFP(builder, val, dest_type, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn BuildICmp(builder: LLVMBuilderRef,
|
||||||
|
op: LLVMIntPredicate,
|
||||||
|
lhs: LLVMValueRef,
|
||||||
|
rhs: LLVMValueRef,
|
||||||
|
name: &str) -> LLVMValueRef {
|
||||||
|
let name = CString::new(name).unwrap();
|
||||||
|
unsafe { core::LLVMBuildICmp(builder, op, lhs, rhs, name.as_ptr()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetBasicBlockParent(block: LLVMBasicBlockRef) -> LLVMValueRef {
|
||||||
|
unsafe { core::LLVMGetBasicBlockParent(block) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetBasicBlocks(function: LLVMValueRef) -> Vec<LLVMBasicBlockRef> {
|
||||||
|
let size = CountBasicBlocks(function);
|
||||||
|
unsafe {
|
||||||
|
let mut container = Vec::with_capacity(size);
|
||||||
|
container.set_len(size);
|
||||||
|
core::LLVMGetBasicBlocks(function, container.as_mut_ptr());
|
||||||
|
container
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn CountBasicBlocks(function: LLVMValueRef) -> usize {
|
||||||
|
unsafe { core::LLVMCountBasicBlocks(function) as usize }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn PrintModuleToString(module: LLVMModuleRef) -> String {
|
||||||
|
unsafe {
|
||||||
|
let str_ptr: *const c_char = core::LLVMPrintModuleToString(module);
|
||||||
|
CStr::from_ptr(str_ptr).to_string_lossy().into_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn AddIncoming(phi_node: LLVMValueRef, mut incoming_values: Vec<LLVMValueRef>,
|
||||||
|
mut incoming_blocks: Vec<LLVMBasicBlockRef>) {
|
||||||
|
|
||||||
|
let count = incoming_blocks.len() as u32;
|
||||||
|
if incoming_values.len() as u32 != count {
|
||||||
|
panic!("Bad invocation of AddIncoming");
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let vals = incoming_values.as_mut_ptr();
|
||||||
|
let blocks = incoming_blocks.as_mut_ptr();
|
||||||
|
core::LLVMAddIncoming(phi_node, vals, blocks, count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn PrintModuleToFile(module: LLVMModuleRef, filename: &str) -> LLVMBool {
|
||||||
|
let out_file = CString::new(filename).unwrap();
|
||||||
|
unsafe { core::LLVMPrintModuleToFile(module, out_file.as_ptr(), ptr::null_mut()) }
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
use rocket;
|
||||||
|
use rocket::State;
|
||||||
|
use rocket::response::Content;
|
||||||
|
use rocket::response::NamedFile;
|
||||||
|
use rocket::http::ContentType;
|
||||||
|
use rocket_contrib::Json;
|
||||||
|
use language::{ProgrammingLanguageInterface, EvalOptions};
|
||||||
|
use WEBFILES;
|
||||||
|
use ::PLIGenerator;
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
fn index() -> Content<String> {
|
||||||
|
let path = "static/index.html";
|
||||||
|
let html_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
|
||||||
|
Content(ContentType::HTML, html_contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/bundle.js")]
|
||||||
|
fn js_bundle() -> Content<String> {
|
||||||
|
let path = "static/bundle.js";
|
||||||
|
let js_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
|
||||||
|
Content(ContentType::JavaScript, js_contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Input {
|
||||||
|
source: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Output {
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/input", format = "application/json", data = "<input>")]
|
||||||
|
fn interpreter_input(input: Json<Input>, generators: State<Vec<PLIGenerator>>) -> Json<Output> {
|
||||||
|
let schala_gen = generators.get(0).unwrap();
|
||||||
|
let mut schala: Box<ProgrammingLanguageInterface> = schala_gen();
|
||||||
|
let code_output = schala.evaluate_in_repl(&input.source, &EvalOptions::default());
|
||||||
|
Json(Output { text: code_output.to_string() })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn web_main(language_generators: Vec<PLIGenerator>) {
|
||||||
|
rocket::ignite().manage(language_generators).mount("/", routes![index, js_bundle, interpreter_input]).launch();
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
|
||||||
|
fn outer() {
|
||||||
|
fn inner(a) {
|
||||||
|
a + 10
|
||||||
|
}
|
||||||
|
|
||||||
|
inner(20) + 8.3
|
||||||
|
}
|
||||||
|
|
||||||
|
outer()
|
|
@ -0,0 +1,21 @@
|
||||||
|
|
||||||
|
fn hella(a, b) {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paha(x, y, z) {
|
||||||
|
x * y * z
|
||||||
|
}
|
||||||
|
|
||||||
|
a = 1
|
||||||
|
|
||||||
|
c = if a {
|
||||||
|
10
|
||||||
|
} else {
|
||||||
|
20
|
||||||
|
}
|
||||||
|
|
||||||
|
q = 4
|
||||||
|
q = q + 2
|
||||||
|
q + 1 + c
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
if 20 {
|
||||||
|
a = 20
|
||||||
|
b = 30
|
||||||
|
c = 40
|
||||||
|
a + b + c
|
||||||
|
} else {
|
||||||
|
Null
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
(fn(q) { q * 2 }(25))
|
||||||
|
|
||||||
|
a = fn(x) { x + 5 }
|
||||||
|
a(2)
|
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
fn add(a, b) {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn subtract(a, b) {
|
||||||
|
a - b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
first_value = add(20, 20)
|
||||||
|
second_value = subtract(700, 650)
|
||||||
|
first_value + second_value
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
|
||||||
|
fn hella(x) {
|
||||||
|
print("hey")
|
||||||
|
if x == 3 {
|
||||||
|
Null
|
||||||
|
} else {
|
||||||
|
hella(x + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hella(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fn fib(x) {
|
||||||
|
if x < 3 {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
fib(x - 1) + fib(x - 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fib(10)
|
|
@ -0,0 +1,10 @@
|
||||||
|
fn main() {
|
||||||
|
const a = 10
|
||||||
|
const b = 20
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
print(main())
|
||||||
|
|
||||||
|
const xxx
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
# comments are scripting-style
|
||||||
|
#{ but can also be
|
||||||
|
|
||||||
|
}# blocks
|
||||||
|
|
||||||
|
@annotations are with @-
|
||||||
|
|
||||||
|
# variable expressions
|
||||||
|
var a: I32 = 20
|
||||||
|
const b: String = 20
|
||||||
|
|
||||||
|
there(); can(); be(); multiple(); statements(); per_line();
|
||||||
|
|
||||||
|
#string interpolation
|
||||||
|
const yolo = "I have ${a + b} people in my house"
|
||||||
|
|
||||||
|
# let expressions ??? not sure if I want this
|
||||||
|
let a = 10, b = 20, c = 30 in a + b + c
|
||||||
|
|
||||||
|
#list literal
|
||||||
|
const q = [1,2,3,4]
|
||||||
|
|
||||||
|
#lambda literal ?? maybe? not sure how this should work
|
||||||
|
q.map(|item| { item * 100 })
|
||||||
|
|
||||||
|
fn yolo(a: MyType, b: YourType): ReturnType<Param1, Param2> {
|
||||||
|
if a == 20 {
|
||||||
|
return "early"
|
||||||
|
}
|
||||||
|
var sex = 20
|
||||||
|
sex
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for {
|
||||||
|
# infinite loop
|
||||||
|
}
|
||||||
|
|
||||||
|
#iteration over a variable
|
||||||
|
for i <- [1..1000] {
|
||||||
|
|
||||||
|
} #return type is return type of block
|
||||||
|
|
||||||
|
#while loop
|
||||||
|
for a != 3 || fuckTard() {
|
||||||
|
break
|
||||||
|
} #return type is return type of block
|
||||||
|
|
||||||
|
#monadic decomposition
|
||||||
|
for {
|
||||||
|
a <- maybeInt();
|
||||||
|
s <- foo()
|
||||||
|
} return {
|
||||||
|
a + s
|
||||||
|
} #return type is Monad<return type of block>
|
||||||
|
|
||||||
|
# let statements too!!
|
||||||
|
for (a = 20
|
||||||
|
b = fuck) {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# pattern-matching
|
||||||
|
match <expr> {
|
||||||
|
Some(a) => {
|
||||||
|
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#syntax is, I guess, for <expr> <brace-block>, where <expr> is a bool, or a <arrow-expr>
|
||||||
|
|
||||||
|
# type level alises
|
||||||
|
typealias <name> = <other type> #maybe thsi should be 'alias'?
|
||||||
|
|
||||||
|
#what if type A = B meant that you could had to create A's with A(B), but when you used A's the interface was exactly like B's?
|
||||||
|
# maybe introduce a 'newtype' keyword for this
|
||||||
|
|
||||||
|
#declaring types of all stripes
|
||||||
|
type MyData = { a: i32, b: String }
|
||||||
|
type MyType = MyType
|
||||||
|
type Option<a> = None | Some(a)
|
||||||
|
type Signal = Absence | SimplePresence(i32) | ComplexPresence {a: i32, b: MyCustomData}
|
||||||
|
|
||||||
|
#traits
|
||||||
|
|
||||||
|
trait Bashable { }
|
||||||
|
trait Luggable {
|
||||||
|
fn lug(self, a: Option<Self>)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# lambdas
|
||||||
|
#
|
||||||
|
|x,y| { }() #is probably fine
|
||||||
|
const a = |x: Type, y|: RetType { <statementblock> }
|
||||||
|
const a: X -> Y -> Z = |x,y| { }
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
fn a(x) {
|
||||||
|
x + 20
|
||||||
|
}
|
||||||
|
|
||||||
|
fn x(x) {
|
||||||
|
x + a(9384)
|
||||||
|
}
|
||||||
|
|
||||||
|
a(0)
|
||||||
|
x(1)
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
(display (+ 1 2))
|
||||||
|
(display "Hello")
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
fn めんどくさい(a) {
|
||||||
|
a + 20
|
||||||
|
}
|
||||||
|
|
||||||
|
print(めんどくさい(394))
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
a = 0
|
||||||
|
while a < 100000
|
||||||
|
print("hello", a)
|
||||||
|
a = a + 1
|
||||||
|
end
|
|
@ -0,0 +1,279 @@
|
||||||
|
extern crate llvm_sys;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use self::llvm_sys::prelude::*;
|
||||||
|
use self::llvm_sys::{LLVMIntPredicate};
|
||||||
|
|
||||||
|
use maaru_lang::parser::{AST, Statement, Function, Prototype, Expression, BinOp};
|
||||||
|
use schala_lib::LLVMCodeString;
|
||||||
|
|
||||||
|
use schala_lib::llvm_wrap as LLVMWrap;
|
||||||
|
|
||||||
|
type VariableMap = HashMap<String, LLVMValueRef>;
|
||||||
|
|
||||||
|
struct CompilationData {
|
||||||
|
context: LLVMContextRef,
|
||||||
|
module: LLVMModuleRef,
|
||||||
|
builder: LLVMBuilderRef,
|
||||||
|
variables: VariableMap,
|
||||||
|
main_function: LLVMValueRef,
|
||||||
|
current_function: Option<LLVMValueRef>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compile_ast(ast: AST) -> LLVMCodeString {
|
||||||
|
println!("Compiling!");
|
||||||
|
let names: VariableMap = HashMap::new();
|
||||||
|
|
||||||
|
let context = LLVMWrap::create_context();
|
||||||
|
let module = LLVMWrap::module_create_with_name("example module");
|
||||||
|
let builder = LLVMWrap::CreateBuilderInContext(context);
|
||||||
|
|
||||||
|
let program_return_type = LLVMWrap::Int64TypeInContext(context);
|
||||||
|
let main_function_type = LLVMWrap::FunctionType(program_return_type, Vec::new(), false);
|
||||||
|
let main_function: LLVMValueRef = LLVMWrap::AddFunction(module, "main", main_function_type);
|
||||||
|
|
||||||
|
let mut data = CompilationData {
|
||||||
|
context: context,
|
||||||
|
builder: builder,
|
||||||
|
module: module,
|
||||||
|
variables: names,
|
||||||
|
main_function: main_function,
|
||||||
|
current_function: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let bb = LLVMWrap::AppendBasicBlockInContext(data.context, data.main_function, "entry");
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(builder, bb);
|
||||||
|
|
||||||
|
let value = ast.codegen(&mut data);
|
||||||
|
|
||||||
|
LLVMWrap::BuildRet(builder, value);
|
||||||
|
|
||||||
|
let ret = LLVMWrap::PrintModuleToString(module);
|
||||||
|
|
||||||
|
// Clean up. Values created in the context mostly get cleaned up there.
|
||||||
|
LLVMWrap::DisposeBuilder(builder);
|
||||||
|
LLVMWrap::DisposeModule(module);
|
||||||
|
LLVMWrap::ContextDispose(context);
|
||||||
|
LLVMCodeString(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
trait CodeGen {
|
||||||
|
fn codegen(&self, &mut CompilationData) -> LLVMValueRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodeGen for AST {
|
||||||
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
|
|
||||||
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
let mut ret = LLVMWrap::ConstInt(int_type, 0, false);
|
||||||
|
|
||||||
|
for statement in self {
|
||||||
|
ret = statement.codegen(data);
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodeGen for Statement {
|
||||||
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
|
use self::Statement::*;
|
||||||
|
match self {
|
||||||
|
&ExprNode(ref expr) => expr.codegen(data),
|
||||||
|
&FuncDefNode(ref func) => func.codegen(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodeGen for Function {
|
||||||
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
|
|
||||||
|
/* should have a check here for function already being defined */
|
||||||
|
let function = self.prototype.codegen(data);
|
||||||
|
let ref body = self.body;
|
||||||
|
|
||||||
|
data.current_function = Some(function);
|
||||||
|
|
||||||
|
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
let mut ret = LLVMWrap::ConstInt(return_type, 0, false);
|
||||||
|
|
||||||
|
let block = LLVMWrap::AppendBasicBlockInContext(data.context, function, "entry");
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(data.builder, block);
|
||||||
|
|
||||||
|
//insert function params into variables
|
||||||
|
for value in LLVMWrap::GetParams(function) {
|
||||||
|
let name = LLVMWrap::GetValueName(value);
|
||||||
|
data.variables.insert(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for expr in body {
|
||||||
|
ret = expr.codegen(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMWrap::BuildRet(data.builder, ret);
|
||||||
|
|
||||||
|
// get basic block of main
|
||||||
|
let main_bb = LLVMWrap::GetBasicBlocks(data.main_function).get(0).expect("Couldn't get first block of main").clone();
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(data.builder, main_bb);
|
||||||
|
|
||||||
|
data.current_function = None;
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodeGen for Prototype {
|
||||||
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
|
let num_args = self.parameters.len();
|
||||||
|
let return_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
let mut arguments: Vec<LLVMTypeRef> = vec![];
|
||||||
|
|
||||||
|
for _ in 0..num_args {
|
||||||
|
arguments.push(LLVMWrap::Int64TypeInContext(data.context));
|
||||||
|
}
|
||||||
|
|
||||||
|
let function_type =
|
||||||
|
LLVMWrap::FunctionType(return_type,
|
||||||
|
arguments,
|
||||||
|
false);
|
||||||
|
|
||||||
|
let function = LLVMWrap::AddFunction(data.module,
|
||||||
|
&*self.name,
|
||||||
|
function_type);
|
||||||
|
|
||||||
|
let function_params = LLVMWrap::GetParams(function);
|
||||||
|
for (index, param) in function_params.iter().enumerate() {
|
||||||
|
let name = self.parameters.get(index).expect(&format!("Failed this check at index {}", index));
|
||||||
|
let new = *param;
|
||||||
|
|
||||||
|
LLVMWrap::SetValueName(new, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodeGen for Expression {
|
||||||
|
fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef {
|
||||||
|
use self::BinOp::*;
|
||||||
|
use self::Expression::*;
|
||||||
|
|
||||||
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
let zero = LLVMWrap::ConstInt(int_type, 0, false);
|
||||||
|
|
||||||
|
match *self {
|
||||||
|
Variable(ref name) => *data.variables.get(&**name).expect(&format!("Can't find variable {}", name)),
|
||||||
|
BinExp(Assign, ref left, ref right) => {
|
||||||
|
if let Variable(ref name) = **left {
|
||||||
|
let new_value = right.codegen(data);
|
||||||
|
data.variables.insert((**name).clone(), new_value);
|
||||||
|
new_value
|
||||||
|
} else {
|
||||||
|
panic!("Bad variable assignment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BinExp(ref op, ref left, ref right) => {
|
||||||
|
let lhs = left.codegen(data);
|
||||||
|
let rhs = right.codegen(data);
|
||||||
|
op.codegen_with_ops(data, lhs, rhs)
|
||||||
|
}
|
||||||
|
Number(ref n) => {
|
||||||
|
let native_val = *n as u64;
|
||||||
|
let int_value: LLVMValueRef = LLVMWrap::ConstInt(int_type, native_val, false);
|
||||||
|
int_value
|
||||||
|
}
|
||||||
|
Conditional(ref test, ref then_expr, ref else_expr) => {
|
||||||
|
let condition_value = test.codegen(data);
|
||||||
|
let is_nonzero =
|
||||||
|
LLVMWrap::BuildICmp(data.builder,
|
||||||
|
LLVMIntPredicate::LLVMIntNE,
|
||||||
|
condition_value,
|
||||||
|
zero,
|
||||||
|
"ifcond");
|
||||||
|
|
||||||
|
let func = LLVMWrap::GetBasicBlockParent(LLVMWrap::GetInsertBlock(data.builder));
|
||||||
|
|
||||||
|
let mut then_block =
|
||||||
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "then_block");
|
||||||
|
let mut else_block =
|
||||||
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "else_block");
|
||||||
|
let merge_block =
|
||||||
|
LLVMWrap::AppendBasicBlockInContext(data.context, func, "ifcont");
|
||||||
|
|
||||||
|
// add conditional branch to ifcond block
|
||||||
|
LLVMWrap::BuildCondBr(data.builder, is_nonzero, then_block, else_block);
|
||||||
|
|
||||||
|
// start inserting into then block
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(data.builder, then_block);
|
||||||
|
|
||||||
|
// then-block codegen
|
||||||
|
let then_return = then_expr.codegen(data);
|
||||||
|
LLVMWrap::BuildBr(data.builder, merge_block);
|
||||||
|
|
||||||
|
// update then block b/c recursive codegen() call may have changed the notion of
|
||||||
|
// the current block
|
||||||
|
then_block = LLVMWrap::GetInsertBlock(data.builder);
|
||||||
|
|
||||||
|
// then do the same stuff again for the else branch
|
||||||
|
//
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(data.builder, else_block);
|
||||||
|
let else_return = match *else_expr {
|
||||||
|
Some(ref e) => e.codegen(data),
|
||||||
|
None => zero,
|
||||||
|
};
|
||||||
|
LLVMWrap::BuildBr(data.builder, merge_block);
|
||||||
|
else_block = LLVMWrap::GetInsertBlock(data.builder);
|
||||||
|
|
||||||
|
LLVMWrap::PositionBuilderAtEnd(data.builder, merge_block);
|
||||||
|
|
||||||
|
let phi = LLVMWrap::BuildPhi(data.builder, int_type, "phinode");
|
||||||
|
let values = vec![then_return, else_return];
|
||||||
|
let blocks = vec![then_block, else_block];
|
||||||
|
LLVMWrap::AddIncoming(phi, values, blocks);
|
||||||
|
phi
|
||||||
|
}
|
||||||
|
Block(ref exprs) => {
|
||||||
|
let mut ret = zero;
|
||||||
|
for e in exprs.iter() {
|
||||||
|
ret = e.codegen(data);
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
ref e => {
|
||||||
|
println!("Unimplemented {:?}", e);
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BinOp {
|
||||||
|
fn codegen_with_ops(&self, data: &CompilationData, lhs: LLVMValueRef, rhs: LLVMValueRef) -> LLVMValueRef {
|
||||||
|
use self::BinOp::*;
|
||||||
|
macro_rules! simple_binop {
|
||||||
|
($fnname: expr, $name: expr) => {
|
||||||
|
$fnname(data.builder, lhs, rhs, $name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let int_type = LLVMWrap::Int64TypeInContext(data.context);
|
||||||
|
match *self {
|
||||||
|
Add => simple_binop!(LLVMWrap::BuildAdd, "addtemp"),
|
||||||
|
Sub => simple_binop!(LLVMWrap::BuildSub, "subtemp"),
|
||||||
|
Mul => simple_binop!(LLVMWrap::BuildMul, "multemp"),
|
||||||
|
Div => simple_binop!(LLVMWrap::BuildUDiv, "divtemp"),
|
||||||
|
Mod => simple_binop!(LLVMWrap::BuildSRem, "remtemp"),
|
||||||
|
Less => {
|
||||||
|
let pred: LLVMValueRef =
|
||||||
|
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntULT, lhs, rhs, "tmp");
|
||||||
|
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
||||||
|
}
|
||||||
|
Greater => {
|
||||||
|
let pred: LLVMValueRef =
|
||||||
|
LLVMWrap::BuildICmp(data.builder, LLVMIntPredicate::LLVMIntUGT, lhs, rhs, "tmp");
|
||||||
|
LLVMWrap::BuildZExt(data.builder, pred, int_type, "temp")
|
||||||
|
}
|
||||||
|
ref unknown => panic!("Bad operator {:?}", unknown),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,481 @@
|
||||||
|
extern crate take_mut;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use maaru_lang::parser::{AST, Statement, Expression, Function, Callable, BinOp};
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::io::{Write, Stdout, BufWriter};
|
||||||
|
use std::convert::From;
|
||||||
|
|
||||||
|
use maaru_lang::parser::Expression::*;
|
||||||
|
use maaru_lang::parser::Statement::*;
|
||||||
|
|
||||||
|
type Reduction<T> = (T, Option<SideEffect>);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
enum ReducedValue {
|
||||||
|
StringLiteral(Rc<String>),
|
||||||
|
ListLiteral(VecDeque<Expression>),
|
||||||
|
StructLiteral(VecDeque<(Rc<String>, Expression)>),
|
||||||
|
Number(f64),
|
||||||
|
Lambda(Function),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ReducedValue> for Expression {
|
||||||
|
fn from(rv: ReducedValue) -> Expression {
|
||||||
|
match rv {
|
||||||
|
ReducedValue::Number(n) => Expression::Number(n),
|
||||||
|
ReducedValue::StringLiteral(n) => Expression::StringLiteral(n),
|
||||||
|
ReducedValue::Lambda(f) => Expression::Lambda(f),
|
||||||
|
ReducedValue::ListLiteral(items) => Expression::ListLiteral(items),
|
||||||
|
ReducedValue::StructLiteral(items) => Expression::StructLiteral(items),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Expression> for ReducedValue {
|
||||||
|
fn from(rv: Expression) -> ReducedValue {
|
||||||
|
match rv {
|
||||||
|
Expression::Number(n) => ReducedValue::Number(n),
|
||||||
|
Expression::StringLiteral(n) => ReducedValue::StringLiteral(n),
|
||||||
|
Expression::Lambda(f) => ReducedValue::Lambda(f),
|
||||||
|
Expression::ListLiteral(items) => ReducedValue::ListLiteral(items),
|
||||||
|
Expression::StructLiteral(items) => ReducedValue::StructLiteral(items),
|
||||||
|
_ => panic!("trying to store a non-fully-reduced variable"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_indexer(f: f64) -> Option<usize> {
|
||||||
|
if f.fract() == 0.0 {
|
||||||
|
if f.trunc() >= 0.0 {
|
||||||
|
return Some(f.trunc() as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum SideEffect {
|
||||||
|
Print(String),
|
||||||
|
AddBinding(Rc<String>, ReducedValue),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Evaluator<'a> {
|
||||||
|
parent: Option<&'a Evaluator<'a>>,
|
||||||
|
variables: HashMap<String, ReducedValue>,
|
||||||
|
stdout: BufWriter<Stdout>,
|
||||||
|
pub trace_evaluation: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Evaluator<'a> {
|
||||||
|
pub fn new(parent: Option<&'a Evaluator>) -> Evaluator<'a> {
|
||||||
|
Evaluator {
|
||||||
|
variables: HashMap::new(),
|
||||||
|
parent: parent,
|
||||||
|
stdout: BufWriter::new(::std::io::stdout()),
|
||||||
|
trace_evaluation: parent.map_or(false, |e| e.trace_evaluation),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
||||||
|
ast.into_iter()
|
||||||
|
.map(|astnode| format!("{}", self.reduction_loop(astnode)))
|
||||||
|
.collect()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_binding(&mut self, var: String, value: ReducedValue) {
|
||||||
|
self.variables.insert(var, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup_binding(&self, var: &str) -> Option<ReducedValue> {
|
||||||
|
match self.variables.get(var) {
|
||||||
|
Some(expr) => Some(expr.clone()),
|
||||||
|
None => match self.parent {
|
||||||
|
Some(env) => env.lookup_binding(var),
|
||||||
|
None => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Evaluable {
|
||||||
|
fn is_reducible(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Evaluable for Statement {
|
||||||
|
fn is_reducible(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
&ExprNode(ref expr) => expr.is_reducible(),
|
||||||
|
&FuncDefNode(_) => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Evaluable for Expression {
|
||||||
|
fn is_reducible(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
Null => false,
|
||||||
|
StringLiteral(_) => false,
|
||||||
|
Lambda(_) => false,
|
||||||
|
Number(_) => false,
|
||||||
|
ListLiteral(ref items) => {
|
||||||
|
items.iter().any(|x| x.is_reducible())
|
||||||
|
}
|
||||||
|
StructLiteral(ref items) => {
|
||||||
|
items.iter().any(|pair| pair.1.is_reducible())
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Expression {
|
||||||
|
fn is_truthy(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
Null => false,
|
||||||
|
StringLiteral(ref s) if **s == "" => false,
|
||||||
|
Number(n) if n == 0.0 => false,
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_assignment(op: &BinOp) -> bool {
|
||||||
|
use self::BinOp::*;
|
||||||
|
match *op {
|
||||||
|
Assign | AddAssign | SubAssign |
|
||||||
|
MulAssign | DivAssign => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Evaluator<'a> {
|
||||||
|
fn reduction_loop(&mut self, mut node: Statement) -> Statement {
|
||||||
|
loop {
|
||||||
|
node = self.step(node);
|
||||||
|
if !node.is_reducible() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node
|
||||||
|
}
|
||||||
|
|
||||||
|
fn step(&mut self, node: Statement) -> Statement {
|
||||||
|
let mut trace = String::new();
|
||||||
|
if self.trace_evaluation {
|
||||||
|
trace.push_str(&format!("Step: {:?}", node));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (new_node, side_effect) = self.reduce_astnode(node);
|
||||||
|
|
||||||
|
if self.trace_evaluation {
|
||||||
|
trace.push_str(&format!(" ➜ {:?}", new_node));
|
||||||
|
}
|
||||||
|
if let Some(s) = side_effect {
|
||||||
|
if self.trace_evaluation {
|
||||||
|
trace.push_str(&format!(" | side-effect: {:?}", s));
|
||||||
|
}
|
||||||
|
self.perform_side_effect(s);
|
||||||
|
}
|
||||||
|
if self.trace_evaluation {
|
||||||
|
println!("{}", trace);
|
||||||
|
}
|
||||||
|
new_node
|
||||||
|
}
|
||||||
|
|
||||||
|
fn perform_side_effect(&mut self, side_effect: SideEffect) {
|
||||||
|
use self::SideEffect::*;
|
||||||
|
match side_effect {
|
||||||
|
Print(s) => {
|
||||||
|
write!(self.stdout, "{}\n", s).unwrap();
|
||||||
|
match self.stdout.flush() {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => println!("Could not flush stdout"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
AddBinding(var, value) => {
|
||||||
|
self.add_binding((*var).clone(), value);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce_astnode(&mut self, node: Statement) -> Reduction<Statement> {
|
||||||
|
match node {
|
||||||
|
ExprNode(expr) => {
|
||||||
|
if expr.is_reducible() {
|
||||||
|
let (new_expr, side_effect) = self.reduce_expr(expr);
|
||||||
|
(ExprNode(new_expr), side_effect)
|
||||||
|
} else {
|
||||||
|
(ExprNode(expr), None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FuncDefNode(func) => {
|
||||||
|
let name = func.prototype.name.clone();
|
||||||
|
let reduced_value = ReducedValue::Lambda(func.clone());
|
||||||
|
let binding = Some(SideEffect::AddBinding(name, reduced_value));
|
||||||
|
(ExprNode(Expression::Lambda(func)), binding)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO I probably want another Expression variant that holds a ReducedValue
|
||||||
|
fn reduce_expr(&mut self, expression: Expression) -> Reduction<Expression> {
|
||||||
|
match expression {
|
||||||
|
Null => (Null, None),
|
||||||
|
e @ StringLiteral(_) => (e, None),
|
||||||
|
e @ Number(_) => (e, None),
|
||||||
|
e @ Lambda(_) => (e, None),
|
||||||
|
Variable(ref var) => {
|
||||||
|
match self.lookup_binding(var).map(|x| x.into()) {
|
||||||
|
None => (Null, None),
|
||||||
|
Some(expr) => (expr, None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BinExp(op, mut left, mut right) => {
|
||||||
|
if right.is_reducible() {
|
||||||
|
let mut side_effect = None;
|
||||||
|
take_mut::take(right.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
||||||
|
return (BinExp(op, left, right), side_effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let BinOp::Assign = op {
|
||||||
|
return match *left {
|
||||||
|
Variable(var) => {
|
||||||
|
let reduced_value: ReducedValue = ReducedValue::from(*right);
|
||||||
|
let binding = SideEffect::AddBinding(var, reduced_value);
|
||||||
|
(Null, Some(binding))
|
||||||
|
},
|
||||||
|
_ => (Null, None)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_assignment(&op) {
|
||||||
|
use self::BinOp::*;
|
||||||
|
let new_op = match op {
|
||||||
|
AddAssign => Add,
|
||||||
|
SubAssign => Sub,
|
||||||
|
MulAssign => Mul,
|
||||||
|
DivAssign => Div,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let reduction =
|
||||||
|
BinExp(BinOp::Assign,
|
||||||
|
Box::new(*left.clone()),
|
||||||
|
Box::new(BinExp(new_op, left, right))
|
||||||
|
);
|
||||||
|
|
||||||
|
return (reduction, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
if left.is_reducible() {
|
||||||
|
let mut side_effect = None;
|
||||||
|
take_mut::take(left.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
||||||
|
(BinExp(op, left, right), side_effect)
|
||||||
|
} else {
|
||||||
|
(self.reduce_binop(op, *left, *right), None) //can assume both arguments are maximally reduced
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Call(callable, mut args) => {
|
||||||
|
let mut f = true;
|
||||||
|
for arg in args.iter_mut() {
|
||||||
|
if arg.is_reducible() {
|
||||||
|
take_mut::take(arg, |arg| self.reduce_expr(arg).0);
|
||||||
|
f = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if f {
|
||||||
|
self.reduce_call(callable, args)
|
||||||
|
} else {
|
||||||
|
(Call(callable, args), None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
While(test, body) => {
|
||||||
|
let mut block = VecDeque::from(body.clone());
|
||||||
|
block.push_back(While(test.clone(), body.clone()));
|
||||||
|
let reduction = Conditional(test, Box::new(Block(block)), None);
|
||||||
|
(reduction, None)
|
||||||
|
}
|
||||||
|
Conditional(box test, then_block, else_block) => {
|
||||||
|
if test.is_reducible() {
|
||||||
|
let (new_test, new_effect) = self.reduce_expr(test);
|
||||||
|
(Conditional(Box::new(new_test), then_block, else_block), new_effect)
|
||||||
|
} else {
|
||||||
|
if test.is_truthy() {
|
||||||
|
(*then_block, None)
|
||||||
|
} else {
|
||||||
|
match else_block {
|
||||||
|
Some(box expr) => (expr, None),
|
||||||
|
None => (Null, None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Block(mut exprs) => {
|
||||||
|
let first = exprs.pop_front();
|
||||||
|
match first {
|
||||||
|
None => (Null, None),
|
||||||
|
Some(expr) => {
|
||||||
|
if exprs.len() == 0 {
|
||||||
|
(expr, None)
|
||||||
|
} else {
|
||||||
|
if expr.is_reducible() {
|
||||||
|
let (new, side_effect) = self.reduce_expr(expr);
|
||||||
|
exprs.push_front(new);
|
||||||
|
(Block(exprs), side_effect)
|
||||||
|
} else {
|
||||||
|
(Block(exprs), None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Index(mut expr, mut index_expr) => {
|
||||||
|
if index_expr.is_reducible() {
|
||||||
|
let mut side_effect = None;
|
||||||
|
take_mut::take(index_expr.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
||||||
|
return (Index(expr, index_expr), side_effect)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expr.is_reducible() {
|
||||||
|
let mut side_effect = None;
|
||||||
|
take_mut::take(expr.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
||||||
|
return (Index(expr, index_expr), side_effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
match (*expr, *index_expr) {
|
||||||
|
(ListLiteral(list_items), Number(n)) => {
|
||||||
|
let indexed_expr = get_indexer(n).and_then(|i| list_items.get(i));
|
||||||
|
if let Some(e) = indexed_expr {
|
||||||
|
(e.clone(), None)
|
||||||
|
} else {
|
||||||
|
(Null, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(StructLiteral(items), StringLiteral(s)) => {
|
||||||
|
for item in items {
|
||||||
|
if s == item.0 {
|
||||||
|
return (item.1.clone(), None); //TODO this is hella inefficient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Null, None)
|
||||||
|
},
|
||||||
|
_ => (Null, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ListLiteral(mut exprs) => {
|
||||||
|
let mut side_effect = None;
|
||||||
|
for expr in exprs.iter_mut() {
|
||||||
|
if expr.is_reducible() {
|
||||||
|
take_mut::take(expr, |expr| {
|
||||||
|
let (a, b) = self.reduce_expr(expr);
|
||||||
|
side_effect = b;
|
||||||
|
a
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(ListLiteral(exprs), side_effect)
|
||||||
|
},
|
||||||
|
|
||||||
|
StructLiteral(mut items) => {
|
||||||
|
let mut side_effect = None;
|
||||||
|
for pair in items.iter_mut() {
|
||||||
|
if pair.1.is_reducible() {
|
||||||
|
take_mut::take(pair, |pair| {
|
||||||
|
let (name, expr) = pair;
|
||||||
|
let (a, b) = self.reduce_expr(expr);
|
||||||
|
side_effect = b;
|
||||||
|
(name, a)
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(StructLiteral(items), side_effect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce_binop(&mut self, op: BinOp, left: Expression, right: Expression) -> Expression {
|
||||||
|
use self::BinOp::*;
|
||||||
|
let truthy = Number(1.0);
|
||||||
|
let falsy = Null;
|
||||||
|
match (op, left, right) {
|
||||||
|
(Add, Number(l), Number(r)) => Number(l + r),
|
||||||
|
(Add, StringLiteral(s1), StringLiteral(s2)) => StringLiteral(Rc::new(format!("{}{}", *s1, *s2))),
|
||||||
|
(Add, StringLiteral(s1), Number(r)) => StringLiteral(Rc::new(format!("{}{}", *s1, r))),
|
||||||
|
(Add, Number(l), StringLiteral(s1)) => StringLiteral(Rc::new(format!("{}{}", l, *s1))),
|
||||||
|
(Sub, Number(l), Number(r)) => Number(l - r),
|
||||||
|
(Mul, Number(l), Number(r)) => Number(l * r),
|
||||||
|
(Div, Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
||||||
|
(Mod, Number(l), Number(r)) => Number(l % r),
|
||||||
|
(Less, Number(l), Number(r)) => if l < r { truthy } else { falsy },
|
||||||
|
(LessEq, Number(l), Number(r)) => if l <= r { truthy } else { falsy },
|
||||||
|
(Greater, Number(l), Number(r)) => if l > r { truthy } else { falsy },
|
||||||
|
(GreaterEq, Number(l), Number(r)) => if l >= r { truthy } else { falsy },
|
||||||
|
(Equal, Number(l), Number(r)) => if l == r { truthy } else { falsy },
|
||||||
|
(Equal, Null, Null) => truthy,
|
||||||
|
(Equal, StringLiteral(s1), StringLiteral(s2)) => if s1 == s2 { truthy } else { falsy },
|
||||||
|
(Equal, _, _) => falsy,
|
||||||
|
_ => falsy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reduce_call(&mut self, callable: Callable, arguments: Vec<Expression>) -> Reduction<Expression> {
|
||||||
|
if let Some(res) = handle_builtin(&callable, &arguments) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
let function = match callable {
|
||||||
|
Callable::Lambda(func) => func.clone(),
|
||||||
|
Callable::NamedFunction(name) => {
|
||||||
|
match self.lookup_binding(&*name) {
|
||||||
|
Some(ReducedValue::Lambda(func)) => func,
|
||||||
|
_ => return (Null, None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if function.prototype.parameters.len() != arguments.len() {
|
||||||
|
return (Null, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut evaluator = Evaluator::new(Some(self));
|
||||||
|
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
|
||||||
|
evaluator.add_binding((**binding).clone(), expr.clone().into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let nodes = function.body.iter().map(|node| node.clone());
|
||||||
|
let mut retval = ExprNode(Null);
|
||||||
|
for n in nodes {
|
||||||
|
retval = evaluator.reduction_loop(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
match retval {
|
||||||
|
ExprNode(expr) => (expr, None),
|
||||||
|
FuncDefNode(_) => panic!("This should never happen! A maximally-reduced node\
|
||||||
|
should never be a function definition!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_builtin(callable: &Callable, arguments: &Vec<Expression>) -> Option<Reduction<Expression>> {
|
||||||
|
let name: &str = match *callable {
|
||||||
|
Callable::NamedFunction(ref name) => *&name,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
match name {
|
||||||
|
"print" => {
|
||||||
|
let mut s = String::new();
|
||||||
|
for arg in arguments {
|
||||||
|
s.push_str(&format!("{} ", arg));
|
||||||
|
}
|
||||||
|
return Some((Null, Some(SideEffect::Print(s))));
|
||||||
|
},
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
pub mod tokenizer;
|
||||||
|
pub mod parser;
|
||||||
|
pub mod eval;
|
||||||
|
pub mod compilation;
|
||||||
|
|
||||||
|
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput, TraceArtifact, LLVMCodeString};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TokenError {
|
||||||
|
pub msg: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TokenError {
|
||||||
|
pub fn new(msg: &str) -> TokenError {
|
||||||
|
TokenError { msg: msg.to_string() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use self::eval::Evaluator as MaaruEvaluator;
|
||||||
|
|
||||||
|
pub struct Maaru<'a> {
|
||||||
|
evaluator: MaaruEvaluator<'a>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Maaru<'a> {
|
||||||
|
pub fn new() -> Maaru<'a> {
|
||||||
|
Maaru {
|
||||||
|
evaluator: MaaruEvaluator::new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
||||||
|
fn get_language_name(&self) -> String {
|
||||||
|
"Maaru".to_string()
|
||||||
|
}
|
||||||
|
fn get_source_file_suffix(&self) -> String {
|
||||||
|
format!("maaru")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||||
|
let mut output = LanguageOutput::default();
|
||||||
|
|
||||||
|
let tokens = match tokenizer::tokenize(input) {
|
||||||
|
Ok(tokens) => {
|
||||||
|
if options.debug_tokens {
|
||||||
|
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens)));
|
||||||
|
}
|
||||||
|
tokens
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
output.add_output(format!("Tokenization error: {:?}\n", err.msg));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast = match parser::parse(&tokens, &[]) {
|
||||||
|
Ok(ast) => {
|
||||||
|
if options.debug_parse {
|
||||||
|
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||||
|
}
|
||||||
|
ast
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut evaluation_output = String::new();
|
||||||
|
for s in self.evaluator.run(ast).iter() {
|
||||||
|
evaluation_output.push_str(s);
|
||||||
|
}
|
||||||
|
output.add_output(evaluation_output);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn can_compile(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile(&mut self, input: &str) -> LLVMCodeString {
|
||||||
|
let tokens = match tokenizer::tokenize(input) {
|
||||||
|
Ok(tokens) => tokens,
|
||||||
|
Err(err) => {
|
||||||
|
let msg = format!("Tokenization error: {:?}\n", err.msg);
|
||||||
|
panic!("{}", msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast = match parser::parse(&tokens, &[]) {
|
||||||
|
Ok(ast) => ast,
|
||||||
|
Err(err) => {
|
||||||
|
let msg = format!("Parse error: {:?}\n", err.msg);
|
||||||
|
panic!("{}", msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
compilation::compile_ast(ast)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,755 @@
|
||||||
|
use maaru_lang::tokenizer::{Token, Kw, OpTok};
|
||||||
|
use maaru_lang::tokenizer::Token::*;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::convert::From;
|
||||||
|
|
||||||
|
// Grammar
|
||||||
|
// program := (statement delimiter ?)*
|
||||||
|
// delimiter := Newline | Semicolon
|
||||||
|
// statement := declaration | expression
|
||||||
|
// declaration := FN prototype LCurlyBrace (statement)* RCurlyBrace
|
||||||
|
// prototype := identifier LParen identlist RParen
|
||||||
|
// identlist := Ident (Comma Ident)* | ε
|
||||||
|
// exprlist := Expression (Comma Expression)* | ε
|
||||||
|
// itemlist := Ident COLON Expression (Comma Ident COLON Expression)* | ε
|
||||||
|
//
|
||||||
|
// expression := postop_expression (op postop_expression)*
|
||||||
|
// postop_expression := primary_expression postop
|
||||||
|
// primary_expression := number_expr | String | identifier_expr | paren_expr | conditional_expr | while_expr | lambda_expr | list_expr | struct_expr
|
||||||
|
// number_expr := (PLUS | MINUS ) number_expr | Number
|
||||||
|
// identifier_expr := call_expression | Variable
|
||||||
|
// list_expr := LSquareBracket exprlist RSquareBracket
|
||||||
|
// struct_expr := LCurlyBrace itemlist RCurlyBrace
|
||||||
|
// call_expression := Identifier LParen exprlist RParen
|
||||||
|
// while_expr := WHILE primary_expression LCurlyBrace (expression delimiter)* RCurlyBrace
|
||||||
|
// paren_expr := LParen expression RParen
|
||||||
|
// conditional_expr := IF expression LCurlyBrace (expression delimiter)* RCurlyBrace (LCurlyBrace (expresion delimiter)* RCurlyBrace)?
|
||||||
|
// lambda_expr := FN LParen identlist RParen LCurlyBrace (expression delimiter)* RCurlyBrace
|
||||||
|
// lambda_call := | LParen exprlist RParen
|
||||||
|
// postop := ε | LParen exprlist RParen | LBracket expression RBracket
|
||||||
|
// op := '+', '-', etc.
|
||||||
|
//
|
||||||
|
|
||||||
|
pub type AST = Vec<Statement>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Statement {
|
||||||
|
ExprNode(Expression),
|
||||||
|
FuncDefNode(Function),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Statement {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
use self::Statement::*;
|
||||||
|
match *self {
|
||||||
|
ExprNode(ref expr) => write!(f, "{}", expr),
|
||||||
|
FuncDefNode(_) => write!(f, "UNIMPLEMENTED"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Function {
|
||||||
|
pub prototype: Prototype,
|
||||||
|
pub body: Vec<Statement>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct Prototype {
|
||||||
|
pub name: Rc<String>,
|
||||||
|
pub parameters: Vec<Rc<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Expression {
|
||||||
|
Null,
|
||||||
|
StringLiteral(Rc<String>),
|
||||||
|
Number(f64),
|
||||||
|
Variable(Rc<String>),
|
||||||
|
BinExp(BinOp, Box<Expression>, Box<Expression>),
|
||||||
|
Call(Callable, Vec<Expression>),
|
||||||
|
Conditional(Box<Expression>, Box<Expression>, Option<Box<Expression>>),
|
||||||
|
Lambda(Function),
|
||||||
|
Block(VecDeque<Expression>),
|
||||||
|
While(Box<Expression>, Vec<Expression>),
|
||||||
|
Index(Box<Expression>, Box<Expression>),
|
||||||
|
ListLiteral(VecDeque<Expression>),
|
||||||
|
StructLiteral(VecDeque<(Rc<String>, Expression)>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Callable {
|
||||||
|
NamedFunction(Rc<String>),
|
||||||
|
Lambda(Function),
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO this ought to be ReducedExpression
|
||||||
|
impl fmt::Display for Expression {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
use self::Expression::*;
|
||||||
|
match *self {
|
||||||
|
Null => write!(f, "null"),
|
||||||
|
StringLiteral(ref s) => write!(f, "\"{}\"", s),
|
||||||
|
Number(n) => write!(f, "{}", n),
|
||||||
|
Lambda(Function { prototype: Prototype { ref name, ref parameters, .. }, .. }) => {
|
||||||
|
write!(f, "«function: {}, {} arg(s)»", name, parameters.len())
|
||||||
|
}
|
||||||
|
ListLiteral(ref items) => {
|
||||||
|
write!(f, "[ ")?;
|
||||||
|
let mut iter = items.iter().peekable();
|
||||||
|
while let Some(item) = iter.next() {
|
||||||
|
write!(f, "{}", item)?;
|
||||||
|
if let Some(_) = iter.peek() {
|
||||||
|
write!(f, ", ")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, " ]")
|
||||||
|
}
|
||||||
|
StructLiteral(ref items) => {
|
||||||
|
write!(f, "{} ", "{")?;
|
||||||
|
let mut iter = items.iter().peekable();
|
||||||
|
while let Some(pair) = iter.next() {
|
||||||
|
write!(f, "{}: {}", pair.0, pair.1)?;
|
||||||
|
if let Some(_) = iter.peek() {
|
||||||
|
write!(f, ", ")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(f, "{} ", "}")
|
||||||
|
}
|
||||||
|
_ => write!(f, "UNIMPLEMENTED"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum BinOp {
|
||||||
|
Add,
|
||||||
|
AddAssign,
|
||||||
|
Sub,
|
||||||
|
SubAssign,
|
||||||
|
Mul,
|
||||||
|
MulAssign,
|
||||||
|
Div,
|
||||||
|
DivAssign,
|
||||||
|
Mod,
|
||||||
|
Less,
|
||||||
|
LessEq,
|
||||||
|
Greater,
|
||||||
|
GreaterEq,
|
||||||
|
Equal,
|
||||||
|
Assign,
|
||||||
|
Custom(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<OpTok> for BinOp {
|
||||||
|
fn from(token: OpTok) -> BinOp {
|
||||||
|
use self::BinOp::*;
|
||||||
|
match &token.0[..] {
|
||||||
|
"+" => Add,
|
||||||
|
"+=" => AddAssign,
|
||||||
|
"-" => Sub,
|
||||||
|
"-=" => SubAssign,
|
||||||
|
"*" => Mul,
|
||||||
|
"*=" => MulAssign,
|
||||||
|
"/" => Div,
|
||||||
|
"/=" => DivAssign,
|
||||||
|
"%" => Mod,
|
||||||
|
"<" => Less,
|
||||||
|
"<=" => LessEq,
|
||||||
|
">" => Greater,
|
||||||
|
">=" => GreaterEq,
|
||||||
|
"==" => Equal,
|
||||||
|
"=" => Assign,
|
||||||
|
op => Custom(op.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Precedence = u8;
|
||||||
|
|
||||||
|
// TODO make this support incomplete parses
|
||||||
|
pub type ParseResult<T> = Result<T, ParseError>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ParseError {
|
||||||
|
pub msg: String,
|
||||||
|
pub remaining_tokens: Vec<Token>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParseError {
|
||||||
|
fn result_from_str<T>(msg: &str) -> ParseResult<T> {
|
||||||
|
Err(ParseError {
|
||||||
|
msg: msg.to_string(),
|
||||||
|
remaining_tokens: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Parser {
|
||||||
|
tokens: Vec<Token>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parser {
|
||||||
|
fn initialize(tokens: &[Token]) -> Parser {
|
||||||
|
let mut tokens = tokens.to_vec();
|
||||||
|
tokens.reverse();
|
||||||
|
Parser { tokens: tokens }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peek(&self) -> Option<Token> {
|
||||||
|
self.tokens.last().map(|x| x.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Token> {
|
||||||
|
self.tokens.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_precedence(&self, op: &OpTok) -> Precedence {
|
||||||
|
match &op.0[..] {
|
||||||
|
"+" => 10,
|
||||||
|
"-" => 10,
|
||||||
|
"*" => 20,
|
||||||
|
"/" => 20,
|
||||||
|
"%" => 20,
|
||||||
|
"==" => 40,
|
||||||
|
"=" | "+=" | "-=" | "*=" | "/=" => 1,
|
||||||
|
">" | ">=" | "<" | "<=" => 30,
|
||||||
|
_ => 255,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! expect {
|
||||||
|
($self_:expr, $token:pat) => {
|
||||||
|
match $self_.peek() {
|
||||||
|
Some($token) => {$self_.next();},
|
||||||
|
Some(x) => {
|
||||||
|
let err = format!("Expected `{:?}` but got `{:?}`", stringify!($token), x);
|
||||||
|
return ParseError::result_from_str(&err)
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let err = format!("Expected `{:?}` but got end of input", stringify!($token));
|
||||||
|
return ParseError::result_from_str(&err) //TODO make this not require 2 stringifications
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! expect_identifier {
|
||||||
|
($self_:expr) => {
|
||||||
|
match $self_.peek() {
|
||||||
|
Some(Identifier(s)) => {$self_.next(); s},
|
||||||
|
Some(x) => return ParseError::result_from_str(&format!("Expected identifier, but got {:?}", x)),
|
||||||
|
None => return ParseError::result_from_str("Expected identifier, but got end of input"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! skip_whitespace {
|
||||||
|
($_self: expr) => {
|
||||||
|
loop {
|
||||||
|
match $_self.peek() {
|
||||||
|
Some(ref t) if is_delimiter(t) => {
|
||||||
|
$_self.next();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! delimiter_block {
|
||||||
|
($_self: expr, $try_parse: ident, $($break_pattern: pat)|+) => {
|
||||||
|
{
|
||||||
|
let mut acc = Vec::new();
|
||||||
|
loop {
|
||||||
|
match $_self.peek() {
|
||||||
|
None => break,
|
||||||
|
Some(ref t) if is_delimiter(t) => { $_self.next(); continue; },
|
||||||
|
$($break_pattern)|+ => break,
|
||||||
|
_ => {
|
||||||
|
let a = try!($_self.$try_parse());
|
||||||
|
acc.push(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_delimiter(token: &Token) -> bool {
|
||||||
|
match *token {
|
||||||
|
Newline | Semicolon => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parser {
|
||||||
|
fn program(&mut self) -> ParseResult<AST> {
|
||||||
|
let mut ast = Vec::new(); //TODO have this come from previously-parsed tree
|
||||||
|
loop {
|
||||||
|
let result: ParseResult<Statement> = match self.peek() {
|
||||||
|
Some(ref t) if is_delimiter(t) => {
|
||||||
|
self.next();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(_) => self.statement(),
|
||||||
|
None => break,
|
||||||
|
};
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(node) => ast.push(node),
|
||||||
|
Err(mut err) => {
|
||||||
|
err.remaining_tokens = self.tokens.clone();
|
||||||
|
err.remaining_tokens.reverse();
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(ast)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn statement(&mut self) -> ParseResult<Statement> {
|
||||||
|
let node: Statement = match self.peek() {
|
||||||
|
Some(Keyword(Kw::Fn)) => self.declaration()?,
|
||||||
|
Some(_) => Statement::ExprNode(self.expression()?),
|
||||||
|
None => panic!("Unexpected end of tokens"),
|
||||||
|
};
|
||||||
|
Ok(node)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn declaration(&mut self) -> ParseResult<Statement> {
|
||||||
|
expect!(self, Keyword(Kw::Fn));
|
||||||
|
let prototype = self.prototype()?;
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
let body = self.body()?;
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
Ok(Statement::FuncDefNode(Function {
|
||||||
|
prototype: prototype,
|
||||||
|
body: body,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prototype(&mut self) -> ParseResult<Prototype> {
|
||||||
|
let name = expect_identifier!(self);
|
||||||
|
expect!(self, LParen);
|
||||||
|
let parameters = self.identlist()?;
|
||||||
|
expect!(self, RParen);
|
||||||
|
Ok(Prototype {
|
||||||
|
name: name,
|
||||||
|
parameters: parameters,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn identlist(&mut self) -> ParseResult<Vec<Rc<String>>> {
|
||||||
|
let mut args = Vec::new();
|
||||||
|
while let Some(Identifier(name)) = self.peek() {
|
||||||
|
args.push(name.clone());
|
||||||
|
self.next();
|
||||||
|
match self.peek() {
|
||||||
|
Some(Comma) => {self.next();},
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exprlist(&mut self) -> ParseResult<Vec<Expression>> {
|
||||||
|
let mut exprs = Vec::new();
|
||||||
|
loop {
|
||||||
|
if let Some(RParen) = self.peek() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let exp = self.expression()?;
|
||||||
|
exprs.push(exp);
|
||||||
|
match self.peek() {
|
||||||
|
Some(Comma) => {self.next();},
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(exprs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn itemlist(&mut self) -> ParseResult<VecDeque<(Rc<String>, Expression)>> {
|
||||||
|
let mut items = VecDeque::new();
|
||||||
|
loop {
|
||||||
|
if let Some(RCurlyBrace) = self.peek() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let name = expect_identifier!(self);
|
||||||
|
expect!(self, Colon);
|
||||||
|
let expr = self.expression()?;
|
||||||
|
items.push_back((name, expr));
|
||||||
|
match self.peek() {
|
||||||
|
Some(Comma) => {self.next();},
|
||||||
|
_ => break,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn body(&mut self) -> ParseResult<Vec<Statement>> {
|
||||||
|
let statements = delimiter_block!(
|
||||||
|
self,
|
||||||
|
statement,
|
||||||
|
Some(RCurlyBrace)
|
||||||
|
);
|
||||||
|
Ok(statements)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expression(&mut self) -> ParseResult<Expression> {
|
||||||
|
let lhs: Expression = self.postop_expression()?;
|
||||||
|
self.precedence_expr(lhs, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn precedence_expr(&mut self,
|
||||||
|
mut lhs: Expression,
|
||||||
|
min_precedence: u8)
|
||||||
|
-> ParseResult<Expression> {
|
||||||
|
while let Some(Operator(op)) = self.peek() {
|
||||||
|
let precedence = self.get_precedence(&op);
|
||||||
|
if precedence < min_precedence {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
self.next();
|
||||||
|
let mut rhs = self.postop_expression()?;
|
||||||
|
while let Some(Operator(ref op)) = self.peek() {
|
||||||
|
if self.get_precedence(op) > precedence {
|
||||||
|
let new_prec = self.get_precedence(op);
|
||||||
|
rhs = self.precedence_expr(rhs, new_prec)?;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lhs = Expression::BinExp(op.into(), Box::new(lhs), Box::new(rhs));
|
||||||
|
}
|
||||||
|
Ok(lhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn postop_expression(&mut self) -> ParseResult<Expression> {
|
||||||
|
use self::Expression::*;
|
||||||
|
let expr = self.primary_expression()?;
|
||||||
|
let ret = match self.peek() {
|
||||||
|
Some(LParen) => {
|
||||||
|
let args = self.call_expression()?;
|
||||||
|
match expr {
|
||||||
|
Lambda(f) => Call(Callable::Lambda(f), args),
|
||||||
|
e => {
|
||||||
|
let err = format!("Expected lambda expression before a call, got {:?}", e);
|
||||||
|
return ParseError::result_from_str(&err);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(LSquareBracket) => {
|
||||||
|
expect!(self, LSquareBracket);
|
||||||
|
let index_expr = self.expression()?;
|
||||||
|
expect!(self, RSquareBracket);
|
||||||
|
Index(Box::new(expr), Box::new(index_expr))
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
expr
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn primary_expression(&mut self) -> ParseResult<Expression> {
|
||||||
|
Ok(match self.peek() {
|
||||||
|
Some(Keyword(Kw::Null)) => {
|
||||||
|
self.next();
|
||||||
|
Expression::Null
|
||||||
|
}
|
||||||
|
Some(NumLiteral(_)) => self.number_expression()?,
|
||||||
|
Some(Operator(OpTok(ref a))) if **a == "+" || **a == "-" => self.number_expression()?,
|
||||||
|
Some(StrLiteral(s)) => {
|
||||||
|
self.next();
|
||||||
|
Expression::StringLiteral(s)
|
||||||
|
}
|
||||||
|
Some(Keyword(Kw::If)) => self.conditional_expr()?,
|
||||||
|
Some(Keyword(Kw::While)) => self.while_expr()?,
|
||||||
|
Some(Identifier(_)) => self.identifier_expr()?,
|
||||||
|
Some(Token::LParen) => self.paren_expr()?,
|
||||||
|
Some(Keyword(Kw::Fn)) => self.lambda_expr()?,
|
||||||
|
Some(Token::LSquareBracket) => self.list_expr()?,
|
||||||
|
Some(Token::LCurlyBrace) => self.struct_expr()?,
|
||||||
|
Some(e) => {
|
||||||
|
return ParseError::result_from_str(&format!("Expected primary expression, got \
|
||||||
|
{:?}",
|
||||||
|
e));
|
||||||
|
}
|
||||||
|
None => return ParseError::result_from_str("Expected primary expression received EoI"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
expect!(self, LSquareBracket);
|
||||||
|
let exprlist: Vec<Expression> = self.exprlist()?;
|
||||||
|
expect!(self, RSquareBracket);
|
||||||
|
|
||||||
|
Ok(Expression::ListLiteral(VecDeque::from(exprlist)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn struct_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
let struct_items = self.itemlist()?;
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
Ok(Expression::StructLiteral(struct_items))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn number_expression(&mut self) -> ParseResult<Expression> {
|
||||||
|
let mut multiplier = 1;
|
||||||
|
loop {
|
||||||
|
match self.peek() {
|
||||||
|
Some(NumLiteral(n)) => {
|
||||||
|
self.next();
|
||||||
|
return Ok(Expression::Number(n * multiplier as f64));
|
||||||
|
}
|
||||||
|
Some(Operator(OpTok(ref a))) if **a == "+" => {
|
||||||
|
self.next();
|
||||||
|
}
|
||||||
|
Some(Operator(OpTok(ref a))) if **a == "-" => {
|
||||||
|
multiplier *= -1;
|
||||||
|
self.next();
|
||||||
|
}
|
||||||
|
Some(e) => {
|
||||||
|
return ParseError::result_from_str(
|
||||||
|
&format!("Expected +, - or number, got {:?}", e));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return ParseError::result_from_str(
|
||||||
|
&format!("Expected +, - or number, got EoI"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lambda_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
use self::Expression::*;
|
||||||
|
expect!(self, Keyword(Kw::Fn));
|
||||||
|
skip_whitespace!(self);
|
||||||
|
expect!(self, LParen);
|
||||||
|
let parameters = self.identlist()?;
|
||||||
|
expect!(self, RParen);
|
||||||
|
skip_whitespace!(self);
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
let body = self.body()?;
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
|
||||||
|
let prototype = Prototype {
|
||||||
|
name: Rc::new("a lambda yo!".to_string()),
|
||||||
|
parameters: parameters,
|
||||||
|
};
|
||||||
|
|
||||||
|
let function = Function {
|
||||||
|
prototype: prototype,
|
||||||
|
body: body,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Lambda(function))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn while_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
use self::Expression::*;
|
||||||
|
expect!(self, Keyword(Kw::While));
|
||||||
|
let test = self.expression()?;
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
let body = delimiter_block!(
|
||||||
|
self,
|
||||||
|
expression,
|
||||||
|
Some(RCurlyBrace)
|
||||||
|
);
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
Ok(While(Box::new(test), body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conditional_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
use self::Expression::*;
|
||||||
|
expect!(self, Keyword(Kw::If));
|
||||||
|
let test = self.expression()?;
|
||||||
|
skip_whitespace!(self);
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
skip_whitespace!(self);
|
||||||
|
let then_block = delimiter_block!(
|
||||||
|
self,
|
||||||
|
expression,
|
||||||
|
Some(RCurlyBrace)
|
||||||
|
);
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
skip_whitespace!(self);
|
||||||
|
let else_block = if let Some(Keyword(Kw::Else)) = self.peek() {
|
||||||
|
self.next();
|
||||||
|
skip_whitespace!(self);
|
||||||
|
expect!(self, LCurlyBrace);
|
||||||
|
let else_exprs = delimiter_block!(
|
||||||
|
self,
|
||||||
|
expression,
|
||||||
|
Some(RCurlyBrace)
|
||||||
|
);
|
||||||
|
Some(else_exprs)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
expect!(self, RCurlyBrace);
|
||||||
|
Ok(Conditional(Box::new(test),
|
||||||
|
Box::new(Block(VecDeque::from(then_block))),
|
||||||
|
else_block.map(|list| Box::new(Block(VecDeque::from(list))))))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn identifier_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
let name = expect_identifier!(self);
|
||||||
|
let expr = match self.peek() {
|
||||||
|
Some(LParen) => {
|
||||||
|
let args = self.call_expression()?;
|
||||||
|
Expression::Call(Callable::NamedFunction(name), args)
|
||||||
|
}
|
||||||
|
__ => Expression::Variable(name),
|
||||||
|
};
|
||||||
|
Ok(expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call_expression(&mut self) -> ParseResult<Vec<Expression>> {
|
||||||
|
expect!(self, LParen);
|
||||||
|
let args: Vec<Expression> = self.exprlist()?;
|
||||||
|
expect!(self, RParen);
|
||||||
|
Ok(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paren_expr(&mut self) -> ParseResult<Expression> {
|
||||||
|
expect!(self, Token::LParen);
|
||||||
|
let expr = self.expression()?;
|
||||||
|
expect!(self, Token::RParen);
|
||||||
|
Ok(expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(tokens: &[Token], _parsed_tree: &[Statement]) -> ParseResult<AST> {
|
||||||
|
let mut parser = Parser::initialize(tokens);
|
||||||
|
parser.program()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use schala_lang::tokenizer;
|
||||||
|
use super::*;
|
||||||
|
use super::Statement::*;
|
||||||
|
use super::Expression::*;
|
||||||
|
|
||||||
|
macro_rules! parsetest {
|
||||||
|
($input:expr, $output:pat, $ifexpr:expr) => {
|
||||||
|
{
|
||||||
|
let tokens = tokenizer::tokenize($input).unwrap();
|
||||||
|
let ast = parse(&tokens, &[]).unwrap();
|
||||||
|
match &ast[..] {
|
||||||
|
$output if $ifexpr => (),
|
||||||
|
x => panic!("Error in parse test, got {:?} instead", x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_parse_test() {
|
||||||
|
use super::Function;
|
||||||
|
parsetest!(
|
||||||
|
"fn a() { 1 + 2 }",
|
||||||
|
&[FuncDefNode(Function {prototype: Prototype { ref name, ref parameters }, ref body})],
|
||||||
|
match &body[..] { &[ExprNode(BinExp(_, box Number(1.0), box Number(2.0)))] => true, _ => false }
|
||||||
|
&& **name == "a" && match ¶meters[..] { &[] => true, _ => false }
|
||||||
|
);
|
||||||
|
|
||||||
|
parsetest!(
|
||||||
|
"fn a(x,y){ 1 + 2 }",
|
||||||
|
&[FuncDefNode(Function {prototype: Prototype { ref name, ref parameters }, ref body})],
|
||||||
|
match &body[..] { &[ExprNode(BinExp(_, box Number(1.0), box Number(2.0)))] => true, _ => false }
|
||||||
|
&& **name == "a" && *parameters[0] == "x" && *parameters[1] == "y" && parameters.len() == 2
|
||||||
|
);
|
||||||
|
|
||||||
|
let t3 = "fn (x) { x + 2 }";
|
||||||
|
let tokens3 = tokenizer::tokenize(t3).unwrap();
|
||||||
|
assert!(parse(&tokens3, &[]).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expression_parse_test() {
|
||||||
|
parsetest!("a", &[ExprNode(Variable(ref s))], **s == "a");
|
||||||
|
parsetest!("a + b",
|
||||||
|
&[ExprNode(BinExp(BinOp::Add, box Variable(ref a), box Variable(ref b)))],
|
||||||
|
**a == "a" && **b == "b");
|
||||||
|
parsetest!("a + b * c",
|
||||||
|
&[ExprNode(BinExp(BinOp::Add, box Variable(ref a), box BinExp(BinOp::Mul, box Variable(ref b), box Variable(ref c))))],
|
||||||
|
**a == "a" && **b == "b" && **c == "c");
|
||||||
|
parsetest!("a * b + c",
|
||||||
|
&[ExprNode(BinExp(BinOp::Add, box BinExp(BinOp::Mul, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
||||||
|
**a == "a" && **b == "b" && **c == "c");
|
||||||
|
parsetest!("(a + b) * c",
|
||||||
|
&[ExprNode(BinExp(BinOp::Mul, box BinExp(BinOp::Add, box Variable(ref a), box Variable(ref b)), box Variable(ref c)))],
|
||||||
|
**a == "a" && **b == "b" && **c == "c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lambda_parse_test() {
|
||||||
|
use schala_lang::tokenizer;
|
||||||
|
let t1 = "(fn(x) { x + 2 })";
|
||||||
|
let tokens1 = tokenizer::tokenize(t1).unwrap();
|
||||||
|
match parse(&tokens1, &[]).unwrap()[..] {
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
let t2 = "fn(x) { x + 2 }";
|
||||||
|
let tokens2 = tokenizer::tokenize(t2).unwrap();
|
||||||
|
assert!(parse(&tokens2, &[]).is_err());
|
||||||
|
|
||||||
|
let t3 = "(fn(x) { x + 10 })(20)";
|
||||||
|
let tokens3 = tokenizer::tokenize(t3).unwrap();
|
||||||
|
match parse(&tokens3, &[]).unwrap() {
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn conditional_parse_test() {
|
||||||
|
use schala_lang::tokenizer;
|
||||||
|
let t1 = "if null { 20 } else { 40 }";
|
||||||
|
let tokens = tokenizer::tokenize(t1).unwrap();
|
||||||
|
match parse(&tokens, &[]).unwrap()[..] {
|
||||||
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
||||||
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
let t2 = r"
|
||||||
|
if null {
|
||||||
|
20
|
||||||
|
} else {
|
||||||
|
40
|
||||||
|
}
|
||||||
|
";
|
||||||
|
let tokens2 = tokenizer::tokenize(t2).unwrap();
|
||||||
|
match parse(&tokens2, &[]).unwrap()[..] {
|
||||||
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
||||||
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
let t2 = r"
|
||||||
|
if null {
|
||||||
|
20 } else
|
||||||
|
{
|
||||||
|
40
|
||||||
|
}
|
||||||
|
";
|
||||||
|
let tokens3 = tokenizer::tokenize(t2).unwrap();
|
||||||
|
match parse(&tokens3, &[]).unwrap()[..] {
|
||||||
|
[ExprNode(Conditional(box Null, box Block(_), Some(box Block(_))))] => (),
|
||||||
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
|
@ -0,0 +1,208 @@
|
||||||
|
extern crate itertools;
|
||||||
|
|
||||||
|
use std::iter::Peekable;
|
||||||
|
use std::str::Chars;
|
||||||
|
use self::itertools::Itertools;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use maaru_lang::TokenError;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum Token {
|
||||||
|
Newline,
|
||||||
|
Semicolon,
|
||||||
|
LParen,
|
||||||
|
RParen,
|
||||||
|
LSquareBracket,
|
||||||
|
RSquareBracket,
|
||||||
|
LCurlyBrace,
|
||||||
|
RCurlyBrace,
|
||||||
|
Comma,
|
||||||
|
Period,
|
||||||
|
Colon,
|
||||||
|
NumLiteral(f64),
|
||||||
|
StrLiteral(Rc<String>),
|
||||||
|
Identifier(Rc<String>),
|
||||||
|
Operator(OpTok),
|
||||||
|
Keyword(Kw),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct OpTok(pub Rc<String>);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum Kw {
|
||||||
|
If,
|
||||||
|
Else,
|
||||||
|
While,
|
||||||
|
Let,
|
||||||
|
Fn,
|
||||||
|
Null,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type TokenizeResult = Result<Vec<Token>, TokenError>;
|
||||||
|
|
||||||
|
fn is_digit(c: &char) -> bool {
|
||||||
|
c.is_digit(10)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tokenize(input: &str) -> TokenizeResult {
|
||||||
|
use self::Token::*;
|
||||||
|
let mut tokens = Vec::new();
|
||||||
|
let mut iter: Peekable<Chars> = input.chars().peekable();
|
||||||
|
while let Some(c) = iter.next() {
|
||||||
|
if c == '#' {
|
||||||
|
while let Some(c) = iter.next() {
|
||||||
|
if c == '\n' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let cur_tok = match c {
|
||||||
|
c if char::is_whitespace(c) && c != '\n' => continue,
|
||||||
|
'\n' => Newline,
|
||||||
|
';' => Semicolon,
|
||||||
|
'(' => LParen,
|
||||||
|
')' => RParen,
|
||||||
|
':' => Colon,
|
||||||
|
',' => Comma,
|
||||||
|
'{' => LCurlyBrace,
|
||||||
|
'}' => RCurlyBrace,
|
||||||
|
'[' => LSquareBracket,
|
||||||
|
']' => RSquareBracket,
|
||||||
|
'"' => tokenize_str(&mut iter)?,
|
||||||
|
c if !char::is_alphanumeric(c) => tokenize_operator(c, &mut iter)?,
|
||||||
|
c @ '.' | c if is_digit(&c) => tokenize_number_or_period(c, &mut iter)?,
|
||||||
|
c => tokenize_identifier(c, &mut iter)?,
|
||||||
|
};
|
||||||
|
tokens.push(cur_tok);
|
||||||
|
}
|
||||||
|
Ok(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize_str(iter: &mut Peekable<Chars>) -> Result<Token, TokenError> {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
loop {
|
||||||
|
// TODO handle string escapes, interpolation
|
||||||
|
match iter.next() {
|
||||||
|
Some(x) if x == '"' => break,
|
||||||
|
Some(x) => buffer.push(x),
|
||||||
|
None => return Err(TokenError::new("Unclosed quote")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Token::StrLiteral(Rc::new(buffer)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize_operator(c: char, iter: &mut Peekable<Chars>) -> Result<Token, TokenError> {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| !char::is_alphanumeric(*x) && !char::is_whitespace(*x)));
|
||||||
|
Ok(Token::Operator(OpTok(Rc::new(buffer))))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize_number_or_period(c: char, iter: &mut Peekable<Chars>) -> Result<Token, TokenError> {
|
||||||
|
if c == '.' && !iter.peek().map_or(false, is_digit) {
|
||||||
|
return Ok(Token::Period);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| is_digit(x) || *x == '.'));
|
||||||
|
|
||||||
|
match buffer.parse::<f64>() {
|
||||||
|
Ok(f) => Ok(Token::NumLiteral(f)),
|
||||||
|
Err(_) => Err(TokenError::new("Failed to parse digit")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize_identifier(c: char, iter: &mut Peekable<Chars>) -> Result<Token, TokenError> {
|
||||||
|
fn ends_identifier(c: &char) -> bool {
|
||||||
|
let c = *c;
|
||||||
|
char::is_whitespace(c) || is_digit(&c) || c == ';' || c == '(' || c == ')' ||
|
||||||
|
c == ',' || c == '.' || c == ',' || c == ':' || c == '[' || c == ']'
|
||||||
|
}
|
||||||
|
|
||||||
|
use self::Token::*;
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| !ends_identifier(x)));
|
||||||
|
|
||||||
|
Ok(match &buffer[..] {
|
||||||
|
"if" => Keyword(Kw::If),
|
||||||
|
"else" => Keyword(Kw::Else),
|
||||||
|
"while" => Keyword(Kw::While),
|
||||||
|
"let" => Keyword(Kw::Let),
|
||||||
|
"fn" => Keyword(Kw::Fn),
|
||||||
|
"null" => Keyword(Kw::Null),
|
||||||
|
b => Identifier(Rc::new(b.to_string())),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use super::Token::*;
|
||||||
|
|
||||||
|
macro_rules! token_test {
|
||||||
|
($input: expr, $output: pat, $ifexpr: expr) => {
|
||||||
|
let tokens = tokenize($input).unwrap();
|
||||||
|
match tokens[..] {
|
||||||
|
$output if $ifexpr => (),
|
||||||
|
_ => panic!("Actual output: {:?}", tokens),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_tokeniziation_tests() {
|
||||||
|
token_test!("let a = 3\n",
|
||||||
|
[Keyword(Kw::Let), Identifier(ref a), Operator(OpTok(ref b)), NumLiteral(3.0), Newline],
|
||||||
|
**a == "a" && **b == "=");
|
||||||
|
|
||||||
|
token_test!("2+1",
|
||||||
|
[NumLiteral(2.0), Operator(OpTok(ref a)), NumLiteral(1.0)],
|
||||||
|
**a == "+");
|
||||||
|
|
||||||
|
token_test!("2 + 1",
|
||||||
|
[NumLiteral(2.0), Operator(OpTok(ref a)), NumLiteral(1.0)],
|
||||||
|
**a == "+");
|
||||||
|
|
||||||
|
token_test!("2.3*49.2",
|
||||||
|
[NumLiteral(2.3), Operator(OpTok(ref a)), NumLiteral(49.2)],
|
||||||
|
**a == "*");
|
||||||
|
|
||||||
|
token_test!("a+3",
|
||||||
|
[Identifier(ref a), NumLiteral(3.0)],
|
||||||
|
**a == "a+");
|
||||||
|
|
||||||
|
assert!(tokenize("2.4.5").is_err());
|
||||||
|
|
||||||
|
token_test!("fn my_func(a) { a ? 3[1] }",
|
||||||
|
[Keyword(Kw::Fn), Identifier(ref a), LParen, Identifier(ref b), RParen, LCurlyBrace, Identifier(ref c),
|
||||||
|
Operator(OpTok(ref d)), NumLiteral(3.0), LSquareBracket, NumLiteral(1.0), RSquareBracket, RCurlyBrace],
|
||||||
|
**a == "my_func" && **b == "a" && **c == "a" && **d == "?");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_test() {
|
||||||
|
token_test!("null + \"a string\"",
|
||||||
|
[Keyword(Kw::Null), Operator(OpTok(ref a)), StrLiteral(ref b)],
|
||||||
|
**a == "+" && **b == "a string");
|
||||||
|
|
||||||
|
token_test!("\"{?'q@?\"",
|
||||||
|
[StrLiteral(ref a)],
|
||||||
|
**a == "{?'q@?");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn operator_test() {
|
||||||
|
token_test!("a *> b",
|
||||||
|
[Identifier(ref a), Operator(OpTok(ref b)), Identifier(ref c)],
|
||||||
|
**a == "a" && **b == "*>" && **c == "b");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
28
src/main.rs
28
src/main.rs
|
@ -1,3 +1,29 @@
|
||||||
|
#![feature(advanced_slice_patterns, slice_patterns, box_patterns, box_syntax)]
|
||||||
|
#![feature(plugin)]
|
||||||
|
extern crate itertools;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate maplit;
|
||||||
|
|
||||||
|
mod schala_lang;
|
||||||
|
mod maaru_lang;
|
||||||
|
mod robo_lang;
|
||||||
|
mod rukka_lang;
|
||||||
|
|
||||||
|
extern crate schala_lib;
|
||||||
|
use schala_lib::{PLIGenerator, schala_main};
|
||||||
|
|
||||||
|
extern { }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let generators: Vec<PLIGenerator> = vec![
|
||||||
|
Box::new(|| { Box::new(schala_lang::Schala::new())}),
|
||||||
|
Box::new(|| { Box::new(schala_lang::autoparser::Schala::new())}),
|
||||||
|
Box::new(|| { Box::new(maaru_lang::Maaru::new())}),
|
||||||
|
Box::new(|| { Box::new(robo_lang::Robo::new())}),
|
||||||
|
Box::new(|| { Box::new(rukka_lang::Rukka::new())}),
|
||||||
|
];
|
||||||
|
schala_main(generators);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,167 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput};
|
||||||
|
|
||||||
|
pub struct Robo {
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Robo {
|
||||||
|
pub fn new() -> Robo {
|
||||||
|
Robo { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TokenError {
|
||||||
|
pub msg: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TokenError {
|
||||||
|
pub fn new(msg: &str) -> TokenError {
|
||||||
|
TokenError { msg: msg.to_string() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Token {
|
||||||
|
StrLiteral(String),
|
||||||
|
Backtick,
|
||||||
|
Newline,
|
||||||
|
LParen,
|
||||||
|
RParen,
|
||||||
|
LBracket,
|
||||||
|
RBracket,
|
||||||
|
LBrace,
|
||||||
|
RBrace,
|
||||||
|
Period,
|
||||||
|
Comma,
|
||||||
|
Colon,
|
||||||
|
Semicolon,
|
||||||
|
SingleQuote,
|
||||||
|
Identifier(String),
|
||||||
|
Operator(String),
|
||||||
|
NumLiteral(Number),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Number {
|
||||||
|
IntegerRep(String),
|
||||||
|
FloatRep(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub type AST = Vec<ASTNode>;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ASTNode {
|
||||||
|
FunctionDefinition(String, Expression),
|
||||||
|
ImportStatement(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Expression {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize(input: &str) -> Result<Vec<Token>, TokenError> {
|
||||||
|
use self::Token::*;
|
||||||
|
let mut tokens = Vec::new();
|
||||||
|
let mut iter = input.chars().peekable();
|
||||||
|
while let Some(c) = iter.next() {
|
||||||
|
if c == ';' {
|
||||||
|
while let Some(c) = iter.next() {
|
||||||
|
if c == '\n' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let cur_tok = match c {
|
||||||
|
c if char::is_whitespace(c) && c != '\n' => continue,
|
||||||
|
'\n' => Newline,
|
||||||
|
'(' => LParen,
|
||||||
|
')' => RParen,
|
||||||
|
'[' => LBracket,
|
||||||
|
']' => RBracket,
|
||||||
|
'{' => LBrace,
|
||||||
|
'}' => RBrace,
|
||||||
|
',' => Comma,
|
||||||
|
':' => Colon,
|
||||||
|
';' => Semicolon,
|
||||||
|
'.' => Period,
|
||||||
|
'`' => Backtick,
|
||||||
|
'\'' => SingleQuote,
|
||||||
|
'"' => {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
loop {
|
||||||
|
match iter.next() {
|
||||||
|
Some(x) if x == '"' => break,
|
||||||
|
Some(x) => buffer.push(x),
|
||||||
|
None => return Err(TokenError::new("Unclosed quote")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StrLiteral(buffer)
|
||||||
|
}
|
||||||
|
c if c.is_digit(10) => {
|
||||||
|
let mut integer = true;
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| x.is_digit(10)));
|
||||||
|
if let Some(&'.') = iter.peek() {
|
||||||
|
buffer.push(iter.next().unwrap());
|
||||||
|
integer = false;
|
||||||
|
}
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| x.is_digit(10)));
|
||||||
|
let inner = if integer {
|
||||||
|
Number::IntegerRep(buffer)
|
||||||
|
} else {
|
||||||
|
Number::FloatRep(buffer)
|
||||||
|
};
|
||||||
|
NumLiteral(inner)
|
||||||
|
},
|
||||||
|
c if char::is_alphanumeric(c) => {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| char::is_alphanumeric(*x)));
|
||||||
|
Identifier(buffer)
|
||||||
|
},
|
||||||
|
c => {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
buffer.push(c);
|
||||||
|
buffer.extend(iter.peeking_take_while(|x| !char::is_whitespace(*x)));
|
||||||
|
Operator(buffer)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
tokens.push(cur_tok);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgrammingLanguageInterface for Robo {
|
||||||
|
fn get_language_name(&self) -> String {
|
||||||
|
"Robo".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_source_file_suffix(&self) -> String {
|
||||||
|
format!("robo")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> LanguageOutput {
|
||||||
|
let mut output = LanguageOutput::default();
|
||||||
|
let tokens = match tokenize(input) {
|
||||||
|
Ok(tokens) => tokens,
|
||||||
|
Err(e) => {
|
||||||
|
output.add_output(format!("Tokenize error: {:?}", e));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
output.add_output(format!("{:?}", tokens));
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,432 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, LanguageOutput};
|
||||||
|
use std::iter::Peekable;
|
||||||
|
use std::vec::IntoIter;
|
||||||
|
use std::str::Chars;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub struct EvaluatorState {
|
||||||
|
binding_stack: Vec<HashMap<String, Sexp>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EvaluatorState {
|
||||||
|
fn new() -> EvaluatorState {
|
||||||
|
use self::Sexp::Primitive;
|
||||||
|
use self::PrimitiveFn::*;
|
||||||
|
let mut default_map = HashMap::new();
|
||||||
|
default_map.insert(format!("+"), Primitive(Plus));
|
||||||
|
default_map.insert(format!("-"), Primitive(Minus));
|
||||||
|
default_map.insert(format!("*"), Primitive(Mult));
|
||||||
|
default_map.insert(format!("/"), Primitive(Div));
|
||||||
|
default_map.insert(format!("%"), Primitive(Mod));
|
||||||
|
default_map.insert(format!(">"), Primitive(Greater));
|
||||||
|
default_map.insert(format!("<"), Primitive(Less));
|
||||||
|
default_map.insert(format!("<="), Primitive(LessThanOrEqual));
|
||||||
|
default_map.insert(format!(">="), Primitive(GreaterThanOrEqual));
|
||||||
|
default_map.insert(format!("display"), Primitive(Display));
|
||||||
|
|
||||||
|
EvaluatorState {
|
||||||
|
binding_stack: vec![default_map],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn set_var(&mut self, var: String, value: Sexp) {
|
||||||
|
let binding = self.binding_stack.last_mut().unwrap();
|
||||||
|
binding.insert(var, value);
|
||||||
|
}
|
||||||
|
fn get_var(&self, var: &str) -> Option<&Sexp> {
|
||||||
|
for bindings in self.binding_stack.iter().rev() {
|
||||||
|
match bindings.get(var) {
|
||||||
|
Some(x) => return Some(x),
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_env(&mut self) {
|
||||||
|
self.binding_stack.push(HashMap::new());
|
||||||
|
}
|
||||||
|
fn pop_env(&mut self) {
|
||||||
|
self.binding_stack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Rukka {
|
||||||
|
state: EvaluatorState
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rukka {
|
||||||
|
pub fn new() -> Rukka { Rukka { state: EvaluatorState::new() } }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgrammingLanguageInterface for Rukka {
|
||||||
|
fn get_language_name(&self) -> String {
|
||||||
|
"Rukka".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_source_file_suffix(&self) -> String {
|
||||||
|
format!("rukka")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> LanguageOutput {
|
||||||
|
let mut output = LanguageOutput::default();
|
||||||
|
let sexps = match read(input) {
|
||||||
|
Err(err) => {
|
||||||
|
output.add_output(format!("Error: {}", err));
|
||||||
|
return output;
|
||||||
|
},
|
||||||
|
Ok(sexps) => sexps
|
||||||
|
};
|
||||||
|
|
||||||
|
let output_str: String = sexps.into_iter().enumerate().map(|(i, sexp)| {
|
||||||
|
match self.state.eval(sexp) {
|
||||||
|
Ok(result) => format!("{}: {}", i, result.print()),
|
||||||
|
Err(err) => format!("{} Error: {}", i, err),
|
||||||
|
}
|
||||||
|
}).intersperse(format!("\n")).collect();
|
||||||
|
output.add_output(output_str);
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EvaluatorState {
|
||||||
|
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
|
||||||
|
use self::Sexp::*;
|
||||||
|
Ok(match expr {
|
||||||
|
SymbolAtom(ref sym) => match self.get_var(sym) {
|
||||||
|
Some(ref sexp) => {
|
||||||
|
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
|
||||||
|
q.clone() //TODO make this not involve a clone
|
||||||
|
},
|
||||||
|
None => return Err(format!("Variable {} not bound", sym)),
|
||||||
|
},
|
||||||
|
expr @ Primitive(_) => expr,
|
||||||
|
expr @ FnLiteral { .. } => expr,
|
||||||
|
expr @ StringAtom(_) => expr,
|
||||||
|
expr @ NumberAtom(_) => expr,
|
||||||
|
expr @ BoolAtom(_) => expr,
|
||||||
|
Cons(box operator, box operands) => match operator {
|
||||||
|
SymbolAtom(ref sym) if match &sym[..] {
|
||||||
|
"quote" | "eq?" | "cons" | "car" | "cdr" | "atom?" | "define" | "lambda" | "if" | "cond" => true, _ => false
|
||||||
|
} => self.eval_special_form(sym, operands)?,
|
||||||
|
_ => {
|
||||||
|
let evaled = self.eval(operator)?;
|
||||||
|
self.apply(evaled, operands)?
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Nil => Nil,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn eval_special_form(&mut self, form: &str, operands: Sexp) -> Result<Sexp, String> {
|
||||||
|
use self::Sexp::*;
|
||||||
|
Ok(match form {
|
||||||
|
"quote" => match operands {
|
||||||
|
Cons(box quoted, box Nil) => quoted,
|
||||||
|
_ => return Err(format!("Bad syntax in quote")),
|
||||||
|
},
|
||||||
|
"eq?" => match operands {//TODO make correct
|
||||||
|
Cons(box lhs, box Cons(box rhs, _)) => BoolAtom(lhs == rhs),
|
||||||
|
_ => BoolAtom(true),
|
||||||
|
},
|
||||||
|
"cons" => match operands {
|
||||||
|
Cons(box cadr, box Cons(box caddr, box Nil)) => {
|
||||||
|
let newl = self.eval(cadr)?;
|
||||||
|
let newr = self.eval(caddr)?;
|
||||||
|
Cons(Box::new(newl), Box::new(newr))
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad arguments for cons")),
|
||||||
|
},
|
||||||
|
"car" => match operands {
|
||||||
|
Cons(box car, _) => car,
|
||||||
|
_ => return Err(format!("called car with a non-pair argument")),
|
||||||
|
},
|
||||||
|
"cdr" => match operands {
|
||||||
|
Cons(_, box cdr) => cdr,
|
||||||
|
_ => return Err(format!("called cdr with a non-pair argument")),
|
||||||
|
},
|
||||||
|
"atom?" => match operands {
|
||||||
|
Cons(_, _) => BoolAtom(false),
|
||||||
|
_ => BoolAtom(true),
|
||||||
|
},
|
||||||
|
"define" => match operands {
|
||||||
|
Cons(box SymbolAtom(sym), box Cons(box expr, box Nil)) => {
|
||||||
|
let evaluated = self.eval(expr)?;
|
||||||
|
self.set_var(sym, evaluated);
|
||||||
|
Nil
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad assignment")),
|
||||||
|
}
|
||||||
|
"lambda" => match operands {
|
||||||
|
Cons(box mut paramlist, box Cons(box formalexp, box Nil)) => {
|
||||||
|
let mut formal_params = vec![];
|
||||||
|
{
|
||||||
|
let mut ptr = ¶mlist;
|
||||||
|
loop {
|
||||||
|
match ptr {
|
||||||
|
&Cons(ref arg, ref rest) => {
|
||||||
|
if let SymbolAtom(ref sym) = **arg {
|
||||||
|
formal_params.push(sym.clone());
|
||||||
|
ptr = rest;
|
||||||
|
} else {
|
||||||
|
return Err(format!("Bad lambda format"));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FnLiteral {
|
||||||
|
formal_params,
|
||||||
|
body: Box::new(formalexp)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad lambda expression")),
|
||||||
|
},
|
||||||
|
"if" => match operands {
|
||||||
|
Cons(box test, box body) => {
|
||||||
|
let truth_value = test.truthy();
|
||||||
|
match (truth_value, body) {
|
||||||
|
(true, Cons(box consequent, _)) => consequent,
|
||||||
|
(false, Cons(_, box Cons(box alternative, _))) => alternative,
|
||||||
|
_ => return Err(format!("Bad if expression"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad if expression"))
|
||||||
|
},
|
||||||
|
s => return Err(format!("Non-existent special form {}; this should never happen", s)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&mut self, function: Sexp, operands: Sexp) -> Result<Sexp, String> {
|
||||||
|
use self::Sexp::*;
|
||||||
|
match function {
|
||||||
|
FnLiteral { formal_params, body } => {
|
||||||
|
self.push_env();
|
||||||
|
|
||||||
|
let mut cur = operands;
|
||||||
|
for param in formal_params {
|
||||||
|
match cur {
|
||||||
|
Cons(box arg, box rest) => {
|
||||||
|
cur = rest;
|
||||||
|
self.set_var(param, arg);
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad argument for function application")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let result = self.eval(*body);
|
||||||
|
self.pop_env();
|
||||||
|
result
|
||||||
|
},
|
||||||
|
Primitive(prim) => {
|
||||||
|
let mut evaled_operands = Vec::new();
|
||||||
|
let mut cur_operand = operands;
|
||||||
|
loop {
|
||||||
|
match cur_operand {
|
||||||
|
Nil => break,
|
||||||
|
Cons(box l, box rest) => {
|
||||||
|
evaled_operands.push(self.eval(l)?);
|
||||||
|
cur_operand = rest;
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Bad operands list"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prim.apply(evaled_operands)
|
||||||
|
}
|
||||||
|
_ => return Err(format!("Bad type to apply")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read(input: &str) -> Result<Vec<Sexp>, String> {
|
||||||
|
let mut chars: Peekable<Chars> = input.chars().peekable();
|
||||||
|
let mut tokens = tokenize(&mut chars).into_iter().peekable();
|
||||||
|
let mut sexps = Vec::new();
|
||||||
|
while let Some(_) = tokens.peek() {
|
||||||
|
sexps.push(parse(&mut tokens)?);
|
||||||
|
}
|
||||||
|
Ok(sexps)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Token {
|
||||||
|
LParen,
|
||||||
|
RParen,
|
||||||
|
Quote,
|
||||||
|
Word(String),
|
||||||
|
StringLiteral(String),
|
||||||
|
NumLiteral(u64),
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO make this notion of Eq more sophisticated
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
enum Sexp {
|
||||||
|
SymbolAtom(String),
|
||||||
|
StringAtom(String),
|
||||||
|
NumberAtom(u64),
|
||||||
|
BoolAtom(bool),
|
||||||
|
Cons(Box<Sexp>, Box<Sexp>),
|
||||||
|
Nil,
|
||||||
|
FnLiteral {
|
||||||
|
formal_params: Vec<String>,
|
||||||
|
body: Box<Sexp>
|
||||||
|
},
|
||||||
|
Primitive(PrimitiveFn)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
enum PrimitiveFn {
|
||||||
|
Plus, Minus, Mult, Div, Mod, Greater, Less, GreaterThanOrEqual, LessThanOrEqual, Display
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrimitiveFn {
|
||||||
|
fn apply(&self, evaled_operands: Vec<Sexp>) -> Result<Sexp, String> {
|
||||||
|
use self::Sexp::*;
|
||||||
|
use self::PrimitiveFn::*;
|
||||||
|
let op = self.clone();
|
||||||
|
Ok(match op {
|
||||||
|
Display => {
|
||||||
|
for arg in evaled_operands {
|
||||||
|
print!("{}\n", arg.print());
|
||||||
|
}
|
||||||
|
Nil
|
||||||
|
},
|
||||||
|
Plus | Mult => {
|
||||||
|
let mut result = match op { Plus => 0, Mult => 1, _ => unreachable!() };
|
||||||
|
for arg in evaled_operands {
|
||||||
|
if let NumberAtom(n) = arg {
|
||||||
|
if let Plus = op {
|
||||||
|
result += n;
|
||||||
|
} else if let Mult = op {
|
||||||
|
result *= n;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(format!("Bad operand: {:?}", arg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NumberAtom(result)
|
||||||
|
},
|
||||||
|
op => return Err(format!("Primitive op {:?} not implemented", op)),
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sexp {
|
||||||
|
fn print(&self) -> String {
|
||||||
|
use self::Sexp::*;
|
||||||
|
match self {
|
||||||
|
&BoolAtom(true) => format!("#t"),
|
||||||
|
&BoolAtom(false) => format!("#f"),
|
||||||
|
&SymbolAtom(ref sym) => format!("{}", sym),
|
||||||
|
&StringAtom(ref s) => format!("\"{}\"", s),
|
||||||
|
&NumberAtom(ref n) => format!("{}", n),
|
||||||
|
&Cons(ref car, ref cdr) => format!("({} . {})", car.print(), cdr.print()),
|
||||||
|
&Nil => format!("()"),
|
||||||
|
&FnLiteral { ref formal_params, .. } => format!("<lambda {:?}>", formal_params),
|
||||||
|
&Primitive(ref sym) => format!("<primitive \"{:?}\">", sym),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn truthy(&self) -> bool {
|
||||||
|
use self::Sexp::*;
|
||||||
|
match self {
|
||||||
|
&BoolAtom(false) => false,
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tokenize(input: &mut Peekable<Chars>) -> Vec<Token> {
|
||||||
|
use self::Token::*;
|
||||||
|
let mut tokens = Vec::new();
|
||||||
|
loop {
|
||||||
|
match input.next() {
|
||||||
|
None => break,
|
||||||
|
Some('(') => tokens.push(LParen),
|
||||||
|
Some(')') => tokens.push(RParen),
|
||||||
|
Some('\'') => tokens.push(Quote),
|
||||||
|
Some(c) if c.is_whitespace() => continue,
|
||||||
|
Some(c) if c.is_numeric() => {
|
||||||
|
let tok: String = input.peeking_take_while(|next| next.is_numeric()).collect();
|
||||||
|
let n: u64 = format!("{}{}", c, tok).parse().unwrap();
|
||||||
|
tokens.push(NumLiteral(n));
|
||||||
|
},
|
||||||
|
Some('"') => {
|
||||||
|
let string: String = input.scan(false, |escape, cur_char| {
|
||||||
|
let seen_escape = *escape;
|
||||||
|
*escape = cur_char == '\\' && !seen_escape;
|
||||||
|
match (cur_char, seen_escape) {
|
||||||
|
('"', false) => None,
|
||||||
|
('\\', false) => Some(None),
|
||||||
|
(c, _) => Some(Some(c))
|
||||||
|
}
|
||||||
|
}).filter_map(|x| x).collect();
|
||||||
|
tokens.push(StringLiteral(string));
|
||||||
|
}
|
||||||
|
Some(c) => {
|
||||||
|
let sym: String = input.peeking_take_while(|next| {
|
||||||
|
match *next {
|
||||||
|
'(' | ')' => false,
|
||||||
|
c if c.is_whitespace() => false,
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
|
tokens.push(Word(format!("{}{}", c, sym)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokens
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
||||||
|
use self::Token::*;
|
||||||
|
use self::Sexp::*;
|
||||||
|
match tokens.next() {
|
||||||
|
Some(Word(ref s)) if s == "#f" => Ok(BoolAtom(false)),
|
||||||
|
Some(Word(ref s)) if s == "#t" => Ok(BoolAtom(true)),
|
||||||
|
Some(Word(s)) => Ok(SymbolAtom(s)),
|
||||||
|
Some(StringLiteral(s)) => Ok(StringAtom(s)),
|
||||||
|
Some(LParen) => parse_sexp(tokens),
|
||||||
|
Some(RParen) => Err(format!("Unexpected ')'")),
|
||||||
|
Some(Quote) => {
|
||||||
|
let quoted = parse(tokens)?;
|
||||||
|
Ok(Cons(Box::new(SymbolAtom(format!("quote"))), Box::new(Cons(Box::new(quoted), Box::new(Nil)))))
|
||||||
|
},
|
||||||
|
Some(NumLiteral(n)) => Ok(NumberAtom(n)),
|
||||||
|
None => Err(format!("Unexpected end of input")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_sexp(tokens: &mut Peekable<IntoIter<Token>>) -> Result<Sexp, String> {
|
||||||
|
use self::Token::*;
|
||||||
|
use self::Sexp::*;
|
||||||
|
let mut cell = Nil;
|
||||||
|
{
|
||||||
|
let mut cell_ptr = &mut cell;
|
||||||
|
loop {
|
||||||
|
match tokens.peek() {
|
||||||
|
None => return Err(format!("Unexpected end of input")),
|
||||||
|
Some(&RParen) => {
|
||||||
|
tokens.next();
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let current = parse(tokens)?;
|
||||||
|
let new_cdr = Cons(Box::new(current), Box::new(Nil));
|
||||||
|
match cell_ptr {
|
||||||
|
&mut Cons(_, ref mut cdr) => **cdr = new_cdr,
|
||||||
|
&mut Nil => *cell_ptr = new_cdr,
|
||||||
|
_ => unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
let old_ptr = cell_ptr;
|
||||||
|
let new_ptr: &mut Sexp = match old_ptr { &mut Cons(_, ref mut cdr) => cdr, _ => unreachable!() } as &mut Sexp;
|
||||||
|
cell_ptr = new_ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(cell)
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,133 @@
|
||||||
|
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use schala_lang::{tokenizing, parsing};
|
||||||
|
use self::tokenizing::*;
|
||||||
|
use self::parsing::*;
|
||||||
|
|
||||||
|
use schala_lang::tokenizing::TokenType::*;
|
||||||
|
|
||||||
|
struct AutoParser {
|
||||||
|
tokens: Vec<Token>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/* BNF
|
||||||
|
* all terminals in this BNF refer to TokenType values
|
||||||
|
|
||||||
|
literal := Kw::True | Kw::False | StrLiteral | number_literal
|
||||||
|
number_literal := int_literal | float_literal
|
||||||
|
float_literal := digits float_continued
|
||||||
|
float_continued := ε | Period digits
|
||||||
|
int_literal := HexLiteral | nonhex_int
|
||||||
|
nonhex_int := BinNumberSigil+ digits
|
||||||
|
digits := (DigitGroup Underscore)+
|
||||||
|
*/
|
||||||
|
|
||||||
|
impl AutoParser {
|
||||||
|
fn new(tokens: Vec<Token>) -> AutoParser {
|
||||||
|
AutoParser { tokens: tokens.into_iter().rev().collect() }
|
||||||
|
}
|
||||||
|
fn peek(&mut self) -> TokenType {
|
||||||
|
self.tokens.last().map(|ref t| { t.token_type.clone() }).unwrap_or(TokenType::EOF)
|
||||||
|
}
|
||||||
|
fn next(&mut self) -> TokenType {
|
||||||
|
self.tokens.pop().map(|t| { t.token_type }).unwrap_or(TokenType::EOF)
|
||||||
|
}
|
||||||
|
fn parse(&mut self) -> (Result<AST, ParseError>, Vec<String>) {
|
||||||
|
let ast = self.program();
|
||||||
|
(ast, vec![])
|
||||||
|
}
|
||||||
|
fn program(&mut self) -> ParseResult<AST> {
|
||||||
|
let etype = self.literal()?;
|
||||||
|
Ok(AST(vec![Statement::ExpressionStatement(Expression(etype, None))]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! expand_match_var {
|
||||||
|
(($pat:pat => $e:expr)) => { $pat };
|
||||||
|
(nonterm ($pat:pat => $e:expr)) => { $pat };
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! expand_match_expr {
|
||||||
|
($self:ident, ($pat:pat => $e:expr)) => {
|
||||||
|
{ $self.next(); $e }
|
||||||
|
};
|
||||||
|
($self:ident, nonterm ($pat:pat => $e:expr)) => {
|
||||||
|
{ $self.next(); $e }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! bnf_rule {
|
||||||
|
($self:ident, $type:ty, $rule:ident := $( $rule_clauses:tt )|*) => {
|
||||||
|
fn $rule(&mut $self) -> ParseResult<$type> {
|
||||||
|
Ok(match $self.peek() {
|
||||||
|
$(
|
||||||
|
expand_match_var!($rule_clauses) => expand_match_expr!($self, $rule_clauses),
|
||||||
|
)*
|
||||||
|
_ => return ParseError::new("Not found"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AutoParser {
|
||||||
|
bnf_rule!(self, ExpressionType, literal :=
|
||||||
|
(Keyword(Kw::True) => ExpressionType::BoolLiteral(true)) |
|
||||||
|
(Keyword(Kw::False) => ExpressionType::BoolLiteral(false))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Schala { }
|
||||||
|
|
||||||
|
impl Schala {
|
||||||
|
pub fn new() -> Schala {
|
||||||
|
Schala { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgrammingLanguageInterface for Schala {
|
||||||
|
fn get_language_name(&self) -> String {
|
||||||
|
"Schala-autoparser".to_string()
|
||||||
|
}
|
||||||
|
fn get_source_file_suffix(&self) -> String {
|
||||||
|
format!("schala")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||||
|
let mut output = LanguageOutput::default();
|
||||||
|
|
||||||
|
let tokens = tokenizing::tokenize(input);
|
||||||
|
if options.debug_tokens {
|
||||||
|
let token_string = tokens.iter().map(|t| format!("{:?}<L:{},C:{}>", t.token_type, t.offset.0, t.offset.1)).join(", ");
|
||||||
|
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", token_string)));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||||
|
if token_errors.len() != 0 {
|
||||||
|
output.add_output(format!("Tokenization error: {:?}\n", token_errors));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut parser = AutoParser::new(tokens);
|
||||||
|
|
||||||
|
let ast = match parser.parse() {
|
||||||
|
(Ok(ast), trace) => {
|
||||||
|
if options.debug_parse {
|
||||||
|
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||||
|
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||||
|
}
|
||||||
|
ast
|
||||||
|
},
|
||||||
|
(Err(err), trace) => {
|
||||||
|
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||||
|
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
output.add_output(format!("{:?}", ast));
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use schala_lang::typechecking::{Type, TypeResult, TConst};
|
||||||
|
use self::Type::*; use self::TConst::*;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct BinOp {
|
||||||
|
sigil: Rc<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BinOp {
|
||||||
|
pub fn from_sigil(sigil: &str) -> BinOp {
|
||||||
|
BinOp { sigil: Rc::new(sigil.to_string()) }
|
||||||
|
}
|
||||||
|
pub fn sigil(&self) -> &Rc<String> {
|
||||||
|
&self.sigil
|
||||||
|
}
|
||||||
|
pub fn get_type(&self) -> TypeResult<Type> {
|
||||||
|
let s = self.sigil.as_str();
|
||||||
|
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
|
||||||
|
}
|
||||||
|
pub fn min_precedence() -> i32 {
|
||||||
|
i32::min_value()
|
||||||
|
}
|
||||||
|
pub fn get_precedence(op: &str) -> i32 {
|
||||||
|
let default = 10_000_000;
|
||||||
|
BINOPS.get(op).map(|x| x.2.clone()).unwrap_or(default)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct PrefixOp {
|
||||||
|
sigil: Rc<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrefixOp {
|
||||||
|
pub fn from_sigil(sigil: &str) -> PrefixOp {
|
||||||
|
PrefixOp { sigil: Rc::new(sigil.to_string()) }
|
||||||
|
}
|
||||||
|
pub fn sigil(&self) -> &Rc<String> {
|
||||||
|
&self.sigil
|
||||||
|
}
|
||||||
|
pub fn is_prefix(op: &str) -> bool {
|
||||||
|
PREFIX_OPS.get(op).is_some()
|
||||||
|
}
|
||||||
|
pub fn get_type(&self) -> TypeResult<Type> {
|
||||||
|
let s = self.sigil.as_str();
|
||||||
|
PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lazy_static! {
|
||||||
|
static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> =
|
||||||
|
hashmap! {
|
||||||
|
"+" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
|
||||||
|
"-" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
|
||||||
|
"!" => (Func(bx!(Const(Bool)), bx!(Const(Bool))), ()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* the second tuple member is a placeholder for when I want to make evaluation rules tied to the
|
||||||
|
* binop definition */
|
||||||
|
lazy_static! {
|
||||||
|
static ref BINOPS: HashMap<&'static str, (Type, (), i32)> =
|
||||||
|
hashmap! {
|
||||||
|
"+" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
|
||||||
|
"-" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
|
||||||
|
"*" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
"/" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Float))))), (), 20),
|
||||||
|
"//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
"%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
"++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30),
|
||||||
|
"^" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
"&" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
"|" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,317 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use schala_lang::parsing::{AST, Statement, Declaration, Expression, Variant, ExpressionType};
|
||||||
|
use schala_lang::builtin::{BinOp, PrefixOp};
|
||||||
|
|
||||||
|
pub struct State<'a> {
|
||||||
|
parent_frame: Option<&'a State<'a>>,
|
||||||
|
values: HashMap<Rc<String>, ValueEntry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> State<'a> {
|
||||||
|
|
||||||
|
fn insert(&mut self, name: Rc<String>, value: ValueEntry) {
|
||||||
|
self.values.insert(name, value);
|
||||||
|
}
|
||||||
|
fn lookup(&self, name: &Rc<String>) -> Option<&ValueEntry> {
|
||||||
|
match (self.values.get(name), self.parent_frame) {
|
||||||
|
(None, None) => None,
|
||||||
|
(None, Some(parent)) => parent.lookup(name),
|
||||||
|
(Some(value), _) => Some(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum ValueEntry {
|
||||||
|
Binding {
|
||||||
|
val: FullyEvaluatedExpr,
|
||||||
|
},
|
||||||
|
Function {
|
||||||
|
param_names: Vec<Rc<String>>,
|
||||||
|
body: Vec<Statement>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type EvalResult<T> = Result<T, String>;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
enum FullyEvaluatedExpr {
|
||||||
|
UnsignedInt(u64),
|
||||||
|
SignedInt(i64),
|
||||||
|
Float(f64),
|
||||||
|
Str(String),
|
||||||
|
Bool(bool),
|
||||||
|
FuncLit(Rc<String>),
|
||||||
|
Custom {
|
||||||
|
string_rep: Rc<String>,
|
||||||
|
},
|
||||||
|
Tuple(Vec<FullyEvaluatedExpr>),
|
||||||
|
List(Vec<FullyEvaluatedExpr>)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FullyEvaluatedExpr {
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
match self {
|
||||||
|
&UnsignedInt(ref n) => format!("{}", n),
|
||||||
|
&SignedInt(ref n) => format!("{}", n),
|
||||||
|
&Float(ref f) => format!("{}", f),
|
||||||
|
&Str(ref s) => format!("\"{}\"", s),
|
||||||
|
&Bool(ref b) => format!("{}", b),
|
||||||
|
&Custom { ref string_rep } => format!("{}", string_rep),
|
||||||
|
&Tuple(ref items) => {
|
||||||
|
let mut buf = String::new();
|
||||||
|
write!(buf, "(").unwrap();
|
||||||
|
for term in items.iter().map(|e| Some(e)).intersperse(None) {
|
||||||
|
match term {
|
||||||
|
Some(e) => write!(buf, "{}", e.to_string()).unwrap(),
|
||||||
|
None => write!(buf, ", ").unwrap(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
write!(buf, ")").unwrap();
|
||||||
|
buf
|
||||||
|
},
|
||||||
|
&FuncLit(ref name) => format!("<function {}>", name),
|
||||||
|
&List(ref items) => {
|
||||||
|
let mut buf = String::new();
|
||||||
|
write!(buf, "[").unwrap();
|
||||||
|
for term in items.iter().map(|e| Some(e)).intersperse(None) {
|
||||||
|
match term {
|
||||||
|
Some(e) => write!(buf, "{}", e.to_string()).unwrap(),
|
||||||
|
None => write!(buf, ", ").unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write!(buf, "]").unwrap();
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> State<'a> {
|
||||||
|
pub fn new() -> State<'a> {
|
||||||
|
State { parent_frame: None, values: HashMap::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_parent(parent: &'a State<'a>) -> State<'a> {
|
||||||
|
State { parent_frame: Some(parent), values: HashMap::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn evaluate(&mut self, ast: AST) -> Vec<String> {
|
||||||
|
let mut acc = vec![];
|
||||||
|
for statement in ast.0 {
|
||||||
|
match self.eval_statement(statement) {
|
||||||
|
Ok(output) => {
|
||||||
|
if let Some(fully_evaluated) = output {
|
||||||
|
acc.push(fully_evaluated.to_string());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(error) => {
|
||||||
|
acc.push(format!("Eval error: {}", error));
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> State<'a> {
|
||||||
|
fn eval_statement(&mut self, statement: Statement) -> EvalResult<Option<FullyEvaluatedExpr>> {
|
||||||
|
Ok(match statement {
|
||||||
|
Statement::ExpressionStatement(expr) => Some(self.eval_expr(expr)?),
|
||||||
|
Statement::Declaration(decl) => { self.eval_decl(decl)?; None }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> {
|
||||||
|
use self::Declaration::*;
|
||||||
|
use self::Variant::*;
|
||||||
|
|
||||||
|
match decl {
|
||||||
|
FuncDecl(signature, statements) => {
|
||||||
|
let name = signature.name;
|
||||||
|
let param_names: Vec<Rc<String>> = signature.params.iter().map(|fp| fp.0.clone()).collect();
|
||||||
|
self.insert(name, ValueEntry::Function { body: statements.clone(), param_names });
|
||||||
|
},
|
||||||
|
TypeDecl(_name, body) => {
|
||||||
|
for variant in body.0.iter() {
|
||||||
|
match variant {
|
||||||
|
&UnitStruct(ref name) => self.insert(name.clone(),
|
||||||
|
ValueEntry::Binding { val: FullyEvaluatedExpr::Custom { string_rep: name.clone() } }),
|
||||||
|
&TupleStruct(ref _name, ref _args) => unimplemented!(),
|
||||||
|
&Record(ref _name, ref _fields) => unimplemented!(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Binding { name, expr, ..} => {
|
||||||
|
let val = self.eval_expr(expr)?;
|
||||||
|
self.insert(name.clone(), ValueEntry::Binding { val });
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Declaration evaluation not yet implemented"))
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_expr(&mut self, expr: Expression) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::ExpressionType::*;
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
|
||||||
|
let expr_type = expr.0;
|
||||||
|
match expr_type {
|
||||||
|
IntLiteral(n) => Ok(UnsignedInt(n)),
|
||||||
|
FloatLiteral(f) => Ok(Float(f)),
|
||||||
|
StringLiteral(s) => Ok(Str(s.to_string())),
|
||||||
|
BoolLiteral(b) => Ok(Bool(b)),
|
||||||
|
PrefixExp(op, expr) => self.eval_prefix_exp(op, expr),
|
||||||
|
BinExp(op, lhs, rhs) => self.eval_binexp(op, lhs, rhs),
|
||||||
|
Value(name) => self.eval_value(name),
|
||||||
|
TupleLiteral(expressions) => {
|
||||||
|
let mut evals = Vec::new();
|
||||||
|
for expr in expressions {
|
||||||
|
match self.eval_expr(expr) {
|
||||||
|
Ok(fully_evaluated) => evals.push(fully_evaluated),
|
||||||
|
error => return error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Tuple(evals))
|
||||||
|
}
|
||||||
|
Call { f, arguments } => {
|
||||||
|
let mut evaled_arguments = Vec::new();
|
||||||
|
for arg in arguments.into_iter() {
|
||||||
|
evaled_arguments.push(self.eval_expr(arg)?);
|
||||||
|
}
|
||||||
|
self.eval_application(*f, evaled_arguments)
|
||||||
|
},
|
||||||
|
Index { box indexee, indexers } => {
|
||||||
|
let evaled = self.eval_expr(indexee)?;
|
||||||
|
match evaled {
|
||||||
|
Tuple(mut exprs) => {
|
||||||
|
let len = indexers.len();
|
||||||
|
if len == 1 {
|
||||||
|
let idx = indexers.into_iter().nth(0).unwrap();
|
||||||
|
match self.eval_expr(idx)? {
|
||||||
|
UnsignedInt(n) if (n as usize) < exprs.len() => Ok(exprs.drain(n as usize..).next().unwrap()),
|
||||||
|
UnsignedInt(n) => Err(format!("Index {} out of range", n)),
|
||||||
|
other => Err(format!("{:?} is not an unsigned integer", other)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(format!("Tuple index must be one integer"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => Err(format!("Bad index expression"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ListLiteral(items) => Ok(List(items.into_iter().map(|item| self.eval_expr(item)).collect::<Result<Vec<_>,_>>()?)),
|
||||||
|
x => Err(format!("Unimplemented thing {:?}", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_application(&mut self, f: Expression, arguments: Vec<FullyEvaluatedExpr>) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::ExpressionType::*;
|
||||||
|
match f {
|
||||||
|
Expression(Value(ref identifier), _) if self.is_builtin(identifier) => self.eval_builtin(identifier, arguments),
|
||||||
|
Expression(Value(identifier), _) => {
|
||||||
|
match self.lookup(&identifier) {
|
||||||
|
Some(&ValueEntry::Function { ref body, ref param_names }) => {
|
||||||
|
if arguments.len() != param_names.len() {
|
||||||
|
return Err(format!("Wrong number of arguments for the function"));
|
||||||
|
}
|
||||||
|
let mut new_state = State::new_with_parent(self);
|
||||||
|
let sub_ast = body.clone();
|
||||||
|
for (param, val) in param_names.iter().zip(arguments.into_iter()) {
|
||||||
|
new_state.insert(param.clone(), ValueEntry::Binding { val });
|
||||||
|
}
|
||||||
|
let mut ret: Option<FullyEvaluatedExpr> = None;
|
||||||
|
for statement in sub_ast.into_iter() {
|
||||||
|
ret = new_state.eval_statement(statement)?;
|
||||||
|
}
|
||||||
|
Ok(ret.unwrap_or(FullyEvaluatedExpr::Custom { string_rep: Rc::new("()".to_string()) }))
|
||||||
|
},
|
||||||
|
_ => Err(format!("Function {} not found", identifier)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
x => Err(format!("Trying to apply {:?} which is not a function", x)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn is_builtin(&self, name: &Rc<String>) -> bool {
|
||||||
|
match &name.as_ref()[..] {
|
||||||
|
"print" | "println" => true,
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn eval_builtin(&mut self, name: &Rc<String>, args: Vec<FullyEvaluatedExpr>) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
match &name.as_ref()[..] {
|
||||||
|
"print" => {
|
||||||
|
for arg in args {
|
||||||
|
print!("{}", arg.to_string());
|
||||||
|
}
|
||||||
|
Ok(Tuple(vec![]))
|
||||||
|
},
|
||||||
|
"println" => {
|
||||||
|
for arg in args {
|
||||||
|
println!("{}", arg.to_string());
|
||||||
|
}
|
||||||
|
Ok(Tuple(vec![]))
|
||||||
|
},
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::ValueEntry::*;
|
||||||
|
match self.lookup(&name) {
|
||||||
|
None => return Err(format!("Value {} not found", *name)),
|
||||||
|
Some(lookup) => match lookup {
|
||||||
|
&Binding { ref val } => Ok(val.clone()),
|
||||||
|
&Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_binexp(&mut self, op: BinOp, lhs: Box<Expression>, rhs: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
let evaled_lhs = self.eval_expr(*lhs)?;
|
||||||
|
let evaled_rhs = self.eval_expr(*rhs)?;
|
||||||
|
let sigil = op.sigil();
|
||||||
|
//let sigil: &str = op.sigil().as_ref().as_str();
|
||||||
|
Ok(match (sigil.as_str(), evaled_lhs, evaled_rhs) {
|
||||||
|
("+", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l + r),
|
||||||
|
("++", Str(s1), Str(s2)) => Str(format!("{}{}", s1, s2)),
|
||||||
|
("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r),
|
||||||
|
("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r),
|
||||||
|
("/", UnsignedInt(l), UnsignedInt(r)) => Float((l as f64)/ (r as f64)),
|
||||||
|
("//", UnsignedInt(l), UnsignedInt(r)) => if r == 0 {
|
||||||
|
return Err(format!("Runtime error: divide by zero"));
|
||||||
|
} else {
|
||||||
|
UnsignedInt(l / r)
|
||||||
|
},
|
||||||
|
("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r),
|
||||||
|
("^", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l ^ r),
|
||||||
|
("&", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l & r),
|
||||||
|
("|", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l | r),
|
||||||
|
_ => return Err(format!("Runtime error: not yet implemented")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_prefix_exp(&mut self, op: PrefixOp, expr: Box<Expression>) -> EvalResult<FullyEvaluatedExpr> {
|
||||||
|
use self::FullyEvaluatedExpr::*;
|
||||||
|
let evaled_expr = self.eval_expr(*expr)?;
|
||||||
|
let sigil = op.sigil();
|
||||||
|
|
||||||
|
Ok(match (sigil.as_str(), evaled_expr) {
|
||||||
|
("!", Bool(true)) => Bool(false),
|
||||||
|
("!", Bool(false)) => Bool(true),
|
||||||
|
("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)),
|
||||||
|
("-", SignedInt(n)) => SignedInt(-1*(n as i64)),
|
||||||
|
("+", SignedInt(n)) => SignedInt(n),
|
||||||
|
("+", UnsignedInt(n)) => UnsignedInt(n),
|
||||||
|
_ => return Err(format!("Runtime error: not yet implemented")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,104 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use schala_lib::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, LanguageOutput};
|
||||||
|
|
||||||
|
macro_rules! bx {
|
||||||
|
($e:expr) => { Box::new($e) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod autoparser;
|
||||||
|
|
||||||
|
mod builtin;
|
||||||
|
|
||||||
|
mod tokenizing;
|
||||||
|
mod parsing;
|
||||||
|
mod typechecking;
|
||||||
|
mod eval;
|
||||||
|
|
||||||
|
use self::typechecking::{TypeContext};
|
||||||
|
|
||||||
|
pub struct Schala {
|
||||||
|
state: eval::State<'static>,
|
||||||
|
type_context: TypeContext
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Schala {
|
||||||
|
pub fn new() -> Schala {
|
||||||
|
Schala {
|
||||||
|
state: eval::State::new(),
|
||||||
|
type_context: TypeContext::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgrammingLanguageInterface for Schala {
|
||||||
|
fn get_language_name(&self) -> String {
|
||||||
|
"Schala".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_source_file_suffix(&self) -> String {
|
||||||
|
format!("schala")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> LanguageOutput {
|
||||||
|
let mut output = LanguageOutput::default();
|
||||||
|
let tokens = tokenizing::tokenize(input);
|
||||||
|
if options.debug_tokens {
|
||||||
|
let token_string = tokens.iter().map(|t| format!("{:?}<L:{},C:{}>", t.token_type, t.offset.0, t.offset.1)).join(", ");
|
||||||
|
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", token_string)));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let token_errors: Vec<&String> = tokens.iter().filter_map(|t| t.get_error()).collect();
|
||||||
|
if token_errors.len() != 0 {
|
||||||
|
output.add_output(format!("Tokenization error: {:?}\n", token_errors));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ast = match parsing::parse(tokens) {
|
||||||
|
(Ok(ast), trace) => {
|
||||||
|
if options.debug_parse {
|
||||||
|
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||||
|
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||||
|
}
|
||||||
|
ast
|
||||||
|
},
|
||||||
|
(Err(err), trace) => {
|
||||||
|
output.add_artifact(TraceArtifact::new_parse_trace(trace));
|
||||||
|
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match self.type_context.add_top_level_types(&ast) {
|
||||||
|
Ok(()) => (),
|
||||||
|
Err(msg) => {
|
||||||
|
output.add_artifact(TraceArtifact::new("type_check", msg));
|
||||||
|
//return output
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if options.debug_symbol_table {
|
||||||
|
let text = self.type_context.debug_symbol_table();
|
||||||
|
output.add_artifact(TraceArtifact::new("symbol_table", text));
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.type_context.type_check_ast(&ast) {
|
||||||
|
Ok(ty) => {
|
||||||
|
output.add_artifact(TraceArtifact::new("type_check", format!("{:?}", ty)));
|
||||||
|
},
|
||||||
|
Err(msg) => {
|
||||||
|
output.add_artifact(TraceArtifact::new("type_check", msg));
|
||||||
|
/*
|
||||||
|
output.add_output(format!("Type error"));
|
||||||
|
return output;
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let evaluation_outputs = self.state.evaluate(ast);
|
||||||
|
let text_output: String = evaluation_outputs.into_iter().intersperse(format!("\n")).collect();
|
||||||
|
output.add_output(text_output);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,264 @@
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::iter::{Iterator, Peekable};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum TokenType {
|
||||||
|
Newline, Semicolon,
|
||||||
|
|
||||||
|
LParen, RParen,
|
||||||
|
LSquareBracket, RSquareBracket,
|
||||||
|
LAngleBracket, RAngleBracket,
|
||||||
|
LCurlyBrace, RCurlyBrace,
|
||||||
|
Pipe,
|
||||||
|
|
||||||
|
Comma, Period, Colon, Underscore,
|
||||||
|
|
||||||
|
Operator(Rc<String>),
|
||||||
|
DigitGroup(Rc<String>), HexLiteral(Rc<String>), BinNumberSigil,
|
||||||
|
StrLiteral(Rc<String>),
|
||||||
|
Identifier(Rc<String>),
|
||||||
|
Keyword(Kw),
|
||||||
|
|
||||||
|
EOF,
|
||||||
|
|
||||||
|
Error(String),
|
||||||
|
}
|
||||||
|
use self::TokenType::*;
|
||||||
|
|
||||||
|
impl fmt::Display for TokenType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
&Operator(ref s) => write!(f, "Operator({})", **s),
|
||||||
|
&DigitGroup(ref s) => write!(f, "DigitGroup({})", s),
|
||||||
|
&HexLiteral(ref s) => write!(f, "HexLiteral({})", s),
|
||||||
|
&StrLiteral(ref s) => write!(f, "StrLiteral({})", s),
|
||||||
|
&Identifier(ref s) => write!(f, "Identifier({})", s),
|
||||||
|
&Error(ref s) => write!(f, "Error({})", s),
|
||||||
|
other => write!(f, "{:?}", other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum Kw {
|
||||||
|
If, Else,
|
||||||
|
Func,
|
||||||
|
For,
|
||||||
|
Match,
|
||||||
|
Var, Const, Let, In,
|
||||||
|
Return,
|
||||||
|
Alias, Type, SelfType, SelfIdent,
|
||||||
|
Trait, Impl,
|
||||||
|
True, False,
|
||||||
|
Module
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref KEYWORDS: HashMap<&'static str, Kw> =
|
||||||
|
hashmap! {
|
||||||
|
"if" => Kw::If,
|
||||||
|
"else" => Kw::Else,
|
||||||
|
"fn" => Kw::Func,
|
||||||
|
"for" => Kw::For,
|
||||||
|
"match" => Kw::Match,
|
||||||
|
"var" => Kw::Var,
|
||||||
|
"const" => Kw::Const,
|
||||||
|
"let" => Kw::Let,
|
||||||
|
"in" => Kw::In,
|
||||||
|
"return" => Kw::Return,
|
||||||
|
"alias" => Kw::Alias,
|
||||||
|
"type" => Kw::Type,
|
||||||
|
"Self" => Kw::SelfType,
|
||||||
|
"self" => Kw::SelfIdent,
|
||||||
|
"trait" => Kw::Trait,
|
||||||
|
"impl" => Kw::Impl,
|
||||||
|
"true" => Kw::True,
|
||||||
|
"false" => Kw::False,
|
||||||
|
"module" => Kw::Module,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Token {
|
||||||
|
pub token_type: TokenType,
|
||||||
|
pub offset: (usize, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Token {
|
||||||
|
pub fn get_error(&self) -> Option<&String> {
|
||||||
|
match self.token_type {
|
||||||
|
TokenType::Error(ref s) => Some(s),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn to_string_with_metadata(&self) -> String {
|
||||||
|
format!("{}(L:{},c:{})", self.token_type, self.offset.0, self.offset.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const OPERATOR_CHARS: [char; 19] = ['!', '$', '%', '&', '*', '+', '-', '.', '/', ':', '<', '>', '=', '?', '@', '^', '|', '~', '`'];
|
||||||
|
fn is_operator(c: &char) -> bool {
|
||||||
|
OPERATOR_CHARS.iter().any(|x| x == c)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CharIter<I: Iterator<Item=(usize,usize,char)>> = Peekable<I>;
|
||||||
|
|
||||||
|
pub fn tokenize(input: &str) -> Vec<Token> {
|
||||||
|
let mut tokens: Vec<Token> = Vec::new();
|
||||||
|
|
||||||
|
let mut input = input.lines().enumerate()
|
||||||
|
.flat_map(|(line_idx, ref line)| {
|
||||||
|
line.chars().enumerate().map(move |(ch_idx, ch)| (line_idx, ch_idx, ch))
|
||||||
|
}).peekable();
|
||||||
|
|
||||||
|
while let Some((line_idx, ch_idx, c)) = input.next() {
|
||||||
|
let cur_tok_type = match c {
|
||||||
|
'#' => {
|
||||||
|
if let Some(&(_, _, '{')) = input.peek() {
|
||||||
|
} else {
|
||||||
|
while let Some((_, _, c)) = input.next() {
|
||||||
|
if c == '\n' {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
c if c.is_whitespace() && c != '\n' => continue,
|
||||||
|
'\n' => Newline, ';' => Semicolon,
|
||||||
|
':' => Colon, ',' => Comma,
|
||||||
|
'(' => LParen, ')' => RParen,
|
||||||
|
'{' => LCurlyBrace, '}' => RCurlyBrace,
|
||||||
|
'[' => LSquareBracket, ']' => RSquareBracket,
|
||||||
|
'"' => handle_quote(&mut input),
|
||||||
|
c if c.is_digit(10) => handle_digit(c, &mut input),
|
||||||
|
c if c.is_alphabetic() || c == '_' => handle_alphabetic(c, &mut input), //TODO I'll probably have to rewrite this if I care about types being uppercase, also type parameterization
|
||||||
|
c if is_operator(&c) => handle_operator(c, &mut input),
|
||||||
|
unknown => Error(format!("Unexpected character: {}", unknown)),
|
||||||
|
};
|
||||||
|
tokens.push(Token { token_type: cur_tok_type, offset: (line_idx, ch_idx) });
|
||||||
|
}
|
||||||
|
tokens
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_digit<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||||
|
if c == '0' && input.peek().map_or(false, |&(_, _, c)| { c == 'x' }) {
|
||||||
|
input.next();
|
||||||
|
let rest: String = input.peeking_take_while(|&(_, _, ref c)| c.is_digit(16) || *c == '_').map(|(_, _, c)| { c }).collect();
|
||||||
|
HexLiteral(Rc::new(rest))
|
||||||
|
} else if c == '0' && input.peek().map_or(false, |&(_, _, c)| { c == 'b' }) {
|
||||||
|
input.next();
|
||||||
|
BinNumberSigil
|
||||||
|
} else {
|
||||||
|
let mut buf = c.to_string();
|
||||||
|
buf.extend(input.peeking_take_while(|&(_, _, ref c)| c.is_digit(10)).map(|(_, _, c)| { c }));
|
||||||
|
DigitGroup(Rc::new(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_quote<I: Iterator<Item=(usize,usize,char)>>(input: &mut CharIter<I>) -> TokenType {
|
||||||
|
let mut buf = String::new();
|
||||||
|
loop {
|
||||||
|
match input.next().map(|(_, _, c)| { c }) {
|
||||||
|
Some('"') => break,
|
||||||
|
Some('\\') => {
|
||||||
|
let next = input.peek().map(|&(_, _, c)| { c });
|
||||||
|
if next == Some('n') {
|
||||||
|
input.next();
|
||||||
|
buf.push('\n')
|
||||||
|
} else if next == Some('"') {
|
||||||
|
input.next();
|
||||||
|
buf.push('"');
|
||||||
|
} else if next == Some('t') {
|
||||||
|
input.next();
|
||||||
|
buf.push('\t');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(c) => buf.push(c),
|
||||||
|
None => return TokenType::Error(format!("Unclosed string")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TokenType::StrLiteral(Rc::new(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_alphabetic<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||||
|
let mut buf = String::new();
|
||||||
|
buf.push(c);
|
||||||
|
if c == '_' && input.peek().map(|&(_, _, c)| { !c.is_alphabetic() }).unwrap_or(true) {
|
||||||
|
return TokenType::Underscore
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match input.peek().map(|&(_, _, c)| { c }) {
|
||||||
|
Some(c) if c.is_alphanumeric() => {
|
||||||
|
input.next();
|
||||||
|
buf.push(c);
|
||||||
|
},
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match KEYWORDS.get(buf.as_str()) {
|
||||||
|
Some(kw) => TokenType::Keyword(*kw),
|
||||||
|
None => TokenType::Identifier(Rc::new(buf)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_operator<I: Iterator<Item=(usize,usize,char)>>(c: char, input: &mut CharIter<I>) -> TokenType {
|
||||||
|
match c {
|
||||||
|
'<' | '>' | '|' | '.' => {
|
||||||
|
let ref next = input.peek().map(|&(_, _, c)| { c });
|
||||||
|
if !next.map(|n| { is_operator(&n) }).unwrap_or(false) {
|
||||||
|
return match c {
|
||||||
|
'<' => LAngleBracket,
|
||||||
|
'>' => RAngleBracket,
|
||||||
|
'|' => Pipe,
|
||||||
|
'.' => Period,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
buf.push(c);
|
||||||
|
loop {
|
||||||
|
match input.peek().map(|&(_, _, c)| { c }) {
|
||||||
|
Some(c) if is_operator(&c) => {
|
||||||
|
input.next();
|
||||||
|
buf.push(c);
|
||||||
|
},
|
||||||
|
_ => break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TokenType::Operator(Rc::new(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod schala_tokenizer_tests {
|
||||||
|
use super::*;
|
||||||
|
use super::Kw::*;
|
||||||
|
|
||||||
|
macro_rules! digit { ($ident:expr) => { DigitGroup(Rc::new($ident.to_string())) } }
|
||||||
|
macro_rules! ident { ($ident:expr) => { Identifier(Rc::new($ident.to_string())) } }
|
||||||
|
macro_rules! op { ($ident:expr) => { Operator(Rc::new($ident.to_string())) } }
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tokens() {
|
||||||
|
let a = tokenize("let a: A<B> = c ++ d");
|
||||||
|
let token_types: Vec<TokenType> = a.into_iter().map(move |t| t.token_type).collect();
|
||||||
|
assert_eq!(token_types, vec![Keyword(Let), ident!("a"), Colon, ident!("A"),
|
||||||
|
LAngleBracket, ident!("B"), RAngleBracket, op!("="), ident!("c"), op!("++"), ident!("d")]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn underscores() {
|
||||||
|
let token_types: Vec<TokenType> = tokenize("4_8").into_iter().map(move |t| t.token_type).collect();
|
||||||
|
assert_eq!(token_types, vec![digit!("4"), Underscore, digit!("8")]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,445 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
|
use schala_lang::parsing::{AST, Statement, Declaration, Signature, Expression, ExpressionType, Operation, Variant, TypeName, TypeSingletonName};
|
||||||
|
|
||||||
|
// from Niko's talk
|
||||||
|
/* fn type_check(expression, expected_ty) -> Ty {
|
||||||
|
let ty = bare_type_check(expression, expected_type);
|
||||||
|
if ty icompatible with expected_ty {
|
||||||
|
try_coerce(expression, ty, expected_ty)
|
||||||
|
} else {
|
||||||
|
ty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bare_type_check(exprssion, expected_type) -> Ty { ... }
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* H-M ALGO NOTES
|
||||||
|
from https://www.youtube.com/watch?v=il3gD7XMdmA
|
||||||
|
(also check out http://dev.stephendiehl.com/fun/006_hindley_milner.html)
|
||||||
|
|
||||||
|
typeInfer :: Expr a -> Matching (Type a)
|
||||||
|
unify :: Type a -> Type b -> Matching (Type c)
|
||||||
|
|
||||||
|
(Matching a) is a monad in which unification is done
|
||||||
|
|
||||||
|
ex:
|
||||||
|
|
||||||
|
typeInfer (If e1 e2 e3) = do
|
||||||
|
t1 <- typeInfer e1
|
||||||
|
t2 <- typeInfer e2
|
||||||
|
t3 <- typeInfer e3
|
||||||
|
_ <- unify t1 BoolType
|
||||||
|
unify t2 t3 -- b/c t2 and t3 have to be the same type
|
||||||
|
|
||||||
|
typeInfer (Const (ConstInt _)) = IntType -- same for other literals
|
||||||
|
|
||||||
|
--function application
|
||||||
|
typeInfer (Apply f x) = do
|
||||||
|
tf <- typeInfer f
|
||||||
|
tx <- typeInfer x
|
||||||
|
case tf of
|
||||||
|
FunctionType t1 t2 -> do
|
||||||
|
_ <- unify t1 tx
|
||||||
|
return t2
|
||||||
|
_ -> fail "Not a function"
|
||||||
|
|
||||||
|
--type annotation
|
||||||
|
typeInfer (Typed x t) = do
|
||||||
|
tx <- typeInfer x
|
||||||
|
unify tx t
|
||||||
|
|
||||||
|
--variable and let expressions - need to pass around a map of variable names to types here
|
||||||
|
typeInfer :: [ (Var, Type Var) ] -> Expr Var -> Matching (Type Var)
|
||||||
|
|
||||||
|
typeInfer ctx (Var x) = case (lookup x ctx) of
|
||||||
|
Just t -> return t
|
||||||
|
Nothing -> fail "Unknown variable"
|
||||||
|
|
||||||
|
--let x = e1 in e2
|
||||||
|
typeInfer ctx (Let x e1 e2) = do
|
||||||
|
t1 <- typeInfer ctx e1
|
||||||
|
typeInfer ((x, t1) :: ctx) e2
|
||||||
|
|
||||||
|
--lambdas are complicated (this represents ʎx.e)
|
||||||
|
typeInfer ctx (Lambda x e) = do
|
||||||
|
t1 <- allocExistentialVariable
|
||||||
|
t2 <- typeInfer ((x, t1) :: ctx) e
|
||||||
|
return $ FunctionType t1 t2 -- ie. t1 -> t2
|
||||||
|
|
||||||
|
|
||||||
|
--to solve the problem of map :: (a -> b) -> [a] -> [b]
|
||||||
|
when we use a variable whose type has universal tvars, convert those universal
|
||||||
|
tvars to existential ones
|
||||||
|
-and each distinct universal tvar needs to map to the same existential type
|
||||||
|
|
||||||
|
-so we change typeinfer:
|
||||||
|
typeInfer ctx (Var x) = do
|
||||||
|
case (lookup x ctx) of
|
||||||
|
Nothing -> ...
|
||||||
|
Just t -> do
|
||||||
|
let uvars = nub (toList t) -- nub removes duplicates, so this gets unique universally quantified variables
|
||||||
|
evars <- mapM (const allocExistentialVariable) uvars
|
||||||
|
let varMap = zip uvars evars
|
||||||
|
let vixVar varMap v = fromJust $ lookup v varMap
|
||||||
|
return (fmap (fixVar varMap) t)
|
||||||
|
|
||||||
|
--how do we define unify??
|
||||||
|
|
||||||
|
-recall, type signature is:
|
||||||
|
unify :: Type a -> Type b -> Matching (Type c)
|
||||||
|
unify BoolType BoolType = BoolType --easy, same for all constants
|
||||||
|
unify (FunctionType t1 t2) (FunctionType t3 t4) = do
|
||||||
|
t5 <- unify t1 t3
|
||||||
|
t6 <- unify t2 t4
|
||||||
|
return $ FunctionType t5 t6
|
||||||
|
unify (TVar a) (TVar b) = if a == b then TVar a else fail
|
||||||
|
--existential types can be assigned another type at most once
|
||||||
|
--some complicated stuff about hanlding existential types
|
||||||
|
--everything else is a type error
|
||||||
|
unify a b = fail
|
||||||
|
|
||||||
|
|
||||||
|
SKOLEMIZATION - how you prevent an unassigned existential type variable from leaking!
|
||||||
|
-before a type gets to global scope, replace all unassigned existential vars w/ new unique universal
|
||||||
|
type variables
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum Type {
|
||||||
|
TVar(TypeVar),
|
||||||
|
TConst(TypeConst),
|
||||||
|
TFunc(Box<Type>, Box<Type>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum TypeVar {
|
||||||
|
Univ(Rc<String>),
|
||||||
|
Exist(u64),
|
||||||
|
}
|
||||||
|
impl TypeVar {
|
||||||
|
fn univ(label: &str) -> TypeVar {
|
||||||
|
TypeVar::Univ(Rc::new(label.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum TypeConst {
|
||||||
|
UserT(Rc<String>),
|
||||||
|
Integer,
|
||||||
|
Float,
|
||||||
|
StringT,
|
||||||
|
Boolean,
|
||||||
|
Unit,
|
||||||
|
Bottom,
|
||||||
|
}
|
||||||
|
|
||||||
|
type TypeCheckResult = Result<Type, String>;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||||
|
struct PathSpecifier(Rc<String>);
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
struct TypeContextEntry {
|
||||||
|
ty: Type,
|
||||||
|
constant: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TypeContext {
|
||||||
|
symbol_table: HashMap<PathSpecifier, TypeContextEntry>,
|
||||||
|
evar_table: HashMap<u64, Type>,
|
||||||
|
existential_type_label_count: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TypeContext {
|
||||||
|
pub fn new() -> TypeContext {
|
||||||
|
TypeContext {
|
||||||
|
symbol_table: HashMap::new(),
|
||||||
|
evar_table: HashMap::new(),
|
||||||
|
existential_type_label_count: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_symbols(&mut self, ast: &AST) {
|
||||||
|
use self::Declaration::*;
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
|
||||||
|
for statement in ast.0.iter() {
|
||||||
|
match *statement {
|
||||||
|
Statement::ExpressionStatement(_) => (),
|
||||||
|
Statement::Declaration(ref decl) => match *decl {
|
||||||
|
FuncSig(_) => (),
|
||||||
|
Impl { .. } => (),
|
||||||
|
TypeDecl(ref type_constructor, ref body) => {
|
||||||
|
for variant in body.0.iter() {
|
||||||
|
let (spec, ty) = match variant {
|
||||||
|
&Variant::UnitStruct(ref data_constructor) => {
|
||||||
|
let spec = PathSpecifier(data_constructor.clone());
|
||||||
|
let ty = TConst(UserT(type_constructor.name.clone()));
|
||||||
|
(spec, ty)
|
||||||
|
},
|
||||||
|
&Variant::TupleStruct(ref data_construcor, ref args) => {
|
||||||
|
//TODO fix
|
||||||
|
let arg = args.get(0).unwrap();
|
||||||
|
let type_arg = self.from_anno(arg);
|
||||||
|
let spec = PathSpecifier(data_construcor.clone());
|
||||||
|
let ty = TFunc(Box::new(type_arg), Box::new(TConst(UserT(type_constructor.name.clone()))));
|
||||||
|
(spec, ty)
|
||||||
|
},
|
||||||
|
&Variant::Record(_, _) => unimplemented!(),
|
||||||
|
};
|
||||||
|
let entry = TypeContextEntry { ty, constant: true };
|
||||||
|
self.symbol_table.insert(spec, entry);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
TypeAlias { .. } => (),
|
||||||
|
Binding {ref name, ref constant, ref expr} => {
|
||||||
|
let spec = PathSpecifier(name.clone());
|
||||||
|
let ty = expr.1.as_ref()
|
||||||
|
.map(|ty| self.from_anno(ty))
|
||||||
|
.unwrap_or_else(|| { self.alloc_existential_type() }); // this call to alloc_existential is OK b/c a binding only ever has one type, so if the annotation is absent, it's fine to just make one de novo
|
||||||
|
let entry = TypeContextEntry { ty, constant: *constant };
|
||||||
|
self.symbol_table.insert(spec, entry);
|
||||||
|
},
|
||||||
|
FuncDecl(ref signature, _) => {
|
||||||
|
let spec = PathSpecifier(signature.name.clone());
|
||||||
|
let ty = self.from_signature(signature);
|
||||||
|
let entry = TypeContextEntry { ty, constant: true };
|
||||||
|
self.symbol_table.insert(spec, entry);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn lookup(&mut self, binding: &Rc<String>) -> Option<TypeContextEntry> {
|
||||||
|
let key = PathSpecifier(binding.clone());
|
||||||
|
self.symbol_table.get(&key).map(|entry| entry.clone())
|
||||||
|
}
|
||||||
|
pub fn debug_symbol_table(&self) -> String {
|
||||||
|
format!("Symbol table:\n {:?}\nEvar table:\n{:?}", self.symbol_table, self.evar_table)
|
||||||
|
}
|
||||||
|
fn alloc_existential_type(&mut self) -> Type {
|
||||||
|
let ret = Type::TVar(TypeVar::Exist(self.existential_type_label_count));
|
||||||
|
self.existential_type_label_count += 1;
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_anno(&mut self, anno: &TypeName) -> Type {
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
|
||||||
|
match anno {
|
||||||
|
&TypeName::Singleton(TypeSingletonName { ref name, .. }) => {
|
||||||
|
match name.as_ref().as_ref() {
|
||||||
|
"Int" => TConst(Integer),
|
||||||
|
"Float" => TConst(Float),
|
||||||
|
"Bool" => TConst(Boolean),
|
||||||
|
"String" => TConst(StringT),
|
||||||
|
s => TVar(TypeVar::Univ(Rc::new(format!("{}",s)))),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&TypeName::Tuple(ref items) => {
|
||||||
|
if items.len() == 1 {
|
||||||
|
TConst(Unit)
|
||||||
|
} else {
|
||||||
|
TConst(Bottom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_signature(&mut self, sig: &Signature) -> Type {
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
|
||||||
|
//TODO this won't work properly until you make sure that all (universal) type vars in the function have the same existential type var
|
||||||
|
// actually this should never even put existential types into the symbol table at all
|
||||||
|
|
||||||
|
//this will crash if more than 5 arg function is used
|
||||||
|
let names = vec!["a", "b", "c", "d", "e", "f"];
|
||||||
|
let mut idx = 0;
|
||||||
|
|
||||||
|
let mut get_type = || { let q = TVar(TypeVar::Univ(Rc::new(format!("{}", names.get(idx).unwrap())))); idx += 1; q };
|
||||||
|
|
||||||
|
let return_type = sig.type_anno.as_ref().map(|anno| self.from_anno(&anno)).unwrap_or_else(|| { get_type() });
|
||||||
|
if sig.params.len() == 0 {
|
||||||
|
TFunc(Box::new(TConst(Unit)), Box::new(return_type))
|
||||||
|
} else {
|
||||||
|
let mut output_type = return_type;
|
||||||
|
for p in sig.params.iter() {
|
||||||
|
let p_type = p.1.as_ref().map(|anno| self.from_anno(anno)).unwrap_or_else(|| { get_type() });
|
||||||
|
output_type = TFunc(Box::new(p_type), Box::new(output_type));
|
||||||
|
}
|
||||||
|
output_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn type_check(&mut self, ast: &AST) -> TypeCheckResult {
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
|
||||||
|
let mut last = TConst(Unit);
|
||||||
|
|
||||||
|
for statement in ast.0.iter() {
|
||||||
|
match statement {
|
||||||
|
&Statement::Declaration(ref _decl) => {
|
||||||
|
//return Err(format!("Declarations not supported"));
|
||||||
|
},
|
||||||
|
&Statement::ExpressionStatement(ref expr) => {
|
||||||
|
last = self.infer(expr)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(last)
|
||||||
|
}
|
||||||
|
fn infer(&mut self, expr: &Expression) -> TypeCheckResult {
|
||||||
|
match (&expr.0, &expr.1) {
|
||||||
|
(exprtype, &Some(ref anno)) => {
|
||||||
|
let tx = self.infer_no_anno(exprtype)?;
|
||||||
|
let ty = self.from_anno(anno);
|
||||||
|
self.unify(tx, ty)
|
||||||
|
},
|
||||||
|
(exprtype, &None) => self.infer_no_anno(exprtype),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn infer_no_anno(&mut self, ex: &ExpressionType) -> TypeCheckResult {
|
||||||
|
use self::ExpressionType::*;
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
|
||||||
|
Ok(match ex {
|
||||||
|
&IntLiteral(_) => TConst(Integer),
|
||||||
|
&FloatLiteral(_) => TConst(Float),
|
||||||
|
&StringLiteral(_) => TConst(StringT),
|
||||||
|
&BoolLiteral(_) => TConst(Boolean),
|
||||||
|
&Value(ref name, _) => {
|
||||||
|
self.lookup(name)
|
||||||
|
.map(|entry| entry.ty)
|
||||||
|
.ok_or(format!("Couldn't find {}", name))?
|
||||||
|
},
|
||||||
|
&BinExp(ref op, ref lhs, ref rhs) => {
|
||||||
|
let t_lhs = self.infer(lhs)?;
|
||||||
|
match self.infer_op(op)? {
|
||||||
|
TFunc(t1, t2) => {
|
||||||
|
let _ = self.unify(t_lhs, *t1)?;
|
||||||
|
let t_rhs = self.infer(rhs)?;
|
||||||
|
let x = *t2;
|
||||||
|
match x {
|
||||||
|
TFunc(t3, t4) => {
|
||||||
|
let _ = self.unify(t_rhs, *t3)?;
|
||||||
|
*t4
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Not a function type either")),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Op {:?} is not a function type", op)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&Call { ref f, ref arguments } => {
|
||||||
|
let tf = self.infer(f)?;
|
||||||
|
let targ = self.infer(arguments.get(0).unwrap())?;
|
||||||
|
match tf {
|
||||||
|
TFunc(box t1, box t2) => {
|
||||||
|
let _ = self.unify(t1, targ)?;
|
||||||
|
t2
|
||||||
|
},
|
||||||
|
_ => return Err(format!("Not a function!")),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => TConst(Bottom),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn infer_op(&mut self, op: &Operation) -> TypeCheckResult {
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeConst::*;
|
||||||
|
macro_rules! binoptype {
|
||||||
|
($lhs:expr, $rhs:expr, $out:expr) => { TFunc(Box::new($lhs), Box::new(TFunc(Box::new($rhs), Box::new($out)))) };
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(match (*op.0).as_ref() {
|
||||||
|
"+" => binoptype!(TConst(Integer), TConst(Integer), TConst(Integer)),
|
||||||
|
"++" => binoptype!(TConst(StringT), TConst(StringT), TConst(StringT)),
|
||||||
|
"-" => binoptype!(TConst(Integer), TConst(Integer), TConst(Integer)),
|
||||||
|
"*" => binoptype!(TConst(Integer), TConst(Integer), TConst(Integer)),
|
||||||
|
"/" => binoptype!(TConst(Integer), TConst(Integer), TConst(Integer)),
|
||||||
|
"%" => binoptype!(TConst(Integer), TConst(Integer), TConst(Integer)),
|
||||||
|
_ => TConst(Bottom)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unify(&mut self, t1: Type, t2: Type) -> TypeCheckResult {
|
||||||
|
use self::Type::*;
|
||||||
|
use self::TypeVar::*;
|
||||||
|
|
||||||
|
println!("Calling unify with `{:?}` and `{:?}`", t1, t2);
|
||||||
|
|
||||||
|
match (&t1, &t2) {
|
||||||
|
(&TConst(ref c1), &TConst(ref c2)) if c1 == c2 => Ok(TConst(c1.clone())),
|
||||||
|
(&TFunc(ref t1, ref t2), &TFunc(ref t3, ref t4)) => {
|
||||||
|
let t5 = self.unify(*t1.clone().clone(), *t3.clone().clone())?;
|
||||||
|
let t6 = self.unify(*t2.clone().clone(), *t4.clone().clone())?;
|
||||||
|
Ok(TFunc(Box::new(t5), Box::new(t6)))
|
||||||
|
},
|
||||||
|
(&TVar(Univ(ref a)), &TVar(Univ(ref b))) => {
|
||||||
|
if a == b {
|
||||||
|
Ok(TVar(Univ(a.clone())))
|
||||||
|
} else {
|
||||||
|
Err(format!("Couldn't unify universal types {} and {}", a, b))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
//the interesting case!!
|
||||||
|
(&TVar(Exist(ref a)), ref t2) => {
|
||||||
|
let x = self.evar_table.get(a).map(|x| x.clone());
|
||||||
|
match x {
|
||||||
|
Some(ref t1) => self.unify(t1.clone().clone(), t2.clone().clone()),
|
||||||
|
None => {
|
||||||
|
self.evar_table.insert(*a, t2.clone().clone());
|
||||||
|
Ok(t2.clone().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(ref t1, &TVar(Exist(ref a))) => {
|
||||||
|
let x = self.evar_table.get(a).map(|x| x.clone());
|
||||||
|
match x {
|
||||||
|
Some(ref t2) => self.unify(t2.clone().clone(), t1.clone().clone()),
|
||||||
|
None => {
|
||||||
|
self.evar_table.insert(*a, t1.clone().clone());
|
||||||
|
Ok(t1.clone().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => Err(format!("Types {:?} and {:?} don't unify", t1, t2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{Type, TypeVar, TypeConst, TypeContext};
|
||||||
|
use super::Type::*;
|
||||||
|
use super::TypeConst::*;
|
||||||
|
use schala_lang::parsing::{parse, tokenize};
|
||||||
|
|
||||||
|
macro_rules! type_test {
|
||||||
|
($input:expr, $correct:expr) => {
|
||||||
|
{
|
||||||
|
let mut tc = TypeContext::new();
|
||||||
|
let ast = parse(tokenize($input)).0.unwrap() ;
|
||||||
|
tc.add_symbols(&ast);
|
||||||
|
assert_eq!($correct, tc.type_check(&ast).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_inference() {
|
||||||
|
type_test!("30", TConst(Integer));
|
||||||
|
type_test!("fn x(a: Int): Bool {}; x(1)", TConst(Boolean));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,254 @@
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::char;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use schala_lang::parsing;
|
||||||
|
|
||||||
|
pub struct TypeContext {
|
||||||
|
type_var_count: u64,
|
||||||
|
bindings: HashMap<Rc<String>, Type>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum Type {
|
||||||
|
Const(TConst),
|
||||||
|
Sum(Vec<Type>),
|
||||||
|
Func(Box<Type>, Box<Type>),
|
||||||
|
UVar(String),
|
||||||
|
EVar(u64),
|
||||||
|
Void
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Type {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
use self::Type::*;
|
||||||
|
match self {
|
||||||
|
&Const(ref c) => write!(f, "{:?}", c),
|
||||||
|
&Sum(ref types) => {
|
||||||
|
write!(f, "(")?;
|
||||||
|
for item in types.iter().map(|ty| Some(ty)).intersperse(None) {
|
||||||
|
match item {
|
||||||
|
Some(ty) => write!(f, "{}", ty)?,
|
||||||
|
None => write!(f, ",")?,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
write!(f, ")")
|
||||||
|
},
|
||||||
|
&Func(ref a, ref b) => write!(f, "{} -> {}", a, b),
|
||||||
|
&UVar(ref s) => write!(f, "{}_u", s),
|
||||||
|
&EVar(ref n) => write!(f, "{}_e", n),
|
||||||
|
&Void => write!(f, "Void")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct UVarGenerator {
|
||||||
|
n: u32,
|
||||||
|
}
|
||||||
|
impl UVarGenerator {
|
||||||
|
fn new() -> UVarGenerator {
|
||||||
|
UVarGenerator::default()
|
||||||
|
}
|
||||||
|
fn next(&mut self) -> Type {
|
||||||
|
//TODO handle this in the case where someone wants to make a function with more than 26 variables
|
||||||
|
let s = format!("{}", unsafe { char::from_u32_unchecked(self.n + ('a' as u32)) });
|
||||||
|
self.n += 1;
|
||||||
|
Type::UVar(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum TConst {
|
||||||
|
Unit,
|
||||||
|
Int,
|
||||||
|
Float,
|
||||||
|
StringT,
|
||||||
|
Bool,
|
||||||
|
Custom(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl parsing::TypeName {
|
||||||
|
fn to_type(&self) -> TypeResult<Type> {
|
||||||
|
use self::parsing::TypeSingletonName;
|
||||||
|
use self::parsing::TypeName::*;
|
||||||
|
use self::Type::*; use self::TConst::*;
|
||||||
|
Ok(match self {
|
||||||
|
&Tuple(_) => return Err(format!("Tuples not yet implemented")),
|
||||||
|
&Singleton(ref name) => match name {
|
||||||
|
&TypeSingletonName { ref name, .. } => match &name[..] {
|
||||||
|
"Int" => Const(Int),
|
||||||
|
"Float" => Const(Float),
|
||||||
|
"Bool" => Const(Bool),
|
||||||
|
"String" => Const(StringT),
|
||||||
|
n => Const(Custom(n.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type TypeResult<T> = Result<T, String>;
|
||||||
|
|
||||||
|
impl TypeContext {
|
||||||
|
pub fn new() -> TypeContext {
|
||||||
|
TypeContext { bindings: HashMap::new(), type_var_count: 0 }
|
||||||
|
}
|
||||||
|
pub fn fresh(&mut self) -> Type {
|
||||||
|
let ret = self.type_var_count;
|
||||||
|
self.type_var_count += 1;
|
||||||
|
Type::EVar(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TypeContext {
|
||||||
|
pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> {
|
||||||
|
use self::parsing::TypeName;
|
||||||
|
use self::parsing::Declaration::*;
|
||||||
|
use self::Type::*;
|
||||||
|
for statement in ast.0.iter() {
|
||||||
|
if let &self::parsing::Statement::Declaration(ref decl) = statement {
|
||||||
|
match decl {
|
||||||
|
&FuncSig(ref signature) | &FuncDecl(ref signature, _) => {
|
||||||
|
let mut uvar_gen = UVarGenerator::new();
|
||||||
|
let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
||||||
|
for &(_, ref type_name) in signature.params.iter().rev() {
|
||||||
|
let arg_type = type_name.as_ref().map(|name| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
||||||
|
ty = Func(bx!(arg_type), bx!(ty));
|
||||||
|
}
|
||||||
|
self.bindings.insert(signature.name.clone(), ty);
|
||||||
|
},
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn debug_symbol_table(&self) -> String {
|
||||||
|
let mut output = format!("Symbols\n");
|
||||||
|
for (sym, ty) in &self.bindings {
|
||||||
|
write!(output, "{} : {}\n", sym, ty).unwrap();
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TypeContext {
|
||||||
|
pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult<Type> {
|
||||||
|
use self::Type::*; use self::TConst::*;
|
||||||
|
let mut ret_type = Const(Unit);
|
||||||
|
for statement in ast.0.iter() {
|
||||||
|
ret_type = self.type_check_statement(statement)?;
|
||||||
|
}
|
||||||
|
Ok(ret_type)
|
||||||
|
}
|
||||||
|
fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult<Type> {
|
||||||
|
use self::parsing::Statement::*;
|
||||||
|
match statement {
|
||||||
|
&ExpressionStatement(ref expr) => self.infer(expr),
|
||||||
|
&Declaration(ref decl) => self.add_declaration(decl),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn add_declaration(&mut self, decl: &parsing::Declaration) -> TypeResult<Type> {
|
||||||
|
use self::parsing::Declaration::*;
|
||||||
|
use self::Type::*;
|
||||||
|
match decl {
|
||||||
|
&Binding { ref name, ref expr, .. } => {
|
||||||
|
let ty = self.infer(expr)?;
|
||||||
|
self.bindings.insert(name.clone(), ty);
|
||||||
|
},
|
||||||
|
_ => return Err(format!("other formats not done"))
|
||||||
|
}
|
||||||
|
Ok(Void)
|
||||||
|
}
|
||||||
|
fn infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
|
||||||
|
use self::parsing::Expression;
|
||||||
|
match expr {
|
||||||
|
&Expression(ref e, Some(ref anno)) => {
|
||||||
|
let anno_ty = anno.to_type()?;
|
||||||
|
let ty = self.infer_exprtype(&e)?;
|
||||||
|
self.unify(ty, anno_ty)
|
||||||
|
},
|
||||||
|
&Expression(ref e, None) => self.infer_exprtype(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn infer_exprtype(&mut self, expr: &parsing::ExpressionType) -> TypeResult<Type> {
|
||||||
|
use self::parsing::ExpressionType::*;
|
||||||
|
use self::Type::*; use self::TConst::*;
|
||||||
|
match expr {
|
||||||
|
&IntLiteral(_) => Ok(Const(Int)),
|
||||||
|
&FloatLiteral(_) => Ok(Const(Float)),
|
||||||
|
&StringLiteral(_) => Ok(Const(StringT)),
|
||||||
|
&BoolLiteral(_) => Ok(Const(Bool)),
|
||||||
|
&BinExp(ref op, ref lhs, ref rhs) => { /* remember there are both the haskell convention talk and the write you a haskell ways to do this! */
|
||||||
|
match op.get_type()? {
|
||||||
|
Func(box t1, box Func(box t2, box t3)) => {
|
||||||
|
let lhs_ty = self.infer(lhs)?;
|
||||||
|
let rhs_ty = self.infer(rhs)?;
|
||||||
|
self.unify(t1, lhs_ty)?;
|
||||||
|
self.unify(t2, rhs_ty)?;
|
||||||
|
Ok(t3)
|
||||||
|
},
|
||||||
|
other => Err(format!("{:?} is not a binary function type", other))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&PrefixExp(ref op, ref expr) => match op.get_type()? {
|
||||||
|
Func(box t1, box t2) => {
|
||||||
|
let expr_ty = self.infer(expr)?;
|
||||||
|
self.unify(t1, expr_ty)?;
|
||||||
|
Ok(t2)
|
||||||
|
},
|
||||||
|
other => Err(format!("{:?} is not a prefix op function type", other))
|
||||||
|
},
|
||||||
|
&Value(ref name) => {
|
||||||
|
match self.bindings.get(name) {
|
||||||
|
Some(ty) => Ok(ty.clone()),
|
||||||
|
None => Err(format!("No binding found for variable: {}", name)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&Call { ref f, ref arguments } => {
|
||||||
|
let mut tf = self.infer(f)?;
|
||||||
|
for arg in arguments.iter() {
|
||||||
|
match tf {
|
||||||
|
Func(box t, box rest) => {
|
||||||
|
let t_arg = self.infer(arg)?;
|
||||||
|
self.unify(t, t_arg)?;
|
||||||
|
tf = rest;
|
||||||
|
},
|
||||||
|
other => return Err(format!("Function call failed to unify; last type: {:?}", other)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(tf)
|
||||||
|
},
|
||||||
|
&TupleLiteral(ref expressions) => {
|
||||||
|
let mut types = vec![];
|
||||||
|
for expr in expressions {
|
||||||
|
types.push(self.infer(expr)?);
|
||||||
|
}
|
||||||
|
Ok(Sum(types))
|
||||||
|
},
|
||||||
|
/*
|
||||||
|
Index {
|
||||||
|
indexee: Box<Expression>,
|
||||||
|
indexers: Vec<Expression>,
|
||||||
|
},
|
||||||
|
IfExpression(Box<Expression>, Vec<Statement>, Option<Vec<Statement>>),
|
||||||
|
MatchExpression(Box<Expression>, Vec<MatchArm>),
|
||||||
|
ForExpression
|
||||||
|
*/
|
||||||
|
_ => Err(format!("Type not yet implemented"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
|
||||||
|
use self::Type::*;// use self::TConst::*;
|
||||||
|
match (t1, t2) {
|
||||||
|
(Const(ref a), Const(ref b)) if a == b => Ok(Const(a.clone())),
|
||||||
|
(a, b) => Err(format!("Types {:?} and {:?} don't unify", a, b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Schala Metainterpreter Web Evaluator</title>
|
||||||
|
<style>
|
||||||
|
.CodeArea {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="main">
|
||||||
|
</div>
|
||||||
|
<script src="bundle.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,64 @@
|
||||||
|
const React = require("react");
|
||||||
|
const ReactDOM = require("react-dom");
|
||||||
|
const superagent = require("superagent");
|
||||||
|
|
||||||
|
const serverAddress = "http://localhost:8000";
|
||||||
|
|
||||||
|
class CodeArea extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {value: "", lastOutput: null};
|
||||||
|
this.handleChange = this.handleChange.bind(this);
|
||||||
|
this.submit = this.submit.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange(event) {
|
||||||
|
this.setState({value: event.target.value});
|
||||||
|
}
|
||||||
|
|
||||||
|
submit(event) {
|
||||||
|
console.log("Event", this.state.value);
|
||||||
|
const source = this.state.value;
|
||||||
|
|
||||||
|
superagent.post(`${serverAddress}/input`)
|
||||||
|
.send({ source })
|
||||||
|
.set("accept", "json")
|
||||||
|
.end((error, response) => {
|
||||||
|
if (response) {
|
||||||
|
console.log("Resp", response);
|
||||||
|
this.setState({lastOutput: response.body.text})
|
||||||
|
} else {
|
||||||
|
console.error("Error: ", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderOutput() {
|
||||||
|
if (!this.state.lastOutput) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return <textarea readOnly value={ this.state.lastOutput } />;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (<div className="CodeArea">
|
||||||
|
<div className="input">
|
||||||
|
<textarea value={ this.state.value } onChange={this.handleChange}>
|
||||||
|
</textarea>
|
||||||
|
<button onClick={ this.submit }>Run!</button>
|
||||||
|
</div>
|
||||||
|
<div className="output">
|
||||||
|
{ this.renderOutput() }
|
||||||
|
</div>
|
||||||
|
</div>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const main = (<div>
|
||||||
|
<h1>Schala web input</h1>
|
||||||
|
<p>Write your source code here</p>
|
||||||
|
<CodeArea/>
|
||||||
|
</div>);
|
||||||
|
|
||||||
|
const rootDom = document.getElementById("main");
|
||||||
|
ReactDOM.render(main, rootDom);
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"name": "static",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"babel": "^6.23.0",
|
||||||
|
"babel-preset-es2015": "^6.24.1",
|
||||||
|
"babel-preset-react": "^6.24.1",
|
||||||
|
"babelify": "^7.3.0",
|
||||||
|
"browserify": "^14.4.0",
|
||||||
|
"react": "^15.6.1",
|
||||||
|
"react-dom": "^15.6.1",
|
||||||
|
"superagent": "^3.6.3",
|
||||||
|
"uglify-js": "^3.1.1"
|
||||||
|
},
|
||||||
|
"babel": {
|
||||||
|
"presets": [
|
||||||
|
"babel-preset-react",
|
||||||
|
"babel-preset-es2015"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "browserify main.jsx -t babelify -o bundle.js",
|
||||||
|
"build-minify": "browserify main.jsx -t babelify | uglifyjs > bundle.js"
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue