commit cff8089a515c186343466a7bc7c8c3421781ab72 Author: Michael Nolan Date: Mon Apr 24 13:41:46 2023 -0500 Inital Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77fef04 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +libtlang/build +libtlang/build_wii +libtlang/libtlang.a +libtlang/boot.dol +libtlang/boot.elf +tlang/build +tlang/tlang +tlang/my_junk_scripts +wrappergen/dest \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..81c3846 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/opt/devkitpro/libogc/include/**", + "/opt/devkitpro/portlibs/wii/include/**", + "/opt/devkitpro/portlibs/ppc/include/**", + "/usr/include/SDL2", + "/opt/devkitpro/libogc/include" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..453ff67 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,56 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch With Program", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/tlang/tlang", + "args": ["${workspaceFolder}/debugging/tlang_wrapper_gen.tlang","${workspaceFolder}/debugging/src.txt","${workspaceFolder}/debugging/dest"], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + }, + { + "name": "(gdb) Launch Shell", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/tlang/tlang", + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a01ac92 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,14 @@ +{ + "files.associations": { + "stdatomic.h": "c", + "tlang.h": "c", + "stdbool.h": "c", + "vector": "c", + "stdint.h": "c", + "stdio.h": "c", + "dirent.h": "c", + "stdlib.h": "c", + "poll.h": "c", + "sdl.h": "c" + } +} \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..01a7a80 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,675 @@ +### GNU GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +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. + + + Copyright (C) + + 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 . + +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: + + Copyright (C) + 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 . + +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 . \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/examples/copyfile.tlang b/examples/copyfile.tlang new file mode 100644 index 0000000..55a3953 --- /dev/null +++ b/examples/copyfile.tlang @@ -0,0 +1,23 @@ +console.write("Type Source: "); +a = console.readln(); +console.write("Type New Path For File With Filename: "); +b = console.readln(); + +src=fs.openread(a); +if(src) +{ + dest = fs.create(b); + if(dest) + { + array = create.array(1024); + read = 0; + do(read > 0) + { + read=src.read(array,array.length); + dest.write(array,read); + } + dest.close(); + } + src.close(); +} +console.writeln("Copied " + a + " => " + b); \ No newline at end of file diff --git a/examples/cppcout.tlang b/examples/cppcout.tlang new file mode 100644 index 0000000..7501f3f --- /dev/null +++ b/examples/cppcout.tlang @@ -0,0 +1,8 @@ +cout = create.dict(); +cout.ls = func(str) { + console.write(str); + cout; +} + +cout << "Hello World\n"; + diff --git a/examples/ls.tlang b/examples/ls.tlang new file mode 100644 index 0000000..b616a16 --- /dev/null +++ b/examples/ls.tlang @@ -0,0 +1,10 @@ +console.write("Please Type A Path: "); +dir = console.readln(); +each(_dir : fs.enumerate_dirs(dir)) +{ + console.writeln("[DIR] " + _dir); +} +each(file : fs.enumerate_files(dir)) +{ + console.writeln("[FILE] " + file); +} \ No newline at end of file diff --git a/examples/optional.tlang b/examples/optional.tlang new file mode 100644 index 0000000..0e44299 --- /dev/null +++ b/examples/optional.tlang @@ -0,0 +1,15 @@ +//same behaviour as math.pow except implemented in this language (the math.pow uses math.h pow) +func pow(base,$exp) +{ + if(exp == undefined) + { + exp = 2; + } + num = 1; + for(i = 0;idevkitPPC") +endif + +include $(DEVKITPPC)/wii_rules + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# INCLUDES is a list of directories containing extra header files +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build_wii +SOURCES := src +DATA := data +INCLUDES := include + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- + +CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE) +CXXFLAGS = $(CFLAGS) + +LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map + +#--------------------------------------------------------------------------------- +# any extra libraries we wish to link with the project +#--------------------------------------------------------------------------------- +LIBS := -lfat -lwiiuse -lbte -logc -lm + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +export DEPSDIR := $(CURDIR)/$(BUILD) + +#--------------------------------------------------------------------------------- +# automatically build a list of object files for our project +#--------------------------------------------------------------------------------- +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) + export LD := $(CC) +else + export LD := $(CXX) +endif + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) + +export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +#--------------------------------------------------------------------------------- +# build a list of include paths +#--------------------------------------------------------------------------------- +export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) \ + -I$(LIBOGC_INC) + +#--------------------------------------------------------------------------------- +# build a list of library paths +#--------------------------------------------------------------------------------- +export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +export OUTPUT := $(CURDIR)/$(TARGET) +.PHONY: $(BUILD) clean + +#--------------------------------------------------------------------------------- +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile.wii + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol + +#--------------------------------------------------------------------------------- +run: + wiiload $(TARGET).dol + + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT).dol: $(OUTPUT).elf +$(OUTPUT).elf: $(OFILES) + +$(OFILES_SOURCES) : $(HFILES) + +#--------------------------------------------------------------------------------- +# This rule links in binary data with the .jpg extension +#--------------------------------------------------------------------------------- +%.jpg.o %_jpg.h : %.jpg +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + $(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- \ No newline at end of file diff --git a/libtlang/include/myfeatures.h b/libtlang/include/myfeatures.h new file mode 100644 index 0000000..ff0d56c --- /dev/null +++ b/libtlang/include/myfeatures.h @@ -0,0 +1,6 @@ +#define USE_SDL2 +#define USE_NETWORK +#define USE_WIISOCKETS_ON_WII //comment out for #include +#define USE_THREADS +#define USE_MBED +//#define USE_GTK \ No newline at end of file diff --git a/libtlang/include/tlang.h b/libtlang/include/tlang.h new file mode 100644 index 0000000..cafea59 --- /dev/null +++ b/libtlang/include/tlang.h @@ -0,0 +1,447 @@ +#pragma once +#include "myfeatures.h" +#include +#include +#include +#include +#include +#include +typedef enum { + NONEXIST=0, + REGULAR = 0b0001, + FOLDER = 0b0010, + SYM = 0b0100, + CHARDEV = 0b01000, + BLOCKDEV = 0b10000, + DEV = CHARDEV | BLOCKDEV, + FIFO = 0b0100000, + OTHER = 0b1000000, + NORMAL = REGULAR | FOLDER, + SPECIAL = SYM | DEV | FIFO | OTHER, + + ANY = NORMAL | SPECIAL +} fs_entry_filter_t; + +typedef struct stream stream_t; +typedef struct ittr ittr_t; +typedef struct scope scope_t; +typedef struct node node_t; +typedef struct lextoken lextoken_t; +typedef struct lextokenlist lextokenlist_t; +typedef struct runtime runtime_t; +typedef void (*action_t)(runtime_t*); +typedef struct list_tobject list_tobject_t; +typedef struct list_string list_string_t; +typedef struct dictinary dict_t; +typedef struct runtime runtime_t; +typedef struct tobject tobject_t; +typedef struct string string_t; +struct string { + char* text; + int capacity; + int length; + }; + +typedef tobject_t* (*node_exec_t)(node_t*,scope_t*); +typedef void (*node_set_t)(node_t*,tobject_t*,scope_t*); +typedef tobject_t* (*external_method_t)(runtime_t*,void* ptr,list_tobject_t* objs); +typedef enum {tnull,tundef,tdict,texternalmethod,tinternalmethod,tlist,tstring,tnumber,tbool,tchar} tobject_type_t; + +typedef void (*texternalmethod_free_t)(tobject_t*); +typedef scope_t* (*get_scope_t)(scope_t*); +typedef void (*runtime_reg_t)(runtime_t* rt); +typedef tobject_t* (*get_variable_t)(scope_t*,char*); +typedef void (*set_variable_t)(scope_t*,char*,tobject_t*); +typedef bool (*has_variable_t)(scope_t*,char*); +typedef void (*ittr_reset_t)(ittr_t*); +typedef tobject_t* (*ittr_getcurrent_t)(ittr_t*); +typedef bool (*ittr_movenext_t)(ittr_t*); +void list_set(list_tobject_t* a,int index,tobject_t* item); +node_t* node_negative_create(node_t* left); +node_t* node_not_create(node_t* left); +node_t* node_const_object_create(tobject_t*); +node_t* node_postfix_decrement_create(node_t* left); +node_t* node_postfix_increment_create(node_t* left); +typedef enum {postfixincrementnode,postfixdecrementnode,methodcallnode,bnotnode,negnode,notnode,notequalsnode,equalsnode,whileloopnode,eachloopnode,forloopnode,ifnode,getarraynode,logicalornode,logicalandnode,xornode,closurenode,scopenode,bitwiseornode,bitwiseandnode,addnode,lessthannode,lessthanequalnode,greaterthannode,greaterthanequalnode,subnode,multiplynode,dividenode,modulonode,leftshiftnode,rightshiftnode,constobjectnode,constnumbernode,constnullnode,constundefnode,conststringnode,constboolnode,constcharnode,functioncallnode,setvariablenode,getvariablenode,getmembernode} node_type_t; + +typedef struct { + char* key; + tobject_t* value; +} kvp_t; +struct dictinary{ + kvp_t* items; + int capacity; + int length; + } ; +struct list_tobject { + runtime_t* rt; + tobject_t** items; + int capacity; + int length; +}; +struct tobject +{ + tobject_type_t type; + atomic_int count; + texternalmethod_free_t free; + void* ptr_data; + union { + struct { + void* data; + external_method_t method; + + } external_method; + struct { + node_t* closure; + scope_t* scope; + } internal_method; + double number; + string_t* string; + list_tobject_t list; + dict_t* dict; + bool boolean; + char chr; + } data; +}; +struct runtime +{ + runtime_reg_t reg; + node_t* program; + scope_t* globals; + list_tobject_t args; + +}; +struct list_string +{ + string_t** strings; + int count; + int capacity; +}; +struct scope { + void* data; + runtime_t* rt; + bool isRoot; + atomic_int count; + get_scope_t root; + get_scope_t subscope; + get_variable_t getvariable; + set_variable_t setvariable; + has_variable_t hasvariable; +}; + +void scope_create_root(runtime_t* rt); +scope_t* scope_begin(scope_t* s); +scope_t* scope_end(scope_t* s); + +dict_t* dict_create(); +void dict_setkvp(dict_t* dict,kvp_t kvp); +kvp_t* dict_getkvp(dict_t* dict,char* key); +bool dict_haskey(dict_t* dict,char* key); +void dict_rm(dict_t* dict,char* key); +void dict_free(dict_t* dict); +typedef void (*node_free_t)(node_t*); +struct node +{ + node_type_t type; + node_exec_t execute; + node_free_t free; + union { + struct { + list_string_t argNames; + node_t* node; + } closure_node; + struct { + node_t* condition; + node_t* body; + bool isDoLoop; + } whileloop_node; + struct { + node_t** items; + int count; + int capacity; + bool isSub; + } scope_node; + node_t* single_node_node; + struct { + node_t* left; + node_t* right; + + } two_node_node; + struct { + node_t* variable; + node_t* value; + bool deleteVariableNode; + } variable_set_node; + double const_number; + string_t* const_string; + bool const_bool; + char const_char; + tobject_t* const_obj; + struct { + node_t* parent; + string_t* name; + node_t** args; + int count; + int capacity; + } function_call_node; + struct { + node_t* parent; + string_t* name; + node_set_t set; + } variable_node; + struct { + node_t* parent; + node_t* argument; + node_set_t set; + } array_node; + struct { + node_t* init; + node_t* condition; + node_t* inc; + node_t* body; + } forloop_node; + struct { + node_t* variable; + node_t* ls; + node_t* body; + } eachloop_node; + struct { + node_t* condition; + node_t* yes; + node_t* no; + } if_node; + } data; +}; + +tobject_t* tobject_create(runtime_t* rt); +tobject_t* tobject_number(runtime_t* rt,double num); +tobject_t* tobject_string(runtime_t* rt,string_t* str); +tobject_t* tobject_bool(runtime_t* rt,bool b); +tobject_t* tobject_char(runtime_t* rt,char c); +tobject_t* tobject_basic(runtime_t* rt,tobject_type_t type); +tobject_t* tobject_charp(runtime_t* rt,const char* text); +void tobject_addref(tobject_t*); +void tobject_rmref(tobject_t*); +void tscope_addref(scope_t*); +void tscope_rmref(scope_t*); + +runtime_t* runtime_init(); +void runtime_free(runtime_t* rt); +void runtime_load(runtime_t* rt,string_t* str); +tobject_t* runtime_exec(runtime_t* rt); +void string_free(string_t*); +string_t* string_create(); +void string_appendc(string_t*,char); +void string_clear(string_t*); +char* string_dupp(string_t*); +string_t* string_dups(string_t*); +void string_appends(string_t*,string_t*); +void string_read(string_t* str,void* ptr,size_t(*_cb)(void*,size_t,size_t,void*)); +ittr_t* string_ittr(runtime_t* rt,string_t* str); +void string_appendp(string_t* a,const char* str); +tobject_t* tobject_stringp(runtime_t* rt,const char* ptr); +struct lextokenlist +{ + lextoken_t* tokens; + int count; + int capacity; +}; +struct lextoken +{ + bool isChar; + bool isString; + string_t* text; +}; + +lextokenlist_t* lextokenlist_create(); +void lextokenlist_addstring(lextokenlist_t* ls,string_t* str); +void lextokenlist_additem(lextokenlist_t* ls,string_t* str); +void lextokenlist_addchar(lextokenlist_t* ls,char c); +bool lextoken_isitem(lextoken_t t,char*); + +lextokenlist_t* lexer_lex(string_t* text); +void lextokenlist_free(lextokenlist_t* t); +const char* string_trimstart(const char* ptr,char c); +void tobject_freeifzero(tobject_t* obj); + +void node_free(node_t*); +node_t* node_scope_create(bool); +node_t* node_add_create(node_t*,node_t*); +node_t* node_sub_create(node_t*,node_t*); +void node_scope_append(node_t* sc,node_t* n); +tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args); +void list_create(runtime_t* rt,list_tobject_t* ls,int count); +void list_add(list_tobject_t* a,tobject_t* item); +void string_appendn(string_t* s,double val); +bool string_samep(string_t* s,const char* text); +string_t* string_replace(string_t* text,string_t* old,string_t* new); +tobject_t* string_split(runtime_t* rt,string_t* text,string_t* del,int max,bool empty); +bool string_startswith(string_t* haystack,string_t* needle); +bool string_endswith(string_t* haystack,string_t* needle); +node_t* node_eq_create(node_t* left,node_t* right); +string_t* string_trimstarts(string_t* s,char c); + +string_t* string_trimends(string_t* s,char c); +bool string_islong(string_t* s,int64_t* number); +bool string_isnumber(string_t* s,double* number); +string_t* tobject_tostring(scope_t* sc,tobject_t* s); +node_t* node_const_number_create(double num); +node_t* node_const_string_create(string_t* s); +node_t* node_const_null_create(); +node_t* node_const_undef_create(); +tobject_t* tobject_fromexternalmethod(runtime_t* rt,void* data,external_method_t method,texternalmethod_free_t free); +tobject_t* tobject_create_array(runtime_t* rt,int count); +void list_free(list_tobject_t* a); +node_t* node_function_call_create(string_t*); +void node_function_call_add(node_t* fc,node_t* arg); +node_t* node_getvariable_create(string_t* name); +node_t* node_setvariable_create(node_t* n1,node_t* n2,bool); +node_t* node_getmember_create(string_t* name,node_t* parent); +node_t* node_bitwiseand_create(node_t* left,node_t* right); +node_t* node_bitwiseor_create(node_t* left,node_t* right); +node_t* node_xor_create(node_t* left,node_t* right); +node_t* node_neq_create(node_t* left,node_t* right); +struct ittr +{ + + void* ptr; + ittr_movenext_t movenext; + ittr_reset_t reset; + ittr_getcurrent_t getcurrent; + ittr_reset_t free; + +}; + +ittr_t* ittr_create(void* ptr,ittr_movenext_t movenext,ittr_reset_t reset,ittr_getcurrent_t current,ittr_reset_t free); +void ittr_reset(ittr_t* ittr); +ittr_t* ittr_obj(tobject_t* obj,scope_t* s); +tobject_t* ittr_current(ittr_t* ittr); +bool ittr_movenext(ittr_t* ittr); +void ittr_free(ittr_t* ittr); +ittr_t* list_ittr(runtime_t* rt,tobject_t* ls); +void runtime_create_method_on_dict(dict_t* dict,char* name,runtime_t* rt,void* ptr,external_method_t cb,texternalmethod_free_t free); +node_t* node_method_call_create(node_t* parent,string_t*); +void node_method_call_add(node_t* fc,node_t* arg); +node_t* node_modulo_create(node_t* left,node_t* right); +node_t* node_sub_create(node_t* left,node_t* right); +node_t* node_multiply_create(node_t* left,node_t* right); +node_t* node_divide_create(node_t* left,node_t* right); +node_t* node_leftshift_create(node_t* left,node_t* right); +node_t* node_rightshift_create(node_t* left,node_t* right); +node_t* node_lessthan_create(node_t* left,node_t* right); +node_t* node_greaterthan_create(node_t* left,node_t* right); +node_t* node_lessthanequal_create(node_t* left,node_t* right); +node_t* node_greaterthanequal_create(node_t* left,node_t* right); +node_t* node_xor_create(node_t* left,node_t* right); +node_t* node_logicaland_create(node_t* left,node_t* right); +node_t* node_logicalor_create(node_t* left,node_t* right); +node_t* node_closure_create(list_string_t argNames,node_t* node); +node_t* node_const_bool_create(bool c); +node_t* node_const_char_create(char c); +bool string_sames(string_t* s,string_t* b); +//node_t* node_setarray_create(node_t* parent,node_t* argument,node_t* variable); +node_t* node_getarray_create(node_t* parent,node_t* argument); + + + +typedef int64_t (*stream_read_t)(stream_t*,void*,int64_t); +typedef void (*stream_write_t)(stream_t*,void*,int64_t); +typedef int64_t (*stream_get_t)(stream_t*); +typedef void (*stream_set_t)(stream_t*,int64_t); +typedef bool (*stream_bool_t)(stream_t*); +typedef void (*stream_free_t)(stream_t*); +typedef void (*set_extra_methods_t)(stream_t*,tobject_t*,runtime_t*,void*); +struct stream +{ + void* ptr; + stream_read_t read; + stream_write_t write; + stream_get_t getpos; + stream_get_t getlength; + stream_set_t setpos; + stream_bool_t canread; + stream_bool_t canwrite; + stream_bool_t canseek; + stream_free_t free; + stream_free_t flush; + set_extra_methods_t set_extra_methods; + void* set_extra_methods_data; +}; + +stream_t* stream_create( + void* ptr, + stream_read_t read, + stream_write_t write, + stream_get_t getpos, + stream_get_t getlength, + stream_set_t setpos, + stream_bool_t canread, + stream_bool_t canwrite, + stream_bool_t canseek, + stream_free_t free, + stream_free_t flush +); +void stream_flush(stream_t* strm); +stream_t* tobject_tostream(scope_t* s,tobject_t* obj,bool closeStream); +tobject_t* tobject_fromstream(runtime_t* rt,stream_t* strm); +int64_t stream_read(stream_t*,void*,int64_t); +void stream_write(stream_t*,void*,int64_t); +int64_t stream_getpos(stream_t*); +int64_t stream_getlength(stream_t*); +void stream_setpos(stream_t*,int64_t); +bool stream_canread(stream_t*); +bool stream_canwrite(stream_t*); +bool stream_canseek(stream_t*); +stream_t* stream_stdin(); +stream_t* stream_stdout(); +stream_t* stream_stderr(); +void stream_free(stream_t*); + +bool stream_true(stream_t*); +bool stream_false(stream_t*); + + +stream_t* stream_file_create(string_t* str); +stream_t* stream_file_openread(string_t* str); +stream_t* stream_file_openreadwrite(string_t* str); +stream_t* stream_file_readappend(string_t* str); +stream_t* stream_file_append(string_t* str); + +ittr_t* fs_dir_ittr(runtime_t*,string_t*,fs_entry_filter_t); +bool fs_file_exists(string_t* f); +bool fs_dir_exists(string_t* d); + +void runtime_register(runtime_t* rt,runtime_reg_t reg); +bool tobject_tobool(tobject_t* o); +node_t* node_while_loop_create(node_t* condition,node_t* body,bool isDoLoop); +node_t* node_for_loop_create(node_t* init,node_t* condition,node_t* inc,node_t* body); +node_t* node_eachloop_create(node_t* var,node_t* ls,node_t* body); +node_t* node_if_create(node_t* condition,node_t* y,node_t* n); +tobject_t* tobject_same(runtime_t* rt,tobject_t* l,tobject_t* r,bool* freeleftifzero); +bool tobject_sameb(runtime_t* rt,tobject_t* l,tobject_t* r); +int list_indexof(list_tobject_t* a,tobject_t* item); +bool list_contains(list_tobject_t* a,tobject_t* item); +void list_remove(list_tobject_t* a,tobject_t* item); +void list_rm(list_tobject_t* a,int offset); +tobject_t* list_at(list_tobject_t* a,int offset); +node_t* node_bitwisenot_create(node_t* left); +typedef void* tmutex_t; +void Global_Mutex_Lock(); +void Global_Mutex_Unlock(); + +#if defined(USE_THREADS) +int64_t Mutex_Lock(tmutex_t mtx); +int64_t Mutex_Unlock(tmutex_t mtx); +#endif + +string_t* path_join(string_t* a,string_t* b); + +fs_entry_filter_t fs_type(string_t* _f); +string_t* string_substring(string_t* s,int start,int length); +string_t* string_remove(string_t* s,int start,int length); +int string_lastindexof(string_t* s,char c); +int string_indexof(string_t* s,char c); +string_t* path_getextension(string_t* a); +string_t* path_filename(string_t* a); +string_t* path_parent(string_t* a); +void runtime_add_argument(runtime_t* rt,tobject_t* arg); \ No newline at end of file diff --git a/libtlang/src/addnode.c b/libtlang/src/addnode.c new file mode 100644 index 0000000..abfc0bc --- /dev/null +++ b/libtlang/src/addnode.c @@ -0,0 +1,114 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __add_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"add")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"add"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + + out->data.number = l->data.number + r->data.number; + + } + if(ltype == tchar && rtype == tstring) + { + string_t* newStr = string_create(); + string_appendc(newStr,l->data.chr); + string_appends(newStr,r->data.string); + out=tobject_create(sc->rt); + out->type = tstring; + out->data.string = newStr; + } + if(ltype == tstring && rtype == tchar) + { + string_t* newStr = string_create(); + string_appends(newStr,l->data.string); + string_appendc(newStr,r->data.chr); + out=tobject_create(sc->rt); + out->type = tstring; + out->data.string = newStr; + } + if(ltype == tstring && rtype == tstring) + { + string_t* newStr = string_create(); + string_appends(newStr,l->data.string); + string_appends(newStr,r->data.string); + out=tobject_create(sc->rt); + out->type = tstring; + out->data.string = newStr; + } + if(ltype == tstring && rtype == tnumber) + { + string_t* newStr = string_create(); + string_appends(newStr,l->data.string); + string_appendn(newStr,r->data.number); + out=tobject_create(sc->rt); + out->type = tstring; + out->data.string = newStr; + } + if(ltype == tnumber && rtype == tstring) + { + string_t* newStr = string_create(); + string_appendn(newStr,l->data.number); + string_appends(newStr,r->data.string); + out=tobject_create(sc->rt); + out->type = tstring; + out->data.string = newStr; + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_add_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = addnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __add_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/bitwiseand.c b/libtlang/src/bitwiseand.c new file mode 100644 index 0000000..1dd5187 --- /dev/null +++ b/libtlang/src/bitwiseand.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __bitwiseand_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"band")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"band"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = (int64_t)l->data.number & (int64_t)r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_bitwiseand_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = bitwiseandnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __bitwiseand_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/bitwiseor.c b/libtlang/src/bitwiseor.c new file mode 100644 index 0000000..bf0443e --- /dev/null +++ b/libtlang/src/bitwiseor.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __bitwiseor_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"bor")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"bor"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = (int64_t)l->data.number | (int64_t)r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_bitwiseor_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = bitwiseornode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __bitwiseor_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/bwnot.c b/libtlang/src/bwnot.c new file mode 100644 index 0000000..805bd61 --- /dev/null +++ b/libtlang/src/bwnot.c @@ -0,0 +1,67 @@ +#include "tlang.h" + +extern void __node_single_free(node_t* n); +tobject_t* __bwnot_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.single_node_node; + tobject_t* l=left->execute(left,sc); + + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"bnot")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"bnot"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + + list_free(&ls); + + freeleftifzero = false; + } + } + } + if(ltype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = ~(int64_t)l->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_bitwisenot_create(node_t* left) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = bnotnode; + node->data.single_node_node = left; + + node->execute = __bwnot_node_exec; + node->free = __node_single_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/closurenode.c b/libtlang/src/closurenode.c new file mode 100644 index 0000000..cdd6c98 --- /dev/null +++ b/libtlang/src/closurenode.c @@ -0,0 +1,26 @@ +#include "tlang.h" + +tobject_t* __closure_node_exec(node_t* n,scope_t* sc) +{ + tobject_t* c = tobject_create(sc->rt); + c->type = tinternalmethod; + c->data.internal_method.closure = node_closure_create(n->data.closure_node.argNames,n->data.closure_node.node); + c->data.internal_method.scope = sc; + sc->count++; + return c; +} +void __closure_free(node_t* n) +{ + +} + +node_t* node_closure_create(list_string_t argNames,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = addnode; + node->data.closure_node.argNames = argNames; + node->data.closure_node.node = right; + node->execute = __closure_node_exec; + node->free = __closure_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/constnode.c b/libtlang/src/constnode.c new file mode 100644 index 0000000..3c7e735 --- /dev/null +++ b/libtlang/src/constnode.c @@ -0,0 +1,114 @@ +#include "tlang.h" +tobject_t* __const_number_exec(node_t* n,scope_t* sc) +{ + tobject_t* obj=tobject_create(sc->rt); + obj->data.number = n->data.const_number; + obj->type =tnumber; + return obj; +} +void __const_number_free(node_t* s) +{ + +} +void __const_string_free(node_t* s) +{ + string_free(s->data.const_string); +} +node_t* node_const_number_create(double num) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->data.const_number = num; + n->type = constnumbernode; + n->execute = __const_number_exec; + n->free = __const_number_free; + return n; +} +tobject_t* __const_bool_exec(node_t* n,scope_t* sc) +{ + tobject_t* obj=tobject_create(sc->rt); + obj->data.boolean = n->data.const_bool; + + obj->type =tbool; + return obj; +} +tobject_t* __const_string_exec(node_t* n,scope_t* sc) +{ + tobject_t* obj=tobject_create(sc->rt); + obj->data.string = string_dups(n->data.const_string); + + obj->type =tstring; + return obj; +} +tobject_t* __const_char_exec(node_t* n,scope_t* sc) +{ + tobject_t* obj=tobject_create(sc->rt); + obj->data.chr = n->data.const_char; + obj->type =tchar; + return obj; +} +node_t* node_const_string_create(string_t* s) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->data.const_string = string_dups(s); + n->type = conststringnode; + n->execute = __const_string_exec; + n->free = __const_string_free; + return n; +} +node_t* node_const_char_create(char c) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->data.const_char = c; + n->type = constcharnode; + n->execute = __const_char_exec; + n->free = __const_number_free; + return n; +} +tobject_t* __const_object_exec(node_t* n,scope_t* sc) +{ + return n->data.const_obj; +} +node_t* node_const_object_create(tobject_t* o) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->data.const_obj = o; + n->type = constobjectnode; + n->execute = __const_object_exec; + n->free = __const_number_free; + return n; +} +node_t* node_const_bool_create(bool c) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->data.const_bool = c; + n->type = constboolnode; + n->execute = __const_bool_exec; + n->free = __const_number_free; + return n; +} +tobject_t* __const_undef_exec(node_t* n,scope_t* s) +{ + return tobject_basic(s->rt,tundef); +} +node_t* node_const_undef_create() +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + + n->type = constundefnode; + n->execute = __const_undef_exec; + n->free = __const_number_free; + return n; +} +tobject_t* __const_null_exec(node_t* n,scope_t* s) +{ + return tobject_basic(s->rt,tnull); +} +node_t* node_const_null_create() +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + + n->type = constnullnode; + n->execute = __const_null_exec; + n->free = __const_number_free; + return n; +} \ No newline at end of file diff --git a/libtlang/src/dict.c b/libtlang/src/dict.c new file mode 100644 index 0000000..c01d8c1 --- /dev/null +++ b/libtlang/src/dict.c @@ -0,0 +1,101 @@ +#include "tlang.h" +#include +dict_t* dict_create() +{ + dict_t* dict = (dict_t*)malloc(sizeof(dict_t)); + dict->capacity =256; + dict->items=(kvp_t*)malloc(sizeof(kvp_t) * 256); + dict->length = 0; + return dict; + +} +void _dict_setkvp(dict_t* dict,kvp_t kvp) +{ + int i; + for(i=0;ilength;i++) + { + if(strcmp(dict->items[i].key,kvp.key) == 0) + { + dict->items[i].value = kvp.value; + return; + } + } + if(dict->length + 128 > dict->capacity) + { + dict->capacity = dict->length + 128; + dict->items=(kvp_t*)realloc(dict,dict->capacity * sizeof(kvp_t)); + + } + dict->length++; + size_t len = strlen(kvp.key); + dict->items[i].key = (char*)malloc(sizeof(char) * (len+1)); + strncpy(dict->items[i].key,kvp.key,len); + dict->items[i].key[len] = 0; + dict->items[i].value = kvp.value; +} +void dict_setkvp(dict_t* d,kvp_t kvp) +{ + tobject_addref(kvp.value); + if(dict_haskey(d,kvp.key)) + { + + tobject_rmref(dict_getkvp(d,kvp.key)->value); + } + + _dict_setkvp(d,kvp); +} +kvp_t* dict_getkvp(dict_t* dict,char* key) +{ + int i; + kvp_t* kvp=NULL; + for(i=0;ilength;i++) + { + if(strcmp(dict->items[i].key,key) == 0) + { + kvp = &dict->items[i]; + } + } + return kvp; +} +bool dict_haskey(dict_t* dict,char* key) +{ + int i; + + for(i=0;ilength;i++) + { + if(strcmp(dict->items[i].key,key) == 0) + { + return true; + } + } + return false; +} +void dict_rm(dict_t* dict,char* key) +{ + int i; + for(i=0;ilength;i++) + { + if(strcmp(dict->items[i].key,key)==0) + { + free(dict->items[i].key); + tobject_rmref(dict->items[i].value); + break; + } + } + dict->length--; + for(;ilength;i++) + { + dict->items[i] = dict->items[i+1]; + } +} +void dict_free(dict_t* dict) +{ + int i; + for(i=0;ilength;i++) + { + free(dict->items[i].key); + tobject_rmref(dict->items[i].value); + } + free(dict->items); + free(dict); +} \ No newline at end of file diff --git a/libtlang/src/dirittr.c b/libtlang/src/dirittr.c new file mode 100644 index 0000000..9c61133 --- /dev/null +++ b/libtlang/src/dirittr.c @@ -0,0 +1,95 @@ +#include "tlang.h" +#include +#include + +typedef struct +{ + runtime_t* rt; + string_t* curStr; + DIR* myDir; + fs_entry_filter_t filter; + char* dir; +} dir_ittr_t; +void _dir_reset(ittr_t* ittr) +{ + dir_ittr_t* itt=(dir_ittr_t*)ittr->ptr; + rewinddir(itt->myDir); +} +void _dir_free(ittr_t* ittr) +{ + dir_ittr_t* itt=(dir_ittr_t*)ittr->ptr; + string_free(itt->curStr); + closedir(itt->myDir); + free(itt->dir); + free(itt); + +} +tobject_t* _dir_getcurrent(ittr_t* ittr) +{ + dir_ittr_t* itt=(dir_ittr_t*)ittr->ptr; + return tobject_string(itt->rt,string_dups(itt->curStr)); +} +struct dirent* readdir2(dir_ittr_t* itt,bool* hadValue) +{ + struct dirent* dir= readdir(itt->myDir); + *hadValue=false; + if(dir != NULL) + { + *hadValue = true; + string_t* _dir = string_create(); + string_appendp(_dir,itt->dir); + string_t* _subentry = string_create(); + string_appendp(_subentry,dir->d_name); + if(strcmp(dir->d_name,"..") == 0 || strcmp(dir->d_name,".") == 0) return NULL; + + string_t* r = path_join(_dir,_subentry); + string_free(_dir); + string_free(_subentry); + bool entry_valid = (fs_type(r) & itt->filter) > 0; + string_free(r); + if(entry_valid) + return dir; + } + + return NULL; +} +struct dirent* readdir_till_matches(dir_ittr_t* itt) +{ + if(itt->myDir == NULL) return NULL; + bool hadValue=true; + while(hadValue) + { + struct dirent* res=readdir2(itt,&hadValue); + if(res != NULL) return res; + } + return NULL; +} +bool _dir_move_next(ittr_t* ittr) +{ + dir_ittr_t* itt=(dir_ittr_t*)ittr->ptr; + struct dirent* dir= readdir_till_matches(itt); + + if(dir != NULL) + { + string_clear(itt->curStr); + string_appendp(itt->curStr,dir->d_name); + return true; + } + + return false; +} + +ittr_t* fs_dir_ittr(runtime_t* rt,string_t* dir,fs_entry_filter_t filter) +{ + + dir_ittr_t* _dirittr=(dir_ittr_t*)malloc(sizeof(dir_ittr_t)); + _dirittr->rt = rt; + char* _dir = string_dupp(dir); + _dirittr->myDir= opendir(_dir); + _dirittr->curStr = string_create(); + _dirittr->filter = filter; + _dirittr->dir = _dir; + + return ittr_create(_dirittr,_dir_move_next,_dir_reset,_dir_getcurrent,_dir_free); + +} \ No newline at end of file diff --git a/libtlang/src/dividenode.c b/libtlang/src/dividenode.c new file mode 100644 index 0000000..e729cd7 --- /dev/null +++ b/libtlang/src/dividenode.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __divide_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"divide")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"divide"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = l->data.number / r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_divide_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = dividenode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __divide_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/eachnode.c b/libtlang/src/eachnode.c new file mode 100644 index 0000000..be4eb24 --- /dev/null +++ b/libtlang/src/eachnode.c @@ -0,0 +1,64 @@ +#include "tlang.h" +extern tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj); +tobject_t* __each_loop_exec(node_t* n,scope_t* s) +{ + tobject_t* ls=NULL; + s=scope_begin(s); + if(n->data.eachloop_node.ls != NULL) + ls= n->data.eachloop_node.ls->execute(n->data.eachloop_node.ls,s); + bool isRunning=true; + s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL)); + tobject_t* res = tobject_basic(s->rt,tundef); + res->count=1; + if(ls != NULL) + { + ittr_t* ittr = ittr_obj(ls,s); + if(ittr != NULL) + { + while(ittr_movenext(ittr) && isRunning) + { + tobject_t* obj= ittr->getcurrent(ittr); + if(n->data.eachloop_node.variable->type == getvariablenode || n->data.eachloop_node.variable->type == getmembernode) + { + n->data.eachloop_node.variable->data.variable_node.set(n->data.eachloop_node.variable,obj,s); + }else + if(n->data.eachloop_node.variable->type == getarraynode) + { + n->data.eachloop_node.variable->data.array_node.set(n->data.eachloop_node.variable,obj,s); + } + tobject_t* myRes=n->data.eachloop_node.body->execute(n->data.eachloop_node.body,s); + tobject_addref(myRes); + tobject_rmref(res); + + res=myRes; + tobject_freeifzero(obj); + } + ittr_free(ittr); + } + } + s=scope_end(s); + tobject_freeifzero(ls); + res->count--; + return res; +} +void __each_loop_free(node_t* n) +{ + if(n->data.eachloop_node.variable != NULL) + node_free(n->data.eachloop_node.variable); + if(n->data.eachloop_node.ls != NULL) + node_free(n->data.eachloop_node.ls); + if(n->data.eachloop_node.body != NULL) + node_free(n->data.eachloop_node.body); +} +node_t* node_eachloop_create(node_t* var,node_t* ls,node_t* body) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = eachloopnode; + node->data.eachloop_node.variable = var; + node->data.eachloop_node.ls = ls; + + node->data.eachloop_node.body = body; + node->execute = __each_loop_exec; + node->free = __each_loop_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/eq.c b/libtlang/src/eq.c new file mode 100644 index 0000000..fd8ae44 --- /dev/null +++ b/libtlang/src/eq.c @@ -0,0 +1,39 @@ +#include "tlang.h" +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __eq_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=tobject_same(sc->rt,l,r,&freeleftifzero); + if(out != NULL) out->count++; + if(freeleftifzero) + { + + tobject_freeifzero(l); + + } + + tobject_freeifzero(r); + if(out != NULL) out->count--; + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_eq_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = equalsnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __eq_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/forloop.c b/libtlang/src/forloop.c new file mode 100644 index 0000000..304841b --- /dev/null +++ b/libtlang/src/forloop.c @@ -0,0 +1,73 @@ +#include "tlang.h" + + +tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + bool* flag = (bool*)ptr; + *flag=false; + return tobject_bool(rt,true); +} +tobject_t* __loop_exec2(node_t* n,scope_t* s) +{ + if(n == NULL) return tobject_bool(s->rt,true); + return n->execute(n,s); +} +bool __loop_bool(node_t* n,scope_t* s) +{ + tobject_t* o = __loop_exec2(n,s); + bool r = tobject_tobool(o); + tobject_freeifzero(o); + return r; +} +tobject_t* __for_loop_exec(node_t* a,scope_t* s) +{ + //do for loop crap + s=scope_begin(s); + if(a->data.forloop_node.init != NULL) + tobject_freeifzero(a->data.forloop_node.init->execute(a->data.forloop_node.init,s)); + + bool isRunning=true; + s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL)); + tobject_t* res = tobject_basic(s->rt,tundef); + res->count=1; + while(__loop_bool(a->data.forloop_node.condition,s) && isRunning) + { + + tobject_t* _r = __loop_exec2(a->data.forloop_node.body,s); + tobject_addref(_r); + tobject_rmref(res); + res=_r; + + __loop_bool(a->data.forloop_node.inc,s); + } + + s=scope_end(s); + res->count--; + return res; +} + + +void __for_loop_free(node_t* a) +{ + if(a->data.forloop_node.init != NULL) + node_free(a->data.forloop_node.init); + if(a->data.forloop_node.condition != NULL) + node_free(a->data.forloop_node.condition); + if(a->data.forloop_node.inc != NULL) + node_free(a->data.forloop_node.inc); + if(a->data.forloop_node.body != NULL) + node_free(a->data.forloop_node.body); +} + +node_t* node_for_loop_create(node_t* init,node_t* condition,node_t* inc,node_t* body) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = forloopnode; + node->data.forloop_node.init = init; + node->data.forloop_node.condition = condition; + node->data.forloop_node.inc = inc; + node->data.forloop_node.body = body; + node->execute = __for_loop_exec; + node->free = __for_loop_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/fs.c b/libtlang/src/fs.c new file mode 100644 index 0000000..6df4544 --- /dev/null +++ b/libtlang/src/fs.c @@ -0,0 +1,111 @@ +#include "tlang.h" +#include +char fs_path_sep() +{ + #if defined(WIN32) || defined(_WIN32) + return '\\'; + #else + return '/'; + #endif +} +char fs_env_sep() +{ + #if defined(WIN32) || defined(_WIN32) + return ';'; + #else + return ':'; + #endif +} + +string_t* path_parent(string_t* a) +{ + int j = string_lastindexof(a,fs_path_sep()); + return string_substring(a,0,j); +} + +string_t* path_filename(string_t* a) +{ + int j = string_lastindexof(a,fs_path_sep()); + if(j == -1) return string_dups(a); + j++; + return string_substring(a,j,a->length-j); +} +string_t* path_getextension(string_t* a) +{ + int j = string_lastindexof(a,'.'); + if(j == -1) return string_create(); + + return string_substring(a,j,a->length-j); +} +string_t* path_join(string_t* a,string_t* b) +{ + string_t* s = string_trimends(a,fs_path_sep()); + string_appendc(s,fs_path_sep()); + string_t* s2 = string_trimstarts(b,fs_path_sep()); + string_appends(s,s2); + string_free(s2); + return s; +} + +fs_entry_filter_t fs_type(string_t* _f) +{ + struct stat myStat; + char* f = string_dupp(_f); + if(stat(f,&myStat) == -1) + { + free(f); + return NONEXIST; + } + free(f); + if(S_ISREG(myStat.st_mode)) + { + return REGULAR; + } + if(S_ISDIR(myStat.st_mode)) + { + return FOLDER; + } + if(S_ISCHR(myStat.st_mode)) + { + return CHARDEV; + } + if(S_ISBLK(myStat.st_mode)) + { + return BLOCKDEV; + } + if(S_ISLNK(myStat.st_mode)) + { + return SYM; + } + if(S_ISFIFO(myStat.st_mode)) + { + return FIFO; + } + return OTHER; +} +bool fs_file_exists(string_t* _f) +{ + struct stat myStat; + char* f = string_dupp(_f); + if(stat(f,&myStat) == -1) + { + free(f); + return false; + } + free(f); + return S_ISREG(myStat.st_mode); + +} + +bool fs_dir_exists(string_t* d) +{ + struct stat myStat; + char* f = string_dupp(d); + if(stat(f,&myStat) == -1) + { + free(f); + return false; + } + free(f); + return S_ISDIR(myStat.st_mode); +} diff --git a/libtlang/src/functioncallnode.c b/libtlang/src/functioncallnode.c new file mode 100644 index 0000000..24bbb07 --- /dev/null +++ b/libtlang/src/functioncallnode.c @@ -0,0 +1,58 @@ +#include "tlang.h" +tobject_t* __node_function_call_execute(node_t* n,scope_t* s) +{ + + list_tobject_t args; + list_create(s->rt,&args,0); + + int i; + for(i = 0;i< n->data.function_call_node.count;i++) + { + node_t* a =n->data.function_call_node.args[i]; + list_add(&args,a->execute(a,s)); + } + char* name = string_dupp(n->data.function_call_node.name); + tobject_t* fname = s->getvariable(s,name); + free(name); + tobject_t* o = tobject_call(s,fname,&args); + o->count++; //ensures this object is not freed when freeing list + list_free(&args); + o->count--; + return o; +} + +void __node_function_call_free(node_t* node) +{ + node_t** args= node->data.function_call_node.args; + int i; + for(i=0;idata.function_call_node.count;i++) + { + node_free(args[i]); + } + free(args); + string_free(node->data.function_call_node.name); +} + +node_t* node_function_call_create(string_t* name) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = functioncallnode; + n->data.function_call_node.count = 0; + n->data.function_call_node.name = string_dups(name); + n->data.function_call_node.capacity=64; + n->data.function_call_node.args=(node_t**)malloc(sizeof(node_t*) * n->data.scope_node.capacity); + n->execute = __node_function_call_execute; + n->free = __node_function_call_free; + + return n; +} +void node_function_call_add(node_t* fc,node_t* arg) +{ + if(fc->data.function_call_node.count + 128 >fc->data.scope_node.capacity) + { + fc->data.function_call_node.capacity = fc->data.function_call_node.count + 128; + fc->data.function_call_node.args=(node_t**)realloc(fc->data.function_call_node.args,fc->data.function_call_node.capacity * sizeof(node_t*)); + + } + fc->data.function_call_node.args[fc->data.function_call_node.count++]=arg; +} \ No newline at end of file diff --git a/libtlang/src/getarraynode.c b/libtlang/src/getarraynode.c new file mode 100644 index 0000000..bf4347b --- /dev/null +++ b/libtlang/src/getarraynode.c @@ -0,0 +1,211 @@ + + +#include "tlang.h" +#include +#include +tobject_t* __kvp_as_str(tobject_t* parent_obj,tobject_t* argument_obj) +{ + if(parent_obj->type != tdict) return NULL; + if(argument_obj->type == tstring) + { + char* p = string_dupp(argument_obj->data.string); + if(dict_haskey(parent_obj->data.dict,p)) + { + tobject_t* v = dict_getkvp(parent_obj->data.dict,p)->value; + free(p); + return v; + } + free(p); + } + return NULL; +} + +tobject_t* __node_getarray_execute(node_t* n,scope_t* s) +{ + node_t* parent=n->data.array_node.parent; + tobject_t* parent_obj = parent->execute(parent,s); + tobject_t* o = NULL; + node_t* argument =n->data.array_node.argument; + tobject_t* argument_obj = argument->execute(argument,s); + + if(parent_obj->type == tlist) + { + //argument is expected to be a number + if(argument_obj->type == tnumber) + { + //yay it is a number + int pos = (int)argument_obj->data.number; + if(pos >= 0 && pos <= parent_obj->data.list.length) + { + o = parent_obj->data.list.items[pos]; + + } + } + } + if(parent_obj->type == tstring) + { + //argument is expected to be a number + if(argument_obj->type == tnumber) + { + //yay it is a number + int pos = (int)argument_obj->data.number; + if(pos >= 0 && pos <= parent_obj->data.string->length) + { + o = tobject_char(s->rt,parent_obj->data.string->text[pos]); + } + + } + } + if(parent_obj->type == tdict) + { + if(dict_haskey(parent_obj->data.string,"at")) + { + tobject_t* obj=dict_getkvp(parent_obj->data.string,"at")->value; + if(obj->type == tinternalmethod || obj->type == texternalmethod) + { + list_tobject_t args; + argument_obj->count++; + list_add(&args,argument_obj); + o=tobject_call(s,obj,&args); + o->count++; + + list_free(&args); + o->count--; + argument_obj->count--; + }else{ + //get kvp value as string + o=__kvp_as_str(parent_obj,argument_obj); + } + }else{ + o=__kvp_as_str(parent_obj,argument_obj); + + } + } + + if(o == NULL) + { + o = tobject_basic(s->rt,tundef); + } + o->count++; + argument_obj->count++; + tobject_freeifzero(parent_obj); + argument_obj->count--; + tobject_freeifzero(argument_obj); + o->count--; + return o; +} +void __kvp_set(tobject_t* parent_obj,tobject_t* argument_obj,tobject_t* variable) +{ + if(parent_obj->type != tdict) return NULL; + if(argument_obj->type == tstring) + { + char* p = string_dupp(argument_obj->data.string); + if(dict_haskey(parent_obj->data.dict,p)) + { + kvp_t k; + k.key = p; + k.value = variable; + dict_setkvp(parent_obj->data.dict,k); + } + free(p); + } + +} + +void __node_setarray_execute(node_t* n,tobject_t* variable_obj,scope_t* s) +{ + node_t* parent=n->data.array_node.parent; + tobject_t* parent_obj = parent->execute(parent,s); + + node_t* argument =n->data.array_node.argument; + tobject_t* argument_obj = argument->execute(argument,s); + + if(parent_obj->type == tlist) + { + //argument is expected to be a number + if(argument_obj->type == tnumber) + { + //yay it is a number + int pos = (int)argument_obj->data.number; + if(pos >= 0 && pos <= parent_obj->data.list.length) + { + list_set(&parent_obj->data.list,pos,variable_obj); + + } + } + } + /* if(parent_obj->type == tstring) + { + //argument is expected to be a number + if(argument_obj->type == tnumber) + { + //yay it is a number + int pos = (int)argument_obj->data.number; + if(pos >= 0 && pos <= parent_obj->data.string->length) + { + o = tobject_char(s->rt,parent_obj->data.string->text[pos]); + } + + } + }*/ + if(parent_obj->type == tdict) + { + if(dict_haskey(parent_obj->data.string,"setAt")) + { + tobject_t* obj=dict_getkvp(parent_obj->data.string,"setAt")->value; + if(obj->type == tinternalmethod || obj->type == texternalmethod) + { + list_tobject_t args; + variable_obj->count++; + parent_obj->count++; + argument_obj->count++; + obj->count++; + list_add(&args,argument_obj); + list_add(&args,variable_obj); + + + tobject_t* o2= tobject_call(s,obj,&args); + tobject_freeifzero(o2); + + list_free(&args); + variable_obj->count--; + parent_obj->count--; + argument_obj->count--; + obj->count--; + }else{ + //get kvp value as string + __kvp_set(parent_obj,argument_obj,variable_obj); + } + }else{ + __kvp_set(parent_obj,argument_obj,variable_obj); + + } + } + + + variable_obj->count++; + argument_obj->count++; + tobject_freeifzero(parent_obj); + argument_obj->count--; + tobject_freeifzero(argument_obj); + variable_obj->count--; + +} + +void __node_getarray_free(node_t* node) +{ + node_free(node->data.array_node.argument); + node_free(node->data.array_node.parent); +} + +node_t* node_getarray_create(node_t* parent,node_t* argument) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = getarraynode; + n->execute = __node_getarray_execute; + n->free = __node_getarray_free; + n->data.array_node.set = __node_setarray_execute; + n->data.array_node.argument = argument; + n->data.array_node.parent = parent; + return n; +} diff --git a/libtlang/src/getmembernode.c b/libtlang/src/getmembernode.c new file mode 100644 index 0000000..0201cfb --- /dev/null +++ b/libtlang/src/getmembernode.c @@ -0,0 +1,223 @@ +#include "tlang.h" +#include +#include + +tobject_t* __ls_get_current(runtime_t* rt,void* ptr,list_tobject_t* ls) +{ + ittr_t* ittr=(ittr_t*)ptr; + return ittr_current(ittr); +} +tobject_t* __ls_movenext(runtime_t* rt,void* ptr,list_tobject_t* ls) +{ + ittr_t* ittr=(ittr_t*)ptr; + + bool v=ittr_movenext(ittr); + tobject_t* o = tobject_create(rt); + o->type = tbool; + o->data.boolean = v; + return o; +} +tobject_t* __ls_reset(runtime_t* rt,void* ptr,list_tobject_t* ls) +{ + ittr_t* ittr=(ittr_t*)ptr; + ittr_reset(ittr); + return tobject_basic(rt,tnull); + +} +tobject_t* __ls_free(runtime_t* rt,void* ptr,list_tobject_t* ls) +{ + ittr_t* ittr=(ittr_t*)ptr; + ittr_free(ittr); + return tobject_basic(rt,tnull); + +} +tobject_t* __node_getmember_execute(node_t* n,scope_t* s) +{ + node_t* parent=n->data.variable_node.parent; + tobject_t* parent_obj = parent->execute(parent,s); + + char* vn = string_dupp(n->data.variable_node.name); + string_t* getvars = string_create(); + string_appendp(getvars,"get"); + string_appends(getvars,n->data.variable_node.name); + char* getvar = string_dupp(getvars); + string_free(getvars); + tobject_t* o = NULL; + if(parent_obj->type == tlist) + { + if(strcmp(vn,"length") == 0 || strcmp(vn,"count") == 0) + { + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = parent_obj->data.list.length; + } + if(strcmp(vn,"ittr") == 0) + { + o = tobject_create(s->rt); + o->type = tdict; + dict_t* d= dict_create(); + ittr_t* ittr=list_ittr(s->rt,parent_obj); + runtime_create_method_on_dict(d,"getcurrent",s->rt,ittr,__ls_get_current,NULL); + runtime_create_method_on_dict(d,"movenext",s->rt,ittr,__ls_movenext,NULL); + runtime_create_method_on_dict(d,"reset",s->rt,ittr,__ls_reset,NULL); + runtime_create_method_on_dict(d,"dispose",s->rt,ittr,__ls_free,NULL); + + + o->data.dict= d; + } + + } + if(parent_obj->type == tdict) + { + if(dict_haskey(parent_obj->data.dict,getvar)) + { + tobject_t* _o = dict_getkvp(parent_obj->data.dict,getvar)->value; + list_tobject_t arg; + list_create(s->rt,&arg,0); + tobject_t* o2=tobject_call(s,_o,&arg); + o2->count++; + list_free(&arg); + o2->count--; + o= o2; + }else + if(dict_haskey(parent_obj->data.dict,vn)) + { + o = dict_getkvp(parent_obj->data.dict,vn)->value; + + } + } + if(parent_obj->type == tnumber) + { + if(strcmp(vn,"abs") == 0) + { + double val=fabs(parent_obj->data.number); + + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = val; + } + if(strcmp(vn,"int") == 0) + { + int64_t val=(int64_t)parent_obj->data.number; + + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = val; + } + + } + if(parent_obj->type == tstring) + { + if(strcmp(vn,"number") == 0) + { + char* ptr=string_dupp(parent_obj->data.string); + double num=0; + if(string_isnumber(parent_obj->data.string,&num)) + { + o = tobject_create(s->rt); + o->data.number = num; + o->type = tnumber; + }else{ + o=tobject_basic(s->rt,tnull); + } + + free(ptr); + } + if(strcmp(vn,"ittr") == 0) + { + o = tobject_create(s->rt); + o->type = tdict; + dict_t* d= dict_create(); + ittr_t* ittr=string_ittr(s->rt,parent_obj->data.string); + runtime_create_method_on_dict(d,"getcurrent",s->rt,ittr,__ls_get_current,NULL); + runtime_create_method_on_dict(d,"movenext",s->rt,ittr,__ls_movenext,NULL); + runtime_create_method_on_dict(d,"reset",s->rt,ittr,__ls_reset,NULL); + runtime_create_method_on_dict(d,"dispose",s->rt,ittr,__ls_free,NULL); + + + o->data.dict= d; + } + if(strcmp(vn,"length") == 0 || strcmp(vn,"count") == 0) + { + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = parent_obj->data.string->length; + + } + if(strcmp(vn,"boolean") == 0) + { + o = tobject_create(s->rt); + o->type = tbool; + o->data.boolean = string_samep(parent_obj->data.string,"true"); + } + } + free(vn); + free(getvar); + if(o == NULL) + { + o = tobject_basic(s->rt,tundef); + } + o->count++; + tobject_freeifzero(parent_obj); + o->count--; + return o; +} +void __node_setmember_execute(node_t* n,tobject_t* o,scope_t* s) +{ + node_t* parent=n->data.variable_node.parent; + //node_t* myN = n->data.variable_node.value; + tobject_t* parent_obj = parent->execute(parent,s); + + char* vn = string_dupp(n->data.variable_node.name); + string_t* getvars = string_create(); + string_appendp(getvars,"set"); + string_appends(getvars,n->data.variable_node.name); + char* getvar = string_dupp(getvars); + string_free(getvars); + + + + if(parent_obj->type == tdict) + { + if(dict_haskey(parent_obj->data.dict,getvar)) + { + tobject_t* _o = dict_getkvp(parent_obj->data.dict,getvar)->value; + list_tobject_t arg; + list_create(s->rt,&arg,0); + list_add(&arg,o); + tobject_freeifzero(tobject_call(s,_o,&arg)); + list_free(&arg); + + }else + { + kvp_t kvp; + kvp.key = vn; + kvp.value = o; + dict_setkvp(parent_obj->data.dict,kvp); + + } + } + free(vn); + free(getvar); + + tobject_freeifzero(parent_obj); + +} + +void __node_getmember_free(node_t* node) +{ + string_free(node->data.variable_node.name); + node_free(node->data.variable_node.parent); +} + +node_t* node_getmember_create(string_t* name,node_t* parent) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = getmembernode; + n->execute = __node_getmember_execute; + n->free = __node_getmember_free; + n->data.variable_node.name = string_dups(name); + n->data.variable_node.parent = parent; + n->data.variable_node.set = __node_setmember_execute; + return n; +} diff --git a/libtlang/src/getvariablenode.c b/libtlang/src/getvariablenode.c new file mode 100644 index 0000000..177634b --- /dev/null +++ b/libtlang/src/getvariablenode.c @@ -0,0 +1,34 @@ +#include "tlang.h" +tobject_t* __node_getvariable_execute(node_t* n,scope_t* s) +{ + char* vn = string_dupp(n->data.variable_node.name); + + tobject_t* o = s->getvariable(s,vn); + + free(vn); + return o; +} +void __node_setvariable(node_t* n,tobject_t* o,scope_t* s) +{ + + char* vn = string_dupp(n->data.variable_node.name); + + s->setvariable(s,vn,o); + free(vn); + +} +void __node_getvariable_free(node_t* node) +{ + string_free(node->data.variable_node.name); +} + +node_t* node_getvariable_create(string_t* name) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = getvariablenode; + n->execute = __node_getvariable_execute; + n->free = __node_getvariable_free; + n->data.variable_node.name = string_dups(name); + n->data.variable_node.set = __node_setvariable; + return n; +} diff --git a/libtlang/src/greaterthan.c b/libtlang/src/greaterthan.c new file mode 100644 index 0000000..d5a9b49 --- /dev/null +++ b/libtlang/src/greaterthan.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __greaterthan_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"gt")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"gt"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.number > r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_greaterthan_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = greaterthannode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __greaterthan_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/greaterthanequal.c b/libtlang/src/greaterthanequal.c new file mode 100644 index 0000000..8f43753 --- /dev/null +++ b/libtlang/src/greaterthanequal.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __greaterthanequal_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"gte")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"gte"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.number >= r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_greaterthanequal_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = greaterthanequalnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __greaterthanequal_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/if.c b/libtlang/src/if.c new file mode 100644 index 0000000..bcdd173 --- /dev/null +++ b/libtlang/src/if.c @@ -0,0 +1,28 @@ +#include "tlang.h" +extern bool __loop_bool(node_t* n,scope_t* s); +tobject_t* __if_exec(node_t* n,scope_t* s) +{ + if(__loop_bool(n->data.if_node.condition,s)) + { + return n->data.if_node.yes->execute(n->data.if_node.yes,s); + } else { + return n->data.if_node.no->execute(n->data.if_node.no,s); + } +} +void __if_free(node_t* n) +{ + node_free(n->data.if_node.condition); + node_free(n->data.if_node.yes); + node_free(n->data.if_node.no); +} +node_t* node_if_create(node_t* condition,node_t* y,node_t* n) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = ifnode; + node->data.if_node.condition = condition; + node->data.if_node.yes = y; + node->data.if_node.no = n; + node->execute = __if_exec; + node->free = __if_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/ittr.c b/libtlang/src/ittr.c new file mode 100644 index 0000000..10842fc --- /dev/null +++ b/libtlang/src/ittr.c @@ -0,0 +1,180 @@ +#include "tlang.h" +typedef struct +{ + tobject_t* o; + scope_t* s; +} __dict_ittr; +bool _dict_movenext(ittr_t* ittr) +{ + __dict_ittr* o = (__dict_ittr*)ittr->ptr; + + if(o->o->type != tdict) return false; + if(!dict_haskey(o->o->data.dict,"movenext")) return false; + tobject_t* j = dict_getkvp(o->o->data.dict,"movenext")->value; + if(j->type == texternalmethod || j->type == tinternalmethod) + { + list_tobject_t ob; + list_create(o->s->rt,&ob,0); + tobject_t* res= tobject_call(o->s,j,&ob); + bool res2=tobject_tobool(res); + tobject_freeifzero(res); + list_free(&ob); + return res2; + } + return false; +} +void _dict_free(ittr_t* ittr) +{ + __dict_ittr* o = (__dict_ittr*)ittr->ptr; + + if(o->o->type != tdict) return; + if(!dict_haskey(o->o->data.dict,"dispose")) return; + tobject_t* j = dict_getkvp(o->o->data.dict,"dispose")->value; + if(j->type == texternalmethod || j->type == tinternalmethod) + { + list_tobject_t ob; + list_create(o->s->rt,&ob,0); + tobject_t* res= tobject_call(o->s,j,&ob); + tobject_freeifzero(res); + list_free(&ob); + + } +} +tobject_t* _dict_getcurrent(ittr_t* ittr) +{ + __dict_ittr* o = (__dict_ittr*)ittr->ptr; + + if(o->o->type != tdict) return tobject_basic(o->s->rt,tnull); + if(!dict_haskey(o->o->data.dict,"getcurrent") && !dict_haskey(o->o->data.dict,"current")) return tobject_basic(o->s->rt,tnull); + tobject_t* res=NULL; + if(dict_haskey(o->o->data.dict,"getcurrent")){ + tobject_t* j = dict_getkvp(o->o->data.dict,"getcurrent")->value; + if(j->type == texternalmethod || j->type == tinternalmethod) + { + list_tobject_t ob; + list_create(o->s->rt,&ob,0); + res= tobject_call(o->s,j,&ob); + + list_free(&ob); + + } + } + if(res == NULL && dict_haskey(o->o->data.dict,"current")) + { + res = dict_getkvp(o->o->data.dict,"current")->value; + } + if(res != NULL) + { + return res; + } + return tobject_basic(o->s->rt,tnull); +} + +void _dict_reset(ittr_t* ittr) +{ + __dict_ittr* o = (__dict_ittr*)ittr->ptr; + + if(o->o->type != tdict) return; + if(!dict_haskey(o->o->data.dict,"reset")) return; + tobject_t* j = dict_getkvp(o->o->data.dict,"reset")->value; + if(j->type == texternalmethod || j->type == tinternalmethod) + { + list_tobject_t ob; + list_create(o->s->rt,&ob,0); + tobject_t* res= tobject_call(o->s,j,&ob); + tobject_freeifzero(res); + list_free(&ob); + + } +} +ittr_t* ittr_dict(tobject_t* obj,scope_t* s) +{ + ittr_t* i=NULL; + if(obj->type = tdict) + { + tobject_t* d=NULL; + if(dict_haskey(obj->data.dict,"getittr")) + { + kvp_t* kvp=dict_getkvp(obj->data.dict,"getittr"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t o; + list_create(s->rt,&o,0); + tobject_t* res=tobject_call(s,kvp->value,&o); + list_free(&o); + if(res->type == tdict) + { + d = res; + } else { + //sorry but we have to free you + tobject_freeifzero(res); + } + } + } + if(d == NULL) + { + if(dict_haskey(obj->data.dict,"ittr")) + { + kvp_t* kvp=dict_getkvp(obj->data.dict,"ittr"); + if(kvp->value->type == tdict) + { + d = kvp->value; + } + + } + } + + if(d != NULL) + { + __dict_ittr* ee = (__dict_ittr*)malloc(sizeof(__dict_ittr)); + ee->o = d; + ee->s = s; + i=ittr_create(ee,_dict_movenext,_dict_reset,_dict_getcurrent,_dict_free); + + } + } + return i; +} +ittr_t* ittr_obj(tobject_t* obj,scope_t* s) +{ + if(obj->type == tdict) + { + return ittr_dict(obj,s); + } + if(obj->type == tstring) + { + return string_ittr(s->rt,obj->data.string); + } + if(obj->type == tlist) + { + return list_ittr(s->rt,obj); + } + return NULL; +} +ittr_t* ittr_create(void* ptr,ittr_movenext_t movenext,ittr_reset_t reset,ittr_getcurrent_t current,ittr_reset_t free) +{ + ittr_t* ittr = (ittr_t*)malloc(sizeof(ittr_t)); + ittr->ptr=ptr; + ittr->movenext = movenext; + ittr->reset = reset; + ittr->getcurrent = current; + ittr->free = free; + return ittr; +} +void ittr_reset(ittr_t* ittr) +{ + ittr->reset(ittr); +} +tobject_t* ittr_current(ittr_t* ittr) +{ + return ittr->getcurrent(ittr); +} +bool ittr_movenext(ittr_t* ittr) +{ + return ittr->movenext(ittr); +} +void ittr_free(ittr_t* ittr) +{ + ittr->free(ittr); + free(ittr); +} \ No newline at end of file diff --git a/libtlang/src/leftshift.c b/libtlang/src/leftshift.c new file mode 100644 index 0000000..5890642 --- /dev/null +++ b/libtlang/src/leftshift.c @@ -0,0 +1,68 @@ +#include "tlang.h" +#include +extern void __node_two_free(node_t* n); +tobject_t* __leftshift_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"ls")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"ls"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = (int64_t)l->data.number << (int32_t)r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_leftshift_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = leftshiftnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __leftshift_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/lessthan.c b/libtlang/src/lessthan.c new file mode 100644 index 0000000..29cb843 --- /dev/null +++ b/libtlang/src/lessthan.c @@ -0,0 +1,67 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __lessthan_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"lt")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"lt"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.number < r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_lessthan_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = lessthannode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __lessthan_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/lessthanequal.c b/libtlang/src/lessthanequal.c new file mode 100644 index 0000000..bdcb653 --- /dev/null +++ b/libtlang/src/lessthanequal.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __lessthanequal_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"lte")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"lte"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.number <= r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_lessthanequal_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = lessthanequalnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __lessthanequal_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/lexer.c b/libtlang/src/lexer.c new file mode 100644 index 0000000..870e417 --- /dev/null +++ b/libtlang/src/lexer.c @@ -0,0 +1,275 @@ +#include "tlang.h" + +typedef struct +{ + lextokenlist_t* tokens; + string_t* curBuffer; + string_t* text; + int offset; + bool escaped; +} lexer_data_t; + + +char hexchr2num(char c) +{ + if(c >= '0' && c <= '9') + return c - '0'; + if(c >= 'A' && c<= 'F') + return (c+10) - 'A'; + if(c >= 'a' && c<= 'f') + return (c+10) - 'a'; + return -1; +} + +void lexer_flushbuilder(lexer_data_t* lexer) +{ + if(lexer->curBuffer->length > 0) + { + lextokenlist_additem(lexer->tokens,lexer->curBuffer); + string_clear(lexer->curBuffer); + } +} +int lexer_peek(lexer_data_t* lexer) +{ + if(lexer->offset < lexer->text->length) + { + return lexer->text->text[lexer->offset]; + } + return -1; +} +int lexer_read(lexer_data_t* lexer) +{ + int chr = lexer_peek(lexer); + lexer->offset++; + return chr; +} +int lexer_readchar(lexer_data_t* lexer) +{ + int read = lexer_read(lexer); + if(read == -1) return -1; + if(read == '\\') + { + lexer->escaped = true; + read = lexer_read(lexer); + if(read == 'x') + { + return (((int)hexchr2num((char)lexer_read(lexer)) & 0x0F) << 8) | ((int)hexchr2num((char)lexer_read(lexer)) & 0x0F); + + } + else if(read == 'n') + { + return '\n'; + } + else if(read == 't') + { + return '\t'; + } + else if(read == 'r') + { + return '\r'; + } + return read; + } + lexer->escaped=false; + return read; + } +void lexer_readchr(lexer_data_t* lexer) +{ + lexer_flushbuilder(lexer); + int res=lexer_readchar(lexer); + lexer->offset++; + if(res == -1) return; + lextokenlist_addchar(lexer->tokens,(char)res); +} +void lexer_readstring(lexer_data_t* lexer) +{ + lexer_flushbuilder(lexer); + while(true) + { + int r= lexer_readchar(lexer); + if(r == -1 || (!lexer->escaped && r=='\"' )) + { + break; + } + string_appendc(lexer->curBuffer,(char)r); + } + lextokenlist_addstring(lexer->tokens,lexer->curBuffer); + string_clear(lexer->curBuffer); +} +lextokenlist_t* lexer_lex(string_t* text) +{ + lexer_data_t lexer; + lexer.text = text; + lexer.curBuffer = string_create(); + lexer.offset=0; + lexer.tokens = lextokenlist_create(); + int read; + while((read=lexer_read(&lexer)) != -1) + { + int next = lexer_peek(&lexer); + switch(read) + { + case '\"': + lexer_readstring(&lexer); + break; + case '\'': + lexer_readchr(&lexer); + break; + case '#': + lexer_flushbuilder(&lexer); + while(true) + { + read = lexer_read(&lexer); + if(read == -1) goto end; + + if(read == '\n') + { + break; + } + + } + break; + case '/': + lexer_flushbuilder(&lexer); + if(next == '=') + { + lexer_read(&lexer); + string_appendc(lexer.curBuffer,(char)read); + string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + } + else if(next == '/') + { + lexer_read(&lexer); + + while(true) + { + read = lexer_read(&lexer); + if(read == -1) goto end; + + if(read == '\n') + { + break; + } + + } + } + else if(next == '*') + { + lexer_read(&lexer); + bool lastIsAstrick=false; + while(true) + { + read = lexer_read(&lexer); + if(read == -1) goto end; + if(read == '*') + { + lastIsAstrick=true; + continue; + } + else if(read == '/') + { + if(lastIsAstrick) + { + break; + } + } + lastIsAstrick=false; + } + } + else + { + string_appendc(lexer.curBuffer,(char)read); + //string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + } + + break; + case '^': + case '*': + case '%': + lexer_flushbuilder(&lexer); + if(next == '=') + { + lexer_read(&lexer); + string_appendc(lexer.curBuffer,(char)read); + string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + }else{ + string_appendc(lexer.curBuffer,(char)read); + //string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + } + break; + case '|': + case '&': + case '<': + case '>': + case '+': + case '-': + case '!': + lexer_flushbuilder(&lexer); + if(next == read || next == '=') + { + lexer_read(&lexer); + string_appendc(lexer.curBuffer,(char)read); + string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + }else{ + string_appendc(lexer.curBuffer,(char)read); + //string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + } + break; + case '{': + case '}': + case '[': + case ']': + case '(': + case ')': + case '.': + case '?': + case ':': + case ',': + case ';': + case '~': + lexer_flushbuilder(&lexer); + string_appendc(lexer.curBuffer,(char)read); + //string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + break; + case '=': + lexer_flushbuilder(&lexer); + if(next == read) + { + lexer_read(&lexer); + string_appendc(lexer.curBuffer,(char)read); + string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + }else{ + string_appendc(lexer.curBuffer,(char)read); + //string_appendc(lexer.curBuffer,(char)next); + lexer_flushbuilder(&lexer); + + } + break; + case ' ': + case '\n': + case '\t': + case '\r': + lexer_flushbuilder(&lexer); + break; + default: + string_appendc(lexer.curBuffer,(char)read); + break; + } + } + end: lexer_flushbuilder(&lexer); + string_free(lexer.curBuffer); + return lexer.tokens; +} \ No newline at end of file diff --git a/libtlang/src/lextokenlist.c b/libtlang/src/lextokenlist.c new file mode 100644 index 0000000..8859c85 --- /dev/null +++ b/libtlang/src/lextokenlist.c @@ -0,0 +1,63 @@ +#include "tlang.h" +lextokenlist_t* lextokenlist_create() +{ + lextokenlist_t* list = (lextokenlist_t*)malloc(sizeof(lextokenlist_t)); + list->capacity = 128; + list->count = 0; + list->tokens = (lextoken_t*)malloc(sizeof(lextoken_t) * list->capacity); + return list; +} +void lextokenlist_ensure(lextokenlist_t* a) +{ + if(a->count + 1 > a->capacity) + { + a->capacity = a->count + 128; + a->tokens=(lextoken_t*)realloc(a->tokens,a->capacity * sizeof(lextoken_t)); + + } +} + + +void lextokenlist_addstring(lextokenlist_t* ls,string_t* str) +{ + ls->count++; + lextokenlist_ensure(ls); + lextoken_t* lt=ls->tokens+(ls->count-1); + lt->isString=true; + lt->isChar = false; + lt->text = string_dups(str); +} +void lextokenlist_additem(lextokenlist_t* ls,string_t* str) +{ + ls->count++; + lextokenlist_ensure(ls); + lextoken_t* lt=ls->tokens+(ls->count-1); + lt->isString=false; + lt->isChar = false; + lt->text = string_dups(str); +} +void lextokenlist_addchar(lextokenlist_t* ls,char c) +{ + ls->count++; + lextokenlist_ensure(ls); + lextoken_t* lt=ls->tokens+(ls->count-1); + lt->isString=false; + lt->isChar = true; + lt->text = string_create(); + string_appendc(lt->text,c); +} +void lextokenlist_free(lextokenlist_t* t) +{ + int i; + for(i=0;icount;i++) + { + string_free(t->tokens[i].text); + } + free(t->tokens); + free(t); +} +bool lextoken_isitem(lextoken_t t,char* c) +{ + if(t.isChar || t.isString) return false; + return string_samep(t.text,c); +} \ No newline at end of file diff --git a/libtlang/src/listittr.c b/libtlang/src/listittr.c new file mode 100644 index 0000000..50486ba --- /dev/null +++ b/libtlang/src/listittr.c @@ -0,0 +1,51 @@ +#include "tlang.h" + +typedef struct { + tobject_t* ls; + int pos; + tobject_t* current; + runtime_t* rt; +} listittr_t; +void _list_free(ittr_t* ittr) +{ + listittr_t* ittrData = (listittr_t*)ittr->ptr; + + tobject_rmref(ittrData->current); + tobject_rmref(ittrData->ls); + free(ittrData); +} +tobject_t* _list_getcurrent(ittr_t* ittr) +{ + listittr_t* ittrData = (listittr_t*)ittr->ptr; + return ittrData->current; +} +void _list_reset(ittr_t* ittr) +{ + listittr_t* ittrData = (listittr_t*)ittr->ptr; + ittrData->pos=-1; +} +bool _list_movenext(ittr_t* ittr) +{ + listittr_t* ittrData = (listittr_t*)ittr->ptr; + ittrData->pos++; + if(ittrData->posls->data.list.length) + { + tobject_rmref(ittrData->current); + ittrData->current = ittrData->ls->data.list.items[ittrData->pos]; + tobject_addref(ittrData->current); + return true; + } + return false; +} +ittr_t* list_ittr(runtime_t* rt,tobject_t* ls) +{ + listittr_t* myittr=(listittr_t*)malloc(sizeof(listittr_t)); + myittr->pos = -1; + myittr->rt = rt; + myittr->ls = ls; + tobject_addref(myittr->ls); + myittr->rt = rt; + myittr->current = tobject_basic(rt,tnull); + tobject_addref(myittr->current); + return ittr_create(myittr,_list_movenext,_list_reset,_list_getcurrent,_list_free); +} \ No newline at end of file diff --git a/libtlang/src/logicaland.c b/libtlang/src/logicaland.c new file mode 100644 index 0000000..0c302f5 --- /dev/null +++ b/libtlang/src/logicaland.c @@ -0,0 +1,67 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __logicaland_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"land")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"land"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(out == NULL) + { out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = tobject_tobool(l) && tobject_tobool(r); + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_logicaland_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = logicalandnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __logicaland_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/logicalor.c b/libtlang/src/logicalor.c new file mode 100644 index 0000000..eb08080 --- /dev/null +++ b/libtlang/src/logicalor.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __logicalor_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"lor")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"lor"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(out == NULL) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = tobject_tobool(l) || tobject_tobool(r); + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_logicalor_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = logicalornode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __logicalor_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/main.c b/libtlang/src/main.c new file mode 100644 index 0000000..e060e3c --- /dev/null +++ b/libtlang/src/main.c @@ -0,0 +1,94 @@ +#if defined(HW_RVL) +#include +#include +#include +#include +#include +#include "tlang.h" +static void *xfb = NULL; +static GXRModeObj *rmode = NULL; +extern void __reg_wpad(runtime_t* rt); +void __reg_wii(runtime_t* rt) +{ + __reg_wpad(rt); +} +//--------------------------------------------------------------------------------- +int main(int argc, char **argv) { +//--------------------------------------------------------------------------------- + + // Initialise the video system + VIDEO_Init(); + + fatInitDefault(); + // Obtain the preferred video mode from the system + // This will correspond to the settings in the Wii menu + rmode = VIDEO_GetPreferredMode(NULL); + + // Allocate memory for the display in the uncached region + xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); + + // Initialise the console, required for printf + console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); + + // Set up the video registers with the chosen mode + VIDEO_Configure(rmode); + + // Tell the video hardware where our display memory is + VIDEO_SetNextFramebuffer(xfb); + + // Make the display visible + VIDEO_SetBlack(FALSE); + + // Flush the video register changes to the hardware + VIDEO_Flush(); + + // Wait for Video setup to complete + VIDEO_WaitVSync(); + if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync(); + + + // The console understands VT terminal escape codes + // This positions the cursor on row 2, column 0 + // we can use variables for this with format codes too + // e.g. printf ("\x1b[%d;%dH", row, column ); + printf("\x1b[2;0H"); + FILE* f = fopen("app.tlang","r"); + + if(f) + { + runtime_t* rt=runtime_init(); + runtime_register(rt,__reg_wii); + string_t* s = string_create(); + string_read(s,f,fread); + runtime_load(rt,s); + string_free(s); + + fclose(f); + + tobject_t* o= rt->program->execute(rt->program,rt->globals); + tobject_freeifzero(o); + runtime_free(rt); + }else{ + printf("\n\n\n\n\tNo Tlang Script found\n"); + WPAD_Init(); + while(1) { + + // Call WPAD_ScanPads each loop, this reads the latest controller states + WPAD_ScanPads(); + + // WPAD_ButtonsDown tells us which buttons were pressed in this loop + // this is a "one shot" state which will not fire again until the button has been released + u32 pressed = WPAD_ButtonsDown(0); + + // We return to the launcher application via exit + if ( pressed & WPAD_BUTTON_HOME ) exit(0); + + // Wait for the next frame + VIDEO_WaitVSync(); + } + } + + + return 0; +} +#endif \ No newline at end of file diff --git a/libtlang/src/methodcallnode.c b/libtlang/src/methodcallnode.c new file mode 100644 index 0000000..ce2a718 --- /dev/null +++ b/libtlang/src/methodcallnode.c @@ -0,0 +1,340 @@ +#include "tlang.h" +#include +#include +tobject_t* __node_method_call_execute(node_t* n,scope_t* s) +{ + node_t* parent=n->data.variable_node.parent; + tobject_t* parent_obj = parent->execute(parent,s); + tobject_t* o=NULL; + char* name = string_dupp(n->data.function_call_node.name); + + if(parent_obj->type == tdict && dict_haskey(parent_obj->data.dict,name)) + { + + tobject_t* fname = dict_getkvp(parent_obj->data.dict,name)->value; + if(fname->type == texternalmethod || fname->type == tinternalmethod) + { + list_tobject_t args; + list_create(s->rt,&args,0); + + int i; + for(i = 0;i< n->data.function_call_node.count;i++) + { + node_t* a =n->data.function_call_node.args[i]; + list_add(&args,a->execute(a,s)); + } + + o = tobject_call(s,fname,&args); + o->count++; //ensures this object is not freed when freeing list + list_free(&args); + o->count--; + } + } + if(parent_obj->type == tbool) + { + + if(strcmp(name,"toChar") == 0) + { + o = tobject_create(s->rt); + o->type = tchar; + o->data.chr= parent_obj->data.boolean ? 'y' : 'n'; + } + if(strcmp(name,'toNumber') == 0) + { + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number= parent_obj->data.boolean ? 1 : 0; + } + + } + if(parent_obj->type == tchar) + { + if(strcmp(name,"toUpper") == 0) + { + o = tobject_create(s->rt); + o->type = tchar; + o->data.chr=(char)toupper(parent_obj->data.chr); + } + if(strcmp(name,"toLower") == 0) + { + o = tobject_create(s->rt); + o->type = tchar; + o->data.chr=(char)tolower(parent_obj->data.chr); + } + if(strcmp(name,"toNumber") == 0) + { + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = parent_obj->data.chr; + } + } + if(parent_obj->type == tstring) + { + + int argcount = n->data.function_call_node.count; + node_t** args=n->data.function_call_node.args; + if(strcmp(name,"replace") == 0) + { + if(argcount >= 2) + { + tobject_t* a0=args[0]->execute(args[0],s); + tobject_t* a1=args[1]->execute(args[1],s); + string_t* s1=tobject_tostring(s,a0); + string_t* s2=tobject_tostring(s,a1); + + string_t* s3=string_replace(parent_obj->data.string,s1,s2); + string_free(s1); + string_free(s2); + o=tobject_string(s->rt,s3); + } + } + if(strcmp(name,"split") == 0) + { + if(argcount >= 1) + { + + tobject_t* sOn=args[0]->execute(args[0],s); + + string_t* splitOn=tobject_tostring(s,sOn); + int count = 2147483647; + + bool splitOp=false; + + if(argcount >= 2) + { + tobject_t* sOn=args[1]->execute(args[1],s); + + splitOp = tobject_tobool(sOn); + //tobject_freeifzero(sOn); + } + if(argcount >= 3) + { + tobject_t* _count=args[2]->execute(args[2],s); + if(_count->type == tnumber) + { + count = (int)_count->data.number; + } + //tobject_freeifzero(_count); + } + + tobject_t* s3=string_split(s->rt,parent_obj->data.string,splitOn,count,!splitOp); + string_free(splitOn); + + o=s3; + } + } + if(strcmp(name,"startsWith") == 0) + { + if(argcount >= 1) + { + tobject_t* sOn=args[0]->execute(args[0],s); + + string_t* starts=tobject_tostring(s,sOn); + o = tobject_bool(s->rt,string_startswith(parent_obj->data.string,starts)); + string_free(starts); + // tobject_freeifzero(sOn); + } + } + if(strcmp(name,"endsWith") == 0) + { + if(argcount >= 1) + { + tobject_t* sOn=args[0]->execute(args[0],s); + + string_t* starts=tobject_tostring(s,sOn); + o = tobject_bool(s->rt,string_endswith(parent_obj->data.string,starts)); + if(o->data.boolean) + { + int i = 0; + } + string_free(starts); + // tobject_freeifzero(sOn); + } + } + if(strcmp(name,"trimStart") == 0) + { + char c = ' '; + if(argcount >= 1) + { + tobject_t* sOn=args[0]->execute(args[0],s); + if(sOn->type == tchar) + { + c = sOn->data.chr; + } + tobject_freeifzero(sOn); + + } + o = tobject_string(s->rt,string_trimstarts(parent_obj->data.string,c)); + + + } + if(strcmp(name,"trimEnd") == 0) + { + char c = ' '; + if(argcount >= 1) + { + tobject_t* sOn=args[0]->execute(args[0],s); + if(sOn->type == tchar) + { + c = sOn->data.chr; + } + tobject_freeifzero(sOn); + + } + o = tobject_string(s->rt,string_trimends(parent_obj->data.string,c)); + + + } + } + + if(parent_obj->type == tnumber) + { + if(strcmp(name,"abs") == 0) + { + double val=fabs(parent_obj->data.number); + + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = val; + } + if(strcmp(name,"int") == 0) + { + int64_t val=(int64_t)parent_obj->data.number; + + o = tobject_create(s->rt); + o->type = tnumber; + o->data.number = val; + } + if(strcmp(name,"toChar") == 0) + { + o = tobject_create(s->rt); + o->type = tchar; + o->data.chr = (char)parent_obj->data.number; + } + } + + if(parent_obj->type == tlist) + { + + int argcount = n->data.function_call_node.count; + node_t** args=n->data.function_call_node.args; + + if(strcmp(name,"add") == 0) + { + if(argcount >= 1) + { + tobject_t* o2=args[0]->execute(args[0],s); + + list_add(&parent_obj->data.list,o2); + o=parent_obj; + } + } + if(strcmp(name,"remove") == 0) + { + if(argcount >= 1) + { + tobject_t* o2=args[0]->execute(args[0],s); + + list_remove(&parent_obj->data.list,o2); + o=parent_obj; + } + } + if(strcmp(name,"at")==0) + { + if(argcount >= 1) + { + tobject_t* o2=args[0]->execute(args[0],s); + if(o2->type != tnumber) + { + int index=(int)o2->data.number; + if(index < parent_obj->data.list.length) + { + o = parent_obj->data.list.items[index]; + } + } + tobject_freeifzero(o2); + + } + } + if(strcmp(name,"setAt") == 0) + { + tobject_t* val = NULL; + if(argcount >= 2) + { + val = args[1]->execute(args[1],val); + o=val; + } + else if(argcount == 1) + { + val = tobject_basic(s->rt,tnull); + o = val; + } + if(argcount >= 1) + { + tobject_t* o2=args[0]->execute(args[0],s); + if(o2->type != tnumber) + { + int index=(int)o2->data.number; + if(index < parent_obj->data.list.length) + { + list_set(&parent_obj->data.list,index,o2); + //o = parent_obj->data.list.items[index]; + } + } + tobject_freeifzero(o2); + } + + } + } + if(o == NULL) + { + if(strcmp(name,"toString") == 0) + { + o = tobject_string(s->rt,tobject_tostring(s,parent_obj)); + } else { + o = tobject_basic(s->rt,tundef); + } + } + free(name); + o->count++; + tobject_freeifzero(parent_obj); + o->count--; + return o; +} +void __node_method_call_free(node_t* node) +{ + node_t** args= node->data.function_call_node.args; + int i; + for(i=0;idata.function_call_node.count;i++) + { + node_free(args[i]); + } + free(args); + node_free(node->data.function_call_node.parent); + string_free(node->data.function_call_node.name); +} + +node_t* node_method_call_create(node_t* parent,string_t* name) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = methodcallnode; + n->data.function_call_node.count = 0; + n->data.function_call_node.name = string_dups(name); + n->data.function_call_node.capacity=64; + n->data.function_call_node.args=(node_t**)malloc(sizeof(node_t*) * n->data.scope_node.capacity); + n->data.function_call_node.parent = parent; + n->execute = __node_method_call_execute; + n->free = __node_method_call_free; + + return n; +} +void node_method_call_add(node_t* fc,node_t* arg) +{ + if(fc->data.function_call_node.count + 128 >fc->data.scope_node.capacity) + { + fc->data.function_call_node.capacity = fc->data.function_call_node.count + 128; + fc->data.function_call_node.args=(node_t**)realloc(fc->data.function_call_node.args,fc->data.function_call_node.capacity * sizeof(node_t*)); + + } + fc->data.function_call_node.args[fc->data.function_call_node.count++]=arg; +} \ No newline at end of file diff --git a/libtlang/src/modulonode.c b/libtlang/src/modulonode.c new file mode 100644 index 0000000..7a35628 --- /dev/null +++ b/libtlang/src/modulonode.c @@ -0,0 +1,69 @@ +#include "tlang.h" +#include + +extern void __node_two_free(node_t* n); +tobject_t* __modulo_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"mod")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"mod"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + + out->data.number =fmod(l->data.number,r->data.number); + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_modulo_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = modulonode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __modulo_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/multiplynode.c b/libtlang/src/multiplynode.c new file mode 100644 index 0000000..94fd0d1 --- /dev/null +++ b/libtlang/src/multiplynode.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __multiply_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"times")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"times"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = l->data.number * r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_multiply_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = multiplynode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __multiply_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/neg.c b/libtlang/src/neg.c new file mode 100644 index 0000000..0a271b8 --- /dev/null +++ b/libtlang/src/neg.c @@ -0,0 +1,66 @@ +#include "tlang.h" + +extern void __node_single_free(node_t* n); +tobject_t* __neg_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + node_t* left = n->data.single_node_node; + tobject_t* l=left->execute(left,sc); + + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"neg")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"neg"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + + list_free(&ls); + + freeleftifzero = false; + } + } + } + if(ltype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = -(l->data.number); + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_negative_create(node_t* left) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = negnode; + node->data.single_node_node = left; + + node->execute = __neg_node_exec; + node->free = __node_single_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/neq.c b/libtlang/src/neq.c new file mode 100644 index 0000000..39cc0a4 --- /dev/null +++ b/libtlang/src/neq.c @@ -0,0 +1,104 @@ +#include "tlang.h" +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __neq_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"neq")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"neq"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } else { + if(r->type == tdict) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l != r; + + } + } + }else { + if(r->type == tdict) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l != r; + + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.number != r->data.number; + + } + if(ltype == tbool && rtype == tbool) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.boolean != r->data.boolean; + + } + if(ltype == tstring && rtype == tstring) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean =!string_sames(l->data.string,r->data.string); + } + if(ltype == tchar && rtype == tchar) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = l->data.chr != r->data.chr; + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_neq_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = notequalsnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __neq_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/net.c b/libtlang/src/net.c new file mode 100644 index 0000000..8ed3625 --- /dev/null +++ b/libtlang/src/net.c @@ -0,0 +1,137 @@ +#include "tlang.h" + +#if defined(USE_NETWORK) + #if defined(GEKKO) + #if defined(HW_RVL) && defined(USE_WIISOCKETS_ON_WII) + #define USE_NORMAL_SOCKETS + #else + #define USE_NETWORK_H + #include + #define read net_read + #define write net_write + #define connect net_connect + #define listen net_listen + #define bind net_bind + #define close net_close + #define accept net_accept + #define socket net_socket + char localip[16] = {0}; + char gateway[16] = {0}; + char netmask[16] = {0}; + + #endif + #else + #define USE_NORMAL_SOCKETS + #endif + + #if defined(USE_NORMAL_SOCKETS) + #include + #include + #include + #include + #include + + #endif + bool networking_init() + { + #if defined(HW_RVL) && defined(USE_WIISOCKETS_ON_WII) + return wiisocket_init() >= 0; + #elif defined(USE_NETWORK_H) + return if_config ( localip, netmask, gateway, TRUE, 20) >= 0; + #else + + #endif + return true; + } + typedef struct + { + int fd; + } myfd_t; + + int64_t __fd_read(stream_t* strm,void* buff,int64_t len) + { + myfd_t* j = (myfd_t*)strm->ptr; + return (ssize_t) read(j->fd,buff,len); + } + void __fd_write(stream_t* strm,void* buff,int64_t len) + { + myfd_t* j = (myfd_t*)strm->ptr; + if(write(j->fd,buff,len) == -1) + { + printf("FAILED TO SEND\n"); + } + } + void __fd_close(stream_t* strm) + { + myfd_t* j = (myfd_t*)strm->ptr; + close(j->fd); + free(j); + } + void __fd_flush(stream_t* strm) + { + myfd_t* j = (myfd_t*)strm->ptr; + //flush(j->fd); + + } + stream_t* create_netfd_strm(int fd) + { + myfd_t* _fd = (myfd_t*)malloc(sizeof(myfd_t)); + _fd->fd = fd; + return stream_create(_fd,__fd_read,__fd_write,NULL,NULL,NULL,stream_true,stream_true,stream_false,__fd_close,__fd_flush); + } + + + stream_t* create_tcp_client(string_t* ipOrFqdn,int port) + { + int fd= socket(AF_INET,SOCK_STREAM,0); + if(fd == -1) + {return NULL;} + struct sockaddr_in addr; + memset(&addr,0,sizeof(struct sockaddr_in)); + char* ipAddress = string_dupp(ipOrFqdn); + if (inet_aton(ipAddress,&addr.sin_addr) <=0) + { + Global_Mutex_Lock(); + struct hostent* host=gethostbyname(ipAddress); + if(host != NULL) + { + memcpy(&addr.sin_addr,host->h_addr_list[0],sizeof(struct sockaddr_in)); + } + Global_Mutex_Unlock(); + } + free(ipAddress); + addr.sin_port = htons((uint16_t)port); + addr.sin_family = AF_INET; + if(connect(fd,&addr,sizeof(struct sockaddr_in)) != 0) + { + return NULL; + } + return create_netfd_strm(fd); + } + + tobject_t* __net_tcpclient(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >1 && args->items[0]->type == tstring && args->items[1]->type == tnumber) + { + stream_t* strm = create_tcp_client(args->items[0]->data.string,(int)args->items[1]->data.number); + if(strm == NULL) + { + return tobject_basic(rt,tnull); + } + return tobject_fromstream(rt,strm); + } + return tobject_basic(rt,tnull); + } + + void runtime_register_network(runtime_t* rt) + { + networking_init(); + tobject_t* __network = tobject_create(rt); + __network->type = tdict; + __network->data.dict = dict_create(); + runtime_create_method_on_dict(__network->data.dict,"tcpclient",rt,NULL,__net_tcpclient,NULL); + rt->globals->setvariable(rt->globals,"net",__network); + + } + +#endif \ No newline at end of file diff --git a/libtlang/src/nodetwo.c b/libtlang/src/nodetwo.c new file mode 100644 index 0000000..7db795c --- /dev/null +++ b/libtlang/src/nodetwo.c @@ -0,0 +1,7 @@ +#include "tlang.h" + +void __node_two_free(node_t* n) +{ + node_free(n->data.two_node_node.left); + node_free(n->data.two_node_node.right); +} \ No newline at end of file diff --git a/libtlang/src/not.c b/libtlang/src/not.c new file mode 100644 index 0000000..5f5f6c1 --- /dev/null +++ b/libtlang/src/not.c @@ -0,0 +1,70 @@ +#include "tlang.h" + +void __node_single_free(node_t* n) +{ + node_free(n->data.single_node_node); +} +tobject_t* __not_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.single_node_node; + tobject_t* l=left->execute(left,sc); + + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"not")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"not"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + + list_free(&ls); + + freeleftifzero = false; + } + } + } + if(out == NULL) + { + out = tobject_create(sc->rt); + out->type = tbool; + out->data.boolean = !tobject_tobool(l); + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_not_create(node_t* left) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = notnode; + node->data.single_node_node = left; + + node->execute = __not_node_exec; + node->free = __node_single_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/parser.c b/libtlang/src/parser.c new file mode 100644 index 0000000..0114268 --- /dev/null +++ b/libtlang/src/parser.c @@ -0,0 +1,810 @@ +#include "tlang.h" +typedef struct +{ + int i; + runtime_t* rt; + lextokenlist_t* ls; +} parser_t; + +void liststr_create(list_string_t* l) +{ + l->capacity=128; + l->count = 0; + l->strings = (tobject_t**)malloc(l->capacity * sizeof(string_t*)); +} +void liststr_add(list_string_t* l,string_t* s) +{ + + if(l->count + 1 > l->capacity) + { + l->capacity = l->count + 128; + l->strings=(tobject_t**)realloc(l->strings,l->capacity * sizeof(string_t*)); + + } + l->strings[l->count++] = string_dups(s); +} + +node_t* parse_expression(parser_t* p); +node_t* parse_assignable(parser_t* p); +node_t* parse_lor(parser_t* p); +node_t* parse_land(parser_t* p); +node_t* parse_bor(parser_t* p); +node_t* parse_xor(parser_t* p); +node_t* parse_band(parser_t* p); +node_t* parse_eq(parser_t* p); +node_t* parse_ro(parser_t* p); +node_t* parse_shift(parser_t* p); +node_t* parse_sum(parser_t* p); +node_t* parse_factor(parser_t* p); +node_t* parse_value(parser_t* p); + +node_t* __parser_parse(parser_t* p,bool isFirst) +{ + if(p->i>= p->ls->count) return node_const_number_create(0); + if(lextoken_isitem(p->ls->tokens[p->i],"{") || isFirst) + { + if(!isFirst) + p->i++; + node_t* scope = node_scope_create(isFirst); + while(p->ils->count && !lextoken_isitem(p->ls->tokens[p->i],"}")) + { + node_scope_append(scope,parse_expression(p)); + //scopeNode.Body.Add(ParseExpression(tokens,ref i)); + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i],";")) + { + p->i++; + } + + } + + p->i++; + return scope; + } + return parse_assignable(p); +} +node_t* parse_assignable(parser_t* p) +{ + node_t* myExpression = parse_lor(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "=") || lextoken_isitem(p->ls->tokens[p->i], "+=") || lextoken_isitem(p->ls->tokens[p->i], "-=") || lextoken_isitem(p->ls->tokens[p->i], "*=") || lextoken_isitem(p->ls->tokens[p->i], "/=") || lextoken_isitem(p->ls->tokens[p->i], "%=") || lextoken_isitem(p->ls->tokens[p->i], "|=") || lextoken_isitem(p->ls->tokens[p->i], "&=") || lextoken_isitem(p->ls->tokens[p->i], "^=") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,parse_lor(p),true); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "+=")){ + p->i++; + + myExpression = node_setvariable_create(myExpression,node_add_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "-=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_sub_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "*=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_multiply_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "/=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_divide_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "%=")){ + p->i++; + + myExpression = node_setvariable_create(myExpression,node_modulo_create(myExpression,parse_lor(p)),false); + + } + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "|=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_bitwiseor_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "&=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_bitwiseand_create(myExpression,parse_lor(p)),false); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "^=")){ + p->i++; + myExpression = node_setvariable_create(myExpression,node_xor_create(myExpression,parse_lor(p)),false); + + } + + + + } + + return myExpression; +} +node_t* parse_lor(parser_t* p) +{ + node_t* myExpression = parse_land(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "||") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "||")){ + p->i++; + myExpression = node_logicalor_create(myExpression,parse_land(p)); + + } + + + + + } + + + return myExpression; +} +node_t* parse_land(parser_t* p) +{ + node_t* myExpression = parse_bor(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "&&") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "&&")){ + p->i++; + myExpression = node_logicaland_create(myExpression,parse_bor(p)); + + } + + + + + } + + return myExpression; +} +node_t* parse_bor(parser_t* p) +{ + node_t* myExpression = parse_xor(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "|") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "|")){ + p->i++; + myExpression = node_bitwiseor_create(myExpression,parse_xor(p)); + + } + + + + + } + + return myExpression; +} +node_t* parse_xor(parser_t* p) +{ + node_t* myExpression = parse_band(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "^") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "^")){ + p->i++; + myExpression = node_xor_create(myExpression,parse_band(p)); + + } + + + + + } + return myExpression; +} +node_t* parse_band(parser_t* p) +{ + node_t* myExpression = parse_eq(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "&") )) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "&")){ + p->i++; + myExpression = node_bitwiseand_create(myExpression,parse_eq(p)); + + } + + + + + } + return myExpression; +} +node_t* parse_eq(parser_t* p) +{ + node_t* myExpression = parse_ro(p); + + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "==") || lextoken_isitem(p->ls->tokens[p->i], "!="))) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "==")){ + p->i++; + myExpression = node_eq_create(myExpression,parse_ro(p)); + + } + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "!=")) + { + p->i++; + myExpression = node_neq_create(myExpression,parse_ro(p)); + } + + + } + return myExpression; + return myExpression; +} +node_t* parse_ro(parser_t* p) +{ + node_t* myExpression = parse_shift(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "<") || lextoken_isitem(p->ls->tokens[p->i], ">") || lextoken_isitem(p->ls->tokens[p->i], "<=") || lextoken_isitem(p->ls->tokens[p->i], ">="))) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "<")){ + p->i++; + myExpression = node_lessthan_create(myExpression,parse_shift(p)); + + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "<=")){ + p->i++; + myExpression = node_lessthanequal_create(myExpression,parse_shift(p)); + + } + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], ">")) + { + p->i++; + myExpression = node_greaterthan_create(myExpression,parse_shift(p)); + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], ">=")) + { + p->i++; + myExpression = node_greaterthanequal_create(myExpression,parse_shift(p)); + } + + + } + + return myExpression; +} +node_t* parse_shift(parser_t* p) +{ + node_t* myExpression = parse_sum(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "<<") || lextoken_isitem(p->ls->tokens[p->i], ">>"))) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "<<")){ + p->i++; + myExpression = node_leftshift_create(myExpression,parse_sum(p)); + + } + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], ">>")) + { + p->i++; + myExpression = node_rightshift_create(myExpression,parse_sum(p)); + } + + + } + + return myExpression; +} +node_t* parse_sum(parser_t* p) +{ + + node_t* myExpression = parse_factor(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "+") || lextoken_isitem(p->ls->tokens[p->i], "-"))) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "+")){ + p->i++; + myExpression = node_add_create(myExpression,parse_factor(p)); + + } + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "-")) + { + p->i++; + myExpression = node_sub_create(myExpression,parse_factor(p)); + } + + + } + return myExpression; +} +node_t* parse_factor(parser_t* p) +{ + node_t* myExpression = parse_value(p); + while(p->ils->count && (lextoken_isitem(p->ls->tokens[p->i], "*") || lextoken_isitem(p->ls->tokens[p->i], "/") || lextoken_isitem(p->ls->tokens[p->i], "%"))) + { + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "*")){ + p->i++; + myExpression = node_multiply_create(myExpression,parse_value(p)); + + } + + + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "/")) + { + p->i++; + myExpression = node_divide_create(myExpression,parse_value(p)); + } + if(p->ils->count && lextoken_isitem(p->ls->tokens[p->i], "%")) + { + p->i++; + myExpression = node_modulo_create(myExpression,parse_value(p)); + } + + + } + return myExpression; +} +node_t* parse_value(parser_t* p) +{ + + if(p->i >= p->ls->count) return node_const_number_create(0); + lextoken_t token=p->ls->tokens[p->i++]; //6 + if(token.isString) return node_const_string_create(token.text); + if(token.isChar) { + char c=' '; + if(token.text->length > 0) + { + c = token.text->text[0]; + } + return node_const_char_create(c); + } + if(string_samep(token.text,"-")) + { + + return node_negative_create(parse_value(p)); + } + if(string_samep(token.text,"~")) + { + //printf("GOT HERE\n"); + return node_bitwisenot_create(parse_value(p)); + } + if(string_samep(token.text,"!")) + { + return node_not_create(parse_value(p)); + } + int64_t num; + //for now try parsing number + + if(string_islong(token.text,&num)) + { + + if(p->ils->count && string_samep(p->ls->tokens[p->i].text, ".") && !p->ls->tokens[p->i].isChar && !p->ls->tokens[p->i].isString) + { + p->i++; + string_t* _s = string_create(); + string_appendn(_s,num); + string_appendc(_s,'.'); + string_appends(_s,p->ls->tokens[p->i].text); + double dbl; + if(string_isnumber(_s,&dbl)) + { + string_free(_s); + return node_const_number_create(dbl); + } + string_free(_s); + }else{ + return node_const_number_create(num); + } + } else { + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"(")) + { + //funcs and stuff + if(string_samep(token.text,"func")) + { + list_string_t strs; + liststr_create(&strs); + p->i++; + while(p->ils->count && !string_samep(p->ls->tokens[p->i].text,")")) + { + if(string_samep(p->ls->tokens[p->i].text,",")) {p->i++;continue;} + if(p->ils->count) + { + node_t* n = parse_expression(p); + if(n->type == getvariablenode) + { + liststr_add(&strs,n->data.variable_node.name); + } + node_free(n); + } + } + p->i++; + return node_closure_create(strs,parse_expression(p)); + + } + else if(string_samep(token.text,"for")) + { + p->i++; + node_t* init = NULL; + node_t* condition = NULL; + node_t* inc = NULL; + node_t* body = NULL; + //tokens[i].Text != ")" + + if(p->ils->count && (!string_samep(p->ls->tokens[p->i].text,")") || !string_samep(p->ls->tokens[p->i].text,";"))) + { + init = parse_expression(p); + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,";")) + p->i++; + if(p->ils->count && (!string_samep(p->ls->tokens[p->i].text,")") || !string_samep(p->ls->tokens[p->i].text,";"))) + { + condition = parse_expression(p); + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,";")) + p->i++; + if(p->ils->count && (!string_samep(p->ls->tokens[p->i].text,")") || !string_samep(p->ls->tokens[p->i].text,";"))) + { + inc = parse_expression(p); + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,")")) + p->i++; + if(p->ils->count) + body = parse_expression(p); + + return node_for_loop_create(init,condition,inc,body); + } + else if(string_samep(token.text,"each")) + { + p->i++; + node_t* ls = parse_expression(p); + string_t* _s02 = string_create(); + string_appendp(_s02,"item"); + node_t* name = node_getvariable_create(_s02); + string_free(_s02); + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,":")) + { + p->i++; + if(ls->type == getvariablenode) + { + node_free(name); + name = ls; + } + + ls = parse_expression(p); + } + p->i++; + if(p->ils->count) + { + return node_eachloop_create(name,ls,parse_expression(p)); + } + } + else if(string_samep(token.text,"while") || string_samep(token.text,"do")) + { + p->i++; + node_t* condition = parse_expression(p); + p->i++; + node_t* yes = NULL; + if(p->ils->count && !string_samep(p->ls->tokens[p->i].text,";")) + { + yes = parse_expression(p); + }else{ + yes = node_const_undef_create(); + } + return node_while_loop_create(condition,yes,string_samep(token.text,"do")); + } + else if(string_samep(token.text,"if")) + { + p->i++; + node_t* condition = parse_expression(p); + p->i++; + node_t* yes = NULL; + node_t* no = NULL; + if(p->i < p->ls->count && !string_samep(p->ls->tokens[p->i].text,"else")) + { + yes = parse_expression(p); + }else{ + yes = node_const_undef_create(); + } + if(p->i < p->ls->count && string_samep(p->ls->tokens[p->i].text,"else")) + { + p->i++; + no = parse_expression(p); + } + else { + no = node_const_undef_create(); + } + return node_if_create(condition,yes,no); + + } + else { + p->i++; + node_t* ret = node_function_call_create(token.text); + while(p->ils->count && !string_samep(p->ls->tokens[p->i].text,")") ) + { + + if(string_samep(p->ls->tokens[p->i].text,",")) {p->i++;continue;} + + if(p->ils->count) + { + node_function_call_add(ret,parse_expression(p)); + + } + + } + p->i++; + while(p->ils->count && string_samep(p->ls->tokens[p->i].text,"[")) + { + + p->i++; + node_t* arg = parse_expression(p); + p->i++; + ret = node_getarray_create(ret,arg); + + + } + + while(p->ils->count && string_samep(p->ls->tokens[p->i].text,".")) + { + p->i++; + + lextoken_t token2 = p->ls->tokens[p->i++]; + + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"(")) + { + p->i++; + ret = node_method_call_create(ret,token2.text); + //args = new List(); + //function call baby + while(p->ils->count && string_samep(p->ls->tokens[p->i].text,")")) + { + + if(string_samep(p->ls->tokens[p->i].text,",")) {p->i++;continue;} + + if(p->ils->count) + { + node_method_call_add(ret,parse_expression(p)); + + } + + } + p->i++; + + } + else if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"[")) + { + p->i++; + node_t* arg = parse_expression(p); + p->i++; + ret = node_getarray_create(ret,arg); + + } + else{ + ret = node_getmember_create(token2.text,ret); + + } + + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"++")) + { + /* var mgvn = ret as GetVariableValue; + i++; + if(mgvn != null) + { + return new PostfixIncrementVariableNode(mgvn); + } + */ + p->i++; + if(ret->type == getvariablenode || ret->type == getarraynode || ret->type == getmembernode) + { + ret= node_postfix_increment_create(ret); + } + + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"--")) + { + /* var mgvn = ret as GetVariableValue; + i++; + if(mgvn != null) + { + return new PostfixIncrementVariableNode(mgvn); + } + */ + p->i++; + if(ret->type == getvariablenode || ret->type == getarraynode || ret->type == getmembernode) + { + ret= node_postfix_decrement_create(ret); + } + + } + return ret; + + } + }else { + node_t* ret = NULL; + if(string_samep(token.text,"func")) + { + node_t* res=node_getvariable_create(p->ls->tokens[p->i].text); + p->i++; + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"(")) + { + list_string_t strs; + liststr_create(&strs); + p->i++; + while(p->ils->count && !string_samep(p->ls->tokens[p->i].text,")")) + { + if(string_samep(p->ls->tokens[p->i].text,",")) {p->i++;continue;} + if(p->ils->count) + { + node_t* n = parse_expression(p); + if(n->type == getvariablenode) + { + liststr_add(&strs,n->data.variable_node.name); + } + node_free(n); + } + } + p->i++; + + return node_setvariable_create(res,node_closure_create(strs,parse_expression(p)),true); + } + return node_const_number_create(0); + } + + if(string_samep(token.text,"null")) + { + ret=node_const_null_create(); + }else if(string_samep(token.text,"undefined")) + { + ret = node_const_undef_create(); + } + else if(string_samep(token.text,"true")) + { + ret = node_const_bool_create(true); + }else if(string_samep(token.text,"false")) + { + ret = node_const_bool_create(false); + }else { + ret = node_getvariable_create(token.text); + } + + + while(p->ils->count && string_samep(p->ls->tokens[p->i].text,"[")) + { + + p->i++; + node_t* arg = parse_expression(p); + p->i++; + ret = node_getarray_create(ret,arg); + + + } + + while(p->ils->count && string_samep(p->ls->tokens[p->i].text,".")) + { + + p->i++; + + lextoken_t token2 = p->ls->tokens[p->i++]; + + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"(")) + { + + p->i++; + ret = node_method_call_create(ret,token2.text); + //args = new List(); + //function call baby + while(p->ils->count && !string_samep(p->ls->tokens[p->i].text,")")) + { + + if(string_samep(p->ls->tokens[p->i].text,",")) {p->i++;continue;} + + if(p->ils->count) + { + node_method_call_add(ret,parse_expression(p)); + + } + + } + p->i++; + + } + else if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"[")) + { + p->i++; + node_t* arg = parse_expression(p); + p->i++; + ret = node_getmember_create(token2.text,ret); + + ret = node_getarray_create(ret,arg); + + } + else{ + ret = node_getmember_create(token2.text,ret); + + } + + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"++")) + { + /* var mgvn = ret as GetVariableValue; + i++; + if(mgvn != null) + { + return new PostfixIncrementVariableNode(mgvn); + } + */ + p->i++; + if(ret->type == getvariablenode || ret->type == getarraynode || ret->type == getmembernode) + { + ret= node_postfix_increment_create(ret); + } + + } + if(p->ils->count && string_samep(p->ls->tokens[p->i].text,"--")) + { + /* var mgvn = ret as GetVariableValue; + i++; + if(mgvn != null) + { + return new PostfixIncrementVariableNode(mgvn); + } + */ + p->i++; + if(ret->type == getvariablenode || ret->type == getarraynode || ret->type == getmembernode) + { + ret= node_postfix_decrement_create(ret); + } + + } + return ret; + + + } + } + + //return node_const_number_create(f); + printf("ERROR: END OF PARSE_VALUE_FUNC\n"); + return node_const_undef_create(); +} + +node_t* parse_expression(parser_t* p) +{ + return __parser_parse(p,false); +} + +void runtime_load(runtime_t* rt,string_t* str) +{ + + lextokenlist_t* list=lexer_lex(str); + parser_t p; + p.rt = rt; + p.ls = list; + p.i = 0; + rt->program=__parser_parse(&p,true); + lextokenlist_free(list); + +} +void node_free(node_t* n) +{ + n->free(n); + + free(n); +} diff --git a/libtlang/src/postfixdecrementnode.c b/libtlang/src/postfixdecrementnode.c new file mode 100644 index 0000000..15433f2 --- /dev/null +++ b/libtlang/src/postfixdecrementnode.c @@ -0,0 +1,46 @@ +#include "tlang.h" + +extern void __node_single_free(node_t* n); +tobject_t* __postdec_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.single_node_node; + + tobject_t* l=left->execute(left,sc); + + tobject_t* out=NULL; + + out = l; + l->count++; + node_t* n3=node_setvariable_create(left,node_sub_create(node_const_object_create(l),node_const_number_create(1)),true); + + n3->execute(n3,sc); + + n3->data.two_node_node.left = node_const_number_create(0); + node_free(n3); + + + + + + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + }else{ + out->count--; + } + + return out; +} +node_t* node_postfix_decrement_create(node_t* left) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = postfixdecrementnode; + node->data.single_node_node = left; + + node->execute = __postdec_node_exec; + node->free = __node_single_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/postfixincrementnode.c b/libtlang/src/postfixincrementnode.c new file mode 100644 index 0000000..c512409 --- /dev/null +++ b/libtlang/src/postfixincrementnode.c @@ -0,0 +1,47 @@ +#include "tlang.h" + +extern void __node_single_free(node_t* n); +tobject_t* __postinc_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.single_node_node; + + tobject_t* l=left->execute(left,sc); + + tobject_t* out=NULL; + + out = l; + l->count++; + + node_t* n3=node_setvariable_create(left,node_add_create(node_const_object_create(l),node_const_number_create(1)),true); + + n3->execute(n3,sc); + + n3->data.two_node_node.left = node_const_number_create(0); + node_free(n3); + + + + + + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + }else{ + out->count--; + } + + return out; +} +node_t* node_postfix_increment_create(node_t* left) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = postfixincrementnode; + node->data.single_node_node = left; + + node->execute = __postinc_node_exec; + node->free = __node_single_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/rightshift.c b/libtlang/src/rightshift.c new file mode 100644 index 0000000..9a94436 --- /dev/null +++ b/libtlang/src/rightshift.c @@ -0,0 +1,68 @@ +#include "tlang.h" +#include +extern void __node_two_free(node_t* n); +tobject_t* __rightshift_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"rs")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"rs"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = (int64_t)l->data.number >> (int32_t)r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_rightshift_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = rightshiftnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __rightshift_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/runtime.c b/libtlang/src/runtime.c new file mode 100644 index 0000000..50744cf --- /dev/null +++ b/libtlang/src/runtime.c @@ -0,0 +1,890 @@ +#include "tlang.h" +#if defined(_WIN32) || defined(WIN32) +#include +#endif + +#include +#include +#include +#include +#if defined(USE_SDL2) +extern void runtime_register_sdl2(runtime_t* rt); +#endif +#if defined(USE_THREADS) +extern void register_register_threads(runtime_t* rt,dict_t* create); +#endif +#if defined(USE_NETWORK) +extern void runtime_register_network(runtime_t* rt); +#endif +extern tobject_t* __ls_free(runtime_t* rt,void* ptr,list_tobject_t* ls); +extern tobject_t* __ls_get_current(runtime_t* rt,void* ptr,list_tobject_t* ls); +extern tobject_t* __ls_movenext(runtime_t* rt,void* ptr,list_tobject_t* ls); +extern tobject_t* __ls_reset(runtime_t* rt,void* ptr,list_tobject_t* ls); +void runtime_create_method_on_dict(dict_t* dict,char* name,runtime_t* rt,void* ptr,external_method_t cb,texternalmethod_free_t free) +{ + kvp_t k; + k.key = name; + k.value = tobject_fromexternalmethod(rt,ptr,cb,free); + + dict_setkvp(dict,k); +} +tobject_t* __console_clear_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + #if defined(_WIN32) || defined(WIN32) + clrscr(); + #else + printf("\033[2J"); + printf ("\x1b[%d;%dH", 0,0 ); + #endif + return tobject_basic(rt,tnull); +} +tobject_t* __fs_openreadwrite_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + stream_t* strm=stream_file_openreadwrite(args->items[0]->data.string); + if(strm != NULL) + { + return tobject_fromstream(rt,strm); + } + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_append_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + stream_t* strm=stream_file_append(args->items[0]->data.string); + if(strm != NULL) + { + return tobject_fromstream(rt,strm); + } + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_openread_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + stream_t* strm=stream_file_openread(args->items[0]->data.string); + if(strm != NULL) + { + return tobject_fromstream(rt,strm); + } + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_readalltext_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + char* fileName = string_dupp(args->items[0]->data.string); + FILE* f = fopen(fileName,"r"); + if(f) + { + string_t* str = string_create(); + string_read(str,f,fread); + free(fileName); + fclose(f); + return tobject_string(rt,str); + } + free(fileName); + + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_parent_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + + string_t* str=path_parent(args->items[0]->data.string); + return tobject_string(rt,str); + + + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_get_filename_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + + string_t* str=path_filename(args->items[0]->data.string); + return tobject_string(rt,str); + + + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_get_extension_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + + string_t* str=path_getextension(args->items[0]->data.string); + return tobject_string(rt,str); + + + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_combine_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + string_t* path=NULL; + if(args->length == 1 && args->items[0]->type ==tlist) + { + int i; + for(i = 0;iitems[0]->data.list.length;i++) + { + tobject_t* item= args->items[0]->data.list.items[i]; + if(item->type == tstring) + { + if(path == NULL) + { + path = string_dups(item->data.string); + }else{ + string_t* path2 = path_join(path,item->data.string); + string_free(path); + path=path2; + } + } + } + } + else { + int i; + for(i = 0;ilength;i++) + { + tobject_t* item= args->items[i]; + if(item->type == tstring) + { + if(path == NULL) + { + path = string_dups(item->data.string); + }else{ + string_t* path2 = path_join(path,item->data.string); + string_free(path); + path=path2; + } + } + } + } + if(path == NULL) return tobject_basic(rt,tnull); + return tobject_string(rt,path); +} +tobject_t* __fs_writealltext_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 1 && args->items[0]->type == tstring && args->items[1]->type == tstring) + { + char* fileName = string_dupp(args->items[0]->data.string); + FILE* f = fopen(fileName,"w"); + if(f) + { + string_t* myString = args->items[1]->data.string; + int totalRead=0; + int read = 0; + do { + read = 1024; + if( myString->length - totalRead < 1024) + { + read = myString->length - totalRead; + } + if(read > 0) + { + fwrite(myString->text + totalRead,1,read,f); + totalRead += read; + } + } while(read > 0); + fclose(f); + } + free(fileName); + + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_create_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + stream_t* strm=stream_file_create(args->items[0]->data.string); + if(strm != NULL) + { + return tobject_fromstream(rt,strm); + } + } + return tobject_basic(rt,tnull); +} +typedef struct { + runtime_t* rt; + tobject_t* exe; + tobject_t* root; + tobject_t* addArg; +} exec_free_ctx_t; +void __code_rt_free(tobject_t* o) +{ + exec_free_ctx_t* ctx = (exec_free_ctx_t*)o->ptr_data; + free(ctx->root); + free(ctx->exe); + free(ctx->addArg); + runtime_free(ctx->rt); + free(ctx); +} +tobject_t* __execute_code_rt(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + runtime_t* rt2 = (runtime_t*)ptr; + + return runtime_exec(rt); +} +tobject_t* runtime_exec(runtime_t* rt) +{ + tobject_t* o = rt->program->execute(rt->program,rt->globals); + if(rt->globals->hasvariable(rt->globals,"main")) + { + tobject_t* myObj=rt->globals->getvariable(rt->globals,"main"); + if(myObj->type == tinternalmethod || myObj->type == texternalmethod) + { + tobject_freeifzero(o); + return tobject_call(rt->globals,myObj,&rt->args); + } + } + return o; +} +tobject_t* __add_argument(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + runtime_t* rt2 = (runtime_t*)ptr; + int i; + for(i=0;ilength;i++) + { + runtime_add_argument(rt2,args->items[i]); + } + return tobject_basic(rt,tnull); +} +tobject_t* __reflection_parse_code(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + runtime_t* rt2 = runtime_init(); + runtime_register(rt2,rt->reg); + runtime_load(rt2,args->items[0]->data.string); + tobject_t* d = tobject_create(rt); + tobject_t* d2 = tobject_create(rt); + d2->data.dict = (dict_t*)rt2->globals->data; + d2->type = tdict; + d2->count=65535; + d->type = tdict; + d->data.dict = dict_create(); + kvp_t kvp; + kvp.key = "root"; + kvp.value = d2; + dict_setkvp(d->data.dict,kvp); + tobject_t* add_arg = tobject_fromexternalmethod(rt,rt2,__add_argument,NULL); + kvp.key = "add"; + add_arg->count = 65535; + kvp.value = add_arg; + + dict_setkvp(d->data.dict,kvp); + d->free = __code_rt_free; + tobject_t* exe = tobject_fromexternalmethod(rt,rt2,__execute_code_rt,NULL); + kvp.key = "run"; + exe->count = 65535; + kvp.value = exe; + dict_setkvp(d->data.dict,kvp); + d->free = __code_rt_free; + exec_free_ctx_t* __free_Data = (exec_free_ctx_t*)malloc(sizeof(exec_free_ctx_t)); + __free_Data->exe = exe; + __free_Data->root = d2; + __free_Data->rt = rt2; + __free_Data->addArg = add_arg; + d->ptr_data = __free_Data; + return d; + } + return tobject_basic(rt,tnull); +} +tobject_t* __math_cosh(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,cosh(num)); +} +tobject_t* __math_sinh(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,sinh(num)); +} +tobject_t* __math_tanh(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,tanh(num)); +} +tobject_t* __math_floor(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,floor(num)); +} +tobject_t* __math_sqrt(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,sqrt(num)); +} +tobject_t* __math_log(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,log(num)); +} +tobject_t* __math_log10(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,log10(num)); +} +tobject_t* __math_ceiling(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,ceil(num)); +} +tobject_t* __math_cos(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,cos(num)); +} +tobject_t* __math_acos(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,acos(num)); +} +tobject_t* __math_sin(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,sin(num)); +} +tobject_t* __math_tan(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,tan(num)); +} +tobject_t* __math_atan(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,atan(num)); +} +tobject_t* __math_asin(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + return tobject_number(rt,asin(num)); +} +tobject_t* __math_atan2(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + double num2 =0; + if(args->length > 1 && args->items[0]->type == tnumber && args->items[1]->type == tnumber) + { + num = args->items[0]->data.number; + num2 = args->items[1]->data.number; + } + return tobject_number(rt,atan2(num,num2)); +} + +tobject_t* __math_pow(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + double num = 0; + double num2 = 0; + if(args->length > 0 && args->items[0]->type == tnumber) + { + num = args->items[0]->data.number; + } + if(args->length > 1 && args->items[1]->type == tnumber) + { + num2 = args->items[1]->data.number; + } + return tobject_number(rt,pow(num,num2)); +} +tobject_t* __console_error_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int i; + + for(i = 0;ilength;i++) + { + string_t* s=tobject_tostring(rt->globals,args->items[i]); + int j; + for(j=0;jlength;j++) + { + fputc(s->text[j],stderr); + } + string_free(s); + } + + return tobject_number(rt,args->length); +} + +tobject_t* __console_write_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int i; + + for(i = 0;ilength;i++) + { + string_t* s=tobject_tostring(rt->globals,args->items[i]); + int j; + for(j=0;jlength;j++) + { + fputc(s->text[j],stdout); + } + string_free(s); + } + + return tobject_number(rt,args->length); +} + + +tobject_t* __console_stdin_external_property(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + stream_t* strm=stream_stdin(); + return tobject_fromstream(rt,strm); +} +tobject_t* __fs_setworking_external_property(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + char* dir = string_dupp(args->items[0]->data.string); + int res=chdir(dir); + free(dir); + return tobject_bool(rt,res==0); + } + return tobject_basic(rt,tnull); +} +tobject_t* __fs_getworking_external_property(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + char CWD_PATH[4096]; + char* path=getcwd(CWD_PATH,sizeof(CWD_PATH)); + if(path != NULL) + { + string_t* myStr = string_create(); + string_appendp(myStr,path); + return tobject_string(rt,myStr); + } + return tobject_basic(rt,tnull); +} +tobject_t* __console_stdout_external_property(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + stream_t* strm=stream_stdout(); + return tobject_fromstream(rt,strm); +} +tobject_t* __console_stderror_external_property(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + stream_t* strm=stream_stderr(); + return tobject_fromstream(rt,strm); +} +tobject_t* __fs_unlink_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int res=0; + char* fileName = NULL; + + if(args->length >= 1 && args->items[0]->type == tstring) + { + fileName = string_dupp(args->items[0]->data.string); + } + + if(fileName != NULL) + { + + res=unlink(fileName); + free(fileName); + } + return tobject_bool(rt,res == 0); +} +tobject_t* __fs_rmdir_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int res=0; + char* dirName = NULL; + + if(args->length >= 1 && args->items[0]->type == tstring) + { + dirName = string_dupp(args->items[0]->data.string); + } + + if(dirName != NULL) + { + + res=rmdir(dirName); + free(dirName); + } + return tobject_bool(rt,res == 0); +} +tobject_t* __fs_mkdir_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int res=0; + char* dirName = NULL; + mode_t mode=0755; + if(args->length >= 1 && args->items[0]->type == tstring) + { + dirName = string_dupp(args->items[0]->data.string); + } + if(args->length >= 2 && args->items[1]->type == tnumber) + { + mode = (mode_t)args->items[1]->data.number; + } + if(dirName != NULL) + { + + res=mkdir(dirName,mode); + free(dirName); + } + return tobject_bool(rt,res == 0); +} +tobject_t* __console_readln_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + string_t* text = string_create(); + int c = 0; + while((c=fgetc(stdin)) != '\n' && c != EOF) + { + if(c != '\r') + { + string_appendc(text,(char)c); + } + } + return tobject_string(rt,text); +} +tobject_t* __console_writeln_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int i; + + for(i = 0;ilength;i++) + { + string_t* s=tobject_tostring(rt->globals,args->items[i]); + int j; + for(j=0;jlength;j++) + { + fputc(s->text[j],stdout); + } + string_free(s); + } + #if defined(WIN32) || defined(_WIN32) + printf("\r\n"); + #else + printf("\n"); + #endif + return tobject_number(rt,args->length); +} +tobject_t* __console_errorln_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int i; + + for(i = 0;ilength;i++) + { + string_t* s=tobject_tostring(rt->globals,args->items[i]); + int j; + for(j=0;jlength;j++) + { + fputc(s->text[j],stderr); + } + string_free(s); + } + #if defined(WIN32) || defined(_WIN32) + fprintf(stderr,"\r\n"); + #else + fprintf(stderr,"\n"); + #endif + return tobject_number(rt,args->length); +} + +tobject_t* __console_writetypeln_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + int i; + + for(i = 0;ilength;i++) + { + switch(args->items[i]->type) + { + case tundef: + printf("undefined\n"); + break; + case tnull: + printf("null\n"); + break; + case tnumber: + printf("number\n"); + break; + case tlist: + printf("list\n"); + break; + case tdict: + printf("dict\n"); + break; + case tstring: + printf("string\n"); + break; + case tinternalmethod: + printf("internal_method\n"); + break; + case texternalmethod: + printf("external_method\n"); + break; + case tchar: + printf("char\n"); + break; + case tbool: + printf("bool\n"); + break; + default: + printf("Unknown: %i\n",(int)args->items[i]->type); + break; + } + } + #if defined(WIN32) || defined(_WIN32) + printf("\r\n"); + #else + printf("\n"); + #endif + return tobject_number(rt,args->length); +} +tobject_t* __create_array_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length == 0) + { + return tobject_create_array(rt,0); + } + if(args->length == 1 && args->items[0]->type ==tnumber) + { + int count = (int)args->items[0]->data.number; + + return tobject_create_array(rt,count); + } + return tobject_basic(rt,tnull); +} +typedef struct { + string_t* path; + fs_entry_filter_t filter; +} fs_enumerable_ctx_t; + +tobject_t* __fs_enumerate_entry_getIttr_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + fs_enumerable_ctx_t* ctx = (fs_enumerable_ctx_t*)ptr; + tobject_t* o = tobject_create(rt); + o->type = tdict; + dict_t* d= dict_create(); + ittr_t* ittr=fs_dir_ittr(rt,ctx->path,ctx->filter); + runtime_create_method_on_dict(d,"getcurrent",rt,ittr,__ls_get_current,NULL); + runtime_create_method_on_dict(d,"movenext",rt,ittr,__ls_movenext,NULL); + runtime_create_method_on_dict(d,"reset",rt,ittr,__ls_reset,NULL); + runtime_create_method_on_dict(d,"dispose",rt,ittr,__ls_free,NULL); + + + o->data.dict= d; + return o; +} +void __fs_enumerate_entry_getIttr_free(tobject_t* obj) +{ + fs_enumerable_ctx_t* ctx = (fs_enumerable_ctx_t*)obj->data.external_method.data; + string_free(ctx->path); + free(ctx); +} + +tobject_t* __fs_enumerate_dirs_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + tobject_t* o = tobject_create(rt); + o->type = tdict; + o->data.dict = dict_create(); + fs_enumerable_ctx_t* f = (fs_enumerable_ctx_t*)malloc(sizeof(fs_enumerable_ctx_t)); + f->filter = FOLDER; + f->path = string_dups(args->items[0]->data.string); + runtime_create_method_on_dict(o->data.dict,"getittr",rt,f,__fs_enumerate_entry_getIttr_external_method,__fs_enumerate_entry_getIttr_free); + return o; + } + return tobject_basic(rt,tnull); +} + +tobject_t* __fs_enumerate_files_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0 && args->items[0]->type == tstring) + { + tobject_t* o = tobject_create(rt); + o->type = tdict; + o->data.dict = dict_create(); + fs_enumerable_ctx_t* f = (fs_enumerable_ctx_t*)malloc(sizeof(fs_enumerable_ctx_t)); + f->filter = REGULAR; + f->path = string_dups(args->items[0]->data.string); + runtime_create_method_on_dict(o->data.dict,"getittr",rt,f,__fs_enumerate_entry_getIttr_external_method,__fs_enumerate_entry_getIttr_free); + return o; + } + return tobject_basic(rt,tnull); +} +tobject_t* __create_dict_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + + tobject_t* o=tobject_create(rt); + o->data.dict = dict_create(); + o->type = tdict; + + return o; +} +void runtime_register(runtime_t* rt,runtime_reg_t reg) +{ + rt->reg = reg; + reg(rt); +} + +runtime_t* runtime_init() +{ + runtime_t* rt = (runtime_t*)malloc(sizeof(runtime_t)); + rt->globals = NULL; + rt->program=NULL; + rt->reg=NULL; + list_create(rt,&rt->args,0); + scope_create_root(rt); + tobject_t* __fs = tobject_create(rt); + __fs->type = tdict; + __fs->data.dict= dict_create(); + tobject_t* __create = tobject_create(rt); + __create->type = tdict; + __create->data.dict= dict_create(); + tobject_t* __console = tobject_create(rt); + __console->type = tdict; + __console->data.dict= dict_create(); + tobject_t* __reflection = tobject_create(rt); + __reflection->type = tdict; + __reflection->data.dict = dict_create(); + tobject_t* __math = tobject_create(rt); + __math->type = tdict; + __math->data.dict = dict_create(); + runtime_create_method_on_dict(__create->data.dict,"array",rt,NULL,__create_array_external_method,NULL); + runtime_create_method_on_dict(__create->data.dict,"dict",rt,NULL,__create_dict_external_method,NULL); + + + runtime_create_method_on_dict(__console->data.dict,"clear",rt,NULL,__console_clear_external_method,NULL); + runtime_create_method_on_dict(__console->data.dict,"readln",rt,NULL,__console_readln_external_method,NULL); + + runtime_create_method_on_dict(__console->data.dict,"write",rt,NULL,__console_write_external_method,NULL); + runtime_create_method_on_dict(__console->data.dict,"getin",rt,NULL,__console_stdin_external_property,NULL); + runtime_create_method_on_dict(__console->data.dict,"getout",rt,NULL,__console_stdout_external_property,NULL); + runtime_create_method_on_dict(__console->data.dict,"geterror",rt,NULL,__console_stderror_external_property,NULL); + runtime_create_method_on_dict(__console->data.dict,"error",rt,NULL,__console_error_external_method,NULL); + runtime_create_method_on_dict(__console->data.dict,"errorln",rt,NULL,__console_errorln_external_method,NULL); + + runtime_create_method_on_dict(__console->data.dict,"writeln",rt,NULL,__console_writeln_external_method,NULL); + runtime_create_method_on_dict(__console->data.dict,"writetypeln",rt,NULL,__console_writetypeln_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"readalltext",rt,NULL,__fs_readalltext_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"parent",rt,NULL,__fs_parent_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"get_extension",rt,NULL,__fs_get_extension_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"get_filename",rt,NULL,__fs_get_filename_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"getworking",rt,NULL,__fs_getworking_external_property,NULL); + runtime_create_method_on_dict(__fs->data.dict,"setworking",rt,NULL,__fs_setworking_external_property,NULL); + + + runtime_create_method_on_dict(__fs->data.dict,"writealltext",rt,NULL,__fs_writealltext_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"mkdir",rt,NULL,__fs_mkdir_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"rmdir",rt,NULL,__fs_rmdir_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"unlink",rt,NULL,__fs_unlink_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"enumerate_files",rt,NULL,__fs_enumerate_files_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"enumerate_dirs",rt,NULL,__fs_enumerate_dirs_external_method,NULL); + + runtime_create_method_on_dict(__fs->data.dict,"create",rt,NULL,__fs_create_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"openread",rt,NULL,__fs_openread_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"append",rt,NULL,__fs_append_external_method,NULL); + runtime_create_method_on_dict(__fs->data.dict,"combine",rt,NULL,__fs_combine_external_method,NULL); + + runtime_create_method_on_dict(__fs->data.dict,"openreadwrite",rt,NULL,__fs_openreadwrite_external_method,NULL); + runtime_create_method_on_dict(__reflection->data.dict,"parse_code",rt,NULL,__reflection_parse_code,NULL); + runtime_create_method_on_dict(__math->data.dict,"sin",rt,NULL,__math_sin,NULL); + runtime_create_method_on_dict(__math->data.dict,"cos",rt,NULL,__math_cos,NULL); + runtime_create_method_on_dict(__math->data.dict,"asin",rt,NULL,__math_asin,NULL); + runtime_create_method_on_dict(__math->data.dict,"acos",rt,NULL,__math_acos,NULL); + runtime_create_method_on_dict(__math->data.dict,"tan",rt,NULL,__math_tan,NULL); + runtime_create_method_on_dict(__math->data.dict,"tanh",rt,NULL,__math_tanh,NULL); + runtime_create_method_on_dict(__math->data.dict,"cosh",rt,NULL,__math_cosh,NULL); + runtime_create_method_on_dict(__math->data.dict,"sinh",rt,NULL,__math_sinh,NULL); + runtime_create_method_on_dict(__math->data.dict,"atan",rt,NULL,__math_atan,NULL); + runtime_create_method_on_dict(__math->data.dict,"atan2",rt,NULL,__math_atan2,NULL); + runtime_create_method_on_dict(__math->data.dict,"log",rt,NULL,__math_log,NULL); + runtime_create_method_on_dict(__math->data.dict,"log10",rt,NULL,__math_log10,NULL); + runtime_create_method_on_dict(__math->data.dict,"floor",rt,NULL,__math_floor,NULL); + runtime_create_method_on_dict(__math->data.dict,"ceiling",rt,NULL,__math_ceiling,NULL); + runtime_create_method_on_dict(__math->data.dict,"sqrt",rt,NULL,__math_sqrt,NULL); + runtime_create_method_on_dict(__math->data.dict,"pow",rt,NULL,__math_pow,NULL); + + + rt->globals->setvariable(rt->globals,"fs",__fs); + rt->globals->setvariable(rt->globals,"console",__console); + rt->globals->setvariable(rt->globals,"math",__math); + #if defined(USE_THREADS) + runtime_register_threads(rt,__create->data.dict); + #endif + rt->globals->setvariable(rt->globals,"create",__create); + rt->globals->setvariable(rt->globals,"reflection",__reflection); + + #if defined(USE_SDL2) + runtime_register_sdl2(rt); + #endif + #if defined(USE_NETWORK) + runtime_register_network(rt); + #endif + return rt; +} +void runtime_add_argument(runtime_t* rt,tobject_t* arg) +{ + list_add(&rt->args,arg); +} +void runtime_free(runtime_t* rt) +{ + dict_t* d = (dict_t*)rt->globals->data; + + dict_free(d); + free(rt->globals); + if(rt->program != NULL) + free(rt->program); + free(rt); +} diff --git a/libtlang/src/scope.c b/libtlang/src/scope.c new file mode 100644 index 0000000..95131ec --- /dev/null +++ b/libtlang/src/scope.c @@ -0,0 +1,157 @@ +#include "tlang.h" + +typedef struct +{ + scope_t* parent; + dict_t* vars; +} __subscope_t; + +scope_t* __subscope_get_root_scope(scope_t* s) +{ + __subscope_t* sc=(__subscope_t*)s->data; + return sc->parent->root(sc->parent); + +} +tobject_t* __subscope_get_variable(scope_t* s,char* key) +{ + __subscope_t* sc=(__subscope_t*)s->data; + if(dict_haskey(sc->vars,key) && strcmp(key,"last_ittr") == 0) + { + return dict_getkvp(sc->vars,key)->value; + } + if(sc->parent->hasvariable(sc->parent,key)) + return sc->parent->getvariable(sc->parent,key); + if(dict_haskey(sc->vars,key)) + return dict_getkvp(sc->vars,key)->value; + + return tobject_basic(s->rt,tundef); +} +bool __subscope_has_variable(scope_t* s,char* key) +{ + __subscope_t* sc=(__subscope_t*)s->data; + if(dict_haskey(sc->vars,key)) + { + return true; + } + return sc->parent->hasvariable(sc->parent,key); +} +void __subscope_set_variable(scope_t* s,char* key,tobject_t* val) +{ + __subscope_t* sc=(__subscope_t*)s->data; + if(sc->parent->hasvariable(sc->parent,key) && strcmp(key,"last_ittr") != 0) + { + + sc->parent->setvariable(sc->parent,key,val); + + }else{ + + kvp_t kvp; + kvp.key = key; + kvp.value = val; + dict_setkvp(sc->vars,kvp); + } +} +scope_t* __get_subscope(scope_t* s) +{ + __subscope_t* ss = (__subscope_t*)malloc(sizeof(s)); + ss->parent = s; + tscope_addref(s); + ss->vars = dict_create(); + scope_t* sub = (scope_t*)malloc(sizeof(scope_t)); + sub->data = ss; + sub->root = __subscope_get_root_scope; + sub->subscope = __get_subscope; + sub->isRoot=false; + sub->count=1; + sub->rt = s->rt; + sub->getvariable = __subscope_get_variable; + sub->hasvariable = __subscope_has_variable; + sub->setvariable = __subscope_set_variable; + return sub; +} +scope_t* __root_get_root_scope(scope_t* s) +{ + return s; +} +bool __root_has_variable(scope_t* s,char* key) +{ + dict_t* dic=(dict_t*)s->data; + return dict_haskey(dic,key); +} +tobject_t* __root_get_variable(scope_t* s,char* key) +{ + dict_t* dic=(dict_t*)s->data; + if(dict_haskey(dic,key)) + { + return dict_getkvp(dic,key)->value; + } + return tobject_basic(s->rt,tundef); +} +void __root_set_variable(scope_t* s,char* key,tobject_t* val) +{ + + dict_t* dic=(dict_t*)s->data; + + kvp_t kvp; + kvp.key = key; + kvp.value = val; + dict_setkvp(dic,kvp); + +} +void scope_create_root(runtime_t* rt) +{ + if(rt->globals != NULL) + { + return; + } + rt->globals = (scope_t*)malloc(sizeof(scope_t)); + rt->globals->isRoot=true; + rt->globals->count=1; + rt->globals->rt=rt; + rt->globals->root = __root_get_root_scope; + rt->globals->hasvariable = __root_has_variable; + rt->globals->getvariable = __root_get_variable; + rt->globals->setvariable = __root_set_variable; + rt->globals->subscope = __get_subscope; + rt->globals->data = dict_create(); + +} + + +scope_t* scope_begin(scope_t* s) +{ + return s->subscope(s); +} +scope_t* scope_end(scope_t* s) +{ + if(s->isRoot) + return s; + + __subscope_t* sc=(__subscope_t*)s->data; + tscope_rmref(s); + return sc->parent; +} +void tscope_rmref(scope_t* s) +{ + s->count--; + if(s->count <=0) + { + if(!s->isRoot) + { + __subscope_t* sc=(__subscope_t*)s->data; + tscope_rmref(sc->parent); + + dict_free(sc->vars); + free(sc); + free(s); + } + + + + + } +} +void tscope_addref(scope_t* s) +{ + s->count++; +} \ No newline at end of file diff --git a/libtlang/src/scopenode.c b/libtlang/src/scopenode.c new file mode 100644 index 0000000..094c5f5 --- /dev/null +++ b/libtlang/src/scopenode.c @@ -0,0 +1,59 @@ +#include "tlang.h" +tobject_t* __node_scope_execute(node_t* n,scope_t* s) +{ + if(n->data.scope_node.isSub) + { + s=scope_begin(s); + } + tobject_t* obj_data = tobject_basic(s->rt,tnull); + tobject_addref(obj_data); + int i; + for(i=0;idata.scope_node.count;i++) + { + tobject_t* d=n->data.scope_node.items[i]->execute(n->data.scope_node.items[i],s); + tobject_addref(d); + tobject_rmref(obj_data); + obj_data=d; + } + if(n->data.scope_node.isSub) + { + s=scope_end(s); + } + obj_data->count--; //subtract so we are what we were, can be 0 like usual, should be freed by something + + return obj_data; +} +void __node_scope_free(node_t* node) +{ + node_t** items= node->data.scope_node.items; + int i; + for(i=0;idata.scope_node.count;i++) + { + node_free(items[i]); + } + free(items); +} + +node_t* node_scope_create(bool first) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = scopenode; + n->data.scope_node.count = 0; + n->data.scope_node.isSub=!first; + n->data.scope_node.capacity=64; + n->data.scope_node.items=(node_t**)malloc(sizeof(node_t*) * n->data.scope_node.capacity); + n->execute = __node_scope_execute; + n->free = __node_scope_free; + + return n; +} +void node_scope_append(node_t* sc,node_t* n) +{ + if(sc->data.scope_node.count + 128 >sc->data.scope_node.capacity) + { + sc->data.scope_node.capacity = sc->data.scope_node.count + 128; + sc->data.scope_node.items=(node_t**)realloc(sc->data.scope_node.items,sc->data.scope_node.capacity * sizeof(node_t*)); + + } + sc->data.scope_node.items[sc->data.scope_node.count++]=n; +} \ No newline at end of file diff --git a/libtlang/src/sdl2.c b/libtlang/src/sdl2.c new file mode 100644 index 0000000..fd9f7e2 --- /dev/null +++ b/libtlang/src/sdl2.c @@ -0,0 +1,95 @@ +#include "tlang.h" + +#if defined(USE_SDL2) + #include "SDL2/SDL.h" + tobject_t* sdl2=NULL; + tobject_t* __sdl_init_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + + if(args->length >= 1 && args->items[0]->type == tnumber) + { + return tobject_number(rt,SDL_Init((Uint32)args->items[0]->data.number)); + } + return tobject_number(rt,SDL_Init(SDL_INIT_EVERYTHING)); + } + tobject_t* __sdl_renderclear_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >= 1 && args->items[0]->type == tnumber) + { + int64_t n = (int64_t)args->items[0]->data.number; + return tobject_number(rt,SDL_RenderClear((SDL_Renderer*)(void*)n)); + } + return tobject_basic(rt,tnull); + } + + tobject_t* __sdl_renderpresent_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >= 1 && args->items[0]->type == tnumber) + { + int64_t n = (int64_t)args->items[0]->data.number; + SDL_RenderPresent((SDL_Renderer*)(void*)n); + } + return tobject_basic(rt,tnull); + } + + + tobject_t* __sdl_destroywindow_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >= 1 && args->items[0]->type == tnumber) + { + int64_t n = (int64_t)args->items[0]->data.number; + SDL_DestroyWindow((SDL_Window*)(void*)n); + } + return tobject_basic(rt,tnull); + } + tobject_t* __sdl_setrendererdrawcolor_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >= 5 && args->items[0]->type == tnumber && args->items[1]->type == tnumber && args->items[2]->type == tnumber && args->items[3]->type == tnumber && args->items[4]->type == tnumber) + { + int64_t n = (int64_t)args->items[0]->data.number; + return tobject_number(rt,SDL_SetRenderDrawColor((SDL_Renderer*)(void*)n,(Uint8)args->items[1]->data.number,(Uint8)args->items[2]->data.number,(Uint8)args->items[3]->data.number,(Uint8)args->items[4]->data.number)); + } + return tobject_basic(rt,tnull); + } + tobject_t* __sdl_createrenderer_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + + if(args->length >= 3 && args->items[0]->type == tnumber && args->items[1]->type == tnumber&& args->items[2]->type == tnumber) + { + int64_t n = (int64_t)args->items[0]->data.number; + SDL_Renderer* r=SDL_CreateRenderer((SDL_Window*)(void*)n,(int)args->items[1]->data.number,(Uint32)args->items[2]->data.number); + + return tobject_number(rt,(double)(int64_t)r); + } + return tobject_basic(rt,tnull); + } + tobject_t* __sdl_createwindow_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) + { + if(args->length >= 6 && args->items[0]->type == tstring && args->items[1]->type == tnumber&& args->items[2]->type == tnumber&& args->items[3]->type == tnumber&& args->items[4]->type == tnumber&& args->items[5]->type == tnumber) + { + char* name = string_dupp(args->items[0]->data.string); + SDL_Window* w=SDL_CreateWindow(name,(int)args->items[1]->data.number,(int)args->items[2]->data.number,(int)args->items[3]->data.number,(int)args->items[4]->data.number,(Uint32)args->items[5]->data.number); + free(name); + return tobject_number(rt,(double)(int64_t)w); + } + return tobject_basic(rt,tnull); + } + void runtime_register_sdl2(runtime_t* rt) + { + if(sdl2 == NULL) + { + sdl2 = tobject_create(rt); + sdl2->type = tdict; + sdl2->data.dict = dict_create(); + runtime_create_method_on_dict(sdl2->data.dict,"destroyWindow",rt,NULL,__sdl_destroywindow_external_method,NULL); + runtime_create_method_on_dict(sdl2->data.dict,"createRenderer",rt,NULL,__sdl_createrenderer_external_method,NULL); + runtime_create_method_on_dict(sdl2->data.dict,"setRenderDrawColor",rt,NULL,__sdl_setrendererdrawcolor_external_method,NULL); + runtime_create_method_on_dict(sdl2->data.dict,"renderClear",rt,NULL,__sdl_renderclear_external_method,NULL); + runtime_create_method_on_dict(sdl2->data.dict,"renderPresent",rt,NULL,__sdl_renderpresent_external_method,NULL); + + runtime_create_method_on_dict(sdl2->data.dict,"createWindow",rt,NULL,__sdl_createwindow_external_method,NULL); + runtime_create_method_on_dict(sdl2->data.dict,"init",rt,NULL,__sdl_init_external_method,NULL); + } + rt->globals->setvariable(rt->globals,"sdl",sdl2); + } +#endif \ No newline at end of file diff --git a/libtlang/src/setvariablenode.c b/libtlang/src/setvariablenode.c new file mode 100644 index 0000000..9841d8d --- /dev/null +++ b/libtlang/src/setvariablenode.c @@ -0,0 +1,37 @@ +#include "tlang.h" + +tobject_t* _node_setvariable_exec(node_t* n,scope_t* sc) +{ + node_t* left=n->data.variable_set_node.variable; + node_t* right = n->data.variable_set_node.value; + tobject_t* o = right->execute(right,sc); + o->count++; + if(left->type == getarraynode) + { + + left->data.array_node.set(left,o,sc); + } + if(left->type == getvariablenode || left->type == getmembernode) + { + left->data.variable_node.set(left,o,sc); + } + o->count--; + return o; +} +void __setVar_free(node_t* n) +{ + if(n->data.variable_set_node.deleteVariableNode) + node_free(n->data.variable_set_node.variable); + node_free(n->data.variable_set_node.value); +} +node_t* node_setvariable_create(node_t* n1,node_t* n2,bool delVar) +{ + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->type = setvariablenode; + n->data.variable_set_node.variable = n1; + n->data.variable_set_node.value = n2; + n->data.variable_set_node.deleteVariableNode = delVar; + n->execute = _node_setvariable_exec; + n->free = __setVar_free; + return n; +} diff --git a/libtlang/src/stream.c b/libtlang/src/stream.c new file mode 100644 index 0000000..e157cc1 --- /dev/null +++ b/libtlang/src/stream.c @@ -0,0 +1,601 @@ +#define _FILE_OFFSET_BITS 64 +#include +#include "tlang.h" + +int64_t _file_read(stream_t* strm,void* buff,int64_t len) +{ + FILE* f = (FILE*)strm->ptr; + return fread(buff,1,(size_t)len,f); +} +void _file_write(stream_t* strm,void* buff,int64_t len) +{ + FILE* f = (FILE*)strm->ptr; + fwrite(buff,1,(size_t)len,f); +} +int64_t _file_getpos(stream_t* strm) +{ + return (int64_t)ftello((FILE*)strm->ptr); +} +void _file_setpos(stream_t* strm,int64_t nu) +{ + + fseeko((FILE*)strm->ptr,(off_t)nu,SEEK_SET); +} +void _file_free(stream_t* strm) +{ + fclose((FILE*)strm->ptr); +} +int64_t _file_getlen(stream_t* strm) +{ + fpos_t pos; + fgetpos((FILE*)strm->ptr,&pos); + fseeko((FILE*)strm->ptr,(off_t)0L,SEEK_END); + int64_t v=(int64_t)ftello((FILE*)strm->ptr); + fsetpos((FILE*)strm->ptr,&pos); + return v; +} +void _file_flush(stream_t* strm) +{ + fflush((FILE*)strm->ptr); +} +stream_t* stream_create_file(FILE* f,stream_bool_t canread,stream_bool_t canwrite,stream_bool_t canseek) +{ + return stream_create(f,_file_read,_file_write,_file_getpos,_file_getlen,_file_setpos,canread,canwrite,canseek,_file_free,_file_flush); +} +stream_t* stream_create( + void* ptr, + stream_read_t read, + stream_write_t write, + stream_get_t getpos, + stream_get_t getlength, + stream_set_t setpos, + stream_bool_t canread, + stream_bool_t canwrite, + stream_bool_t canseek, + stream_free_t free, + stream_free_t flush +) +{ + stream_t* strm = (stream_t*)malloc(sizeof(stream_t)); + strm->ptr = ptr; + strm->read = read; + strm->write =write; + strm->getpos = getpos; + strm->setpos = setpos; + strm->getlength = getlength; + strm->canread = canread; + strm->canwrite =canwrite; + strm->canseek = canseek; + strm->free = free; + strm->flush = flush; + strm->set_extra_methods = NULL; + return strm; +} + +typedef struct +{ + scope_t* s; + tobject_t* o; + bool closeStrm; + /* data */ +} __toStrm; + +int64_t __tobject_read(stream_t* strm,void* ptr,int64_t len) +{ + uint8_t* pt = (uint8_t*)ptr; + __toStrm* _strm= (__toStrm*)strm->ptr; + tobject_t* r=tobject_create_array(_strm->s->rt,(int)len); + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"read")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"read")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + list_add(&g,r); + list_add(&g,tobject_number(_strm->s->rt,len)); + tobject_t* fg=tobject_call(_strm->s,f,&g); + len = (int64_t)fg->data.number; + tobject_freeifzero(fg); + int i; + for(i=0;idata.list.items[i]; + if(oe->type == tnumber) + { + pt[i] = (uint8_t)oe->data.number; + } + } + list_free(&g); + return len; + } + return 0; +} +void __tobject_write(stream_t* strm,void* ptr,int64_t len) +{ + uint8_t* pt = (uint8_t*)ptr; + __toStrm* _strm= (__toStrm*)strm->ptr; + tobject_t* r=tobject_create_array(_strm->s->rt,0); + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"write")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"write")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + list_add(&g,r); + list_add(&g,tobject_number(_strm->s->rt,len)); + + int i; + for(i=0;idata.list,tobject_number(_strm->s->rt,(double)pt[i])); + + } + tobject_t* j=tobject_call(_strm->s,f,&g); + tobject_freeifzero(j); + list_free(&g); + + } +} +int64_t __tobject_getlength(stream_t* strm) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"getlength")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"getlength")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + double r = j->data.number; + tobject_freeifzero(j); + list_free(&g); + return (int64_t)r; + } + return 0; +} +bool __tobject_canread(stream_t* strm) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"canread")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"canread")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + bool r = tobject_tobool(j); + tobject_freeifzero(j); + list_free(&g); + return r; + } + return false; +} + +bool __tobject_canseek(stream_t* strm) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"canseek")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"canseek")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + bool r = tobject_tobool(j); + tobject_freeifzero(j); + list_free(&g); + return r; + } + return false; +} +bool __tobject_canwrite(stream_t* strm) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"canwrite")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"canwrite")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + bool r = tobject_tobool(j); + tobject_freeifzero(j); + list_free(&g); + return r; + } + return false; +} +void __tobject_flush(stream_t* strm) +{ + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"flush")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"flush")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + + tobject_freeifzero(j); + list_free(&g); + + } +} +int64_t __tobject_getpos(stream_t* strm) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"getposition")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"getposition")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + double r = j->data.number; + tobject_freeifzero(j); + list_free(&g); + return (int64_t)r; + } + return 0; +} +void __tobject_setpos(stream_t* strm,int64_t len) +{ + + __toStrm* _strm= (__toStrm*)strm->ptr; + + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"setposition")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"setposition")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + list_add(&g,tobject_number(_strm->s->rt,(double)len)); + + tobject_t* j=tobject_call(_strm->s,f,&g); + + tobject_freeifzero(j); + list_free(&g); + + } + +} +void __tobject_strm_free(stream_t* strm) +{ + __toStrm* _strm= (__toStrm*)strm->ptr; + _strm->o->count--; + if(_strm->o->count <= 0 || _strm->closeStrm) + { + _strm->o->count=1; + if(_strm->o->type == tdict && dict_haskey(_strm->o->data.dict,"close")) + { + tobject_t* f = dict_getkvp(_strm->o->data.dict,"close")->value; + list_tobject_t g; + list_create(_strm->s->rt,&g,0); + + + tobject_t* j=tobject_call(_strm->s,f,&g); + + tobject_freeifzero(j); + list_free(&g); + } + tobject_rmref(_strm->o); + } + free(_strm); +} +stream_t* tobject_tostream(scope_t* s,tobject_t* obj,bool closeStream) +{ + __toStrm* strm = (__toStrm*)malloc(sizeof(__toStrm)); + strm->s=s; + strm->o = obj; + strm->o->count++; + strm->closeStrm = closeStream; + return stream_create(strm,__tobject_read,__tobject_write,__tobject_getpos,__tobject_getlength,__tobject_setpos,__tobject_canread,__tobject_canwrite,__tobject_canseek,__tobject_strm_free,__tobject_flush); +} +tobject_t* __write_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(!strm->canwrite(strm) || strm->write == NULL) return tobject_number(rt,0); + if(obj->length >= 2) + { + tobject_t* array = obj->items[0]; + int len = 0; + int offset=0; + if(obj->items[1]->type == tnumber) + { + len = (int)obj->items[1]->data.number; + if(obj->length >= 3 && obj->items[2]->type == tnumber) + { + offset = (int)obj->items[2]->data.number; + } + + int a = array->data.list.length-offset; + int totalRead = len; + if(a < totalRead) + { + totalRead = a; + } + uint8_t* buffer = (uint8_t*)malloc(totalRead); + int i; + for(i=0;idata.list.items[offset+i]; + if(o->type == tnumber) + { + buffer[i] = (uint8_t)o->data.number; + } + else if(o->type == tchar) + { + buffer[i] = (uint8_t)o->data.chr; + } + } + + strm->write(strm,buffer,totalRead); + + + free(buffer); + return tobject_number(rt,totalRead); + + } + } + return tobject_number(rt,0); +} +tobject_t* __read_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(!strm->canread(strm) || strm->read == NULL) return tobject_number(rt,0); + if(obj->length >= 2) + { + tobject_t* array = obj->items[0]; + int len = 0; + int offset=0; + if(obj->items[1]->type == tnumber) + { + len = (int)obj->items[1]->data.number; + if(obj->length >= 3 && obj->items[2]->type == tnumber) + { + offset = (int)obj->items[2]->data.number; + } + + int a = array->data.list.length-offset; + int totalRead = len; + if(a < totalRead) + { + totalRead = a; + } + uint8_t* buffer = (uint8_t*)malloc(totalRead); + totalRead=strm->read(strm,buffer,totalRead); + int i; + for(i = 0;idata.list,i+offset,tobject_number(rt,buffer[i])); + } + free(buffer); + return tobject_number(rt,totalRead); + + } + } + return tobject_number(rt,0); +} +tobject_t* __set_pos(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->setpos == NULL || obj->length == 0 || obj->items[0]->type != tnumber) return tobject_basic(rt,tnull); + strm->setpos(strm,(int64_t)obj->items[0]->data.number); + return tobject_basic(rt,tnull); +} +tobject_t* __get_pos(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->getpos == NULL) return tobject_number(rt,0); + return tobject_number(rt,strm->getpos(strm)); +} +tobject_t* __get_len(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->getlength == NULL) return tobject_number(rt,0); + return tobject_number(rt,strm->getlength(strm)); +} +tobject_t* __can_read_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->canread == NULL) return tobject_bool(rt,false); + return tobject_bool(rt,strm->canread(strm)); +} +tobject_t* __can_write_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->canwrite == NULL) return tobject_bool(rt,false); + return tobject_bool(rt,strm->canwrite(strm)); +} +tobject_t* __can_seek_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(strm->canseek == NULL) return tobject_bool(rt,false); + return tobject_bool(rt,strm->canseek(strm)); +} +tobject_t* __read_byte(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + if(!strm->canread(strm) || strm->read == NULL) return tobject_number(rt,-1); + uint8_t r[1]; + int r2=strm->read(strm,r,1); + return tobject_number(rt,r2 == 1 ? r[0] : -1); +} +tobject_t* __close_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + stream_free(strm); + return tobject_basic(rt,tundef); +} +tobject_t* __flush_func(runtime_t* rt,void* ptr,list_tobject_t* obj) +{ + stream_t* strm=(stream_t*)ptr; + stream_flush(strm); + return tobject_basic(rt,tundef); +} +tobject_t* tobject_fromstream(runtime_t* rt,stream_t* strm) +{ + tobject_t* _strm = tobject_create(rt); + _strm->type = tdict; + _strm->data.dict=dict_create(); + runtime_create_method_on_dict(_strm->data.dict,"read",rt,strm,__read_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"write",rt,strm,__write_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"setposition",rt,strm,__set_pos,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getposition",rt,strm,__get_pos,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getlength",rt,strm,__get_len,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getcount",rt,strm,__get_len,NULL); + runtime_create_method_on_dict(_strm->data.dict,"readbyte",rt,strm,__read_byte,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getnextbyte",rt,strm,__read_byte,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getcanread",rt,strm,__can_read_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getcanwrite",rt,strm,__can_write_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"getcanseek",rt,strm,__can_seek_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"close",rt,strm,__close_func,NULL); + runtime_create_method_on_dict(_strm->data.dict,"flush",rt,strm,__flush_func,NULL); + if(strm->set_extra_methods != NULL) + { + strm->set_extra_methods(strm,_strm,rt,strm->set_extra_methods_data); + } + return _strm; +} +int64_t stream_read(stream_t* strm,void* buff,int64_t len) +{ + if(strm->read != NULL) + strm->read(strm,buff,len); + return 0; +} +void stream_write(stream_t* strm,void* buffer,int64_t len) +{ + if(strm->write != NULL) + strm->write(strm,buffer,len); +} +int64_t stream_getpos(stream_t* strm) +{ + if(strm->getpos == NULL) return 0; + return strm->getpos(strm); +} +void stream_flush(stream_t* strm) +{ + if(strm->flush != NULL) + strm->flush(strm); +} +int64_t stream_getlength(stream_t* strm) +{ + if(strm->getpos == NULL) return 0; + return strm->getlength(strm); +} +void stream_setpos(stream_t* strm,int64_t pos) +{ + if(strm->setpos != NULL) + strm->setpos(strm,pos); +} +bool stream_canread(stream_t* strm) +{ + if(strm->canread != NULL) + return strm->canread(strm); + + return false; +} +bool stream_canwrite(stream_t* strm) +{ + if(strm->canwrite != NULL) + return strm->canwrite(strm); + + return false; +} +bool stream_canseek(stream_t* strm) +{ + if(strm->canseek != NULL) + return strm->canseek(strm); + + return false; +} +stream_t* stream_stdin() +{ + stream_t* strm=stream_create_file(stdin,stream_true,stream_false,stream_false); + strm->free=NULL; + return strm; +} +stream_t* stream_stdout() +{ + stream_t* strm=stream_create_file(stdout,stream_false,stream_true,stream_false); + strm->free=NULL; + return strm; +} +stream_t* stream_stderr() +{ + stream_t* strm=stream_create_file(stderr,stream_false,stream_true,stream_false); + strm->free=NULL; + return strm; +} +void stream_free(stream_t* strm) +{ + if(strm->free != NULL) + strm->free(strm); + free(strm); +} + +bool stream_true(stream_t* strm) +{ + return true; +} + +bool stream_false(stream_t* stream) +{ + return false; +} + + +stream_t* stream_file_create(string_t* str) +{ + char* fname=string_dupp(str); + FILE* f=fopen(fname,"wb"); + free(fname); + return stream_create_file(f,stream_false,stream_true,stream_true); + +} +stream_t* stream_file_openread(string_t* str) +{ + char* fname=string_dupp(str); + FILE* f=fopen(fname,"rb"); + free(fname); + return stream_create_file(f,stream_true,stream_false,stream_true); + +} +stream_t* stream_file_openreadwrite(string_t* str) +{ + char* fname=string_dupp(str); + FILE* f=fopen(fname,"r+b"); + free(fname); + return stream_create_file(f,stream_true,stream_true,stream_true); + +} +stream_t* stream_file_readappend(string_t* str) +{ + char* fname=string_dupp(str); + FILE* f=fopen(fname,"a+b"); + free(fname); + return stream_create_file(f,stream_true,stream_true,stream_true); + +} +stream_t* stream_file_append(string_t* str) +{ + char* fname=string_dupp(str); + FILE* f=fopen(fname,"ab"); + free(fname); + return stream_create_file(f,stream_false,stream_true,stream_false); + +} \ No newline at end of file diff --git a/libtlang/src/string.c b/libtlang/src/string.c new file mode 100644 index 0000000..1057374 --- /dev/null +++ b/libtlang/src/string.c @@ -0,0 +1,379 @@ +#include "tlang.h" +#include +#include +#include +#include +void string_free(string_t* s) +{ + free(s->text); + free(s); +} +string_t* string_create() +{ + string_t* s2 = (string_t*)malloc(sizeof(string_t)); + s2->capacity = 128; + s2->length = 0; + s2->text = (char*)malloc(sizeof(char)* s2->capacity); + + return s2; +} +bool string_islong(string_t* s,int64_t* number) +{ + //https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number + char* _s = string_dupp(s); + char *end; + int si; + bool res=false; + errno = 0; + + *number = strtoll(_s, &end,0); + + if (end == _s) { + res = false; + } else if ('\0' != *end) { + res=false; + } else if ( ERANGE == errno) { + res=false; + } else { + res = true; + + + /* Process si */ + } + free(_s); + return res; +} + +bool string_isnumber(string_t* s,double* number) +{ + //https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number + char* _s = string_dupp(s); + char *end; + int si; + bool res=false; + errno = 0; + + *number = strtod(_s, &end); + + if (end == _s) { + res = false; + } else if ('\0' != *end) { + res=false; + } else if ( ERANGE == errno) { + res=false; + } else { + res = true; + + + /* Process si */ + } + free(_s); + return res; +} +void string_ensure(string_t* a) +{ + if(a->length + 1 > a->capacity) + { + a->capacity = a->length + 128; + a->text=(char*)realloc(a->text,a->capacity); + + } +} + +void string_appendp(string_t* a,const char* str) +{ + size_t len = strlen(str); + int _len = a->length; + a->length = _len+ (int)len; + string_ensure(a); + memcpy(a->text+_len,str,len); +} +void string_appendc(string_t* a,char c) +{ + char str[2]; + str[0] = c; + str[1] = 0; + string_appendp(a,str); +} + +void string_appendn(string_t* s,double val) +{ + char txt[1000]; + //snprintf(txt,len,"%f",val); + int64_t bigNo = (int64_t)val; + val -= bigNo; + uint64_t littleNo = (uint64_t)(val * 100000000000.0); + while(littleNo % 10 == 0 && littleNo != 0) + { + littleNo /= 10; + } + snprintf(txt,1000,"%" PRId64 "", bigNo); + string_appendp(s,txt); + + if(littleNo > 0) + { + snprintf(txt,1000,".%" PRIu64 "", littleNo); + string_appendp(s,txt); + } + + + + +} +void string_clear(string_t* s) +{ + s->length=0; +} +char* string_dupp(string_t* s) +{ + char* text = (char*)malloc(sizeof(char) * (s->length + 1)); + memcpy(text,s->text,s->length); + text[s->length] = 0; + return text; +} +string_t* string_dups(string_t* s) +{ + string_t* s2 = (string_t*)malloc(sizeof(string_t)); + s2->capacity = s->length + 128; + s2->length = s->length; + s2->text = (char*)malloc(sizeof(char)* s2->capacity); + memcpy(s2->text,s->text,s->length); + return s2; +} +void string_appends(string_t* a,string_t* b) +{ + + int _len = a->length; + a->length = _len+ b->length; + string_ensure(a); + memcpy(a->text+_len,b->text,b->length); +} +const char* string_trimstart(const char* ptr,char c) +{ + while(*ptr != '\0') + { + if(*ptr != c) break; + ptr++; + } + return ptr; +} +string_t* string_substring(string_t* s,int start,int length) +{ + if(s->length - start < length) + { + length = s->length - start; + } + if(length < 0) length = 0; + string_t* dest = string_create(); + if(length == 0) return dest; + dest->length = length; + string_ensure(dest); + memcpy(dest->text,s->text + start,length); + return dest; +} +string_t* string_remove(string_t* s,int start,int length) +{ + string_t* dest = string_create(); + int after = start + length; + if(start > 0) + { + string_t* dest1=string_substring(s,0,start); + string_appends(dest,dest1); + string_free(dest1); + } + + if(after < s->length - 1) + { + string_t* dest1=string_substring(s,after,s->length-after); + string_appends(dest,dest1); + string_free(dest1); + } + return dest; +} +string_t* string_trimstarts(string_t* s,char c) +{ + int i = 0; + while(ilength) + { + if(s->text[i] != c) break; + i++; + } + string_t* s2=string_create(); + int _len = s->length-i; + s2->length = _len; + string_ensure(s2); + memcpy(s2->text,s->text+i,_len); + return s2; +} + +int string_lastindexof(string_t* s,char c) +{ + int i; + for(i=s->length-1;i>=0;i--) + { + if(s->text[i] == c) return i; + } + return i; +} + +int string_indexof(string_t* s,char c) +{ + int i=0; + for(i=0;ilength;i++) + { + if(s->text[i] == c) return i; + } + return -1; +} +string_t* string_trimends(string_t* s,char c) +{ + int i = s->length-1; + while(i>=0) + { + if(s->text[i] != c) break; + i--; + } + i++; + if(i < 0) i=0; + string_t* s2=string_create(); + s2->length =i; + string_ensure(s2); + memcpy(s2->text,s->text,s2->length); + return s2; +} +void string_read(string_t* str,void* ptr,size_t(*_cb)(void*,size_t,size_t,void*)) +{ + char buffer[1024]; + size_t read=0; + do{ + read = _cb(buffer,1,1024,ptr); + if(read > 0){ + int _len = str->length; + str->length = _len+ (int)read; + string_ensure(str); + memcpy(str->text+_len,buffer,read); + } + }while(read > 0); +} +bool string_samep(string_t* s,const char* text) +{ + size_t len = strlen(text); + if(s->length != len) + { + return false; + } + return memcmp(s->text,text,len) == 0; +} +bool string_sames(string_t* s,string_t* b) +{ + int len = b->length; + if(s->length != b->length) + { + return false; + } + return memcmp(s->text,b->text,(size_t)len) == 0; +} +bool string_is_del(string_t* text,int i,string_t* del) +{ + if(text->length - i < del->length) return false; + int j; + for(j = 0;jlength;j++) + { + if(text->text[j+i] != del->text[j]) return false; + } + return true; +} +tobject_t* string_split(runtime_t* rt,string_t* text,string_t* del,int max,bool empty) +{ + if(max == 0) + { + max = 2147483647; + } + int j = 0; + int i; + tobject_t* o = tobject_create_array(rt,0); + string_t* b=string_create(); + for(i=0;ilength;i++) + { + if(string_is_del(text,i,del) && j < max - 1) + { + i += del->length; + j++; + if(b->length > 0 || empty) + list_add(&o->data.list,tobject_string(rt,string_dups(b))); + string_clear(b); + } + if(ilength) + string_appendc(b,text->text[i]); + } + if(b->length > 0 || empty) + list_add(&o->data.list,tobject_string(rt,string_dups(b))); + + string_free(b); + return o; + /* List p = new List(); + StringBuilder b= new StringBuilder(); + for(int i = 0;i 0 || empty) + p.Add(b.ToString()); + + b.Clear(); + } + + b.Append(text[i]); + + } + if(b.Length > 0 || empty) + p.Add(b.ToString()); + return p.ToArray(); + */ +} +bool string_startswith(string_t* haystack,string_t* needle) +{ + int len = needle->length; + if(haystack->length < len) return false; + int i; + for(i = 0;i < len;i++) + { + if(needle->text[i] != haystack->text[i]) return false; + } + return true; +} +bool string_endswith(string_t* haystack,string_t* needle) +{ + int len = needle->length; + if(haystack->length < len) return false; + int i; + for(i = 0;i < needle->length;i++) + { + if(needle->text[i] != haystack->text[haystack->length-needle->length+i]) return false; + } + return true; +} +string_t* string_replace(string_t* text,string_t* old,string_t* new) +{ + int j = 0; + int i; + + string_t* b=string_create(); + for(i=0;ilength;i++) + { + if(string_is_del(text,i,old)) + { + i += old->length; + j++; + string_appends(b,new); + + } + if(ilength) + string_appendc(b,text->text[i]); + } + + + return b; +} diff --git a/libtlang/src/stringittr.c b/libtlang/src/stringittr.c new file mode 100644 index 0000000..9c0e924 --- /dev/null +++ b/libtlang/src/stringittr.c @@ -0,0 +1,52 @@ + +#include "tlang.h" + +typedef struct { + string_t* str; + int pos; + tobject_t* current; + runtime_t* rt; +} strittr_t; + +void _string_free(ittr_t* ittr) +{ + strittr_t* ittrData = (strittr_t*)ittr->ptr; + string_free(ittrData->str); + tobject_rmref(ittrData->current); + free(ittrData); +} +tobject_t* _string_getcurrent(ittr_t* ittr) +{ + strittr_t* ittrData = (strittr_t*)ittr->ptr; + return ittrData->current; +} +void _string_reset(ittr_t* ittr) +{ + strittr_t* ittrData = (strittr_t*)ittr->ptr; + ittrData->pos=-1; +} +bool _string_movenext(ittr_t* ittr) +{ + strittr_t* ittrData = (strittr_t*)ittr->ptr; + ittrData->pos++; + if(ittrData->posstr->length) + { + tobject_rmref(ittrData->current); + ittrData->current = tobject_create(ittrData->rt); + ittrData->current->type=tchar; + ittrData->current->data.chr = ittrData->str->text[ittrData->pos]; + tobject_addref(ittrData->current); + return true; + } + return false; +} +ittr_t* string_ittr(runtime_t* rt,string_t* str) +{ + strittr_t* myittr=(strittr_t*)malloc(sizeof(strittr_t)); + myittr->pos = -1; + myittr->str = string_dups(str); + myittr->rt = rt; + myittr->current = tobject_basic(rt,tnull); + tobject_addref(myittr->current); + return ittr_create(myittr,_string_movenext,_string_reset,_string_getcurrent,_string_free); +} \ No newline at end of file diff --git a/libtlang/src/subnode.c b/libtlang/src/subnode.c new file mode 100644 index 0000000..bfa5b88 --- /dev/null +++ b/libtlang/src/subnode.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __sub_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"sub")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"sub"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = l->data.number - r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_sub_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = subnode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __sub_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/threading.c b/libtlang/src/threading.c new file mode 100644 index 0000000..5a3349c --- /dev/null +++ b/libtlang/src/threading.c @@ -0,0 +1,165 @@ +#include "tlang.h" + +#if defined(USE_THREADS) + +bool thread_init=false; +#if defined(GEKKO) +#include +#include +#else +#include +#endif + +tmutex_t global_mutex; + +tmutex_t Mutex_Create() +{ + #if defined(GEKKO) + mutex_t mutex=LWP_MUTEX_NULL; + LWP_MutexInit(&mutex,true); + return (tmutex_t)mutex; + #else + pthread_mutex_t* mtx= (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); + memset(mtx,0,sizeof(pthread_mutex_t)); + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + #if defined(PTHREAD_MUTEX_RECURSIVE_NP) + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); + #else + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + #endif + pthread_mutex_init(mtx,&attr); + return (tmutex_t)mtx; + #endif +} +void Mutex_Free(tmutex_t mutex) +{ + #if defined(GEKKO) + LWP_MutexDestroy((mutex_t)mutex); + #else + pthread_mutex_destroy((pthread_mutex_t*)mutex); + free(mutex); + #endif + +} +int64_t Mutex_Lock(tmutex_t mutex) +{ + #if defined(GEKKO) + return LWP_MutexLock((mutex_t)mutex); + #else + return pthread_mutex_lock((pthread_mutex_t*)mutex); + #endif +} +int64_t Mutex_Unlock(tmutex_t mutex) +{ + #if defined(GEKKO) + LWP_MutexUnlock((mutex_t)mutex); + #else + return pthread_mutex_unlock((pthread_mutex_t*)mutex); + #endif +} +void Global_Mutex_Lock() +{ + if(thread_init) + Mutex_Lock(global_mutex); +} +void Global_Mutex_Unlock() +{ + if(thread_init) + Mutex_Unlock(global_mutex); +} + + +void Threading_Thread_Create(void*(*entry)(void*),void* arg) +{ + #if defined(GEKKO) + lwp_t lwp=LWP_THREAD_NULL; + LWP_CreateThread(&lwp,entry,arg,NULL,0,64); + #else + pthread_t thrd; + pthread_create(&thrd,NULL,entry,arg); + pthread_detach(thrd); + #endif + +} +typedef struct { + tobject_t* obj; + runtime_t* rt; + +} __thread_arg_ctx; + +static void* __create_thread_thread_func(void* data) +{ + __thread_arg_ctx* ctx = (__thread_arg_ctx*)data; + list_tobject_t objs; + list_create(ctx->rt,&objs,0); + tobject_t* obj=tobject_call(ctx->rt->globals,ctx->obj,&objs); + tobject_freeifzero(obj); + tobject_rmref(ctx->obj); + free(data); +} +void __create_mutex_destroy(tobject_t* obj) +{ + Mutex_Free((tmutex_t)obj->ptr_data); +} +tobject_t* __create_mutex_lock_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + Mutex_Lock((tmutex_t)ptr); + return tobject_basic(rt,tundef); +} +tobject_t* __create_mutex_unlock_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + Mutex_Unlock((tmutex_t)ptr); + return tobject_basic(rt,tundef); +} +tobject_t* __create_mutex_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + tobject_t* o = tobject_create(rt); + o->type = tdict; + o->data.dict = dict_create(); + tmutex_t* mutex=Mutex_Create(); + o->ptr_data = mutex; + runtime_create_method_on_dict(o->data.dict,"lock",rt,mutex,__create_mutex_lock_external_method,NULL); + runtime_create_method_on_dict(o->data.dict,"unlock",rt,mutex,__create_mutex_unlock_external_method,NULL); + o->free = __create_mutex_destroy; + return o; +} +tobject_t* __create_thread_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length > 0) + { + tobject_t* data=args->items[0]; + if(data->type == tinternalmethod || data->type == texternalmethod) + { + data->count++; + __thread_arg_ctx* ctx = (__thread_arg_ctx*)malloc(sizeof(__thread_arg_ctx)); + ctx->obj = data; + ctx->rt = rt; + Threading_Thread_Create(__create_thread_thread_func,(void*)ctx); + } + } + return tobject_basic(rt,tundef); +} +void runtime_register_threads(runtime_t* rt,dict_t* create) +{ + if(!thread_init) + { + global_mutex = Mutex_Create(); + thread_init = true; + } + + runtime_create_method_on_dict(create,"thread",rt,NULL,__create_thread_external_method,NULL); + runtime_create_method_on_dict(create,"mutex",rt,NULL,__create_mutex_external_method,NULL); +} + +#else +void Global_Mutex_Lock() +{ + +} +void Global_Mutex_Unlock() +{ + +} +#endif + diff --git a/libtlang/src/tobject.c b/libtlang/src/tobject.c new file mode 100644 index 0000000..8f62b55 --- /dev/null +++ b/libtlang/src/tobject.c @@ -0,0 +1,506 @@ +#include "tlang.h" +#include +tobject_t* tobject_same(runtime_t* rt,tobject_t* l,tobject_t* r,bool* freeleftifzero) +{ + + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"eq")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"eq"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(rt,&ls,0); + list_add(&ls,r); + out=tobject_call(rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + + *freeleftifzero = false; + } else { + if(r->type == tdict) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean = l == r; + + } + } + }else { + if(r->type == tdict) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean = l == r; + + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean = l->data.number == r->data.number; + + } + if(ltype == tbool && rtype == tbool) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean = l->data.boolean == r->data.boolean; + + } + if(ltype == tstring && rtype == tstring) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean =string_sames(l->data.string,r->data.string); + } + if(ltype == tchar && rtype == tchar) + { + out = tobject_create(rt); + out->type = tbool; + out->data.boolean = l->data.chr == r->data.chr; + } + if(out == NULL) + { + out = tobject_bool(rt,false); + } + + return out; +} +bool tobject_sameb(runtime_t* rt,tobject_t* l,tobject_t* r) +{ + bool freeleftifzero=true; + bool ret0=false; + tobject_t* ret=tobject_same(rt,l,r,&freeleftifzero); + ret0=tobject_tobool(ret); + ret->count++; + if(freeleftifzero) + { + tobject_freeifzero(l); + } + tobject_freeifzero(r); + ret->count--; + tobject_freeifzero(ret); + return ret0; +} +tobject_t* tobject_create(runtime_t* rt) +{ + tobject_t* obj =(tobject_t*)malloc(sizeof(tobject_t)); + memset(obj,0,sizeof(tobject_t)); + obj->count = 0; + obj->type = tnull; + obj->free = NULL; + return obj; +} +void list_create(runtime_t* rt,list_tobject_t* ls,int count) +{ + ls->rt = rt; + ls->length = count; + ls->capacity = count + 128; + ls->items = (tobject_t**)malloc(sizeof(tobject_t*) * ls->capacity); + int i; + for(i=0;ilength;i++) + { + ls->items[i] = tobject_create(rt); + ls->items[i]->count = 1; + } +} +void list_set(list_tobject_t* a,int index,tobject_t* item) +{ + + if(index >= a->length) + { + index=a->length-1; + } + if(index==-1) + { + list_add(a,item); + }else{ + tobject_addref(item); // just in case someone sets a variable to itself + tobject_rmref(a->items[index]); + a->items[index] = item; + + } +} +int list_indexof(list_tobject_t* a,tobject_t* item) +{ + int index = -1; + int i; + item->count++; + for(i=0;ilength;i++) + { + bool res= tobject_sameb(a->rt,a->items[0],item); + if(res) + { + index=i; + break; + } + } + item->count--; + return index; +} +bool list_contains(list_tobject_t* a,tobject_t* item) +{ + return list_indexof(a,item) > -1; +} +void list_remove(list_tobject_t* a,tobject_t* item) +{ + int i= list_indexof(a,item); + if(i>-1) list_rm(a,i); +} +void list_add(list_tobject_t* a,tobject_t* item) +{ + tobject_addref(item); + if(a->length + 1 > a->capacity) + { + a->capacity = a->length + 128; + a->items=(tobject_t**)realloc(a->items,a->capacity * sizeof(tobject_t*)); + + } + a->items[a->length++] = item; +} +tobject_t* list_at(list_tobject_t* a,int offset) +{ + if(offsetlength) return a->items[offset]; + return NULL; +} +void list_rm(list_tobject_t* a,int offset) +{ + if(offsetlength) + { + tobject_rmref(a->items[offset]); + int i; + a->length--; + for(i=offset;ilength;i++) + { + a->items[i] = a->items[i+1]; + } + } +} +void list_clear(list_tobject_t* a) +{ + int i; + for(i=0;ilength;i++) + { + tobject_rmref(a->items[i]); + } + a->length = 0; +} +void list_free(list_tobject_t* a) +{ + list_clear(a); + free(a->items); + +} +tobject_t* tobject_create_array(runtime_t* rt,int count) +{ + tobject_t* obj= tobject_create(rt); + obj->type = tlist; + list_create(rt,&obj->data.list,count); + return obj; +} +tobject_t* tobject_basic(runtime_t* rt,tobject_type_t type) +{ + tobject_t* o=tobject_create(rt); + o->type = type; + return o; +} +tobject_t* tobject_fromexternalmethod(runtime_t* rt,void* data,external_method_t method,texternalmethod_free_t free) +{ + tobject_t* obj= tobject_create(rt); + obj->type = texternalmethod; + obj->data.external_method.data = data; + obj->data.external_method.method = method; + obj->free = free; + obj->count=0; + + return obj; +} + + int requiredArguments(list_string_t* str) + { + int i; + for(i =0;icount;i++) + { + if(str->strings[i]->length > 0 && str->strings[i]->text[0] == '$') + { + return i; + } + } + return str->count; + } +int optionalArgs(list_string_t* str,int argLen) +{ + int i; + for(i =0;icount;i++) + { + string_t* str0=str->strings[i]; + if(str0->length > 2 && str0->text[0] == '$' && str0->text[1] == '$') + { + if(argLen < i) + return argLen; + return i; + } + } + if(argLen < str->count) + return argLen; + return str->count; + +} + +tobject_t* tobject_call(scope_t* scope,tobject_t* func,list_tobject_t* args) +{ + if(func->type == texternalmethod) + { + + return func->data.external_method.method(scope->rt,func->data.external_method.data,args); + } + if(func->type == tinternalmethod) + { + //execute_internal_method + tobject_t* res; + scope_t* scope2=scope_begin(scope); + list_string_t* strs=&func->data.internal_method.closure->data.closure_node.argNames; + int argCountClosure = strs->count;//Body.Arguments.Count; + int argCountCaller = args->length; + if(argCountCaller < requiredArguments(strs)) + { + return tobject_basic(scope->rt,tnull); + } + int i = 0; + for(;iitems[i]; + char* t = string_dupp(strs->strings[i]); + + scope2->setvariable(scope2,string_trimstart(t,'$'),_obj); + free(t); + } + + if(i==argCountClosure-1) + { + //do tarray + tobject_t* array= tobject_create_array(scope->rt,0); + char* t = string_dupp(strs->strings[i]); + scope2->setvariable(scope2,string_trimstart(t,'$'),array); + free(t); + for(;ilength;i++) + { + tobject_t* _obj=args->items[i]; + + list_add(&array->data.list,_obj); + } + + } + res=func->data.internal_method.closure->data.closure_node.node->execute(func->data.internal_method.closure->data.closure_node.node,scope2); + scope_end(scope2); + return res; + } + return tobject_basic(scope->rt,tundef); +} +void tobject_addref(tobject_t* obj) +{ + obj->count++; +} +void tobject_freeifzero(tobject_t* obj) +{ + if(obj->count<=0) + { + if(obj->type == tdict) + { + dict_free(obj->data.dict); + } + if(obj->type == tlist) + { + list_free(&obj->data.list); + } + if(obj->type == tstring) + { + string_free(obj->data.string); + } + if(obj->type == tinternalmethod) + { + node_t* n = obj->data.internal_method.closure; + node_free(n->data.closure_node.node); + int i; + for(i=0;idata.closure_node.argNames.count;i++) + { + string_free(n->data.closure_node.argNames.strings[i]); + } + free(n->data.closure_node.argNames.strings); + node_free(n); + tscope_rmref(obj->data.internal_method.scope); + + } + if(obj->free != NULL) + { + obj->free(obj); + } + free(obj); + + } +} +void tobject_rmref(tobject_t* obj) +{ + obj->count--; + tobject_freeifzero(obj); + +} +tobject_t* tobject_number(runtime_t* rt,double num) +{ + tobject_t* o = tobject_create(rt); + o->data.number = num; + o->type = tnumber; + return o; +} +tobject_t* tobject_charp(runtime_t* rt,const char* text) +{ + string_t* s = string_create(); + string_appendp(s,text); + return tobject_string(rt,s); +} +tobject_t* tobject_string(runtime_t* rt,string_t* str) +{ + tobject_t* o = tobject_create(rt); + o->data.string = str; + o->type = tstring; + return o; +} +string_t* tobject_tostring(scope_t* sc,tobject_t* s) +{ + if(s->type == tstring) + { + + string_t* s2= string_dups(s->data.string); + tobject_freeifzero(s); + return s2; + } + if(s->type == tnumber) + { + string_t* s2 = string_create(); + string_appendn(s2,s->data.number); + tobject_freeifzero(s); + return s2; + } + if(s->type == tchar) + { + string_t* s2 = string_create(); + string_appendc(s2,s->data.chr); + tobject_freeifzero(s); + return s2; + } + if(s->type == tbool) + { + string_t* s2 = string_create(); + string_appendp(s2,s->data.boolean ? "true" : "false"); + tobject_freeifzero(s); + return s2; + } + if(s->type == tnull) + { + string_t* s2 = string_create(); + string_appendp(s2, "null"); + tobject_freeifzero(s); + return s2; + } + if(s->type == tdict) + { + string_t* s2 = string_create(); + if(dict_haskey(s->data.dict,"toString")) + { + tobject_t* o2 = dict_getkvp(s->data.dict,"toString")->value; + if(o2->type == texternalmethod || o2->type == tinternalmethod) + { + list_tobject_t o; + list_create(sc->rt,&o,0); + tobject_t* res=tobject_call(sc,o2,&o); + list_free(&o); + + + string_t* str=tobject_tostring(sc,res); + + tobject_freeifzero(o2); + tobject_freeifzero(s); + return str; + + } + } + + tobject_freeifzero(s); + return s2; + } + + string_t* s3 = string_create(); + string_appendp(s3, "undefined"); + return s3; +} + +tobject_t* tobject_bool(runtime_t* rt,bool b) +{ + tobject_t* o = tobject_create(rt); + o->data.boolean = b; + o->type = tbool; + return o; +} +tobject_t* tobject_stringp(runtime_t* rt,const char* ptr) +{ + string_t* str=string_create(); + string_appendp(str,ptr); + return tobject_string(rt,str); +} +tobject_t* tobject_char(runtime_t* rt,char c) +{ + tobject_t* o = tobject_create(rt); + o->data.chr = c; + o->type = tchar; + return o; +} +bool tobject_tobool(tobject_t* o) +{ + if(o->type == tbool) + { + return o->data.boolean; + } + if(o->type == tnumber) + { + return o->data.number != 0; + } + if(o->type == tchar) + { + return o->data.chr != 0; + } + if(o->type == tdict) + { + return true; + } + if(o->type == tlist) + { + return o->data.list.length > 0; + } + if(o->type == tstring) + { + return o->data.list.length > 0; + } + if(o->type == texternalmethod) + { + return true; + } + if(o->type == tinternalmethod) + { + return true; + } + if(o->type == tnull) + { + return false; + } + + return false; +} \ No newline at end of file diff --git a/libtlang/src/whileloop.c b/libtlang/src/whileloop.c new file mode 100644 index 0000000..1cb6600 --- /dev/null +++ b/libtlang/src/whileloop.c @@ -0,0 +1,48 @@ +#include "tlang.h" +extern tobject_t* __loop_exec2(node_t* n,scope_t* s); +extern bool __loop_bool(node_t* n,scope_t* s); +extern tobject_t* last_ittr_loop(runtime_t* rt,void* ptr,list_tobject_t* obj); +node_t* __while_loop_exec(node_t* n,scope_t* s) +{ + bool isRunning=true; + s=scope_begin(s); + + + s->setvariable(s,"last_ittr",tobject_fromexternalmethod(s->rt,&isRunning,last_ittr_loop,NULL)); + tobject_t* res = tobject_basic(s->rt,tundef); + res->count=1; + if(!n->data.whileloop_node.isDoLoop) + { + isRunning = __loop_bool(n->data.whileloop_node.condition,s); + } + while(isRunning) + { + tobject_t* _r = __loop_exec2(n->data.whileloop_node.body,s); + tobject_addref(_r); + tobject_rmref(res); + res=_r; + if(isRunning) + { + isRunning = __loop_bool(n->data.whileloop_node.condition,s); + } + } + s=scope_end(s); + res->count--; + return res; +} +void __while_loop_free(node_t* n) +{ + node_free(n->data.whileloop_node.condition); + node_free(n->data.whileloop_node.body); +} +node_t* node_while_loop_create(node_t* condition,node_t* body,bool isDoLoop) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = whileloopnode; + node->data.whileloop_node.isDoLoop = isDoLoop; + node->data.whileloop_node.condition = condition; + node->data.whileloop_node.body = body; + node->execute = __while_loop_exec; + node->free = __while_loop_free; + return node; +} \ No newline at end of file diff --git a/libtlang/src/wpad-wii.c b/libtlang/src/wpad-wii.c new file mode 100644 index 0000000..6e190e6 --- /dev/null +++ b/libtlang/src/wpad-wii.c @@ -0,0 +1,223 @@ +#if defined(HW_RVL) +#include "tlang.h" +#include + +tobject_t* wpad=NULL; +bool init=false; +struct { + list_tobject_t down_handler; + list_tobject_t up_handler; +} wpad_data[4]; + +typedef struct { + tobject_t* obj; + int chan; +} eventctx_t; + +int* channels = {WPAD_CHAN_0,WPAD_CHAN_1,WPAD_CHAN_2,WPAD_CHAN_3}; +tobject_t* __wpad_get_buttons_down_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + u32 b = WPAD_ButtonsDown(*((int*)ptr)); + return tobject_number(rt,b); +} +void __free_eventctx(tobject_t* o) +{ + free(o->data.external_method.data); +} + +tobject_t* __wpad_get_buttons_up_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + u32 b = WPAD_ButtonsUp(*((int*)ptr)); + return tobject_number(rt,b); +} +tobject_t* __wpad_init_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + + if(!init) + { + int i; + for(i = 0;i<4;i++){ + list_create(rt,&wpad_data[i].down_handler,0); + list_create(rt,&wpad_data[i].up_handler,0); + } + return tobject_number(rt,WPAD_Init()); + } +} +tobject_t* __wpad_remove_up_handler(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length >= 0) + { + eventctx_t* ctx = (eventctx_t*)ptr; + + list_remove(&wpad_data[ctx->chan].up_handler,args->items[0]); + return ctx->obj; + } + return tobject_basic(rt,tnull); +} +tobject_t* __wpad_add_up_handler(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length >= 0) + { + eventctx_t* ctx = (eventctx_t*)ptr; + + list_add(&wpad_data[ctx->chan].up_handler,args->items[0]); + return ctx->obj; + } + return tobject_basic(rt,tnull); +} +tobject_t* __wpad_remove_down_handler(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length >= 0) + { + eventctx_t* ctx = (eventctx_t*)ptr; + + list_remove(&wpad_data[ctx->chan].down_handler,args->items[0]); + return ctx->obj; + } + return tobject_basic(rt,tnull); +} +tobject_t* __wpad_add_down_handler(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + if(args->length >= 0) + { + eventctx_t* ctx = (eventctx_t*)ptr; + + list_add(&wpad_data[ctx->chan].down_handler,args->items[0]); + return ctx->obj; + } + return tobject_basic(rt,tnull); +} +void wpad_send_up_command(runtime_t* rt) +{ + int i; + for(i=0;i<4;i++) + { + tobject_t* o = tobject_create(rt); + o->type = tdict; + o->data.dict = dict_create(); + kvp_t kvp0; + kvp0.key = "channel"; + kvp0.value = tobject_number(rt,i); + dict_setkvp(o->data.dict,kvp0); + kvp0.key = "buttons"; + kvp0.value = tobject_number(rt,WPAD_ButtonsUp(i)); + dict_setkvp(o->data.dict,kvp0); + int j; + list_tobject_t myArgs; + list_add(&myArgs,wpad); + list_add(&myArgs,o); + for(j=0;jtype == texternalmethod || res->type == tinternalmethod) + { + + tobject_call(rt->globals,res,&myArgs); + } + } + list_free(&myArgs); + } +} +void wpad_send_down_command(runtime_t* rt) +{ + int i; + for(i=0;i<4;i++) + { + tobject_t* o = tobject_create(rt); + o->type = tdict; + o->data.dict = dict_create(); + kvp_t kvp0; + kvp0.key = "channel"; + kvp0.value = tobject_number(rt,i); + dict_setkvp(o->data.dict,kvp0); + kvp0.key = "buttons"; + kvp0.value = tobject_number(rt,WPAD_ButtonsDown(i)); + dict_setkvp(o->data.dict,kvp0); + int j; + list_tobject_t myArgs; + list_add(&myArgs,wpad); + list_add(&myArgs,o); + for(j=0;jtype == texternalmethod || res->type == tinternalmethod) + { + + tobject_call(rt->globals,res,&myArgs); + } + } + list_free(&myArgs); + } +} +tobject_t* __wpad_scanpads_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + s32 s =WPAD_ScanPads(); + + wpad_send_down_command(rt); + wpad_send_up_command(rt); + + return tobject_number(rt,s); +} +tobject_t* __wpad_at_external_method(runtime_t* rt,void* ptr,list_tobject_t* args) +{ + //at(i) + if(args->length > 0 && args->items[0]->type == tnumber && args->items[0]->data.number >= 0 && args->items[0]->data.number < 4) + { + tobject_t* wpad_chan = tobject_create(rt); + wpad_chan->type = tdict; + wpad_chan->data.dict = dict_create(); + runtime_create_method_on_dict(wpad->data.dict,"getbuttonsdown",rt,&channels[(int)args->items[0]->data.number],__wpad_get_buttons_down_external_method,NULL); + runtime_create_method_on_dict(wpad->data.dict,"getbuttonsup",rt,&channels[(int)args->items[0]->data.number],__wpad_get_buttons_up_external_method,NULL); + + tobject_t* downEvent = tobject_create(rt); + downEvent->type = tdict; + downEvent->data.dict = dict_create(); + tobject_t* upEvent = tobject_create(rt); + upEvent->type = tdict; + upEvent->data.dict = dict_create(); + eventctx_t* addObjDown = (eventctx_t*)malloc(sizeof(eventctx_t)); + addObjDown->chan = (int)args->items[0]->data.number; + addObjDown->obj = downEvent; + eventctx_t* subObjDown = (eventctx_t*)malloc(sizeof(eventctx_t)); + subObjDown->chan = (int)args->items[0]->data.number; + subObjDown->obj = downEvent; + eventctx_t* addObjUp = (eventctx_t*)malloc(sizeof(eventctx_t)); + addObjUp->chan = (int)args->items[0]->data.number; + addObjUp->obj = upEvent; + eventctx_t* subObjUp = (eventctx_t*)malloc(sizeof(eventctx_t)); + subObjDown->chan = (int)args->items[0]->data.number; + subObjUp->obj = upEvent; + runtime_create_method_on_dict(downEvent->data.dict,"add",rt,addObjDown,__wpad_add_down_handler,__free_eventctx); + runtime_create_method_on_dict(downEvent->data.dict,"sub",rt,subObjDown,__wpad_remove_down_handler,__free_eventctx); + + runtime_create_method_on_dict(upEvent->data.dict,"add",rt,addObjUp,__wpad_add_up_handler,__free_eventctx); + runtime_create_method_on_dict(upEvent->data.dict,"sub",rt,subObjUp,__wpad_remove_up_handler,__free_eventctx); + kvp_t kvp; + kvp.key = "down"; + kvp.value = downEvent; + dict_setkvp(wpad_chan->data.dict,kvp); + kvp.key = "up"; + kvp.value = upEvent; + dict_setkvp(wpad_chan->data.dict,kvp); + + return wpad_chan; + } + return tobject_basic(rt,tundef); +} + +void __reg_wpad(runtime_t* rt) +{ + if(wpad ==NULL){ + wpad = tobject_create(rt); + wpad->type = tdict; + wpad->data.dict = dict_create(); + wpad->count = 1; + runtime_create_method_on_dict(wpad->data.dict,"init",rt,NULL,__wpad_init_external_method,NULL); + runtime_create_method_on_dict(wpad->data.dict,"scanpads",rt,NULL,__wpad_scanpads_external_method,NULL); + runtime_create_method_on_dict(wpad->data.dict,"at",rt,NULL,__wpad_at_external_method,NULL); + + } + //dict_setkvp(wpad->data.dict,); + rt->globals->setvariable(rt->globals,"wpad",wpad); +} +#endif \ No newline at end of file diff --git a/libtlang/src/xor.c b/libtlang/src/xor.c new file mode 100644 index 0000000..8743824 --- /dev/null +++ b/libtlang/src/xor.c @@ -0,0 +1,68 @@ +#include "tlang.h" + +extern void __node_two_free(node_t* n); +tobject_t* __xor_node_exec(node_t* n,scope_t* sc) +{ + bool freeleftifzero=true; + + node_t* left = n->data.two_node_node.left; + node_t* right = n->data.two_node_node.right; + tobject_t* l=left->execute(left,sc); + tobject_t* r=right->execute(right,sc); + tobject_t* out=NULL; + tobject_type_t ltype=l->type; + tobject_type_t rtype =r->type; + if(ltype == tdict) + { + if(dict_haskey(l->data.dict,"xor")) + { + kvp_t* kvp=dict_getkvp(l->data.dict,"xor"); + if(kvp->value->type == texternalmethod || kvp->value->type == tinternalmethod) + { + list_tobject_t ls; + list_create(sc->rt,&ls,0); + list_add(&ls,r); + out=tobject_call(sc->rt->globals,kvp->value,&ls); + + out->count++; //ensure i dont get deleted + tobject_freeifzero(l); + out->count--; //set me back bitch + r->count++; + list_free(&ls); + r->count--; + freeleftifzero = false; + } + } + } + if(ltype == tnumber && rtype == tnumber) + { + out = tobject_create(sc->rt); + out->type = tnumber; + out->data.number = (int64_t)l->data.number ^ (int64_t)r->data.number; + + } + + + if(freeleftifzero) + { + tobject_freeifzero(l); + } + + tobject_freeifzero(r); + + if(out == NULL) + { + return tobject_basic(sc->rt,tnull); + } + return out; +} +node_t* node_xor_create(node_t* left,node_t* right) +{ + node_t* node = (node_t*)malloc(sizeof(node_t)); + node->type = xornode; + node->data.two_node_node.left = left; + node->data.two_node_node.right = right; + node->execute = __xor_node_exec; + node->free = __node_two_free; + return node; +} \ No newline at end of file diff --git a/tlang/Makefile b/tlang/Makefile new file mode 100644 index 0000000..7dacb03 --- /dev/null +++ b/tlang/Makefile @@ -0,0 +1,22 @@ +CC :=gcc +AR := ar +CFLAGS := -Wall -g + +target := tlang +tlang_c_source_files := $(shell find src/ -name *.c) +tlang_c_object_files := $(patsubst src/%.c, build/%.o, $(tlang_c_source_files)) + + +$(target): $(tlang_c_object_files) + $(CC) -o $(target) $(CFLAGS) -L../libtlang $(tlang_c_object_files) -ltlang -lpthread -lSDL2 -lSDL2_gfx -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lm + +$(tlang_c_object_files): build/%.o : src/%.c + mkdir -p $(dir $@) && \ + $(CC) $(CFLAGS) -c -I../libtlang/include $(patsubst build/%.o, src/%.c, $@) -o $@ +.PHONY: clean +.PHONY: install +install: $(target) + rm /usr/local/bin/tlang > /dev/null; true + cp $(target) /usr/local/bin/tlang +clean: + rm -rf build; rm $(target) diff --git a/tlang/src/main.c b/tlang/src/main.c new file mode 100644 index 0000000..091f171 --- /dev/null +++ b/tlang/src/main.c @@ -0,0 +1,60 @@ +#include "tlang.h" +#include +void register_funcs(runtime_t* rt) +{ + +} + +int main(int argc,char** argv) +{ + runtime_t* rt = runtime_init(); + runtime_register(rt,register_funcs); + if(argc < 2) + { + string_t* cmd=string_create(); + printf("TLang interactive shell.\nType \"exit\" to exit.\n"); + while(true) + { + string_clear(cmd); + int c=-1; + printf("> "); + while((c = fgetc(stdin)) != '\n') + { + string_appendc(cmd,(char)c); + } + if(string_samep(cmd,"exit")) break; + runtime_load(rt,cmd); + tobject_t* o = rt->program->execute(rt->program,rt->globals); + tobject_freeifzero(o); + node_free(rt->program); + rt->program=NULL; + } + string_free(cmd); + } + else { + string_t* cmd=string_create(); + + FILE* f = fopen(argv[1],"r"); + if(f != NULL){ + string_read(cmd,f,fread); + fclose(f); + } + + runtime_load(rt,cmd); + int i; + for(i=2;i +INCLUDE: #include "WHATEVERYOUTYPEAFTER INCLUDE: " +FUNCTION: TLang Signature = C Signature (ex number tlangFunc(string,char,number,bool) = int tlangFunc(const char* c,char a,int32_t num,bool someBool)) +NUMBER: VARIABLE_NAME 42.5 +CHAR: VARIABLE_NAME j +STRING: VARIABLE_NAME YOUR TEXT STRING\" supports C escaping (hince it is passed into C compiler) +TRUE: SOME_CONSTANT_TRUE_VALUE +FALSE: SOME_CONSTANT_FALSE_VALUE +NULL: SOME_CONSTANT_NULL_VALUE \ No newline at end of file diff --git a/wrappergen/example-c-code/example-lib.c b/wrappergen/example-c-code/example-lib.c new file mode 100644 index 0000000..328cf91 --- /dev/null +++ b/wrappergen/example-c-code/example-lib.c @@ -0,0 +1,10 @@ +#include +#include "example.h" +int myfunction(int a,int b) +{ + return a + b; +} +void printNum(int a) +{ + printf("%i\n"); +} diff --git a/wrappergen/example-c-code/example.h b/wrappergen/example-c-code/example.h new file mode 100644 index 0000000..efd0315 --- /dev/null +++ b/wrappergen/example-c-code/example.h @@ -0,0 +1,3 @@ +#pragma once +int myfunction(int a,int b); +void printNum(int a); \ No newline at end of file diff --git a/wrappergen/src/data.txt b/wrappergen/src/data.txt new file mode 100644 index 0000000..de3f7cf --- /dev/null +++ b/wrappergen/src/data.txt @@ -0,0 +1,5 @@ +NAME: example +INCLUDE: example.h +FUNCTION: number myfunction(number,number) = int myfunction(int a,int b) +FUNCTION: null printNum(number) = void printNum(int a) +END: \ No newline at end of file diff --git a/wrappergen/src/wrappergen.tlang b/wrappergen/src/wrappergen.tlang new file mode 100755 index 0000000..46b42fa --- /dev/null +++ b/wrappergen/src/wrappergen.tlang @@ -0,0 +1,437 @@ +#!/usr/local/bin/tlang +__internal_tlang ="\""; +__internal_tlang2 ="\""; +//valid identifiers for function args: char, bool, number, string, null +//numbers are used for pointers +//if a function requires list (array in language), dict, null or undefined implement those functions yourself + + +func basic_c_lex(text) +{ + myText = ""; + tokens = create.array(); + token = -1; + + func flush() + { + if(myText.length > 0) + { + if(myText != "const" && myText != "restrict") //add more keywords to ignore + tokens.add(myText); + myText = ""; + } + } + + i = 0; + func read_c_number() +{ + badVal=true; + token = -1; + if(ilength > NO_ARGS && args->items[N]->type == typeN ...) + { + //do C stuff in here + } + return tobject_basic(rt,tnull); + } +*/ + +/* + String -> PTR + TYPE* _argN = (TYPE*)string_dupp(args->items[N]->data.string); + + end: + free(_argN); //use string_nofree if you dont want this +*/ + +/* + PTR -> String + char* _res = (char*)YOURFUNC(ARGS); + string_t* _ress=string_create(); + string_appendp(_ress,_res); + free(_res); //use string_nofree if you dont want this + return tobject_string(rt,_ress); +*/ +func generateFuncCall(tlangRet,fName,argCount) +{ + res = ""; + if(tlangRet == "string_nofree" || tlangRet == "string") + { + res = "char* _res = (char*)"; + }else if(tlangRet == "number") + { + res = "double _res = (double)"; + }else if(tlangRet == "bool") + { + res = "bool _res = (bool)"; + }else if(tlangRet == "char") + { + res = "char _res = (char)"; + } + if(argCount>0) + { + res += fName + "(_arg0"; + for(i=1;iitems[N]->data.string); + res += " _arg" + num + " = (" + c + ")string_dupp(args->items[" + num + "]->data.string);" + } + else if(tlang == "number") + { + res += " _arg" + num + " = (" + c + ")args->items[" + num + "]->data.number;"; + } + else if(tlang == "char") + { + res += " _arg" + num + " = (" + c + ")args->items[" + num + "]->data.chr;"; + } + else if(tlang == "bool") + { + res += " _arg" + num + " = (" + c + ")args->items[" + num + "]->data.boolean;"; + } + res; +} +func printToken(t) +{ + if(t.token == "assign") + { + console.writeln("BEGIN ASSIGN:"); + console.writeln("LEFT:"); + printToken(t.left); + console.writeln(); + console.writeln("RIGHT:"); + te = t.right; + printToken(te); + console.writeln("END ASSIGN"); + } + else if(t.token == "function") + { + console.write("BEGIN FUNCTION WITH NAME "); + console.write(t.name); + console.writeln(":"); + console.write("RETURN TYPE: "); + console.writeln(t.type); + console.writeln("ARGS:"); + each(t.args) + { + console.writeln(item); + } + console.writeln("END FUNCTION"); + } +} +func generateFuncBody(parse,name) +{ + str = "tobject_t* "+ name +"_" + parse.left.name + "_external_function(runtime_t* rt,void* ptr,list_tobject_t* args)\n{\nif(args->length >= " + parse.left.args.length; + //&& args->items[N]->type == typeN" + for(i = 0;itype == " + t; + } + str += ")\n{\n"; + + for(i=0;idata.dict,kvp_" + key + ");" +} +func runtime_on_dict_name(parse,name) +{ + "runtime_create_method_on_dict(_"+name+"->data.dict,\""+parse.left.name+"\",rt,NULL," + name +"_" + parse.left.name + "_external_function,NULL);"; +} +func runtime_add_func(text,obj) +{ + l = create.dict(); + l.tokens = basic_c_lex(text); + l.i = 0; + e = parse_equals(l); + obj.funcs += generateFuncBody(e,obj.name) + "\n"; + obj.calls += runtime_on_dict_name(e,obj.name) + "\n"; + +} +func getLs(l,name) +{ + _w2 = true; + _w = null; + each(l) + { + if(item.name == name) + { + _w2 = false; + _w = item; + last_ittr(); + } + } + + if(_w2) + { + + _w = create.dict(); + _w.funcs = ""; + _w.calls = ""; + _w.includes = ""; + _w.name = name; + l.add(_w); + } + _w; +} +func main(src,destDir,$internal_tlang) +{ + if(internal_tlang == undefined) + { + internal_tlang = ""; + } + if(internal_tlang == "--external") + { + __internal_tlang = "<"; + __internal_tlang2 = ">"; + } + ls = create.array(); + lines=fs.readalltext(src); + lines = lines.replace("\r",""); + lines = lines.split("\n"); + curName = ""; + wrappers = create.array(); + each(lines) + { + j = item.split(": ",true,2); + if(j[0]=="NAME") + { + curName = j[1]; + }else + if(j[0]=="END") + { + last_ittr(); + } + else if(j[0] == "INCLUDE_PATH") + { + mls=getLs(wrappers,curName); + mls.includes += "#include <" + j[1] + ">\n"; + } + else if(j[0] == "INCLUDE") + { + mls=getLs(wrappers,curName); + mls.includes += "#include \"" + j[1] + "\"\n"; + } + else if(j[0] == "FUNCTION") + { + mls=getLs(wrappers,curName); + runtime_add_func(j[1],mls) + } + else if(j[0] == "NUMBER") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_number(rt," + res[1] + ")") + "\n"; + } + else if(j[0] == "NULL") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_basic(rt,tnull)") + "\n"; + } + else if(j[0] == "TRUE") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_bool(rt,true)") + "\n"; + } + else if(j[0] == "FALSE") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_bool(rt,false)") + "\n"; + } + else if(j[0] == "CHAR") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_char(rt,\'" + res[1] + "\')") + "\n"; + } + else if(j[0] == "STRING") + { + mls=getLs(wrappers,curName); + res = j[1].split(" ",true,2); + mls.calls += runtime_on_dict_name_const(curName,res[0],"tobject_stringp(rt,\"" + res[1] + "\")") + "\n"; + } + } + fs.mkdir(destDir); + each(wrappers) + { + file = fs.combine(destDir,item.name + ".c"); + console.writeln(file); + file_con = "#include " + __internal_tlang + "tlang.h" + __internal_tlang2 + "\n" + item.includes + "\n" + + item.funcs + "\nvoid runtime_register_" + item.name + + "(runtime_t* rt)\n{\n_" + item.name + " = tobject_create(rt);\n_" + + item.name + "->type = tdict;\n_" + item.name + "->data.dict = dict_create();\n" + item.calls + + "\nrt->globals->setvariable(rt->globals,\"" + item.name + "\",_" + item.name + ");\n}\n"; + + fs.writealltext(file,file_con); + + + } +} \ No newline at end of file