diff --git a/.gitignore b/.gitignore index 11b54c03d87a65caf1953a871b1c181b1ec30b6a..1f1c3ea654680736455a1fbcef1405f5226c6db7 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,7 @@ replay_pid* # Intellij /.idea /out -*/target +target # Server server-*/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..f288702d2fa16d3cdf0035b15a9fcbc552cd88e7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. diff --git a/pom.xml b/pom.xml index 55dd35201b7510ed6d6e1dce6b41a0684d844d27..5e44fc481b0a742e69185ba99dfb1e12842e9ec6 100644 --- a/pom.xml +++ b/pom.xml @@ -21,23 +21,6 @@ <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-shade-plugin</artifactId> - <version>3.3.1-SNAPSHOT</version> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>shade</goal> - </goals> - <configuration> - <minimizeJar>true</minimizeJar> - <createDependencyReducedPom>false</createDependencyReducedPom> - </configuration> - </execution> - </executions> - </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> @@ -88,13 +71,6 @@ <scope>provided</scope> </dependency> - <dependency> - <groupId>com.bedrockk</groupId> - <artifactId>molang</artifactId> - <version>1.0-SNAPSHOT</version> - <scope>compile</scope> - </dependency> - <!-- Spigot --> <dependency> <groupId>org.spigotmc</groupId> diff --git a/src/main/java/com/bedrockk/molang/ExprFinder.java b/src/main/java/com/bedrockk/molang/ExprFinder.java new file mode 100644 index 0000000000000000000000000000000000000000..7dd429d05e528a11391a0d9faa8bc6ee380e5456 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ExprFinder.java @@ -0,0 +1,30 @@ +package com.bedrockk.molang; + +import com.bedrockk.molang.visitor.FindingVisitor; +import com.bedrockk.molang.visitor.FirstFindingVisitor; + +import java.util.List; +import java.util.function.Predicate; + +public class ExprFinder { + + public static List<Expression> find(List<Expression> expressions, Predicate<Expression> predicate) { + ExprTraverser traverser = new ExprTraverser(); + FindingVisitor visitor = new FindingVisitor(predicate); + + traverser.getVisitors().add(visitor); + traverser.traverse(expressions); + + return visitor.getFoundExpressions(); + } + + public static Expression findFirst(List<Expression> expressions, Predicate<Expression> predicate) { + ExprTraverser traverser = new ExprTraverser(); + FirstFindingVisitor visitor = new FirstFindingVisitor(predicate); + + traverser.getVisitors().add(visitor); + traverser.traverse(expressions); + + return visitor.getFound(); + } +} diff --git a/src/main/java/com/bedrockk/molang/ExprTraverser.java b/src/main/java/com/bedrockk/molang/ExprTraverser.java new file mode 100644 index 0000000000000000000000000000000000000000..580ef0c8f8f2380d38ac5512c90e100de62968ae --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ExprTraverser.java @@ -0,0 +1,187 @@ +package com.bedrockk.molang; + +import lombok.Getter; +import lombok.NonNull; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +@Getter +public class ExprTraverser { + + private boolean stopTraversal = false; + private final List<ExprVisitor> visitors = new LinkedList<>(); + + public void traverse(List<Expression> expressions) { + for (ExprVisitor visitor : visitors) { + visitor.beforeTraverse(expressions); + } + + stopTraversal = false; + traverseArray(expressions); + + for (ExprVisitor visitor : visitors) { + visitor.afterTraverse(expressions); + } + } + + private void traverseArray(List<Expression> expressions) { + List<Expression> list = new ArrayList<>(expressions); + + for (int i = 0; i < list.size(); i++) { + Expression expression = list.get(i); + + boolean removeCurrent = false; + boolean traverseChildren = true; + boolean traverseCurrent = true; + + for (ExprVisitor visitor : visitors) { + Object result = visitor.onVisit(expression); + + if (result instanceof ActionType) { + switch ((ActionType) result) { + case REMOVE_CURRENT: + removeCurrent = true; + break; + case STOP_TRAVERSAL: + stopTraversal = true; + break; + case DONT_TRAVERSE_CURRENT_AND_CHILDREN: + traverseCurrent = false; + case DONT_TRAVERSE_CHILDREN: + traverseChildren = false; + break; + } + } else if (result instanceof Expression) { + expression = (Expression) result; + } + } + + if (!traverseCurrent) { + break; + } else if (traverseChildren && !removeCurrent) { + traverseExpr(expression); + } + + for (ExprVisitor visitor : visitors) { + visitor.onLeave(expression); + } + + if (removeCurrent) { + expressions.remove(i); + } else { + expressions.set(i, expression); + } + + if (stopTraversal) { + break; + } + } + } + + private void traverseExpr(@NonNull Expression expression) { + for (Field field : getAllFields(expression.getClass())) { + field.setAccessible(true); + Object fieldValue = getFieldValue(field, expression); + + if (fieldValue instanceof Expression subExpr) { + boolean removeCurrent = false; + boolean traverseChildren = true; + boolean traverseCurrent = true; + + for (ExprVisitor visitor : visitors) { + Object result = visitor.onVisit(subExpr); + + if (result instanceof ActionType) { + switch ((ActionType) result) { + case REMOVE_CURRENT: + removeCurrent = true; + break; + case STOP_TRAVERSAL: + stopTraversal = true; + break; + case DONT_TRAVERSE_CURRENT_AND_CHILDREN: + traverseCurrent = false; + case DONT_TRAVERSE_CHILDREN: + traverseChildren = false; + break; + } + } else if (result instanceof Expression) { + subExpr = (Expression) result; + } + } + + if (!traverseCurrent) { + break; + } else if (traverseChildren && !removeCurrent) { + traverseExpr(subExpr); + } + + for (ExprVisitor visitor : visitors) { + visitor.onLeave(subExpr); + } + + if (removeCurrent) { + setFieldValue(field, expression, null); + } else { + setFieldValue(field, expression, subExpr); + } + + if (stopTraversal) { + break; + } + } else if (fieldValue != null && fieldValue.getClass().isArray()) { + Object[] array = (Object[]) fieldValue; + List<Expression> exprs = new ArrayList<>(); + + for (Object i : array) { + if (i instanceof Expression) { + exprs.add((Expression) i); + } + } + + traverseArray(exprs); + + setFieldValue(field, expression, exprs.toArray(new Expression[0])); + } + } + } + + public static List<Field> getAllFields(Class<?> type) { + List<Field> fields = new ArrayList<>(); + + for (Class<?> c = type; c != null; c = c.getSuperclass()) { + fields.addAll(Arrays.asList(c.getDeclaredFields())); + } + + return fields; + } + + private Object getFieldValue(Field field, Object obj) { + try { + return field.get(obj); + } catch (Throwable throwable) { + // noop + } + + return null; + } + + private void setFieldValue(Field field, Object obj, Object value) { + try { + field.set(obj, value); + } catch (Throwable throwable) { + // noop + } + } + + public enum ActionType{ + REMOVE_CURRENT, + STOP_TRAVERSAL, + DONT_TRAVERSE_CURRENT_AND_CHILDREN, + DONT_TRAVERSE_CHILDREN + } +} diff --git a/src/main/java/com/bedrockk/molang/ExprVisitor.java b/src/main/java/com/bedrockk/molang/ExprVisitor.java new file mode 100644 index 0000000000000000000000000000000000000000..df76a9d4241f32d8df1ea6ce3d06c44fe2bb8ebc --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ExprVisitor.java @@ -0,0 +1,20 @@ +package com.bedrockk.molang; + +import java.util.List; + +public interface ExprVisitor { + + default void beforeTraverse(List<Expression> expressions) { + // noop + } + + Object onVisit(Expression expression); + + default void onLeave(Expression expression) { + // noop + } + + default void afterTraverse(List<Expression> expressions) { + // noop + } +} diff --git a/src/main/java/com/bedrockk/molang/Expression.java b/src/main/java/com/bedrockk/molang/Expression.java new file mode 100644 index 0000000000000000000000000000000000000000..c469775dbe21a301152b1f6a63dfccd03589007b --- /dev/null +++ b/src/main/java/com/bedrockk/molang/Expression.java @@ -0,0 +1,23 @@ +package com.bedrockk.molang; + +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; + +import java.util.HashMap; +import java.util.Map; + +public interface Expression { + + Map<String, Object> attributes = new HashMap<>(); + + default Map<String, Object> getAttributes() { + return attributes; + } + + MoValue evaluate(MoScope scope, MoLangEnvironment environment); + + default void assign(MoScope scope, MoLangEnvironment environment, MoValue value) { + throw new RuntimeException("Cannot assign a value to " + this.getClass()); + } +} diff --git a/src/main/java/com/bedrockk/molang/MoLang.java b/src/main/java/com/bedrockk/molang/MoLang.java new file mode 100644 index 0000000000000000000000000000000000000000..260b6ccb124c35fe9dce9c7471e35403f1d4a38f --- /dev/null +++ b/src/main/java/com/bedrockk/molang/MoLang.java @@ -0,0 +1,51 @@ +package com.bedrockk.molang; + +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.tokenizer.TokenIterator; +import com.bedrockk.molang.runtime.MoLangRuntime; +import com.bedrockk.molang.utils.FileUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class MoLang { + + public static List<Expression> parse(String code) { + return (createParser(code)).parse(); + } + + public static List<Expression> parse(Path path) { + return (createParser(path)).parse(); + } + + public static List<Expression> parse(InputStream stream) throws IOException { + return (createParser(stream)).parse(); + } + + public static MoLangParser createParser(String code) { + return new MoLangParser(new TokenIterator(code)); + } + + public static MoLangParser createParser(Path path) { + byte[] fileBytes; + try { + fileBytes = Files.readAllBytes(path); + } catch (IOException e) { + fileBytes = new byte[0]; + } + + return new MoLangParser(new TokenIterator(new String(fileBytes, StandardCharsets.UTF_8))); + } + + public static MoLangParser createParser(InputStream stream) throws IOException { + return new MoLangParser(new TokenIterator(FileUtils.readFile(stream))); + } + + public static MoLangRuntime createRuntime() { + return new MoLangRuntime(); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/ArrayAccessExpression.java b/src/main/java/com/bedrockk/molang/ast/ArrayAccessExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..720570a85f591fd1584ddc2879cea5a83dea15a4 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/ArrayAccessExpression.java @@ -0,0 +1,28 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class ArrayAccessExpression implements Expression { + + Expression array; + Expression index; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + String name = array instanceof NameExpression ? ((NameExpression) array).getName() : array.evaluate(scope, environment).asString(); + + return environment.getValue(name + "." + (int) index.evaluate(scope, environment).asDouble()); + } + + @Override + public void assign(MoScope scope, MoLangEnvironment environment, MoValue value) { + String name = array instanceof NameExpression ? ((NameExpression) array).getName() : array.evaluate(scope, environment).asString(); + + environment.setValue(name + "." + (int) index.evaluate(scope, environment).asDouble(), value); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/AssignExpression.java b/src/main/java/com/bedrockk/molang/ast/AssignExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..793249d86c1f391446b78143e324e3cbc099b740 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/AssignExpression.java @@ -0,0 +1,21 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class AssignExpression implements Expression { + + Expression variable; + Expression expr; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + var value = expr.evaluate(scope, environment); + variable.assign(scope, environment, value); + return value; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/BinaryOpExpression.java b/src/main/java/com/bedrockk/molang/ast/BinaryOpExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..a4ff1ce5eb22c4980446b90468fb4b07409d0318 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/BinaryOpExpression.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +abstract public class BinaryOpExpression implements Expression { + + protected final Expression left; + protected final Expression right; + + public abstract String getSigil(); +} diff --git a/src/main/java/com/bedrockk/molang/ast/BooleanExpression.java b/src/main/java/com/bedrockk/molang/ast/BooleanExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..def34ca28b9fec01d854f5ee9637ecfd1deb8df2 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/BooleanExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class BooleanExpression implements Expression { + + boolean value; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(value); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/BooleanNotExpression.java b/src/main/java/com/bedrockk/molang/ast/BooleanNotExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..2882f126b6cd37b01f24fcbc61c7d316a4f65673 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/BooleanNotExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class BooleanNotExpression implements Expression { + + Expression expression; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return expression.evaluate(scope, environment).equals(DoubleValue.ONE) ? DoubleValue.ZERO : DoubleValue.ONE; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/BreakExpression.java b/src/main/java/com/bedrockk/molang/ast/BreakExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..eb4c0e301b5e1c4cff48802e8052497dc42fef2b --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/BreakExpression.java @@ -0,0 +1,18 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class BreakExpression implements Expression { + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + scope.setBreak(true); + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/ContinueExpression.java b/src/main/java/com/bedrockk/molang/ast/ContinueExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..d415453dde7cb83e6c986c0c7e911776d2184970 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/ContinueExpression.java @@ -0,0 +1,18 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class ContinueExpression implements Expression { + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + scope.setContinue(true); + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/ForEachExpression.java b/src/main/java/com/bedrockk/molang/ast/ForEachExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..4e91202a1d1092a4649c9fe04e3fdb79e5ef33e6 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/ForEachExpression.java @@ -0,0 +1,42 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.struct.VariableStruct; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +import java.util.ArrayList; + +@Value +public class ForEachExpression implements Expression { + + Expression variable; + Expression array; + Expression body; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + MoValue array = this.array.evaluate(scope, environment); + + if (array instanceof VariableStruct) { + VariableStruct struct = (VariableStruct) array; + MoScope scope2 = new MoScope(); + + for (MoValue value : new ArrayList<>(struct.getMap().values())) { + variable.assign(scope2, environment, value); + body.evaluate(scope2, environment); + + if (scope2.getReturnValue() != null) { + return scope2.getReturnValue(); + } else if (scope2.isBreak()) { + break; + } + } + } + + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/FuncCallExpression.java b/src/main/java/com/bedrockk/molang/ast/FuncCallExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..06ac445dc52161839957771a5f35bc1806592727 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/FuncCallExpression.java @@ -0,0 +1,29 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoParams; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@Value +public class FuncCallExpression implements Expression { + + Expression name; + Expression[] args; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + List<Expression> params = Arrays.asList(args); + String name = this.name instanceof NameExpression ? ((NameExpression) this.name).getName() : this.name.evaluate(scope, environment).asString(); + + return environment.getValue(name, new MoParams( + params.stream().map(e -> e.evaluate(scope, environment)).collect(Collectors.toList()) + )); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/LoopExpression.java b/src/main/java/com/bedrockk/molang/ast/LoopExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..bc2596e21acab0ee48ab30a6bf0ab0550e394e7c --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/LoopExpression.java @@ -0,0 +1,34 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class LoopExpression implements Expression { + + Expression count; + Expression body; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + int loop = (int) count.evaluate(scope, environment).asDouble(); + MoScope subScope = new MoScope(); + + while (loop > 0) { + body.evaluate(subScope, environment); + loop--; + + if (subScope.getReturnValue() != null) { + return subScope.getReturnValue(); + } else if (subScope.isBreak()) { + break; + } + } + + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/NameExpression.java b/src/main/java/com/bedrockk/molang/ast/NameExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..42acdd037fe2f5794c425aeb16ca691f2abf0218 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/NameExpression.java @@ -0,0 +1,23 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class NameExpression implements Expression { + + String name; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return environment.getValue(name); + } + + @Override + public void assign(MoScope scope, MoLangEnvironment environment, MoValue value) { + environment.setValue(name, value); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/NumberExpression.java b/src/main/java/com/bedrockk/molang/ast/NumberExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..b22588bb6b885fa0bb2d170ccdd0b35d4135051d --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/NumberExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class NumberExpression implements Expression { + + double number; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(number); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/ReturnExpression.java b/src/main/java/com/bedrockk/molang/ast/ReturnExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..30a4db91c9ca6fbd442b8aae9e266f891b87799e --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/ReturnExpression.java @@ -0,0 +1,21 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class ReturnExpression implements Expression { + + Expression expression; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + MoValue eval = expression.evaluate(scope, environment); + scope.setReturnValue(eval); + + return eval; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/StatementExpression.java b/src/main/java/com/bedrockk/molang/ast/StatementExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..c77b05cffee1124a85ee6bcb1b031881baa4bfc8 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/StatementExpression.java @@ -0,0 +1,29 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class StatementExpression implements Expression { + + Expression[] expressions; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + for (Expression expression : expressions) { + expression.evaluate(scope, environment); + + if (scope.getReturnValue() != null) { + return scope.getReturnValue(); + } else if (scope.isBreak() || scope.isContinue()) { + break; + } + } + + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/StringExpression.java b/src/main/java/com/bedrockk/molang/ast/StringExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..3a84959f87de09c9af3a0d30219d117cbf20c4a1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/StringExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import com.bedrockk.molang.runtime.value.StringValue; +import lombok.Value; + +@Value +public class StringExpression implements Expression { + + String string; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new StringValue(string); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/TernaryExpression.java b/src/main/java/com/bedrockk/molang/ast/TernaryExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..c1503ef8926f586a998a6d12cc51993b9124a21a --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/TernaryExpression.java @@ -0,0 +1,27 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class TernaryExpression implements Expression { + + Expression condition; + Expression thenExpr; + Expression elseExpr; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + if (condition.evaluate(scope, environment).equals(DoubleValue.ONE)) { + return thenExpr == null ? condition.evaluate(scope, environment) : thenExpr.evaluate(scope, environment); + } else if (elseExpr != null) { + return elseExpr.evaluate(scope, environment); + } + + return DoubleValue.ZERO; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/ThisExpression.java b/src/main/java/com/bedrockk/molang/ast/ThisExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..525cda6e4ea91318d011c6ad2f55b5605a880fc1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/ThisExpression.java @@ -0,0 +1,16 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class ThisExpression implements Expression { + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return environment; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/UnaryMinusExpression.java b/src/main/java/com/bedrockk/molang/ast/UnaryMinusExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..3d2a8e6384084de4b39d8abfe4520538486d4825 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/UnaryMinusExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class UnaryMinusExpression implements Expression { + + Expression expression; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(-expression.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/UnaryPlusExpression.java b/src/main/java/com/bedrockk/molang/ast/UnaryPlusExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..b8911d7b97163017a292f4058bae36acd0caf747 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/UnaryPlusExpression.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.ast; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +@Value +public class UnaryPlusExpression implements Expression { + + Expression expression; + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(+expression.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/ArrowExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/ArrowExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..01087c7a926ba6f7982c3f00c90ee481ef946c55 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/ArrowExpression.java @@ -0,0 +1,29 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.MoValue; + +public class ArrowExpression extends BinaryOpExpression { + + public ArrowExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "->"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + Object leftEnv = left.evaluate(scope, environment); + if (leftEnv instanceof MoLangEnvironment) { + return right.evaluate(scope, (MoLangEnvironment) leftEnv); + } + + return null; + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanAndExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanAndExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..c0d78c8df60bf7f5d37454c8664cc566413d5b22 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanAndExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class BooleanAndExpression extends BinaryOpExpression { + + public BooleanAndExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "&&"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(!left.evaluate(scope, environment).equals(DoubleValue.ZERO) && !right.evaluate(scope, environment).equals(DoubleValue.ZERO)); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanOrExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanOrExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..b69f1ef7e35d0840571d32fa4e02b3ba834c4ffd --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/BooleanOrExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class BooleanOrExpression extends BinaryOpExpression { + + public BooleanOrExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "||"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(!left.evaluate(scope, environment).equals(DoubleValue.ZERO) || !right.evaluate(scope, environment).equals(DoubleValue.ZERO)); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/CoalesceExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/CoalesceExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..6c8fe23cc0c1578525e4f63cfda48fc20b385e93 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/CoalesceExpression.java @@ -0,0 +1,32 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class CoalesceExpression extends BinaryOpExpression { + + public CoalesceExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "??"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + MoValue evalLeft = left.evaluate(scope, environment); + MoValue value = environment.getValue(evalLeft.asString()); + + if (value == null || value.equals(DoubleValue.ZERO)) { + return right.evaluate(scope, environment); + } else { + return evalLeft; + } + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/DivideExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/DivideExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..a31917e2d28000513f3169f08df441d60266c70b --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/DivideExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class DivideExpression extends BinaryOpExpression { + + public DivideExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "/"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() / right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/EqualExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/EqualExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..d989efc8c3d5c13d9957c591db7b00fbf52c60f1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/EqualExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class EqualExpression extends BinaryOpExpression { + + public EqualExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "=="; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).equals(right.evaluate(scope, environment))); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..843e713f5c2f58919aee246b035c8b47d871e53f --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class GreaterExpression extends BinaryOpExpression { + + public GreaterExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return ">"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() > right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterOrEqualExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterOrEqualExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..1ccfe8ffd01054c140b37fec1629e83778e3d3ed --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/GreaterOrEqualExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class GreaterOrEqualExpression extends BinaryOpExpression { + + public GreaterOrEqualExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return ">="; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() >= right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/MinusExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/MinusExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..853277010291c4b34aae40f0d86572c7e7941677 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/MinusExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class MinusExpression extends BinaryOpExpression { + + public MinusExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "-"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() - right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/NotEqualExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/NotEqualExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..4209ada962e67c95e7ad4a255b0a4152bcf1942a --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/NotEqualExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class NotEqualExpression extends BinaryOpExpression { + + public NotEqualExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "!="; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(!left.evaluate(scope, environment).equals(right.evaluate(scope, environment))); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/PlusExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/PlusExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..76447ce27e820d3108990be82de6796689e1fa87 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/PlusExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class PlusExpression extends BinaryOpExpression { + + public PlusExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "+"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() + right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/PowExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/PowExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..4e22ae4c5852a87105e32e9ee87f0c17aa9ea73a --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/PowExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class PowExpression extends BinaryOpExpression { + + public PowExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "*"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() * right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..ec4208baf44d9add9bb84714b57e56a81b64d73c --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class SmallerExpression extends BinaryOpExpression { + + public SmallerExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "<"; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() < right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerOrEqualExpression.java b/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerOrEqualExpression.java new file mode 100644 index 0000000000000000000000000000000000000000..3c2895e70e3dffadedb548c8c535570dec80dbc1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/ast/binaryop/SmallerOrEqualExpression.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.ast.binaryop; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BinaryOpExpression; +import com.bedrockk.molang.runtime.MoLangEnvironment; +import com.bedrockk.molang.runtime.MoScope; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; + +public class SmallerOrEqualExpression extends BinaryOpExpression { + + public SmallerOrEqualExpression(Expression left, Expression right) { + super(left, right); + } + + @Override + public String getSigil() { + return "<="; + } + + @Override + public MoValue evaluate(MoScope scope, MoLangEnvironment environment) { + return new DoubleValue(left.evaluate(scope, environment).asDouble() <= right.evaluate(scope, environment).asDouble()); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/InfixParselet.java b/src/main/java/com/bedrockk/molang/parser/InfixParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..1e5d71af067513bd671246f107b4bee08a77970d --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/InfixParselet.java @@ -0,0 +1,13 @@ +package com.bedrockk.molang.parser; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.parser.tokenizer.Token; + +public interface InfixParselet { + + Expression parse(MoLangParser parser, Token token, Expression leftExpr); + + default Precedence getPrecedence() { + return Precedence.ANYTHING; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/MoLangParser.java b/src/main/java/com/bedrockk/molang/parser/MoLangParser.java new file mode 100644 index 0000000000000000000000000000000000000000..83d4ff21f79ba8f4c701660cae27028ced77fc27 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/MoLangParser.java @@ -0,0 +1,210 @@ +package com.bedrockk.molang.parser; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.parser.parselet.*; +import com.bedrockk.molang.parser.tokenizer.Token; +import com.bedrockk.molang.parser.tokenizer.TokenIterator; +import com.bedrockk.molang.parser.tokenizer.TokenType; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class MoLangParser { + + private final static Map<TokenType, PrefixParselet> prefixParselets = new HashMap<>(); + private final static Map<TokenType, InfixParselet> infixParselets = new HashMap<>(); + + private final TokenIterator tokenIterator; + private final List<Token> readTokens = new ArrayList<>(); + + static { + prefixParselets.put(TokenType.NAME, new NameParselet()); + prefixParselets.put(TokenType.STRING, new StringParselet()); + prefixParselets.put(TokenType.NUMBER, new NumberParselet()); + prefixParselets.put(TokenType.TRUE, new BooleanParselet()); + prefixParselets.put(TokenType.FALSE, new BooleanParselet()); + prefixParselets.put(TokenType.RETURN, new ReturnParselet()); + prefixParselets.put(TokenType.CONTINUE, new ContinueParselet()); + prefixParselets.put(TokenType.BREAK, new BreakParselet()); + prefixParselets.put(TokenType.LOOP, new LoopParselet()); + prefixParselets.put(TokenType.FOR_EACH, new ForEachParselet()); + prefixParselets.put(TokenType.THIS, new ThisParselet()); + prefixParselets.put(TokenType.BRACKET_LEFT, new GroupParselet()); + prefixParselets.put(TokenType.CURLY_BRACKET_LEFT, new BracketScopeParselet()); + prefixParselets.put(TokenType.MINUS, new UnaryMinusParselet()); + prefixParselets.put(TokenType.PLUS, new UnaryPlusParselet()); + prefixParselets.put(TokenType.BANG, new BooleanNotParselet()); + + infixParselets.put(TokenType.QUESTION, new TernaryParselet()); + infixParselets.put(TokenType.ARRAY_LEFT, new ArrayAccessParselet()); + infixParselets.put(TokenType.PLUS, new GenericBinaryOpParselet(Precedence.SUM)); + infixParselets.put(TokenType.MINUS, new GenericBinaryOpParselet(Precedence.SUM)); + infixParselets.put(TokenType.SLASH, new GenericBinaryOpParselet(Precedence.PRODUCT)); + infixParselets.put(TokenType.ASTERISK, new GenericBinaryOpParselet(Precedence.PRODUCT)); + infixParselets.put(TokenType.EQUALS, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.NOT_EQUALS, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.GREATER, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.GREATER_OR_EQUALS, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.SMALLER, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.SMALLER_OR_EQUALS, new GenericBinaryOpParselet(Precedence.COMPARE)); + infixParselets.put(TokenType.AND, new GenericBinaryOpParselet(Precedence.AND)); + infixParselets.put(TokenType.OR, new GenericBinaryOpParselet(Precedence.OR)); + infixParselets.put(TokenType.COALESCE, new GenericBinaryOpParselet(Precedence.COALESCE)); + infixParselets.put(TokenType.ARROW, new GenericBinaryOpParselet(Precedence.ARROW)); + infixParselets.put(TokenType.ASSIGN, new AssignParselet()); + } + + public MoLangParser(TokenIterator iterator) { + this.tokenIterator = iterator; + } + + public List<Expression> parse() { + List<Expression> exprs = new ArrayList<>(); + + do { + Expression expr = parseExpression(); + if (expr != null) { + exprs.add(expr); + } else { + break; + } + } while (matchToken(TokenType.SEMICOLON)); + + return exprs; + } + + public Expression parseExpression() { + return parseExpression(Precedence.ANYTHING); + } + + public Expression parseExpression(Precedence precedence) { + Token token = consumeToken(); + + if (token.getType().equals(TokenType.EOF)) { + return null; + } + + PrefixParselet parselet = prefixParselets.get(token.getType()); + + if (parselet == null) { + throw new RuntimeException("Cannot parse " + token.getType().name() + " expression"); + } + + Expression expr = parselet.parse(this, token); + initExpr(expr, token); + + return parseInfixExpression(expr, precedence); + } + + private Expression parseInfixExpression(Expression left, Precedence precedence) { + Token token; + + while (precedence.ordinal() < getPrecedence().ordinal()) { + token = consumeToken(); + left = infixParselets.get(token.getType()).parse(this, token, left); + initExpr(left, token); + } + + return left; + } + + private void initExpr(Expression expression, Token token) { + expression.getAttributes().put("position", token.getPosition()); + } + + private Precedence getPrecedence() { + Token token = readToken(); + + if (token != null) { + InfixParselet parselet = infixParselets.get(token.getType()); + + if (parselet != null) { + return parselet.getPrecedence(); + } + } + + return Precedence.ANYTHING; + } + + public List<Expression> parseArgs() { + List<Expression> args = new ArrayList<>(); + + if (matchToken(TokenType.BRACKET_LEFT)) { + if (!matchToken(TokenType.BRACKET_RIGHT)) { // check for empty groups + do { + args.add(parseExpression()); + } while (matchToken(TokenType.COMMA)); + + consumeToken(TokenType.BRACKET_RIGHT); + } + } + + return args; + } + + public String fixNameShortcut(String name) { + String[] splits = name.split("\\."); + + switch (splits[0]) { + case "q" -> splits[0] = "query"; + case "v" -> splits[0] = "variable"; + case "t" -> splits[0] = "temp"; + case "c" -> splits[0] = "context"; + } + + return String.join(".", splits); + } + + public String getNameHead(String name) { + return name.split("\\.")[0]; + } + + public Token consumeToken() { + return consumeToken(null); + } + + public Token consumeToken(TokenType expectedType) { + tokenIterator.step(); + Token token = readToken(); + + if (expectedType != null) { + if (!token.getType().equals(expectedType)) { + throw new RuntimeException("Expected token " + expectedType.name() + " and " + token.getType().name() + " given"); + } + } + + return readTokens.remove(0); + } + + public boolean matchToken(TokenType expectedType) { + return matchToken(expectedType, true); + } + + public boolean matchToken(TokenType expectedType, boolean consume) { + Token token = readToken(); + + if (token == null || !token.getType().equals(expectedType)) { + return false; + } else { + if (consume) { + consumeToken(); + } + + return true; + } + } + + private Token readToken() { + return readToken(0); + } + + private Token readToken(int distance) { + while (distance >= readTokens.size()) { + readTokens.add(tokenIterator.next()); + } + + return readTokens.get(distance); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/Precedence.java b/src/main/java/com/bedrockk/molang/parser/Precedence.java new file mode 100644 index 0000000000000000000000000000000000000000..bc65ee0edb1075df56cad5afb1295595101ac993 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/Precedence.java @@ -0,0 +1,22 @@ +package com.bedrockk.molang.parser; + +public enum Precedence { + ANYTHING, + SCOPE, + + ASSIGNMENT, + CONDITIONAL, + ARRAY_ACCESS, + + COALESCE, + + AND, + OR, + + COMPARE, + + SUM, + PRODUCT, + PREFIX, + ARROW +} diff --git a/src/main/java/com/bedrockk/molang/parser/PrefixParselet.java b/src/main/java/com/bedrockk/molang/parser/PrefixParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..19cdf54c8c08ca3f7db9a948fb7bef792a83a1f2 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/PrefixParselet.java @@ -0,0 +1,9 @@ +package com.bedrockk.molang.parser; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.parser.tokenizer.Token; + +public interface PrefixParselet { + + Expression parse(MoLangParser parser, Token token); +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/ArrayAccessParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/ArrayAccessParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..ad6fa815e850ae69476b5c75fe56dd2faf70dbe4 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/ArrayAccessParselet.java @@ -0,0 +1,25 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.ArrayAccessExpression; +import com.bedrockk.molang.parser.InfixParselet; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.tokenizer.Token; +import com.bedrockk.molang.parser.tokenizer.TokenType; + +public class ArrayAccessParselet implements InfixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token, Expression leftExpr) { + Expression index = parser.parseExpression(getPrecedence()); + parser.consumeToken(TokenType.ARRAY_RIGHT); + + return new ArrayAccessExpression(leftExpr, index); + } + + @Override + public Precedence getPrecedence() { + return Precedence.ARRAY_ACCESS; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/AssignParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/AssignParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..f8c26e693b0a436ce05700792887914d84365f17 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/AssignParselet.java @@ -0,0 +1,21 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.AssignExpression; +import com.bedrockk.molang.parser.InfixParselet; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class AssignParselet implements InfixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token, Expression leftExpr) { + return new AssignExpression(leftExpr, parser.parseExpression(getPrecedence())); + } + + @Override + public Precedence getPrecedence() { + return Precedence.ASSIGNMENT; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/BooleanNotParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/BooleanNotParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..84087fe716e32d34574d588d6eeea8e7c52015be --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/BooleanNotParselet.java @@ -0,0 +1,16 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BooleanNotExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class BooleanNotParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new BooleanNotExpression(parser.parseExpression(Precedence.PREFIX)); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/BooleanParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/BooleanParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..c4871a770fe980ba101fa13fb05c3852534381d0 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/BooleanParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BooleanExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class BooleanParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new BooleanExpression(Boolean.parseBoolean(token.getText())); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/BracketScopeParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/BracketScopeParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..d88789b6246e83c68750d783b8afc70693218bed --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/BracketScopeParselet.java @@ -0,0 +1,34 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.StatementExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; +import com.bedrockk.molang.parser.tokenizer.TokenType; + +import java.util.ArrayList; +import java.util.List; + +public class BracketScopeParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + List<Expression> exprs = new ArrayList<>(); + + if (!parser.matchToken(TokenType.CURLY_BRACKET_RIGHT)) { + do { + if (parser.matchToken(TokenType.CURLY_BRACKET_RIGHT, false)) { + break; + } + + exprs.add(parser.parseExpression(Precedence.SCOPE)); + } while (parser.matchToken(TokenType.SEMICOLON)); + + parser.consumeToken(TokenType.CURLY_BRACKET_RIGHT); + } + + return new StatementExpression(exprs.toArray(new Expression[0])); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/BreakParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/BreakParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..f9dba1f6412c0a9f1ca9158ab4413cd8ae1ed69a --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/BreakParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.BreakExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class BreakParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new BreakExpression(); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/ContinueParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/ContinueParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..50dd84c0d2906145629c388c1ba68199a141d6f4 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/ContinueParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.ContinueExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class ContinueParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new ContinueExpression(); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/ForEachParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/ForEachParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..d4aae0b5462aeaf73081b41f83ad3536c47ec96c --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/ForEachParselet.java @@ -0,0 +1,23 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.ForEachExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +import java.util.List; + +public class ForEachParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + List<Expression> args = parser.parseArgs(); + + if (args.size() != 3) { + throw new RuntimeException("ForEach: Expected 3 argument, " + args.size() + " argument given"); + } else { + return new ForEachExpression(args.get(0), args.get(1), args.get(2)); + } + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/GenericBinaryOpParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/GenericBinaryOpParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..208032b0d3f8c948f0c7e5511b901b2f56e92751 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/GenericBinaryOpParselet.java @@ -0,0 +1,57 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.binaryop.*; +import com.bedrockk.molang.parser.InfixParselet; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.tokenizer.Token; +import lombok.Value; + +@Value +public class GenericBinaryOpParselet implements InfixParselet { + Precedence precedence; + + @Override + public Expression parse(MoLangParser parser, Token token, Expression leftExpr) { + Expression rightExpr = parser.parseExpression(getPrecedence()); + + switch (token.getType()) { + case ARROW: + return new ArrowExpression(leftExpr, rightExpr); + case AND: + return new BooleanAndExpression(leftExpr, rightExpr); + case OR: + return new BooleanOrExpression(leftExpr, rightExpr); + case COALESCE: + return new CoalesceExpression(leftExpr, rightExpr); + case SLASH: + return new DivideExpression(leftExpr, rightExpr); + case EQUALS: + return new EqualExpression(leftExpr, rightExpr); + case GREATER: + return new GreaterExpression(leftExpr, rightExpr); + case GREATER_OR_EQUALS: + return new GreaterOrEqualExpression(leftExpr, rightExpr); + case MINUS: + return new MinusExpression(leftExpr, rightExpr); + case NOT_EQUALS: + return new NotEqualExpression(leftExpr, rightExpr); + case PLUS: + return new PlusExpression(leftExpr, rightExpr); + case ASTERISK: + return new PowExpression(leftExpr, rightExpr); + case SMALLER: + return new SmallerExpression(leftExpr, rightExpr); + case SMALLER_OR_EQUALS: + return new SmallerOrEqualExpression(leftExpr, rightExpr); + } + + return null; + } + + @Override + public Precedence getPrecedence() { + return precedence; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/GroupParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/GroupParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..46a59d12d4a18427ddfb4ca6e8d0b637175287e5 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/GroupParselet.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; +import com.bedrockk.molang.parser.tokenizer.TokenType; + +public class GroupParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + // this only for conditions + Expression expr = parser.parseExpression(); + parser.consumeToken(TokenType.BRACKET_RIGHT); + + return expr; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/LoopParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/LoopParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..49ea91a96f37f4b19f064c30b17b8aace2e7ef50 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/LoopParselet.java @@ -0,0 +1,23 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.LoopExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +import java.util.List; + +public class LoopParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + List<Expression> args = parser.parseArgs(); + + if (args.size() != 2) { + throw new RuntimeException("Loop: Expected 2 argument, " + args.size() + " argument given"); + } else { + return new LoopExpression(args.get(0), args.get(1)); + } + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/NameParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/NameParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..9369036dba576681dc92462ef385dbec1030e6a6 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/NameParselet.java @@ -0,0 +1,27 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.FuncCallExpression; +import com.bedrockk.molang.ast.NameExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +import java.util.List; + +public class NameParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + List<Expression> args = parser.parseArgs(); + String name = parser.fixNameShortcut(token.getText()); + + Expression nameExpr = new NameExpression(name); + + if (args.size() > 0 || parser.getNameHead(name).equals("query") || parser.getNameHead(name).equals("math")){ + return new FuncCallExpression(nameExpr, args.toArray(new Expression[0])); + } + + return nameExpr; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/NumberParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/NumberParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..7874841acd411c8dc5e9ab1d4846e7f2e8dd8634 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/NumberParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.NumberExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class NumberParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new NumberExpression(Double.parseDouble(token.getText())); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/ReturnParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/ReturnParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..299620cc7ff74518e2ccdef59bb8cfc5c27e06ff --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/ReturnParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.ReturnExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class ReturnParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new ReturnExpression(parser.parseExpression()); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/StringParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/StringParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..a9dd78cca912356bfff2f6a391ed502a3bf8cb14 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/StringParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.StringExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class StringParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new StringExpression(token.getText()); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/TernaryParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/TernaryParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..14c63d17b1a64533bf96ef0998a4cfcd12c698a4 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/TernaryParselet.java @@ -0,0 +1,32 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.TernaryExpression; +import com.bedrockk.molang.parser.InfixParselet; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.tokenizer.Token; +import com.bedrockk.molang.parser.tokenizer.TokenType; + +public class TernaryParselet implements InfixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token, Expression leftExpr) { + if (parser.matchToken(TokenType.COLON)) { + return new TernaryExpression(leftExpr, null, parser.parseExpression(getPrecedence())); + } else { + Expression thenExpr = parser.parseExpression(getPrecedence()); + + if (!parser.matchToken(TokenType.COLON)) { + return new TernaryExpression(leftExpr, thenExpr, null); + } else { + return new TernaryExpression(leftExpr, thenExpr, parser.parseExpression(getPrecedence())); + } + } + } + + @Override + public Precedence getPrecedence() { + return Precedence.CONDITIONAL; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/ThisParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/ThisParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..a8e2f3640f16f340211332bae169e76a8463fb0f --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/ThisParselet.java @@ -0,0 +1,15 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.ThisExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class ThisParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new ThisExpression(); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/UnaryMinusParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/UnaryMinusParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..78fe27c47656b93cd429eb6425b24e816ecbc888 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/UnaryMinusParselet.java @@ -0,0 +1,16 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.UnaryMinusExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class UnaryMinusParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new UnaryMinusExpression(parser.parseExpression(Precedence.PREFIX)); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/parselet/UnaryPlusParselet.java b/src/main/java/com/bedrockk/molang/parser/parselet/UnaryPlusParselet.java new file mode 100644 index 0000000000000000000000000000000000000000..1222cd0462b6c8e2af17485d517255a6b6f563c7 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/parselet/UnaryPlusParselet.java @@ -0,0 +1,16 @@ +package com.bedrockk.molang.parser.parselet; + +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.ast.UnaryPlusExpression; +import com.bedrockk.molang.parser.MoLangParser; +import com.bedrockk.molang.parser.Precedence; +import com.bedrockk.molang.parser.PrefixParselet; +import com.bedrockk.molang.parser.tokenizer.Token; + +public class UnaryPlusParselet implements PrefixParselet { + + @Override + public Expression parse(MoLangParser parser, Token token) { + return new UnaryPlusExpression(parser.parseExpression(Precedence.PREFIX)); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/tokenizer/Token.java b/src/main/java/com/bedrockk/molang/parser/tokenizer/Token.java new file mode 100644 index 0000000000000000000000000000000000000000..de003d4a7f191fabee3a8d0b7ff4254a434fc685 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/tokenizer/Token.java @@ -0,0 +1,18 @@ +package com.bedrockk.molang.parser.tokenizer; + +import lombok.RequiredArgsConstructor; +import lombok.Value; + +@Value +@RequiredArgsConstructor +public class Token { + TokenType type; + String text; + TokenPosition position; + + public Token(TokenType tokenType, TokenPosition position) { + this.type = tokenType; + this.text = tokenType.getSymbol(); + this.position = position; + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenIterator.java b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenIterator.java new file mode 100644 index 0000000000000000000000000000000000000000..9cf0a2ea850e2ced149de6f768a7c2b9752cfacd --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenIterator.java @@ -0,0 +1,104 @@ +package com.bedrockk.molang.parser.tokenizer; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class TokenIterator { + + private final String code; + + private int index = 0; + private int currentLine = 0; + private int lastStep = 0; + private int lastStepLine = 0; + + public boolean hasNext() { + return index < code.length(); + } + + public Token next() { + while (index < code.length()) { + if (code.length() > index + 1) { // check tokens with double chars + TokenType token = TokenType.bySymbol(code.substring(index, index + 2)); + + if (token != null) { + index += 2; + return new Token(token, getPosition()); + } + } + + String expr = getStringAt(index); + TokenType tokenType = TokenType.bySymbol(expr); + if (tokenType != null) { + index++; + return new Token(tokenType, getPosition()); + } else if (expr.equals("'")) { + int stringStart = index; + int stringLength = index + 1; + + while (stringLength < code.length() && !getStringAt(stringLength).equals("'")) { + stringLength++; + } + + stringLength++; + index = stringLength; + + return new Token(TokenType.STRING, code.substring(stringStart + 1, stringLength - 1), getPosition()); + } else if (Character.isLetter(expr.charAt(0))) { + int nameLength = index + 1; + + while (nameLength < code.length() && (Character.isLetterOrDigit(getStringAt(nameLength).charAt(0)) || getStringAt(nameLength).equals("_") || getStringAt(nameLength).equals("."))) { + nameLength++; + } + + String value = code.substring(index, nameLength).toLowerCase(); + TokenType token = TokenType.bySymbol(value); + + if (token == null) { + token = TokenType.NAME; + } + + index = nameLength; + return new Token(token, value, getPosition()); + } else if (Character.isDigit(expr.charAt(0))) { + int numStart = index; + int numLength = index + 1; + boolean hasDecimal = false; + + while (numLength < code.length() && (Character.isDigit(getStringAt(numLength).charAt(0)) || (getStringAt(numLength).equals(".") && !hasDecimal))) { + if (getStringAt(numLength).equals(".")) { + hasDecimal = true; + } + numLength++; + } + + index = numLength; + + return new Token(TokenType.NUMBER, code.substring(numStart, numLength), getPosition()); + } else if (expr.equals("\n") || expr.equals("\r")) { + currentLine++; + } + + index++; + } + + return new Token(TokenType.EOF, getPosition()); + } + + public void step() { + lastStep = index; + lastStepLine = currentLine; + } + + public TokenPosition getPosition() { + return new TokenPosition(lastStepLine, currentLine, lastStep, index); + } + + public String getStringAt(String str, int i) { + return str.substring(i, i + 1); + } + + public String getStringAt(int i) { + return code.substring(i, i + 1); + } +} diff --git a/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenPosition.java b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenPosition.java new file mode 100644 index 0000000000000000000000000000000000000000..b1111b1689afe846f00aaa50a9601a6be53c7559 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenPosition.java @@ -0,0 +1,12 @@ +package com.bedrockk.molang.parser.tokenizer; + +import lombok.Value; + +@Value +public class TokenPosition { + + int startLineNumber; + int endLineNumber; + int startColumn; + int endColumn; +} diff --git a/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenType.java b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenType.java new file mode 100644 index 0000000000000000000000000000000000000000..88fa9c4132b6d4bf9bee00616618a0c2548680b1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/parser/tokenizer/TokenType.java @@ -0,0 +1,64 @@ +package com.bedrockk.molang.parser.tokenizer; + +public enum TokenType { + EQUALS("=="), + NOT_EQUALS("!="), + COALESCE("??"), + AND("&&"), + OR("||"), + GREATER_OR_EQUALS(">="), + SMALLER_OR_EQUALS("<="), + ARROW("->"), + + GREATER(">"), + SMALLER("<"), + BRACKET_LEFT("("), + BRACKET_RIGHT(")"), + ARRAY_LEFT("["), + ARRAY_RIGHT("]"), + CURLY_BRACKET_LEFT("{"), + CURLY_BRACKET_RIGHT("}"), + COMMA(","), + ASSIGN("="), + PLUS("+"), + MINUS("-"), + ASTERISK("*"), + SLASH("/"), + QUESTION("?"), + COLON(":"), + SEMICOLON(";"), + BANG("!"), + + RETURN("return"), + CONTINUE("continue"), + BREAK("break"), + FOR_EACH("for_each"), + LOOP("loop"), + THIS("this"), + TRUE("true"), + FALSE("false"), + STRING(""), + NUMBER(""), + NAME(""), + EOF(""); + + public final String symbol; + + TokenType(String symbol) { + this.symbol = symbol; + } + + public String getSymbol() { + return symbol; + } + + public static TokenType bySymbol(String symbol) { + for (TokenType tokenType : TokenType.values()) { + if (tokenType.getSymbol().equals(symbol)) { + return tokenType; + } + } + + return null; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/MoLangEnvironment.java b/src/main/java/com/bedrockk/molang/runtime/MoLangEnvironment.java new file mode 100644 index 0000000000000000000000000000000000000000..06b2a68f82de64c72e126d0b035c29d65e66e6cb --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/MoLangEnvironment.java @@ -0,0 +1,46 @@ +package com.bedrockk.molang.runtime; + +import com.bedrockk.molang.runtime.struct.MoStruct; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Value; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +@Value +public class MoLangEnvironment implements MoValue { + + ConcurrentMap<String, MoStruct> structs = new ConcurrentHashMap<>(); + + public MoValue getValue(String name) { + return getValue(name, MoParams.EMPTY); + } + + public MoValue getValue(String name, MoParams params) { + LinkedList<String> segments = new LinkedList<>(Arrays.asList(name.split("\\."))); + String main = segments.poll(); + + if (structs.containsKey(main)) { + return structs.get(main).get(String.join(".", segments), params); + } + + return new DoubleValue(0.0); + } + + public void setValue(String name, MoValue value) { + LinkedList<String> segments = new LinkedList<>(Arrays.asList(name.split("\\."))); + String main = segments.poll(); + + if (structs.containsKey(main)) { + structs.get(main).set(String.join(".", segments), value); + } + } + + @Override + public Object value() { + return this; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/MoLangMath.java b/src/main/java/com/bedrockk/molang/runtime/MoLangMath.java new file mode 100644 index 0000000000000000000000000000000000000000..38573d6f42aca9952e004fbcebe12ba53dbe4ae9 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/MoLangMath.java @@ -0,0 +1,96 @@ +package com.bedrockk.molang.runtime; + +import com.bedrockk.molang.runtime.struct.StaticQueryStruct; + +import java.util.HashMap; + +public final class MoLangMath { + + public static final float DEG2RAD = 0.0174532f; + public static final StaticQueryStruct LIBRARY = new StaticQueryStruct(new HashMap<>() { + { + put("abs", params -> Math.abs(params.getDouble(0))); + put("acos", params -> Math.acos(params.getDouble(0))); + put("sin", params -> Math.sin(params.getDouble(0) * DEG2RAD)); + put("asin", params -> Math.asin(params.getDouble(0))); + put("atan", params -> Math.atan(params.getDouble(0))); + put("atan2", params -> Math.atan2(params.getDouble(0), params.getDouble(1))); + put("ceil", params -> Math.ceil(params.getDouble(0))); + put("clamp", params -> Math.min(params.getDouble(2), Math.max(params.getDouble(0), params.getDouble(1)))); + put("cos", params -> Math.cos(params.getDouble(0) * DEG2RAD)); + put("die_roll", params -> dieRoll(params.getDouble(0), params.getDouble(1), params.getDouble(2))); + put("die_roll_integer", params -> dieRollInt(params.getInt(0), params.getInt(1), params.getInt(2))); + put("exp", params -> Math.exp(params.getDouble(0))); + put("mod", params -> params.getDouble(0) % params.getDouble(1)); + put("floor", params -> Math.floor(params.getDouble(0))); + put("hermite_blend", params -> hermiteBlend(params.getInt(0))); + put("lerp", params -> lerp(params.getDouble(0), params.getDouble(1), params.getDouble(2))); + put("lerp_rotate", params -> lerpRotate(params.getDouble(0), params.getDouble(1), params.getDouble(2))); + put("ln", params -> Math.log(params.getDouble(0))); + put("max", params -> Math.max(params.getDouble(0), params.getDouble(1))); + put("min", params -> Math.min(params.getDouble(0), params.getDouble(1))); + put("pi", params -> Math.PI); + put("pow", params -> Math.pow(params.getDouble(0), params.getDouble(1))); + put("random", params -> random(params.getDouble(0), params.getDouble(1))); + put("random_integer", params -> randomInt(params.getInt(0), params.getInt(1))); + put("round", params -> Math.round(params.getDouble(0))); + put("sqrt", params -> Math.sqrt(params.getDouble(0))); + put("trunc", params -> Math.floor(params.getDouble(0))); + } + }); + + public static double random(double low, double high) { + return low + Math.random() * (high - low); + } + + public static int randomInt(int low, int high) { + return (int) Math.round(low + Math.random() * (high - low)); + } + + public static double dieRoll(double num, double low, double high) { + int i = 0; + int total = 0; + while (i++ < num) total += random(low, high); + + return total; + } + + public static int dieRollInt(int num, int low, int high) { + int i = 0; + int total = 0; + while (i++ < num) total += randomInt(low, high); + + return total; + } + + public static int hermiteBlend(int value) { + return (3 * value) ^ (2 - 2 * value) ^ 3; + } + + public static double lerp(double start, double end, double amount) { + amount = Math.max(0, Math.min(1, amount)); + + return start + (end - start) * amount; + } + + public static double lerpRotate(double start, double end, double amount) { + start = radify(start); + end = radify(end); + + if (start > end) { + double tmp = start; + start = end; + end = tmp; + } + + if (end - start > 180) { + return radify(end + amount * (360 - (end - start))); + } + + return start + amount * (end - start); + } + + public static double radify(double num) { + return (((num + 180) % 360) + 180) % 360; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/MoLangRuntime.java b/src/main/java/com/bedrockk/molang/runtime/MoLangRuntime.java new file mode 100644 index 0000000000000000000000000000000000000000..c5766ed18642324a43ff5407b90ffe066c99c351 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/MoLangRuntime.java @@ -0,0 +1,57 @@ +package com.bedrockk.molang.runtime; + +import com.bedrockk.molang.ExprTraverser; +import com.bedrockk.molang.Expression; +import com.bedrockk.molang.runtime.struct.ArrayStruct; +import com.bedrockk.molang.runtime.struct.ContextStruct; +import com.bedrockk.molang.runtime.struct.VariableStruct; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import com.bedrockk.molang.visitor.ExprConnectingVisitor; + +import java.util.*; + +public class MoLangRuntime { + private final MoLangEnvironment environment = new MoLangEnvironment(); + + public MoLangRuntime() { + environment.getStructs().put("math", MoLangMath.LIBRARY); + environment.getStructs().put("temp", new VariableStruct()); + environment.getStructs().put("variable", new VariableStruct()); + environment.getStructs().put("array", new ArrayStruct()); + } + + public MoValue execute(Expression expression) { + return execute(Collections.singletonList(expression)); + } + + public MoValue execute(List<Expression> expressions) { + return execute(expressions, new HashMap<>()); + } + + public MoValue execute(List<Expression> expressions, Map<String, MoValue> context) { + var traverser = new ExprTraverser(); + traverser.getVisitors().add(new ExprConnectingVisitor()); + traverser.traverse(expressions); + + environment.getStructs().put("context", new ContextStruct(context)); + + MoValue result = DoubleValue.ZERO; + var scope = new MoScope(); + for (Expression expression : new ArrayList<>(expressions)) { + if (scope.getReturnValue() != null) { + break; + } + result = expression.evaluate(scope, environment); + } + + environment.getStructs().get("temp").clear(); + environment.getStructs().remove("context"); + + return scope.getReturnValue() != null ? scope.getReturnValue() : result; + } + + public MoLangEnvironment getEnvironment() { + return environment; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/MoParams.java b/src/main/java/com/bedrockk/molang/runtime/MoParams.java new file mode 100644 index 0000000000000000000000000000000000000000..b190b8e56ddeed042e314dde1ae345402dfb56f8 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/MoParams.java @@ -0,0 +1,53 @@ +package com.bedrockk.molang.runtime; + +import com.bedrockk.molang.runtime.struct.MoStruct; +import com.bedrockk.molang.runtime.value.DoubleValue; +import com.bedrockk.molang.runtime.value.MoValue; +import com.bedrockk.molang.runtime.value.StringValue; + +import java.util.ArrayList; +import java.util.List; + +public class MoParams { + + public static final MoParams EMPTY = new MoParams(new ArrayList<>()); + + private final List<MoValue> params; + + public MoParams(List<MoValue> params) { + this.params = params; + } + + @SuppressWarnings("unchecked") + public <T extends MoValue> T get(int index) { + return (T) params.get(index); + } + + public boolean contains(int index) { + return params.size() >= index + 1; + } + + public int getInt(int index) { + return (int) getDouble(index); + } + + public double getDouble(int index) { + return this.<DoubleValue>get(index).asDouble(); + } + + public MoStruct getStruct(int index) { + return get(index); + } + + public String getString(int index) { + return this.<StringValue>get(index).asString(); + } + + public MoLangEnvironment getEnv(int index) { + return get(index); + } + + public List<MoValue> getParams() { + return params; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/MoScope.java b/src/main/java/com/bedrockk/molang/runtime/MoScope.java new file mode 100644 index 0000000000000000000000000000000000000000..95f422522cd2a57852abfdfc4b29b2d660a1d01e --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/MoScope.java @@ -0,0 +1,13 @@ +package com.bedrockk.molang.runtime; + +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class MoScope { + private boolean isBreak = false; + private boolean isContinue = false; + private MoValue returnValue; +} \ No newline at end of file diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/ArrayStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/ArrayStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..30677f07650d1fb70f7c6bac6146cdf20919c494 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/ArrayStruct.java @@ -0,0 +1,23 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.value.MoValue; + +import java.util.Map; + +public class ArrayStruct extends VariableStruct { + + public ArrayStruct() {} + + public ArrayStruct(Map<String, MoValue> map) { + super(map); + } + + @Override + public void set(String name, MoValue value) { + // Last part always must be a integer + String[] parts = name.split("\\."); + parts[parts.length - 1] = String.valueOf(Integer.parseInt(parts[parts.length - 1])); + + super.set(String.join(".", parts), value); + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/ContextStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/ContextStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..3c71fd6556e2caed79121105a8c5a6934772fcfa --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/ContextStruct.java @@ -0,0 +1,19 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.value.MoValue; + +import java.util.Map; + +public class ContextStruct extends VariableStruct { + + public ContextStruct() {} + + public ContextStruct(Map<String, MoValue> map) { + super(map); + } + + @Override + public void set(String name, MoValue value) { + throw new RuntimeException("Tried to set a value in read-only context struct"); + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/MoStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/MoStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..f05cbca9c60f4e07038528b7a1c67701029d4cbb --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/MoStruct.java @@ -0,0 +1,18 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.MoParams; +import com.bedrockk.molang.runtime.value.MoValue; + +public interface MoStruct extends MoValue { + + void set(String key, MoValue value); + + MoValue get(String key, MoParams params); + + void clear(); + + @Override + default MoStruct value() { + return this; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/QueryStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/QueryStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..4e54e3c6bbc9286c34ba7244aa68891165709d26 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/QueryStruct.java @@ -0,0 +1,37 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.MoParams; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.Map; +import java.util.function.BiFunction; + +@Getter +@RequiredArgsConstructor +public class QueryStruct<T> implements MoStruct { + + private final T target; + private final Map<String, BiFunction<MoParams, T, Object>> functions; + + @Override + public void set(String key, MoValue value) { + throw new RuntimeException("Cannot set a value in query struct"); + } + + @Override + public MoValue get(String key, MoParams params) { + if (functions.containsKey(key)) { + return MoValue.of(functions.get(key).apply(params, target)); + } + + return null; + } + + @Override + public void clear() { + functions.clear(); + } + +} diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/StaticQueryStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/StaticQueryStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..8ed555f863728e60e6d3bd97b40fa759e6e76175 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/StaticQueryStruct.java @@ -0,0 +1,35 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.MoParams; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.Map; +import java.util.function.Function; + +@Getter +@RequiredArgsConstructor +public class StaticQueryStruct implements MoStruct { + + private final Map<String, Function<MoParams, Object>> functions; + + @Override + public MoValue get(String key, MoParams params) { + if (functions.containsKey(key)) { + return MoValue.of(functions.get(key).apply(params)); + } + + return null; + } + + @Override + public void set(String name, MoValue value) { + throw new RuntimeException("Cannot set a value in query struct"); + } + + @Override + public void clear() { + functions.clear(); + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/struct/VariableStruct.java b/src/main/java/com/bedrockk/molang/runtime/struct/VariableStruct.java new file mode 100644 index 0000000000000000000000000000000000000000..fabd422497e68d3c4a1d22661dcf36f2e692cdf1 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/struct/VariableStruct.java @@ -0,0 +1,62 @@ +package com.bedrockk.molang.runtime.struct; + +import com.bedrockk.molang.runtime.MoParams; +import com.bedrockk.molang.runtime.value.MoValue; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +@Getter +@RequiredArgsConstructor +public class VariableStruct implements MoStruct { + private final Map<String, MoValue> map; + + public VariableStruct() { + this.map = new HashMap<>(); + } + + @Override + public void set(String name, MoValue value) { + LinkedList<String> segments = new LinkedList<>(Arrays.asList(name.split("\\."))); + String main = segments.poll(); + + if (segments.size() > 0 && main != null) { + Object struct = get(main, MoParams.EMPTY); + + if (!(struct instanceof MoStruct)) { + struct = new VariableStruct(); + } + + ((MoStruct) struct).set(segments.poll(), value); + + map.put(main, (MoStruct) struct); + } else { + map.put(name, value); + } + } + + @Override + public MoValue get(String name, MoParams params) { + LinkedList<String> segments = new LinkedList<>(Arrays.asList(name.split("\\."))); + String main = segments.poll(); + + if (segments.size() > 0 && main != null) { + Object struct = map.get(main); + + if (struct instanceof MoStruct) { + return ((MoStruct) struct).get(segments.poll(), MoParams.EMPTY); + } + } + + return map.get(name); + } + + @Override + public void clear() { + map.clear(); + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/value/DoubleValue.java b/src/main/java/com/bedrockk/molang/runtime/value/DoubleValue.java new file mode 100644 index 0000000000000000000000000000000000000000..b92dd8707e7057221920c3031d683570f686993d --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/value/DoubleValue.java @@ -0,0 +1,34 @@ +package com.bedrockk.molang.runtime.value; + +public class DoubleValue implements MoValue { + + public final static DoubleValue ZERO = new DoubleValue(0.0); + public final static DoubleValue ONE = new DoubleValue(1.0); + + private final double value; + + public DoubleValue(Object value) { + if (value instanceof Boolean) { + this.value = (boolean) value ? 1.0 : 0.0; + } else if (value instanceof Number) { + this.value = (double) value; + } else { + this.value = 1.0; + } + } + + @Override + public Double value() { + return value; + } + + @Override + public String asString() { + return Double.toString(value); + } + + @Override + public double asDouble() { + return value; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/value/MoValue.java b/src/main/java/com/bedrockk/molang/runtime/value/MoValue.java new file mode 100644 index 0000000000000000000000000000000000000000..6d78d899eeb27f7822de572a5524789564c5d814 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/value/MoValue.java @@ -0,0 +1,22 @@ +package com.bedrockk.molang.runtime.value; + +public interface MoValue { + + static MoValue of(Object value) { + if (value instanceof MoValue) { + return (MoValue) value; + } else { + return new DoubleValue(value); + } + } + + Object value(); + + default String asString() { + return this.toString(); + } + + default double asDouble() { + return 1.0; + } +} diff --git a/src/main/java/com/bedrockk/molang/runtime/value/StringValue.java b/src/main/java/com/bedrockk/molang/runtime/value/StringValue.java new file mode 100644 index 0000000000000000000000000000000000000000..40510312adb6b2fc9a89c57d5d79c152e7deebe5 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/runtime/value/StringValue.java @@ -0,0 +1,20 @@ +package com.bedrockk.molang.runtime.value; + +public class StringValue implements MoValue { + + private final String value; + + public StringValue(String value) { + this.value = value; + } + + @Override + public String value() { + return value; + } + + @Override + public String asString() { + return value; + } +} diff --git a/src/main/java/com/bedrockk/molang/utils/ExprUtils.java b/src/main/java/com/bedrockk/molang/utils/ExprUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..19f0550221f6a7f3f2a4012f7345b088ef623889 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/utils/ExprUtils.java @@ -0,0 +1,28 @@ +package com.bedrockk.molang.utils; + +import com.bedrockk.molang.Expression; + +public final class ExprUtils { + + public static Expression getExprAttribute(Expression expression, String attributeName) { + Object parent = expression.getAttributes().get(attributeName); + + if (parent instanceof Expression) { + return (Expression) parent; + } + + return null; + } + + public static Expression parent(Expression expression) { + return getExprAttribute(expression, "parent"); + } + + public static Expression next(Expression expression) { + return getExprAttribute(expression, "next"); + } + + public static Expression previous(Expression expression) { + return getExprAttribute(expression, "previous"); + } +} diff --git a/src/main/java/com/bedrockk/molang/utils/FileUtils.java b/src/main/java/com/bedrockk/molang/utils/FileUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..d90d270766a25e90776f21178adddb5baccd9519 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/utils/FileUtils.java @@ -0,0 +1,28 @@ +package com.bedrockk.molang.utils; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class FileUtils { + + public static String readFile(InputStream inputStream) throws IOException { + return readFile(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + } + + private static String readFile(Reader reader) throws IOException { + try (BufferedReader br = new BufferedReader(reader)) { + String temp = br.readLine(); + StringBuilder stringBuilder = new StringBuilder(); + + while (temp != null) { + if (stringBuilder.length() != 0) { + stringBuilder.append("\n"); + } + stringBuilder.append(temp); + temp = br.readLine(); + } + + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/com/bedrockk/molang/visitor/ExprConnectingVisitor.java b/src/main/java/com/bedrockk/molang/visitor/ExprConnectingVisitor.java new file mode 100644 index 0000000000000000000000000000000000000000..41bb3f0f490efb21e9a44278b1b12edb8906e080 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/visitor/ExprConnectingVisitor.java @@ -0,0 +1,43 @@ +package com.bedrockk.molang.visitor; + +import com.bedrockk.molang.ExprVisitor; +import com.bedrockk.molang.Expression; +import lombok.RequiredArgsConstructor; + +import java.util.LinkedList; +import java.util.List; + +@RequiredArgsConstructor +public class ExprConnectingVisitor implements ExprVisitor { + + private final LinkedList<Expression> stack = new LinkedList<>(); + private Expression previous; + + @Override + public void beforeTraverse(List<Expression> expressions) { + stack.clear(); + previous = null; + } + + @Override + public Object onVisit(Expression expression) { + if (!stack.isEmpty()) { + expression.getAttributes().put("parent", stack.getLast()); + } + + if (previous != null && expression.getAttributes().get("parent") == previous.getAttributes().get("parent")) { + expression.getAttributes().put("previous", previous); + previous.getAttributes().put("next", expression); + } + + stack.add(expression); + + return null; + } + + @Override + public void onLeave(Expression expression) { + previous = expression; + stack.pollLast(); + } +} diff --git a/src/main/java/com/bedrockk/molang/visitor/FindingVisitor.java b/src/main/java/com/bedrockk/molang/visitor/FindingVisitor.java new file mode 100644 index 0000000000000000000000000000000000000000..5ae6e530b7afa4419d3e3b48138ffae4feb20636 --- /dev/null +++ b/src/main/java/com/bedrockk/molang/visitor/FindingVisitor.java @@ -0,0 +1,29 @@ +package com.bedrockk.molang.visitor; + +import com.bedrockk.molang.ExprVisitor; +import com.bedrockk.molang.Expression; +import lombok.RequiredArgsConstructor; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +@RequiredArgsConstructor +public class FindingVisitor implements ExprVisitor { + + private final Predicate<Expression> predicate; + private final List<Expression> foundExpressions = new ArrayList<>(); + + @Override + public Object onVisit(Expression expression) { + if (predicate.test(expression)) { + foundExpressions.add(expression); + } + + return null; + } + + public List<Expression> getFoundExpressions() { + return foundExpressions; + } +} diff --git a/src/main/java/com/bedrockk/molang/visitor/FirstFindingVisitor.java b/src/main/java/com/bedrockk/molang/visitor/FirstFindingVisitor.java new file mode 100644 index 0000000000000000000000000000000000000000..8f75f633f2eefeafdb8abeafc75547d13144db0a --- /dev/null +++ b/src/main/java/com/bedrockk/molang/visitor/FirstFindingVisitor.java @@ -0,0 +1,30 @@ +package com.bedrockk.molang.visitor; + +import com.bedrockk.molang.ExprTraverser; +import com.bedrockk.molang.ExprVisitor; +import com.bedrockk.molang.Expression; +import lombok.RequiredArgsConstructor; + +import java.util.function.Predicate; + +@RequiredArgsConstructor +public class FirstFindingVisitor implements ExprVisitor { + + private final Predicate<Expression> predicate; + private Expression found; + + @Override + public Object onVisit(Expression expression) { + if (predicate.test(expression)) { + found = expression; + + return ExprTraverser.ActionType.STOP_TRAVERSAL; + } + + return null; + } + + public Expression getFound() { + return found; + } +} diff --git a/src/main/java/com/ticxo/megml/MegML.java b/src/main/java/com/ticxo/megml/MegML.java index 32ca70b8f488d9c9b4a1280c9df13ec53c1b6d79..9edb1d7ef246358e91c52c0868f753035e5ec84f 100644 --- a/src/main/java/com/ticxo/megml/MegML.java +++ b/src/main/java/com/ticxo/megml/MegML.java @@ -23,7 +23,7 @@ public class MegML extends JavaPlugin { @Override public void onDisable() { - + manager.cancel(); } public static IKeyframeData parse(String data) { diff --git a/target/classes/plugin.yml b/target/classes/plugin.yml deleted file mode 100644 index 904bca2901098ba03e419da661cf59931bc6de62..0000000000000000000000000000000000000000 --- a/target/classes/plugin.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: ModelEngine-MoLang -author: Ticxo -main: com.ticxo.megml.MegML -version: R1.0.0 -api-version: 1.16 -depend: - - ModelEngine \ No newline at end of file diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties deleted file mode 100644 index 1546b4e8728fe49f0fe31fc68378d7af9b4d0d88..0000000000000000000000000000000000000000 --- a/target/maven-archiver/pom.properties +++ /dev/null @@ -1,5 +0,0 @@ -#Generated by Maven -#Wed Dec 07 01:24:15 PST 2022 -groupId=com.ticxo.megmolang -artifactId=Meg-Molang -version=R1.0.0 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst deleted file mode 100644 index d3776a7b8636cd7d9404dfb47bdadf5ab839486a..0000000000000000000000000000000000000000 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ /dev/null @@ -1,5 +0,0 @@ -com\ticxo\megml\MegML.class -com\ticxo\megml\ModelQuery.class -com\ticxo\megml\ModelQuery$1.class -com\ticxo\megml\QueryData.class -com\ticxo\megml\RuntimeManager.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst deleted file mode 100644 index 7e845fbfbb6859b93f0e3007490f96420a685fcd..0000000000000000000000000000000000000000 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ /dev/null @@ -1,4 +0,0 @@ -C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\RuntimeManager.java -C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\QueryData.java -C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\ModelQuery.java -C:\Users\shawn\IdeaProjects\MegMolang\src\main\java\com\ticxo\megml\MegML.java diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000