├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── automate_error_message_documentation.py ├── docs ├── error_messages.txt ├── operators.txt ├── predefined_name_list.txt ├── tokens.txt └── valid_characters.txt ├── examples ├── calculate_average_in_array ├── counter ├── guessing_game ├── hello_world └── reverse_array ├── src ├── execution.rs ├── main.rs ├── predefined_name_order.in ├── tokenizer.rs └── verine_expression.rs └── to_do.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /test_files 3 | /.vscode -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "minosrus_lang" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minosrus_lang" 3 | version = "0.1.0" 4 | authors = ["Tix3Dev"] 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 | # minosrus_lang 2 | 3 | **DISCLAIMER: This project is still in development. That means some examples don't work yet.** 4 | 5 | A simple interpreter for my own programming language called minosrus_lang. 6 | 7 | It's inspired by BASIC but also by new languages like Rust. In the future the interpreter will (eventually) be used for an OS. And it's my first project in Rust. 8 | 9 | Short Paradigm: 10 | - Imperative 11 | - Procedural 12 | - Structured 13 | 14 | ---- 15 | 16 | ## Installing 17 | 18 | - You can simply clone this repository with ```git clone https://github.com/Tix3Dev/minosrus_lang```. 19 | - You need to be able to execute rust code. To install rust have a look at the [instructions](https://www.rust-lang.org/tools/install) of the official rust-lang page. 20 | - To build the project execute ```cargo build --release```. The path to the executable is now ```/minosrus_lang/target/release/minosrus_lang```. 21 | 22 | **IMPORTANT: If you decided to try this out, and you get this error: ```SOMEHOW THIS SHOULDN'T BE PRINTED!```, please create a new issue and post your input + the output. Thank you!** 23 | 24 | ---- 25 | 26 | ## Examples 27 | 28 | Repl: 29 | ``` 30 | > PRINT "HELLO WORLD" 31 | ``` 32 | Output: 33 | ``` 34 | HELLO WORLD 35 | ``` 36 | 37 | Repl: 38 | ``` 39 | > LET COUNTER = 1 40 | > WHILE COUNTER <= 5 START 41 | > PRINT ( "COUNTER: " + STRING_FROM COUNTER ) 42 | > LET COUNTER = ( COUNTER + 1 ) 43 | > END 44 | ``` 45 | Output: 46 | ``` 47 | COUNTER: 1 48 | COUNTER: 2 49 | COUNTER: 3 50 | COUNTER: 4 51 | COUNTER: 5 52 | ``` 53 | 54 | For more examples have a look at the [examples](https://github.com/Tix3Dev/minosrus_lang/tree/master/examples) folder. 55 | 56 | ---- 57 | 58 | ## Documentation 59 | 60 | To get a better understanding of the language, you might consider looking at the documentation. Here is the link to the [docs](https://github.com/Tix3Dev/minosrus_lang/tree/master/docs). 61 | 62 | ---- 63 | 64 | ## Contributing 65 | 66 | This is mainly a personal project, so if you want to contribute, please let me know first so we can talk about your desire. 67 | 68 | Big thanks to [Rosca Alex](https://github.com/roscale) who is in account for verines and who is a really talented programmer. 69 | -------------------------------------------------------------------------------- /automate_error_message_documentation.py: -------------------------------------------------------------------------------- 1 | # extracting possible error lines from files 2 | 3 | """ 4 | file_names = ["src/main.rs", "src/tokenizer.rs", "src/execution.rs"] 5 | keywords = ["INPUT ERROR", "INDENTATION ERROR", "BLOCK CODE ISN'T CLOSED", "INPUT INCLUDES LOWERCASE CHARACTER", "INTERPRETER ERROR", "ERROR_MESSAGE", "EXECUTION ERROR"] 6 | 7 | for file_name in file_names: 8 | file = open(file_name, "r") 9 | lines = file.readlines() 10 | line_number = 0 11 | 12 | lines_with_keywords = [] 13 | 14 | for line in lines: 15 | line = line.strip() 16 | for keyword in keywords: 17 | if keyword in line: 18 | lines_with_keywords.append(line) 19 | line_number += 1 20 | 21 | print("-----\nfile_name: {}\n-----".format(file_name)) 22 | for line_with_keyword in lines_with_keywords: 23 | print("{}: {}\n".format(line_number, line_with_keyword)) 24 | """ 25 | 26 | # when all valid lines are in docs/error_messages.txt delete duplicates and write to new file 27 | 28 | file = open("docs/error_messages.txt", "r") 29 | lines = file.readlines() 30 | 31 | all_lines = [] 32 | for line in lines: 33 | all_lines.append(line) 34 | all_lines = list(dict.fromkeys(all_lines)) 35 | 36 | # new_file = open("docs/error_messages_duplicate.txt", "w") 37 | new_file = open("docs/error_messages.txt", "w") 38 | 39 | for line in all_lines: 40 | new_file.writelines(line) 41 | new_file.write("\n") 42 | 43 | new_file.close() 44 | -------------------------------------------------------------------------------- /docs/error_messages.txt: -------------------------------------------------------------------------------- 1 | Syntax Error: 2 | 3 | - SYNTAX ERROR: A [ ALWAYS NEEDS A ] ! ORDER: [ AND THEN ] ! 4 | 5 | - SYNTAX ERROR: A ] ALWAYS NEEDS A [ ! ORDER: [ AND THEN ] ! 6 | 7 | - SYNTAX ERROR: INVALID CHARACTER INSIDE OF THE STRING! 8 | 9 | - SYNTAX ERROR: STRING ISN'T CLOSED! 10 | 11 | - SYNTAX ERROR: NEGATIVE SIGN HAS TO BE AT THE BEGINNING OF A NUMBER! 12 | 13 | - SYNTAX ERROR: DOT CAN'T BE AT THE BEGINNING OF THE NUMBER! 14 | 15 | - SYNTAX ERROR: DOT CAN'T BE AT THE END OF THE NUMBER! 16 | 17 | - SYNTAX ERROR: THERE ARE TOO MANY DOTS IN THE NUMBER! 18 | 19 | - SYNTAX ERROR: THE NUMBER IS NOT I32 OR F32! 20 | 21 | - SYNTAX ERROR: THERE IS A COMMA AT THE BEGINNING OF THE ARRAY; NOT ALLOWED! 22 | 23 | - SYNTAX ERROR: THERE IS A COMMA AT THE END OF THE ARRAY; NOT ALLOWED! 24 | 25 | - SYNTAX ERROR: STRING IN THE ARRAY ISN'T CLOSED! 26 | 27 | - SYNTAX ERROR: INVALID CHARACTER IN THE ARRAY! 28 | 29 | - SYNTAX ERROR: THERE ARE TOO MANY COMMAS IN THE ARRAY! 30 | 31 | - SYNTAX ERROR: VARIABLES IN ARRAYS CAN'T BE ARRAYS! 32 | 33 | - SYNTAX ERROR: THERE IS NO VARIABLE CALLED {}! 34 | 35 | - SYNTAX ERROR: NUMBER ELEMENTS OF AN ARRAY HAVE TO BE I32 OR F32! 36 | 37 | - SYNTAX ERROR: A COMMA IS MISSING! 38 | 39 | - SYNTAX ERROR: VARIABLE/FUNCTION NAME INCLUDES INVALID CHARACTERS! 40 | 41 | - SYNTAX ERROR: INVALID CHARACTER INSIDE OF THE STRING IN THE ARRAY! 42 | 43 | - SYNTAX ERROR: INVALID CHARACTER IN VERINE! 44 | 45 | 46 | Execution Error: 47 | 48 | - EXECUTION ERROR: BLOCK CODE ISN'T CLOSED! 49 | 50 | - EXECUTION ERROR: PROBLEMS READING USER INPUT! 51 | 52 | - EXECUTION ERROR: '{}' IS NOT A INTEGER! 53 | 54 | - EXECUTION ERROR: '{}' IS NOT A FLOAT! 55 | 56 | - EXECUTION ERROR: INVALID OPERANDS! 57 | 58 | - EXECUTION ERROR: '{}' IS NOT A VALID INDEX! 59 | 60 | - EXECUTION ERROR: INDEX IS OUT OF BOUNDS! 61 | 62 | - EXECUTION ERROR: TYPE IS NOT INDEXABLE! 63 | 64 | - EXECUTION ERROR: TYPE HAS NO LENGTH! 65 | 66 | - EXECUTION ERROR: CAN'T DIVIDE BY ZERO! 67 | 68 | - EXECUTION ERROR: VERINE RETURN TYPE IS NOT SUPPORTED! 69 | 70 | - EXECUTION ERROR: INVALID VERINE EXPRESSION! 71 | 72 | - EXECUTION ERROR: THERE ARE LESS TOKENS THAN '{}' NEEDS! 73 | 74 | - EXECUTION ERROR: THERE ARE MORE TOKENS THAN '{}' NEEDS! 75 | 76 | - EXECUTION ERROR: KEY ORDER FOR '{}' ISN'T RIGHT! 77 | 78 | - EXECUTION ERROR: VALUE ORDER FOR '{}' ISN'T RIGHT! 79 | 80 | - EXECUTION ERROR: '{}' IS NEVER AT THE BEGINNING! 81 | 82 | - EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING OR INTEGER! 83 | 84 | - EXECUTION ERROR: SECOND VARIABLE HAS TO BE A STRING OR INTEGER! 85 | 86 | - EXECUTION ERROR: FUNCTIONS CAN'T BE INSIDE OF OTHER CODE BLOCKS! 87 | 88 | - EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}! 89 | 90 | - EXECUTION ERROR: THERE ARE MORE TOKENS THAN ELSE NEEDS! 91 | 92 | - EXECUTION ERROR: EVERY LINE HAS TO START WITH A PREDEFINED NAME (EXCEPT FOR COMMENT-LINES) ! 93 | 94 | - EXECUTION ERROR: CAN'T PRINT THIS VARIABLE! 95 | 96 | - EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT! 97 | 98 | - EXECUTION ERROR: SECOND VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT! 99 | 100 | - EXECUTION ERROR: THERE IS NO FUNCTION CALLED {}! 101 | 102 | - EXECUTION ERROR: YOU HAVE TO PUSH A STRING, INTEGER OR FLOAT ONTO AN ARRAY! 103 | 104 | - EXECUTION ERROR: INDEX IS OUT OF BOUNDS! 105 | 106 | - EXECUTION ERROR: YOU HAVE TO INSERT A STRING, INTEGER OR FLOAT INTO AN ARRAY! 107 | 108 | - EXECUTION ERROR: YOU CAN ONLY INSERT INTO ARRAYS! 109 | 110 | - EXECUTION ERROR: CAN'T PRINT HELP FOR {} BECAUSE IT'S NOT A PREDEFINED NAME WHICH IS AT THE BEGINNING OF A LINE! 111 | 112 | 113 | 114 | Special Error: 115 | 116 | - INPUT ERROR: INPUT INCLUDES LOWERCASE CHARACTER! 117 | 118 | - INDENTATION ERROR! 119 | 120 | 121 | 122 | Interpreter Error: 123 | 124 | - INTERPRETER ERROR: THIRD COMMAND LINE ARGUMENT IS NOT A VALID PATH! 125 | 126 | - INPUT ERROR: SOMETHING WITH YOUR INPUT IS WRONG! 127 | 128 | - INTERPRETER ERROR: COMMAND LINE ARGUMENTS AREN'T RIGHT! 129 | 130 | 131 | 132 | Unreachable Error: 133 | 134 | - SOMEHOW THIS SHOULDN'T BE PRINTED 135 | -------------------------------------------------------------------------------- /docs/operators.txt: -------------------------------------------------------------------------------- 1 | - There are arithmetic operators and comparing operators 2 | 3 | Arithmetic operators: 4 | + | Addition 5 | - | Subtraction 6 | * | Multiplication 7 | / | Division 8 | ** | Power 9 | 10 | Comparing operators: 11 | == | Equal 12 | != | Not equal 13 | < | Less than 14 | > | Greater than 15 | <= | Less than or equal 16 | >= | Greater than or equal 17 | -------------------------------------------------------------------------------- /docs/predefined_name_list.txt: -------------------------------------------------------------------------------- 1 | - Predefined names are keywords of minosrus lang 2 | - Every line starts with a predefined name 3 | - There are predefined names, such as ONTO, that can't be at the beginning of a line 4 | 5 | List (of those that are at the beginning): 6 | - LET 7 | - Used to give a variable a value 8 | - All variables are global 9 | 10 | - Example: 11 | > LET A = 123 12 | 13 | - PRINT 14 | - Used to print a value 15 | - Can only print strings and integers 16 | 17 | - Example: 18 | > PRINT "HELLO WORLD" 19 | 20 | - FN 21 | - Used to create a function 22 | - Functions don't have parameters/return values because all variables are global 23 | 24 | - Example: 25 | > FN MAIN START 26 | > PRINT "HELLO FROM FUNCTION" 27 | > END 28 | 29 | - DO 30 | - Used to execute a function 31 | - No parameters can be passed 32 | 33 | - Example (see "FN" example): 34 | > DO MAIN 35 | 36 | - IF 37 | - Used for control flow 38 | - Compares left to right 39 | 40 | - Example: 41 | > IF 12 < 122 START 42 | > PRINT "HELLO" 43 | > END 44 | 45 | - WHILE 46 | - Used for control flow 47 | - Executes code block as long as the condition is true 48 | 49 | - Example: 50 | > LET COUNTER = 1 51 | > WHILE COUNTER <= 10 START 52 | > PRINT COUNTER 53 | > LET COUNTER = ( COUNTER + 1 ) 54 | > END 55 | 56 | - PUSH 57 | - Used to put either strings or integers on an array 58 | - Puts string/integer at the end of the array 59 | 60 | - Example: 61 | > LET A = ["TOM", "WILLY", "JERRY", "RUSTY"] 62 | > PUSH "RICK" ONTO A 63 | 64 | - POP 65 | - Used to remove the last element of an array 66 | - If array is empty, nothing happens 67 | 68 | - Example: 69 | > LET A = ["TOM", "WILLY", "JERRY", "RUSTY"] 70 | > POP FROM A 71 | 72 | - INSERT 73 | - Used to put either strings or integers on an array 74 | - Puts string/integer at position X 75 | 76 | - Example: 77 | > LET A = ["TOM", "WILLY", "JERRY", "RUSTY"] 78 | > INSERT "RICK" INTO A AT 1 79 | 80 | - REMOVE 81 | - Used to remove a string or an integer from an array 82 | - It removes it at position X 83 | 84 | - Example: 85 | > LET A = ["TOM", "WILLY", "JERRY", "RUSTY"] 86 | > REMOVE FROM A AT 1 87 | 88 | - SET 89 | - Used to change the value of an element of an array 90 | 91 | - Fails if index of element doesn't exist 92 | 93 | - Example: 94 | > LET A = ["TOM", "WILLY", "JERRY", "RUSTY"] 95 | > SET A AT 0 TO "BOB" 96 | 97 | ( 98 | RESET -> reset environment (everything that is saved gets deleted) 99 | STOP -> stop interpreter 100 | ) 101 | 102 | List (of those that are in verines): 103 | - GET 104 | - Used to get something; either case 1(length of a string/array) or case 2(character of string/element of array) 105 | - case 1: GET FROM A LEN 106 | - case 2: GET FROM A AT 0 107 | 108 | - Example: 109 | > LET A = "HELLO" 110 | > LET LEN_OF_STRING = ( GET FROM A LEN ) 111 | > LET B = [12, 34, 56, 78] 112 | > LET FIRST_ELEMENT = ( GET FROM B AT 0 ) 113 | 114 | - READLN 115 | - Used to get user input 116 | - Returns by default a string 117 | - In case you need an integer you can use INTEGER_FROM 118 | 119 | - Example: 120 | > LET USER_INPUT = ( READLN ) 121 | 122 | - STRING_FROM 123 | - Used to convert a integer to a string 124 | - Throws an EXECUTION ERROR if the given type isn't a integer 125 | 126 | - Example: 127 | > LET A = 123 128 | > LET A_AS_STRING = ( STRING_FROM A ) 129 | 130 | - INTEGER_FROM 131 | - Used to convert a string (which consists of digits) to an integer 132 | - Throws an EXECUTION ERROR if the given type isn't a string or can't be converted (because it includes invalid characters) 133 | 134 | - Example: 135 | > LET A = "123" 136 | > LET A_AS_INTEGER = ( INTEGER_FROM A ) 137 | 138 | - FLOAT_FROM 139 | - Used to convert a string (which consists of digits) to a float 140 | - Throws an EXECUTION ERROR if the given type isn't a string or can't be converted (because it includes invalid characters) 141 | 142 | - Example: 143 | > LET A = "123.123" 144 | > LET A_AS_FLOAT = ( FLOAT_FROM A ) 145 | 146 | List (of those that are in code blocks): 147 | - END 148 | - Used to go back an indentation 149 | 150 | - Example: 151 | > FN PRINT_HELLO START 152 | > PRINT "HELLO" 153 | > END 154 | 155 | - ELIF 156 | - Used to evaluate with a condition when IF returns false 157 | - Optional 158 | 159 | - Example: 160 | > LET NUM = 7 161 | > IF NUM < 5 START 162 | > PRINT "NUM IS SMALLER THAN 5" 163 | > ELIF NUM > 5 START 164 | > PRINT "NUM IS BIGGER THAN 5" 165 | > END 166 | 167 | - ELSE 168 | - Used to evaluate without a condition when IF or ELIF returns false 169 | - Optional 170 | 171 | - Example: 172 | > LET NUM = 5 173 | > IF NUM < 5 START 174 | > PRINT "NUM IS SMALLER THAN 5" 175 | > ELIF NUM > 5 START 176 | > PRINT "NUM IS BIGGER THAN 5" 177 | > ELSE 178 | > PRINT "NUM IS 5" 179 | > END 180 | 181 | -------------------------------------------------------------------------------- /docs/tokens.txt: -------------------------------------------------------------------------------- 1 | Predefined names: 2 | - e.g. LET or TRUE 3 | - Semantics: Predefined keywords 4 | 5 | 6 | 7 | Operators: 8 | - e.g. = or + or == 9 | - Semantics: In charge for arithmetic operations and for conditions 10 | 11 | 12 | 13 | Variable/function names: 14 | - e.g. FOO but not 1337 15 | - Semantics: Custom words for saving stuff 16 | 17 | 18 | 19 | Strings: 20 | - e.g. "HELLO WORLD?" 21 | - Semantics: Data type 22 | Single characters or multiple characters 23 | 24 | 25 | 26 | Integers: 27 | - e.g. 352 or -72727 28 | - Semantics: Data type 29 | Numbers which are always signed and 32 bit -> i32 30 | 31 | 32 | 33 | Floats: 34 | - e.g. 352.001 or -72727.1337 35 | - Semantics: Data type 36 | Numbers which are always signed and 32 bit -> f32 37 | - Note: Dots are used; no commas 38 | 39 | 40 | 41 | Arrays: 42 | - e.g. [21, -211, 3828] 43 | - Semantics: Multiple strings, integers or floats; dynamic 44 | - Manipulating: PUSH 121 ONTO A 45 | - Manipulating: POP FROM A 46 | - Manipulating: INSERT 121 INTO A AT 0 47 | - Manipulating: REMOVE FROM A AT 1 48 | - Note: Can't print an array; just elements 49 | - Note: Variables are supported; e.g. [A, 32, "HELLO"] where A is a variable 50 | 51 | 52 | Comments: 53 | - e.g. # HELLO, I AM A COMMENT 54 | - Semantics: The # is always at the beginning of a line; everything after it gets ignored 55 | 56 | 57 | 58 | Verines: 59 | - e.g. ( 12 ** 2 + 6 ) 60 | - Semantics: The inner part of a verine gets evaluated and will have one value. 61 | - Note: You can use this everywhere where you would normally write a string or an integer. 62 | 63 | 64 | 65 | Some words about code blocks: 66 | - Every line with indentation is only syntax check; once you fully go back (indentation = 0), there are 3 cases: 67 | - Case 1: It's a function -> nothing wil be executed until you call it with DO 68 | - Case 2: The condition for IF/WHILE is true -> execution 69 | - Case 3: The condition for IF/WHILE is false -> no execution 70 | -------------------------------------------------------------------------------- /docs/valid_characters.txt: -------------------------------------------------------------------------------- 1 | Allowed characters for the strings 2 | - A 3 | - B 4 | - C 5 | - D 6 | - E 7 | - F 8 | - G 9 | - H 10 | - I 11 | - J 12 | - K 13 | - L 14 | - M 15 | - N 16 | - O 17 | - P 18 | - Q 19 | - R 20 | - S 21 | - T 22 | - U 23 | - V 24 | - W 25 | - X 26 | - Y 27 | - Z 28 | - , 29 | - . 30 | - : 31 | - ! 32 | - ? 33 | 34 | Allowed character for variable/function names 35 | - A 36 | - B 37 | - C 38 | - D 39 | - E 40 | - F 41 | - G 42 | - H 43 | - I 44 | - J 45 | - K 46 | - L 47 | - M 48 | - N 49 | - O 50 | - P 51 | - Q 52 | - R 53 | - S 54 | - T 55 | - U 56 | - V 57 | - W 58 | - X 59 | - Y 60 | - Z 61 | - _ 62 | -------------------------------------------------------------------------------- /examples/calculate_average_in_array: -------------------------------------------------------------------------------- 1 | Repl: 2 | 3 | > LET NUM_ARRAY = [12, 78, 24, 10] 4 | > LET ARR_LEN = ( GET FROM NUM_ARRAY LEN ) 5 | > LET I = 0 6 | > LET SUM = 0 7 | > WHILE I < ARR_LEN START 8 | > LET CURRENT_ELEMENT = ( GET FROM NUM_ARRAY AT I ) 9 | > LET SUM = ( SUM + CURRENT_ELEMENT ) 10 | > LET I = ( I + 1 ) 11 | > END 12 | > LET AVERAGE = ( SUM / ARR_LEN ) 13 | > PRINT AVERAGE 14 | 15 | Output: 16 | 17 | 31 18 | -------------------------------------------------------------------------------- /examples/counter: -------------------------------------------------------------------------------- 1 | Repl: 2 | 3 | > LET COUNTER = 1 4 | > WHILE COUNTER <= 5 START 5 | > PRINT ( "COUNTER: " + COUNTER ) 6 | > LET COUNTER = ( COUNTER + 1 ) 7 | > END 8 | 9 | Output: 10 | 11 | COUNTER: 1 12 | COUNTER: 2 13 | COUNTER: 3 14 | COUNTER: 4 15 | COUNTER: 5 16 | -------------------------------------------------------------------------------- /examples/guessing_game: -------------------------------------------------------------------------------- 1 | Repl: 2 | 3 | > LET RANDOM_NUMBER = 32 4 | > # 0 = FALSE | 1 = TRUE 5 | > LET DONE = 0 6 | > PRINT "ENTER A NUMBER" 7 | > LET GUESS = ( INTEGER_FROM READLN ) 8 | > 9 | > WHILE DONE != 1 START 10 | > IF GUESS < RANDOM_NUMBER START 11 | > PRINT "TOO LOW" 12 | > PRINT "ENTER A NUMBER" 13 | > ELIF GUESS > RANDOM_NUMBER START 14 | > PRINT "TOO HIGH" 15 | > PRINT "ENTER A NUMBER" 16 | > LET GUESS = ( INTEGER_FROM READLN ) 17 | > ELSE 18 | > PRINT "YOU GUESSED IT!" 19 | > LET DONE = 1 20 | > END 21 | > END 22 | 23 | Output (with example inputs): 24 | 25 | ENTER A NUMBER 26 | 5 27 | TO LOW 28 | ENTER A NUMBER 29 | 34 30 | TO HIGH 31 | ENTER A NUMBER 32 | 31 33 | TO LOW 34 | ENTER A NUMBER 35 | 32 36 | YOU GUESSED IT! 37 | -------------------------------------------------------------------------------- /examples/hello_world: -------------------------------------------------------------------------------- 1 | Repl: 2 | 3 | > PRINT "HELLO WORLD" 4 | 5 | Output: 6 | 7 | HELLO WORLD 8 | -------------------------------------------------------------------------------- /examples/reverse_array: -------------------------------------------------------------------------------- 1 | Repl: 2 | 3 | > LET ARR = ["MY", "NAME", "IS", "RUSTY"] 4 | > LET I = 0 5 | > LET J = ( GET FROM ARR LEN - 1) 6 | > 7 | > WHILE I < J START 8 | > LET TEMP = ( GET FROM ARR AT I ) 9 | > SET ARR AT I TO ( GET FROM ARR AT J ) 10 | > SET ARR AT J TO TEMP 11 | > LET I = ( I + 1 ) 12 | > LET J = ( J - 1 ) 13 | > END 14 | 15 | Output: 16 | 17 | RUSTY 18 | IS 19 | NAME 20 | MY 21 | -------------------------------------------------------------------------------- /src/execution.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of an interpreter for a programming language called minosrus_lang 3 | * Copyright (C) 2020-2021 Yves Vollmeier 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | 21 | use crate::ExecData; 22 | use crate::tokenizer; 23 | 24 | use std::collections::HashMap; 25 | use std::process; 26 | use crate::verine_expression::{VerineTokenizer, VerineValue, Token}; 27 | use crate::tokenizer::ValueEnum; 28 | use crate::tokenizer::ArrayTypesEnum; 29 | 30 | #[derive(Clone)] 31 | enum OrderEnum { 32 | SingleOption(Vec<&'static str>), 33 | MultipleOptions(Vec>) 34 | } 35 | 36 | fn add_indentation(indentation: &mut String) { 37 | indentation.push_str(" "); 38 | } 39 | 40 | fn subtract_indentation(indentation: &mut String) { 41 | *indentation = indentation[4..].to_string(); 42 | } 43 | 44 | fn is_key_and_value_order_right(line: Vec<(String, tokenizer::ValueEnum)>) -> Result<(), String> { 45 | let clean = match &line[0].1 { 46 | tokenizer::ValueEnum::String(clean) => clean, 47 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 48 | }; 49 | 50 | let predefined_name_order = include!("predefined_name_order.in"); 51 | let value = predefined_name_order.get(&clean.as_str()).ok_or(format!("EXECUTION ERROR: '{}' IS NEVER AT THE BEGINNING!", clean))?; 52 | 53 | // check if the key of the first token has multiple options 54 | match value { 55 | OrderEnum::SingleOption(v) => { 56 | // length check - otherwise the indexing would panic 57 | if line.len() < v.len() { 58 | return Err(format!("EXECUTION ERROR: THERE ARE LESS TOKENS THAN '{}' NEEDS!", clean)); 59 | } 60 | if line.len() > v.len() { 61 | return Err(format!("EXECUTION ERROR: THERE ARE MORE TOKENS THAN '{}' NEEDS!", clean)); 62 | } 63 | 64 | // analyse if order of key and value is right 65 | let mut is_key_order_right = true; 66 | let mut is_value_order_right = true; 67 | 68 | for element_nr in 0..v.len() { 69 | // check if key is right 70 | if line[element_nr].0 != v[element_nr].split(':').nth(0).unwrap() { 71 | is_key_order_right= false; 72 | break; 73 | } 74 | // check if value is right 75 | if let tokenizer::ValueEnum::String(tc) = &line[element_nr].1 { 76 | if tc != v[element_nr].split(':').nth(1).unwrap() && v[element_nr].split(':').nth(1).unwrap() != "?" { 77 | is_value_order_right = false; 78 | break; 79 | } 80 | } 81 | } 82 | if !(is_key_order_right) { 83 | return Err(format!("EXECUTION ERROR: KEY ORDER FOR '{}' ISN'T RIGHT!", clean)); 84 | } 85 | if !(is_value_order_right) { 86 | return Err(format!("EXECUTION ERROR: VALUE ORDER FOR '{}' ISN'T RIGHT!", clean)); 87 | } 88 | }, 89 | OrderEnum::MultipleOptions(v) => { 90 | // length check - otherwise the indexing would panic 91 | let mut too_few_tokens = false; 92 | let mut too_many_tokens = false; 93 | for possibility_nr in 0..v.len() { 94 | if line.len() < v[possibility_nr].len() { 95 | too_few_tokens = true; 96 | } 97 | if line.len() > v[possibility_nr].len() { 98 | too_many_tokens = true; 99 | } 100 | } 101 | if too_few_tokens { 102 | return Err(format!("EXECUTION ERROR: THERE ARE LESS TOKENS THAN '{}' NEEDS!", clean)); 103 | } 104 | if too_many_tokens { 105 | return Err(format!("EXECUTION ERROR: THERE ARE MORE TOKENS THAN '{}' NEEDS!", clean)); 106 | } 107 | 108 | // analyse if order of key and value is right 109 | let mut is_one_token_order_right = false; 110 | let mut is_one_value_order_right = false; 111 | // iterate trough possibilitys 112 | for possibility_nr in 0..v.len() { 113 | let mut is_current_token_order_right = true; 114 | let mut is_current_value_order_right = true; 115 | 116 | for element_nr in 0..v[possibility_nr].len() { 117 | // check if key is right 118 | if line[element_nr].0 != v[possibility_nr][element_nr].split(':').nth(0).unwrap() { 119 | is_current_token_order_right = false; 120 | } 121 | // check if value is right 122 | if let tokenizer::ValueEnum::String(tc) = &line[element_nr].1 { 123 | if tc != v[possibility_nr][element_nr].split(':').nth(1).unwrap() && v[possibility_nr][element_nr].split(':').nth(1).unwrap() != "?"{ 124 | is_current_value_order_right = false; 125 | } 126 | } 127 | } 128 | if is_current_token_order_right { 129 | is_one_token_order_right = true; 130 | } 131 | if is_current_value_order_right { 132 | is_one_value_order_right = true; 133 | } 134 | } 135 | // print help - show all possible orders 136 | if !(is_one_token_order_right) { 137 | return Err(format!("EXECUTION ERROR: KEY ORDER FOR '{}' ISN'T RIGHT!", clean)); 138 | } 139 | if !(is_one_value_order_right) { 140 | return Err(format!("EXECUTION ERROR: VALUE ORDER FOR '{}' ISN'T RIGHT!", clean)); 141 | } 142 | } 143 | } 144 | 145 | Ok(()) 146 | } 147 | 148 | fn check_block_code_condition(operator: String, block_code: Vec>) -> bool { 149 | if operator == "==" { 150 | match (&block_code[0][1].1, &block_code[0][3].1) { 151 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a == b, 152 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a == b, 153 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a == b, 154 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 155 | } 156 | } 157 | else if operator == "!=" { 158 | match (&block_code[0][1].1, &block_code[0][3].1) { 159 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a != b, 160 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a != b, 161 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a != b, 162 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 163 | } 164 | } 165 | else if operator == "<" { 166 | match (&block_code[0][1].1, &block_code[0][3].1) { 167 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a < b, 168 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a < b, 169 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a < b, 170 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 171 | } 172 | } 173 | else if operator == ">" { 174 | match (&block_code[0][1].1, &block_code[0][3].1) { 175 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a > b, 176 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a > b, 177 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a > b, 178 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 179 | } 180 | } 181 | else if operator == "<=" { 182 | match (&block_code[0][1].1, &block_code[0][3].1) { 183 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a <= b, 184 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a <= b, 185 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a <= b, 186 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 187 | } 188 | } 189 | else if operator == ">=" { 190 | match (&block_code[0][1].1, &block_code[0][3].1) { 191 | (tokenizer::ValueEnum::String(a), tokenizer::ValueEnum::String(b)) => a >= b, 192 | (tokenizer::ValueEnum::Integer(a), tokenizer::ValueEnum::Integer(b)) => a >= b, 193 | (tokenizer::ValueEnum::Float(a), tokenizer::ValueEnum::Float(b)) => a >= b, 194 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 195 | } 196 | } else { 197 | unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!"); 198 | } 199 | } 200 | 201 | fn update_while_condition_values( 202 | block_code: &Vec>, 203 | global_variables: &HashMap, 204 | error: &mut String, 205 | ) -> Result>, String> { 206 | let mut block_code_clone = block_code.clone(); 207 | 208 | if block_code[0][1].0 == "VARIABLE/FUNCTION_NAME" { 209 | let variable_name = match &block_code[0][1].1 { 210 | tokenizer::ValueEnum::String(variable_name) => variable_name, 211 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 212 | }; 213 | 214 | let value_of_variable = global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 215 | 216 | match value_of_variable { 217 | tokenizer::ValueEnum::String(v) => { 218 | block_code_clone[0][1].0 = "STRING".to_string(); 219 | block_code_clone[0][1].1 = tokenizer::ValueEnum::String(v.to_string()); 220 | }, 221 | tokenizer::ValueEnum::Integer(v) => { 222 | block_code_clone[0][1].0 = "INTEGER".to_string(); 223 | block_code_clone[0][1].1 = tokenizer::ValueEnum::Integer(*v); 224 | }, 225 | _ => *error = "EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING OR INTEGER!".to_string() 226 | } 227 | } 228 | if block_code[0][3].0 == "VARIABLE/FUNCTION_NAME" { 229 | let variable_name = match &block_code[0][3].1 { 230 | tokenizer::ValueEnum::String(variable_name) => variable_name, 231 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 232 | }; 233 | 234 | let value_of_variable = global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 235 | 236 | match value_of_variable { 237 | tokenizer::ValueEnum::String(v) => { 238 | block_code_clone[0][3].0 = "STRING".to_string(); 239 | block_code_clone[0][3].1 = tokenizer::ValueEnum::String(v.to_string()); 240 | }, 241 | tokenizer::ValueEnum::Integer(v) => { 242 | block_code_clone[0][3].0 = "INTEGER".to_string(); 243 | block_code_clone[0][3].1 = tokenizer::ValueEnum::Integer(*v); 244 | }, 245 | _ => *error = "EXECUTION ERROR: SECOND VARIABLE HAS TO BE A STRING OR INTEGER!".to_string() 246 | } 247 | } 248 | 249 | Ok(block_code_clone) 250 | } 251 | 252 | impl ExecData { 253 | fn execute_block_code(&mut self, block_code: Vec>, called_from_block: bool) -> Result<(), String> { 254 | self.block_code.clear(); 255 | 256 | for line in block_code { 257 | let return_of_execution = self.exec(line, called_from_block); 258 | if let Err(e) = return_of_execution { 259 | return Err(e); 260 | } 261 | } 262 | 263 | Ok(()) 264 | } 265 | 266 | pub fn exec(&mut self, token_collection: Vec<(String, tokenizer::ValueEnum)>, called_from_while: bool) -> Result<(), String> { 267 | let mut token_collection = token_collection.clone(); 268 | // debugging purpose 269 | println!("token_collection: {:?}", token_collection); 270 | 271 | // check for syntax errors 272 | if let Some((_, value)) = token_collection.iter().find(|(key, _)| key == &"ERROR_MESSAGE") { 273 | match value { 274 | tokenizer::ValueEnum::String(v) => { 275 | return Err(format!("SYNTAX ERROR: {}", v)); 276 | }, 277 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 278 | } 279 | } 280 | // check for comments -> just make a newline 281 | if let Some((_, value)) = token_collection.iter().find(|(key, _)| key == &"COMMENT") { 282 | match value { 283 | tokenizer::ValueEnum::String(_v) => { 284 | return Ok(()); 285 | }, 286 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 287 | } 288 | } 289 | // check for reset 290 | match &token_collection[0].1 { 291 | tokenizer::ValueEnum::String(v) => { 292 | if v == "RESET" && token_collection.len() == 1 { 293 | self.global_variables.clear(); 294 | return Ok(()); 295 | } 296 | }, 297 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 298 | } 299 | 300 | let mut saved_verines = HashMap::>::new(); 301 | 302 | if called_from_while || self.indentation.is_empty() { 303 | for (i, (key, token)) in &mut token_collection.iter_mut().enumerate() { 304 | if let ValueEnum::Verine(verine) = token { 305 | saved_verines.insert(i, verine.clone()); 306 | 307 | let verine = VerineTokenizer::evaluate(verine.clone(), &self.global_variables); 308 | 309 | let push_error = |message: &str| { 310 | return Err(format!("EXECUTION ERROR: {}", message)); 311 | }; 312 | 313 | match verine { 314 | Ok(verine) => { 315 | match verine { 316 | VerineValue::String(s) => { 317 | *key = "STRING".to_string(); 318 | *token = ValueEnum::String(s); 319 | }, 320 | VerineValue::Integer(int) => { 321 | *key = "INTEGER".to_string(); 322 | *token = ValueEnum::Integer(int); 323 | }, 324 | VerineValue::Float(float) => { 325 | *key = "FLOAT".to_string(); 326 | *token = ValueEnum::Float(float); 327 | }, 328 | } 329 | } 330 | Err(error) => { 331 | use crate::verine_expression::VerineTokenizerError::*; 332 | return match error { 333 | StdInError => push_error("PROBLEMS READING USER INPUT!"), 334 | VariableNotFound(var) => push_error(&format!("THERE IS NO VARIABLE CALLED {}!", var)), 335 | NumberNotAnInteger(var) => push_error(&format!("'{}' IS NOT A INTEGER!", var)), 336 | InvalidOperands => push_error("INVALID OPERANDS!"), 337 | InvalidIndex(i) => push_error(&format!("'{}' IS NOT A VALID INDEX!", i)), 338 | IndexOutOfBounds => push_error("INDEX IS OUT OF BOUNDS!"), 339 | TypeNotIndexable => push_error("TYPE IS NOT INDEXABLE!"), 340 | TypeHasNoLength => push_error("TYPE HAS NO LENGTH!"), 341 | DivisionByZero => push_error("CAN'T DIVIDE BY ZERO!"), 342 | UnsupportedReturnType => push_error("VERINE RETURN TYPE IS NOT SUPPORTED!"), 343 | InvalidExpression => push_error("INVALID VERINE EXPRESSION!"), 344 | NumberNotAFloat(var) => push_error(&format!("'{}' IS NOT A FLOAT!", var)), 345 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 346 | } 347 | } 348 | } 349 | } 350 | } 351 | } 352 | // check for stop 353 | match &token_collection[0].1 { 354 | tokenizer::ValueEnum::String(v) => { 355 | if v == "STOP" && token_collection.len() == 1 { 356 | process::exit(1); 357 | } 358 | }, 359 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 360 | } 361 | 362 | let predefined_name_order = include!("predefined_name_order.in"); 363 | 364 | // check for code block stuff 365 | if self.indentation.to_string() != "".to_string() { 366 | let v = match &token_collection[0].1 { 367 | tokenizer::ValueEnum::String(v) => v, 368 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 369 | }; 370 | 371 | if v == "IF" || v == "WHILE" { 372 | add_indentation(&mut self.indentation); 373 | } 374 | else if v == "FN" { 375 | return Err("EXECUTION ERROR: FUNCTIONS CAN'T BE INSIDE OF OTHER CODE BLOCKS!".to_string()); 376 | } 377 | else if v == "END" && token_collection.len() == 1 { 378 | subtract_indentation(&mut self.indentation); 379 | if self.indentation.to_string() == "".to_string() { 380 | if self.current_block_type.0 == "normal" { 381 | let first_predefined_name = match &self.block_code[0][0].1 { 382 | tokenizer::ValueEnum::String(first_predefined_name) => first_predefined_name, 383 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 384 | }; 385 | 386 | if first_predefined_name == "IF" { 387 | let operator = match &self.block_code[0][2].1 { 388 | tokenizer::ValueEnum::String(operator) => operator, 389 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 390 | }; 391 | 392 | let mut else_part: Vec> = vec![]; 393 | 394 | let mut is_there_elif_block = false; 395 | let mut is_elif_block_true = false; 396 | let mut where_are_elif_blocks = vec![]; 397 | let mut where_is_right_elif_block = 0; 398 | 399 | let mut is_there_else_block = false; 400 | let mut where_is_else_block = 0; 401 | 402 | for (line_position, line) in self.block_code.iter().enumerate() { 403 | let first_token = match &line[0].1 { 404 | tokenizer::ValueEnum::String(first_token) => first_token, 405 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 406 | }; 407 | 408 | if first_token == "ELIF" { 409 | is_there_elif_block = true; 410 | where_are_elif_blocks.push(line_position); 411 | 412 | // check key and value order 413 | let return_of_check = is_key_and_value_order_right(line.to_vec()); 414 | 415 | match return_of_check { 416 | Ok(_) => (), 417 | Err(e) => return Err(format!("{}", e)) 418 | } 419 | 420 | // evaluate left and right side of elif 421 | let mut line_clone = line.clone(); 422 | 423 | if line[1].0 == "VARIABLE/FUNCTION_NAME" { 424 | let variable_name = match &line[1].1 { 425 | tokenizer::ValueEnum::String(variable_name) => variable_name, 426 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 427 | }; 428 | 429 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 430 | 431 | match value_of_variable { 432 | tokenizer::ValueEnum::String(v) => { 433 | line_clone[1].0 = "STRING".to_string(); 434 | line_clone[1].1 = tokenizer::ValueEnum::String(v.to_string()); 435 | }, 436 | tokenizer::ValueEnum::Integer(v) => { 437 | line_clone[1].0 = "INTEGER".to_string(); 438 | line_clone[1].1 = tokenizer::ValueEnum::Integer(*v); 439 | }, 440 | _ => { 441 | return Err("EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING OR INTEGER!".to_string()); 442 | } 443 | } 444 | } 445 | if line[3].0 == "VARIABLE/FUNCTION_NAME" { 446 | let variable_name = match &line[3].1 { 447 | tokenizer::ValueEnum::String(variable_name) => variable_name, 448 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 449 | }; 450 | 451 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 452 | 453 | match value_of_variable { 454 | tokenizer::ValueEnum::String(v) => { 455 | line_clone[3].0 = "STRING".to_string(); 456 | line_clone[3].1 = tokenizer::ValueEnum::String(v.to_string()); 457 | }, 458 | tokenizer::ValueEnum::Integer(v) => { 459 | line_clone[3].0 = "INTEGER".to_string(); 460 | line_clone[3].1 = tokenizer::ValueEnum::Integer(*v); 461 | }, 462 | _ => { 463 | return Err("EXECUTION ERROR: SECOND VARIABLE HAS TO BE A STRING OR INTEGER!".to_string()); 464 | } 465 | } 466 | } 467 | 468 | // check if elif condition is true 469 | match &line_clone[2].1 { 470 | tokenizer::ValueEnum::String(elif_operator) => { 471 | if check_block_code_condition(elif_operator.to_string(), vec![line_clone]) { 472 | is_elif_block_true = true; 473 | where_is_right_elif_block = where_are_elif_blocks.len() - 1; 474 | } 475 | }, 476 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 477 | } 478 | } 479 | if first_token == "ELSE" { 480 | if line.len() == 1 { 481 | is_there_else_block = true; 482 | where_is_else_block = line_position; 483 | } else { 484 | return Err("EXECUTION ERROR: THERE ARE MORE TOKENS THAN ELSE NEEDS!".to_string()); 485 | } 486 | } 487 | } 488 | let if_part = if is_there_elif_block && is_there_else_block { 489 | else_part = self.block_code[where_is_else_block+1..].to_vec(); 490 | self.block_code[1..where_are_elif_blocks[0]].to_vec() 491 | } 492 | else if is_there_elif_block { 493 | self.block_code[1..where_are_elif_blocks[0]].to_vec() 494 | } 495 | else if is_there_else_block { 496 | else_part = self.block_code[where_is_else_block+1..].to_vec(); 497 | self.block_code[1..where_is_else_block].to_vec() 498 | } else { 499 | self.block_code[1..].to_vec() 500 | }; 501 | 502 | if check_block_code_condition(operator.to_string(), self.block_code.to_vec()) { 503 | let return_of_block_code_execution = self.execute_block_code(if_part.to_vec(), false); 504 | 505 | match return_of_block_code_execution { 506 | Ok(_) => (), 507 | Err(e) => return Err(format!("{}", e)) 508 | } 509 | } 510 | else if is_there_elif_block && is_elif_block_true { 511 | let final_right_elif_position = where_are_elif_blocks[where_is_right_elif_block]+1; 512 | let elif_part = self.block_code[final_right_elif_position..final_right_elif_position+1].to_vec(); 513 | 514 | let return_of_block_code_execution = self.execute_block_code(elif_part.to_vec(), false); 515 | 516 | match return_of_block_code_execution { 517 | Ok(_) => (), 518 | Err(e) => return Err(format!("{}", e)) 519 | } 520 | } 521 | else if is_there_else_block { 522 | let return_of_block_code_execution = self.execute_block_code(else_part.to_vec(), false); 523 | 524 | match return_of_block_code_execution { 525 | Ok(_) => (), 526 | Err(e) => return Err(format!("{}", e)) 527 | } 528 | } 529 | } 530 | else if first_predefined_name == "WHILE" { 531 | let mut error = "".to_string(); 532 | 533 | let original_operator = &self.block_code[0][2].1.clone(); 534 | let operator = match original_operator { 535 | tokenizer::ValueEnum::String(operator) => operator, 536 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 537 | }; 538 | 539 | loop { 540 | let block_code_original = self.block_code.clone(); 541 | let new_block_code = update_while_condition_values(&self.block_code, &self.global_variables, &mut error)?; 542 | 543 | if check_block_code_condition(operator.to_string(), new_block_code) { 544 | let return_of_block_code_execution = self.execute_block_code(self.block_code[1..].to_vec(), true); 545 | 546 | match return_of_block_code_execution { 547 | Ok(_) => (), 548 | Err(e) => return Err(format!("{}", e)) 549 | } 550 | } else { 551 | break; 552 | } 553 | 554 | self.block_code = block_code_original; 555 | } 556 | } 557 | 558 | self.current_block_type.0 = "".to_string(); 559 | } 560 | else if self.current_block_type.0 == "function" { 561 | self.current_block_type.0 = "".to_string(); 562 | self.current_block_type.1 = "".to_string(); 563 | } 564 | } 565 | } 566 | 567 | // saving code block stuff 568 | if self.current_block_type.0 == "normal" { 569 | for (i, tokens) in saved_verines { 570 | token_collection[i].0 = "VERINE".to_string(); 571 | token_collection[i].1 = ValueEnum::Verine(tokens); 572 | } 573 | 574 | self.block_code.push(token_collection.clone()); 575 | } 576 | else if self.current_block_type.0 == "function" { 577 | for (i, tokens) in saved_verines { 578 | token_collection[i].0 = "VERINE".to_string(); 579 | token_collection[i].1 = ValueEnum::Verine(tokens); 580 | } 581 | 582 | self.functions.get_mut(&self.current_block_type.1).unwrap().push(token_collection.clone()); 583 | } 584 | 585 | return Ok(()); 586 | } 587 | 588 | 589 | // *check order of keys and values + evaluation* // 590 | 591 | let first_key_element = &token_collection[0].0; 592 | 593 | if first_key_element != "PREDEFINED_NAME" { 594 | return Err("EXECUTION ERROR: EVERY LINE HAS TO START WITH A PREDEFINED NAME (EXCEPT FOR COMMENT-LINES) !".to_string()); 595 | } 596 | 597 | // check if key and value order is right 598 | let return_of_check = is_key_and_value_order_right(token_collection.to_vec()); 599 | 600 | match return_of_check { 601 | Ok(_) => (), 602 | Err(e) => return Err(format!("{}", e)) 603 | } 604 | 605 | // evaluate value for LET, PRINT, IF, PUSH, INSERT, SET 606 | if token_collection.len() > 0 { 607 | let mut token_collection_clone = token_collection.clone(); 608 | 609 | if let tokenizer::ValueEnum::String(clean) = &token_collection[0].1 { 610 | let value = predefined_name_order.get(&clean.as_str()).ok_or(format!("EXECUTION ERROR: '{}' IS NEVER AT THE BEGINNING!", clean))?; 611 | 612 | if let OrderEnum::MultipleOptions(_v) = value { 613 | if let tokenizer::ValueEnum::String(fv) = &token_collection[0].1 { 614 | if fv == "LET" { 615 | if token_collection[3].0 == "VARIABLE/FUNCTION_NAME" { 616 | let variable_name = match &token_collection[3].1 { 617 | tokenizer::ValueEnum::String(variable_name) => variable_name, 618 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 619 | }; 620 | 621 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 622 | 623 | match value_of_variable { 624 | tokenizer::ValueEnum::String(v) => { 625 | token_collection_clone[3].0 = "STRING".to_string(); 626 | token_collection_clone[3].1 = tokenizer::ValueEnum::String(v.to_string()); 627 | }, 628 | tokenizer::ValueEnum::Integer(v) => { 629 | token_collection_clone[3].0 = "INTEGER".to_string(); 630 | token_collection_clone[3].1 = tokenizer::ValueEnum::Integer(*v); 631 | }, 632 | tokenizer::ValueEnum::Float(v) => { 633 | token_collection_clone[3].0 = "FLOAT".to_string(); 634 | token_collection_clone[3].1 = tokenizer::ValueEnum::Float(*v); 635 | }, 636 | tokenizer::ValueEnum::Array(v) => { 637 | token_collection_clone[3].0 = "ARRAY".to_string(); 638 | token_collection_clone[3].1 = tokenizer::ValueEnum::Array(v.to_vec()); 639 | } 640 | tokenizer::ValueEnum::Verine(_) => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 641 | } 642 | } 643 | } 644 | if fv == "PRINT" { 645 | if token_collection[1].0 == "VARIABLE/FUNCTION_NAME" { 646 | let variable_name = match &token_collection[1].1 { 647 | tokenizer::ValueEnum::String(variable_name) => variable_name, 648 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 649 | }; 650 | 651 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 652 | 653 | match value_of_variable { 654 | tokenizer::ValueEnum::String(v) => { 655 | token_collection_clone[1].0 = "STRING".to_string(); 656 | token_collection_clone[1].1 = tokenizer::ValueEnum::String(v.to_string()); 657 | }, 658 | tokenizer::ValueEnum::Integer(v) => { 659 | token_collection_clone[1].0 = "INTEGER".to_string(); 660 | token_collection_clone[1].1 = tokenizer::ValueEnum::Integer(*v); 661 | }, 662 | tokenizer::ValueEnum::Float(v) => { 663 | token_collection_clone[1].0 = "FLOAT".to_string(); 664 | token_collection_clone[1].1 = tokenizer::ValueEnum::Float(*v); 665 | }, 666 | _ => return Err("EXECUTION ERROR: CAN'T PRINT THIS VARIABLE!".to_string()) 667 | } 668 | } 669 | } 670 | else if fv == "IF" { 671 | if token_collection[1].0 == "VARIABLE/FUNCTION_NAME" { 672 | let variable_name = match &token_collection[1].1 { 673 | tokenizer::ValueEnum::String(variable_name) => variable_name, 674 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 675 | }; 676 | 677 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 678 | 679 | match value_of_variable { 680 | tokenizer::ValueEnum::String(v) => { 681 | token_collection_clone[1].0 = "STRING".to_string(); 682 | token_collection_clone[1].1 = tokenizer::ValueEnum::String(v.to_string()); 683 | }, 684 | tokenizer::ValueEnum::Integer(v) => { 685 | token_collection_clone[1].0 = "INTEGER".to_string(); 686 | token_collection_clone[1].1 = tokenizer::ValueEnum::Integer(*v); 687 | }, 688 | tokenizer::ValueEnum::Float(v) => { 689 | token_collection_clone[1].0 = "FLOAT".to_string(); 690 | token_collection_clone[1].1 = tokenizer::ValueEnum::Float(*v); 691 | }, 692 | _ => return Err("EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT!".to_string()) 693 | } 694 | } 695 | if token_collection[3].0 == "VARIABLE/FUNCTION_NAME" { 696 | let variable_name = match &token_collection[3].1 { 697 | tokenizer::ValueEnum::String(variable_name) => variable_name, 698 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 699 | }; 700 | 701 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 702 | 703 | match value_of_variable { 704 | tokenizer::ValueEnum::String(v) => { 705 | token_collection_clone[3].0 = "STRING".to_string(); 706 | token_collection_clone[3].1 = tokenizer::ValueEnum::String(v.to_string()); 707 | }, 708 | tokenizer::ValueEnum::Integer(v) => { 709 | token_collection_clone[3].0 = "INTEGER".to_string(); 710 | token_collection_clone[3].1 = tokenizer::ValueEnum::Integer(*v); 711 | }, 712 | tokenizer::ValueEnum::Float(v) => { 713 | token_collection_clone[3].0 = "FLOAT".to_string(); 714 | token_collection_clone[3].1 = tokenizer::ValueEnum::Float(*v); 715 | }, 716 | _ => return Err("EXECUTION ERROR: SECOND VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT!".to_string()) 717 | } 718 | } 719 | } 720 | else if fv == "PUSH" { 721 | if token_collection[1].0 == "VARIABLE/FUNCTION_NAME" { 722 | let variable_name = match &token_collection[1].1 { 723 | tokenizer::ValueEnum::String(variable_name) => variable_name, 724 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 725 | }; 726 | 727 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 728 | 729 | match value_of_variable { 730 | tokenizer::ValueEnum::String(v) => { 731 | token_collection_clone[1].0 = "STRING".to_string(); 732 | token_collection_clone[1].1 = tokenizer::ValueEnum::String(v.to_string()); 733 | }, 734 | tokenizer::ValueEnum::Integer(v) => { 735 | token_collection_clone[1].0 = "INTEGER".to_string(); 736 | token_collection_clone[1].1 = tokenizer::ValueEnum::Integer(*v); 737 | }, 738 | tokenizer::ValueEnum::Float(v) => { 739 | token_collection_clone[1].0 = "FLOAT".to_string(); 740 | token_collection_clone[1].1 = tokenizer::ValueEnum::Float(*v); 741 | }, 742 | _ => return Err("EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT!".to_string()) 743 | } 744 | } 745 | } 746 | else if fv == "INSERT" { 747 | if token_collection[1].0 == "VARIABLE/FUNCTION_NAME" { 748 | let variable_name = match &token_collection[1].1 { 749 | tokenizer::ValueEnum::String(variable_name) => variable_name, 750 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 751 | }; 752 | 753 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 754 | 755 | match value_of_variable { 756 | tokenizer::ValueEnum::String(v) => { 757 | token_collection_clone[1].0 = "STRING".to_string(); 758 | token_collection_clone[1].1 = tokenizer::ValueEnum::String(v.to_string()); 759 | }, 760 | tokenizer::ValueEnum::Integer(v) => { 761 | token_collection_clone[1].0 = "INTEGER".to_string(); 762 | token_collection_clone[1].1 = tokenizer::ValueEnum::Integer(*v); 763 | }, 764 | tokenizer::ValueEnum::Float(v) => { 765 | token_collection_clone[1].0 = "FLOAT".to_string(); 766 | token_collection_clone[1].1 = tokenizer::ValueEnum::Float(*v); 767 | }, 768 | _ => return Err("EXECUTION ERROR: FIRST VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT!".to_string()) 769 | } 770 | } 771 | if token_collection[5].0 == "VARIABLE/FUNCTION_NAME" { 772 | let variable_name = match &token_collection[5].1 { 773 | tokenizer::ValueEnum::String(variable_name) => variable_name, 774 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 775 | }; 776 | 777 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 778 | 779 | match value_of_variable { 780 | tokenizer::ValueEnum::Integer(v) => { 781 | token_collection_clone[5].0 = "INTEGER".to_string(); 782 | token_collection_clone[5].1 = tokenizer::ValueEnum::Integer(*v); 783 | }, 784 | _ => return Err("EXECUTION ERROR: THIRD VARIABLE HAS TO BE AN INTEGER!".to_string()) 785 | } 786 | } 787 | 788 | } 789 | else if fv == "SET" { 790 | if token_collection[3].0 == "VARIABLE/FUNCTION_NAME" { 791 | let variable_name = match &token_collection[3].1 { 792 | tokenizer::ValueEnum::String(variable_name) => variable_name, 793 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 794 | }; 795 | 796 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 797 | 798 | match value_of_variable { 799 | tokenizer::ValueEnum::Integer(v) => { 800 | token_collection_clone[3].0 = "INTEGER".to_string(); 801 | token_collection_clone[3].1 = tokenizer::ValueEnum::Integer(*v); 802 | }, 803 | _ => return Err("EXECUTION ERROR: SECOND VARIABLE HAS TO BE AN INTEGER!".to_string()) 804 | } 805 | } 806 | if token_collection[5].0 == "VARIABLE/FUNCTION_NAME" { 807 | let variable_name = match &token_collection[5].1 { 808 | tokenizer::ValueEnum::String(variable_name) => variable_name, 809 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 810 | }; 811 | 812 | let value_of_variable = self.global_variables.get(variable_name).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", variable_name))?; 813 | 814 | match value_of_variable { 815 | tokenizer::ValueEnum::String(v) => { 816 | token_collection_clone[5].0 = "STRING".to_string(); 817 | token_collection_clone[5].1 = tokenizer::ValueEnum::String(v.to_string()); 818 | }, 819 | tokenizer::ValueEnum::Integer(v) => { 820 | token_collection_clone[5].0 = "INTEGER".to_string(); 821 | token_collection_clone[5].1 = tokenizer::ValueEnum::Integer(*v); 822 | }, 823 | tokenizer::ValueEnum::Float(v) => { 824 | token_collection_clone[5].0 = "FLOAT".to_string(); 825 | token_collection_clone[5].1 = tokenizer::ValueEnum::Float(*v); 826 | }, 827 | _ => return Err("EXECUTION ERROR: THIRD VARIABLE HAS TO BE A STRING, INTEGER OR FLOAT!".to_string()) 828 | } 829 | } 830 | } 831 | } 832 | } 833 | } 834 | 835 | token_collection = token_collection_clone; 836 | } 837 | 838 | 839 | // * real execution part * // 840 | 841 | match &token_collection[0].1 { 842 | tokenizer::ValueEnum::String(v) => { 843 | if v == &"LET".to_string() { 844 | let variable_name: String = { 845 | match &token_collection[1].1 { 846 | tokenizer::ValueEnum::String(current_v) => current_v.to_string(), 847 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 848 | } 849 | }; 850 | self.global_variables.insert(variable_name, token_collection[3].1.clone()); 851 | } 852 | if v == &"PRINT".to_string() { 853 | let stuff_to_print: String = { 854 | match &token_collection[1].1 { 855 | tokenizer::ValueEnum::String(stuff) => stuff.to_string(), 856 | tokenizer::ValueEnum::Integer(stuff) => stuff.to_string(), 857 | tokenizer::ValueEnum::Float(stuff) => format!("{:?}", stuff), 858 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 859 | } 860 | }; 861 | 862 | println!("{}", stuff_to_print); 863 | } 864 | else if v == &"FN".to_string() { 865 | let fn_name = match &token_collection[1].1 { 866 | tokenizer::ValueEnum::String(fn_name) => fn_name, 867 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 868 | }; 869 | 870 | self.functions.insert(fn_name.to_string(), vec![token_collection.clone()]); 871 | self.current_block_type.0 = "function".to_string(); 872 | self.current_block_type.1 = fn_name.to_string(); 873 | 874 | add_indentation(&mut self.indentation); 875 | } 876 | else if v == &"DO".to_string() { 877 | let function_name = match &token_collection[1].1 { 878 | tokenizer::ValueEnum::String(function_name) => function_name, 879 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 880 | }; 881 | 882 | let function_code_block = self.functions.get(function_name).ok_or(format!("EXECUTION ERROR: THERE IS NO FUNCTION CALLED {}!", function_name))?; 883 | 884 | let block_code = function_code_block[1..].to_vec(); 885 | let return_of_block_code_execution = self.execute_block_code(block_code, false); 886 | 887 | match return_of_block_code_execution { 888 | Ok(_) => (), 889 | Err(e) => return Err(format!("{}", e)) 890 | } 891 | } 892 | else if v == &"IF".to_string() { 893 | self.block_code.push(token_collection); 894 | self.current_block_type.0 = "normal".to_string(); 895 | add_indentation(&mut self.indentation); 896 | } 897 | else if v == &"WHILE".to_string() { 898 | self.block_code.push(token_collection); 899 | self.current_block_type.0 = "normal".to_string(); 900 | add_indentation(&mut self.indentation); 901 | } 902 | else if v == &"PUSH".to_string() { 903 | let stuff = match &token_collection[3].1 { 904 | tokenizer::ValueEnum::String(stuff) => stuff, 905 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 906 | }; 907 | 908 | let value = self.global_variables.get(stuff).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", stuff))?; 909 | 910 | let vec = match &value { 911 | tokenizer::ValueEnum::Array(vec) => vec, 912 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 913 | }; 914 | 915 | match &token_collection[1].1 { 916 | tokenizer::ValueEnum::String(stuff_to_push) => { 917 | let mut vec_clone = vec.clone(); 918 | vec_clone.push(tokenizer::ArrayTypesEnum::String(stuff_to_push.to_string())); 919 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 920 | }, 921 | tokenizer::ValueEnum::Integer(stuff_to_push) => { 922 | let mut vec_clone = vec.clone(); 923 | vec_clone.push(tokenizer::ArrayTypesEnum::Integer(*stuff_to_push)); 924 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 925 | }, 926 | tokenizer::ValueEnum::Float(stuff_to_push) => { 927 | let mut vec_clone = vec.clone(); 928 | vec_clone.push(tokenizer::ArrayTypesEnum::Float(*stuff_to_push)); 929 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 930 | }, 931 | _ => return Err("EXECUTION ERROR: YOU HAVE TO PUSH A STRING, INTEGER OR FLOAT ONTO AN ARRAY!".to_string()) 932 | } 933 | } 934 | else if v == &"POP".to_string() { 935 | let stuff = match &token_collection[2].1 { 936 | tokenizer::ValueEnum::String(stuff) => stuff, 937 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 938 | }; 939 | 940 | let value = self.global_variables.get(stuff).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", stuff))?; 941 | 942 | let vec = match &value { 943 | tokenizer::ValueEnum::Array(vec) => vec, 944 | _ => return Err("EXECUTION ERROR: YOU CAN ONLY POP FROM ARRAYS!".to_string()) 945 | }; 946 | 947 | let mut vec_clone = vec.clone(); 948 | vec_clone.pop(); 949 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 950 | } 951 | else if v == &"INSERT".to_string() { 952 | let stuff = match &token_collection[3].1 { 953 | tokenizer::ValueEnum::String(stuff) => stuff, 954 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 955 | }; 956 | 957 | let value = self.global_variables.get(stuff).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", stuff))?; 958 | 959 | let vec = match &value { 960 | tokenizer::ValueEnum::Array(vec) => vec, 961 | _ => return Err("EXECUTION ERROR: YOU CAN ONLY INSERT INTO ARRAYS!".to_string()) 962 | }; 963 | 964 | let index = { 965 | match &token_collection[5].1 { 966 | tokenizer::ValueEnum::Integer(index_where_to_insert) => *index_where_to_insert as usize, 967 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 968 | } 969 | }; 970 | 971 | if index >= vec.len() && index != 0 { 972 | return Err("EXECUTION ERROR: INDEX IS OUT OF BOUNDS!".to_string()); 973 | } 974 | 975 | match &token_collection[1].1 { 976 | tokenizer::ValueEnum::String(stuff_to_push) => { 977 | let mut vec_clone = vec.clone(); 978 | vec_clone.insert(index, tokenizer::ArrayTypesEnum::String(stuff_to_push.to_string())); 979 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 980 | }, 981 | tokenizer::ValueEnum::Integer(stuff_to_push) => { 982 | let mut vec_clone = vec.clone(); 983 | vec_clone.insert(index, tokenizer::ArrayTypesEnum::Integer(*stuff_to_push)); 984 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 985 | }, 986 | tokenizer::ValueEnum::Float(stuff_to_push) => { 987 | let mut vec_clone = vec.clone(); 988 | vec_clone.insert(index, tokenizer::ArrayTypesEnum::Float(*stuff_to_push)); 989 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 990 | }, 991 | _ => return Err("EXECUTION ERROR: YOU HAVE TO INSERT A STRING, INTEGER OR FLOAT INTO AN ARRAY!".to_string()) 992 | } 993 | } 994 | else if v == &"REMOVE".to_string() { 995 | let stuff = match &token_collection[2].1 { 996 | tokenizer::ValueEnum::String(stuff) => stuff, 997 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 998 | }; 999 | 1000 | let value = self.global_variables.get(stuff).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", stuff))?; 1001 | 1002 | let vec = match &value { 1003 | tokenizer::ValueEnum::Array(vec) => vec, 1004 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1005 | }; 1006 | 1007 | let index = { 1008 | match &token_collection[4].1 { 1009 | tokenizer::ValueEnum::Integer(index_where_to_remove) => *index_where_to_remove as usize, 1010 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1011 | } 1012 | }; 1013 | 1014 | if index >= vec.len() { 1015 | return Err("EXECUTION ERROR: INDEX IS OUT OF BOUNDS!".to_string()); 1016 | } 1017 | 1018 | let mut vec_clone = vec.clone(); 1019 | vec_clone.remove(index); 1020 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 1021 | } 1022 | else if v == &"SET".to_string() { 1023 | let stuff = match &token_collection[1].1 { 1024 | tokenizer::ValueEnum::String(stuff) => stuff, 1025 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1026 | }; 1027 | 1028 | let value = self.global_variables.get(stuff).ok_or(format!("EXECUTION ERROR: THERE IS NO VARIABLE CALLED {}!", stuff))?; 1029 | 1030 | let vec = match &value { 1031 | tokenizer::ValueEnum::Array(vec) => vec, 1032 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1033 | }; 1034 | 1035 | let index = { 1036 | match &token_collection[3].1 { 1037 | tokenizer::ValueEnum::Integer(index_where_to_remove) => *index_where_to_remove as usize, 1038 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1039 | } 1040 | }; 1041 | 1042 | if index >= vec.len() { 1043 | return Err("EXECUTION ERROR: INDEX IS OUT OF BOUNDS!".to_string()); 1044 | } 1045 | 1046 | match &token_collection[5].1 { 1047 | tokenizer::ValueEnum::String(end_value) => { 1048 | let mut vec_clone = vec.clone(); 1049 | vec_clone[index] = ArrayTypesEnum::String(end_value.to_string()); 1050 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 1051 | }, 1052 | tokenizer::ValueEnum::Integer(end_value) => { 1053 | let mut vec_clone = vec.clone(); 1054 | vec_clone[index] = ArrayTypesEnum::Integer(*end_value); 1055 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 1056 | }, 1057 | tokenizer::ValueEnum::Float(end_value) => { 1058 | let mut vec_clone = vec.clone(); 1059 | vec_clone[index] = ArrayTypesEnum::Float(*end_value); 1060 | *self.global_variables.get_mut(stuff).unwrap() = tokenizer::ValueEnum::Array(vec_clone); 1061 | } 1062 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1063 | } 1064 | } 1065 | 1066 | else if v == &"HELP_FOR".to_string() { 1067 | let keyword = match &token_collection[1].1 { 1068 | tokenizer::ValueEnum::String(keyword) => keyword, 1069 | _ => unreachable!("SOME THIS SHOULDN'T BE PRINTED!") 1070 | }; 1071 | 1072 | let order_collection= predefined_name_order.get(&keyword.as_str()).ok_or(format!("EXECUTION ERROR: CAN'T PRINT HELP FOR {} BECAUSE IT'S NOT A PREDEFINED NAME WHICH IS AT THE BEGINNING OF A LINE!", keyword))?; 1073 | 1074 | match order_collection { 1075 | OrderEnum::SingleOption(v) => { 1076 | println!("-SINGLE OPTION-"); 1077 | print!("1: '"); 1078 | for e_nr in 0..v.len() { 1079 | if e_nr == v.len() - 1 { 1080 | println!("{}'", v[e_nr]); 1081 | } else { 1082 | print!("{}, ", v[e_nr]); 1083 | } 1084 | } 1085 | }, 1086 | OrderEnum::MultipleOptions(v) => { 1087 | println!("-MULTIPLE OPTIONS-"); 1088 | for p_nr in 0..v.len() { 1089 | print!("{}: '", p_nr+1); 1090 | for e_nr in 0..v[p_nr].len() { 1091 | if e_nr == v[p_nr].len() - 1 { 1092 | println!("{}'", v[p_nr][e_nr]); 1093 | } else { 1094 | print!("{}, ", v[p_nr][e_nr]); 1095 | } 1096 | } 1097 | } 1098 | } 1099 | } 1100 | } 1101 | }, 1102 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 1103 | } 1104 | 1105 | // debugging purpose 1106 | println!("self.global_variables: {:?}", self.global_variables); 1107 | 1108 | Ok(()) 1109 | } 1110 | } 1111 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of an interpreter for a programming language called minosrus_lang 3 | * Copyright (C) 2020-2021 Yves Vollmeier 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | 21 | mod execution; 22 | mod verine_expression; 23 | mod tokenizer; 24 | 25 | use std::io::{self, Write}; 26 | use std::collections::HashMap; 27 | use std::env; 28 | use std::path::Path; 29 | use std::fs::File; 30 | use std::io::{BufRead, BufReader}; 31 | 32 | // here are all variables for exec saved 33 | pub struct ExecData { 34 | // here are all the global variables stored; not changed after one loop iteration 35 | pub global_variables: HashMap, 36 | 37 | // here are all verines stored 38 | pub verines: HashMap, 39 | 40 | // save state of indentation 41 | pub indentation: String, 42 | 43 | // here is all block code saved (except for function code) 44 | pub block_code: Vec>, 45 | 46 | // here are all functions saved 47 | pub functions: HashMap>>, 48 | 49 | // keep track of the current block code type (normal or functions), the name of the function 50 | pub current_block_type: (String, String), 51 | 52 | } 53 | 54 | impl ExecData { 55 | pub fn new() -> Self { 56 | Self { 57 | global_variables: HashMap::new(), 58 | verines: HashMap::new(), 59 | indentation: "".to_string(), 60 | block_code: Vec::new(), 61 | functions: HashMap::new(), 62 | current_block_type: ("".to_string(), "".to_string()), 63 | } 64 | } 65 | } 66 | 67 | fn print_interpreter_error(error_message: &str) { 68 | println!("{}", error_message); 69 | println!("HELP: ARGUMENTS"); 70 | println!(" --execute | WILL EXECUTE A MORL FILE"); 71 | println!(" --repl | WILL START A REPL"); 72 | } 73 | 74 | fn file_execution(args_2: String) { 75 | if Path::new(&args_2).exists() { 76 | let file = File::open(&args_2).unwrap(); 77 | let reader = BufReader::new(file); 78 | 79 | let mut exec_data_variable = ExecData::new(); 80 | 81 | let mut token_collection_of_all_lines: Vec> = Vec::new(); 82 | let mut collection_of_all_lines: Vec = Vec::new(); 83 | 84 | let mut tokenizing_error_count = 0; 85 | 86 | // tokenizing - syntax errors - compilation errors - all lines are checked 87 | for (line_nr, line) in reader.lines().enumerate() { 88 | let line = line.unwrap(); 89 | 90 | let mut print_err = | error_message | { 91 | println!("- ERROR OCCURED ON LINE NR. {}: '{}'", line_nr + 1, line); 92 | println!(" -> {}", error_message); 93 | tokenizing_error_count += 1; 94 | }; 95 | 96 | if !(line.trim().to_string().is_empty()) { 97 | for character in line.chars() { 98 | if character.is_lowercase() { 99 | print_err("INPUT ERROR: INPUT INCLUDES LOWERCASE CHARACTER!"); 100 | println!("INTERPRETER STOPPPED!"); 101 | return; 102 | } 103 | } 104 | 105 | let token_collection_of_current_line = tokenizer::make_tokens(line.clone(), &mut exec_data_variable); 106 | 107 | // check for syntax errors 108 | if let Some((_, value)) = token_collection_of_current_line.iter().find(|(key, _)| key == &"ERROR_MESSAGE") { 109 | match value { 110 | tokenizer::ValueEnum::String(v) => { 111 | print_err(format!("SYNTAX ERROR: {}", v).as_str()); 112 | }, 113 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 114 | } 115 | } 116 | 117 | // save stuff if there were no errors 118 | token_collection_of_all_lines.push(token_collection_of_current_line); 119 | collection_of_all_lines.push(line); 120 | } 121 | } 122 | 123 | if tokenizing_error_count != 0 { 124 | if tokenizing_error_count == 1 { 125 | println!("\nABORTING DUE THE PREVIOUS {} ERROR!", tokenizing_error_count); 126 | } else { 127 | println!("\nABORTING DUE THE PREVIOUS {} ERRORS!", tokenizing_error_count); 128 | } 129 | println!("INTERPRETER STOPPED!"); 130 | return; 131 | } 132 | 133 | // executing - execution errors - runtime errors - execution stops when an error occurs 134 | for (tokenized_line_nr, tokenized_line) in token_collection_of_all_lines.iter().enumerate() { 135 | let print_err = | error_message | { 136 | println!("- ERROR OCCURED ON LINE NR. {}: '{}'", tokenized_line_nr + 1, collection_of_all_lines[tokenized_line_nr]); 137 | println!(" -> {}", error_message); 138 | println!("INTERPRETER STOPPED DUE PREVIOUS RUNTIME ERROR!"); 139 | }; 140 | 141 | // check if indentation is right 142 | let mut indentation_count = 0; 143 | for character in collection_of_all_lines[tokenized_line_nr].chars() { 144 | if character == ' ' { 145 | indentation_count += 1; 146 | } 147 | else if character == '\t' { 148 | indentation_count += 4; 149 | } else { 150 | break; 151 | } 152 | } 153 | if exec_data_variable.indentation.len() != indentation_count { 154 | print_err("INDENTATION ERROR!"); 155 | return; 156 | } 157 | 158 | let return_of_execution = exec_data_variable.exec(tokenized_line.to_vec(), false); 159 | match return_of_execution { 160 | Ok(_) => (), 161 | Err(e) => { 162 | print_err(&e); 163 | break; 164 | } 165 | } 166 | } 167 | 168 | // check if block code is closed at the end of the file 169 | if exec_data_variable.indentation != "".to_string() { 170 | let len_of_file = collection_of_all_lines.len(); 171 | println!("- ERROR OCCURED ON LINE NR. {}: '{}'", len_of_file, collection_of_all_lines[len_of_file - 1]); 172 | println!(" -> EXECUTION ERROR: BLOCK CODE ISN'T CLOSED!"); 173 | println!("INTERPRETER STOPPED DUE PREVIOUS RUNTIME ERROR!"); 174 | } 175 | } else { 176 | print_interpreter_error("INTERPRETER ERROR: THIRD COMMAND LINE ARGUMENT IS NOT A VALID PATH!"); 177 | } 178 | } 179 | 180 | fn repl() { 181 | // some starting text 182 | println!("EVERYTHING HAS TO BE UPPERCASE!"); 183 | 184 | // make ExecData instance 185 | let mut exec_data_variable = ExecData::new(); 186 | 187 | // one loop iteration = one prompt (" >") 188 | loop { 189 | let mut valid_input = true; 190 | let mut input = String::new(); 191 | 192 | print!("{}> ", exec_data_variable.indentation); 193 | let _ = io::stdout().flush(); // needed because of the print! macro 194 | 195 | match io::stdin().read_line(&mut input) { // reading input 196 | Ok(_) => { 197 | if !(input.trim().to_string().is_empty()) { 198 | for character in input.chars() { 199 | if character.is_lowercase() { 200 | println!("SYNTAX ERROR: INPUT INCLUDES LOWERCASE CHARACTER!"); 201 | valid_input = false; 202 | break; 203 | } 204 | } 205 | if valid_input { 206 | let tokens = tokenizer::make_tokens(input, &mut exec_data_variable); 207 | let return_of_execution = exec_data_variable.exec(tokens, false); 208 | match return_of_execution { 209 | Ok(_) => (), 210 | Err(e) => println!("{}", &e) 211 | } 212 | } 213 | } 214 | }, 215 | 216 | Err(_) => println!("INPUT ERROR: SOMETHING WITH YOUR INPUT IS WRONG!") 217 | } 218 | } 219 | } 220 | 221 | fn main() { 222 | // get command line arguments 223 | let args: Vec = env::args().collect(); 224 | 225 | // check if valid 226 | if args.len() == 3 { 227 | if args[1] == "--execute".to_string() { 228 | file_execution(args[2].to_string()); 229 | } else { 230 | print_interpreter_error("INTERPRETER ERROR: COMMAND LINE ARGUMENTS AREN'T RIGHT!"); 231 | } 232 | } 233 | else if args.len() == 2 { 234 | if args[1] == "--repl".to_string() { 235 | repl(); 236 | } else { 237 | print_interpreter_error("INTERPRETER ERROR: COMMAND LINE ARGUMENTS AREN'T RIGHT!"); 238 | } 239 | } else { 240 | print_interpreter_error("INTERPRETER ERROR: COMMAND LINE ARGUMENTS AREN'T RIGHT!"); 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /src/predefined_name_order.in: -------------------------------------------------------------------------------- 1 | // order of predefined names for checking and if the value is set the value 2 | { 3 | let mut hashmap = HashMap::new(); 4 | 5 | hashmap.insert("LET", OrderEnum::MultipleOptions( 6 | vec![ 7 | vec![ 8 | "PREDEFINED_NAME:LET", 9 | "VARIABLE/FUNCTION_NAME:?", 10 | "EQUAL_SIGN:=", 11 | "STRING:?" 12 | ], 13 | vec![ 14 | "PREDEFINED_NAME:LET", 15 | "VARIABLE/FUNCTION_NAME:?", 16 | "EQUAL_SIGN:=", 17 | "INTEGER:?" 18 | ], 19 | vec![ 20 | "PREDEFINED_NAME:LET", 21 | "VARIABLE/FUNCTION_NAME:?", 22 | "EQUAL_SIGN:=", 23 | "FLOAT:?" 24 | ], 25 | vec![ 26 | "PREDEFINED_NAME:LET", 27 | "VARIABLE/FUNCTION_NAME:?", 28 | "EQUAL_SIGN:=", 29 | "ARRAY:?" 30 | ], 31 | vec![ 32 | "PREDEFINED_NAME:LET", 33 | "VARIABLE/FUNCTION_NAME:?", 34 | "EQUAL_SIGN:=", 35 | "VARIABLE/FUNCTION_NAME:?" 36 | ] 37 | ])); 38 | 39 | hashmap.insert("PRINT", OrderEnum::MultipleOptions( 40 | vec![ 41 | vec![ 42 | "PREDEFINED_NAME:PRINT", 43 | "STRING:?" 44 | ], 45 | vec![ 46 | "PREDEFINED_NAME:PRINT", 47 | "INTEGER:?" 48 | ], 49 | vec![ 50 | "PREDEFINED_NAME:PRINT", 51 | "FLOAT:?" 52 | ], 53 | vec![ 54 | "PREDEFINED_NAME:PRINT", 55 | "VARIABLE/FUNCTION_NAME:?" 56 | ], 57 | ])); 58 | 59 | hashmap.insert("FN", OrderEnum::SingleOption( 60 | vec![ 61 | "PREDEFINED_NAME:FN", 62 | "VARIABLE/FUNCTION_NAME:?", 63 | "PREDEFINED_NAME:START" 64 | ])); 65 | 66 | hashmap.insert("DO", OrderEnum::SingleOption( 67 | vec![ 68 | "PREDEFINED_NAME:DO", 69 | "VARIABLE/FUNCTION_NAME:?", 70 | ])); 71 | 72 | hashmap.insert("IF", OrderEnum::MultipleOptions( 73 | vec![ 74 | vec![ 75 | "PREDEFINED_NAME:IF", 76 | "STRING:?", 77 | "COMPARING_OPERATOR:?", 78 | "STRING:?", 79 | "PREDEFINED_NAME:START" 80 | ], 81 | vec![ 82 | "PREDEFINED_NAME:IF", 83 | "INTEGER:?", 84 | "COMPARING_OPERATOR:?", 85 | "INTEGER:?", 86 | "PREDEFINED_NAME:START" 87 | ], 88 | vec![ 89 | "PREDEFINED_NAME:IF", 90 | "FLOAT:?", 91 | "COMPARING_OPERATOR:?", 92 | "FLOAT:?", 93 | "PREDEFINED_NAME:START" 94 | ], 95 | vec![ 96 | "PREDEFINED_NAME:IF", 97 | "STRING:?", 98 | "COMPARING_OPERATOR:?", 99 | "VARIABLE/FUNCTION_NAME:?", 100 | "PREDEFINED_NAME:START" 101 | ], 102 | vec![ 103 | "PREDEFINED_NAME:IF", 104 | "INTEGER:?", 105 | "COMPARING_OPERATOR:?", 106 | "VARIABLE/FUNCTION_NAME:?", 107 | "PREDEFINED_NAME:START" 108 | ], 109 | vec![ 110 | "PREDEFINED_NAME:IF", 111 | "FLOAT:?", 112 | "COMPARING_OPERATOR:?", 113 | "VARIABLE/FUNCTION_NAME:?", 114 | "PREDEFINED_NAME:START" 115 | ], 116 | vec![ 117 | "PREDEFINED_NAME:IF", 118 | "VARIABLE/FUNCTION_NAME:?", 119 | "COMPARING_OPERATOR:?", 120 | "STRING:?", 121 | "PREDEFINED_NAME:START" 122 | ], 123 | vec![ 124 | "PREDEFINED_NAME:IF", 125 | "VARIABLE/FUNCTION_NAME:?", 126 | "COMPARING_OPERATOR:?", 127 | "INTEGER:?", 128 | "PREDEFINED_NAME:START" 129 | ], 130 | vec![ 131 | "PREDEFINED_NAME:IF", 132 | "VARIABLE/FUNCTION_NAME:?", 133 | "COMPARING_OPERATOR:?", 134 | "FLOAT:?", 135 | "PREDEFINED_NAME:START" 136 | ], 137 | vec![ 138 | "PREDEFINED_NAME:IF", 139 | "VARIABLE/FUNCTION_NAME:?", 140 | "COMPARING_OPERATOR:?", 141 | "VARIABLE/FUNCTION_NAME:?", 142 | "PREDEFINED_NAME:START" 143 | ] 144 | ])); 145 | 146 | hashmap.insert("ELIF", OrderEnum::MultipleOptions( 147 | vec![ 148 | vec![ 149 | "PREDEFINED_NAME:ELIF", 150 | "STRING:?", 151 | "COMPARING_OPERATOR:?", 152 | "STRING:?", 153 | "PREDEFINED_NAME:START" 154 | ], 155 | vec![ 156 | "PREDEFINED_NAME:ELIF", 157 | "INTEGER:?", 158 | "COMPARING_OPERATOR:?", 159 | "INTEGER:?", 160 | "PREDEFINED_NAME:START" 161 | ], 162 | vec![ 163 | "PREDEFINED_NAME:ELIF", 164 | "FLOAT:?", 165 | "COMPARING_OPERATOR:?", 166 | "FLOAT:?", 167 | "PREDEFINED_NAME:START" 168 | ], 169 | vec![ 170 | "PREDEFINED_NAME:ELIF", 171 | "STRING:?", 172 | "COMPARING_OPERATOR:?", 173 | "VARIABLE/FUNCTION_NAME:?", 174 | "PREDEFINED_NAME:START" 175 | ], 176 | vec![ 177 | "PREDEFINED_NAME:ELIF", 178 | "INTEGER:?", 179 | "COMPARING_OPERATOR:?", 180 | "VARIABLE/FUNCTION_NAME:?", 181 | "PREDEFINED_NAME:START" 182 | ], 183 | vec![ 184 | "PREDEFINED_NAME:ELIF", 185 | "FLOAT:?", 186 | "COMPARING_OPERATOR:?", 187 | "VARIABLE/FUNCTION_NAME:?", 188 | "PREDEFINED_NAME:START" 189 | ], 190 | vec![ 191 | "PREDEFINED_NAME:ELIF", 192 | "VARIABLE/FUNCTION_NAME:?", 193 | "COMPARING_OPERATOR:?", 194 | "STRING:?", 195 | "PREDEFINED_NAME:START" 196 | ], 197 | vec![ 198 | "PREDEFINED_NAME:ELIF", 199 | "VARIABLE/FUNCTION_NAME:?", 200 | "COMPARING_OPERATOR:?", 201 | "INTEGER:?", 202 | "PREDEFINED_NAME:START" 203 | ], 204 | vec![ 205 | "PREDEFINED_NAME:ELIF", 206 | "VARIABLE/FUNCTION_NAME:?", 207 | "COMPARING_OPERATOR:?", 208 | "FLOAT:?", 209 | "PREDEFINED_NAME:START" 210 | ], 211 | vec![ 212 | "PREDEFINED_NAME:ELIF", 213 | "VARIABLE/FUNCTION_NAME:?", 214 | "COMPARING_OPERATOR:?", 215 | "VARIABLE/FUNCTION_NAME:?", 216 | "PREDEFINED_NAME:START" 217 | ] 218 | ])); 219 | 220 | 221 | hashmap.insert("WHILE", OrderEnum::MultipleOptions( 222 | vec![ 223 | vec![ 224 | "PREDEFINED_NAME:WHILE", 225 | "STRING:?", 226 | "COMPARING_OPERATOR:?", 227 | "STRING:?", 228 | "PREDEFINED_NAME:START" 229 | ], 230 | vec![ 231 | "PREDEFINED_NAME:WHILE", 232 | "INTEGER:?", 233 | "COMPARING_OPERATOR:?", 234 | "INTEGER:?", 235 | "PREDEFINED_NAME:START" 236 | ], 237 | vec![ 238 | "PREDEFINED_NAME:WHILE", 239 | "FLOAT:?", 240 | "COMPARING_OPERATOR:?", 241 | "FLOAT:?", 242 | "PREDEFINED_NAME:START" 243 | ], 244 | vec![ 245 | "PREDEFINED_NAME:WHILE", 246 | "STRING:?", 247 | "COMPARING_OPERATOR:?", 248 | "VARIABLE/FUNCTION_NAME:?", 249 | "PREDEFINED_NAME:START" 250 | ], 251 | vec![ 252 | "PREDEFINED_NAME:WHILE", 253 | "INTEGER:?", 254 | "COMPARING_OPERATOR:?", 255 | "VARIABLE/FUNCTION_NAME:?", 256 | "PREDEFINED_NAME:START" 257 | ], 258 | vec![ 259 | "PREDEFINED_NAME:WHILE", 260 | "FLOAT:?", 261 | "COMPARING_OPERATOR:?", 262 | "VARIABLE/FUNCTION_NAME:?", 263 | "PREDEFINED_NAME:START" 264 | ], 265 | vec![ 266 | "PREDEFINED_NAME:WHILE", 267 | "VARIABLE/FUNCTION_NAME:?", 268 | "COMPARING_OPERATOR:?", 269 | "STRING:?", 270 | "PREDEFINED_NAME:START" 271 | ], 272 | vec![ 273 | "PREDEFINED_NAME:WHILE", 274 | "VARIABLE/FUNCTION_NAME:?", 275 | "COMPARING_OPERATOR:?", 276 | "INTEGER:?", 277 | "PREDEFINED_NAME:START" 278 | ], 279 | vec![ 280 | "PREDEFINED_NAME:WHILE", 281 | "VARIABLE/FUNCTION_NAME:?", 282 | "COMPARING_OPERATOR:?", 283 | "FLOAT:?", 284 | "PREDEFINED_NAME:START" 285 | ], 286 | vec![ 287 | "PREDEFINED_NAME:WHILE", 288 | "VARIABLE/FUNCTION_NAME:?", 289 | "COMPARING_OPERATOR:?", 290 | "VARIABLE/FUNCTION_NAME:?", 291 | "PREDEFINED_NAME:START" 292 | ] 293 | ])); 294 | 295 | hashmap.insert("PUSH", OrderEnum::MultipleOptions( 296 | vec![ 297 | vec![ 298 | "PREDEFINED_NAME:PUSH", 299 | "STRING:?", 300 | "PREDEFINED_NAME:ONTO", 301 | "VARIABLE/FUNCTION_NAME:?" 302 | ], 303 | vec![ 304 | "PREDEFINED_NAME:PUSH", 305 | "INTEGER:?", 306 | "PREDEFINED_NAME:ONTO", 307 | "VARIABLE/FUNCTION_NAME:?" 308 | ], 309 | vec![ 310 | "PREDEFINED_NAME:PUSH", 311 | "FLOAT:?", 312 | "PREDEFINED_NAME:ONTO", 313 | "VARIABLE/FUNCTION_NAME:?" 314 | ], 315 | vec![ 316 | "PREDEFINED_NAME:PUSH", 317 | "VARIABLE/FUNCTION_NAME:?", 318 | "PREDEFINED_NAME:ONTO", 319 | "VARIABLE/FUNCTION_NAME:?" 320 | ] 321 | ])); 322 | 323 | hashmap.insert("POP", OrderEnum::SingleOption( 324 | vec![ 325 | "PREDEFINED_NAME:POP", 326 | "PREDEFINED_NAME:FROM", 327 | "VARIABLE/FUNCTION_NAME:?" 328 | ])); 329 | 330 | hashmap.insert("INSERT", OrderEnum::MultipleOptions( 331 | vec![ 332 | vec![ 333 | "PREDEFINED_NAME:INSERT", 334 | "STRING:?", 335 | "PREDEFINED_NAME:INTO", 336 | "VARIABLE/FUNCTION_NAME:?", 337 | "PREDEFINED_NAME:AT", 338 | "INTEGER:?" 339 | ], 340 | vec![ 341 | "PREDEFINED_NAME:INSERT", 342 | "STRING:?", 343 | "PREDEFINED_NAME:INTO", 344 | "VARIABLE/FUNCTION_NAME:?", 345 | "PREDEFINED_NAME:AT", 346 | "VARIABLE/FUNCTION_NAME:?" 347 | ], 348 | vec![ 349 | "PREDEFINED_NAME:INSERT", 350 | "INTEGER:?", 351 | "PREDEFINED_NAME:INTO", 352 | "VARIABLE/FUNCTION_NAME:?", 353 | "PREDEFINED_NAME:AT", 354 | "INTEGER:?" 355 | ], 356 | vec![ 357 | "PREDEFINED_NAME:INSERT", 358 | "INTEGER:?", 359 | "PREDEFINED_NAME:INTO", 360 | "VARIABLE/FUNCTION_NAME:?", 361 | "PREDEFINED_NAME:AT", 362 | "VARIABLE/FUNCTION_NAME:?" 363 | ], 364 | vec![ 365 | "PREDEFINED_NAME:INSERT", 366 | "FLOAT:?", 367 | "PREDEFINED_NAME:INTO", 368 | "VARIABLE/FUNCTION_NAME:?", 369 | "PREDEFINED_NAME:AT", 370 | "INTEGER:?" 371 | ], 372 | vec![ 373 | "PREDEFINED_NAME:INSERT", 374 | "FLOAT:?", 375 | "PREDEFINED_NAME:INTO", 376 | "VARIABLE/FUNCTION_NAME:?", 377 | "PREDEFINED_NAME:AT", 378 | "VARIABLE/FUNCTION_NAME:?" 379 | ], 380 | vec![ 381 | "PREDEFINED_NAME:INSERT", 382 | "VARIABLE/FUNCTION_NAME:?", 383 | "PREDEFINED_NAME:INTO", 384 | "VARIABLE/FUNCTION_NAME:?", 385 | "PREDEFINED_NAME:AT", 386 | "INTEGER:?" 387 | ], 388 | vec![ 389 | "PREDEFINED_NAME:INSERT", 390 | "VARIABLE/FUNCTION_NAME:?", 391 | "PREDEFINED_NAME:INTO", 392 | "VARIABLE/FUNCTION_NAME:?", 393 | "PREDEFINED_NAME:AT", 394 | "VARIABLE/FUNCTION_NAME:?" 395 | ] 396 | ])); 397 | 398 | hashmap.insert("REMOVE", OrderEnum::SingleOption( 399 | vec![ 400 | "PREDEFINED_NAME:REMOVE", 401 | "PREDEFINED_NAME:FROM", 402 | "VARIABLE/FUNCTION_NAME:?", 403 | "PREDEFINED_NAME:AT", 404 | "INTEGER:?" 405 | ])); 406 | hashmap.insert("SET", OrderEnum::MultipleOptions( 407 | vec![ 408 | vec![ 409 | "PREDEFINED_NAME:SET", 410 | "VARIABLE/FUNCTION_NAME:?", 411 | "PREDEFINED_NAME:AT", 412 | "INTEGER:?", 413 | "PREDEFINED_NAME:TO", 414 | "STRING:?" 415 | ], 416 | vec![ 417 | "PREDEFINED_NAME:SET", 418 | "VARIABLE/FUNCTION_NAME:?", 419 | "PREDEFINED_NAME:AT", 420 | "VARIABLE/FUNCTION_NAME:?", 421 | "PREDEFINED_NAME:TO", 422 | "STRING:?" 423 | ], 424 | vec![ 425 | "PREDEFINED_NAME:SET", 426 | "VARIABLE/FUNCTION_NAME:?", 427 | "PREDEFINED_NAME:AT", 428 | "INTEGER:?", 429 | "PREDEFINED_NAME:TO", 430 | "INTEGER:?" 431 | ], 432 | vec![ 433 | "PREDEFINED_NAME:SET", 434 | "VARIABLE/FUNCTION_NAME:?", 435 | "PREDEFINED_NAME:AT", 436 | "VARIABLE/FUNCTION_NAME:?", 437 | "PREDEFINED_NAME:TO", 438 | "INTEGER:?" 439 | ], 440 | vec![ 441 | "PREDEFINED_NAME:SET", 442 | "VARIABLE/FUNCTION_NAME:?", 443 | "PREDEFINED_NAME:AT", 444 | "INTEGER:?", 445 | "PREDEFINED_NAME:TO", 446 | "FLOAT:?" 447 | ], 448 | vec![ 449 | "PREDEFINED_NAME:SET", 450 | "VARIABLE/FUNCTION_NAME:?", 451 | "PREDEFINED_NAME:AT", 452 | "VARIABLE/FUNCTION_NAME:?", 453 | "PREDEFINED_NAME:TO", 454 | "FLOAT:?" 455 | ], 456 | vec![ 457 | "PREDEFINED_NAME:SET", 458 | "VARIABLE/FUNCTION_NAME:?", 459 | "PREDEFINED_NAME:AT", 460 | "INTEGER:?", 461 | "PREDEFINED_NAME:TO", 462 | "VARIABLE_FUNCTION_NAME:?" 463 | ], 464 | vec![ 465 | "PREDEFINED_NAME:SET", 466 | "VARIABLE/FUNCTION_NAME:?", 467 | "PREDEFINED_NAME:AT", 468 | "VARIABLE/FUNCTION_NAME:?", 469 | "PREDEFINED_NAME:TO", 470 | "VARIABLE/FUNCTION_NAME:?" 471 | ] 472 | ])); 473 | 474 | hashmap.insert("HELP_FOR", OrderEnum::SingleOption( 475 | vec![ 476 | "PREDEFINED_NAME:HELP_FOR", 477 | "PREDEFINED_NAME:?", 478 | ])); 479 | 480 | 481 | hashmap 482 | } 483 | -------------------------------------------------------------------------------- /src/tokenizer.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of an interpreter for a programming language called minosrus_lang 3 | * Copyright (C) 2020-2021 Yves Vollmeier 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | 21 | use crate::verine_expression::{VerineTokenizer, VerineTokenizerError, Token}; 22 | use crate::ExecData; 23 | 24 | #[derive(Debug, Clone)] 25 | pub enum ArrayTypesEnum { 26 | String(String), 27 | Integer(i32), 28 | Float(f32) 29 | } 30 | 31 | #[derive(Debug, Clone)] 32 | pub enum ValueEnum { 33 | String(String), 34 | Integer(i32), 35 | Float(f32), 36 | Array(Vec), 37 | Verine(Vec), 38 | } 39 | 40 | pub fn make_tokens(input: String, exec_data_variable: &mut ExecData) -> Vec<(String, ValueEnum)> { 41 | // final tokens that are returned stored here 42 | let mut final_tokens: Vec<(String, ValueEnum)> = Vec::new(); 43 | 44 | // used for the hashmap final_tokens -> classification of token 45 | let token_classification = vec![ 46 | "COMMENT".to_string(), 47 | "PREDEFINED_NAME".to_string(), 48 | "ARITHMETIC_OPERATOR".to_string(), 49 | "COMPARING_OPERATOR".to_string(), 50 | "EQUAL_SIGN".to_string(), 51 | "STRING".to_string(), 52 | "INTEGER".to_string(), 53 | "FLOAT".to_string(), 54 | "VERINE".to_string(), 55 | "ARRAY".to_string(), 56 | "VARIABLE/FUNCTION_NAME".to_string(), 57 | ]; 58 | 59 | // used to check whether a token is a ... or not 60 | let predefined_names = vec![ 61 | "LET".to_string(), 62 | "PRINT".to_string(), 63 | "FN".to_string(), 64 | "START".to_string(), 65 | "END".to_string(), 66 | "DO".to_string(), 67 | "IF".to_string(), 68 | "ELSE".to_string(), 69 | "ELIF".to_string(), 70 | "WHILE".to_string(), 71 | "PUSH".to_string(), 72 | "POP".to_string(), 73 | "INSERT".to_string(), 74 | "REMOVE".to_string(), 75 | "SET".to_string(), 76 | "GET".to_string(), 77 | "ONTO".to_string(), 78 | "FROM".to_string(), 79 | "INTO".to_string(), 80 | "AT".to_string(), 81 | "TO".to_string(), 82 | "LEN".to_string(), 83 | "READLN".to_string(), 84 | "STRING_FROM".to_string(), 85 | "INTEGER_FROM".to_string(), 86 | "HELP_FOR".to_string(), 87 | "RESET".to_string(), 88 | "STOP".to_string() 89 | ]; 90 | let arithmetic_operators = vec![ 91 | "+".to_string(), 92 | "-".to_string(), 93 | "*".to_string(), 94 | "/".to_string(), 95 | "**".to_string(), 96 | ]; 97 | let comparing_operators = vec![ 98 | "==".to_string(), 99 | "!=".to_string(), 100 | "<".to_string(), 101 | ">".to_string(), 102 | "<=".to_string(), 103 | ">=".to_string() 104 | ]; 105 | let equal_sign = "=".to_string(); 106 | 107 | // used for checking whether a name is valid or not 108 | let allowed_variable_function_characters = { 109 | let mut vec = vec![]; 110 | vec.extend('A'..='Z'); 111 | vec.push('_'); 112 | vec 113 | }; 114 | 115 | // used for checking whether the inner part of a string is valid or not 116 | let allowed_string_inner_part_characters = { 117 | let mut vec = vec![]; 118 | vec.extend('A'..='Z'); 119 | vec.extend('0'..='9'); 120 | vec.extend_from_slice(&[',', '.', ':', '!', '?', ' ']); 121 | vec 122 | }; 123 | 124 | // comment check 125 | if input.trim().chars().nth(0).unwrap() == '#' { 126 | final_tokens.push((token_classification[0].to_string(), ValueEnum::String(input.trim()[1..].to_string()))); 127 | return final_tokens; 128 | } 129 | 130 | let mut verines_tokens = vec![]; 131 | 132 | let mut verine_openings = 0; 133 | let mut from = 0; 134 | for (index, character) in input.chars().enumerate() { 135 | match character { 136 | '(' => { 137 | if verine_openings == 0 { 138 | from = index; 139 | } 140 | verine_openings += 1; 141 | } 142 | ')' => { 143 | verine_openings -= 1; 144 | if verine_openings == 0 { 145 | let to = index; 146 | 147 | let mut push_error = |message: &str| { 148 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String(message.to_string()))); 149 | }; 150 | 151 | let chars = input[from + 1..to].chars().collect::>(); 152 | let mut tokenizer = VerineTokenizer::new(chars.as_slice()); 153 | 154 | let tokens_result = tokenizer.tokenize(); 155 | match tokens_result { 156 | Ok(tokens) => verines_tokens.push(Some(tokens)), 157 | Err(err) => { 158 | use VerineTokenizerError::*; 159 | match err { 160 | UnexpectedCharacter(_char) => push_error("INVALID CHARACTER IN VERINE!"), 161 | StringLiteralNotClosed => push_error("STRING ISN'T CLOSED!"), 162 | _ => unreachable!("SOMEHOW THIS SHOULDN'T BE PRINTED!") 163 | } 164 | verines_tokens.push(None); 165 | } 166 | } 167 | } 168 | } 169 | _ => {} 170 | } 171 | } 172 | 173 | // split input into parts; strings don't get split; arrays don't get split 174 | let input = input.trim().to_string() + " "; 175 | let mut current_token = String::new(); 176 | let mut array_token = String::new(); 177 | let mut verine_token = String::new(); 178 | let mut last_character = 'a'; // just something that isn't a space 179 | let mut is_there_a_string = false; 180 | let mut array_started = false; 181 | let mut string_started = false; 182 | let mut verine_started = false; 183 | let mut split_of_input: Vec = vec![]; 184 | let mut open_verines = 0; 185 | 186 | if input.contains(&"[".to_string()) && !(input.contains(&"]".to_string())) { 187 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("A [ ALWAYS NEEDS A ] ! ORDER: [ AND THEN ] !".to_string()))); 188 | return final_tokens; 189 | } 190 | 191 | for character in input.chars() { 192 | if character == '[' { 193 | array_token.push(character); 194 | array_started = true; 195 | } 196 | else if character == ']' { 197 | if array_started { 198 | array_token.push(character); 199 | split_of_input.push(array_token); 200 | array_token = String::new(); 201 | array_started = false; 202 | } else { 203 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("A ] ALWAYS NEEDS A [ ! ORDER: [ AND THEN ] !".to_string()))); 204 | return final_tokens; 205 | } 206 | } 207 | else if array_started { 208 | array_token.push(character); 209 | } 210 | else if character == '(' { 211 | if open_verines == 0 { 212 | verine_started = true; 213 | verine_token.clear(); 214 | } 215 | open_verines += 1; 216 | verine_token.push(character); 217 | } 218 | else if character == ')' { 219 | verine_token.push(character); 220 | open_verines -= 1; 221 | if open_verines == 0 { 222 | split_of_input.push(verine_token.clone()); 223 | verine_started = false; 224 | verine_token.clear(); 225 | } 226 | } 227 | else if verine_started { 228 | verine_token.push(character); 229 | } 230 | else if character == ' ' { 231 | if current_token.starts_with('"') && !current_token.ends_with('"') { 232 | // space belongs to the string 233 | current_token.push(character); 234 | } 235 | else { 236 | // end of token 237 | if last_character != ' ' && !current_token.is_empty() { 238 | split_of_input.push(current_token); 239 | current_token = String::new(); 240 | } 241 | } 242 | } else { 243 | // normal character and if used (later on) for string not closed error; not in array because array checking does that; and checking if character is valid 244 | if character == '"' { 245 | is_there_a_string = true; 246 | if current_token.starts_with('"') { 247 | string_started = false; 248 | } 249 | } 250 | 251 | if !(allowed_string_inner_part_characters.contains(&character)) && string_started { 252 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("INVALID CHARACTER INSIDE OF THE STRING!".to_string()))); 253 | return final_tokens; 254 | } 255 | 256 | if character == '"' { 257 | if !(current_token.starts_with('"')) { 258 | string_started = true; 259 | } 260 | } 261 | 262 | current_token.push(character); 263 | } 264 | 265 | last_character = character; 266 | } 267 | 268 | if is_there_a_string && current_token != "" { 269 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("STRING ISN'T CLOSED!".to_string()))); 270 | return final_tokens; 271 | } 272 | 273 | if split_of_input[split_of_input.len() - 1] == "" { 274 | split_of_input.remove(split_of_input.len() - 1); 275 | } 276 | // debugging purpose 277 | println!("split_of_input: {:?}", split_of_input); 278 | 279 | let mut i = 0; 280 | while i < split_of_input.len() { 281 | let part = &split_of_input[i]; 282 | // predefined name check 283 | if predefined_names.contains(part) { 284 | final_tokens.push((token_classification[1].to_string(), ValueEnum::String(part.to_string()))); 285 | } 286 | // arithmetic_operator check 287 | else if arithmetic_operators.contains(part) { 288 | final_tokens.push((token_classification[2].to_string(), ValueEnum::String(part.to_string()))); 289 | } 290 | // comparing_operator check 291 | else if comparing_operators.contains(part) { 292 | final_tokens.push((token_classification[3].to_string(), ValueEnum::String(part.to_string()))); 293 | } 294 | // equal_sign check 295 | else if part == &equal_sign { 296 | final_tokens.push((token_classification[4].to_string(), ValueEnum::String(part.to_string()))); 297 | } 298 | // string check 299 | else if part.chars().nth(0).unwrap() == '\"' && part.chars().rev().nth(0).unwrap() == '\"' { 300 | final_tokens.push((token_classification[5].to_string(), ValueEnum::String(part.as_str()[1..part.len()-1].to_string()))); 301 | } 302 | // integer check 303 | else if !(part.chars().any(|c| !(c.is_numeric() || c == '-' || c == '.'))) { 304 | let mut dot_count = 0; 305 | for (index, character) in part.chars().enumerate() { 306 | if index != 0 && character == '-' { 307 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("NEGATIVE SIGN HAS TO BE AT THE BEGINNING OF A NUMBER!".to_string()))); 308 | break; 309 | 310 | } 311 | else if character == '.' { 312 | if index == 0 { 313 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("DOT CAN'T BE AT THE BEGINNING OF THE NUMBER!".to_string()))); 314 | break; 315 | 316 | } 317 | else if index == part.len() - 1 { 318 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("DOT CAN'T BE AT THE END OF THE NUMBER!".to_string()))); 319 | break; 320 | } else { 321 | dot_count += 1; 322 | } 323 | } 324 | } 325 | if dot_count > 1 { 326 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THERE ARE TOO MANY DOTS IN THE NUMBER!".to_string()))); 327 | break; 328 | } 329 | 330 | if part.parse::().is_ok() { 331 | final_tokens.push((token_classification[6].to_string(), ValueEnum::Integer(part.parse::().unwrap()))); 332 | } 333 | else if part.parse::().is_ok() { 334 | final_tokens.push((token_classification[7].to_string(), ValueEnum::Float(part.parse::().unwrap()))); 335 | } else { 336 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THE NUMBER IS NOT I32 OR F32!".to_string()))); 337 | break; 338 | } 339 | } 340 | // verine check 341 | else if part.chars().nth(0).unwrap() == '(' && part.chars().rev().nth(0).unwrap() == ')' { 342 | if let Some(tokens) = verines_tokens.remove(0).clone() { 343 | final_tokens.push((token_classification[8].to_string(), ValueEnum::Verine(tokens))) 344 | } 345 | } 346 | // array check 347 | else if part.chars().nth(0).unwrap() == '[' && part.chars().rev().nth(0).unwrap() == ']' { 348 | let mut array = part.clone(); 349 | 350 | // remove [ and ] 351 | array.remove(0); 352 | array.remove(array.len() - 1); 353 | 354 | // check if array is empty 355 | if array.trim().is_empty() { 356 | final_tokens.push((token_classification[9].to_string(), ValueEnum::Array(vec![]))); 357 | break; 358 | } 359 | 360 | // check if there is a comma at the beginning or at the end - otherwise wrong error message would occur 361 | if array.trim().chars().nth(0).unwrap() == ',' { 362 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THERE IS A COMMA AT THE BEGINNING OF THE ARRAY; NOT ALLOWED!".to_string()))); 363 | break; 364 | } 365 | if array.trim().chars().rev().nth(0).unwrap() == ',' { 366 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THERE IS A COMMA AT THE END OF THE ARRAY; NOT ALLOWED!".to_string()))); 367 | break; 368 | } else { 369 | array.push(','); 370 | } 371 | 372 | // acutal spliting 373 | let mut split_of_array: Vec = vec![]; 374 | let mut current_element = String::new(); 375 | let mut is_variable_active = false; 376 | let mut is_string_active = false; 377 | let mut is_integer_active = false; 378 | let mut dot_count = 0; 379 | let mut valid_for_next_element = true; 380 | 381 | for (position, character) in array.chars().enumerate() { 382 | if is_string_active && character != '"' && position == array.len() - 1 { 383 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("STRING IN THE ARRAY ISN'T CLOSED!".to_string()))); 384 | break; 385 | } 386 | if character == '"' { 387 | if !(is_string_active) && !(is_integer_active) { 388 | current_element.push(character); 389 | is_string_active = true; 390 | } 391 | else if is_integer_active { 392 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("INVALID CHARACTER IN THE ARRAY!".to_string()))); 393 | break; 394 | } 395 | else if is_string_active { 396 | current_element.push(character); 397 | split_of_array.push(ArrayTypesEnum::String(current_element)); 398 | current_element = String::new(); 399 | is_string_active = false; 400 | valid_for_next_element = false; 401 | } 402 | } 403 | else if character == '-' { 404 | if !(is_string_active) && !(is_integer_active) && valid_for_next_element { 405 | if array.chars().nth(position+1).unwrap().is_numeric() { 406 | current_element.push(character); 407 | is_integer_active = true; 408 | } else { 409 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("INVALID CHARACTER IN THE ARRAY!".to_string()))); 410 | break; 411 | } 412 | } 413 | } 414 | else if character == ',' { 415 | if !(is_string_active) && !(is_integer_active) && !(is_variable_active) { 416 | if !(valid_for_next_element) { 417 | valid_for_next_element = true; 418 | } else { 419 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THERE ARE TOO MANY COMMAS IN THE ARRAY!".to_string()))); 420 | break; 421 | } 422 | } 423 | else if is_variable_active { 424 | match exec_data_variable.global_variables.get(¤t_element) { 425 | Some(value_of_variable) => { 426 | match value_of_variable { 427 | ValueEnum::String(v) => { 428 | split_of_array.push(ArrayTypesEnum::String(v.to_string())); 429 | current_element = String::new(); 430 | is_string_active = false; 431 | valid_for_next_element = true; 432 | }, 433 | ValueEnum::Integer(v) => { 434 | split_of_array.push(ArrayTypesEnum::Integer(*v)); 435 | current_element = String::new(); 436 | is_variable_active = false; 437 | }, 438 | ValueEnum::Float(v) => { 439 | split_of_array.push(ArrayTypesEnum::Float(*v)); 440 | current_element = String::new(); 441 | is_variable_active = false; 442 | }, 443 | _ => { 444 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("VARIABLES IN ARRAYS CAN'T BE ARRAYS!".to_string()))); 445 | break; 446 | } 447 | } 448 | }, 449 | None => { 450 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String(format!("THERE IS NO VARIABLE CALLED {}!", current_element)))); 451 | break; 452 | } 453 | } 454 | } 455 | else if is_integer_active { 456 | if current_element.parse::().is_ok() { 457 | split_of_array.push(ArrayTypesEnum::Integer(current_element.parse::().unwrap())); 458 | current_element = String::new(); 459 | dot_count = 0; 460 | is_integer_active = false; 461 | } 462 | else if current_element.parse::().is_ok() { 463 | split_of_array.push(ArrayTypesEnum::Float(current_element.parse::().unwrap())); 464 | current_element = String::new(); 465 | dot_count = 0; 466 | is_integer_active = false; 467 | } else { 468 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("NUMBER ELEMENTS OF AN ARRAY HAVE TO BE I32 OR F32!".to_string()))); 469 | break; 470 | } 471 | } 472 | } else { 473 | if !(valid_for_next_element) { 474 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("A COMMA IS MISSING!".to_string()))); 475 | break; 476 | } 477 | else if !(is_string_active) && !(is_integer_active) && !(is_variable_active) { 478 | if character.is_numeric() { 479 | current_element.push(character); 480 | is_integer_active = true; 481 | } else { 482 | if character != ' ' { 483 | current_element.push(character); 484 | is_variable_active = true; 485 | } 486 | } 487 | } 488 | else if is_variable_active { 489 | if character == ' ' { 490 | match exec_data_variable.global_variables.get(¤t_element) { 491 | Some(value_of_variable) => { 492 | match value_of_variable { 493 | ValueEnum::String(v) => { 494 | split_of_array.push(ArrayTypesEnum::String(v.to_string())); 495 | current_element = String::new(); 496 | is_string_active = false; 497 | valid_for_next_element = false; 498 | }, 499 | ValueEnum::Integer(v) => { 500 | split_of_array.push(ArrayTypesEnum::Integer(*v)); 501 | current_element = String::new(); 502 | is_variable_active = false; 503 | valid_for_next_element = false; 504 | }, 505 | ValueEnum::Float(v) => { 506 | split_of_array.push(ArrayTypesEnum::Float(*v)); 507 | current_element = String::new(); 508 | is_variable_active = false; 509 | valid_for_next_element = false; 510 | }, 511 | _ => { 512 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("VARIABLES IN ARRAYS CAN'T BE ARRAYS!".to_string()))); 513 | break; 514 | } 515 | } 516 | }, 517 | None => { 518 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String(format!("THERE IS NO VARIABLE CALLED {}!", current_element)))); 519 | break; 520 | } 521 | } 522 | } 523 | else if !(allowed_variable_function_characters.contains(&character)) { 524 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("VARIABLE/FUNCTION NAME INCLUDES INVALID CHARACTERS!".to_string()))); 525 | break; 526 | } else { 527 | current_element.push(character); 528 | } 529 | } 530 | else if is_string_active { 531 | if !(allowed_string_inner_part_characters.contains(&character)) { 532 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("INVALID CHARACTER INSIDE OF THE STRING IN THE ARRAY!".to_string()))); 533 | break; 534 | } 535 | current_element.push(character); 536 | } 537 | else if is_integer_active { 538 | if character.is_numeric() { 539 | current_element.push(character); 540 | } 541 | else if character == ' ' { 542 | if part.parse::().is_ok() { 543 | split_of_array.push(ArrayTypesEnum::Integer(part.parse::().unwrap())); 544 | current_element = String::new(); 545 | dot_count = 0; 546 | is_integer_active = false; 547 | valid_for_next_element = false; 548 | } 549 | else if part.parse::().is_ok() { 550 | split_of_array.push(ArrayTypesEnum::Float(part.parse::().unwrap())); 551 | current_element = String::new(); 552 | dot_count = 0; 553 | is_integer_active = false; 554 | valid_for_next_element = false; 555 | } else { 556 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("NUMBER ELEMENTS OF AN ARRAY HAVE TO BE I32 OR F32!".to_string()))); 557 | break; 558 | } 559 | } 560 | else if character == '.' { 561 | if dot_count > 0 { 562 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("THERE ARE TOO MANY DOTS IN THE NUMBER!".to_string()))); 563 | break; 564 | } else { 565 | current_element.push(character); 566 | dot_count += 1; 567 | } 568 | } else { 569 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("INVALID CHARACTER IN THE ARRAY!".to_string()))); 570 | break; 571 | } 572 | } 573 | } 574 | } 575 | 576 | final_tokens.push((token_classification[9].to_string(), ValueEnum::Array(split_of_array))); 577 | } 578 | // variable/function name check 579 | else { 580 | let mut is_valid_name = true; 581 | for character in part.chars() { 582 | if !(allowed_variable_function_characters.contains(&character)) { 583 | is_valid_name = false; 584 | } 585 | } 586 | if is_valid_name { 587 | final_tokens.push((token_classification[10].to_string(), ValueEnum::String(part.to_string()))); 588 | } else { 589 | final_tokens.push(("ERROR_MESSAGE".to_string(), ValueEnum::String("VARIABLE/FUNCTION NAME INCLUDES INVALID CHARACTERS!".to_string()))); 590 | break; 591 | } 592 | } 593 | 594 | i += 1; 595 | } 596 | return final_tokens; 597 | } 598 | -------------------------------------------------------------------------------- /src/verine_expression.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of an interpreter for a programming language called minosrus_lang 3 | * Copyright (C) 2020-2021 Yves Vollmeier 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | */ 19 | 20 | 21 | use std::collections::HashMap; 22 | 23 | use crate::tokenizer; 24 | use crate::tokenizer::{ArrayTypesEnum, ValueEnum}; 25 | use crate::verine_expression::VerineTokenizerError::*; 26 | 27 | #[derive(Debug, Clone)] 28 | pub enum Token { 29 | Id(String), 30 | Value(VerineValue), 31 | Operator(Op), 32 | Get, 33 | From, 34 | Len, 35 | At, 36 | StringFrom, 37 | IntegerFrom, 38 | FloatFrom, 39 | ReadLn, 40 | OpenVerine, 41 | CloseVerine, 42 | } 43 | 44 | #[derive(Debug, Clone)] 45 | pub enum VerineValue { 46 | Float(f32), 47 | Integer(i32), 48 | String(String), 49 | } 50 | 51 | impl From for Token { 52 | fn from(value: VerineValue) -> Self { 53 | Self::Value(value) 54 | } 55 | } 56 | 57 | #[derive(Debug, Copy, Clone)] 58 | pub enum Op { 59 | Plus, 60 | Minus, 61 | Asterisk, 62 | Slash, 63 | Pow, 64 | } 65 | 66 | impl From for Token { 67 | fn from(op: Op) -> Self { 68 | Self::Operator(op) 69 | } 70 | } 71 | 72 | pub enum VerineTokenizerError { 73 | UnexpectedCharacter(char), 74 | StdInError, 75 | InvalidExpression, 76 | VariableNotFound(String), 77 | NumberNotAnInteger(String), 78 | NumberNotAFloat(String), 79 | InvalidOperands, 80 | InvalidIndex(String), 81 | IndexOutOfBounds, 82 | TypeNotIndexable, 83 | TypeHasNoLength, 84 | DivisionByZero, 85 | StringLiteralNotClosed, 86 | UnsupportedReturnType, // Returning arrays is not supported 87 | } 88 | 89 | pub struct VerineTokenizer<'a> { 90 | view: &'a [char], 91 | tokens: Vec, 92 | } 93 | 94 | type Globals = HashMap; 95 | 96 | impl<'a> VerineTokenizer<'a> { 97 | pub fn new(view: &'a [char]) -> Self { 98 | Self { 99 | view, 100 | tokens: vec![], 101 | } 102 | } 103 | 104 | pub fn tokenize(&mut self) -> Result, VerineTokenizerError> { 105 | loop { 106 | let is_the_last_token_an_operator = { 107 | match self.tokens.last() { 108 | None => true, 109 | Some(Token::Operator(_)) => true, 110 | Some(_) => false, 111 | } 112 | }; 113 | 114 | match self.view { 115 | [w, ..] if w.is_whitespace() => self.view = &self.view[1..], 116 | ['"', ..] => self.process_string_literals()?, 117 | [digit, ..] if digit.is_ascii_digit() => self.process_numeric_literals()?, 118 | ['+', digit, ..] if digit.is_ascii_digit() && is_the_last_token_an_operator => self.process_numeric_literals()?, 119 | ['-', digit, ..] if digit.is_ascii_digit() && is_the_last_token_an_operator => self.process_numeric_literals()?, 120 | [p, ..] if is_punctuation(*p) => self.process_operators_and_punctuation()?, 121 | [c, ..] if is_valid_identifier_character(*c) => self.process_keywords_and_identifiers()?, 122 | [e, ..] => return Err(VerineTokenizerError::UnexpectedCharacter(*e)), 123 | [] => break, 124 | } 125 | } 126 | Ok(self.tokens.clone()) 127 | } 128 | 129 | fn process_keywords_and_identifiers(&mut self) -> Result<(), VerineTokenizerError> { 130 | let start = self.view; 131 | let mut i = 0; 132 | 133 | fn end_token(start: &[char], i: usize) -> Option { 134 | match start.is_empty() { 135 | true => None, 136 | false => { 137 | let token = start[..i].iter().collect::(); 138 | let token = match token.as_str() { 139 | "GET" => Token::Get, 140 | "FROM" => Token::From, 141 | "LEN" => Token::Len, 142 | "AT" => Token::At, 143 | "STRING_FROM" => Token::StringFrom, 144 | "INTEGER_FROM" => Token::IntegerFrom, 145 | "FLOAT_FROM" => Token::FloatFrom, 146 | "READLN" => Token::ReadLn, 147 | _ => Token::Id(start[..i].iter().collect::()) 148 | }; 149 | Some(token) 150 | } 151 | } 152 | } 153 | 154 | loop { 155 | match self.view { 156 | [c, ..] if !is_valid_identifier_character(*c) => { 157 | if let Some(token) = end_token(start, i) { 158 | self.tokens.push(token); 159 | } 160 | break Ok(()); 161 | } 162 | [] => { 163 | if let Some(token) = end_token(start, i) { 164 | self.tokens.push(token); 165 | } 166 | break Ok(()); 167 | } 168 | [_, ..] => { 169 | self.view = &self.view[1..]; 170 | i += 1; 171 | } 172 | } 173 | } 174 | } 175 | 176 | fn process_operators_and_punctuation(&mut self) -> Result<(), VerineTokenizerError> { 177 | let token = match self.view { 178 | ['(', ..] => Some((1, Token::OpenVerine)), 179 | [')', ..] => Some((1, Token::CloseVerine)), 180 | ['+', ..] => Some((1, Op::Plus.into())), 181 | ['-', ..] => Some((1, Op::Minus.into())), 182 | ['*', '*', ..] => Some((2, Op::Pow.into())), 183 | ['*', ..] => Some((1, Op::Asterisk.into())), 184 | ['/', ..] => Some((1, Op::Slash.into())), 185 | _ => None, 186 | }; 187 | if let Some((n, token)) = token { 188 | self.tokens.push(token); 189 | self.view = &self.view[n..]; 190 | } 191 | Ok(()) 192 | } 193 | 194 | fn process_string_literals(&mut self) -> Result<(), VerineTokenizerError> { 195 | self.view = &self.view[1..]; // Eat first quote 196 | let start = self.view; 197 | let mut i = 0; 198 | loop { 199 | match self.view { 200 | ['\\', '"', ..] => { 201 | self.view = &self.view[2..]; 202 | i += 2; 203 | } 204 | ['"', ..] => { 205 | let string = start[..i].iter().collect::(); 206 | self.tokens.push(VerineValue::String(string).into()); 207 | 208 | self.view = &self.view[1..]; // Eat last quote 209 | break Ok(()); 210 | } 211 | [_, ..] => { 212 | self.view = &self.view[1..]; 213 | i += 1; 214 | } 215 | [] => break Err(StringLiteralNotClosed), 216 | } 217 | } 218 | } 219 | 220 | fn process_numeric_literals(&mut self) -> Result<(), VerineTokenizerError> { 221 | let start = self.view; 222 | let mut i = 0; 223 | let mut is_float = false; 224 | 225 | let mut is_sign_allowed = true; 226 | let mut is_point_allowed = true; 227 | 228 | loop { 229 | match self.view { 230 | ['+', ..] | ['-', ..] if is_sign_allowed => { 231 | is_sign_allowed = false; 232 | 233 | self.view = &self.view[1..]; 234 | i += 1; 235 | } 236 | ['.', ..] if is_point_allowed => { 237 | is_point_allowed = false; 238 | is_sign_allowed = false; 239 | is_float = true; 240 | 241 | self.view = &self.view[1..]; 242 | i += 1; 243 | } 244 | [d, ..] if d.is_ascii_digit() => { 245 | is_sign_allowed = false; 246 | 247 | self.view = &self.view[1..]; 248 | i += 1; 249 | } 250 | _ => { 251 | let number = &start[..i].iter().collect::(); 252 | self.tokens.push(if is_float { 253 | let float = number.parse::().unwrap(); 254 | VerineValue::Float(float).into() 255 | } else { 256 | let integer = number.parse::().unwrap(); 257 | VerineValue::Integer(integer).into() 258 | }); 259 | break Ok(()) 260 | } 261 | } 262 | } 263 | } 264 | 265 | pub fn evaluate(mut tokens: Vec, global_variables: &Globals) -> Result { 266 | // Nested verine expression evaluation 267 | { 268 | // Remember the ranges of top level verine expressions 269 | let mut verine_expression_ranges = vec![]; 270 | let mut verine_level = 0; 271 | let mut opening_verine = 0; 272 | 273 | for (i, token) in tokens.iter().enumerate() { 274 | match token { 275 | Token::OpenVerine => { 276 | if verine_level == 0 { 277 | opening_verine = i; 278 | } 279 | verine_level += 1; 280 | } 281 | Token::CloseVerine => { 282 | verine_level -= 1; 283 | if verine_level == 0 { 284 | verine_expression_ranges.push(opening_verine..=i); 285 | } 286 | } 287 | _ => () 288 | } 289 | } 290 | 291 | // Evaluate each verine expression 292 | let mut resulting_tokens: Vec = vec![]; 293 | 294 | for range in &verine_expression_ranges { 295 | let without_verines = range.start() + 1..*range.end(); 296 | let result = VerineTokenizer::evaluate(tokens[without_verines].to_vec(), &global_variables)?; 297 | resulting_tokens.push(result.into()); 298 | } 299 | 300 | // Replace the tokens of top level verine expression with their resulting token 301 | // We need to shift the ranges to the left because we remove from the beginning of the vector 302 | let mut shift = 0; 303 | for (range, token) in verine_expression_ranges.iter().zip(resulting_tokens) { 304 | let range = range.start() - shift..=range.end() - shift; 305 | tokens.splice(range.clone(), std::iter::once(token)); 306 | shift += range.end() - range.start(); 307 | } 308 | } 309 | 310 | // Start evaluating this verine expression 311 | 312 | let get_global_variable = |var: &str| { 313 | global_variables.get(var).ok_or(VariableNotFound(var.to_owned())) 314 | }; 315 | 316 | // Do a first pass for READLN 317 | for token in &mut tokens { 318 | if matches!(token, Token::ReadLn) { 319 | let mut input = String::new(); 320 | std::io::stdin().read_line(&mut input).map_err(|_| StdInError)?; 321 | input.pop(); // Remove \n 322 | *token = VerineValue::String(input).into() 323 | } 324 | } 325 | 326 | // Do a second pass for STRING_FROM and INTEGER_FROM 327 | let mut tokens = { 328 | let mut new_tokens = vec![]; 329 | 330 | let mut tokens = tokens.as_slice(); 331 | loop { 332 | match tokens { 333 | [Token::StringFrom, argument, ..] => { 334 | let argument = Self::evaluate(vec![argument.clone()], global_variables)?; 335 | let argument_string = match argument { 336 | VerineValue::String(str) => str, 337 | VerineValue::Integer(int) => int.to_string(), 338 | VerineValue::Float(float) => float.to_string(), 339 | }; 340 | new_tokens.push(VerineValue::String(argument_string).into()); 341 | tokens = &tokens[2..]; 342 | } 343 | [Token::IntegerFrom, argument, ..] => { 344 | let argument = Self::evaluate(vec![argument.clone()], global_variables)?; 345 | let argument_int = match argument { 346 | VerineValue::Integer(int) => int, 347 | VerineValue::Float(float) => float as i32, 348 | VerineValue::String(str) => str.parse::().map_err(|_| NumberNotAnInteger(str))?, 349 | }; 350 | new_tokens.push(VerineValue::Integer(argument_int).into()); 351 | tokens = &tokens[2..]; 352 | } 353 | [Token::FloatFrom, argument, ..] => { 354 | let argument = Self::evaluate(vec![argument.clone()], global_variables)?; 355 | let argument_float = match argument { 356 | VerineValue::Integer(int) => int as f32, 357 | VerineValue::Float(float) => float, 358 | VerineValue::String(str) => str.parse::().map_err(|_| NumberNotAFloat(str))?, 359 | }; 360 | new_tokens.push(VerineValue::Float(argument_float).into()); 361 | tokens = &tokens[2..]; 362 | } 363 | [token, ..] => { 364 | new_tokens.push(token.clone()); 365 | tokens = &tokens[1..]; 366 | } 367 | [] => break, 368 | } 369 | } 370 | 371 | new_tokens 372 | }; 373 | 374 | use Token::{Get, From, Id, At, Len}; 375 | 376 | // Final pass, evaluation 377 | loop { 378 | match tokens.as_slice() { 379 | [left, Token::Operator(op), right, ..] => { 380 | let left = VerineTokenizer::evaluate(vec![left.clone()], global_variables)?; 381 | let right = VerineTokenizer::evaluate(vec![right.clone()], global_variables)?; 382 | 383 | fn compute_float_operation(l: f32, op: &Op, r: f32) -> VerineValue { 384 | match op { 385 | Op::Plus => VerineValue::Float(l + r), 386 | Op::Minus => VerineValue::Float(l - r), 387 | Op::Asterisk => VerineValue::Float(l * r), 388 | Op::Slash => VerineValue::Float(l / r), 389 | Op::Pow => VerineValue::Float(l.powf(r)) 390 | } 391 | } 392 | 393 | use VerineValue::*; 394 | let result = match (left, op, right) { 395 | // String concatenation 396 | (String(l), Op::Plus, String(r)) => String(format!("{}{}", l, r)), 397 | (String(l), Op::Plus, Integer(r)) => String(format!("{}{}", l, r)), 398 | (String(l), Op::Plus, Float(r)) => String(format!("{}{}", l, r)), 399 | (Integer(l), Op::Plus, String(r)) => String(format!("{}{}", l, r)), 400 | (Float(l), Op::Plus, String(r)) => String(format!("{}{}", l, r)), 401 | // int [op] int = int 402 | (Integer(l), _, Integer(r)) => { 403 | match op { 404 | Op::Plus => Integer(l + r), 405 | Op::Minus => Integer(l - r), 406 | Op::Asterisk => Integer(l * r), 407 | Op::Slash => Integer(l.checked_div(r).ok_or(DivisionByZero)?), 408 | Op::Pow => Float((l as f32).powi(r)) 409 | } 410 | } 411 | // Implicit int to float conversion 412 | (Integer(l), _, Float(r)) => compute_float_operation(l as f32, op, r), 413 | (Float(l), _, Integer(r)) => compute_float_operation(l, op, r as f32), 414 | (Float(l), _, Float(r)) => compute_float_operation(l, op, r), 415 | _ => return Err(InvalidOperands) 416 | }; 417 | // Replace the first 3 tokens with the result of them 418 | tokens.splice(0..3, std::iter::once(result.into())); 419 | } 420 | [Get, From, Id(var), At, index, ..] => { 421 | let index = match index { 422 | Id(id) => { 423 | match get_global_variable(id)? { 424 | &ValueEnum::Integer(index) => index as usize, 425 | _ => return Err(InvalidIndex(var.to_owned())) 426 | } 427 | } 428 | Token::Value(VerineValue::Integer(index)) => *index as usize, 429 | _ => return Err(InvalidIndex(var.to_owned())) 430 | }; 431 | 432 | let result = match get_global_variable(var)? { 433 | ValueEnum::Array(array) => { 434 | match array.get(index).ok_or(IndexOutOfBounds)? { 435 | ArrayTypesEnum::String(s) => VerineValue::String(s.to_owned()), 436 | ArrayTypesEnum::Integer(i) => VerineValue::Integer(*i), 437 | ArrayTypesEnum::Float(f) => VerineValue::Float(*f), 438 | } 439 | } 440 | ValueEnum::String(var) => { 441 | let char = var.chars().nth(index).ok_or(IndexOutOfBounds)?; 442 | VerineValue::String(char.to_string()) 443 | } 444 | _ => return Err(TypeNotIndexable) 445 | }; 446 | 447 | tokens.splice(0..5, std::iter::once(result.into())); 448 | } 449 | [Get, From, Id(var), Len, ..] => { 450 | let result = match get_global_variable(var)? { 451 | ValueEnum::Array(array) => VerineValue::Integer(array.len() as i32), 452 | ValueEnum::String(var) => VerineValue::Integer(var.len() as i32), 453 | _ => return Err(TypeHasNoLength) 454 | }; 455 | 456 | tokens.splice(0..4, std::iter::once(result.into())); 457 | } 458 | [single] => { 459 | // Make sure the remaining token is valid for the interpreter 460 | let single = match single.clone() { 461 | Token::Id(var) => { 462 | match get_global_variable(&var)? { 463 | ValueEnum::String(str) => VerineValue::String(str.to_owned()), 464 | ValueEnum::Integer(int) => VerineValue::Integer(*int), 465 | ValueEnum::Float(float) => VerineValue::Float(*float), 466 | _ => return Err(UnsupportedReturnType) 467 | } 468 | } 469 | Token::Value(value) => value, 470 | _ => return Err(InvalidExpression) 471 | }; 472 | break Ok(single) 473 | } 474 | _ => break Err(InvalidExpression) 475 | } 476 | } 477 | } 478 | } 479 | 480 | fn is_valid_identifier_character(c: char) -> bool { 481 | c.is_alphanumeric() || c == '_' 482 | } 483 | 484 | fn is_punctuation(c: char) -> bool { 485 | match c { 486 | '+' | '-' | '*' | '/' | '.' | '(' | ')' => true, 487 | _ => false, 488 | } 489 | } -------------------------------------------------------------------------------- /to_do.txt: -------------------------------------------------------------------------------- 1 | - fix bugs of examples: guessing_game 2 | 3 | - error message for errors in a code block get indexed with the index of END 4 | e.g.: 5 | - ERROR OCCURED ON LINE NR. 19: ' END' 6 | -> EXECUTION ERROR: '' IS NOT A INTEGER! 7 | INTERPRETER STOPPED DUE PREVIOUS RUNTIME ERROR! 8 | --------------------------------------------------------------------------------