From 7cdb37d35e6978c245ca656ba9d55df0a6e362e6 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 9 May 2026 02:56:07 +0200 Subject: Add project skeleton --- CMakeLists.txt | 35 +++ LICENSE | 661 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ call.c | 318 ++++++++++++++++++++++++++ call.h | 28 +++ cgen.c | 166 ++++++++++++++ cgen.h | 21 ++ display.c | 226 +++++++++++++++++++ display.h | 27 +++ div.c | 18 ++ div.h | 8 + errloc.c | 28 +++ errloc.h | 9 + examples/hello.uc | 62 +++++ exit.c | 8 + exit.h | 16 ++ fn.c | 204 +++++++++++++++++ fn.h | 21 ++ gl.c | 24 ++ gl.h | 12 + im.c | 11 + im.h | 9 + kw.c | 30 +++ lex.c | 247 ++++++++++++++++++++ lex.h | 34 +++ lit.c | 102 +++++++++ lit.h | 12 + lk.c | 12 + lk.h | 9 + main.c | 53 +++++ parse.c | 155 +++++++++++++ parse.h | 104 +++++++++ pop.c | 37 +++ pr.c | 289 ++++++++++++++++++++++++ pr.h | 11 + prv.h | 45 ++++ push.c | 26 +++ sdup.c | 16 ++ set.c | 447 ++++++++++++++++++++++++++++++++++++ set.h | 29 +++ stmt.c | 59 +++++ stmt.h | 32 +++ storage.c | 209 +++++++++++++++++ storage.h | 29 +++ td.c | 571 ++++++++++++++++++++++++++++++++++++++++++++++ td.h | 22 ++ type.c | 51 +++++ type.h | 8 + ws.c | 12 + ws.h | 15 ++ 49 files changed, 4578 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 call.c create mode 100644 call.h create mode 100644 cgen.c create mode 100644 cgen.h create mode 100644 display.c create mode 100644 display.h create mode 100644 div.c create mode 100644 div.h create mode 100644 errloc.c create mode 100644 errloc.h create mode 100644 examples/hello.uc create mode 100644 exit.c create mode 100644 exit.h create mode 100644 fn.c create mode 100644 fn.h create mode 100644 gl.c create mode 100644 gl.h create mode 100644 im.c create mode 100644 im.h create mode 100644 kw.c create mode 100644 lex.c create mode 100644 lex.h create mode 100644 lit.c create mode 100644 lit.h create mode 100644 lk.c create mode 100644 lk.h create mode 100644 main.c create mode 100644 parse.c create mode 100644 parse.h create mode 100644 pop.c create mode 100644 pr.c create mode 100644 pr.h create mode 100644 prv.h create mode 100644 push.c create mode 100644 sdup.c create mode 100644 set.c create mode 100644 set.h create mode 100644 stmt.c create mode 100644 stmt.h create mode 100644 storage.c create mode 100644 storage.h create mode 100644 td.c create mode 100644 td.h create mode 100644 type.c create mode 100644 type.h create mode 100644 ws.c create mode 100644 ws.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..baec228 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.5) +project(slcob C) +add_executable(${PROJECT_NAME} + call.c + cgen.c + display.c + div.c + errloc.c + exit.c + fn.c + gl.c + im.c + kw.c + lex.c + lit.c + lk.c + main.c + set.c + parse.c + pop.c + pr.c + push.c + sdup.c + stmt.c + storage.c + td.c + type.c + ws.c +) + +target_compile_options(${PROJECT_NAME} PRIVATE -g -Wall -std=c99 -pedantic) + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(${PROJECT_NAME} PRIVATE -Og) +endif() diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be3f7b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 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 Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are 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. + + 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. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + 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 Affero 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. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + 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 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 work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero 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 Affero 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 Affero 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 Affero 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 Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + 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 AGPL, see +. diff --git a/call.c b/call.c new file mode 100644 index 0000000..ee177b3 --- /dev/null +++ b/call.c @@ -0,0 +1,318 @@ +#include "call.h" +#include "errloc.h" +#include "fn.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include "stmt.h" +#include +#include +#include +#include + +static int callpr(const struct lex *l, struct prv *p); +static int callp(const struct lex *l, struct prv *p); +static int callr(const struct lex *l, struct prv *p); +static int callv(const struct lex *l, struct prv *p); + +static const struct seq seqs[] = +{ + {(const struct step[]){ + {ID}, + {ID, "with"}, + {ID, NULL, 1}, + {ID, "returning"}, + {ID}, + {0}}, .fn = callpr}, + {(const struct step[]){ + {ID}, + {ID, "with"}, + {ID, NULL, 1}, + {0}}, .fn = callp}, + {(const struct step[]){ + {ID}, + {ID, "returning"}, + {ID}, + {0}}, .fn = callr}, + {(const struct step[]){ + {ID}, + {0}}, .fn = callv}, + {0} +}; + +static int param(const struct lex *l, struct prv *p, const struct tk *tk, + struct call *c) +{ + const struct fn *fn = fn_cur(p); + const struct stentry *e = fn_var(fn, tk); + size_t n = c->nparams + 1; + struct callparam *params; + + if (!e) + { + errloc(tk, "undefined reference to parameter \"%s\"", tk->s); + return -1; + } + else if (!(params = realloc(c->params, n * sizeof *params))) + { + perror("realloc(3)"); + return -1; + } + + c->params = params; + c->params[c->nparams++] = (struct callparam){.entry = e}; + return 0; +} + +static int callpr(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk + 2; + struct call *c = &stmt_cur(p)->u.call; + const struct pr *pr = c->pr; + const struct stentry *e; + size_t n = 0; + + while (!kw((tk++)->s)) + n++; + + if (n != pr->nparams) + { + tk -= 2; + errloc(tk, "function \"%s\" expects %zu parameters, " + "but %zu were given", c->tk->s, pr->nparams, n); + return -1; + } + else if (!pr->ret) + { + errloc(tk, "function \"%s\" does not expect any return variable", + c->tk->s); + return -1; + } + + tk = p->stk + 2; + + for (size_t i = 0; i < n; i++) + if (param(l, p, tk++, c)) + return -1; + + if (!(e = fn_var(fn_cur(p), ++tk))) + { + errloc(tk, "undefined reference to return variable \"%s\"", + tk->s); + return -1; + } + else if (pop(l, p)) + return -1; + + fprintf(stderr, "\t\tadding call to %s with %zu params " + "and %s as return variable\n", c->tk->s, n, tk->s); + c->ret = e; + c->nparams = n; + p->stk = ++tk; + return 1; +} + +static int callr(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk + 2; + const struct fn *fn = fn_cur(p); + const struct stentry *e; + struct call *c = &stmt_cur(p)->u.call; + const struct pr *pr = c->pr; + + if (!pr->ret) + { + errloc(tk, "function \"%s\" does not expect any return variable", + c->tk->s); + return -1; + } + else if (pr->nparams) + { + errloc(tk, "function \"%s\" expects %zu parameters, " + "but none were given", c->tk->s, pr->nparams); + return -1; + } + else if (!(e = fn_var(fn, tk))) + { + errloc(tk, "undefined reference to return variable \"%s\"", + tk->s); + return -1; + } + else if (pop(l, p)) + return -1; + + fprintf(stderr, "\t\tadding call to %s with %s as return variable\n", + c->tk->s, tk->s); + c->ret = e; + p->stk = ++tk; + return 1; +} + +static int callp(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk + 2; + struct call *c = &stmt_cur(p)->u.call; + const struct pr *pr = c->pr; + size_t n = 0; + + while (!kw((tk++)->s)) + n++; + + if (pr->nparams != n) + { + errloc(tk, "function \"%s\" expects %zu parameters, " + "but %zu were given", c->tk->s, pr->nparams, n); + return -1; + } + + tk = p->stk + 2; + + for (size_t i = 0; i < n; i++) + if (param(l, p, tk++, c)) + return -1; + + if (pop(l, p)) + return -1; + + fprintf(stderr, "\t\tadding call to %s with 1 param\n", c->tk->s); + p->stk = tk; + return 1; +} + +static int callv(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + struct call *c = &stmt_cur(p)->u.call; + const struct pr *pr = c->pr; + + if (pr->nparams) + { + errloc(tk, "function \"%s\" expects %zu parameters, " + "but none were given", c->tk->s, pr->nparams); + return -1; + } + else if (pop(l, p)) + return -1; + + fprintf(stderr, "\t\tadding call to %s with %s as return variable\n", + c->tk->s, tk->s); + p->stk = ++tk; + return 1; +} + +static const struct pr *find(const struct ast *ast, const char *s) +{ + for (size_t i = 0; i < ast->nfns; i++) + { + const struct fn *fn = &ast->fns[i]; + + if (!strcmp(fn->tk->s, s)) + return fn->pr; + } + + return NULL; +} + +int call_cgen(const struct call *m, struct cgen *c) +{ + const struct stentry *ret = m->ret; + + if (ret) + printf("%%r =%s ", cgen_sz(ret->t->sz)); + + printf("call $%s(", m->tk->s); + + for (size_t i = 0; i < m->nparams; i++) + { + const struct callparam *cp = &m->params[i]; + const struct stentry *e = cp->entry; + const char *name = e->tk->s; + + printf("%s ", cgen_sz(e->t->sz)); + + if (cgen_global(c->fn, e)) + printf("$%s", name); + else + printf("%%%s_", name); + + if (i + 1 < m->nparams) + fputs(", ", stdout); + } + + puts(")"); + + if (ret) + { + const char *name = ret->tk->s; + const struct fn *fn = c->fn; + const struct param *fnret = fn->pr->ret; + size_t sz = ret->t->sz; + + if (cgen_global(c->fn, ret)) + printf("store%s %%r, $%s", cgen_sz(sz), name); + else if (fnret && fnret->entry == ret) + printf("%%__ret =%s add %%r, 0", cgen_abity(ret->t)); + else + printf("%%%s_ =%s add %%r, 0", name, cgen_abity(ret->t)); + + putchar('\n'); + } + + return 0; +} + +void call_free(struct call *c) +{ + if (c) + free(c->params); +} + +int call(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->tk; + const struct pr *pr; + struct fn *fn = fn_cur(p); + size_t n = fn->nstmts +1; + struct stmt *stmts; + struct pos init = + { + .seq = seqs, + .stseq = seqs, + .step = seqs->steps + }; + + /* TODO: function pointers */ + + if (lex_eof(l, tk)) + { + errloc(tk - 1, "incomplete call statement"); + return -1; + } + else if (!(pr = find(p->ast, tk->s))) + { + errloc(tk, "undefined reference to function \"%s\"", tk->s); + return -1; + } + else if (!(stmts = realloc(fn->stmts, n * sizeof *stmts))) + { + perror("realloc(3)"); + return -1; + } + + fn->stmts = stmts; + fn->stmts[fn->nstmts++] = (struct stmt) + { + .type = CALL, + .u.call = + { + .tk = tk, + .pr = pr + } + }; + + if (push(&init, p)) + return -1; + + p->stk = p->tk; + return 1; +} diff --git a/call.h b/call.h new file mode 100644 index 0000000..44a01d2 --- /dev/null +++ b/call.h @@ -0,0 +1,28 @@ +#ifndef CALL_H +#define CALL_H + +#include "cgen.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include + +struct callparam +{ + const struct stentry *entry; +}; + +struct call +{ + const struct tk *tk; + const struct pr *pr; + const struct stentry *ret; + struct callparam *params; + size_t nparams; +}; + +int call(const struct lex *l, struct prv *p); +int call_cgen(const struct call *m, struct cgen *c); +void call_free(struct call *c); + +#endif diff --git a/cgen.c b/cgen.c new file mode 100644 index 0000000..46ea085 --- /dev/null +++ b/cgen.c @@ -0,0 +1,166 @@ +#include "cgen.h" +#include "fn.h" +#include "lex.h" +#include "lit.h" +#include "parse.h" +#include "storage.h" +#include +#include +#include + +void cgen_free(struct cgen *c) +{ +} + +const char *cgen_sz(const size_t sz) +{ + switch (sz) + { + case 1: + return "b"; + case 2: + return "h"; + case 4: + return "w"; + case 8: + return "l"; + default: + break; + } + + return NULL; +} + +const char *cgen_type(const struct type *t) +{ + switch (t->sz) + { + case 1: + return t->sign ? "b" : "ub"; + case 2: + return t->sign ? "h" : "uh"; + case 4: + return t->sign ? "w" : "uw"; + case 8: + return t->sign ? "l" : "ul"; + + default: + break; + } + + return NULL; +} + +const char *cgen_abity(const struct type *t) +{ + switch (t->sz) + { + case 1: + return t->sign ? "b" : "ub"; + case 2: + return t->sign ? "h" : "uh"; + case 4: + return "w"; + case 8: + return "l"; + + default: + break; + } + + return NULL; +} + +const char *cgen_load(const struct type *t) +{ + switch (t->sz) + { + case 1: + return t->sign ? "sb" : "ub"; + case 2: + return t->sign ? "sh" : "uh"; + case 4: + return t->sign ? "sw" : "uw"; + case 8: + return "l"; + + default: + break; + } + + return NULL; +} + +int cgen_param(const struct fn *fn, const struct stentry *e) +{ + const struct pr *pr = fn->pr; + const char *name = e->tk->s; + + for (size_t i = 0; i < pr->nparams; i++) + if (!strcmp(pr->params[i].tk->s, name)) + return 1; + + return 0; +} + +int cgen_global(const struct fn *fn, const struct stentry *e) +{ + const struct storage *ws = fn->ws, *gl = fn->gl; + const struct pr *pr = fn->pr; + const struct param *ret = pr->ret; + const char *name = e->tk->s; + + /* A variable is only global if it is defined inside the global + * variables section ("gl"), or if it is not listed on the working + * storage section ("ws"), as a function parameter or a return value. */ + + if (gl) + for (size_t i = 0; i < gl->nentries; i++) + if (!strcmp(gl->entries[i].tk->s, name)) + return 1; + + if (cgen_param(fn, e)) + return 0; + + if (ret && !strcmp(ret->tk->s, name)) + return 0; + + if (ws) + for (size_t i = 0; i < ws->nentries; i++) + if (!strcmp(ws->entries[i].tk->s, name)) + return 0; + + return 1; +} + +static void pushfmt(const char *type, const char *fmt) +{ + printf("data $____fmt%s = { b \"%s\" , b 0 }\n", type, fmt); +} + +int cgen(const struct ast *ast, struct cgen *c) +{ + /* generate literals to use with printf(3) */ + pushfmt("b", "%hhd"); + pushfmt("h", "%hd"); + pushfmt("w", "%d"); + pushfmt("l", "%ld"); + pushfmt("ub", "%hhu"); + pushfmt("uh", "%hu"); + pushfmt("uw", "%u"); + pushfmt("ul", "%lu"); + pushfmt("bx", "%#hhx"); + pushfmt("hx", "%#hx"); + pushfmt("wx", "%#x"); + pushfmt("lx", "%#lx"); + + for (const struct lit *l = ast->lits; l; l = l->next) + if (lit_cgen(l)) + return-1; + + for (size_t i = 0; i < ast->nfns; i++) + if (fn_cgen((c->fn = &ast->fns[i]), c)) + return -1; + + return 0; +} diff --git a/cgen.h b/cgen.h new file mode 100644 index 0000000..f1c42b8 --- /dev/null +++ b/cgen.h @@ -0,0 +1,21 @@ +#ifndef CGEN_H +#define CGEN_H + +#include "parse.h" +#include "storage.h" + +struct cgen +{ + const struct fn *fn; +}; + +int cgen(const struct ast *ast, struct cgen *c); +int cgen_param(const struct fn *fn, const struct stentry *e); +int cgen_global(const struct fn *fn, const struct stentry *e); +const char *cgen_sz(const size_t sz); +const char *cgen_type(const struct type *t); +const char *cgen_abity(const struct type *t); +const char *cgen_load(const struct type *t); +void cgen_free(struct cgen *c); + +#endif diff --git a/display.c b/display.c new file mode 100644 index 0000000..308fa24 --- /dev/null +++ b/display.c @@ -0,0 +1,226 @@ +#include "display.h" +#include "cgen.h" +#include "errloc.h" +#include "fn.h" +#include "lit.h" +#include "parse.h" +#include "prv.h" +#include "stmt.h" +#include "storage.h" +#include "type.h" +#include +#include +#include + +static int finalize(const struct lex *l, const struct display *d, + const struct prv *p) +{ + const struct ast *ast = p->ast; + struct fn *fn = &ast->fns[ast->nfns - 1]; + size_t n = fn->nstmts + 1; + struct stmt *stmts = realloc(fn->stmts, n * sizeof *stmts); + + if (!stmts) + { + perror("realloc(3)"); + return -1; + } + + fn->stmts = stmts; + fn->stmts[fn->nstmts++] = (struct stmt) + { + .type = DISPLAY, + .u.display = *d + }; + + fprintf(stderr, "\t\tadding display statement with %zu entries%s\n", + d->nentries, d->println ? "" : ", no lf"); + return 0; +} + +static int entry(const struct lex *l, const struct tk *tk, struct display *d, + struct prv *p) +{ + size_t n = d->nentries + 1; + struct dspentry *entries; + const struct stentry *stentry = NULL; + const struct fn *fn = fn_cur(p); + const struct lit *lit = NULL; + + if (tk->type == ID) + { + const struct type *t = NULL; + + if ((t = type_find(fn, tk->s))) + { + if (t->type != C) + { + errloc(tk, "type \"%s\" not a constant", tk->s); + return -1; + } + else if (!(lit = lit_push(t->tk, p))) + return -1; + } + else if (!(stentry = fn_var(fn, tk))) + { + errloc(tk, "undefined reference to \"%s\"", tk->s); + return -1; + } + } + + if (tk->type == LIT && !(lit = lit_push(tk, p))) + return -1; + else if (!(entries = realloc(d->entries, n * sizeof *entries))) + { + perror("realloc(3)"); + return -1; + } + + d->entries = entries; + d->entries[d->nentries++] = (struct dspentry) + { + .tk = tk, + .lit = lit, + .entry = stentry + }; + + return 0; +} + +static int cg_val(const struct tk *tk) +{ + const char *num = tk->s; + int neg = *num == '-'; + unsigned long long uv; + char *end; + + fputs("call $printf(l $____fmt", stdout); + errno = 0; + uv = strtoull(num, &end, 16); + + if (!neg && !errno && !*end) + printf("lx, ..., l %llu )\n", uv); + else + { + errno = 0; + uv = strtoull(num, &end, 0); + + if (neg || errno || *end) + { + long long v = strtoll(num, NULL, 0); + printf("l, ..., l %lld )\n", v); + } + else + printf("l, ..., l %llu )\n", uv); + } + + return 0; +} + +static int cg_lit(const struct dspentry *e) +{ + const struct lit *l = e->lit; + + if (!l) + { + fprintf(stderr, "%s: unexpected null literal\n", __func__); + return -1; + } + + puts("%v =l loadl $stdout"); + printf("call $fputs(l $%s, w %%v)\n", l->name); + return 0; +} + +static int cg_id(const struct dspentry *de, const struct fn *fn) +{ + const struct stentry *e = de->entry; + const struct type *t = e->t; + const char *id = e->tk->s; + int global = cgen_global(fn, e); + size_t sz = t->sz; + const char *asz = cgen_sz(sz < 4 ? 4 : sz); + const struct param *ret = fn->pr->ret; + int isret = ret && ret->entry == e; + + if (global) + printf("%%v =%s load%s $%s\n", asz, cgen_load(t), id); + else if (isret) + printf("%%v =%s add 0, %%__ret\n", asz); + else + printf("%%v =%s add 0, %%%s_\n", asz, id); + + if (isret) + printf("call $printf(l $____fmt%s, ..., w %%__ret )\n", cgen_type(t)); + else if (global) + printf("call $printf(l $____fmt%s, ..., w %%v )\n", cgen_type(t)); + else + printf("call $printf(l $____fmt%s, ..., w %%%s_ )\n", cgen_type(t), id); + + return 0; +} + +int display_cgen(const struct display *d, struct cgen *c) +{ + for (size_t i = 0; i < d->nentries; i++) + { + const struct dspentry *e = &d->entries[i]; + const struct tk *tk = e->tk; + + if (tk->type == LIT && cg_lit(e)) + return -1; + else if (tk->type == NUM && cg_val(tk)) + return -1; + else if (tk->type == ID && e->entry && cg_id(e, c->fn)) + return -1; + else if (tk->type == ID && e->lit && cg_lit(e)) + return -1; + } + + if (d->println) + puts("call $putchar(l 10 )"); + + return 0; +} + +void display_free(struct display *d) +{ + free(d->entries); +} + +int display(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk + 1; + struct display d = {0}; + struct pos *pos = &p->pos[p->i]; + int eof; + + if (lex_eof(l, tk)) + { + errloc(tk - 1, "incomplete display statement"); + return -1; + } + + for (;;) + if (entry(l, tk, &d, p)) + goto failure; + else if ((eof = lex_eof(l, ++tk)) || kw(tk->s)) + break; + + if (eof || strcmp(tk->s, "etc")) + d.println = 1; + else if (!eof) + tk++; + + if (finalize(l, &d, p)) + goto failure; + + pos->seq = pos->stseq = stmts; + pos->step = stmts->steps; + p->stk = tk; + return 1; + +failure: + display_free(&d); + return -1; +} diff --git a/display.h b/display.h new file mode 100644 index 0000000..5a16758 --- /dev/null +++ b/display.h @@ -0,0 +1,27 @@ +#ifndef DISPLAY_H +#define DISPLAY_H + +#include "cgen.h" +#include "lex.h" +#include "prv.h" +#include + +struct dspentry +{ + const struct tk *tk; + const struct lit *lit; + const struct stentry *entry; +}; + +struct display +{ + struct dspentry *entries; + size_t nentries; + int println; +}; + +int display(const struct lex *l, struct prv *p); +int display_cgen(const struct display *d, struct cgen *c); +void display_free(struct display *d); + +#endif diff --git a/div.c b/div.c new file mode 100644 index 0000000..ec73323 --- /dev/null +++ b/div.c @@ -0,0 +1,18 @@ +#include "div.h" +#include "gl.h" +#include "im.h" +#include "lk.h" +#include "pr.h" +#include "td.h" +#include "ws.h" + +const struct seq divseq[] = +{ + {(const struct step[]){{ID, "storage"}, {0}}, .fn = ws}, + {(const struct step[]){{ID, "linkage"}, {0}}, .fn = lk}, + {(const struct step[]){{ID, "globals"}, {0}}, .fn = gl}, + {(const struct step[]){{ID, "procedure"}, {0}}, .fn = pr}, + {(const struct step[]){{ID, "types"}, {0}}, .fn = td}, + {(const struct step[]){{ID, "import"}, {LIT}, {0}}, .fn = im}, + {0} +}; diff --git a/div.h b/div.h new file mode 100644 index 0000000..7efbc8e --- /dev/null +++ b/div.h @@ -0,0 +1,8 @@ +#ifndef DIV_H +#define DIV_H + +#include "prv.h" + +extern const struct seq divseq[]; + +#endif diff --git a/errloc.c b/errloc.c new file mode 100644 index 0000000..1f3e224 --- /dev/null +++ b/errloc.c @@ -0,0 +1,28 @@ +#include "errloc.h" +#include "lex.h" +#include +#include + +void errcloc(const struct lex *l, const char *fmt, ...) +{ + const struct loc *loc = &l->loc; + va_list ap; + + va_start(ap, fmt); + fprintf(stderr, "%s:%d:%d: error: ", loc->f, loc->line, loc->col); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); +} + +void errloc(const struct tk *tk, const char *fmt, ...) +{ + const struct loc *loc = &tk->loc; + va_list ap; + + va_start(ap, fmt); + fprintf(stderr, "%s:%d:%d: error: ", loc->f, loc->line, loc->col); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); +} diff --git a/errloc.h b/errloc.h new file mode 100644 index 0000000..127caa7 --- /dev/null +++ b/errloc.h @@ -0,0 +1,9 @@ +#ifndef ERRLOC_H +#define ERRLOC_H + +#include "lex.h" + +void errloc(const struct tk *tk, const char *fmt, ...); +void errcloc(const struct lex *l, const char *fmt, ...); + +#endif diff --git a/examples/hello.uc b/examples/hello.uc new file mode 100644 index 0000000..bbd7b14 --- /dev/null +++ b/examples/hello.uc @@ -0,0 +1,62 @@ +* simple example with 3 functions + +function bar +storage + var word +types + MONDAY constant + TUESDAY constant + WEDNESDAY constant + THURSDAY constant + FRIDAY constant + SATURDAY constant + SUNDAY constant + + mystruct struct + var long + var2 long + end + + mystruct2 struct + var word + end +linkage + retcode uword +procedure returning retcode + set retcode to SATURDAY + display "bar has now changed the retcode to " SATURDAY + +function foo +storage +linkage + param uword + param2 uword + retcode uword +procedure with param param2 returning retcode + set retcode to 12 + display "hi from foo! param is " param + ", param2 is " param2 + ", assigning retcode to " retcode + set param to 90 + set param2 to 60 + display "now, param is " param ", param2 is " param2 + call bar returning retcode + display "bar returned " retcode + +function main public +globals +storage + myparam uword + myparam2 uword + myret uword +procedure + set myret to size of word + display "myret is " myret + set myret to 100 + set myparam to 300 + set myparam2 to 400 + display "calling foo, myparam is " myparam + ", myparam2 is " myparam2 + ", myret is " myret + call foo with myparam myparam2 + display "foo in turn returned " myret diff --git a/exit.c b/exit.c new file mode 100644 index 0000000..0438b66 --- /dev/null +++ b/exit.c @@ -0,0 +1,8 @@ +#include "exit.h" +#include "prv.h" + +int s_exit(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} diff --git a/exit.h b/exit.h new file mode 100644 index 0000000..24b1292 --- /dev/null +++ b/exit.h @@ -0,0 +1,16 @@ +#ifndef EXIT_H +#define EXIT_H + +#include "lex.h" +#include "prv.h" +#include + +struct exit +{ + int dummy; +}; + +int s_exit(const struct lex *l, struct prv *p); +void exit_free(struct exit *d); + +#endif diff --git a/fn.c b/fn.c new file mode 100644 index 0000000..232b28b --- /dev/null +++ b/fn.c @@ -0,0 +1,204 @@ +#include "fn.h" +#include "div.h" +#include "errloc.h" +#include "gl.h" +#include "parse.h" +#include "prv.h" +#include "pr.h" +#include "stmt.h" +#include "storage.h" +#include "td.h" +#include +#include +#include + +static int pubfn(const struct lex *l, struct prv *p); +static int prvfn(const struct lex *l, struct prv *p); +static int end(const struct lex *l, struct prv *p); + +static const struct seq fnseq[] = +{ + {(const struct step[]) + {{ID, "function"}, {ID}, {ID, "public"}, {0}}, .fn = pubfn, .end = end}, + {(const struct step[]) + {{ID, "function"}, {ID}, {0}}, .fn = prvfn, .end = end}, + {0} +}; + +static int end(const struct lex *l, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + + if (!fn->pr) + { + const struct tk *tk = fn->tk; + + errloc(p->stk, "function \"%s\" has no body", tk->s); + return -1; + } + + return 0; +} + +static const struct tk *findfn(const char *s, const struct ast *ast) +{ + for (size_t i = 0; i < ast->nfns; i++) + { + const struct tk *tk = ast->fns[i].tk; + + if (!strcmp(tk->s, s)) + return tk; + } + + return NULL; +} + +static int pushfn(const struct lex *lex, struct prv *p, enum linkage l) +{ + const struct tk *tk = p->stk + 1, *def; + struct ast *ast = p->ast; + size_t n = ast->nfns + 1; + struct fn *fns; + struct pos init = + { + .seq = divseq, + .stseq = divseq, + .step = divseq->steps + }; + + if ((def = findfn(tk->s, ast))) + { + const struct loc *loc = &def->loc; + + errloc(tk, "function \"%s\" already defined at %s:%d:%d", tk->s, + loc->f, loc->line, loc->col); + return -1; + } + else if (!(fns = realloc(ast->fns, n * sizeof *fns))) + { + perror("realloc(3)"); + return -1; + } + else if (push(&init, p)) + return -1; + + fns[ast->nfns++] = (struct fn){.linkage = l, .tk = tk}; + ast->fns = fns; + p->stk = p->tk; + fprintf(stderr, "adding %s function %s\n", + l == STATIC ? "private" : "public", tk->s); + return 1; +} + +static int pubfn(const struct lex *l, struct prv *p) +{ + return pushfn(l, p, EXTERNAL); +} + +static int prvfn(const struct lex *l, struct prv *p) +{ + return pushfn(l, p, STATIC); +} + +const struct stentry *fn_var(const struct fn *fn, const struct tk *tk) +{ + const struct stentry *e = NULL; + const struct storage *lk = fn->lk, *ws = fn->ws, *gl = fn->gl; + + if ((!lk || !(e = storage_find(tk->s, lk))) + && (!ws || !(e = storage_find(tk->s, ws))) + && (!gl || !(e = storage_find(tk->s, gl)))) + return NULL; + + return e; +} + +int fn_cgen(const struct fn *fn, struct cgen *c) +{ + const struct pr *pr = fn->pr; + const struct param *ret = pr->ret; + + if (fn->gl && gl_cgen(fn->gl, c)) + return -1; + + switch (fn->linkage) + { + case STATIC: + printf("function "); + break; + case EXTERNAL: + printf("export function "); + break; + } + + if (ret) + printf("%s ", cgen_abity(ret->entry->t)); + + printf("$%s(", fn->tk->s); + + for (size_t i = 0; i < pr->nparams; i++) + { + const struct param *p = &pr->params[i]; + + printf(" %s", cgen_abity(p->entry->t)); + printf(" %%%s_ ", p->tk->s); + + if (i + 1 < pr->nparams) + fputs(", ", stdout); + } + + puts(") {\n@start"); + + for (size_t i = 0; i < fn->nstmts; i++) + if (stmt_cgen(&fn->stmts[i], c)) + return -1; + + fputs("ret", stdout); + + if (pr->ret) + fputs(" %__ret", stdout); + + puts("\n}"); + return 0; +} + +void fn_cgen_free(struct fn_cgen *c) +{ + free(c); +} + +void fn_free(struct fn *fn) +{ + for (size_t i = 0; i < fn->nstmts; i++) + stmt_free(&fn->stmts[i]); + + storage_free(fn->lk); + storage_free(fn->ws); + storage_free(fn->gl); + pr_free(fn->pr); + td_free(fn->td); + free(fn->stmts); +} + +struct fn *fn_cur(const struct prv *p) +{ + struct ast *ast = p->ast; + + return &ast->fns[p->ast->nfns - 1]; +} + +int fn(const struct lex *l, struct prv *p) +{ + struct pos init = + { + .seq = fnseq, + .stseq = fnseq, + .step = fnseq->steps + }; + + if (push(&init, p)) + return -1; + + p->stk = p->tk; + return 1; +} diff --git a/fn.h b/fn.h new file mode 100644 index 0000000..801d7bf --- /dev/null +++ b/fn.h @@ -0,0 +1,21 @@ +#ifndef FN_H +#define FN_H + +#include "cgen.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" + +struct fn_cgen +{ + int dummy; +}; + +int fn(const struct lex *l, struct prv *p); +struct fn *fn_cur(const struct prv *p); +int fn_cgen(const struct fn *fn, struct cgen *c); +const struct stentry *fn_var(const struct fn *fn, const struct tk *tk); +void fn_free(struct fn *fn); +void fn_cgen_free(struct fn_cgen *c); + +#endif diff --git a/gl.c b/gl.c new file mode 100644 index 0000000..87d4063 --- /dev/null +++ b/gl.c @@ -0,0 +1,24 @@ +#include "gl.h" +#include "prv.h" +#include "parse.h" +#include "storage.h" + +int gl_cgen(const struct storage *gl, struct cgen *c) +{ + for (size_t i = 0; i < gl->nentries; i++) + { + const struct stentry *e = &gl->entries[i]; + + printf("export data $%s = { z %zu }\n", e->tk->s, e->t->sz); + } + + return 0; +} + +int gl(const struct lex *l, struct prv *p) +{ + struct ast *ast = p->ast; + struct fn *fn = &ast->fns[ast->nfns - 1]; + + return storage(l, p, "globals section", 1, &fn->gl); +} diff --git a/gl.h b/gl.h new file mode 100644 index 0000000..d8aaaaf --- /dev/null +++ b/gl.h @@ -0,0 +1,12 @@ +#ifndef GL_H +#define GL_H + +#include "cgen.h" +#include "lex.h" +#include "prv.h" +#include "storage.h" + +int gl(const struct lex *l, struct prv *p); +int gl_cgen(const struct storage *gl, struct cgen *c); + +#endif diff --git a/im.c b/im.c new file mode 100644 index 0000000..26ff257 --- /dev/null +++ b/im.c @@ -0,0 +1,11 @@ +#include "im.h" +#include "prv.h" +#include "parse.h" +#include "storage.h" +#include + +int im(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} diff --git a/im.h b/im.h new file mode 100644 index 0000000..2579a42 --- /dev/null +++ b/im.h @@ -0,0 +1,9 @@ +#ifndef IM_H +#define IM_H + +#include "lex.h" +#include "prv.h" + +int im(const struct lex *l, struct prv *p); + +#endif diff --git a/kw.c b/kw.c new file mode 100644 index 0000000..e3d6336 --- /dev/null +++ b/kw.c @@ -0,0 +1,30 @@ +#include "prv.h" +#include +#include + +static const char *kws[] = +{ + "function", "public", + "types", "storage", "linkage", "procedure", "locals", "globals", + "union", "struct", "constant", "array", "prototype", "type", + "add", "subtract", "multiply", "divide", "xor", + "orbit", "andbit", "leftshift", "rightshift", + "address", "size", + "with", "returning", "by", "to", "than", "of", "etc", + "import", "call", "display", "set", + "if", "else", "end", "exit", "go", + "is", "not", "equal", "greater", "smaller", "and", "or", + "perform", "until", + "void", "byte", "halfword", "word", "long", + "ubyte", "uhalfword", "uword", "ulong", + "pointer" +}; + +int kw(const char *s) +{ + for (size_t i = 0; i < sizeof kws / sizeof *kws; i++) + if (!strcmp(s, kws[i])) + return 1; + + return 0; +} diff --git a/lex.c b/lex.c new file mode 100644 index 0000000..0ff1f20 --- /dev/null +++ b/lex.c @@ -0,0 +1,247 @@ +#include "lex.h" +#include "errloc.h" +#include +#include +#include +#include +#include + +static void free_tk(struct tk *tk) +{ + free(tk->s); +} + +static int invnum(const struct tk *tk, char *end) +{ + if (errno) + errloc(tk, "invalid number: %s (%s)", tk->s, strerror(errno)); + else if (*end) + errloc(tk, "invalid number: %s", tk->s); + + return -1; +} + +static int fintok(struct lex *lex) +{ + struct tk *tk = &lex->tk, *tokens; + const struct tk empty = {.loc = tk->loc}; + size_t ntk = lex->ntok + 1; + char *s; + + if (tk->type == UNDEF) + return 0; + else if (!(s = realloc(tk->s, lex->len + 1))) + { + perror("realloc(3)"); + return -1; + } + + tk->s = s; + tk->s[lex->len] = '\0'; + + switch (tk->type) + { + case UNDEF: + break; + + case ANY: + errloc(tk, "%s: unreachable", __func__); + return -1; + + case LIT: + case ID: + break; + + case NUM: + { + int neg = *tk->s == '-'; + char *end; + + errno = 0; + strtoll(tk->s, &end, 0); + + if (errno || *end) + { + if (neg) + return invnum(tk, end); + + errno = 0; + strtoull(tk->s, &end, 0); + + if (errno || *end) + return invnum(tk, end); + } + } + } + + if (!(tokens = realloc(lex->tokens, ntk * sizeof *tokens))) + { + perror("realloc(3)"); + return -1; + } + + tokens[lex->ntok++] = lex->tk; + lex->tokens = tokens; + lex->tk = empty; + lex->len = 0; + return 0; +} + +static int printable(char c) +{ + return c >= '!' && c <= '~'; +} + +static void invch(const struct lex *l, char c) +{ + if (printable(c)) + errcloc(l, "invalid character: %c", c); + else + errcloc(l, "invalid character: (%#hhx)", c); +} + +static int ch(char c, struct lex *lex) +{ + struct tk *tk = &lex->tk; + char *s; + + switch (tk->type) + { + case UNDEF: + tk->loc = lex->loc; + + if (c == '\"') + { + tk->type = LIT; + return 0; + } + else if (c == '-' || c == '+' + || (c >= '0' && c <= '9')) + tk->type = NUM; + else if (c == '_' + || (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z')) + tk->type = ID; + else + { + invch(lex, c); + return -1; + } + + break; + + case LIT: + if (c == '\"') + return fintok(lex); + + break; + + case NUM: + if (c == '\"') + { + invch(lex, c); + return -1; + } + + break; + + case ID: + if (c == '\"' || c == '-' || c == '+') + { + invch(lex, c); + return -1; + } + else if (!(c == '_' + || (c >= '0' && c <= '9') + || (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z'))) + { + invch(lex, c); + return -1; + } + + break; + case ANY: + errcloc(lex, "%s: unreachable", __func__); + return -1; + } + + if (!(s = realloc(tk->s, lex->len + 1))) + { + perror("realloc(3)"); + return -1; + } + + s[lex->len++] = c; + tk->s = s; + return 0; +} + +static int clex(char c, struct lex *lex) +{ + struct tk *tk = &lex->tk; + struct loc *loc = &lex->loc; + + ++loc->col; + + switch (c) + { + case '*': + lex->comment = 1; + return fintok(lex); + + case '\n': + if (tk->type == LIT) + { + errcloc(lex, "unterminated literal"); + return -1; + } + else if (fintok(lex)) + return -1; + + loc->line++; + loc->col = lex->comment = 0; + return 0; + + case ' ': + case '\t': + if (tk->type != LIT) + return lex->comment ? 0 : fintok(lex); + default: + return lex->comment ? 0 : ch(c, lex); + } + + invch(lex, c); + return -1; +} + +void lex_free(struct lex *lex) +{ + for (size_t i = 0; i < lex->ntok; i++) + free_tk(&lex->tokens[i]); + + free(lex->tokens); + free_tk(&lex->tk); +} + +int lex_eof(const struct lex *lex, const struct tk *tk) +{ + return tk - lex->tokens >= lex->ntok; +} + +int lex(struct lex *l, FILE *f) +{ + l->loc.line = 1; + + for (;;) + { + int c = fgetc(f); + + if (c == EOF) + break; + else if (clex(c, l)) + return -1; + } + + return 0; +} diff --git a/lex.h b/lex.h new file mode 100644 index 0000000..6e836ac --- /dev/null +++ b/lex.h @@ -0,0 +1,34 @@ +#ifndef LEX_H +#define LEX_H + +#include +#include + +enum tktype {UNDEF, ID, LIT, NUM, ANY}; + +struct loc +{ + const char *f; + int line, col; +}; + +struct tk +{ + enum tktype type; + struct loc loc; + char *s; +}; + +struct lex +{ + struct loc loc; + struct tk *tokens, tk; + size_t ntok, len; + int comment; +}; + +int lex(struct lex *lex, FILE *f); +int lex_eof(const struct lex *lex, const struct tk *tk); +void lex_free(struct lex *lex); + +#endif diff --git a/lit.c b/lit.c new file mode 100644 index 0000000..09528a2 --- /dev/null +++ b/lit.c @@ -0,0 +1,102 @@ +#include "lit.h" +#include "fn.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include +#include +#include +#include + +void lit_free(struct lit *l) +{ + free(l->name); +} + +static char *gen(struct ast *ast, struct fn *fn) +{ + static const char fmt[] = "__str_%zu"; + int n = snprintf(NULL, 0, fmt, ast->litcnt); + char *ret = NULL; + size_t sz; + + if (n < 0) + { + fprintf(stderr, "%s: snprintf(3) failed\n", __func__); + goto failure; + } + else if (!(ret = malloc((sz = n + 1)))) + { + perror("malloc(3)"); + goto failure; + } + + n = snprintf(ret, sz, fmt, ast->litcnt); + + if (n < 0 || n >= sz) + { + fprintf(stderr, "%s: snprintf(3) failed with %d\n", __func__, n); + goto failure; + } + + ast->litcnt++; + return ret; + +failure: + free(ret); + return NULL; +} + +static const struct lit *find(const struct tk *tk, const struct ast *ast) +{ + for (const struct lit *l = ast->lits; l; l = l->next) + if (!strcmp(l->tk->s, tk->s)) + return l; + + return NULL; +} + +int lit_cgen(const struct lit *l) +{ + printf("data $%s = { b \"%s\", b 0 }\n", l->name, l->tk->s); + return 0; +} + +const struct lit *lit_push(const struct tk *tk, struct prv *p) +{ + char *name = NULL; + struct fn *fn = fn_cur(p); + struct ast *ast = p->ast; + const struct lit *prev = find(tk, ast); + struct lit *l; + + if (prev) + return prev; + else if (!(name = gen(ast, fn))) + goto failure; + else if (!(l = malloc(sizeof *l))) + { + perror("malloc(3)"); + goto failure; + } + + *l = (struct lit) + { + .name = name, + .fn = fn, + .tk = tk + }; + + if (!ast->lits) + ast->lits = l; + else if (ast->lastlit) + ast->lastlit->next = l; + + ast->lastlit = l; + fprintf(stderr, "adding literal \"%s\"\n", name); + return l; + +failure: + free(name); + return NULL; +} diff --git a/lit.h b/lit.h new file mode 100644 index 0000000..e94263c --- /dev/null +++ b/lit.h @@ -0,0 +1,12 @@ +#ifndef LIT_H +#define LIT_H + +#include "lex.h" +#include "parse.h" +#include "prv.h" + +const struct lit *lit_push(const struct tk *tk, struct prv *p); +int lit_cgen(const struct lit *l); +void lit_free(struct lit *l); + +#endif diff --git a/lk.c b/lk.c new file mode 100644 index 0000000..2164817 --- /dev/null +++ b/lk.c @@ -0,0 +1,12 @@ +#include "lk.h" +#include "prv.h" +#include "parse.h" +#include "storage.h" + +int lk(const struct lex *l, struct prv *p) +{ + struct ast *ast = p->ast; + struct fn *fn = &ast->fns[ast->nfns - 1]; + + return storage(l, p, "linkage section", 1, &fn->lk); +} diff --git a/lk.h b/lk.h new file mode 100644 index 0000000..35df205 --- /dev/null +++ b/lk.h @@ -0,0 +1,9 @@ +#ifndef LK_H +#define LK_H + +#include "lex.h" +#include "prv.h" + +int lk(const struct lex *l, struct prv *p); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..319f943 --- /dev/null +++ b/main.c @@ -0,0 +1,53 @@ +#include "cgen.h" +#include "lex.h" +#include "parse.h" +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct lex l = {0}; + struct ast p = {0}; + struct cgen c = {0}; + FILE *f = stdin; + int opened = 0, ret = EXIT_FAILURE; + + if (argc != 2) + { + fprintf(stderr, "%s: error: missing input file\n", *argv); + goto end; + } + else if (strcmp(argv[1], "-")) + { + if (!(f = fopen(argv[1], "rb"))) + { + fprintf(stderr, "failed to open %s: %s\n", argv[1], + strerror(errno)); + goto end; + } + + opened = 1; + l.loc.f = argv[1]; + } + else + l.loc.f = "stdin"; + + if (lex(&l, f) || parse(&l, &p) || cgen(&p, &c)) + goto end; + + ret = EXIT_SUCCESS; + +end: + if (opened && fclose(f)) + { + perror("fclose(3)"); + ret = EXIT_FAILURE; + } + + lex_free(&l); + ast_free(&p); + cgen_free(&c); + return ret; +} diff --git a/parse.c b/parse.c new file mode 100644 index 0000000..48871a4 --- /dev/null +++ b/parse.c @@ -0,0 +1,155 @@ +#include "parse.h" +#include "fn.h" +#include "errloc.h" +#include "lex.h" +#include "lit.h" +#include "prv.h" +#include +#include +#include + +void ast_free(struct ast *ast) +{ + for (struct lit *l = ast->lits, *next; l; l = next) + { + next = l->next; + lit_free(l); + free(l); + } + + for (size_t i = 0; i < ast->nfns; i++) + fn_free(&ast->fns[i]); + + free(ast->fns); +} + +static const char *expected(const struct step *s) +{ + static const char *tmap[] = + { + [ID] = "identifier", [LIT] = "literal", [NUM] = "number" + }; + + return s->id ? s->id : tmap[s->type]; +} + +static int iter(const struct lex *l, struct prv *p) +{ + struct pos *pos = p->pos; + const struct step *s; + const struct tk *tk = p->tk; + + if (!pos) + return lex_eof(l, tk) ? 0 : fn(l, p); + + pos = &p->pos[p->i]; + + if (!(s = pos->step)) + { + if (lex_eof(l, tk)) + return 0; + else if (p->n > 1) + return pop(l, p) ? -1 : 1; + else + { + switch (tk->type) + { + case LIT: + errloc(tk, "unexpected literal \"%s\"", tk->s); + break; + case NUM: + errloc(tk, "unexpected number %s", tk->s); + break; + case ID: + errloc(tk, "unexpected %s %s", kw(tk->s) ? + "keyword" : "identifier", tk->s); + break; + case ANY: + case UNDEF: + errloc(tk, "%s: unreachable", __func__); + break; + } + + return -1; + } + } + else if (lex_eof(l, tk)) + { + if (p->stk == p->tk) + return pop(l, p) ? -1 : 1; + else if (s->type) + { + tk--; + + if (lex_eof(l, p->stk)) + { + errloc(tk, "expected %s", expected(pos->step)); + return -1; + } + } + } + + if (!s->type) + return pos->seq->fn(l, p); + else if ((s->type == ID || s->type == ANY) && !s->id && kw(tk->s) + && !s->chain) + { + if (tk != p->stk || !p->n) + { + errloc(tk, "unexpected keyword %s", tk->s); + return -1; + } + else if (pop(l, p)) + return -1; + + pos = &p->pos[p->i]; + } + else if ((s->type != tk->type && s->type != ANY) + || (s->type == ID && s->id && strcmp(s->id, tk->s)) + || (s->type == ID && kw(tk->s) && s->chain)) + { + pos->step = (++pos->seq)->steps; + p->tk = p->stk; + } + else + { + const struct tk *next = tk + 1; + + if (!s->chain) + pos->step++; + else if (!lex_eof(l, next)) + { + if (s->chain && kw(next->s)) + pos->step++; + } + else + pos->step++; + + p->tk++; + } + + return 1; +} + +static void free_prv(struct prv *p) +{ + free(p->tdc); + free(p->pos); +} + +int parse(const struct lex *l, struct ast *ast) +{ + int ret; + struct prv prv = {0}; + + prv.ast = ast; + + if (!(prv.tk = l->tokens)) + return 0; + + while ((ret = iter(l, &prv)) > 0) + ; + + free_prv(&prv); + return ret; +} diff --git a/parse.h b/parse.h new file mode 100644 index 0000000..e5ab3b6 --- /dev/null +++ b/parse.h @@ -0,0 +1,104 @@ +#ifndef PARSE_H +#define PARSE_H + +#include "lex.h" + +struct u +{ + struct storage *storage; +}; + +struct s +{ + struct storage *storage; +}; + +union c +{ + unsigned long long uv; + long long v; +}; + +struct p +{ + int dummy; +}; + +struct t +{ + const struct type *alias; +}; + +struct builtin +{ + const char *name; +}; + +struct type +{ + enum {U, S, C, P, T, BUILTIN} type; + const struct tk *tk; + size_t sz; + int sign; + + union + { + struct u u; + struct s s; + union c c; + struct p p; + struct t t; + struct builtin b; + } u; +}; + +struct param +{ + const struct tk *tk; + const struct stentry *entry; +}; + +struct pr +{ + const struct tk *tk; + struct param *params, *ret; + size_t nparams; +}; + +struct td +{ + const struct tk *tk; + struct type *types; + size_t ntypes; +}; + +struct fn +{ + const struct tk *tk; + enum linkage {EXTERNAL, STATIC} linkage; + struct storage *lk, *ws, *gl; + struct stmt *stmts; + struct pr *pr; + struct td *td; + size_t nstmts; +}; + +struct lit +{ + char *name; + const struct tk *tk; + const struct fn *fn; + struct lit *next; +}; + +struct ast +{ + struct fn *fns; + struct lit *lits, *lastlit; + size_t nfns, litcnt; +}; + +int parse(const struct lex *l, struct ast *ast); +void ast_free(struct ast *ast); + +#endif diff --git a/pop.c b/pop.c new file mode 100644 index 0000000..99ed3d1 --- /dev/null +++ b/pop.c @@ -0,0 +1,37 @@ +#include "prv.h" +#include + +int pop(const struct lex *l, struct prv *p) +{ + size_t n; + struct pos *npos = NULL; + + if (!p->n) + { + fprintf(stderr, "%s: underflow\n", __func__); + return -1; + } + else if (!(n = p->n - 1)) + { + free(p->pos); + p->pos = NULL; + } + else if (!(npos = realloc(p->pos, n * sizeof *npos))) + { + perror("realloc(3)"); + return -1; + } + + p->pos = npos; + p->n = n; + + if (p->n) + { + const struct pos *old = &p->pos[--p->i]; + + if (old->seq->end && old->seq->end(l, p)) + return -1; + } + + return 0; +} diff --git a/pr.c b/pr.c new file mode 100644 index 0000000..df2d127 --- /dev/null +++ b/pr.c @@ -0,0 +1,289 @@ +#include "pr.h" +#include "errloc.h" +#include "fn.h" +#include "prv.h" +#include "parse.h" +#include "stmt.h" +#include "storage.h" +#include +#include +#include + +static int pryy(const struct lex *l, struct prv *p); +static int pryn(const struct lex *l, struct prv *p); +static int prny(const struct lex *l, struct prv *p); +static int prnn(const struct lex *l, struct prv *p); + +static const struct seq seqs[] = +{ + /* function definition with parameters and return variable */ + { + (const struct step[]) + { + {ID, "with"}, + {ID, NULL, 1}, + {ID, "returning"}, + {ID}, + {0} + }, + .fn = pryy + }, + + /* function definition with parameters, no return variable */ + { + (const struct step[]) + { + {ID, "with"}, + {ID, NULL, 1}, + {0} + }, + + .fn = pryn + }, + + /* function definition with no parameters, with return variable */ + { + (const struct step[]) + { + {ID, "returning"}, + {ID}, + {0} + }, + + .fn = prny + }, + + {0} +}; + +static const struct tk *findparam(const char *s, const struct pr *pr) +{ + for (size_t i = 0; i < pr->nparams; i++) + { + const struct param *p = &pr->params[i]; + const struct tk *tk = p->tk; + + if (!strcmp(tk->s, s)) + return tk; + } + + return NULL; +} + +static int param(const struct tk *tk, const struct stentry *e, struct pr *pr) +{ + size_t n = pr->nparams + 1; + struct param *params = realloc(pr->params, n * sizeof *params); + + if (!params) + { + perror("realloc(3)"); + return -1; + } + + pr->params = params; + pr->params[pr->nparams++] = (struct param){.tk = tk, .entry = e}; + return 0; +} + +static int setret(const struct tk *tk, const struct stentry *e, struct pr *pr) +{ + if (!(pr->ret = malloc(sizeof *pr->ret))) + { + perror("malloc(3)"); + return -1; + } + + *pr->ret = (struct param){.tk = tk, .entry = e}; + return 0; +} + +static int finalize(struct prv *p) +{ + const struct fn *fn = fn_cur(p); + const struct pr *pr = fn->pr; + struct pos init = + { + .seq = stmts, + .stseq = stmts, + .step = stmts->steps + }; + + if (push(&init, p)) + return -1; + + fprintf(stderr, "\tadding pr with %zu parameters and%s return value\n", + pr->nparams, pr->ret ? "" : " no"); + p->stk = p->tk; + return 1; +} + +static int prret(const struct lex *l, const struct tk *tk, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + struct pr *pr = fn->pr; + const char *s = tk->s; + const struct storage *lk = fn->lk, *gl = fn->gl; + const struct stentry *e, *gle; + const struct tk *prm; + + if (!lk || !(e = storage_find(s, lk))) + { + errloc(tk, "return value \"%s\" not in %s", s, lk->name); + return -1; + } + else if (gl && (gle = storage_find(s, gl))) + { + const struct loc *loc = &gle->tk->loc; + + errloc(tk, "return value \"%s\" also in %s at %d:%d", s, gl->name, + loc->line, loc->col); + return -1; + } + else if ((prm = findparam(s, pr))) + { + const struct loc *loc = &prm->loc; + + errloc(tk, "return value \"%s\" already listed as " + "parameter at %s:%d:%d", tk->s, loc->f, loc->line, loc->col); + return -1; + } + else if (setret(tk, e, pr)) + return -1; + + return finalize(p); +} + +static const struct tk *prparams(const struct lex *l, struct prv *p, int *ret) +{ + const struct tk *tk = p->stk + 1; + const struct fn *fn = fn_cur(p); + struct pr *pr = fn->pr; + + if (ret) + *ret = 0; + + for (;;) + { + const char *s = tk->s; + const struct stentry *e, *gle; + const struct tk *prm; + const struct storage *lk = fn->lk, *gl = fn->gl; + + if (kw(s) && strcmp(s, "returning")) + return tk; + else if (!strcmp(s, "returning") && ret) + { + *ret = 1; + return ++tk; + } + else if ((prm = findparam(s, pr))) + { + const struct loc *loc = &prm->loc; + + errloc(tk, "parameter \"%s\" already listed at %d:%d", s, + loc->line, loc->col); + return NULL; + } + else if (!lk || !(e = storage_find(s, lk))) + { + errloc(tk, "parameter \"%s\" not in %s", s, lk->name); + return NULL; + } + else if (gl && (gle = storage_find(s, gl))) + { + const struct loc *loc = &gle->tk->loc; + + errloc(tk, "parameter \"%s\" also in %s at %d:%d", s, gl->name, + loc->line, loc->col); + return NULL; + } + else if (param(tk, e, pr)) + return NULL; + else + tk++; + } + + return tk; +} + +static int pryy(const struct lex *l, struct prv *p) +{ + int ret; + const struct tk *tk = prparams(l, p, &ret); + + if (!tk) + return -1; + else if (ret) + return prret(l, tk, p); + + return finalize(p); +} + +static int pryn(const struct lex *l, struct prv *p) +{ + if (!prparams(l, p, NULL)) + return -1; + + return finalize(p); +} + +static int prny(const struct lex *l, struct prv *p) +{ + return prret(l, p->stk + 1, p); +} + +static int prnn(const struct lex *l, struct prv *p) +{ + return finalize(p); +} + +void pr_free(struct pr *pr) +{ + if (pr) + { + free(pr->params); + free(pr->ret); + } + + free(pr); +} + +int pr(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->tk - 1, *next = p->tk; + struct fn *fn = fn_cur(p); + struct pos init = + { + .seq = seqs, + .stseq = seqs, + .step = seqs->steps + }; + + if (fn->pr) + { + const struct loc *loc = &fn->pr->tk->loc; + + errloc(tk, "pr already defined at %s:%d:%d", loc->f, loc->line, + loc->col); + return -1; + } + else if (!((fn->pr = malloc(sizeof *fn->pr)))) + { + perror("malloc(3)"); + return -1; + } + + *fn->pr = (struct pr){.tk = tk}; + + /* on no parameters or return values, go directly to next sequences list. */ + if (next - l->tokens < l->ntok + && strcmp(next->s, "with") + && strcmp(next->s, "returning")) + return prnn(l, p); + else if (push(&init, p)) + return -1; + + p->stk = p->tk; + return 1; +} diff --git a/pr.h b/pr.h new file mode 100644 index 0000000..e3d8bf8 --- /dev/null +++ b/pr.h @@ -0,0 +1,11 @@ +#ifndef PR_H +#define PR_H + +#include "lex.h" +#include "parse.h" +#include "prv.h" + +int pr(const struct lex *l, struct prv *p); +void pr_free(struct pr *pr); + +#endif diff --git a/prv.h b/prv.h new file mode 100644 index 0000000..86afe46 --- /dev/null +++ b/prv.h @@ -0,0 +1,45 @@ +#ifndef PRV_H +#define PRV_H + +#include "lex.h" +#include + +struct step +{ + enum tktype type; + const char *id; + int chain; +}; + +struct pos +{ + const struct seq *seq, *stseq; + const struct step *step; +}; + +struct prv +{ + const struct tk *tk, *stk; + struct storage *st; + struct tdconstant *tdc; + struct pos *pos; + struct ast *ast; + struct type *nest; + size_t n, i, block; +}; + +struct seq +{ + const struct step *steps; + int (*fn)(const struct lex *l, struct prv *p); + int (*end)(const struct lex *l, struct prv *p); + const char *chain; +}; + +int kw(const char *s); +int prev(const struct prv *p); +int push(const struct pos *pos, struct prv *p); +int pop(const struct lex *l, struct prv *p); +char *sdup(const char *s); + +#endif diff --git a/push.c b/push.c new file mode 100644 index 0000000..d31de5e --- /dev/null +++ b/push.c @@ -0,0 +1,26 @@ +#include "prv.h" +#include + +int push(const struct pos *pos, struct prv *p) +{ + size_t n = p->n + 1; + struct pos *npos = realloc(p->pos, n * sizeof *npos); + + if (!npos) + { + perror("realloc(3)"); + return -1; + } + else if (p->n) + { + struct pos *prev = &npos[p->n - 1]; + + prev->seq = prev->stseq; + prev->step = prev->seq->steps; + p->i++; + } + + npos[p->n++] = *pos; + p->pos = npos; + return 0; +} diff --git a/sdup.c b/sdup.c new file mode 100644 index 0000000..e990744 --- /dev/null +++ b/sdup.c @@ -0,0 +1,16 @@ +#include "prv.h" +#include +#include +#include + +char *sdup(const char *s) +{ + char *n = malloc(strlen(s) + 1); + + if (n) + strcpy(n, s); + else + perror("malloc(3)"); + + return n; +} diff --git a/set.c b/set.c new file mode 100644 index 0000000..389da7a --- /dev/null +++ b/set.c @@ -0,0 +1,447 @@ +#include "set.h" +#include "cgen.h" +#include "errloc.h" +#include "fn.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include "stmt.h" +#include "storage.h" +#include "type.h" +#include +#include +#include +#include + +static int mv(const struct lex *l, struct prv *p); +static int maddr(const struct lex *l, struct prv *p); +static int msz(const struct lex *l, struct prv *p); +static int mimb(const struct lex *l, struct prv *p); + +static const struct seq seqs[] = +{ + /* size of elementary data types */ + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "void"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "byte"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "halfword"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "word"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "long"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "ubyte"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "uhalfword"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "uword"}, + {0}}, .fn = msz}, + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ID, "ulong"}, + {0}}, .fn = msz}, + + /* address of variable */ + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "address"}, + {ID, "of"}, + {ID}, + {0}}, .fn = maddr}, + + /* size of variable or type alias */ + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ID, "size"}, + {ID, "of"}, + {ANY}, + {0}}, .fn = msz}, + + /* array index or aggregate member */ + {(const struct step[]){ + {ID, NULL, 1}, + {ID, "to"}, + {ANY}, + {ID, "of"}, + {ID}, + {0}}, .fn = mimb}, + + /* simple set */ + {(const struct step[]){{ID, NULL, 1}, {ID, "to"}, {ANY}, {0}}, .fn = mv}, + {0} +}; + +static unsigned long long tonum(const char *s) +{ + unsigned long long v; + char *end; + + errno = 0; + v = strtoull(s, &end, 0); + + if (*s == '-' || errno || *end) + return strtoll(s, NULL, 0); + + return v; +} + +static int dst(const struct lex *l, const struct tk *tk, struct set *mv, + struct prv *p) +{ + const struct stentry *e = fn_var(fn_cur(p), tk); + size_t n = mv->ndsts + 1; + struct setdst *dsts; + + if (!e) + return -1; + else if (!(dsts = realloc(mv->dsts, n * sizeof *dsts))) + { + perror("realloc(3)"); + return -1; + } + + mv->dsts = dsts; + mv->dsts[mv->ndsts++] = (struct setdst){.entry = e}; + return 0; +} + +static int mv(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + const struct stentry *e; + struct stmt *stmt = stmt_cur(p); + struct set *mv = &stmt->u.set; + + while (!lex_eof(l, tk) && !kw((tk)->s)) + if (dst(l, tk++, mv, p)) + return -1; + + /* skip "to" */ + tk++; + + fputs("\t\tadding set ", stderr); + + for (size_t i = 0; i < mv->ndsts; i++) + fprintf(stderr, "%s ", mv->dsts[i].entry->tk->s); + + if (tk->type == LIT) + { + /* this can only be applied to arrays of/pointers to chars */ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; + } + else if (tk->type == NUM) + { + mv->type = MV_VAL; + mv->srcv = tonum(tk->s); + } + else if (tk->type == ID) + { + const struct fn *fn = fn_cur(p); + const struct type *t = type_find(fn, tk->s); + + if (t) + { + if (t->type != C) + { + errloc(tk, "type \"%s\" not a constant", tk->s); + return -1; + } + + mv->type = MV_VAL; + mv->srcv = t->u.c.v; + } + else if ((e = fn_var(fn_cur(p), tk))) + { + mv->type = MV_ADDR; + mv->src = e; + } + else + { + errloc(tk, "undefined reference to \"%s\"", tk->s); + return -1; + } + } + + fprintf(stderr, "to %s\n", tk->s); + + if (pop(l, p)) + return -1; + + p->stk = ++tk; + return 1; +} + +static int maddr(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + struct stmt *stmt = stmt_cur(p); + struct set *mv = &stmt->u.set; + const struct stentry *e; + + while (!lex_eof(l, tk) && !kw((tk)->s)) + if (dst(l, tk++, mv, p)) + return -1; + + /* skip "to address of" */ + tk += 3; + + if (!(e = fn_var(fn_cur(p), tk))) + { + errloc(tk, "undefined reference to \"%s\"", tk->s); + return -1; + } + else if (pop(l, p)) + return -1; + + mv->type = MV_ADDR; + mv->src = e; + p->stk = ++tk; + fputs("\t\tadding set ", stderr); + + for (size_t i = 0; i < mv->ndsts; i++) + fprintf(stderr, "%s ", mv->dsts[i].entry->tk->s); + + fprintf(stderr, "to address of %s\n", mv->src->tk->s); + return 1; +} + +static int msz(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + const struct stentry *e; + struct stmt *stmt = stmt_cur(p); + struct set *mv = &stmt->u.set; + const struct fn *fn = fn_cur(p); + const struct type *t; + + while (!lex_eof(l, tk) && !kw((tk)->s)) + if (dst(l, tk++, mv, p)) + return -1; + + /* skip "to size of" */ + tk += 3; + + if (tk->type == NUM) + { + errloc(tk, "cannot compute size of a number"); + return -1; + } + else if (tk->type == LIT) + mv->srcv = strlen(tk->s) + 1; + else if ((t = type_find(fn, tk->s))) + { + if (!t->sz) + { + errloc(tk, "type \"%s\" has no size", tk->s); + return-1; + } + + mv->srcv = t->sz; + } + else if ((e = fn_var(fn, tk))) + mv->srcv = e->t->sz; + else + return-1; + + if (pop(l, p)) + return -1; + + mv->type = MV_VAL; + p->stk = ++tk; + fputs("\t\tadding set ", stderr); + + for (size_t i = 0; i < mv->ndsts; i++) + fprintf(stderr, "%s ", mv->dsts[i].entry->tk->s); + + fprintf(stderr, "size=%llu\n", mv->srcv); + return 1; +} + +static int mimb(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + const struct fn *fn = fn_cur(p); + struct stmt *stmt = stmt_cur(p); + struct set *mv = &stmt->u.set; + while (!lex_eof(l, tk) && !kw((tk)->s)) + if (dst(l, tk++, mv, p)) + return -1; + + /* skip "to" */ + tk++; + + fputs("\t\tadding set ", stderr); + + for (size_t i = 0; i < mv->ndsts; i++) + fprintf(stderr, "%s ", mv->dsts[i].entry->tk->s); + + if (tk->type == LIT) + { + errloc(tk, "unexpected literal \"%s\"", tk->s); + return -1; + } + else if (tk->type == ID && !(mv->off = fn_var(fn, tk))) + { + errloc(tk, "undefined reference to \"%s\"", tk->s); + return -1; + } + else if (tk->type == NUM) + mv->srcv = tonum(tk->s); + + fprintf(stderr, "to [%s] ", tk->s); + + /* skip index and "of" */ + tk += 2; + + if (!(mv->src = fn_var(fn, tk))) + { + errloc(tk, "undefined reference to \"%s\"", tk->s); + return -1; + } + else if (pop(l, p)) + return -1; + + fprintf(stderr, " of %s\n", tk->s); + mv->type = MV_IMB; + p->stk = ++tk; + return 1; +} + +void set_free(struct set *m) +{ + if (m) + free(m->dsts); +} + +static int cg_val(const struct set *m, const struct stentry *dst, + struct cgen *c) +{ + const char *name = dst->tk->s; + size_t sz = dst->t->sz; + const char *szstr = cgen_sz(sz); + const struct pr *pr = c->fn->pr; + const struct param *ret = pr->ret; + + if (ret && dst == ret->entry) + printf("%%__ret =%s add %llu, 0", cgen_abity(ret->entry->t), m->srcv); + else if (cgen_global(c->fn, dst)) + printf("store%s %llu, $%s", szstr, m->srcv, name); + else + printf("%%%s_ =%s add %llu, 0", name, cgen_abity(dst->t), m->srcv); + + putchar('\n'); + return 0; +} + +int set_cgen(const struct set *m, struct cgen *c) +{ + for (size_t i = 0; i < m->ndsts; i++) + { + const struct stentry *dst = m->dsts[i].entry; + + switch (m->type) + { + case MV_VAL: + if (cg_val(m, dst, c)) + return -1; + + break; + + case MV_ADDR: + case MV_VAR: + case MV_IMB: + fprintf(stderr, "%s: TODO\n", __func__); + return -1; + } + } + + return 0; +} + +int set(const struct lex *l, struct prv *p) +{ + const struct tk *next = p->tk; + const char *ns = next->s; + struct fn *fn = fn_cur(p); + size_t n = fn->nstmts + 1; + struct stmt *stmts; + + if (lex_eof(l, next)) + { + errloc(next - 1, "incomplete set statement"); + return -1; + } + else if (kw(ns)) + { + errloc(next, "incomplete set statement"); + return -1; + } + else if (!(stmts = realloc(fn->stmts, n * sizeof *stmts))) + { + perror("realloc(3)"); + return -1; + } + + fn->stmts = stmts; + fn->stmts[fn->nstmts++] = (struct stmt){.type = SET}; + + struct pos init = + { + .seq = seqs, + .stseq = seqs, + .step = seqs->steps + }; + + if (push(&init, p)) + return -1; + + p->stk = p->tk; + return 1; +} diff --git a/set.h b/set.h new file mode 100644 index 0000000..1908c86 --- /dev/null +++ b/set.h @@ -0,0 +1,29 @@ +#ifndef set_H +#define set_H + +#include "cgen.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include "storage.h" +#include + +struct setdst +{ + const struct stentry *entry; +}; + +struct set +{ + enum {MV_VAL, MV_VAR, MV_ADDR, MV_IMB} type; + const struct stentry *off, *src; + unsigned long long srcv; + struct setdst *dsts; + size_t ndsts; +}; + +int set(const struct lex *l, struct prv *p); +int set_cgen(const struct set *m, struct cgen *c); +void set_free(struct set *m); + +#endif diff --git a/stmt.c b/stmt.c new file mode 100644 index 0000000..97cbe70 --- /dev/null +++ b/stmt.c @@ -0,0 +1,59 @@ +#include "stmt.h" +#include "call.h" +#include "fn.h" +#include "parse.h" +#include "prv.h" +#include "display.h" +#include "exit.h" +#include "set.h" + +void stmt_free(struct stmt *s) +{ + switch (s->type) + { + case DISPLAY: + display_free(&s->u.display); + break; + case SET: + set_free(&s->u.set); + break; + case CALL: + call_free(&s->u.call); + break; + } +} + +int stmt_cgen(const struct stmt *s, struct cgen *c) +{ + switch (s->type) + { + case DISPLAY: + return display_cgen(&s->u.display, c); + case SET: + return set_cgen(&s->u.set, c); + case CALL: + return call_cgen(&s->u.call, c); + } + + fprintf(stderr, "%s: unreachable\n", __func__); + return -1; +} + +struct stmt *stmt_cur(const struct prv *p) +{ + const struct fn *fn = fn_cur(p); + + return &fn->stmts[fn->nstmts - 1]; +} + +const struct seq stmts[] = +{ + {(const struct step[]){ + {ID, "display"}, {ANY, NULL, 1}, {ID, "etc"}, {0}}, .fn = display}, + {(const struct step[]) + {{ID, "display"}, {ANY, NULL, 1}, {0}}, .fn = display}, + {(const struct step[]){{ID, "exit"}, {0}}, .fn = s_exit}, + {(const struct step[]){{ID, "set"}, {0}}, .fn = set}, + {(const struct step[]){{ID, "call"}, {0}}, .fn = call}, + {0} +}; diff --git a/stmt.h b/stmt.h new file mode 100644 index 0000000..0ee70cb --- /dev/null +++ b/stmt.h @@ -0,0 +1,32 @@ +#ifndef STMTS_H +#define STMTS_H + +#include "cgen.h" +#include "prv.h" +#include "call.h" +#include "display.h" +#include "set.h" + +struct stmt +{ + enum + { + DISPLAY, + SET, + CALL + } type; + + union + { + struct display display; + struct set set; + struct call call; + } u; +}; + +struct stmt *stmt_cur(const struct prv *p); +int stmt_cgen(const struct stmt *s, struct cgen *c); +void stmt_free(struct stmt *s); +extern const struct seq stmts[]; + +#endif diff --git a/storage.c b/storage.c new file mode 100644 index 0000000..87b238a --- /dev/null +++ b/storage.c @@ -0,0 +1,209 @@ +#include "storage.h" +#include "errloc.h" +#include "fn.h" +#include "lex.h" +#include "prv.h" +#include "type.h" +#include +#include +#include + +static int vab(const struct lex *l, struct prv *p); +static int vac(const struct lex *l, struct prv *p); +static int vb(const struct lex *l, struct prv *p); +static int vc(const struct lex *l, struct prv *p); + +#define VAB(x) \ + {(const struct step[]){{ID}, {ID, "array"}, {ID, x}, {NUM}, {0}}, .fn = vab} + +#define VB(x) {(const struct step[]){{ID}, {ID, x}, {0}}, .fn = vb} + +static const struct seq seqs[] = +{ + /* array with built-in type */ + VAB("void"), + VAB("byte"), + VAB("halfword"), + VAB("word"), + VAB("long"), + VAB("ubyte"), + VAB("uhalfword"), + VAB("uword"), + VAB("ulong"), + + /* array with custom type */ + {(const struct step[]){{ID}, {ID, "array"}, {ID}, {0}}, .fn = vac}, + + /* var with built-in type */ + VB("void"), + VB("byte"), + VB("halfword"), + VB("word"), + VB("long"), + VB("ubyte"), + VB("uhalfword"), + VB("uword"), + VB("ulong"), + + /* var with custom type */ + {(const struct step[]){{ID}, {ID}, {0}}, .fn = vc}, + {0} +}; + +static int stpush(const struct lex *l, struct prv *p, const struct tk *tk, + const struct tk *type) +{ + const struct ast *ast = p->ast; + struct pos *pos = &p->pos[p->i]; + struct stentry *entries; + struct storage *st = p->st; + size_t n = st->nentries + 1; + const struct type *t = type_find(&ast->fns[ast->nfns - 1], type->s); + const struct loc *loc = &tk->loc; + + if (!t) + { + errloc(type, "undefined reference to type \"%s\"", type->s); + return -1; + } + else if (!t->sz) + { + errloc(type, "type \"%s\" has null size", type->s); + return -1; + } + else if (!(entries = realloc(st->entries, n * sizeof *entries))) + { + perror("realloc(3)"); + return -1; + } + + st->offset += st->offset % t->sz; + entries[st->nentries++] = (struct stentry) + { + .t = t, + .tk = tk, + .offset = st->offset + }; + + st->entries = entries; + st->offset += t->sz; + p->stk = p->tk; + pos->seq = seqs; + pos->step = seqs->steps; + fprintf(stderr, "\tadding %s (%d:%d) to %s, type %s, offset %zu\n", + tk->s, loc->line, loc->col, st->name, type->s, + entries[st->nentries - 1].offset); + return 1; +} + +static int vb(const struct lex *l, struct prv *p) +{ + const struct tk *name = p->stk, *type = p->tk - 1; + const struct fn *fn = fn_cur(p); + const struct storage *st = p->st; + const struct stentry *e; + + if (st->check && (e = fn_var(fn, name))) + { + const struct loc *loc = &e->tk->loc; + + errloc(name, "variable \"%s\" already defined at %s:%d:%d", + name->s, loc->f, loc->line, loc->col); + return -1; + } + + /* find var in currently known symbols */ + for (size_t i = 0; i < st->nentries; i++) + { + const struct stentry *e = &st->entries[i]; + const struct tk *tk = e->tk; + + if (!strcmp(tk->s, name->s)) + { + const struct loc *loc = &tk->loc; + + errloc(name, "variable \"%s\" already defined at %s:%d:%d", + name->s, loc->f, loc->line, loc->col); + return -1; + } + } + + return stpush(l, p, name, type); +} + +static int vc(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} + +static int vab(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} + +static int vac(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} + +void storage_free(struct storage *s) +{ + if (s) + free(s->entries); + + free(s); +} + +const struct stentry *storage_find(const char *name, const struct storage *s) +{ + for (size_t i = 0; i < s->nentries; i++) + { + const struct stentry *e = &s->entries[i]; + + if (!strcmp(name, e->tk->s)) + return e; + } + + return NULL; +} + +int storage(const struct lex *l, struct prv *p, const char *name, int check, + struct storage **out) +{ + struct storage *st = NULL; + const struct tk *tk = p->tk - 1; + struct pos init = + { + .seq = seqs, + .stseq = seqs, + .step = seqs->steps + }; + + if (*out) + { + const struct loc *loc = &(*out)->tk->loc; + + errloc(tk, "%s already defined at %s:%d:%d", name, loc->f, loc->line, + loc->col); + goto failure; + } + else if (!(st = malloc(sizeof *st))) + { + perror("malloc(3)"); + goto failure; + } + else if (push(&init, p)) + goto failure; + + *st = (struct storage){.name = name, .tk = tk, .check = check}; + *out = p->st = st; + p->stk = p->tk; + return 1; + +failure: + free(st); + return -1; +} diff --git a/storage.h b/storage.h new file mode 100644 index 0000000..a3509c5 --- /dev/null +++ b/storage.h @@ -0,0 +1,29 @@ +#ifndef STORAGE_H +#define STORAGE_H + +#include "lex.h" +#include "prv.h" +#include + +struct stentry +{ + const struct tk *tk; + const struct type *t; + size_t offset; +}; + +struct storage +{ + const char *name; + const struct tk *tk; + struct stentry *entries; + size_t nentries, offset; + int check; +}; + +int storage(const struct lex *l, struct prv *p, const char *name, int check, + struct storage **out); +const struct stentry *storage_find(const char *name, const struct storage *s); +void storage_free(struct storage *s); + +#endif diff --git a/td.c b/td.c new file mode 100644 index 0000000..34908df --- /dev/null +++ b/td.c @@ -0,0 +1,571 @@ +#include "td.h" +#include "errloc.h" +#include "fn.h" +#include "lex.h" +#include "parse.h" +#include "prv.h" +#include "storage.h" +#include "type.h" +#include +#include +#include +#include +#include + +static int cid(const struct lex *, struct prv *); +static int cnum(const struct lex *, struct prv *); +static int end(const struct lex *, struct prv *); +static int u(const struct lex *, struct prv *); +static int s(const struct lex *, struct prv *); +static int c(const struct lex *, struct prv *); +static int p(const struct lex *, struct prv *); +static int t(const struct lex *, struct prv *); + +static const struct seq seqs[] = +{ + {(const struct step[]){{ID, "end"}, {0}}, .fn = end}, + {(const struct step[]){ + {ID}, {ID, "constant"}, {ID, "to"}, {ID}, {0}}, .fn = cid}, + {(const struct step[]){ + {ID}, {ID, "constant"}, {ID, "to"}, {NUM}, {0}}, .fn = cnum}, + /* type alias to built-in types */ + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "byte"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "halfword"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "word"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "long"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "ubyte"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "uhalfword"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "uword"}, {0}}, .fn = t}, + {(const struct step[]){ + {ID}, {ID, "type"}, {ID, "to"}, {ID, "ulong"}, {0}}, .fn = t}, + /* type alias to custom type */ + {(const struct step[]){{ID}, {ID, "type"}, {ID, "to"}, {ID}, {0}}, .fn = t}, + {(const struct step[]){{ID}, {ID, "union"}, {0}}, .fn = u}, + {(const struct step[]){{ID}, {ID, "struct"}, {0}}, .fn = s}, + {(const struct step[]){{ID}, {ID, "constant"}, {0}}, .fn = c}, + {(const struct step[]){{ID}, {ID, "prototype"}, {0}}, .fn = p}, + {0} +}; + +static void reset(struct prv *p) +{ + struct pos *pos = &p->pos[p->i]; + + pos->seq = seqs; + pos->step = seqs->steps; + p->stk = p->tk; +} + +static int bump(const struct tk *tk, const struct type *t, struct prv *p) +{ + struct tdconstant *tdc = p->tdc; + int neg = 0; + + if (!t->sign) + { + unsigned long long uv = t->u.c.uv; + + if (uv + 1 < uv) + { + errloc(tk, "constant value exceeds maximum (%llu)", ULLONG_MAX); + return -1; + } + + tdc->u.uv = ++uv; + } + else + { + long long v = t->u.c.v; + + if (++v < 0) + neg = 1; + + tdc->u.v = v; + } + + tdc->neg = neg; + + for (size_t i = 0; i < p->block; i++) + fputc('\t', stderr); + + fprintf(stderr, "\t\tnext %s constant value set to ", + neg ? "negative" : "positive"); + + if (neg) + fprintf(stderr, "%lld", tdc->u.v); + else + fprintf(stderr, "%llu", tdc->u.uv); + + fputc('\n', stderr); + return 0; +} + +static int cid(const struct lex *l, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + const struct tk *id = p->stk, *v = p->stk + 3; + const struct stentry *e; + const struct type *t = type_find(fn, id->s); + struct tdconstant *tdc = p->tdc; + struct td *td = fn->td; + size_t n = td->ntypes + 1; + struct type *types, *nt; + + if (t) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, + loc->line, loc->col); + return -1; + } + else if ((e = fn_var(fn, id))) + { + const struct loc *loc = &e->tk->loc; + + errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, + loc->f, loc->line, loc->col); + return -1; + } + else if ((e = fn_var(fn, v))) + { + const struct loc *loc = &e->tk->loc; + + errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant", + v->s, loc->f, loc->line, loc->col); + return -1; + } + else if (!(t = type_find(fn, v->s))) + { + errloc(id, "undefined reference to value \"%s\"", v->s); + return -1; + } + else if (t->type != C) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant", + v->s, loc->f, loc->line, loc->col); + return -1; + } + else if (!(types = realloc(td->types, n * sizeof *types))) + { + perror("realloc(3)"); + return -1; + } + + td->types = types; + nt = &td->types[td->ntypes++]; + *nt = (struct type) + { + .type = C, + .tk = id, + .sign = t->sign, + .u.c = t->u.c + }; + + tdc->neg = t->sign; + + if (bump(id, t, p)) + return -1; + + for (size_t i = 0; i < p->block; i++) + fputc('\t', stderr); + + fprintf(stderr, "\tadding constant %s to %s (%lld)\n", id->s, v->s, + t->u.c.v); + reset(p); + return 1; +} + +static int cnum(const struct lex *l, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + const struct tk *id = p->stk, *num = p->stk + 3; + const char *name = id->s; + const struct stentry *e; + struct td *td = fn->td; + size_t n = td->ntypes + 1; + struct type *types, *nt; + struct tdconstant *tdc = p->tdc; + const struct type *t = type_find(fn, name); + int sign = *num->s == '-'; + unsigned long long uv = 0; + long long v; + char *end; + + errno = 0; + v = strtoll(num->s, &end, 0); + + if (!sign || errno || *end) + { + errno = 0; + uv = strtoull(num->s, NULL, 0); + } + + if (t) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", name, loc->f, + loc->line, loc->col); + return -1; + } + else if ((e = fn_var(fn, id))) + { + const struct loc *loc = &e->tk->loc; + + errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, + loc->f, loc->line, loc->col); + return -1; + } + else if (!(types = realloc(td->types, n * sizeof *types))) + { + perror("realloc(3)"); + return -1; + } + + td->types = types; + nt = &td->types[td->ntypes++]; + *nt = (struct type) + { + .type = C, + .tk = id, + .sign = sign + }; + + if (sign) + tdc->u.v = nt->u.c.v = v; + else + tdc->u.uv = nt->u.c.uv = uv; + + tdc->neg = sign; + + for (size_t i = 0; i < p->block; i++) + fputc('\t', stderr); + + fprintf(stderr, "\tadding constant %s to %s\n", id->s, num->s); + + if (bump(id, nt, p)) + return -1; + + reset(p); + return 1; +} + +static int c(const struct lex *l, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + const struct tk *id = p->stk; + const struct stentry *e; + const struct type *t = type_find(fn, id->s); + struct td *td = fn->td; + size_t n = td->ntypes + 1; + struct type *types, *nt; + const struct tdconstant *tdc = p->tdc; + + if (t) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, + loc->line, loc->col); + return -1; + } + else if ((e = fn_var(fn, id))) + { + const struct loc *loc = &e->tk->loc; + + errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, + loc->f, loc->line, loc->col); + return -1; + } + else if (!(types = realloc(td->types, n * sizeof *types))) + { + perror("realloc(3)"); + return -1; + } + + td->types = types; + nt = &td->types[td->ntypes]; + *nt = (struct type) + { + .type = C, + .tk = id, + .sign = tdc->neg, + }; + + if (nt->sign) + nt->u.c.v = tdc->u.v; + else + nt->u.c.uv = tdc->u.uv; + + for (size_t i = 0; i < p->block; i++) + fputc('\t', stderr); + + fprintf(stderr, "\tadding constant %s to ", id->s); + + if (tdc->neg) + fprintf(stderr, "%lld", tdc->u.v); + else + fprintf(stderr, "%llu", tdc->u.uv); + + fputc('\n', stderr); + + if (bump(id, &td->types[td->ntypes++], p)) + return -1; + + reset(p); + return 1; +} + +static int ends(struct type *t) +{ + const struct s *s = &t->u.s; + const struct storage *st = s->storage; + const struct stentry *e; + + if (!st->nentries) + { + errloc(t->tk, "struct \"%s\" has no children", t->tk->s); + return -1; + } + + e = &st->entries[st->nentries - 1]; + t->sz = e->offset + e->t->sz; + fprintf(stderr, "\tadding struct %s, size %zu\n", t->tk->s, t->sz); + return 0; +} + +static int endu(struct type *t) +{ + const struct u *u = &t->u.u; + + if (!u->storage->nentries) + { + errloc(t->tk, "struct \"%s\" has no children", t->tk->s); + return -1; + } + + return 0; +} + +static int end(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->tk - 1; + struct type *t; + + if (!p->block) + { + errloc(tk, "unexpected \"end\" statement"); + return -1; + } + else if (!(t = p->nest)) + { + errloc(tk, "unreachable"); + return -1; + } + + switch (t->type) + { + case U: + if (endu(t)) + return -1; + + break; + + case S: + if (ends(t)) + return -1; + + break; + + default: + errloc(tk, "unreachable"); + return -1; + } + + p->nest = NULL; + p->block--; + reset(p); + return 1; +} + +static int s(const struct lex *l, struct prv *p) +{ + const struct tk *id = p->stk; + struct fn *fn = fn_cur(p); + struct td *td = fn->td; + size_t nt = td->ntypes + 1; + struct type *types, *newt; + const struct type *t = type_find(fn, id->s); + + if (t) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, + loc->line, loc->col); + return -1; + } + else if (!(types = realloc(td->types, nt * sizeof *types))) + { + perror("realloc(3)"); + return -1; + } + + td->types = types; + newt = &td->types[td->ntypes++]; + *newt = (struct type) + { + .type = S, + .tk = id + }; + + p->nest = newt; + p->block++; + return storage(l, p, id->s, 0, &newt->u.s.storage); +} + +static int u(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} + +static int t(const struct lex *l, struct prv *p) +{ + const struct fn *fn = fn_cur(p); + const struct tk *id = p->stk, *v = p->stk + 3; + const struct type *tp = type_find(fn, id->s); + struct td *td = fn->td; + size_t n = td->ntypes + 1; + struct type *types, *nt; + + if (tp) + { + const struct loc *loc = &tp->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, + loc->line, loc->col); + return -1; + } + else if (!(tp = type_find(fn, v->s))) + { + errloc(id, "undefined reference to type \"%s\"", v->s); + return -1; + } + else if (!(types = realloc(td->types, n * sizeof *types))) + { + perror("realloc(3)"); + return -1; + } + + td->types = types; + nt = &td->types[td->ntypes++]; + *nt = (struct type) + { + .type = T, + .tk = id, + .u.t.alias = tp + }; + + fprintf(stderr, "\tadding %s as alias for %s\n", id->s, v->s); + reset(p); + return 1; +} + +static int p(const struct lex *l, struct prv *p) +{ + fprintf(stderr, "%s: TODO\n", __func__); + return -1; +} + +static void type_free(struct type *t) +{ + switch (t->type) + { + case U: + { + struct u *u = &t->u.u; + + storage_free(u->storage); + } + break; + case S: + { + struct s *s = &t->u.s; + + storage_free(s->storage); + } + break; + case C: + case P: + case T: + case BUILTIN: + break; + } +} + +void td_free(struct td *td) +{ + if (td) + { + for (size_t i = 0; i < td->ntypes; i++) + type_free(&td->types[i]); + + free(td->types); + } + + free(td); +} + +int td(const struct lex *l, struct prv *p) +{ + const struct tk *tk = p->stk; + struct fn *fn = fn_cur(p); + struct td *td = NULL, *prev = fn->td; + struct tdconstant *tdc = NULL; + struct pos init = + { + .seq = seqs, + .stseq = seqs, + .step = seqs->steps + }; + + if (prev) + { + const struct loc *loc = &prev->tk->loc; + + errloc(tk, "types section already defined at %s:%d:%d", loc->f, + loc->line, loc->col); + goto failure; + } + else if (!(td = malloc(sizeof *td)) + || !(tdc = malloc(sizeof *tdc))) + { + perror("malloc(3)"); + goto failure; + } + else if (push(&init, p)) + goto failure; + + *tdc = (struct tdconstant){0}; + *td = (struct td){.tk = tk}; + fn->td = td; + p->tdc = tdc; + p->stk = p->tk; + return 1; + +failure: + free(tdc); + free(td); + return -1; +} diff --git a/td.h b/td.h new file mode 100644 index 0000000..2e23348 --- /dev/null +++ b/td.h @@ -0,0 +1,22 @@ +#ifndef TD_H +#define TD_H + +#include "lex.h" +#include "parse.h" +#include "prv.h" + +struct tdconstant +{ + int neg; + + union + { + unsigned long long uv; + long long v; + } u; +}; + +int td(const struct lex *l, struct prv *prv); +void td_free(struct td *td); + +#endif diff --git a/type.c b/type.c new file mode 100644 index 0000000..b142227 --- /dev/null +++ b/type.c @@ -0,0 +1,51 @@ +#include "type.h" +#include "parse.h" +#include + +const struct type *type_find(const struct fn *fn, const char *s) +{ + static const struct type builtins[] = + { + {.type = BUILTIN, .u.b.name = "void"}, + {.type = BUILTIN, .u.b.name = "byte", .sz = 1, .sign = 1}, + {.type = BUILTIN, .u.b.name = "halfword", .sz = 2, .sign = 1}, + {.type = BUILTIN, .u.b.name = "word", .sz = 4, .sign = 1}, + {.type = BUILTIN, .u.b.name = "long", .sz = 8, .sign = 1}, + {.type = BUILTIN, .u.b.name = "ubyte", .sz = 1}, + {.type = BUILTIN, .u.b.name = "uhalfword", .sz = 2}, + {.type = BUILTIN, .u.b.name = "uword", .sz = 4}, + {.type = BUILTIN, .u.b.name = "ulong", .sz = 8} + }; + + const struct td *td = fn->td; + + for (size_t i = 0; i < sizeof builtins / sizeof *builtins; i++) + { + const struct type *t = &builtins[i]; + + if (!strcmp(t->u.b.name, s)) + return t; + } + + if (td) + for (size_t i = 0; i < td->ntypes; i++) + { + const struct type *t = &td->types[i]; + + if (!strcmp(t->tk->s, s)) + { + if (t->type == T) + { + const struct type *a = t->u.t.alias; + const char *name = a->type == BUILTIN ? + a->u.b.name : a->tk->s; + + return type_find(fn, name); + } + else + return t; + } + } + + return NULL; +} diff --git a/type.h b/type.h new file mode 100644 index 0000000..d2c7fc3 --- /dev/null +++ b/type.h @@ -0,0 +1,8 @@ +#ifndef TYPE_H +#define TYPE_H + +#include "parse.h" + +const struct type *type_find(const struct fn *fn, const char *s); + +#endif diff --git a/ws.c b/ws.c new file mode 100644 index 0000000..1d82156 --- /dev/null +++ b/ws.c @@ -0,0 +1,12 @@ +#include "ws.h" +#include "prv.h" +#include "parse.h" +#include "storage.h" + +int ws(const struct lex *l, struct prv *p) +{ + struct ast *ast = p->ast; + struct fn *fn = &ast->fns[ast->nfns - 1]; + + return storage(l, p, "storage section", 1, &fn->ws); +} diff --git a/ws.h b/ws.h new file mode 100644 index 0000000..847861f --- /dev/null +++ b/ws.h @@ -0,0 +1,15 @@ +#ifndef WS_H +#define WS_H + +#include "prv.h" + +struct ws +{ + int dummy; +}; + +int ws(const struct lex *l, struct prv *p); +void ws_free(struct ws *ws); +extern const struct seq wsseq[]; + +#endif -- cgit v1.2.3