├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── evaluate ├── builtin.rs ├── evaluate.rs ├── mod.rs └── object.rs ├── lexer ├── lexer.rs └── mod.rs ├── main.rs ├── parser ├── ast.rs ├── mod.rs └── parser.rs ├── repl ├── mod.rs └── repl.rs └── tokens ├── mod.rs └── tokens.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Build 13 | run: cargo build --verbose 14 | - name: Run tests 15 | run: cargo test --verbose 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | .idea/ -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "monkey_interpreter" 5 | version = "1.0.0" 6 | 7 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "monkey_interpreter" 3 | version = "1.0.0" 4 | authors = ["Wessel van Lit "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-monkey 2 | ![Monkey](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png) 3 | 4 | A rust version of the Monkey Interpreter from "Writing an Interpreter in Go" 5 | 6 | Written without any external libraries, lexers or parsers. 7 | 8 | Inspired by [this repo](https://github.com/Rydgel/monkey-rust). 9 | 10 | ## REPL 11 | The REPL is started when running the repo: 12 | 13 | `cargo run` 14 | ## Tests 15 | All tests from "Writing an Interpreter in Go" have been copied. 16 | 17 | ## Implemented Features 18 | Rust-Monkey supports: 19 | * Variable bindings, 20 | * Prefix and Infix operators 21 | * First-class and Higher-order Functions 22 | * Closures 23 | * Integers, Booleans, Strings 24 | * Immutable Arrays 25 | * Hashes 26 | 27 | ## Planned Features 28 | * While Loops 29 | * For Loops 30 | -------------------------------------------------------------------------------- /src/evaluate/builtin.rs: -------------------------------------------------------------------------------- 1 | use crate::evaluate::object::*; 2 | use crate::parser::ast::*; 3 | 4 | pub type BuiltinFunction = fn(Vec) -> Result; 5 | 6 | pub fn get_builtin_functions() -> Vec<(Identifier, Object)> { 7 | vec![ 8 | create_builtin("len", 1, builtin_len), 9 | create_builtin("first", 1, builtin_first), 10 | create_builtin("last", 1, builtin_last), 11 | create_builtin("rest", 1, builtin_rest), 12 | create_builtin("puts", -1, builtin_puts), 13 | ] 14 | } 15 | 16 | fn create_builtin(name: &str, param_count: i32, function: BuiltinFunction) -> (Identifier, Object) { 17 | ( 18 | name.to_string(), 19 | Object::Builtin(name.to_string(), param_count, function), 20 | ) 21 | } 22 | 23 | fn builtin_len(input: Vec) -> Result { 24 | let input_object = &input[0]; 25 | match input_object { 26 | Object::Array(elem) => Ok(Object::Integer(elem.len() as i32)), 27 | Object::String(s) => Ok(Object::Integer(s.len() as i32)), 28 | _ => Err(format!( 29 | "argument to 'len' not supported, got {}", 30 | input_object.type_string() 31 | )), 32 | } 33 | } 34 | 35 | fn builtin_first(input: Vec) -> Result { 36 | let input_object = &input[0]; 37 | match input_object { 38 | Object::Array(elem) => { 39 | if elem.len() > 0 { 40 | Ok(elem[0].clone()) 41 | } else { 42 | Ok(Object::Null) 43 | } 44 | } 45 | _ => Err(format!( 46 | "argument to 'first' not supported, got {}", 47 | input_object.type_string() 48 | )), 49 | } 50 | } 51 | 52 | fn builtin_last(input: Vec) -> Result { 53 | let input_object = &input[0]; 54 | match input_object { 55 | Object::Array(elem) => { 56 | if elem.len() > 0 { 57 | Ok(elem[elem.len() - 1].clone()) 58 | } else { 59 | Ok(Object::Null) 60 | } 61 | } 62 | _ => Err(format!( 63 | "argument to 'last' not supported, got {}", 64 | input_object.type_string() 65 | )), 66 | } 67 | } 68 | 69 | fn builtin_rest(input: Vec) -> Result { 70 | let input_object = &input[0]; 71 | match input_object { 72 | Object::Array(elem) => { 73 | if elem.len() > 0 { 74 | let mut new_elem = elem.clone(); 75 | new_elem.remove(0); 76 | Ok(Object::Array(new_elem)) 77 | } else { 78 | Ok(Object::Null) 79 | } 80 | } 81 | _ => Err(format!( 82 | "argument to 'rest' not supported, got {}", 83 | input_object.type_string() 84 | )), 85 | } 86 | } 87 | 88 | fn builtin_puts(input: Vec) -> Result { 89 | for object in input.iter() { 90 | println!("{}", object.inspect()); 91 | } 92 | 93 | Ok(Object::Null) 94 | } 95 | -------------------------------------------------------------------------------- /src/evaluate/evaluate.rs: -------------------------------------------------------------------------------- 1 | use crate::evaluate::object::{Environment, Object}; 2 | use crate::parser::ast; 3 | use crate::tokens::tokens::TokenType; 4 | use std::cell::RefCell; 5 | use std::collections::HashMap; 6 | use std::rc::Rc; 7 | 8 | #[derive(Debug)] 9 | pub struct Evaluator { 10 | environment: Rc>, 11 | } 12 | 13 | impl Evaluator { 14 | pub fn new() -> Self { 15 | Evaluator { 16 | environment: Rc::new(RefCell::new(Environment::new())), 17 | } 18 | } 19 | 20 | pub fn new_with_env(env: Environment) -> Self { 21 | Evaluator { 22 | environment: Rc::new(RefCell::new(env)), 23 | } 24 | } 25 | } 26 | 27 | impl Evaluator { 28 | // add code here 29 | pub fn eval_program(&mut self, program: &mut ast::Program) -> Object { 30 | match program.statements.len() { 31 | 0 => Object::Null, 32 | 1 => { 33 | let obj = self.eval_statement(&mut program.statements.remove(0)); 34 | match obj { 35 | Object::Return(object) => return *object, 36 | _ => obj, 37 | } 38 | } 39 | _ => self.eval_statements(&mut program.statements), 40 | } 41 | } 42 | 43 | fn eval_statement(&mut self, statement: &mut ast::Statement) -> Object { 44 | match statement { 45 | ast::Statement::Expr(expr) => self.eval_expression(expr), 46 | ast::Statement::BlockStatement(vec) => self.eval_statements(vec), 47 | ast::Statement::Return(expr) => self.eval_return_statement(expr), 48 | ast::Statement::Let(ident, expr) => { 49 | let obj = self.eval_expression(expr); 50 | self.eval_let_statement(ident.clone(), obj) 51 | } 52 | _ => Object::Null, 53 | } 54 | } 55 | 56 | fn eval_statements(&mut self, statements: &mut Vec) -> Object { 57 | let mut result = Object::Null; 58 | 59 | for statement in statements.iter_mut() { 60 | result = self.eval_statement(statement); 61 | 62 | match result { 63 | Object::Return(obj) => return *obj, 64 | Object::Error(_) => return result, 65 | _ => (), 66 | }; 67 | } 68 | 69 | result 70 | } 71 | 72 | fn eval_return_statement(&mut self, expr: &mut ast::Expr) -> Object { 73 | let val = self.eval_expression(expr); 74 | if val.is_error() { 75 | val 76 | } else { 77 | val.return_object() 78 | } 79 | } 80 | 81 | fn eval_let_statement(&mut self, identifier: String, object: Object) -> Object { 82 | if object.is_error() { 83 | return object; 84 | } 85 | 86 | let mut env = self.environment.borrow_mut(); 87 | env.set(identifier, object.clone()); 88 | Object::Null 89 | } 90 | 91 | fn eval_expression(&mut self, expression: &mut ast::Expr) -> Object { 92 | match expression { 93 | ast::Expr::IntegerLiteral(i) => Object::Integer(*i), 94 | ast::Expr::Bool(b) => Object::Boolean(*b), 95 | ast::Expr::String(s) => Object::String(s.clone()), 96 | ast::Expr::ArrayLiteral(e) => self.eval_array(e), 97 | ast::Expr::Index(left, index) => { 98 | let left_obj = self.eval_expression(left); 99 | let index_obj = self.eval_expression(index); 100 | self.eval_index(left_obj, index_obj) 101 | } 102 | ast::Expr::HashLiteral(key_vec) => self.eval_hash_literal(key_vec), 103 | ast::Expr::Identifier(ident) => self.eval_identifier(ident.clone()), 104 | ast::Expr::Prefix(op, right) => { 105 | let right_obj = self.eval_expression(right); 106 | self.eval_prefix_expression(op, right_obj) 107 | } 108 | ast::Expr::Infix(op, left, right) => { 109 | let left_obj = self.eval_expression(left); 110 | let right_obj = self.eval_expression(right); 111 | self.eval_infix_expression(op, left_obj, right_obj) 112 | } 113 | ast::Expr::If(condition, then, else_option) => { 114 | self.eval_if_expression(condition, then, else_option) 115 | } 116 | ast::Expr::FunctionLiteral(parameters, body) => Object::Function { 117 | parameters: parameters.to_vec(), 118 | body: *body.clone(), 119 | env: self.environment.clone(), 120 | }, 121 | ast::Expr::CallExpression { 122 | function, 123 | arguments, 124 | } => self.eval_call_expression(function, arguments), 125 | _ => Object::Null, 126 | } 127 | } 128 | 129 | fn eval_expressions(&mut self, expressions: &mut Vec) -> Vec { 130 | let mut result = vec![]; 131 | for expression in expressions { 132 | let obj = self.eval_expression(expression); 133 | if obj.is_error() { 134 | return vec![obj]; 135 | } 136 | result.push(obj); 137 | } 138 | result 139 | } 140 | 141 | fn eval_identifier(&mut self, identifier: String) -> Object { 142 | let env = self.environment.borrow(); 143 | let optional = env.get(identifier.clone()); 144 | match optional { 145 | Some(obj) => obj.clone(), 146 | None => Object::Error(format!("identifier not found: {}", identifier)), 147 | } 148 | } 149 | 150 | fn eval_array(&mut self, expressions: &mut Vec) -> Object { 151 | let list = self.eval_expressions(expressions); 152 | if list[0].is_error() { 153 | return list[0].clone(); 154 | } 155 | 156 | Object::Array(list) 157 | } 158 | 159 | fn eval_index(&mut self, data: Object, index: Object) -> Object { 160 | if data.is_error() { 161 | return data; 162 | } 163 | 164 | if index.is_error() { 165 | return index; 166 | } 167 | 168 | match data { 169 | Object::Array(elem) => match index { 170 | Object::Integer(i) => { 171 | if ((elem.len() - 1) as i32) < i || i < 0 { 172 | return Object::Null; 173 | } 174 | 175 | elem[i as usize].clone() 176 | } 177 | _ => Object::Error(format!( 178 | "Index is not an Integer, instead found: {}", 179 | index.type_string() 180 | )), 181 | }, 182 | Object::Hash(map) => match map.get(&index) { 183 | Some(obj) => obj.clone(), 184 | None => Object::Null, 185 | }, 186 | _ => Object::Error(format!( 187 | "Expression is not an Array, instead found: {}", 188 | data.type_string() 189 | )), 190 | } 191 | } 192 | 193 | fn eval_hash_literal(&mut self, key_vec: &mut Vec<(ast::Expr, ast::Expr)>) -> Object { 194 | let mut map = HashMap::new(); 195 | 196 | for (key, value) in key_vec.iter_mut() { 197 | let key_obj = self.eval_expression(key); 198 | if key_obj.is_error() { 199 | return key_obj; 200 | } 201 | 202 | let value_obj = self.eval_expression(value); 203 | 204 | if value_obj.is_error() { 205 | return value_obj; 206 | } 207 | 208 | map.insert(key_obj, value_obj); 209 | } 210 | 211 | Object::Hash(map) 212 | } 213 | 214 | fn eval_prefix_expression(&self, operator: &String, right: Object) -> Object { 215 | if right.is_error() { 216 | return right; 217 | } 218 | 219 | match operator.as_str() { 220 | "!" => self.eval_bang_operator_expression(right), 221 | "-" => self.eval_minus_operator_expression(right), 222 | _ => Object::Error(format!( 223 | "unknown operator: {}{}", 224 | operator, 225 | right.type_string() 226 | )), 227 | } 228 | } 229 | 230 | fn eval_bang_operator_expression(&self, right: Object) -> Object { 231 | match right { 232 | Object::Boolean(val) => Object::Boolean(!val), 233 | Object::Integer(i) => Object::Boolean(i == 0), 234 | _ => Object::Error(format!("unknown operator: !{}", right.type_string())), 235 | } 236 | } 237 | 238 | fn eval_minus_operator_expression(&self, right: Object) -> Object { 239 | match right { 240 | Object::Integer(i) => Object::Integer(-i), 241 | _ => Object::Error(format!("unknown operator: -{}", right.type_string())), 242 | } 243 | } 244 | 245 | fn eval_infix_expression(&self, operator: &TokenType, left: Object, right: Object) -> Object { 246 | if left.is_error() { 247 | return left; 248 | } else if right.is_error() { 249 | return right; 250 | } 251 | 252 | let type_error = Object::Error(format!( 253 | "type mismatch: {} {} {}", 254 | left.type_string(), 255 | operator, 256 | right.type_string() 257 | )); 258 | 259 | match left { 260 | Object::Integer(i) => match right { 261 | Object::Integer(j) => self.eval_infix_integer_expression(operator, i, j), 262 | _ => type_error, 263 | }, 264 | Object::Boolean(b1) => match right { 265 | Object::Boolean(b2) => self.eval_infix_bool_expression(operator, b1, b2), 266 | _ => type_error, 267 | }, 268 | Object::String(s1) => match right { 269 | Object::String(s2) => self.eval_infix_string_expression(operator, s1, s2), 270 | _ => type_error, 271 | }, 272 | 273 | _ => type_error, 274 | } 275 | } 276 | 277 | fn eval_infix_integer_expression( 278 | &self, 279 | operator: &TokenType, 280 | left_val: i32, 281 | right_val: i32, 282 | ) -> Object { 283 | match operator { 284 | // Mathematical 285 | TokenType::PLUS => Object::Integer(left_val + right_val), 286 | TokenType::MINUS => Object::Integer(left_val - right_val), 287 | TokenType::ASTERISK => Object::Integer(left_val * right_val), 288 | TokenType::SLASH => Object::Integer(left_val / right_val), 289 | // Equality 290 | TokenType::LT => Object::Boolean(left_val < right_val), 291 | TokenType::GT => Object::Boolean(left_val > right_val), 292 | TokenType::EQ => Object::Boolean(left_val == right_val), 293 | TokenType::NOT_EQ => Object::Boolean(left_val != right_val), 294 | _ => Object::Null, 295 | } 296 | } 297 | 298 | fn eval_infix_bool_expression( 299 | &self, 300 | operator: &TokenType, 301 | left_val: bool, 302 | right_val: bool, 303 | ) -> Object { 304 | match operator { 305 | TokenType::EQ => Object::Boolean(left_val == right_val), 306 | TokenType::NOT_EQ => Object::Boolean(left_val != right_val), 307 | _ => Object::Error(format!("unknown operator: BOOLEAN {} BOOLEAN", operator)), 308 | } 309 | } 310 | 311 | fn eval_infix_string_expression( 312 | &self, 313 | operator: &TokenType, 314 | left_val: String, 315 | right_val: String, 316 | ) -> Object { 317 | match operator { 318 | TokenType::PLUS => Object::String(left_val + &right_val), 319 | _ => Object::Error(format!("unknown operator: STRING {} STRING", operator)), 320 | } 321 | } 322 | 323 | fn eval_if_expression( 324 | &mut self, 325 | mut condition: &mut Box, 326 | mut then: &mut Box, 327 | else_option: &mut Option>, 328 | ) -> Object { 329 | let condition_object = self.eval_expression(&mut condition); 330 | if condition_object.is_error() { 331 | return condition_object; 332 | } 333 | 334 | if self.is_truthy(condition_object) { 335 | self.eval_statement(&mut then) 336 | } else { 337 | match else_option { 338 | Some(stmnt) => self.eval_statement(stmnt), 339 | None => Object::Null, 340 | } 341 | } 342 | } 343 | 344 | fn eval_call_expression( 345 | &mut self, 346 | function: &mut Box, 347 | arguments: &mut Vec, 348 | ) -> Object { 349 | let function_object = self.eval_expression(function); 350 | if function_object.is_error() { 351 | return function_object; 352 | } 353 | 354 | let args = self.eval_expressions(arguments); 355 | if args.len() == 1 && args[0].is_error() { 356 | return args[0].clone(); 357 | } 358 | 359 | self.apply_function(function_object, args) 360 | } 361 | 362 | fn apply_function(&mut self, function_object: Object, args: Vec) -> Object { 363 | match function_object { 364 | Object::Function { 365 | parameters, 366 | mut body, 367 | env, 368 | } => { 369 | if parameters.len() != args.len() { 370 | return Object::Error(format!( 371 | "wrong number of arguments, got {} instead of {}", 372 | args.len(), 373 | parameters.len() 374 | )); 375 | } 376 | let extended_env = self.extend_environment(env, parameters, args); 377 | let mut evaluator = Evaluator::new_with_env(extended_env); 378 | let eval = evaluator.eval_statement(&mut body); 379 | match eval { 380 | Object::Return(val) => *val, 381 | _ => eval, 382 | } 383 | } 384 | Object::Builtin(_, paramcount, func) => { 385 | if paramcount != (args.len() as i32) && paramcount != -1 { 386 | return Object::Error(format!( 387 | "wrong number of arguments, got {} instead of {}", 388 | args.len(), 389 | paramcount 390 | )); 391 | }; 392 | match func(args) { 393 | Ok(obj) => obj, 394 | Err(e) => Object::Error(e), 395 | } 396 | } 397 | _ => Object::Error(format!("not a function: {}", function_object.type_string())), 398 | } 399 | } 400 | 401 | fn extend_environment( 402 | &mut self, 403 | outer: Rc>, 404 | params: Vec, 405 | args: Vec, 406 | ) -> Environment { 407 | let mut env = Environment::new_outer(outer); 408 | for (index, parameter) in params.iter().enumerate() { 409 | env.set(parameter.clone(), args[index].clone()); 410 | } 411 | 412 | env 413 | } 414 | 415 | fn is_truthy(&self, object: Object) -> bool { 416 | match object { 417 | Object::Boolean(b) => b, 418 | Object::Null => false, 419 | _ => match self.object_to_bool_object(object) { 420 | Object::Boolean(b) => b, 421 | _ => false, 422 | }, 423 | } 424 | } 425 | 426 | fn object_to_bool_object(&self, object: Object) -> Object { 427 | match object { 428 | Object::Integer(i) => Object::Boolean(i != 0), 429 | _ => Object::Null, 430 | } 431 | } 432 | } 433 | #[cfg(test)] 434 | mod tests { 435 | use super::*; 436 | use crate::lexer::lexer::Lexer; 437 | use crate::parser::parser::Parser; 438 | 439 | fn test_eval(input: String) -> Object { 440 | let l = Lexer::new(input); 441 | let mut p = Parser::new(l); 442 | let mut e = Evaluator::new(); 443 | e.eval_program(&mut p.parse_program()) 444 | } 445 | 446 | fn test_integer_object(object: Object, expected: i32) { 447 | match object { 448 | Object::Integer(i) => assert_eq!(i, expected), 449 | _ => assert!(false, "{:?} is not an integer!", object), 450 | } 451 | } 452 | 453 | fn test_boolean_object(object: Object, expected: bool) { 454 | match object { 455 | Object::Boolean(b) => assert_eq!(b, expected), 456 | _ => assert!(false, "{:?} is not a boolean!", object), 457 | } 458 | } 459 | 460 | fn test_object(object: Object, expected: &Object) { 461 | match expected { 462 | Object::Integer(i) => test_integer_object(object, *i), 463 | Object::Boolean(b) => test_boolean_object(object, *b), 464 | Object::Null => match object { 465 | Object::Null => (), 466 | _ => assert!(false, "Expected Null, got {:?}", object), 467 | }, 468 | _ => assert!(false, "{:?} not in test_object", expected), 469 | } 470 | } 471 | 472 | #[test] 473 | fn test_eval_integer_expression() { 474 | let input = vec![ 475 | ("5", 5), 476 | ("10", 10), 477 | ("-5", -5), 478 | ("-10", -10), 479 | ("5 + 5 - 2", 8), 480 | ("2 * 2 * 2 * 2", 16), 481 | ("-50 + 70", 20), 482 | ("5 * 5 + 2", 27), 483 | ("5 + 5 * 2", 15), 484 | ("20 / 5 * 2", 8), 485 | ("4 * (2 + 10)", 48), 486 | ("(20 + 60) / (14 - 4)", 8), 487 | ]; 488 | 489 | for test in input.iter() { 490 | let evaluated = test_eval(test.0.to_string()); 491 | test_integer_object(evaluated, test.1); 492 | } 493 | } 494 | 495 | #[test] 496 | fn test_eval_boolean_expression() { 497 | let input = vec![ 498 | // Standard 499 | ("true", true), 500 | ("false", false), 501 | // Bang Prefix 502 | ("!true", false), 503 | ("!false", true), 504 | ("!5", false), 505 | ("!!true", true), 506 | ("!!false", false), 507 | ("!!5", true), 508 | // Equality 509 | ("1 < 2", true), 510 | ("1 > 2", false), 511 | ("2 < 1", false), 512 | ("2 > 1", true), 513 | ("1 == 1", true), 514 | ("1 == 2", false), 515 | ("1 != 1", false), 516 | ("1 != 2", true), 517 | ("true == true", true), 518 | ("true != true", false), 519 | ("true == false", false), 520 | ("true != false", true), 521 | ("(2 == 2) != (true == false)", true), 522 | ]; 523 | 524 | for test in input.iter() { 525 | let evaluated = test_eval(test.0.to_string()); 526 | test_boolean_object(evaluated, test.1); 527 | } 528 | } 529 | 530 | fn int(i: i32) -> Object { 531 | Object::Integer(i) 532 | } 533 | 534 | fn null() -> Object { 535 | Object::Null 536 | } 537 | 538 | #[test] 539 | fn test_eval_conditional_expression() { 540 | let input = vec![ 541 | ("if (true) { 10 }", int(10)), 542 | ("if (false) { 10 }", null()), 543 | ("if (1) { 10 }", int(10)), 544 | ("if (1 < 2) { 10 }", int(10)), 545 | ("if (1 > 2) { 10 }", null()), 546 | ("if (1 > 2) { 10 } else { 20 }", int(20)), 547 | ("if (1 < 2) { 10 } else { 20 }", int(10)), 548 | ]; 549 | 550 | for test in input.iter() { 551 | println!("{:?}", test); 552 | let evaluated = test_eval(test.0.to_string()); 553 | test_object(evaluated, &test.1); 554 | } 555 | } 556 | 557 | #[test] 558 | fn test_return_expression() { 559 | let input = vec![ 560 | ("return 5;", 5), 561 | ("2; return 15;", 15), 562 | ("return 10; 7;", 10), 563 | ("return -5;", -5), 564 | ("return 2 * -5; 6;", -10), 565 | ( 566 | "if (10 > 1) { 567 | if (10 > 1) { 8; return 10; 6; } 568 | else{ return 5;} 569 | }else{ return 1; }", 570 | 10, 571 | ), 572 | ]; 573 | 574 | println!("Start Test!"); 575 | for test in input.iter() { 576 | println!("{:?}", test); 577 | let evaluated = test_eval(test.0.to_string()); 578 | test_integer_object(evaluated, test.1); 579 | } 580 | } 581 | 582 | #[test] 583 | fn test_error_handling() { 584 | let input = vec![ 585 | ("5 + true;", "ERROR: type mismatch: INTEGER + BOOLEAN"), 586 | ("5 + true; 5;", "ERROR: type mismatch: INTEGER + BOOLEAN"), 587 | ("5; 5 + true;", "ERROR: type mismatch: INTEGER + BOOLEAN"), 588 | ("-true", "ERROR: unknown operator: -BOOLEAN"), 589 | ( 590 | "true + false;", 591 | "ERROR: unknown operator: BOOLEAN + BOOLEAN", 592 | ), 593 | ( 594 | "5; false + true; 5", 595 | "ERROR: unknown operator: BOOLEAN + BOOLEAN", 596 | ), 597 | ( 598 | "if (10 > 1) { 599 | if (10 > 1) { return true + false; } 600 | }else{ return 1; }", 601 | "ERROR: unknown operator: BOOLEAN + BOOLEAN", 602 | ), 603 | ("foo", "ERROR: identifier not found: foo"), 604 | (r#" "hello" + 1 "#, "ERROR: type mismatch: STRING + INTEGER"), 605 | ( 606 | r#" "hello world" - "world" "#, 607 | "ERROR: unknown operator: STRING - STRING", 608 | ), 609 | ]; 610 | 611 | println!("Start Test!"); 612 | for test in input.iter() { 613 | let evaluated = test_eval(test.0.to_string()); 614 | assert_eq!(evaluated.inspect(), test.1); 615 | } 616 | } 617 | 618 | #[test] 619 | fn test_let_statement() { 620 | let input = vec![ 621 | ("let a = 5; a;", 5), 622 | ("let a = 5 * 5; a;", 25), 623 | ("let a = 5; let b = a; b;", 5), 624 | ("let a = 5; let b = a; let c = a + b + 10; c;", 20), 625 | ]; 626 | 627 | for test in input.iter() { 628 | let evaluated = test_eval(test.0.to_string()); 629 | test_integer_object(evaluated, test.1); 630 | } 631 | } 632 | 633 | #[test] 634 | fn test_function_object() { 635 | let input = "fn(x,y){x+y;};"; 636 | 637 | let evaluated = test_eval(input.to_string()); 638 | 639 | match evaluated { 640 | Object::Function { 641 | mut parameters, 642 | body, 643 | env: _, 644 | } => { 645 | assert_eq!(parameters.len(), 2); 646 | assert_eq!(parameters.pop().expect("expected value"), "y"); 647 | assert_eq!(parameters.pop().expect("expected value"), "x"); 648 | 649 | assert_eq!(ast::statement_to_string(&body), "(x + y)"); 650 | } 651 | _ => assert!(false), 652 | } 653 | } 654 | 655 | #[test] 656 | fn test_function_call() { 657 | let input = vec![ 658 | ("let identity = fn(x){x;}; identity(5);", 5), 659 | ("let identity = fn(x){return x;}; identity(5);", 5), 660 | ("let double = fn(x){x*2;}; double(5);", 10), 661 | ("let add = fn(x,y){x+y;}; add(5, 2);", 7), 662 | ("let add = fn(x,y){x+y;}; add(3+4, add(1,2));", 10), 663 | ("fn(x){x;}(5);", 5), 664 | ( 665 | "let add = fn (x) { fn(y){return x+y; }; }; let addFive = add(5); addFive(2)", 666 | 7, 667 | ), 668 | ]; 669 | 670 | for test in input.iter() { 671 | let evaluated = test_eval(test.0.to_string()); 672 | test_integer_object(evaluated, test.1); 673 | } 674 | } 675 | 676 | #[test] 677 | fn test_string_literal() { 678 | let input = r#" "Hello World!" "#; 679 | 680 | let evaluated = test_eval(input.to_string()); 681 | match evaluated { 682 | Object::String(s) => assert_eq!(s, "Hello World!".to_string()), 683 | _ => assert!( 684 | false, 685 | "Expected String Object, got {:?} instead!", 686 | evaluated 687 | ), 688 | } 689 | } 690 | 691 | #[test] 692 | fn test_string_literal_concat() { 693 | let input = r#" "Hello" + " " + "World!" "#; 694 | 695 | let evaluated = test_eval(input.to_string()); 696 | match evaluated { 697 | Object::String(s) => assert_eq!(s, "Hello World!".to_string()), 698 | _ => assert!( 699 | false, 700 | "Expected String Object, got {:?} instead!", 701 | evaluated 702 | ), 703 | } 704 | } 705 | 706 | fn error(message: &str) -> Object { 707 | Object::Error(message.to_string()) 708 | } 709 | 710 | #[test] 711 | fn test_builtin_functions() { 712 | let input = vec![ 713 | (r#"len("")"#, int(0)), 714 | (r#"len("hello")"#, int(5)), 715 | (r#"len("hello world")"#, int(11)), 716 | ( 717 | r#"len(1)"#, 718 | error("argument to 'len' not supported, got INTEGER"), 719 | ), 720 | ( 721 | r#"len("hello", "world")"#, 722 | error("wrong number of arguments, got 2 instead of 1"), 723 | ), 724 | ]; 725 | 726 | for test in input.iter() { 727 | let evaluated = test_eval(test.0.to_string()); 728 | match &test.1 { 729 | Object::Error(m) => match evaluated { 730 | Object::Error(m2) => assert_eq!(*m, m2), 731 | _ => assert!( 732 | false, 733 | "Expected [{}] but got [{}]!", 734 | test.1.inspect(), 735 | evaluated.inspect() 736 | ), 737 | }, 738 | Object::Integer(i) => test_integer_object(evaluated, *i), 739 | _ => assert!( 740 | false, 741 | "Type {} not supported in test!", 742 | test.1.type_string() 743 | ), 744 | } 745 | } 746 | } 747 | 748 | #[test] 749 | fn test_array_literal() { 750 | let input = "[1, 2 * 2, 3 + 3]"; 751 | 752 | let evaluated = test_eval(input.to_string()); 753 | match evaluated { 754 | Object::Array(e) => { 755 | assert_eq!(e[0].inspect(), "1"); 756 | assert_eq!(e[1].inspect(), "4"); 757 | assert_eq!(e[2].inspect(), "6"); 758 | } 759 | _ => assert!(false, "Expected Array Object, got {:?} instead!", evaluated), 760 | } 761 | } 762 | 763 | #[test] 764 | fn test_array_index() { 765 | let input = vec![ 766 | ("[1, 2, 3][0]", int(1)), 767 | ("[1, 2, 3][1]", int(2)), 768 | ("[1, 2, 3][2]", int(3)), 769 | ("let i = 0; [1][i];", int(1)), 770 | ("[1, 2, 3][1 + 1];", int(3)), 771 | ("let myArray = [1, 2, 3]; myArray[2];", int(3)), 772 | ( 773 | "let myArray = [1, 2, 3]; myArray[0] + myArray[1] + myArray[2];", 774 | int(6), 775 | ), 776 | ( 777 | "let myArray = [1, 2, 3]; let i = myArray[0]; myArray[i]", 778 | int(2), 779 | ), 780 | ("[1, 2, 3][3]", null()), 781 | ("[1, 2, 3][-1]", null()), 782 | ]; 783 | 784 | for test in input.iter() { 785 | let evaluated = test_eval(test.0.to_string()); 786 | match &test.1 { 787 | Object::Integer(i) => test_integer_object(evaluated, *i), 788 | Object::Null => match evaluated { 789 | Object::Null => (), 790 | _ => assert!(false, "Expected NULL not {}", evaluated.type_string()), 791 | }, 792 | _ => assert!( 793 | false, 794 | "Type {} not supported in test!", 795 | test.1.type_string() 796 | ), 797 | } 798 | } 799 | } 800 | 801 | fn string(s: &str) -> Object { 802 | Object::String(s.to_string()) 803 | } 804 | 805 | fn boolean(b: bool) -> Object { 806 | Object::Boolean(b) 807 | } 808 | 809 | #[test] 810 | fn test_hash_literal() { 811 | let input = r#" 812 | let two = "two"; 813 | { 814 | "one": 10 - 9, 815 | two: 1 + 1, 816 | "thr" + "ee": 6 / 2, 817 | 4: 4, 818 | true: 5, 819 | false: 6 820 | } 821 | "#; 822 | 823 | let evaluated = test_eval(input.to_string()); 824 | match evaluated { 825 | Object::Hash(map) => { 826 | assert_eq!(map.len(), 6); 827 | assert_eq!(int(1), map[&string("one")]); 828 | assert_eq!(int(2), map[&string("two")]); 829 | assert_eq!(int(3), map[&string("three")]); 830 | assert_eq!(int(4), map[&int(4)]); 831 | assert_eq!(int(5), map[&boolean(true)]); 832 | assert_eq!(int(6), map[&boolean(false)]); 833 | } 834 | _ => assert!( 835 | false, 836 | "Expected HASH got {} instead!", 837 | evaluated.type_string() 838 | ), 839 | } 840 | } 841 | 842 | #[test] 843 | fn test_hash_index() { 844 | let input = vec![ 845 | ("{0:1, 1:2, 2:3}[0]", int(1)), 846 | (r#"{"one":1, "two":2, "three":3}["two"]"#, int(2)), 847 | ("{true:1, false:0}[false]", int(0)), 848 | ("{0:1, 1:2, 2:3}[-1]", null()), 849 | ]; 850 | 851 | for test in input.iter() { 852 | let evaluated = test_eval(test.0.to_string()); 853 | match &test.1 { 854 | Object::Integer(i) => test_integer_object(evaluated, *i), 855 | Object::Null => match evaluated { 856 | Object::Null => (), 857 | _ => assert!(false, "Expected NULL not {}", evaluated.type_string()), 858 | }, 859 | _ => assert!( 860 | false, 861 | "Type {} not supported in test!", 862 | test.1.type_string() 863 | ), 864 | } 865 | } 866 | } 867 | } 868 | -------------------------------------------------------------------------------- /src/evaluate/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod builtin; 2 | pub mod evaluate; 3 | pub mod object; 4 | -------------------------------------------------------------------------------- /src/evaluate/object.rs: -------------------------------------------------------------------------------- 1 | use crate::evaluate::builtin::{get_builtin_functions, BuiltinFunction}; 2 | use crate::parser::ast; 3 | use core::cell::RefCell; 4 | use core::hash::{Hash, Hasher}; 5 | use std::collections::HashMap; 6 | use std::rc::Rc; 7 | 8 | #[derive(Debug, Clone, PartialEq, Eq)] 9 | pub enum Object { 10 | Integer(i32), 11 | Boolean(bool), 12 | String(String), 13 | Array(Vec), 14 | Hash(HashMap), 15 | Return(Box), 16 | Function { 17 | parameters: Vec, 18 | body: ast::Statement, 19 | env: Rc>, 20 | }, 21 | Builtin(String, i32, BuiltinFunction), 22 | Null, 23 | Error(String), 24 | } 25 | 26 | impl Hash for Object { 27 | fn hash(&self, state: &mut H) { 28 | match self { 29 | Object::Integer(i) => i.hash(state), 30 | Object::Boolean(b) => b.hash(state), 31 | Object::String(s) => s.hash(state), 32 | _ => "".hash(state), 33 | } 34 | } 35 | } 36 | 37 | impl Object { 38 | pub fn inspect(&self) -> String { 39 | match self { 40 | Object::Integer(i) => format!("{}", i), 41 | Object::Boolean(b) => format!("{}", b), 42 | Object::String(s) => s.clone(), 43 | Object::Array(elements) => { 44 | let mut output = String::new(); 45 | for (index, obj) in elements.iter().enumerate() { 46 | output += &obj.inspect(); 47 | if elements.len() - index != 1 { 48 | output += ", "; 49 | } 50 | } 51 | format!("[{}]", output) 52 | } 53 | Object::Hash(map) => { 54 | let mut output = String::new(); 55 | for (index, (key, value)) in map.iter().enumerate() { 56 | output += format!("{}:{}", key.inspect(), value.inspect()).as_str(); 57 | if map.len() - index != 1 { 58 | output += ", "; 59 | } 60 | } 61 | format!("{{{}}}", output) 62 | } 63 | Object::Return(r) => format!("return {}", r.inspect()), 64 | Object::Function { 65 | parameters, 66 | body, 67 | env: _, 68 | } => { 69 | let mut output = String::new(); 70 | for (index, ident) in parameters.iter().enumerate() { 71 | output += &ident; 72 | if parameters.len() - index != 1 { 73 | output += ", "; 74 | } 75 | } 76 | format!("fn ({}) {{\n{}\n}}", output, ast::statement_to_string(body)) 77 | } 78 | Object::Builtin(name, _, _) => format!("{}()", name), 79 | Object::Null => "NULL".to_string(), 80 | Object::Error(message) => format!("ERROR: {}", message), 81 | } 82 | } 83 | 84 | pub fn return_object(self) -> Object { 85 | Object::Return(Box::new(self)) 86 | } 87 | 88 | pub fn is_error(&self) -> bool { 89 | match self { 90 | Object::Error(_) => true, 91 | _ => false, 92 | } 93 | } 94 | 95 | pub fn type_string(&self) -> &str { 96 | match self { 97 | Object::Integer(_) => "INTEGER", 98 | Object::Boolean(_) => "BOOLEAN", 99 | Object::String(_) => "STRING", 100 | Object::Array(_) => "ARRAY", 101 | Object::Hash(_) => "HASH", 102 | Object::Return(_) => "RETURN", 103 | Object::Function { 104 | parameters: _, 105 | body: _, 106 | env: _, 107 | } => "FUNCTION", 108 | Object::Builtin(_, _, _) => "BUILTIN", 109 | Object::Null => "NULL", 110 | Object::Error(_) => "ERROR", 111 | } 112 | } 113 | } 114 | 115 | #[derive(Debug, Clone, PartialEq, Eq)] 116 | pub struct Environment { 117 | store: HashMap, 118 | outer: Option>>, 119 | } 120 | 121 | impl Environment { 122 | fn fill_builtins(&mut self) { 123 | for (key, func) in get_builtin_functions().iter() { 124 | self.set(key.to_string(), func.clone()); 125 | } 126 | } 127 | 128 | pub fn new() -> Self { 129 | let mut e = Environment { 130 | store: HashMap::new(), 131 | outer: None, 132 | }; 133 | e.fill_builtins(); 134 | e 135 | } 136 | 137 | pub fn new_outer(outer: Rc>) -> Self { 138 | let mut e = Environment { 139 | store: HashMap::new(), 140 | outer: Some(outer), 141 | }; 142 | e.fill_builtins(); 143 | e 144 | } 145 | 146 | pub fn get(&self, key: String) -> Option { 147 | match self.store.get(&key) { 148 | Some(obj) => Some(obj.clone()), 149 | None => match &self.outer { 150 | Some(env) => env.borrow_mut().get(key).clone(), 151 | None => return None, 152 | }, 153 | } 154 | } 155 | 156 | pub fn set(&mut self, key: String, value: Object) { 157 | self.store.insert(key, value); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/lexer/lexer.rs: -------------------------------------------------------------------------------- 1 | use crate::tokens::tokens::{lookup_identifier, Token, TokenType}; 2 | 3 | #[derive(Debug)] 4 | pub struct Lexer { 5 | input: String, 6 | input_chars: Vec, // Char Vector to allow getting chars at certain indices 7 | position: usize, // Current position in input (positions to current_char) 8 | read_position: usize, // Current reading position in input (positions to after current_char) 9 | current_char: char, // Char that is currently being examined 10 | } 11 | 12 | impl Lexer { 13 | pub fn new(input: String) -> Lexer { 14 | let mut lexer = Lexer { 15 | input: input.clone(), 16 | input_chars: input.chars().collect(), 17 | position: 0, 18 | read_position: 0, 19 | current_char: '\0', 20 | }; 21 | lexer.read_char(); 22 | lexer 23 | } 24 | 25 | fn peek_char(&self) -> char { 26 | if self.read_position >= self.input.len() { 27 | '\0' 28 | } else { 29 | self.input_chars[self.read_position] 30 | } 31 | } 32 | 33 | fn read_char(&mut self) { 34 | if self.read_position >= self.input.len() { 35 | self.current_char = '\0'; 36 | } else { 37 | self.current_char = self.input_chars[self.read_position]; 38 | } 39 | 40 | self.position = self.read_position; 41 | self.read_position += 1; 42 | } 43 | 44 | fn read_identifier(&mut self) -> &str { 45 | let position = self.position; 46 | while is_letter(self.current_char) { 47 | self.read_char(); 48 | } 49 | &self.input[position..self.position] 50 | } 51 | 52 | fn read_number(&mut self) -> &str { 53 | let position = self.position; 54 | while is_digit(self.current_char) { 55 | self.read_char(); 56 | } 57 | &self.input[position..self.position] 58 | } 59 | 60 | fn skip_whitespace(&mut self) { 61 | while self.current_char.is_ascii_whitespace() { 62 | self.read_char(); 63 | } 64 | } 65 | 66 | fn read_string(&mut self) -> &str { 67 | let start_pos = self.position + 1; 68 | self.read_char(); 69 | while self.current_char != '"' { 70 | self.read_char(); 71 | } 72 | &self.input[start_pos..self.position] 73 | } 74 | 75 | pub fn next_token(&mut self) -> Token { 76 | self.skip_whitespace(); 77 | let token = match self.current_char { 78 | // Operators 79 | '=' => { 80 | // Check if '==' else '=' 81 | if self.peek_char() == '=' { 82 | let ch = self.current_char; 83 | self.read_char(); 84 | Token::new( 85 | TokenType::EQ, 86 | ch.to_string() + &self.current_char.to_string(), 87 | ) 88 | } else { 89 | Token::new(TokenType::ASSIGN, self.current_char.to_string()) 90 | } 91 | } 92 | 93 | '!' => { 94 | // Check if '!=' else '!' 95 | if self.peek_char() == '=' { 96 | let ch = self.current_char; 97 | self.read_char(); 98 | Token::new( 99 | TokenType::NOT_EQ, 100 | ch.to_string() + &self.current_char.to_string(), 101 | ) 102 | } else { 103 | Token::new(TokenType::BANG, self.current_char.to_string()) 104 | } 105 | } 106 | 107 | // String 108 | '"' => Token::new(TokenType::STRING, self.read_string().to_string()), 109 | 110 | // Mathematical Operators 111 | '+' => Token::new(TokenType::PLUS, self.current_char.to_string()), 112 | '-' => Token::new(TokenType::MINUS, self.current_char.to_string()), 113 | 114 | '*' => Token::new(TokenType::ASTERISK, self.current_char.to_string()), 115 | '/' => Token::new(TokenType::SLASH, self.current_char.to_string()), 116 | 117 | '<' => Token::new(TokenType::LT, self.current_char.to_string()), 118 | '>' => Token::new(TokenType::GT, self.current_char.to_string()), 119 | 120 | // Delimiters 121 | ':' => Token::new(TokenType::COLON, self.current_char.to_string()), 122 | ';' => Token::new(TokenType::SEMICOLON, self.current_char.to_string()), 123 | ',' => Token::new(TokenType::COMMA, self.current_char.to_string()), 124 | 125 | '{' => Token::new(TokenType::LBRACE, self.current_char.to_string()), 126 | '}' => Token::new(TokenType::RBRACE, self.current_char.to_string()), 127 | '(' => Token::new(TokenType::LPAREN, self.current_char.to_string()), 128 | ')' => Token::new(TokenType::RPAREN, self.current_char.to_string()), 129 | '[' => Token::new(TokenType::LBRACKET, self.current_char.to_string()), 130 | ']' => Token::new(TokenType::RBRACKET, self.current_char.to_string()), 131 | 132 | // End of File delimiter 133 | '\0' => Token::new(TokenType::EOF, "".to_string()), 134 | 135 | // Else 136 | _ => { 137 | if is_letter(self.current_char) { 138 | let literal = self.read_identifier(); 139 | return Token::new(lookup_identifier(literal), literal.to_string()); 140 | } else if is_digit(self.current_char) { 141 | return Token::new(TokenType::INT, self.read_number().to_string()); 142 | } else { 143 | Token::new(TokenType::ILLEGAL, self.current_char.to_string()) 144 | } 145 | } 146 | }; 147 | self.read_char(); 148 | token 149 | } 150 | } 151 | 152 | fn is_letter(ch: char) -> bool { 153 | ch.is_alphabetic() || ch == '_' 154 | } 155 | 156 | fn is_digit(ch: char) -> bool { 157 | ch.is_digit(10) 158 | } 159 | 160 | #[cfg(test)] 161 | mod tests { 162 | use super::Lexer; 163 | use crate::tokens::tokens::{Token, TokenType}; 164 | 165 | #[test] 166 | fn next_token() { 167 | let input = "=+(){},;"; 168 | 169 | let expected = [ 170 | Token { 171 | token_type: TokenType::ASSIGN, 172 | literal: "=".to_string(), 173 | }, 174 | Token { 175 | token_type: TokenType::PLUS, 176 | literal: "+".to_string(), 177 | }, 178 | Token { 179 | token_type: TokenType::LPAREN, 180 | literal: "(".to_string(), 181 | }, 182 | Token { 183 | token_type: TokenType::RPAREN, 184 | literal: ")".to_string(), 185 | }, 186 | Token { 187 | token_type: TokenType::LBRACE, 188 | literal: "{".to_string(), 189 | }, 190 | Token { 191 | token_type: TokenType::RBRACE, 192 | literal: "}".to_string(), 193 | }, 194 | Token { 195 | token_type: TokenType::COMMA, 196 | literal: ",".to_string(), 197 | }, 198 | Token { 199 | token_type: TokenType::SEMICOLON, 200 | literal: ";".to_string(), 201 | }, 202 | ]; 203 | 204 | let mut lexer = Lexer::new(input.to_string()); 205 | 206 | for token in expected.iter() { 207 | let lexed_token = lexer.next_token(); 208 | assert_eq!(lexed_token, *token); 209 | } 210 | } 211 | 212 | #[test] 213 | fn number_parsing() { 214 | let input = "let x = 11;"; 215 | 216 | let expected = [ 217 | Token::new(TokenType::LET, "let".to_string()), 218 | Token::new(TokenType::IDENT, "x".to_string()), 219 | Token::new(TokenType::ASSIGN, "=".to_string()), 220 | Token::new(TokenType::INT, "11".to_string()), 221 | Token::new(TokenType::SEMICOLON, ";".to_string()), 222 | Token::new(TokenType::EOF, "".to_string()), 223 | ]; 224 | 225 | let mut lexer = Lexer::new(input.to_string()); 226 | 227 | for token in expected.iter() { 228 | let lexed_token = lexer.next_token(); 229 | assert_eq!(lexed_token, *token); 230 | } 231 | } 232 | 233 | #[test] 234 | fn simple_code() { 235 | let input = " 236 | let five = 5; 237 | let ten = 10; 238 | 239 | let add = fn(x, y){ 240 | x+y; 241 | }; 242 | 243 | let result = add(five, ten); 244 | "; 245 | 246 | let expected = [ 247 | Token::new(TokenType::LET, "let".to_string()), 248 | Token::new(TokenType::IDENT, "five".to_string()), 249 | Token::new(TokenType::ASSIGN, "=".to_string()), 250 | Token::new(TokenType::INT, "5".to_string()), 251 | Token::new(TokenType::SEMICOLON, ";".to_string()), 252 | Token::new(TokenType::LET, "let".to_string()), 253 | Token::new(TokenType::IDENT, "ten".to_string()), 254 | Token::new(TokenType::ASSIGN, "=".to_string()), 255 | Token::new(TokenType::INT, "10".to_string()), 256 | Token::new(TokenType::SEMICOLON, ";".to_string()), 257 | Token::new(TokenType::LET, "let".to_string()), 258 | Token::new(TokenType::IDENT, "add".to_string()), 259 | Token::new(TokenType::ASSIGN, "=".to_string()), 260 | Token::new(TokenType::FUNCTION, "fn".to_string()), 261 | Token::new(TokenType::LPAREN, "(".to_string()), 262 | Token::new(TokenType::IDENT, "x".to_string()), 263 | Token::new(TokenType::COMMA, ",".to_string()), 264 | Token::new(TokenType::IDENT, "y".to_string()), 265 | Token::new(TokenType::RPAREN, ")".to_string()), 266 | Token::new(TokenType::LBRACE, "{".to_string()), 267 | Token::new(TokenType::IDENT, "x".to_string()), 268 | Token::new(TokenType::PLUS, "+".to_string()), 269 | Token::new(TokenType::IDENT, "y".to_string()), 270 | Token::new(TokenType::SEMICOLON, ";".to_string()), 271 | Token::new(TokenType::RBRACE, "}".to_string()), 272 | Token::new(TokenType::SEMICOLON, ";".to_string()), 273 | Token::new(TokenType::LET, "let".to_string()), 274 | Token::new(TokenType::IDENT, "result".to_string()), 275 | Token::new(TokenType::ASSIGN, "=".to_string()), 276 | Token::new(TokenType::IDENT, "add".to_string()), 277 | Token::new(TokenType::LPAREN, "(".to_string()), 278 | Token::new(TokenType::IDENT, "five".to_string()), 279 | Token::new(TokenType::COMMA, ",".to_string()), 280 | Token::new(TokenType::IDENT, "ten".to_string()), 281 | Token::new(TokenType::RPAREN, ")".to_string()), 282 | Token::new(TokenType::SEMICOLON, ";".to_string()), 283 | Token::new(TokenType::EOF, "".to_string()), 284 | ]; 285 | 286 | let mut lexer = Lexer::new(input.to_string()); 287 | 288 | for token in expected.iter() { 289 | let lexed_token = lexer.next_token(); 290 | assert_eq!(lexed_token, *token); 291 | } 292 | } 293 | 294 | #[test] 295 | fn extended_tokens() { 296 | let input = " 297 | !-/*5; 298 | 5 < 10 > 5; 299 | "; 300 | 301 | let expected = [ 302 | Token::new(TokenType::BANG, "!".to_string()), 303 | Token::new(TokenType::MINUS, "-".to_string()), 304 | Token::new(TokenType::SLASH, "/".to_string()), 305 | Token::new(TokenType::ASTERISK, "*".to_string()), 306 | Token::new(TokenType::INT, "5".to_string()), 307 | Token::new(TokenType::SEMICOLON, ";".to_string()), 308 | Token::new(TokenType::INT, "5".to_string()), 309 | Token::new(TokenType::LT, "<".to_string()), 310 | Token::new(TokenType::INT, "10".to_string()), 311 | Token::new(TokenType::GT, ">".to_string()), 312 | Token::new(TokenType::INT, "5".to_string()), 313 | Token::new(TokenType::SEMICOLON, ";".to_string()), 314 | Token::new(TokenType::EOF, "".to_string()), 315 | ]; 316 | 317 | let mut lexer = Lexer::new(input.to_string()); 318 | 319 | for token in expected.iter() { 320 | let lexed_token = lexer.next_token(); 321 | assert_eq!(lexed_token, *token); 322 | } 323 | } 324 | 325 | #[test] 326 | fn if_else_tokens() { 327 | let input = " 328 | if (5 < 10) { 329 | return true; 330 | } else { 331 | return false; 332 | } 333 | "; 334 | 335 | let expected = [ 336 | Token::new(TokenType::IF, "if".to_string()), 337 | Token::new(TokenType::LPAREN, "(".to_string()), 338 | Token::new(TokenType::INT, "5".to_string()), 339 | Token::new(TokenType::LT, "<".to_string()), 340 | Token::new(TokenType::INT, "10".to_string()), 341 | Token::new(TokenType::RPAREN, ")".to_string()), 342 | Token::new(TokenType::LBRACE, "{".to_string()), 343 | Token::new(TokenType::RETURN, "return".to_string()), 344 | Token::new(TokenType::TRUE, "true".to_string()), 345 | Token::new(TokenType::SEMICOLON, ";".to_string()), 346 | Token::new(TokenType::RBRACE, "}".to_string()), 347 | Token::new(TokenType::ELSE, "else".to_string()), 348 | Token::new(TokenType::LBRACE, "{".to_string()), 349 | Token::new(TokenType::RETURN, "return".to_string()), 350 | Token::new(TokenType::FALSE, "false".to_string()), 351 | Token::new(TokenType::SEMICOLON, ";".to_string()), 352 | Token::new(TokenType::RBRACE, "}".to_string()), 353 | Token::new(TokenType::EOF, "".to_string()), 354 | ]; 355 | 356 | let mut lexer = Lexer::new(input.to_string()); 357 | 358 | for token in expected.iter() { 359 | let lexed_token = lexer.next_token(); 360 | assert_eq!(lexed_token, *token); 361 | } 362 | } 363 | 364 | #[test] 365 | fn equals_not_equals() { 366 | let input = " 367 | 10 == 10; 368 | 10 != 9; 369 | "; 370 | 371 | let expected = [ 372 | Token::new(TokenType::INT, "10".to_string()), 373 | Token::new(TokenType::EQ, "==".to_string()), 374 | Token::new(TokenType::INT, "10".to_string()), 375 | Token::new(TokenType::SEMICOLON, ";".to_string()), 376 | Token::new(TokenType::INT, "10".to_string()), 377 | Token::new(TokenType::NOT_EQ, "!=".to_string()), 378 | Token::new(TokenType::INT, "9".to_string()), 379 | Token::new(TokenType::SEMICOLON, ";".to_string()), 380 | Token::new(TokenType::EOF, "".to_string()), 381 | ]; 382 | 383 | let mut lexer = Lexer::new(input.to_string()); 384 | 385 | for token in expected.iter() { 386 | let lexed_token = lexer.next_token(); 387 | assert_eq!(lexed_token, *token); 388 | } 389 | } 390 | 391 | #[test] 392 | fn test_strings() { 393 | let input = r#" 394 | "foobar" 395 | "foo bar baz" 396 | "#; 397 | 398 | let expected = [ 399 | Token::new(TokenType::STRING, "foobar".to_string()), 400 | Token::new(TokenType::STRING, "foo bar baz".to_string()), 401 | Token::new(TokenType::EOF, "".to_string()), 402 | ]; 403 | 404 | let mut lexer = Lexer::new(input.to_string()); 405 | 406 | for token in expected.iter() { 407 | let lexed_token = lexer.next_token(); 408 | assert_eq!(lexed_token, *token); 409 | } 410 | } 411 | 412 | #[test] 413 | fn test_brackets() { 414 | let input = "[1, 2];"; 415 | 416 | let expected = [ 417 | Token::new(TokenType::LBRACKET, "[".to_string()), 418 | Token::new(TokenType::INT, "1".to_string()), 419 | Token::new(TokenType::COMMA, ",".to_string()), 420 | Token::new(TokenType::INT, "2".to_string()), 421 | Token::new(TokenType::RBRACKET, "]".to_string()), 422 | Token::new(TokenType::SEMICOLON, ";".to_string()), 423 | Token::new(TokenType::EOF, "".to_string()), 424 | ]; 425 | 426 | let mut lexer = Lexer::new(input.to_string()); 427 | 428 | for token in expected.iter() { 429 | let lexed_token = lexer.next_token(); 430 | assert_eq!(lexed_token, *token); 431 | } 432 | } 433 | 434 | #[test] 435 | fn test_hash() { 436 | let input = r#"{"foo": "bar"};"#; 437 | 438 | let expected = [ 439 | Token::new(TokenType::LBRACE, "{".to_string()), 440 | Token::new(TokenType::STRING, "foo".to_string()), 441 | Token::new(TokenType::COLON, ":".to_string()), 442 | Token::new(TokenType::STRING, "bar".to_string()), 443 | Token::new(TokenType::RBRACE, "}".to_string()), 444 | Token::new(TokenType::SEMICOLON, ";".to_string()), 445 | Token::new(TokenType::EOF, "".to_string()), 446 | ]; 447 | 448 | let mut lexer = Lexer::new(input.to_string()); 449 | 450 | for token in expected.iter() { 451 | let lexed_token = lexer.next_token(); 452 | assert_eq!(lexed_token, *token); 453 | } 454 | } 455 | } 456 | -------------------------------------------------------------------------------- /src/lexer/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod lexer; 2 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod evaluate; 2 | mod lexer; 3 | mod parser; 4 | mod repl; 5 | mod tokens; 6 | 7 | fn main() { 8 | repl::repl::start(); 9 | } 10 | -------------------------------------------------------------------------------- /src/parser/ast.rs: -------------------------------------------------------------------------------- 1 | use crate::tokens::tokens::TokenType; 2 | 3 | #[derive(Debug, Clone, PartialEq)] 4 | pub struct Program { 5 | pub statements: Vec, 6 | } 7 | 8 | pub fn program_to_string(program: &Program) -> String { 9 | let mut output = String::new(); 10 | for (index, statement) in program.statements.iter().enumerate() { 11 | output += &statement_to_string(&statement); 12 | if program.statements.len() - index != 1 { 13 | output += "\n"; 14 | } 15 | } 16 | 17 | output 18 | } 19 | 20 | #[derive(Debug, Clone, PartialEq, Eq)] 21 | pub enum Statement { 22 | Let(Identifier, Expr), 23 | Return(Expr), 24 | BlockStatement(Vec), 25 | Expr(Expr), 26 | None, 27 | } 28 | 29 | pub fn statement_to_string(statement: &Statement) -> String { 30 | match &*statement { 31 | Statement::Let(ident, expr) => format!("let {} = {}", ident, expression_to_string(expr)), 32 | Statement::Return(expr) => format!("(return {})", expression_to_string(expr)), 33 | Statement::Expr(expr) => format!("{}", expression_to_string(expr)), 34 | Statement::BlockStatement(vec) => { 35 | let mut output = String::new(); 36 | for (index, stmnt) in vec.iter().enumerate() { 37 | output += &statement_to_string(stmnt); 38 | if vec.len() - index != 1 { 39 | output += "\n"; 40 | } 41 | } 42 | output 43 | } 44 | Statement::None => "None".to_string(), 45 | } 46 | } 47 | 48 | #[derive(Debug, Clone, PartialEq, Eq)] 49 | pub enum Expr { 50 | Bool(bool), 51 | Identifier(Identifier), 52 | IntegerLiteral(i32), 53 | String(String), 54 | ArrayLiteral(Vec), 55 | Index(Box, Box), // Left, Index 56 | HashLiteral(Vec<(Expr, Expr)>), 57 | Prefix(String, Box), 58 | Infix(TokenType, Box, Box), 59 | If(Box, Box, Option>), 60 | FunctionLiteral(Vec, Box), 61 | CallExpression { 62 | function: Box, 63 | arguments: Vec, 64 | }, // Function can be either func_literal or identifier 65 | None, 66 | } 67 | 68 | pub fn expression_to_string(expression: &Expr) -> String { 69 | match expression { 70 | Expr::Bool(val) => format!("{}", val), 71 | Expr::Identifier(id) => format!("{}", id), 72 | Expr::IntegerLiteral(i) => format!("{}", i), 73 | Expr::String(s) => s.clone(), 74 | Expr::Index(l, i) => format!("({}[{}])", expression_to_string(l), expression_to_string(i)), 75 | Expr::ArrayLiteral(vec) => { 76 | let mut output = String::new(); 77 | for (index, expr) in vec.iter().enumerate() { 78 | output += &expression_to_string(expr); 79 | if vec.len() - index != 1 { 80 | output += ", "; 81 | } 82 | } 83 | format!("[{}]", output) 84 | } 85 | Expr::HashLiteral(vec) => { 86 | let mut output = String::new(); 87 | for (index, (key, expr)) in vec.iter().enumerate() { 88 | output += format!( 89 | "{}:{}", 90 | expression_to_string(key).as_str(), 91 | expression_to_string(expr).as_str() 92 | ) 93 | .as_str(); 94 | if vec.len() - index != 1 { 95 | output += ", "; 96 | } 97 | } 98 | format!("{{{}}}", output) 99 | } 100 | Expr::Prefix(op, boxed_expr) => format!("({}{})", op, expression_to_string(boxed_expr)), 101 | Expr::Infix(op, boxed_left, boxed_right) => format!( 102 | "({} {} {})", 103 | expression_to_string(boxed_left), 104 | op, 105 | expression_to_string(boxed_right) 106 | ), 107 | Expr::If(condition, if_block, else_block) => match else_block { 108 | Some(box_else) => format!( 109 | "if {} {} else {}", 110 | expression_to_string(condition), 111 | statement_to_string(if_block), 112 | statement_to_string(box_else) 113 | ), 114 | None => format!( 115 | "if {} {}", 116 | expression_to_string(condition), 117 | statement_to_string(if_block) 118 | ), 119 | }, 120 | Expr::FunctionLiteral(parameters, body) => { 121 | let mut output = String::new(); 122 | for (index, ident) in parameters.iter().enumerate() { 123 | output += &ident; 124 | if parameters.len() - index != 1 { 125 | output += ", "; 126 | } 127 | } 128 | format!("fn ({}) {}", output, statement_to_string(body)) 129 | } 130 | Expr::CallExpression { 131 | function: boxed_func, 132 | arguments: args, 133 | } => { 134 | let mut arg_string = String::new(); 135 | for (index, expr) in args.iter().enumerate() { 136 | arg_string.push_str(&expression_to_string(expr)); 137 | if args.len() - (index + 1) > 0 { 138 | println!("len {:?} index {}", args.len(), index); 139 | arg_string.push_str(", "); 140 | } 141 | } 142 | 143 | format!("{}({})", expression_to_string(boxed_func), arg_string) 144 | } 145 | Expr::None => "none".to_string(), 146 | } 147 | } 148 | 149 | pub type Identifier = String; 150 | -------------------------------------------------------------------------------- /src/parser/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod ast; 2 | pub mod parser; 3 | -------------------------------------------------------------------------------- /src/parser/parser.rs: -------------------------------------------------------------------------------- 1 | use crate::lexer::lexer::Lexer; 2 | use crate::parser::ast; 3 | use crate::tokens::tokens::{Token, TokenType}; 4 | use std::mem; 5 | 6 | #[derive(Debug, PartialEq, PartialOrd)] 7 | pub enum Precedence { 8 | LOWEST, 9 | EQUALS, 10 | // '==' or '!=' 11 | LESSGREATER, 12 | // '<' or '>' 13 | SUM, 14 | // '+' or '-' 15 | PRODUCT, 16 | // '*' or '/' 17 | PREFIX, 18 | // '-x' or '!x' 19 | CALL, 20 | // 'func(x)' 21 | INDEX, 22 | // 'arr[x]' 23 | } 24 | 25 | fn get_token_precedence(token_type: TokenType) -> Precedence { 26 | match token_type { 27 | TokenType::EQ => Precedence::EQUALS, 28 | TokenType::NOT_EQ => Precedence::EQUALS, 29 | TokenType::LT => Precedence::LESSGREATER, 30 | TokenType::GT => Precedence::LESSGREATER, 31 | TokenType::PLUS => Precedence::SUM, 32 | TokenType::MINUS => Precedence::SUM, 33 | TokenType::SLASH => Precedence::PRODUCT, 34 | TokenType::ASTERISK => Precedence::PRODUCT, 35 | TokenType::LPAREN => Precedence::CALL, 36 | TokenType::LBRACKET => Precedence::INDEX, 37 | _ => Precedence::LOWEST, 38 | } 39 | } 40 | 41 | pub struct Parser { 42 | lexer: Lexer, 43 | 44 | cur_token: Box, 45 | peek_token: Box, 46 | 47 | pub errors: Vec>, 48 | } 49 | 50 | impl Parser { 51 | pub fn new(lexer: Lexer) -> Parser { 52 | let mut parser = Parser { 53 | lexer: lexer, 54 | 55 | cur_token: Box::new(Token::new(TokenType::ILLEGAL, "".to_string())), 56 | peek_token: Box::new(Token::new(TokenType::ILLEGAL, "".to_string())), 57 | 58 | errors: vec![], 59 | }; 60 | 61 | // Setup Tokens 62 | parser.next_token(); 63 | parser.next_token(); 64 | 65 | parser 66 | } 67 | 68 | /* 69 | * Token Manipulation 70 | */ 71 | 72 | fn next_token(&mut self) { 73 | // Put peek into cur and put the next token into peek 74 | self.cur_token = mem::replace(&mut self.peek_token, Box::new(self.lexer.next_token())); 75 | } 76 | 77 | fn cur_token_is(&self, token_type: TokenType) -> bool { 78 | (*self.cur_token).token_type == token_type 79 | } 80 | 81 | fn peek_token_is(&self, token_type: TokenType) -> bool { 82 | (*self.peek_token).token_type == token_type 83 | } 84 | 85 | fn expect_peek(&mut self, token_type: TokenType) -> bool { 86 | if self.peek_token_is(token_type) { 87 | self.next_token(); 88 | true 89 | } else { 90 | self.errors.push(Err(format!( 91 | "Token {:?} expected, instead got {:?}!", 92 | token_type, 93 | (*self.peek_token).token_type 94 | ))); 95 | false 96 | } 97 | } 98 | 99 | fn curr_precedence(&self) -> Precedence { 100 | get_token_precedence((*self.cur_token).token_type) 101 | } 102 | 103 | fn peek_precedence(&self) -> Precedence { 104 | get_token_precedence((*self.peek_token).token_type) 105 | } 106 | /* 107 | * Program Parsing 108 | */ 109 | 110 | pub fn parse_program(&mut self) -> ast::Program { 111 | let mut program = ast::Program { statements: vec![] }; 112 | 113 | loop { 114 | match (*self.cur_token).token_type { 115 | TokenType::EOF => break, 116 | _ => { 117 | let statement_option = self.parse_statement(); 118 | match statement_option { 119 | Some(statement) => program.statements.push(statement), 120 | None => (), 121 | } 122 | self.next_token(); 123 | } 124 | } 125 | } 126 | program 127 | } 128 | 129 | /* 130 | * Statement Parsing 131 | */ 132 | 133 | fn parse_statement(&mut self) -> Option { 134 | match (*self.cur_token).token_type { 135 | TokenType::LET => self.parse_let_statement(), 136 | TokenType::RETURN => self.parse_return_statement(), 137 | _ => self.parse_expression_statement(), 138 | } 139 | } 140 | 141 | fn parse_let_statement(&mut self) -> Option { 142 | if !self.expect_peek(TokenType::IDENT) { 143 | return None; 144 | } 145 | 146 | let cur_token_box = mem::replace( 147 | &mut self.cur_token, 148 | Box::new(Token::new(TokenType::ILLEGAL, "".to_string())), 149 | ); 150 | let cur_token = *cur_token_box; 151 | 152 | if !self.expect_peek(TokenType::ASSIGN) { 153 | return None; 154 | } 155 | 156 | // Parse Expression 157 | self.next_token(); 158 | 159 | let value = match self.parse_expression(&mut Precedence::LOWEST) { 160 | Some(expr) => expr, 161 | None => ast::Expr::None, 162 | }; 163 | 164 | while (*self.cur_token).token_type != TokenType::SEMICOLON { 165 | self.next_token(); 166 | } 167 | 168 | Some(ast::Statement::Let(cur_token.literal.clone(), value)) 169 | } 170 | 171 | fn parse_return_statement(&mut self) -> Option { 172 | self.next_token(); 173 | 174 | let return_value = match self.parse_expression(&mut Precedence::LOWEST) { 175 | Some(expr) => expr, 176 | None => ast::Expr::None, 177 | }; 178 | 179 | while (*self.cur_token).token_type != TokenType::SEMICOLON { 180 | self.next_token(); 181 | } 182 | 183 | Some(ast::Statement::Return(return_value)) 184 | } 185 | 186 | fn parse_expression_statement(&mut self) -> Option { 187 | let expr = self.parse_expression(&mut Precedence::LOWEST); 188 | 189 | // Check for the optional semicolon 190 | if self.peek_token_is(TokenType::SEMICOLON) { 191 | self.next_token(); 192 | } 193 | match expr { 194 | Some(expr) => Some(ast::Statement::Expr(expr)), 195 | None => None, 196 | } 197 | } 198 | 199 | /* 200 | * Expression Parsing 201 | */ 202 | 203 | fn parse_prefix(&mut self, token_type: TokenType) -> ast::Expr { 204 | match token_type { 205 | TokenType::IDENT => self.parse_identifier(), 206 | TokenType::INT => self.parse_integer_literal(), 207 | TokenType::BANG | TokenType::MINUS => self.parse_prefix_expression(), 208 | TokenType::TRUE | TokenType::FALSE => self.parse_boolean(), 209 | TokenType::LPAREN => self.parse_grouped_expression(), 210 | TokenType::IF => self.parse_if_expression(), 211 | TokenType::FUNCTION => self.parse_function_literal(), 212 | TokenType::STRING => self.parse_string_literal(), 213 | TokenType::LBRACKET => self.parse_array_literal(), 214 | TokenType::LBRACE => self.parse_hash_literal(), 215 | _ => ast::Expr::None, 216 | } 217 | } 218 | 219 | fn parse_infix( 220 | &mut self, 221 | left: ast::Expr, 222 | token_type: TokenType, 223 | ) -> Result { 224 | match token_type { 225 | TokenType::LPAREN => self.parse_call_expression(left), 226 | TokenType::LBRACKET => self.parse_index_expression(left), 227 | _ => self.parse_infix_expression(left), 228 | } 229 | } 230 | 231 | fn parse_expression(&mut self, priority: &mut Precedence) -> Option { 232 | let prefix_expr = self.parse_prefix((*self.cur_token).token_type); 233 | let mut left_expr = match prefix_expr { 234 | ast::Expr::None => { 235 | self.errors.push(Err(format!( 236 | "No Prefix found for: {:?}", 237 | (*self.cur_token).token_type 238 | ))); 239 | return None; 240 | } 241 | _ => prefix_expr, 242 | }; 243 | 244 | while !self.peek_token_is(TokenType::SEMICOLON) 245 | // Check if the current precedence still doesn't overule the next one 246 | && priority < &mut self.peek_precedence() 247 | // Check if INFIX operator (has a precedence) 248 | && get_token_precedence((*self.peek_token).token_type) != Precedence::LOWEST 249 | { 250 | self.next_token(); 251 | match self.parse_infix(left_expr, (*self.cur_token).token_type) { 252 | Ok(result) => { 253 | left_expr = result; 254 | } 255 | Err(err) => return Some(err), 256 | } 257 | } 258 | 259 | Some(left_expr) 260 | } 261 | 262 | fn parse_identifier(&mut self) -> ast::Expr { 263 | let cur_token_box = mem::replace( 264 | &mut self.cur_token, 265 | Box::new(Token::new(TokenType::ILLEGAL, "".to_string())), 266 | ); 267 | let cur_token = *cur_token_box; 268 | 269 | ast::Expr::Identifier(cur_token.literal.clone()) 270 | } 271 | 272 | fn parse_integer_literal(&mut self) -> ast::Expr { 273 | let value = self.cur_token.literal.parse(); 274 | match value { 275 | Ok(expr) => ast::Expr::IntegerLiteral(expr), 276 | Err(e) => { 277 | self.errors.push(Err(e.to_string())); 278 | ast::Expr::None 279 | } 280 | } 281 | } 282 | 283 | fn parse_boolean(&mut self) -> ast::Expr { 284 | ast::Expr::Bool(self.cur_token_is(TokenType::TRUE)) 285 | } 286 | 287 | fn parse_grouped_expression(&mut self) -> ast::Expr { 288 | self.next_token(); 289 | 290 | let exp = self.parse_expression(&mut Precedence::LOWEST); 291 | 292 | if !self.expect_peek(TokenType::RPAREN) { 293 | self.errors 294 | .push(Err("Expected RPAREN to close LPAREN".to_string())); 295 | return ast::Expr::None; 296 | } 297 | 298 | match exp { 299 | Some(expr) => expr, 300 | None => ast::Expr::None, 301 | } 302 | } 303 | 304 | fn parse_block_statement(&mut self) -> ast::Statement { 305 | let mut statements: Vec = vec![]; 306 | 307 | self.next_token(); 308 | 309 | while !self.cur_token_is(TokenType::RBRACE) { 310 | match self.parse_statement() { 311 | Some(statement) => statements.push(statement), 312 | None => (), 313 | } 314 | self.next_token(); 315 | } 316 | 317 | ast::Statement::BlockStatement(statements) 318 | } 319 | 320 | fn parse_if_expression(&mut self) -> ast::Expr { 321 | if !self.expect_peek(TokenType::LPAREN) { 322 | self.errors 323 | .push(Err("Expected LPAREN after IF!".to_string())); 324 | return ast::Expr::None; 325 | } 326 | 327 | self.next_token(); 328 | // Parse Condition 329 | let condition = match self.parse_expression(&mut Precedence::LOWEST) { 330 | Some(expr) => Box::new(expr), 331 | None => Box::new(ast::Expr::None), 332 | }; 333 | 334 | if !self.expect_peek(TokenType::RPAREN) { 335 | self.errors 336 | .push(Err("Expected RPAREN after IF condition!".to_string())); 337 | return ast::Expr::None; 338 | } 339 | // Parse Consequence (if then) 340 | if !self.expect_peek(TokenType::LBRACE) { 341 | self.errors.push(Err( 342 | "Expected BlockStatement after IF condition!".to_string() 343 | )); 344 | return ast::Expr::None; 345 | } 346 | 347 | let consequence = Box::new(self.parse_block_statement()); 348 | 349 | // Parse Alternative (else) 350 | let alternative = if self.peek_token_is(TokenType::ELSE) { 351 | self.next_token(); 352 | if !self.expect_peek(TokenType::LBRACE) { 353 | None 354 | } else { 355 | Some(Box::new(self.parse_block_statement())) 356 | } 357 | } else { 358 | None 359 | }; 360 | 361 | ast::Expr::If(condition, consequence, alternative) 362 | } 363 | 364 | fn parse_function_parameters(&mut self) -> Vec { 365 | let mut identifiers: Vec = vec![]; 366 | 367 | if self.peek_token_is(TokenType::RPAREN) { 368 | self.next_token(); 369 | return identifiers; 370 | } 371 | 372 | self.next_token(); 373 | 374 | let mut ident = self.cur_token.literal.clone(); 375 | 376 | identifiers.push(ident); 377 | 378 | while self.peek_token_is(TokenType::COMMA) { 379 | self.next_token(); 380 | self.next_token(); 381 | 382 | ident = self.cur_token.literal.clone(); 383 | 384 | identifiers.push(ident); 385 | } 386 | 387 | if !self.expect_peek(TokenType::RPAREN) { 388 | self.errors 389 | .push(Err("Expected RPAREN after parsing parameters!".to_string())); 390 | } 391 | 392 | identifiers 393 | } 394 | 395 | fn parse_function_literal(&mut self) -> ast::Expr { 396 | if !self.expect_peek(TokenType::LPAREN) { 397 | self.errors 398 | .push(Err("Expected LPAREN after FUNCTION!".to_string())); 399 | return ast::Expr::None; 400 | } 401 | 402 | let parameters = self.parse_function_parameters(); 403 | 404 | if !self.expect_peek(TokenType::LBRACE) { 405 | self.errors 406 | .push(Err("Expected LBRACE after FUNCTION PARAMETERS!".to_string())); 407 | return ast::Expr::None; 408 | } 409 | 410 | let body = self.parse_block_statement(); 411 | 412 | ast::Expr::FunctionLiteral(parameters, Box::new(body)) 413 | } 414 | 415 | fn parse_string_literal(&mut self) -> ast::Expr { 416 | ast::Expr::String(self.cur_token.literal.clone()) 417 | } 418 | 419 | fn parse_array_literal(&mut self) -> ast::Expr { 420 | ast::Expr::ArrayLiteral(self.parse_expression_list(TokenType::RBRACKET)) 421 | } 422 | 423 | fn parse_hash_literal(&mut self) -> ast::Expr { 424 | let mut keys = vec![]; 425 | 426 | while !self.peek_token_is(TokenType::RBRACE) { 427 | self.next_token(); 428 | let key = match self.parse_expression(&mut Precedence::LOWEST) { 429 | Some(expr) => expr, 430 | None => ast::Expr::None, 431 | }; 432 | 433 | if !self.expect_peek(TokenType::COLON) { 434 | return ast::Expr::None; 435 | } 436 | 437 | self.next_token(); 438 | 439 | let value = match self.parse_expression(&mut Precedence::LOWEST) { 440 | Some(expr) => expr, 441 | None => ast::Expr::None, 442 | }; 443 | 444 | keys.push((key, value)); 445 | 446 | if !self.peek_token_is(TokenType::RBRACE) && !self.expect_peek(TokenType::COMMA) { 447 | return ast::Expr::None; 448 | } 449 | } 450 | 451 | if !self.expect_peek(TokenType::RBRACE) { 452 | return ast::Expr::None; 453 | } 454 | 455 | ast::Expr::HashLiteral(keys) 456 | } 457 | 458 | fn parse_expression_list(&mut self, until: TokenType) -> Vec { 459 | let mut args: Vec = vec![]; 460 | 461 | if self.peek_token_is(until) { 462 | self.next_token(); 463 | return args; 464 | } 465 | 466 | self.next_token(); 467 | match self.parse_expression(&mut Precedence::LOWEST) { 468 | Some(expr) => args.push(expr), 469 | None => (), 470 | } 471 | 472 | while self.peek_token_is(TokenType::COMMA) { 473 | self.next_token(); 474 | self.next_token(); 475 | match self.parse_expression(&mut Precedence::LOWEST) { 476 | Some(expr) => args.push(expr), 477 | None => (), 478 | } 479 | } 480 | 481 | if !self.expect_peek(until) { 482 | self.errors.push(Err( 483 | "Expected RPAREN after parsing call arguments!".to_string() 484 | )); 485 | } 486 | 487 | args 488 | } 489 | 490 | fn parse_prefix_expression(&mut self) -> ast::Expr { 491 | let operator = self.cur_token.literal.clone(); 492 | 493 | self.next_token(); 494 | 495 | let right = match self.parse_expression(&mut Precedence::PREFIX) { 496 | Some(expr) => expr, 497 | None => { 498 | self.errors.push(Err(format!( 499 | "No expression to the right of prefix {}", 500 | operator 501 | ))); 502 | ast::Expr::None 503 | } 504 | }; 505 | ast::Expr::Prefix(operator.to_string(), Box::new(right)) 506 | } 507 | 508 | fn parse_call_expression(&mut self, function: ast::Expr) -> Result { 509 | Ok(ast::Expr::CallExpression { 510 | function: Box::new(function), 511 | arguments: self.parse_expression_list(TokenType::RPAREN), 512 | }) 513 | } 514 | 515 | fn parse_index_expression(&mut self, left: ast::Expr) -> Result { 516 | self.next_token(); 517 | let right = match self.parse_expression(&mut Precedence::LOWEST) { 518 | Some(expr) => expr, 519 | None => return Err(ast::Expr::None), 520 | }; 521 | 522 | if !self.expect_peek(TokenType::RBRACKET) { 523 | return Err(ast::Expr::None); 524 | } 525 | 526 | Ok(ast::Expr::Index(Box::new(left), Box::new(right))) 527 | } 528 | 529 | fn parse_infix_expression(&mut self, left: ast::Expr) -> Result { 530 | let mut precedence = self.curr_precedence(); 531 | let operator = self.cur_token.token_type; 532 | 533 | self.next_token(); 534 | let right = match self.parse_expression(&mut precedence) { 535 | Some(expr) => expr, 536 | None => ast::Expr::None, 537 | }; 538 | 539 | Ok(ast::Expr::Infix(operator, Box::new(left), Box::new(right))) 540 | } 541 | } 542 | 543 | #[cfg(test)] 544 | mod tests { 545 | use super::*; 546 | 547 | #[test] 548 | fn test_let_statement() { 549 | let input = " 550 | let x = 5; 551 | let y = 10; 552 | let foobar = 838383; 553 | "; 554 | 555 | let lexer = Lexer::new(input.to_string()); 556 | let mut parser = Parser::new(lexer); 557 | 558 | let program = parser.parse_program(); 559 | 560 | // Check for errors 561 | for err in parser.errors.iter() { 562 | println!("{:?}", err.as_ref().unwrap_err()); 563 | } 564 | assert_eq!(parser.errors.len(), 0); 565 | 566 | assert_eq!(program.statements.len(), 3); 567 | 568 | let expected_identifier = ["x", "y", "foobar"]; 569 | 570 | for (index, expected_name) in expected_identifier.iter().enumerate() { 571 | assert_let_statement(&program.statements[index], expected_name); 572 | } 573 | } 574 | 575 | fn assert_let_statement(statement: &ast::Statement, expected_identifier: &str) { 576 | match statement { 577 | ast::Statement::Let(identifier, _) => { 578 | assert_eq!(*identifier, expected_identifier.to_string()); 579 | } 580 | _ => assert!(false), 581 | }; 582 | } 583 | 584 | #[test] 585 | fn test_let_statement_expressions() { 586 | let input = " 587 | let x = 5; 588 | let y = true; 589 | let foo = y; 590 | "; 591 | 592 | let lexer = Lexer::new(input.to_string()); 593 | let mut parser = Parser::new(lexer); 594 | 595 | let mut program = parser.parse_program(); 596 | 597 | // Check for errors 598 | for err in parser.errors.iter() { 599 | println!("{:?}", err.as_ref().unwrap_err()); 600 | } 601 | assert_eq!(parser.errors.len(), 0); 602 | 603 | assert_eq!(program.statements.len(), 3); 604 | 605 | let x_is_5 = program.statements.remove(0); 606 | match x_is_5 { 607 | ast::Statement::Let(identifier, expr) => { 608 | assert_eq!(identifier, "x".to_string()); 609 | test_integer_literal(&expr, 5); 610 | } 611 | _ => assert!(false), 612 | }; 613 | 614 | let y_is_true = program.statements.remove(0); 615 | match y_is_true { 616 | ast::Statement::Let(identifier, expr) => { 617 | assert_eq!(identifier, "y".to_string()); 618 | test_bool(&expr, true); 619 | } 620 | _ => assert!(false), 621 | }; 622 | 623 | let foo_is_y = program.statements.remove(0); 624 | match foo_is_y { 625 | ast::Statement::Let(identifier, expr) => { 626 | assert_eq!(identifier, "foo".to_string()); 627 | match expr { 628 | ast::Expr::Identifier(ident) => assert_eq!(ident, "y".to_string()), 629 | _ => assert!(false), 630 | } 631 | } 632 | _ => assert!(false), 633 | }; 634 | } 635 | 636 | #[test] 637 | fn test_return_statement() { 638 | let input = " 639 | return 5; 640 | return 10; 641 | return 993322; 642 | "; 643 | 644 | let lexer = Lexer::new(input.to_string()); 645 | let mut parser = Parser::new(lexer); 646 | 647 | let program = parser.parse_program(); 648 | 649 | // Check for errors 650 | for err in parser.errors.iter() { 651 | println!("{:?}", err.as_ref().unwrap_err()); 652 | } 653 | assert_eq!(parser.errors.len(), 0); 654 | 655 | assert_eq!(program.statements.len(), 3); 656 | 657 | //let expected_identifier = ["x", "y", "foobar"]; 658 | 659 | for statement in program.statements.iter() { 660 | match statement { 661 | ast::Statement::Return(_) => (), 662 | _ => assert!(false), 663 | }; 664 | } 665 | } 666 | 667 | #[test] 668 | fn test_identifier_expression() { 669 | let input = "foobar;"; 670 | 671 | let lexer = Lexer::new(input.to_string()); 672 | let mut parser = Parser::new(lexer); 673 | 674 | let mut program = parser.parse_program(); 675 | 676 | // Check for errors 677 | for err in parser.errors.iter() { 678 | println!("{:?}", err.as_ref().unwrap_err()); 679 | } 680 | 681 | assert_eq!(parser.errors.len(), 0); 682 | 683 | assert_eq!(program.statements.len(), 1); 684 | 685 | let statement = program.statements.pop().expect("Expected Statement!"); 686 | match statement { 687 | ast::Statement::Expr(expr) => match expr { 688 | ast::Expr::Identifier(identifier) => { 689 | assert_eq!(identifier, "foobar".to_string()); 690 | } 691 | _ => assert!(false), 692 | }, 693 | _ => assert!(false), 694 | } 695 | } 696 | 697 | #[test] 698 | fn test_integer_expression() { 699 | let input = "5;"; 700 | 701 | let lexer = Lexer::new(input.to_string()); 702 | let mut parser = Parser::new(lexer); 703 | 704 | let mut program = parser.parse_program(); 705 | 706 | // Check for errors 707 | for err in parser.errors.iter() { 708 | println!("{:?}", err.as_ref().unwrap_err()); 709 | } 710 | 711 | assert_eq!(parser.errors.len(), 0); 712 | 713 | assert_eq!(program.statements.len(), 1); 714 | 715 | let statement = program.statements.pop().expect("Expected Statement!"); 716 | match statement { 717 | ast::Statement::Expr(expr) => match expr { 718 | ast::Expr::IntegerLiteral(value) => { 719 | assert_eq!(value, 5); 720 | } 721 | _ => assert!(false), 722 | }, 723 | _ => assert!(false), 724 | } 725 | } 726 | 727 | fn test_integer_literal(expr: &ast::Expr, expected: i32) { 728 | match expr { 729 | ast::Expr::IntegerLiteral(value) => assert_eq!(*value, expected), 730 | _ => assert!(false), 731 | } 732 | } 733 | 734 | fn test_bool(expr: &ast::Expr, expected: bool) { 735 | match expr { 736 | ast::Expr::Bool(value) => assert_eq!(*value, expected), 737 | _ => assert!(false), 738 | } 739 | } 740 | 741 | #[test] 742 | fn test_parsing_prefix_expressions() { 743 | let input = vec![ 744 | ("!5;", "!", int(5)), 745 | ("-15;", "-", int(15)), 746 | ("!true;", "!", bool(true)), 747 | ("!false;", "!", bool(false)), 748 | ]; 749 | 750 | for test in input.iter() { 751 | let lexer = Lexer::new(test.0.to_string()); 752 | let mut parser = Parser::new(lexer); 753 | 754 | let mut program = parser.parse_program(); 755 | 756 | // Check for errors 757 | for err in parser.errors.iter() { 758 | println!("{:?}", err.as_ref().unwrap_err()); 759 | } 760 | 761 | assert_eq!(parser.errors.len(), 0); 762 | 763 | assert_eq!(program.statements.len(), 1); 764 | 765 | let statement = program.statements.pop().expect("Expected Statement!"); 766 | match statement { 767 | ast::Statement::Expr(expr) => match expr { 768 | ast::Expr::Prefix(value, right) => { 769 | assert_eq!(value, test.1); 770 | test_expression(&right, &test.2); 771 | } 772 | _ => assert!(false), 773 | }, 774 | _ => assert!(false), 775 | } 776 | } 777 | } 778 | 779 | fn int(i: i32) -> ast::Expr { 780 | ast::Expr::IntegerLiteral(i) 781 | } 782 | 783 | fn bool(b: bool) -> ast::Expr { 784 | ast::Expr::Bool(b) 785 | } 786 | 787 | fn ident(value: &str) -> ast::Expr { 788 | ast::Expr::Identifier(value.to_string()) 789 | } 790 | 791 | #[test] 792 | fn test_parsing_infix_expressions() { 793 | let input = vec![ 794 | ("5 + 5;", int(5), TokenType::PLUS, int(5)), 795 | ("5 - 5;", int(5), TokenType::MINUS, int(5)), 796 | ("5 * 5;", int(5), TokenType::ASTERISK, int(5)), 797 | ("5 / 5;", int(5), TokenType::SLASH, int(5)), 798 | ("5 > 5;", int(5), TokenType::GT, int(5)), 799 | ("5 < 5;", int(5), TokenType::LT, int(5)), 800 | ("5 == 5;", int(5), TokenType::EQ, int(5)), 801 | ("5 != 5;", int(5), TokenType::NOT_EQ, int(5)), 802 | ("true != false;", bool(true), TokenType::NOT_EQ, bool(false)), 803 | ("false == false;", bool(false), TokenType::EQ, bool(false)), 804 | ("true == true;", bool(true), TokenType::EQ, bool(true)), 805 | ]; 806 | 807 | for test in input.iter() { 808 | let lexer = Lexer::new(test.0.to_string()); 809 | let mut parser = Parser::new(lexer); 810 | 811 | let mut program = parser.parse_program(); 812 | 813 | // Check for errors 814 | for err in parser.errors.iter() { 815 | println!("{:?}", err.as_ref().unwrap_err()); 816 | } 817 | 818 | assert_eq!(parser.errors.len(), 0); 819 | 820 | assert_eq!(program.statements.len(), 1); 821 | 822 | let statement = program.statements.pop().expect("Expected Statement!"); 823 | match statement { 824 | ast::Statement::Expr(expr) => { 825 | test_infix_expression(&expr, &test.1, test.2, &test.3) 826 | } 827 | _ => assert!(false), 828 | } 829 | } 830 | } 831 | 832 | #[test] 833 | fn test_operator_precedence() { 834 | let input = vec![ 835 | ("-a * b", "((-a) * b)"), 836 | ("!-a", "(!(-a))"), 837 | ("a + b + c", "((a + b) + c)"), 838 | ("a * b * c", "((a * b) * c)"), 839 | ("a * b / c", "((a * b) / c)"), 840 | ("a + b / c", "(a + (b / c))"), 841 | ("a + b * c + d / e - f", "(((a + (b * c)) + (d / e)) - f)"), 842 | ("3 + 4; -5 * 5", "(3 + 4)\n((-5) * 5)"), 843 | ("5 > 4 == 3 < 4", "((5 > 4) == (3 < 4))"), 844 | ("5 < 4 != 3 > 4", "((5 < 4) != (3 > 4))"), 845 | ( 846 | "3 + 4 * 5 == 3 * 1 + 4 * 5", 847 | "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))", 848 | ), 849 | ( 850 | "3 + 4 * 5 == 3 * 1 + 4 * 5", 851 | "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))", 852 | ), 853 | ("true", "true"), 854 | ("false", "false"), 855 | ("3 > 5 == false", "((3 > 5) == false)"), 856 | ("3 < 5 != true", "((3 < 5) != true)"), 857 | ("1 + (2 + 3) + 4", "((1 + (2 + 3)) + 4)"), 858 | ("(5 + 5) * 2", "((5 + 5) * 2)"), 859 | ("2 / (5 + 5)", "(2 / (5 + 5))"), 860 | ("-(5 + 5)", "(-(5 + 5))"), 861 | ("!(true == true)", "(!(true == true))"), 862 | ("a + add(b * c) + d", "((a + add((b * c))) + d)"), 863 | ( 864 | "add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))", 865 | "add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))", 866 | ), 867 | ( 868 | "add(a + b + c * d / f + g)", 869 | "add((((a + b) + ((c * d) / f)) + g))", 870 | ), 871 | ( 872 | "a * [1, 2, 3, 4][b * c] * d", 873 | "((a * ([1, 2, 3, 4][(b * c)])) * d)", 874 | ), 875 | ( 876 | "add(a * b[2], b[1], 2 * [1, 2][1])", 877 | "add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))", 878 | ), 879 | ]; 880 | 881 | for test in input.iter() { 882 | let lexer = Lexer::new(test.0.to_string()); 883 | let mut parser = Parser::new(lexer); 884 | 885 | let program = parser.parse_program(); 886 | 887 | // Check for errors 888 | for err in parser.errors.iter() { 889 | println!("{:?}", err.as_ref().unwrap_err()); 890 | } 891 | 892 | assert_eq!(parser.errors.len(), 0); 893 | 894 | assert_eq!(ast::program_to_string(&program), test.1); 895 | } 896 | } 897 | 898 | fn test_identifier(expr: &ast::Expr, expected: &String) { 899 | match expr { 900 | ast::Expr::Identifier(ident) => { 901 | assert_eq!(ident, expected); 902 | } 903 | _ => assert!(false), 904 | } 905 | } 906 | 907 | fn test_expression(expr: &ast::Expr, expected: &ast::Expr) { 908 | match expr { 909 | ast::Expr::Identifier(_) => match expected { 910 | ast::Expr::Identifier(ident) => test_identifier(expr, &ident), 911 | _ => assert!(false), 912 | }, 913 | ast::Expr::IntegerLiteral(_) => match expected { 914 | ast::Expr::IntegerLiteral(i) => test_integer_literal(expr, *i), 915 | _ => assert!(false), 916 | }, 917 | ast::Expr::Bool(val) => match expected { 918 | ast::Expr::Bool(val2) => assert_eq!(val, val2), 919 | _ => assert!(false), 920 | }, 921 | _ => assert!(false), 922 | } 923 | } 924 | 925 | fn test_infix_expression( 926 | infix: &ast::Expr, 927 | left: &ast::Expr, 928 | operator: TokenType, 929 | right: &ast::Expr, 930 | ) { 931 | match infix { 932 | ast::Expr::Infix(op, box_left, box_right) => { 933 | // Test Left Expression 934 | test_expression(&box_left, left); 935 | // Test Right Expression 936 | test_expression(&box_right, right); 937 | // Test Operator 938 | assert_eq!(operator, *op); 939 | } 940 | _ => assert!(false), 941 | } 942 | } 943 | 944 | #[test] 945 | fn test_boolean_expression() { 946 | let input = "true;"; 947 | 948 | let lexer = Lexer::new(input.to_string()); 949 | let mut parser = Parser::new(lexer); 950 | 951 | let mut program = parser.parse_program(); 952 | 953 | // Check for errors 954 | for err in parser.errors.iter() { 955 | println!("{:?}", err.as_ref().unwrap_err()); 956 | } 957 | 958 | assert_eq!(parser.errors.len(), 0); 959 | 960 | assert_eq!(program.statements.len(), 1); 961 | 962 | let statement = program.statements.pop().expect("Expected Statement!"); 963 | match statement { 964 | ast::Statement::Expr(expr) => match expr { 965 | ast::Expr::Bool(value) => { 966 | assert_eq!(value, true); 967 | } 968 | _ => assert!(false), 969 | }, 970 | _ => assert!(false), 971 | } 972 | } 973 | 974 | #[test] 975 | fn test_if_expression() { 976 | let input = "if (x < y) { x };"; 977 | 978 | let lexer = Lexer::new(input.to_string()); 979 | let mut parser = Parser::new(lexer); 980 | 981 | let mut program = parser.parse_program(); 982 | 983 | // Check for errors 984 | for err in parser.errors.iter() { 985 | println!("{:?}", err.as_ref().unwrap_err()); 986 | } 987 | 988 | assert_eq!(parser.errors.len(), 0); 989 | 990 | assert_eq!(program.statements.len(), 1); 991 | 992 | let statement = program.statements.pop().expect("Expected Statement!"); 993 | match statement { 994 | ast::Statement::Expr(expr) => match expr { 995 | ast::Expr::If(condition, if_block, else_option) => { 996 | // Test the condition 997 | test_infix_expression(&condition, &ident("x"), TokenType::LT, &ident("y")); 998 | 999 | // Check the if block 1000 | match *if_block { 1001 | ast::Statement::BlockStatement(mut vec) => { 1002 | assert_eq!(vec.len(), 1); 1003 | match vec.pop().expect("Nothing in Vec") { 1004 | ast::Statement::Expr(expr) => { 1005 | test_identifier(&expr, &"x".to_string()) 1006 | } 1007 | _ => assert!(false), 1008 | } 1009 | } 1010 | _ => assert!(false), 1011 | }; 1012 | 1013 | // Make sure else is empty 1014 | match else_option { 1015 | Some(_) => assert!(false), 1016 | None => (), 1017 | } 1018 | } 1019 | _ => assert!(false), 1020 | }, 1021 | _ => assert!(false), 1022 | } 1023 | } 1024 | 1025 | #[test] 1026 | fn test_if_else_expression() { 1027 | let input = "if (x < y) { x } else { y };"; 1028 | 1029 | let lexer = Lexer::new(input.to_string()); 1030 | let mut parser = Parser::new(lexer); 1031 | 1032 | let mut program = parser.parse_program(); 1033 | 1034 | // Check for errors 1035 | for err in parser.errors.iter() { 1036 | println!("{:?}", err.as_ref().unwrap_err()); 1037 | } 1038 | 1039 | assert_eq!(parser.errors.len(), 0); 1040 | 1041 | assert_eq!(program.statements.len(), 1); 1042 | 1043 | let statement = program.statements.pop().expect("Expected Statement!"); 1044 | match statement { 1045 | ast::Statement::Expr(expr) => match expr { 1046 | ast::Expr::If(condition, if_block, else_option) => { 1047 | // Test the condition 1048 | test_infix_expression(&condition, &ident("x"), TokenType::LT, &ident("y")); 1049 | 1050 | // Check the if block 1051 | match *if_block { 1052 | ast::Statement::BlockStatement(mut vec) => { 1053 | assert_eq!(vec.len(), 1); 1054 | match vec.pop().expect("Nothing in Vec") { 1055 | ast::Statement::Expr(expr) => { 1056 | test_identifier(&expr, &"x".to_string()) 1057 | } 1058 | _ => assert!(false), 1059 | } 1060 | } 1061 | _ => assert!(false), 1062 | }; 1063 | 1064 | // Check Else Block 1065 | match else_option { 1066 | Some(else_block) => match *else_block { 1067 | ast::Statement::BlockStatement(mut vec) => { 1068 | assert_eq!(vec.len(), 1); 1069 | match vec.pop().expect("Nothing in Vec") { 1070 | ast::Statement::Expr(expr) => { 1071 | test_identifier(&expr, &"y".to_string()) 1072 | } 1073 | _ => assert!(false), 1074 | } 1075 | } 1076 | _ => assert!(false), 1077 | }, 1078 | None => assert!(false), 1079 | } 1080 | } 1081 | _ => assert!(false), 1082 | }, 1083 | _ => assert!(false), 1084 | } 1085 | } 1086 | 1087 | #[test] 1088 | fn test_function_literal_parsing() { 1089 | let input = "fn(x, y) { x + y; }"; 1090 | 1091 | let lexer = Lexer::new(input.to_string()); 1092 | let mut parser = Parser::new(lexer); 1093 | 1094 | let mut program = parser.parse_program(); 1095 | 1096 | // Check for errors 1097 | for err in parser.errors.iter() { 1098 | println!("{:?}", err.as_ref().unwrap_err()); 1099 | } 1100 | 1101 | assert_eq!(parser.errors.len(), 0); 1102 | 1103 | assert_eq!(program.statements.len(), 1); 1104 | 1105 | let statement = program.statements.pop().expect("Expected Statement!"); 1106 | 1107 | match statement { 1108 | ast::Statement::Expr(expr) => match expr { 1109 | ast::Expr::FunctionLiteral(mut parameters, body_box) => { 1110 | // Test Parameters 1111 | assert_eq!(parameters.len(), 2); 1112 | 1113 | assert_eq!(parameters.remove(0), "x"); 1114 | assert_eq!(parameters.remove(0), "y"); 1115 | 1116 | // Test Body 1117 | match *body_box { 1118 | ast::Statement::BlockStatement(mut statements) => { 1119 | assert_eq!(statements.len(), 1); 1120 | match statements.pop() { 1121 | Some(stmnt) => match stmnt { 1122 | ast::Statement::Expr(expr) => test_infix_expression( 1123 | &expr, 1124 | &ident("x"), 1125 | TokenType::PLUS, 1126 | &ident("y"), 1127 | ), 1128 | _ => assert!(false), 1129 | }, 1130 | None => assert!(false), 1131 | } 1132 | } 1133 | _ => assert!(false), 1134 | } 1135 | } 1136 | _ => assert!(false), 1137 | }, 1138 | _ => assert!(false), 1139 | } 1140 | } 1141 | 1142 | #[test] 1143 | fn test_call_expresion_parsing() { 1144 | let input = "add(1, 2 * 3, 4 + 5)"; 1145 | 1146 | let lexer = Lexer::new(input.to_string()); 1147 | let mut parser = Parser::new(lexer); 1148 | 1149 | let mut program = parser.parse_program(); 1150 | 1151 | // Check for errors 1152 | for err in parser.errors.iter() { 1153 | println!("{:?}", err.as_ref().unwrap_err()); 1154 | } 1155 | 1156 | assert_eq!(parser.errors.len(), 0); 1157 | 1158 | assert_eq!(program.statements.len(), 1); 1159 | 1160 | let statement = program.statements.pop().expect("Expected Statement!"); 1161 | 1162 | match statement { 1163 | ast::Statement::Expr(expr) => match expr { 1164 | ast::Expr::CallExpression { 1165 | function: boxed_func, 1166 | arguments: mut args, 1167 | } => { 1168 | test_identifier(&boxed_func, &"add".to_string()); 1169 | 1170 | test_integer_literal(&args.remove(0), 1); 1171 | test_infix_expression(&args.remove(0), &int(2), TokenType::ASTERISK, &int(3)); 1172 | test_infix_expression(&args.remove(0), &int(4), TokenType::PLUS, &int(5)); 1173 | } 1174 | _ => assert!(false), 1175 | }, 1176 | _ => assert!(false), 1177 | } 1178 | } 1179 | 1180 | #[test] 1181 | fn test_string_literal_expression() { 1182 | let input = r#" "hello world" "#; 1183 | 1184 | let lexer = Lexer::new(input.to_string()); 1185 | let mut parser = Parser::new(lexer); 1186 | 1187 | let mut program = parser.parse_program(); 1188 | 1189 | // Check for errors 1190 | for err in parser.errors.iter() { 1191 | println!("{:?}", err.as_ref().unwrap_err()); 1192 | } 1193 | 1194 | assert_eq!(parser.errors.len(), 0); 1195 | 1196 | assert_eq!(program.statements.len(), 1); 1197 | 1198 | let statement = program.statements.pop().expect("Expected Statement!"); 1199 | 1200 | match statement { 1201 | ast::Statement::Expr(expr) => match expr { 1202 | ast::Expr::String(s) => assert_eq!(s, "hello world".to_string()), 1203 | _ => assert!(false, "Expression is {:?} instead of String", expr), 1204 | }, 1205 | _ => assert!(false, "Statement is {:?} instead of Expr", statement), 1206 | } 1207 | } 1208 | 1209 | #[test] 1210 | fn test_array_literal_parsing() { 1211 | let input = "[1, 2 * 2, 3 + 3]"; 1212 | 1213 | let lexer = Lexer::new(input.to_string()); 1214 | let mut parser = Parser::new(lexer); 1215 | 1216 | let mut program = parser.parse_program(); 1217 | 1218 | // Check for errors 1219 | for err in parser.errors.iter() { 1220 | println!("{:?}", err.as_ref().unwrap_err()); 1221 | } 1222 | 1223 | assert_eq!(parser.errors.len(), 0); 1224 | 1225 | assert_eq!(program.statements.len(), 1); 1226 | 1227 | let statement = program.statements.pop().expect("Expected Statement!"); 1228 | 1229 | match statement { 1230 | ast::Statement::Expr(expr) => match expr { 1231 | ast::Expr::ArrayLiteral(mut elements) => { 1232 | assert_eq!(elements.len(), 3); 1233 | test_infix_expression(&elements.remove(2), &int(3), TokenType::PLUS, &int(3)); 1234 | test_infix_expression( 1235 | &elements.remove(1), 1236 | &int(2), 1237 | TokenType::ASTERISK, 1238 | &int(2), 1239 | ); 1240 | test_integer_literal(&elements.remove(0), 1); 1241 | } 1242 | _ => assert!(false, "Expression is {:?} instead of Array", expr), 1243 | }, 1244 | _ => assert!(false, "Statement is {:?} instead of Expr", statement), 1245 | } 1246 | } 1247 | 1248 | #[test] 1249 | fn test_array_index_parsing() { 1250 | let input = "arr[1 + 1]"; 1251 | 1252 | let lexer = Lexer::new(input.to_string()); 1253 | let mut parser = Parser::new(lexer); 1254 | 1255 | let mut program = parser.parse_program(); 1256 | 1257 | // Check for errors 1258 | for err in parser.errors.iter() { 1259 | println!("{:?}", err.as_ref().unwrap_err()); 1260 | } 1261 | 1262 | assert_eq!(parser.errors.len(), 0); 1263 | 1264 | assert_eq!(program.statements.len(), 1); 1265 | 1266 | let statement = program.statements.pop().expect("Expected Statement!"); 1267 | 1268 | match statement { 1269 | ast::Statement::Expr(expr) => match expr { 1270 | ast::Expr::Index(left, index) => { 1271 | test_identifier(&left, &"arr".to_string()); 1272 | test_infix_expression(&index, &int(1), TokenType::PLUS, &int(1)); 1273 | } 1274 | _ => assert!(false, "Expression is {:?} instead of Index", expr), 1275 | }, 1276 | _ => assert!(false, "Statement is {:?} instead of Expr", statement), 1277 | } 1278 | } 1279 | 1280 | fn string(s: &str) -> ast::Expr { 1281 | ast::Expr::String(s.to_string()) 1282 | } 1283 | 1284 | #[test] 1285 | fn test_hash_literal_parsing_string_keys() { 1286 | let input = r#"{"one":1, true:2, 3:3}"#; 1287 | 1288 | let lexer = Lexer::new(input.to_string()); 1289 | let mut parser = Parser::new(lexer); 1290 | 1291 | let mut program = parser.parse_program(); 1292 | 1293 | // Check for errors 1294 | for err in parser.errors.iter() { 1295 | println!("{:?}", err.as_ref().unwrap_err()); 1296 | } 1297 | 1298 | assert_eq!(parser.errors.len(), 0); 1299 | 1300 | assert_eq!(program.statements.len(), 1); 1301 | 1302 | let statement = program.statements.pop().expect("Expected Statement!"); 1303 | 1304 | match statement { 1305 | ast::Statement::Expr(expr) => match expr { 1306 | ast::Expr::HashLiteral(keys) => { 1307 | assert_eq!(keys.len(), 3); 1308 | 1309 | assert_eq!(keys[0].0, string("one")); 1310 | assert_eq!(keys[0].1, int(1)); 1311 | 1312 | assert_eq!(keys[1].0, bool(true)); 1313 | assert_eq!(keys[1].1, int(2)); 1314 | 1315 | assert_eq!(keys[2].0, int(3)); 1316 | assert_eq!(keys[2].1, int(3)); 1317 | } 1318 | _ => assert!(false, "Expression is {:?} instead of HashLiteral", expr), 1319 | }, 1320 | _ => assert!(false, "Statement is {:?} instead of Expr", statement), 1321 | } 1322 | } 1323 | 1324 | #[test] 1325 | fn test_hash_literal_parsing_empty() { 1326 | let input = r#"{}"#; 1327 | 1328 | let lexer = Lexer::new(input.to_string()); 1329 | let mut parser = Parser::new(lexer); 1330 | 1331 | let mut program = parser.parse_program(); 1332 | 1333 | // Check for errors 1334 | for err in parser.errors.iter() { 1335 | println!("{:?}", err.as_ref().unwrap_err()); 1336 | } 1337 | 1338 | assert_eq!(parser.errors.len(), 0); 1339 | 1340 | assert_eq!(program.statements.len(), 1); 1341 | 1342 | let statement = program.statements.pop().expect("Expected Statement!"); 1343 | 1344 | match statement { 1345 | ast::Statement::Expr(expr) => match expr { 1346 | ast::Expr::HashLiteral(keys) => { 1347 | assert_eq!(keys.len(), 0); 1348 | } 1349 | _ => assert!(false, "Expression is {:?} instead of HashLiteral", expr), 1350 | }, 1351 | _ => assert!(false, "Statement is {:?} instead of Expr", statement), 1352 | } 1353 | } 1354 | } 1355 | -------------------------------------------------------------------------------- /src/repl/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod repl; 2 | -------------------------------------------------------------------------------- /src/repl/repl.rs: -------------------------------------------------------------------------------- 1 | use crate::evaluate::evaluate::Evaluator; 2 | use crate::evaluate::object::Object; 3 | use crate::lexer::lexer::Lexer; 4 | use crate::parser::parser::Parser; 5 | 6 | use std::io::{self}; 7 | 8 | const PROMPT: &str = "$> "; 9 | 10 | pub fn start() { 11 | println!("Welcome to the Monkey REPL!"); 12 | 13 | // REPL Loop 14 | let mut evaluator = Evaluator::new(); 15 | loop { 16 | // Print prompt and flush to write it to console 17 | print!("{}", PROMPT); 18 | io::Write::flush(&mut io::stdout()).expect("flush failed!"); 19 | 20 | // Scan Input line 21 | let mut buffer = String::new(); 22 | match io::stdin().read_line(&mut buffer) { 23 | Err(error) => { 24 | println!("error: {}", error); 25 | return; 26 | } 27 | Ok(_) => (), 28 | } 29 | 30 | // Create Lexer 31 | let lexer = Lexer::new(buffer); 32 | let mut parser = Parser::new(lexer); 33 | 34 | let mut program = parser.parse_program(); 35 | 36 | if parser.errors.len() > 0 { 37 | for err in parser.errors.iter() { 38 | println!("{:?}", err.as_ref().unwrap_err()); 39 | } 40 | continue; 41 | } 42 | 43 | let outcome = evaluator.eval_program(&mut program); 44 | match outcome { 45 | Object::Null => (), 46 | _ => println!("{}", outcome.inspect()), 47 | }; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/tokens/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod tokens; 2 | -------------------------------------------------------------------------------- /src/tokens/tokens.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | #[derive(Debug, PartialEq)] 4 | pub struct Token { 5 | pub token_type: TokenType, 6 | pub literal: String, 7 | } 8 | 9 | impl Token { 10 | pub fn new(token_type: TokenType, literal: String) -> Token { 11 | Token { 12 | token_type: token_type, 13 | literal: literal, 14 | } 15 | } 16 | } 17 | 18 | pub fn lookup_identifier(ident: &str) -> TokenType { 19 | match ident { 20 | // General Keywords 21 | "let" => TokenType::LET, 22 | "fn" => TokenType::FUNCTION, 23 | "return" => TokenType::RETURN, 24 | 25 | // Conditional Statement Keywords 26 | "if" => TokenType::IF, 27 | "else" => TokenType::ELSE, 28 | 29 | // Boolean Keywords 30 | "true" => TokenType::TRUE, 31 | "false" => TokenType::FALSE, 32 | 33 | // Normal identifier 34 | _ => TokenType::IDENT, 35 | } 36 | } 37 | 38 | #[allow(non_camel_case_types)] 39 | #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] 40 | pub enum TokenType { 41 | ILLEGAL, 42 | EOF, 43 | 44 | // Identifiers 45 | IDENT, // Add, foobar, x, y, etc 46 | INT, // Integers such as 1, 33 or 89 47 | STRING, 48 | 49 | // Operators 50 | ASSIGN, // '=' 51 | PLUS, // '+' 52 | MINUS, // '-' 53 | BANG, // '!' 54 | ASTERISK, // '*' 55 | SLASH, // '/' 56 | 57 | LT, // '<' 58 | GT, // '>' 59 | 60 | EQ, // '==' 61 | NOT_EQ, // '!=' 62 | 63 | // Delimiters 64 | COMMA, // ',' 65 | SEMICOLON, // ';' 66 | COLON, // ':' 67 | 68 | LPAREN, // '(' 69 | RPAREN, // ')' 70 | LBRACE, // '{' 71 | RBRACE, // '}' 72 | LBRACKET, // '[' 73 | RBRACKET, // ']' 74 | 75 | // General Keywords 76 | FUNCTION, // Function declaration 77 | LET, // Variable declaration 78 | RETURN, // return declaration 79 | 80 | // Conditional Statement Keywords 81 | IF, // If statement 82 | ELSE, // Else statement 83 | 84 | // Boolean Keywords 85 | TRUE, // true boolean 86 | FALSE, // false boolean 87 | } 88 | 89 | impl fmt::Display for TokenType { 90 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 91 | match self { 92 | TokenType::ASTERISK => write!(f, "*"), 93 | TokenType::SLASH => write!(f, "/"), 94 | TokenType::PLUS => write!(f, "+"), 95 | TokenType::MINUS => write!(f, "-"), 96 | TokenType::GT => write!(f, ">"), 97 | TokenType::LT => write!(f, "<"), 98 | TokenType::EQ => write!(f, "=="), 99 | TokenType::NOT_EQ => write!(f, "!="), 100 | _ => write!(f, "{:?}", self), 101 | } 102 | } 103 | } 104 | --------------------------------------------------------------------------------