commit 45cfd8ff11770f7dcfbb1c0fe4ff1434565d896a Author: Peter McGoron Date: Thu Aug 22 20:05:49 2024 -0400 add doubly linked lists, tests, minischeme diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..357e144 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.POSIX: + +miniscm-run-tests: miniscm/scm tests.scm + ./miniscm/scm < tests.scm +miniscm/scm: + c89 -DSYSV -DInitFile=\"miniscm/init.scm\" miniscm/miniscm.c -o miniscm/scm +clean: + rm -f miniscm diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..cd78e66 --- /dev/null +++ b/README.rst @@ -0,0 +1,24 @@ +======= +UNSLISP +======= + +R7RS compiler written in a basic form of Scheme. Compiles to Universal +Service GLLV bytecode. + +License: GPL-3.0-only + +------------------- +Source Restrictions +------------------- + +Although the compiler handles all of R7RS, the source of UNSLISP is +designed to be used by a severely limited Scheme interpreter, which + +* lacks ``call/cc``, ``call-with-values``, etc +* lacks user definable macros +* only uses required features from R3RS +* has fixnums only +* minimizes the use of strings +* does not use "load" recursively + +The goal is to have the compiler run under MiniScheme 0.85 in DOS. diff --git a/doubly-linked-list.scm b/doubly-linked-list.scm new file mode 100644 index 0000000..fa6bd20 --- /dev/null +++ b/doubly-linked-list.scm @@ -0,0 +1,332 @@ +;;; Copyright (C) Peter McGoron 2024 +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, version 3 of the License. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Code to handle the linked list container. +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; +;;; The container is constructed with DL:CTR and has two fields, +;;; head and tail which point to the start and end of the list. + +;;; (DL:CTR) constructs an empty doubly linked list container. +(define dl:ctr + (lambda () (cons '() '()))) + +(define dl:head car) +(define dl:set-head! set-car!) +(define dl:tail cdr) +(define dl:set-tail! set-cdr!) + +;;; (DL:EMPTY? CTR) tests if CTR is a list of zero elements. +(define dl:empty? + (lambda (ctr) + (null? (dl:head ctr)))) + +;;; (DL:ADD-FIRST-ELEMENT CTR ELEM) initializes CTR to be a list of one +;;; element ELEM. +(define dl:add-first-element + (lambda (ctr elem) + (dl:set-head! ctr elem) + (dl:set-tail! ctr elem))) + +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Code to handle list elements +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; List elements are constructed with (DL:ELEM VAL), where VAL is the +;;; value to be stored in the list. + +;;; (DL:ELEM VAL) constructs a new doubly linked list element with element +;;; VAL. +(define dl:elem + (lambda (val) + (vector '() val '()))) + +(define dl:prev + (lambda (elem) (vector-ref elem 0))) +(define dl:set-prev! + (lambda (elem prev) (vector-set! elem 0 prev))) + +(define dl:val + (lambda (elem) (vector-ref elem 1))) + +(define dl:val-equal? + (lambda (elem x) (equal? (dl:val elem) x))) + +(define dl:next + (lambda (elem) (vector-ref elem 2))) +(define dl:set-next! + (lambda (elem next) (vector-set! elem 2 next))) + +(define dl:is-head? + (lambda (ctr elem) + (eqv? (dl:head ctr) elem))) + +(define dl:is-tail? + (lambda (ctr elem) + (eqv? (dl:tail ctr) elem))) + +(define dl:link + (lambda (before after) + (dl:set-next! before after) + (dl:set-prev! after before))) + +(define dl:unlink + (lambda (elem) + (let ((prev (dl:prev elem)) + (next (dl:next elem))) + (if (not (null? prev)) + (dl:set-next! prev next) + '()) + (if (not (null? next)) + (dl:set-prev! next prev) + '()) + (dl:set-prev! elem '()) + (dl:set-next! elem '())))) + +;;; ;;;;;;;;;;;;;;;;;;;;;; +;;; Linked List operations +;;; ;;;;;;;;;;;;;;;;;;;;;; + +;;; (DL:PUSH CTR ELEM) pushes ELEM to the head of the list in CTR. +;;; The element after ELEM is the previous head. +(define dl:push + (lambda (ctr elem) + (if (dl:empty? ctr) + (dl:add-first-element ctr elem) + (begin + (dl:set-prev! elem '()) + (dl:link elem (dl:head ctr)) + (dl:set-head! ctr elem))))) + +;;; (DL:PUSH-TAIL CTR ELEM) pushes ELEM to the tail of the list in CTR. +;;; The element before ELEM is the previous tail. +(define dl:push-tail + (lambda (ctr elem) + (if (dl:empty? ctr) + (dl:add-first-element ctr elem) + (begin + (dl:set-next! elem '()) + (dl:link (dl:tail ctr) elem) + (dl:set-tail! ctr elem))))) + +;;; (DL:FIND CTR OK?) searches CTR for the first element whose value +;;; VAL satisfies (OK? VAL). If there is no element, the function +;;; returns #F. +(define dl:find + (lambda (ctr ok?) + (letrec + ((loop + (lambda (element) + (cond + ((null? element) #f) + ((ok? (dl:val element)) element) + (else (loop (dl:next element))))))) + (loop (dl:head ctr))))) + +;;; (DL:FIND-EQUAL CTR VAL) searches CTR for the first element such that +;;; (EQUAL? CTR (DL:VAL ELEM)). See documentation for DL:FIND +(define dl:find-equal + (lambda (ctr val) (dl:find ctr (lambda (x) + (equal? val x))))) + +;;; (DL:INSERT-BEFORE CTR ELEM TO-BE-INSERTED) +;;; places TO-BE-INSERTED in the list before ELEM. ELEM must be a part of +;;; the list. +(define dl:insert-before + (lambda (ctr elem to-be-inserted) + (if (dl:is-head? ctr elem) + (dl:push ctr to-be-inserted) + (let ((prev (dl:prev elem))) + (dl:link prev to-be-inserted) + (dl:link to-be-inserted elem))))) + +;;; (DL:INSERT-AFTER CTR ELEM TO-BE-INSERTED) +;;; places TO-BE-INSERTED in the list after ELEM. ELEM must be a part of +;;; the list. +(define dl:insert-after + (lambda (ctr elem to-be-inserted) + (if (dl:is-tail? ctr elem) + (dl:push-tail ctr to-be-inserted) + (let ((next (dl:next elem))) + (dl:link to-be-inserted next) + (dl:link elem to-be-inserted))))) + +;;; (DL:REMOVE CTR ELEM) removes ELEM from the list by linking the +;;; previous and next elements of the list together (if they exist). +(define dl:remove + (lambda (ctr elem) + (let ((was-head? (dl:is-head? ctr elem)) + (was-tail? (dl:is-tail? ctr elem)) + (prev (dl:prev elem)) + (next (dl:next elem))) + (dl:unlink elem) + (if was-head? + (dl:set-head! ctr next) + #f) + (if was-tail? + (dl:set-tail! ctr tail) + #f)))) + +;;; (DL:PUSH-LIST-BACK CTR LST) appends LST to CTR. +(define dl:push-list-back + (lambda (ctr lst) + (if (null? lst) + ctr + (begin + (dl:push-tail ctr (dl:elem (car lst))) + (dl:push-list-back ctr (cdr lst)))))) + +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Tests +;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define %dl:equal-to-list? + (lambda (ctr lst) + (letrec + ((xor? + (lambda (x y) + (if x (not y) y))) + (loop + (lambda (dl-elem lst-elem) + (if (not (dl:val-equal? dl-elem (car lst-elem))) + #f + (let ((dl-next (dl:next dl-elem)) + (lst-next (cdr lst-elem))) + (if (xor? (null? dl-next) (null? lst-next)) + #f + (if (null? dl-next) + #t + (loop dl-next lst-next)))))))) + (loop (dl:head ctr) lst)))) + +(define %dl:find-remove + (lambda (ctr val) + (let ((elem (dl:find-equal ctr val))) + (if elem + (begin + (dl:remove ctr elem) + #t) + #f)))) + +(define %dl:find-remove-from-list + (lambda (ctr lst) + (if (null? lst) + #t + (if (not (%dl:find-remove ctr (car lst))) + #f + (%dl:find-remove-from-list ctr (cdr lst)))))) + +(define %dl:test-insert-into-list + (lambda (original-list searched-value value-to-insert + insert-direction modified-list) + (let ((ctr (dl:ctr))) + (dl:push-list-back ctr original-list) + (let ((searched-elem (dl:find-equal ctr searched-value))) + (if (not searched-elem) + #f + (begin + (insert-direction ctr + searched-elem + (dl:elem value-to-insert)) + (%dl:equal-to-list? ctr modified-list))))))) + +(define %dl:tests + (list + (cons "insert one in front" + (lambda () + (let ((ctr (dl:ctr))) + (dl:push ctr (dl:elem 5)) + (%dl:equal-to-list? ctr '(5))))) + (cons "insert one in back" + (lambda () + (let ((ctr (dl:ctr))) + (dl:push-tail ctr (dl:elem 5)) + (%dl:equal-to-list? ctr '(5))))) + (cons "insert many" + (lambda () + (let ((ctr (dl:ctr)) + (to-insert '(1 2 3 4 5))) + (dl:push-list-back ctr to-insert) + (%dl:equal-to-list? ctr '(1 2 3 4 5))))) + (cons "insert then delete" + (lambda () + (let ((ctr (dl:ctr)) + (el (dl:elem 5))) + (dl:push ctr el) + (dl:remove ctr el) + (dl:empty? ctr)))) + (cons "insert many then delete all" + (lambda () + (let ((ctr (dl:ctr)) + (vals '(1 2 3 4 5))) + (dl:push-list-back ctr vals) + (if (not (%dl:find-remove-from-list ctr vals)) + #f + (dl:empty? ctr))))) + (cons "insert many then delete some" + (lambda () + (let ((ctr (dl:ctr))) + (dl:push-list-back ctr '(1 2 3 4 5 6 7 8 9 10)) + (if (not (%dl:find-remove-from-list ctr '(1 2 5 7 10))) + #f + (%dl:equal-to-list? ctr '(3 4 6 8 9)))))) + (cons "push head many" + (lambda () + (let ((ctr (dl:ctr))) + (dl:push ctr (dl:elem 1)) + (dl:push ctr (dl:elem 2)) + (dl:push ctr (dl:elem 3)) + (%dl:equal-to-list? ctr '(3 2 1))))) + (cons "insert before in the middle" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 3 + 10 + dl:insert-before + '(1 2 10 3 4 5)))) + (cons "insert before at head" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 1 + 10 + dl:insert-before + '(10 1 2 3 4 5)))) + (cons "insert before at tail" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 5 + 10 + dl:insert-before + '(1 2 3 4 10 5)))) + (cons "insert after in middle" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 3 + 10 + dl:insert-after + '(1 2 3 10 4 5)))) + (cons "insert after at head" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 1 + 10 + dl:insert-after + '(1 10 2 3 4 5)))) + (cons "insert after at tail" + (lambda () + (%dl:test-insert-into-list '(1 2 3 4 5) + 5 + 10 + dl:insert-after + '(1 2 3 4 5 10)))))) + diff --git a/miniscm/init.scm b/miniscm/init.scm new file mode 100644 index 0000000..cbc8e37 --- /dev/null +++ b/miniscm/init.scm @@ -0,0 +1,78 @@ +; This is a init file for Mini-Scheme. +; Modified for UNSLISP. + +(define (caar x) (car (car x))) +(define (cadr x) (car (cdr x))) +(define (cdar x) (cdr (car x))) +(define (cddr x) (cdr (cdr x))) +(define (caaar x) (car (car (car x)))) +(define (caadr x) (car (car (cdr x)))) +(define (cadar x) (car (cdr (car x)))) +(define (caddr x) (car (cdr (cdr x)))) +(define (cdaar x) (cdr (car (car x)))) +(define (cdadr x) (cdr (car (cdr x)))) +(define (cddar x) (cdr (cdr (car x)))) +(define (cdddr x) (cdr (cdr (cdr x)))) + +(define (list . x) x) + +(define (map proc list) + (if (pair? list) + (cons (proc (car list)) (map proc (cdr list))))) + +(define (for-each proc list) + (if (pair? list) + (begin (proc (car list)) (for-each proc (cdr list))) + #t )) + +(define (list-tail x k) + (if (zero? k) + x + (list-tail (cdr x) (- k 1)))) + +(define (list-ref x k) + (car (list-tail x k))) + +(define list-set! + (lambda (lst k obj) + (if (= k 0) + (set-car! lst obj) + (list-set! (cdr lst) (- k 1) obj)))) + +(define (last-pair x) + (if (pair? (cdr x)) + (last-pair (cdr x)) + x)) + +(define vector list) +(define vector-ref list-ref) +(define vector-set! list-set!) + +(define (head stream) (car stream)) + +(define (tail stream) (force (cdr stream))) + +;;;;; following part is written by a.k + +;;;; atom? +(define (atom? x) + (not (pair? x))) + +;;;; memq +(define (memq obj lst) + (cond + ((null? lst) #f) + ((eq? obj (car lst)) lst) + (else (memq obj (cdr lst))))) + +;;;; equal? +(define (equal? x y) + (if (pair? x) + (and (pair? y) + (equal? (car x) (car y)) + (equal? (cdr x) (cdr y))) + (and (not (pair? y)) + (eqv? x y)))) + +(display "This is a limited version of MiniScheme used to run UNSLISP.") + diff --git a/miniscm/miniscm.c b/miniscm/miniscm.c new file mode 100644 index 0000000..ea4dc90 --- /dev/null +++ b/miniscm/miniscm.c @@ -0,0 +1,2443 @@ +/* + * ---------- Mini-Scheme Interpreter Version 0.85 ---------- + * + * coded by Atsushi Moriwaki (11/5/1989) + * + * E-MAIL : moriwaki@kurims.kurims.kyoto-u.ac.jp + * + * THIS SOFTWARE IS IN THE PUBLIC DOMAIN + * ------------------------------------ + * This software is completely free to copy, modify and/or re-distribute. + * But I would appreciate it if you left my name on the code as the author. + * + */ +/*-- + * + * This version has been modified by R.C. Secrist. + * + * Mini-Scheme is now maintained by Akira KIDA. + * + * This is a revised and modified version by Akira KIDA. + * current version is 0.85k4 (15 May 1994) + * + * Please send suggestions, bug reports and/or requests to: + * + *-- + */ + +/* + * Here is System declaration. + * Please define exactly one symbol in the following section. + */ +/* #define LSC */ /* LightSpeed C for Macintosh */ +/* #define LSC4 */ /* THINK C version 4.0 for Macintosh */ +/* #define MPW2 */ /* Macintosh Programmer's Workshop v2.0x */ +/* #define BSD */ /* 4.x BSD */ +/* #define MSC */ /* Microsoft C Compiler v.4.00 - 7.00 */ +/* #define TURBOC */ /* Turbo C compiler v.2.0, or TC++ 1.0 */ +/* #define SYSV */ /* System-V, or POSIX */ +/* #define VAXC */ /* VAX/VMS VAXC 2.x or later */ /* (automatic) */ + +#ifdef __BORLANDC__ /* Borland C++ - MS-DOS */ +#define TURBOC +#endif + +#ifdef __TURBOC__ /* Turbo C V1.5 - MS-DOS */ +#define TURBOC +#endif + +#ifdef mips /* DECstation running OSF/1 */ +#define BSD +#endif + +#ifdef __osf__ /* Alpha AXP running OSF/1 */ +#define BSD +#endif + +#ifdef __DECC /* Alpha AXP running VMS */ +#define VAXC +#endif + +#ifdef _AIX /* RS/6000 running AIX */ +#define BSD +#endif + +/* + * Define or undefine following symbols as you need. + */ +/* #define VERBOSE */ /* define this if you want verbose GC */ +#define AVOID_HACK_LOOP /* define this if your compiler is poor + * enougth to complain "do { } while (0)" + * construction. + */ +#define USE_SETJMP /* undef this if you do not want to use setjmp() */ +#define USE_QQUOTE /* undef this if you do not need quasiquote */ +#define USE_MACRO /* undef this if you do not need macro */ + + +#ifdef USE_QQUOTE +/*-- + * If your machine can't support "forward single quotation character" + * i.e., '`', you may have trouble to use backquote. + * So use '^' in place of '`'. + */ +# define BACKQUOTE '`' +#endif + +/* + * Basic memory allocation units + */ + +#ifdef TURBOC /* rcs */ +#define CELL_SEGSIZE 2048 +#define CELL_NSEGMENT 100 +#define STR_SEGSIZE 2048 +#define STR_NSEGMENT 100 +#else +#define CELL_SEGSIZE 5000 /* # of cells in one segment */ +#define CELL_NSEGMENT 100 /* # of segments for cells */ +#define STR_SEGSIZE 2500 /* bytes of one string segment */ +#define STR_NSEGMENT 100 /* # of segments for strings */ +#endif + + + +#define banner "Hello, This is Mini-Scheme Interpreter Version 0.85k4-a.\n" + + +#include +#include +#ifdef USE_SETJMP +#include +#endif + + +/* System dependency */ +#ifdef LSC +#include +#include +#define malloc(x) NewPtr((long)(x)) +#define prompt "> " +#define FIRST_CELLSEGS 5 +#endif + +#ifdef LSC4 +#include +#include +#define malloc(x) NewPtr((long)(x)) +#define prompt "> " +#define FIRST_CELLSEGS 5 +#endif + +#ifdef MPW2 +#include +#include +#define malloc(x) NewPtr((long)(x)) +#define prompt "> [enter at next line]\n" +#define FIRST_CELLSEGS 5 +#endif + +#ifdef BSD +#include +#include +#define prompt "> " +#define FIRST_CELLSEGS 10 +#endif + +#ifdef MSC +#include +#include +#include +#include +#define prompt "> " +#define FIRST_CELLSEGS 3 +#endif + +#ifdef TURBOC +#include +#include +#define prompt "> " +#define FIRST_CELLSEGS 3 +#endif + +#ifdef SYSV +#include +#include +#if __STDC__ +# include +#endif +#define prompt "> " +#define FIRST_CELLSEGS 10 +#endif + +#ifdef VAXC +#include +#include +#define prompt "> " +#define FIRST_CELLSEGS 10 +#endif + +#ifdef __GNUC__ +/* + * If we use gcc, AVOID_HACK_LOOP is unnecessary + */ +#undef AVOID_HACK_LOOP +#endif + +#ifndef FIRST_CELLSEGS +#error Please define your system type. +/* + * We refrain this to raise an error anyway even if on pre-ANSI system. + */ +error Please define your system type. +#endif + +/* cell structure */ +struct cell { + unsigned short _flag; + union { + struct { + char *_svalue; + short _keynum; + } _string; + struct { + long _ivalue; + } _number; + struct { + struct cell *_car; + struct cell *_cdr; + } _cons; + } _object; +}; + +typedef struct cell *pointer; + +#define T_STRING 1 /* 0000000000000001 */ +#define T_NUMBER 2 /* 0000000000000010 */ +#define T_SYMBOL 4 /* 0000000000000100 */ +#define T_SYNTAX 8 /* 0000000000001000 */ +#define T_PROC 16 /* 0000000000010000 */ +#define T_PAIR 32 /* 0000000000100000 */ +#define T_CLOSURE 64 /* 0000000001000000 */ +#define T_CONTINUATION 128 /* 0000000010000000 */ +#ifdef USE_MACRO +# define T_MACRO 256 /* 0000000100000000 */ +#endif +#define T_PROMISE 512 /* 0000001000000000 */ +#define T_ATOM 16384 /* 0100000000000000 */ /* only for gc */ +#define CLRATOM 49151 /* 1011111111111111 */ /* only for gc */ +#define MARK 32768 /* 1000000000000000 */ +#define UNMARK 32767 /* 0111111111111111 */ + +/* macros for cell operations */ +#define type(p) ((p)->_flag) + +#define isstring(p) (type(p)&T_STRING) +#define strvalue(p) ((p)->_object._string._svalue) +#define keynum(p) ((p)->_object._string._keynum) + +#define isnumber(p) (type(p)&T_NUMBER) +#define ivalue(p) ((p)->_object._number._ivalue) + +#define ispair(p) (type(p)&T_PAIR) +#define car(p) ((p)->_object._cons._car) +#define cdr(p) ((p)->_object._cons._cdr) + +#define issymbol(p) (type(p)&T_SYMBOL) +#define symname(p) strvalue(car(p)) +#define hasprop(p) (type(p)&T_SYMBOL) +#define symprop(p) cdr(p) + +#define issyntax(p) (type(p)&T_SYNTAX) +#define isproc(p) (type(p)&T_PROC) +#define syntaxname(p) strvalue(car(p)) +#define syntaxnum(p) keynum(car(p)) +#define procnum(p) ivalue(p) + +#define isclosure(p) (type(p)&T_CLOSURE) +#ifdef USE_MACRO +# define ismacro(p) (type(p)&T_MACRO) +#endif +#define closure_code(p) car(p) +#define closure_env(p) cdr(p) + +#define iscontinuation(p) (type(p)&T_CONTINUATION) +#define cont_dump(p) cdr(p) + +#define ispromise(p) (type(p)&T_PROMISE) +#define setpromise(p) type(p) |= T_PROMISE + +#define isatom(p) (type(p)&T_ATOM) +#define setatom(p) type(p) |= T_ATOM +#define clratom(p) type(p) &= CLRATOM + +#define ismark(p) (type(p)&MARK) +#define setmark(p) type(p) |= MARK +#define clrmark(p) type(p) &= UNMARK + +#define caar(p) car(car(p)) +#define cadr(p) car(cdr(p)) +#define cdar(p) cdr(car(p)) +#define cddr(p) cdr(cdr(p)) +#define cadar(p) car(cdr(car(p))) +#define caddr(p) car(cdr(cdr(p))) +#define cadaar(p) car(cdr(car(car(p)))) +#define cadddr(p) car(cdr(cdr(cdr(p)))) +#define cddddr(p) cdr(cdr(cdr(cdr(p)))) + +/* arrays for segments */ +pointer cell_seg[CELL_NSEGMENT]; +int last_cell_seg = -1; +char *str_seg[STR_NSEGMENT]; +int str_seglast = -1; + +/* We use 4 registers. */ +pointer args; /* register for arguments of function */ +pointer envir; /* stack register for current environment */ +pointer code; /* register for current code */ +pointer dump; /* stack register for next evaluation */ + +struct cell _NIL; +pointer NIL = &_NIL; /* special cell representing empty cell */ +struct cell _T; +pointer T = &_T; /* special cell representing #t */ +struct cell _F; +pointer F = &_F; /* special cell representing #f */ +pointer oblist = &_NIL; /* pointer to symbol table */ +pointer global_env; /* pointer to global environment */ + +/* global pointers to special symbols */ +pointer LAMBDA; /* pointer to syntax lambda */ +pointer QUOTE; /* pointer to syntax quote */ + +#ifdef USE_QQUOTE +pointer QQUOTE; /* pointer to symbol quasiquote */ +pointer UNQUOTE; /* pointer to symbol unquote */ +pointer UNQUOTESP; /* pointer to symbol unquote-splicing */ + +#endif + +pointer free_cell = &_NIL; /* pointer to top of free cells */ +long fcells = 0; /* # of free cells */ + +FILE *infp; /* input file */ +FILE *outfp; /* output file */ + +#ifdef USE_SETJMP +jmp_buf error_jmp; + +#endif +char gc_verbose; /* if gc_verbose is not zero, print gc status */ + +/* allocate new cell segment */ +alloc_cellseg(n) +int n; +{ + register pointer p; + register long i; + register int k; + + for (k = 0; k < n; k++) { + if (last_cell_seg >= CELL_NSEGMENT - 1) + return k; + p = (pointer) malloc(CELL_SEGSIZE * sizeof(struct cell)); + if (p == (pointer) 0) + return k; + cell_seg[++last_cell_seg] = p; + fcells += CELL_SEGSIZE; + for (i = 0; i < CELL_SEGSIZE - 1; i++, p++) { + type(p) = 0; + car(p) = NIL; + cdr(p) = p + 1; + } + type(p) = 0; + car(p) = NIL; + cdr(p) = free_cell; + free_cell = cell_seg[last_cell_seg]; + } + return n; +} + +/* allocate new string segment */ +alloc_strseg(n) +int n; +{ + register char *p; + register long i; + register int k; + + for (k = 0; k < n; k++) { + if (str_seglast >= STR_NSEGMENT) + return k; + p = (char *) malloc(STR_SEGSIZE * sizeof(char)); + if (p == (char *) 0) + return k; + str_seg[++str_seglast] = p; + for (i = 0; i < STR_SEGSIZE; i++) + *p++ = (char) (-1); + } + return n; +} + +/* initialization of Mini-Scheme */ +init_scheme() +{ + register pointer i; + + if (alloc_cellseg(FIRST_CELLSEGS) != FIRST_CELLSEGS) + FatalError("Unable to allocate initial cell segments"); + if (!alloc_strseg(1)) + FatalError("Unable to allocate initial string segments"); +#ifdef VERBOSE + gc_verbose = 1; +#else + gc_verbose = 0; +#endif + init_globals(); +} + +/* get new cell. parameter a, b is marked by gc. */ +pointer get_cell(a, b) +register pointer a, b; +{ + register pointer x; + + if (free_cell == NIL) { + gc(a, b); + if (free_cell == NIL) +#ifdef USE_SETJMP + if (!alloc_cellseg(1)) { + args = envir = code = dump = NIL; + gc(NIL, NIL); + if (free_cell != NIL) + Error("run out of cells --- rerurn to top level"); + else + FatalError("run out of cells --- unable to recover cells"); + } +#else + if (!alloc_cellseg(1)) + FatalError("run out of cells --- unable to recover cells"); +#endif + } + x = free_cell; + free_cell = cdr(x); + --fcells; + return (x); +} + +/* get new cons cell */ +pointer cons(a, b) +register pointer a, b; +{ + register pointer x = get_cell(a, b); + + type(x) = T_PAIR; + car(x) = a; + cdr(x) = b; + return (x); +} + +/* get number atom */ +pointer mk_number(num) +register long num; +{ + register pointer x = get_cell(NIL, NIL); + + type(x) = (T_NUMBER | T_ATOM); + ivalue(x) = num; + return (x); +} + +/* allocate name to string area */ +char *store_string(name) +char *name; +{ + register char *q; + register short i; + long len, remain; + + /* first check name has already listed */ + for (i = 0; i <= str_seglast; i++) + for (q = str_seg[i]; *q != (char) (-1); ) { + if (!strcmp(q, name)) + goto FOUND; + while (*q++) + ; /* get next string */ + } + len = strlen(name) + 2; + remain = (long) STR_SEGSIZE - ((long) q - (long) str_seg[str_seglast]); + if (remain < len) { + if (!alloc_strseg(1)) + FatalError("run out of string area"); + q = str_seg[str_seglast]; + } + strcpy(q, name); +FOUND: + return (q); +} + +/* get new string */ +pointer mk_string(str) +char *str; +{ + register pointer x = get_cell(NIL, NIL); + + strvalue(x) = store_string(str); + type(x) = (T_STRING | T_ATOM); + keynum(x) = (short) (-1); + return (x); +} + +/* get new symbol */ +pointer mk_symbol(name) +char *name; +{ + register pointer x; + + /* fisrt check oblist */ + for (x = oblist; x != NIL; x = cdr(x)) + if (!strcmp(name, symname(car(x)))) + break; + + if (x != NIL) + return (car(x)); + else { + x = cons(mk_string(name), NIL); + type(x) = T_SYMBOL; + oblist = cons(x, oblist); + return (x); + } +} + +/* make symbol or number atom from string */ +pointer mk_atom(q) +char *q; +{ + char c, *p; + + p = q; + if (!isdigit(c = *p++)) { + if ((c != '+' && c != '-') || !isdigit(*p)) + return (mk_symbol(q)); + } + for ( ; (c = *p) != 0; ++p) + if (!isdigit(c)) + return (mk_symbol(q)); + return (mk_number(atol(q))); +} + +/* make constant */ +pointer mk_const(name) +char *name; +{ + long x; + char tmp[256]; + + if (!strcmp(name, "t")) + return (T); + else if (!strcmp(name, "f")) + return (F); + else if (*name == 'o') {/* #o (octal) */ + sprintf(tmp, "0%s", &name[1]); + sscanf(tmp, "%lo", &x); + return (mk_number(x)); + } else if (*name == 'd') { /* #d (decimal) */ + sscanf(&name[1], "%ld", &x); + return (mk_number(x)); + } else if (*name == 'x') { /* #x (hex) */ + sprintf(tmp, "0x%s", &name[1]); + sscanf(tmp, "%lx", &x); + return (mk_number(x)); + } else + return (NIL); +} + + +/* ========== garbage collector ========== */ + +/*-- + * We use algorithm E (Kunuth, The Art of Computer Programming Vol.1, + * sec.3.5) for marking. + */ +mark(a) +pointer a; +{ + register pointer t, q, p; + +E1: t = (pointer) 0; + p = a; +E2: setmark(p); +E3: if (isatom(p)) + goto E6; +E4: q = car(p); + if (q && !ismark(q)) { + setatom(p); + car(p) = t; + t = p; + p = q; + goto E2; + } +E5: q = cdr(p); + if (q && !ismark(q)) { + cdr(p) = t; + t = p; + p = q; + goto E2; + } +E6: if (!t) + return; + q = t; + if (isatom(q)) { + clratom(q); + t = car(q); + car(q) = p; + p = q; + goto E5; + } else { + t = cdr(q); + cdr(q) = p; + p = q; + goto E6; + } +} + + +/* garbage collection. parameter a, b is marked. */ +gc(a, b) +register pointer a, b; +{ + register pointer p; + register short i; + register long j; + + if (gc_verbose) + printf("gc..."); + + /* mark system globals */ + mark(oblist); + mark(global_env); + + /* mark current registers */ + mark(args); + mark(envir); + mark(code); + mark(dump); + + /* mark variables a, b */ + mark(a); + mark(b); + + /* garbage collect */ + clrmark(NIL); + fcells = 0; + free_cell = NIL; + for (i = 0; i <= last_cell_seg; i++) { + for (j = 0, p = cell_seg[i]; j < CELL_SEGSIZE; j++, p++) { + if (ismark(p)) + clrmark(p); + else { + type(p) = 0; + cdr(p) = free_cell; + car(p) = NIL; + free_cell = p; + ++fcells; + } + } + } + + if (gc_verbose) + printf(" done %ld cells are recovered.\n", fcells); +} + + +/* ========== Rootines for Reading ========== */ + +#define TOK_LPAREN 0 +#define TOK_RPAREN 1 +#define TOK_DOT 2 +#define TOK_ATOM 3 +#define TOK_QUOTE 4 +#define TOK_COMMENT 5 +#define TOK_DQUOTE 6 +#ifdef USE_QQUOTE +# define TOK_BQUOTE 7 +# define TOK_COMMA 8 +# define TOK_ATMARK 9 +#endif +#define TOK_SHARP 10 + +#define LINESIZE 1024 +char linebuff[LINESIZE]; +char strbuff[256]; +char *currentline = linebuff; +char *endline = linebuff; + +/* get new character from input file */ +int inchar() +{ + if (currentline >= endline) { /* input buffer is empty */ + if (feof(infp)) { + fclose(infp); + infp = stdin; + printf(prompt); + } + strcpy(linebuff, "\n"); + if (fgets(currentline = linebuff, LINESIZE, infp) == NULL) + if (infp == stdin) { + fprintf(stderr, "Good-bye\n"); + exit(0); + } + endline = linebuff + strlen(linebuff); + } + return (*currentline++); +} + +/* clear input buffer */ +clearinput() +{ + currentline = endline = linebuff; +} + +/* back to standard input */ +flushinput() +{ + if (infp != stdin) { + fclose(infp); + infp = stdin; + } + clearinput(); +} + +/* back character to input buffer */ +backchar() +{ + currentline--; +} + +/* read chacters to delimiter */ +char *readstr(delim) +char *delim; +{ + char *p = strbuff; + + while (isdelim(delim, (*p++ = inchar()))) + ; + backchar(); + *--p = '\0'; + return (strbuff); +} + +/* read string expression "xxx...xxx" */ +char *readstrexp() +{ + char c, *p = strbuff; + + for (;;) { + if ((c = inchar()) != '"') + *p++ = c; + else if (p > strbuff && *(p - 1) == '\\') + *(p - 1) = '"'; + else { + *p = '\0'; + return (strbuff); + } + } +} + +/* check c is delimiter */ +isdelim(s, c) +char *s; +char c; +{ + while (*s) + if (*s++ == c) + return (0); + return (1); +} + +/* skip white characters */ +skipspace() +{ + while (isspace(inchar())) + ; + backchar(); +} + +/* get token */ +token() +{ + skipspace(); + switch (inchar()) { + case '(': + return (TOK_LPAREN); + case ')': + return (TOK_RPAREN); + case '.': + return (TOK_DOT); + case '\'': + return (TOK_QUOTE); + case ';': + return (TOK_COMMENT); + case '"': + return (TOK_DQUOTE); +#ifdef USE_QQUOTE + case BACKQUOTE: + return (TOK_BQUOTE); + case ',': + if (inchar() == '@') + return (TOK_ATMARK); + else { + backchar(); + return (TOK_COMMA); + } +#endif + case '#': + return (TOK_SHARP); + default: + backchar(); + return (TOK_ATOM); + } +} + +/* ========== Rootines for Printing ========== */ +#define ok_abbrev(x) (ispair(x) && cdr(x) == NIL) + +strunquote(p, s) +char *p; +char *s; +{ + *p++ = '"'; + for ( ; *s; ++s) { + if (*s == '"') { + *p++ = '\\'; + *p++ = '"'; + } else if (*s == '\n') { + *p++ = '\\'; + *p++ = 'n'; + } else + *p++ = *s; + } + *p++ = '"'; + *p = '\0'; +} + + +/* print atoms */ +int printatom(l, f) +pointer l; +int f; +{ + char *p; + + if (l == NIL) + p = "()"; + else if (l == T) + p = "#t"; + else if (l == F) + p = "#f"; + else if (isnumber(l)) { + p = strbuff; + sprintf(p, "%ld", ivalue(l)); + } else if (isstring(l)) { + if (!f) + p = strvalue(l); + else { + p = strbuff; + strunquote(p, strvalue(l)); + } + } else if (issymbol(l)) + p = symname(l); + else if (isproc(l)) { + p = strbuff; + sprintf(p, "#", procnum(l)); +#ifdef USE_MACRO + } else if (ismacro(l)) { + p = "#"; +#endif + } else if (isclosure(l)) + p = "#"; + else if (iscontinuation(l)) + p = "#"; + if (f < 0) + return strlen(p); + fputs(p, outfp); + return 0; +} + + +/* ========== Rootines for Evaluation Cycle ========== */ + +/* make closure. c is code. e is environment */ +pointer mk_closure(c, e) +register pointer c, e; +{ + register pointer x = get_cell(c, e); + + type(x) = T_CLOSURE; + car(x) = c; + cdr(x) = e; + return (x); +} + +/* make continuation. */ +pointer mk_continuation(d) +register pointer d; +{ + register pointer x = get_cell(NIL, d); + + type(x) = T_CONTINUATION; + cont_dump(x) = d; + return (x); +} + +/* reverse list -- make new cells */ +pointer reverse(a) +register pointer a; /* a must be checked by gc */ +{ + register pointer p = NIL; + + for ( ; ispair(a); a = cdr(a)) + p = cons(car(a), p); + return (p); +} + +/* reverse list --- no make new cells */ +pointer non_alloc_rev(term, list) +pointer term, list; +{ + register pointer p = list, result = term, q; + + while (p != NIL) { + q = cdr(p); + cdr(p) = result; + result = p; + p = q; + } + return (result); +} + +/* append list -- make new cells */ +pointer append(a, b) +register pointer a, b; +{ + register pointer p = b, q; + + if (a != NIL) { + a = reverse(a); + while (a != NIL) { + q = cdr(a); + cdr(a) = p; + p = a; + a = q; + } + } + return (p); +} + +/* equivalence of atoms */ +eqv(a, b) +register pointer a, b; +{ + if (isstring(a)) { + if (isstring(b)) + return (strvalue(a) == strvalue(b)); + else + return (0); + } else if (isnumber(a)) { + if (isnumber(b)) + return (ivalue(a) == ivalue(b)); + else + return (0); + } else + return (a == b); +} + +/* true or false value macro */ +#define istrue(p) ((p) != NIL && (p) != F) +#define isfalse(p) ((p) == NIL || (p) == F) + +/* Error macro */ +#ifdef AVOID_HACK_LOOP +# define BEGIN { +# define END } +#else +/* + * I believe this is better, but some compiler complains.... + */ +# define BEGIN do { +# define END } while (0) +#endif + +#define Error_0(s) BEGIN \ + args = cons(mk_string((s)), NIL); \ + operator = (short)OP_ERR0; \ + return T; END + +#define Error_1(s, a) BEGIN \ + args = cons((a), NIL); \ + args = cons(mk_string((s)), args); \ + operator = (short)OP_ERR0; \ + return T; END + +/* control macros for Eval_Cycle */ +#define s_goto(a) BEGIN \ + operator = (short)(a); \ + return T; END + +#define s_save(a, b, c) ( \ + dump = cons(envir, cons((c), dump)), \ + dump = cons((b), dump), \ + dump = cons(mk_number((long)(a)), dump)) \ + + +#define s_return(a) BEGIN \ + value = (a); \ + operator = ivalue(car(dump)); \ + args = cadr(dump); \ + envir = caddr(dump); \ + code = cadddr(dump); \ + dump = cddddr(dump); \ + return T; END + +#define s_retbool(tf) s_return((tf) ? T : F) + + + +/* ========== Evaluation Cycle ========== */ + +/* operator code */ +#define OP_LOAD 0 +#define OP_T0LVL 1 +#define OP_T1LVL 2 +#define OP_READ 3 +#define OP_VALUEPRINT 4 +#define OP_EVAL 5 +#define OP_E0ARGS 6 +#define OP_E1ARGS 7 +#define OP_APPLY 8 +#define OP_DOMACRO 9 + +#define OP_LAMBDA 10 +#define OP_QUOTE 11 +#define OP_DEF0 12 +#define OP_DEF1 13 +#define OP_BEGIN 14 +#define OP_IF0 15 +#define OP_IF1 16 +#define OP_SET0 17 +#define OP_SET1 18 +#define OP_LET0 19 +#define OP_LET1 20 +#define OP_LET2 21 +#define OP_LET0AST 22 +#define OP_LET1AST 23 +#define OP_LET2AST 24 +#define OP_LET0REC 25 +#define OP_LET1REC 26 +#define OP_LET2REC 27 +#define OP_COND0 28 +#define OP_COND1 29 +#define OP_DELAY 30 +#define OP_AND0 31 +#define OP_AND1 32 +#define OP_OR0 33 +#define OP_OR1 34 +#define OP_C0STREAM 35 +#define OP_C1STREAM 36 +#define OP_0MACRO 37 +#define OP_1MACRO 38 +#define OP_CASE0 39 +#define OP_CASE1 40 +#define OP_CASE2 41 + +#define OP_PEVAL 42 +#define OP_PAPPLY 43 +#define OP_CONTINUATION 44 +#define OP_ADD 45 +#define OP_SUB 46 +#define OP_MUL 47 +#define OP_DIV 48 +#define OP_REM 49 +#define OP_CAR 50 +#define OP_CDR 51 +#define OP_CONS 52 +#define OP_SETCAR 53 +#define OP_SETCDR 54 +#define OP_NOT 55 +#define OP_BOOL 56 +#define OP_NULL 57 +#define OP_ZEROP 58 +#define OP_POSP 59 +#define OP_NEGP 60 +#define OP_NEQ 61 +#define OP_LESS 62 +#define OP_GRE 63 +#define OP_LEQ 64 +#define OP_GEQ 65 +#define OP_SYMBOL 66 +#define OP_NUMBER 67 +#define OP_STRING 68 +#define OP_PROC 69 +#define OP_PAIR 70 +#define OP_EQ 71 +#define OP_EQV 72 +#define OP_FORCE 73 +#define OP_WRITE 74 +#define OP_DISPLAY 75 +#define OP_NEWLINE 76 +#define OP_ERR0 77 +#define OP_ERR1 78 +#define OP_REVERSE 79 +#define OP_APPEND 80 +#define OP_PUT 81 +#define OP_GET 82 +#define OP_QUIT 83 +#define OP_GC 84 +#define OP_GCVERB 85 +#define OP_NEWSEGMENT 86 + +#define OP_RDSEXPR 87 +#define OP_RDLIST 88 +#define OP_RDDOT 89 +#define OP_RDQUOTE 90 +#define OP_RDQQUOTE 91 +#define OP_RDUNQUOTE 92 +#define OP_RDUQTSP 93 + +#define OP_P0LIST 94 +#define OP_P1LIST 95 + +#define OP_LIST_LENGTH 96 +#define OP_ASSQ 97 +#define OP_PRINT_WIDTH 98 +#define OP_P0_WIDTH 99 +#define OP_P1_WIDTH 100 +#define OP_GET_CLOSURE 101 +#define OP_CLOSUREP 102 +#define OP_MACROP 103 + + +static FILE *tmpfp; +static int tok; +static int print_flag; +static pointer value; +static short operator; + +pointer opexe_0(op) +register short op; +{ + register pointer x, y; + + switch (op) { + case OP_LOAD: /* load */ + if (!isstring(car(args))) { + Error_0("load -- argument is not string"); + } + if ((infp = fopen(strvalue(car(args)), "r")) == NULL) { + infp = stdin; + Error_1("Unable to open", car(args)); + } + fprintf(outfp, "loading %s", strvalue(car(args))); + s_goto(OP_T0LVL); + + case OP_T0LVL: /* top level */ + fprintf(outfp, "\n"); + dump = NIL; + envir = global_env; + s_save(OP_VALUEPRINT, NIL, NIL); + s_save(OP_T1LVL, NIL, NIL); + if (infp == stdin) + printf(prompt); + s_goto(OP_READ); + + case OP_T1LVL: /* top level */ + code = value; + s_goto(OP_EVAL); + + case OP_READ: /* read */ + tok = token(); + s_goto(OP_RDSEXPR); + + case OP_VALUEPRINT: /* print evalution result */ + print_flag = 1; + args = value; + s_save(OP_T0LVL, NIL, NIL); + s_goto(OP_P0LIST); + + case OP_EVAL: /* main part of evalution */ + if (issymbol(code)) { /* symbol */ + for (x = envir; x != NIL; x = cdr(x)) { + for (y = car(x); y != NIL; y = cdr(y)) + if (caar(y) == code) + break; + if (y != NIL) + break; + } + if (x != NIL) { + s_return(cdar(y)); + } else { + Error_1("Unbounded variable", code); + } + } else if (ispair(code)) { + if (issyntax(x = car(code))) { /* SYNTAX */ + code = cdr(code); + s_goto(syntaxnum(x)); + } else {/* first, eval top element and eval arguments */ +#ifdef USE_MACRO + s_save(OP_E0ARGS, NIL, code); +#else + s_save(OP_E1ARGS, NIL, cdr(code)); +#endif + code = car(code); + s_goto(OP_EVAL); + } + } else { + s_return(code); + } + +#ifdef USE_MACRO + case OP_E0ARGS: /* eval arguments */ + if (ismacro(value)) { /* macro expansion */ + s_save(OP_DOMACRO, NIL, NIL); + args = cons(code, NIL); + code = value; + s_goto(OP_APPLY); + } else { + code = cdr(code); + s_goto(OP_E1ARGS); + } +#endif + + case OP_E1ARGS: /* eval arguments */ + args = cons(value, args); + if (ispair(code)) { /* continue */ + s_save(OP_E1ARGS, args, cdr(code)); + code = car(code); + args = NIL; + s_goto(OP_EVAL); + } else { /* end */ + args = reverse(args); + code = car(args); + args = cdr(args); + s_goto(OP_APPLY); + } + + case OP_APPLY: /* apply 'code' to 'args' */ + if (isproc(code)) { + s_goto(procnum(code)); /* PROCEDURE */ + } else if (isclosure(code)) { /* CLOSURE */ + /* make environment */ + envir = cons(NIL, closure_env(code)); + for (x = car(closure_code(code)), y = args; + ispair(x); x = cdr(x), y = cdr(y)) { + if (y == NIL) { + Error_0("Few arguments"); + } else { + car(envir) = cons(cons(car(x), car(y)), car(envir)); + } + } + if (x == NIL) { + /*-- + * if (y != NIL) { + * Error_0("Many arguments"); + * } + */ + } else if (issymbol(x)) + car(envir) = cons(cons(x, y), car(envir)); + else { + Error_0("Syntax error in closure"); + } + code = cdr(closure_code(code)); + args = NIL; + s_goto(OP_BEGIN); + } else if (iscontinuation(code)) { /* CONTINUATION */ + dump = cont_dump(code); + s_return(args != NIL ? car(args) : NIL); + } else { + Error_0("Illegal function"); + } + +#ifdef USE_MACRO + case OP_DOMACRO: /* do macro */ + code = value; + s_goto(OP_EVAL); +#endif + + case OP_LAMBDA: /* lambda */ + s_return(mk_closure(code, envir)); + + case OP_QUOTE: /* quote */ + s_return(car(code)); + + case OP_DEF0: /* define */ + if (ispair(car(code))) { + x = caar(code); + code = cons(LAMBDA, cons(cdar(code), cdr(code))); + } else { + x = car(code); + code = cadr(code); + } + if (!issymbol(x)) { + Error_0("Variable is not symbol"); + } + s_save(OP_DEF1, NIL, x); + s_goto(OP_EVAL); + + case OP_DEF1: /* define */ + for (x = car(envir); x != NIL; x = cdr(x)) + if (caar(x) == code) + break; + if (x != NIL) + cdar(x) = value; + else + car(envir) = cons(cons(code, value), car(envir)); + s_return(code); + + case OP_SET0: /* set! */ + s_save(OP_SET1, NIL, car(code)); + code = cadr(code); + s_goto(OP_EVAL); + + case OP_SET1: /* set! */ + for (x = envir; x != NIL; x = cdr(x)) { + for (y = car(x); y != NIL; y = cdr(y)) + if (caar(y) == code) + break; + if (y != NIL) + break; + } + if (x != NIL) { + cdar(y) = value; + s_return(value); + } else { + Error_1("Unbounded variable", code); + } + + case OP_BEGIN: /* begin */ + if (!ispair(code)) { + s_return(code); + } + if (cdr(code) != NIL) { + s_save(OP_BEGIN, NIL, cdr(code)); + } + code = car(code); + s_goto(OP_EVAL); + + case OP_IF0: /* if */ + s_save(OP_IF1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + + case OP_IF1: /* if */ + if (istrue(value)) + code = car(code); + else + code = cadr(code); /* (if #f 1) ==> () because + * car(NIL) = NIL */ + s_goto(OP_EVAL); + + case OP_LET0: /* let */ + args = NIL; + value = code; + code = issymbol(car(code)) ? cadr(code) : car(code); + s_goto(OP_LET1); + + case OP_LET1: /* let (caluculate parameters) */ + args = cons(value, args); + if (ispair(code)) { /* continue */ + s_save(OP_LET1, args, cdr(code)); + code = cadar(code); + args = NIL; + s_goto(OP_EVAL); + } else { /* end */ + args = reverse(args); + code = car(args); + args = cdr(args); + s_goto(OP_LET2); + } + + case OP_LET2: /* let */ + envir = cons(NIL, envir); + for (x = issymbol(car(code)) ? cadr(code) : car(code), y = args; + y != NIL; x = cdr(x), y = cdr(y)) + car(envir) = cons(cons(caar(x), car(y)), car(envir)); + if (issymbol(car(code))) { /* named let */ + for (x = cadr(code), args = NIL; x != NIL; x = cdr(x)) + args = cons(caar(x), args); + x = mk_closure(cons(reverse(args), cddr(code)), envir); + car(envir) = cons(cons(car(code), x), car(envir)); + code = cddr(code); + args = NIL; + } else { + code = cdr(code); + args = NIL; + } + s_goto(OP_BEGIN); + + case OP_LET0AST: /* let* */ + if (car(code) == NIL) { + envir = cons(NIL, envir); + code = cdr(code); + s_goto(OP_BEGIN); + } + s_save(OP_LET1AST, cdr(code), car(code)); + code = cadaar(code); + s_goto(OP_EVAL); + + case OP_LET1AST: /* let* (make new frame) */ + envir = cons(NIL, envir); + s_goto(OP_LET2AST); + + case OP_LET2AST: /* let* (caluculate parameters) */ + car(envir) = cons(cons(caar(code), value), car(envir)); + code = cdr(code); + if (ispair(code)) { /* continue */ + s_save(OP_LET2AST, args, code); + code = cadar(code); + args = NIL; + s_goto(OP_EVAL); + } else { /* end */ + code = args; + args = NIL; + s_goto(OP_BEGIN); + } + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + } + return T; +} + + +pointer opexe_1(op) +register short op; +{ + register pointer x, y; + + switch (op) { + case OP_LET0REC: /* letrec */ + envir = cons(NIL, envir); + args = NIL; + value = code; + code = car(code); + s_goto(OP_LET1REC); + + case OP_LET1REC: /* letrec (caluculate parameters) */ + args = cons(value, args); + if (ispair(code)) { /* continue */ + s_save(OP_LET1REC, args, cdr(code)); + code = cadar(code); + args = NIL; + s_goto(OP_EVAL); + } else { /* end */ + args = reverse(args); + code = car(args); + args = cdr(args); + s_goto(OP_LET2REC); + } + + case OP_LET2REC: /* letrec */ + for (x = car(code), y = args; y != NIL; x = cdr(x), y = cdr(y)) + car(envir) = cons(cons(caar(x), car(y)), car(envir)); + code = cdr(code); + args = NIL; + s_goto(OP_BEGIN); + + case OP_COND0: /* cond */ + if (!ispair(code)) { + Error_0("Syntax error in cond"); + } + s_save(OP_COND1, NIL, code); + code = caar(code); + s_goto(OP_EVAL); + + case OP_COND1: /* cond */ + if (istrue(value)) { + if ((code = cdar(code)) == NIL) { + s_return(value); + } + s_goto(OP_BEGIN); + } else { + if ((code = cdr(code)) == NIL) { + s_return(NIL); + } else { + s_save(OP_COND1, NIL, code); + code = caar(code); + s_goto(OP_EVAL); + } + } + + case OP_DELAY: /* delay */ + x = mk_closure(cons(NIL, code), envir); + setpromise(x); + s_return(x); + + case OP_AND0: /* and */ + if (code == NIL) { + s_return(T); + } + s_save(OP_AND1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + + case OP_AND1: /* and */ + if (isfalse(value)) { + s_return(value); + } else if (code == NIL) { + s_return(value); + } else { + s_save(OP_AND1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + } + + case OP_OR0: /* or */ + if (code == NIL) { + s_return(F); + } + s_save(OP_OR1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + + case OP_OR1: /* or */ + if (istrue(value)) { + s_return(value); + } else if (code == NIL) { + s_return(value); + } else { + s_save(OP_OR1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + } + + case OP_C0STREAM: /* cons-stream */ + s_save(OP_C1STREAM, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + + case OP_C1STREAM: /* cons-stream */ + args = value; /* save value to register args for gc */ + x = mk_closure(cons(NIL, code), envir); + setpromise(x); + s_return(cons(args, x)); + +#ifdef USE_MACRO + case OP_0MACRO: /* macro */ + x = car(code); + code = cadr(code); + if (!issymbol(x)) { + Error_0("Variable is not symbol"); + } + s_save(OP_1MACRO, NIL, x); + s_goto(OP_EVAL); + + case OP_1MACRO: /* macro */ + type(value) |= T_MACRO; + for (x = car(envir); x != NIL; x = cdr(x)) + if (caar(x) == code) + break; + if (x != NIL) + cdar(x) = value; + else + car(envir) = cons(cons(code, value), car(envir)); + s_return(code); +#endif + + case OP_CASE0: /* case */ + s_save(OP_CASE1, NIL, cdr(code)); + code = car(code); + s_goto(OP_EVAL); + + case OP_CASE1: /* case */ + for (x = code; x != NIL; x = cdr(x)) { + if (!ispair(y = caar(x))) + break; + for ( ; y != NIL; y = cdr(y)) + if (eqv(car(y), value)) + break; + if (y != NIL) + break; + } + if (x != NIL) { + if (ispair(caar(x))) { + code = cdar(x); + s_goto(OP_BEGIN); + } else {/* else */ + s_save(OP_CASE2, NIL, cdar(x)); + code = caar(x); + s_goto(OP_EVAL); + } + } else { + s_return(NIL); + } + + case OP_CASE2: /* case */ + if (istrue(value)) { + s_goto(OP_BEGIN); + } else { + s_return(NIL); + } + case OP_PAPPLY: /* apply */ + code = car(args); + args = cadr(args); + s_goto(OP_APPLY); + + case OP_PEVAL: /* eval */ + code = car(args); + args = NIL; + s_goto(OP_EVAL); + + case OP_CONTINUATION: /* call-with-current-continuation */ + code = car(args); + args = cons(mk_continuation(dump), NIL); + s_goto(OP_APPLY); + + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + } + return T; +} + + +pointer opexe_2(op) +register short op; +{ + register pointer x, y; + register long v; + + switch (op) { + case OP_ADD: /* + */ + for (x = args, v = 0; x != NIL; x = cdr(x)) + v += ivalue(car(x)); + s_return(mk_number(v)); + + case OP_SUB: /* - */ + for (x = cdr(args), v = ivalue(car(args)); x != NIL; x = cdr(x)) + v -= ivalue(car(x)); + s_return(mk_number(v)); + + case OP_MUL: /* * */ + for (x = args, v = 1; x != NIL; x = cdr(x)) + v *= ivalue(car(x)); + s_return(mk_number(v)); + + case OP_DIV: /* / */ + for (x = cdr(args), v = ivalue(car(args)); x != NIL; x = cdr(x)) { + if (ivalue(car(x)) != 0) + v /= ivalue(car(x)); + else { + Error_0("Divided by zero"); + } + } + s_return(mk_number(v)); + + case OP_REM: /* remainder */ + for (x = cdr(args), v = ivalue(car(args)); x != NIL; x = cdr(x)) { + if (ivalue(car(x)) != 0) + v %= ivalue(car(x)); + else { + Error_0("Divided by zero"); + } + } + s_return(mk_number(v)); + + case OP_CAR: /* car */ + if (ispair(car(args))) { + s_return(caar(args)); + } else { + Error_0("Unable to car for non-cons cell"); + } + + case OP_CDR: /* cdr */ + if (ispair(car(args))) { + s_return(cdar(args)); + } else { + Error_0("Unable to cdr for non-cons cell"); + } + + case OP_CONS: /* cons */ + cdr(args) = cadr(args); + s_return(args); + + case OP_SETCAR: /* set-car! */ + if (ispair(car(args))) { + caar(args) = cadr(args); + s_return(car(args)); + } else { + Error_0("Unable to set-car! for non-cons cell"); + } + + case OP_SETCDR: /* set-cdr! */ + if (ispair(car(args))) { + cdar(args) = cadr(args); + s_return(car(args)); + } else { + Error_0("Unable to set-cdr! for non-cons cell"); + } + + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + } + return T; +} + + +pointer opexe_3(op) +register short op; +{ + register pointer x, y; + + switch (op) { + case OP_NOT: /* not */ + s_retbool(isfalse(car(args))); + case OP_BOOL: /* boolean? */ + s_retbool(car(args) == F || car(args) == T); + case OP_NULL: /* null? */ + s_retbool(car(args) == NIL); + case OP_ZEROP: /* zero? */ + s_retbool(ivalue(car(args)) == 0); + case OP_POSP: /* positive? */ + s_retbool(ivalue(car(args)) > 0); + case OP_NEGP: /* negative? */ + s_retbool(ivalue(car(args)) < 0); + case OP_NEQ: /* = */ + s_retbool(ivalue(car(args)) == ivalue(cadr(args))); + case OP_LESS: /* < */ + s_retbool(ivalue(car(args)) < ivalue(cadr(args))); + case OP_GRE: /* > */ + s_retbool(ivalue(car(args)) > ivalue(cadr(args))); + case OP_LEQ: /* <= */ + s_retbool(ivalue(car(args)) <= ivalue(cadr(args))); + case OP_GEQ: /* >= */ + s_retbool(ivalue(car(args)) >= ivalue(cadr(args))); + case OP_SYMBOL: /* symbol? */ + s_retbool(issymbol(car(args))); + case OP_NUMBER: /* number? */ + s_retbool(isnumber(car(args))); + case OP_STRING: /* string? */ + s_retbool(isstring(car(args))); + case OP_PROC: /* procedure? */ + /*-- + * continuation should be procedure by the example + * (call-with-current-continuation procedure?) ==> #t + * in R^3 report sec. 6.9 + */ + s_retbool(isproc(car(args)) || isclosure(car(args)) + || iscontinuation(car(args))); + case OP_PAIR: /* pair? */ + s_retbool(ispair(car(args))); + case OP_EQ: /* eq? */ + s_retbool(car(args) == cadr(args)); + case OP_EQV: /* eqv? */ + s_retbool(eqv(car(args), cadr(args))); + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + } + return T; +} + + +pointer opexe_4(op) +register short op; +{ + register pointer x, y; + + switch (op) { + case OP_FORCE: /* force */ + code = car(args); + if (ispromise(code)) { + args = NIL; + s_goto(OP_APPLY); + } else { + s_return(code); + } + + case OP_WRITE: /* write */ + print_flag = 1; + args = car(args); + s_goto(OP_P0LIST); + + case OP_DISPLAY: /* display */ + print_flag = 0; + args = car(args); + s_goto(OP_P0LIST); + + case OP_NEWLINE: /* newline */ + fprintf(outfp, "\n"); + s_return(T); + + case OP_ERR0: /* error */ + if (!isstring(car(args))) { + Error_0("error -- first argument must be string"); + } + tmpfp = outfp; + outfp = stderr; + fprintf(outfp, "Error: "); + fprintf(outfp, "%s", strvalue(car(args))); + args = cdr(args); + s_goto(OP_ERR1); + + case OP_ERR1: /* error */ + fprintf(outfp, " "); + if (args != NIL) { + s_save(OP_ERR1, cdr(args), NIL); + args = car(args); + print_flag = 1; + s_goto(OP_P0LIST); + } else { + fprintf(outfp, "\n"); + flushinput(); + outfp = tmpfp; + s_goto(OP_T0LVL); + } + + case OP_REVERSE: /* reverse */ + s_return(reverse(car(args))); + + case OP_APPEND: /* append */ + s_return(append(car(args), cadr(args))); + + case OP_PUT: /* put */ + if (!hasprop(car(args)) || !hasprop(cadr(args))) { + Error_0("Illegal use of put"); + } + for (x = symprop(car(args)), y = cadr(args); x != NIL; x = cdr(x)) + if (caar(x) == y) + break; + if (x != NIL) + cdar(x) = caddr(args); + else + symprop(car(args)) = cons(cons(y, caddr(args)), + symprop(car(args))); + s_return(T); + + case OP_GET: /* get */ + if (!hasprop(car(args)) || !hasprop(cadr(args))) { + Error_0("Illegal use of get"); + } + for (x = symprop(car(args)), y = cadr(args); x != NIL; x = cdr(x)) + if (caar(x) == y) + break; + if (x != NIL) { + s_return(cdar(x)); + } else { + s_return(NIL); + } + + case OP_QUIT: /* quit */ + return (NIL); + + case OP_GC: /* gc */ + gc(NIL, NIL); + s_return(T); + + case OP_GCVERB: /* gc-verbose */ + { int was = gc_verbose; + + gc_verbose = (car(args) != F); + s_retbool(was); + } + + case OP_NEWSEGMENT: /* new-segment */ + if (!isnumber(car(args))) { + Error_0("new-segment -- argument must be number"); + } + fprintf(outfp, "allocate %d new segments\n", + alloc_cellseg((int) ivalue(car(args)))); + s_return(T); + } +} + + +pointer opexe_5(op) +register short op; +{ + register pointer x, y; + + switch (op) { + /* ========== reading part ========== */ + case OP_RDSEXPR: + switch (tok) { + case TOK_COMMENT: + while (inchar() != '\n') + ; + tok = token(); + s_goto(OP_RDSEXPR); + case TOK_LPAREN: + tok = token(); + if (tok == TOK_RPAREN) { + s_return(NIL); + } else if (tok == TOK_DOT) { + Error_0("syntax error -- illegal dot expression"); + } else { + s_save(OP_RDLIST, NIL, NIL); + s_goto(OP_RDSEXPR); + } + case TOK_QUOTE: + s_save(OP_RDQUOTE, NIL, NIL); + tok = token(); + s_goto(OP_RDSEXPR); +#ifdef USE_QQUOTE + case TOK_BQUOTE: + s_save(OP_RDQQUOTE, NIL, NIL); + tok = token(); + s_goto(OP_RDSEXPR); + case TOK_COMMA: + s_save(OP_RDUNQUOTE, NIL, NIL); + tok = token(); + s_goto(OP_RDSEXPR); + case TOK_ATMARK: + s_save(OP_RDUQTSP, NIL, NIL); + tok = token(); + s_goto(OP_RDSEXPR); +#endif + case TOK_ATOM: + s_return(mk_atom(readstr("();\t\n "))); + case TOK_DQUOTE: + s_return(mk_string(readstrexp())); + case TOK_SHARP: + if ((x = mk_const(readstr("();\t\n "))) == NIL) { + Error_0("Undefined sharp expression"); + } else { + s_return(x); + } + default: + Error_0("syntax error -- illegal token"); + } + break; + + case OP_RDLIST: + args = cons(value, args); + tok = token(); + if (tok == TOK_COMMENT) { + while (inchar() != '\n') + ; + tok = token(); + } + if (tok == TOK_RPAREN) { + s_return(non_alloc_rev(NIL, args)); + } else if (tok == TOK_DOT) { + s_save(OP_RDDOT, args, NIL); + tok = token(); + s_goto(OP_RDSEXPR); + } else { + s_save(OP_RDLIST, args, NIL);; + s_goto(OP_RDSEXPR); + } + + case OP_RDDOT: + if (token() != TOK_RPAREN) { + Error_0("syntax error -- illegal dot expression"); + } else { + s_return(non_alloc_rev(value, args)); + } + + case OP_RDQUOTE: + s_return(cons(QUOTE, cons(value, NIL))); + +#ifdef USE_QQUOTE + case OP_RDQQUOTE: + s_return(cons(QQUOTE, cons(value, NIL))); + + case OP_RDUNQUOTE: + s_return(cons(UNQUOTE, cons(value, NIL))); + + case OP_RDUQTSP: + s_return(cons(UNQUOTESP, cons(value, NIL))); +#endif + + /* ========== printing part ========== */ + case OP_P0LIST: + if (!ispair(args)) { + printatom(args, print_flag); + s_return(T); + } else if (car(args) == QUOTE && ok_abbrev(cdr(args))) { + fprintf(outfp, "'"); + args = cadr(args); + s_goto(OP_P0LIST); + } else if (car(args) == QQUOTE && ok_abbrev(cdr(args))) { + fprintf(outfp, "`"); + args = cadr(args); + s_goto(OP_P0LIST); + } else if (car(args) == UNQUOTE && ok_abbrev(cdr(args))) { + fprintf(outfp, ","); + args = cadr(args); + s_goto(OP_P0LIST); + } else if (car(args) == UNQUOTESP && ok_abbrev(cdr(args))) { + fprintf(outfp, ",@"); + args = cadr(args); + s_goto(OP_P0LIST); + } else { + fprintf(outfp, "("); + s_save(OP_P1LIST, cdr(args), NIL); + args = car(args); + s_goto(OP_P0LIST); + } + + case OP_P1LIST: + if (ispair(args)) { + s_save(OP_P1LIST, cdr(args), NIL); + fprintf(outfp, " "); + args = car(args); + s_goto(OP_P0LIST); + } else { + if (args != NIL) { + fprintf(outfp, " . "); + printatom(args, print_flag); + } + fprintf(outfp, ")"); + s_return(T); + } + + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + + } + return T; +} + + +pointer opexe_6(op) +register short op; +{ + register pointer x, y; + register long v; + static long w; + char buffer[32]; + + switch (op) { + case OP_LIST_LENGTH: /* list-length */ /* a.k */ + for (x = car(args), v = 0; ispair(x); x = cdr(x)) + ++v; + s_return(mk_number(v)); + + case OP_ASSQ: /* assq */ /* a.k */ + x = car(args); + for (y = cadr(args); ispair(y); y = cdr(y)) { + if (!ispair(car(y))) { + Error_0("Unable to handle non pair element"); + } + if (x == caar(y)) + break; + } + if (ispair(y)) { + s_return(car(y)); + } else { + s_return(F); + } + + case OP_PRINT_WIDTH: /* print-width */ /* a.k */ + w = 0; + args = car(args); + print_flag = -1; + s_goto(OP_P0_WIDTH); + + case OP_P0_WIDTH: + if (!ispair(args)) { + w += printatom(args, print_flag); + s_return(mk_number(w)); + } else if (car(args) == QUOTE + && ok_abbrev(cdr(args))) { + ++w; + args = cadr(args); + s_goto(OP_P0_WIDTH); + } else if (car(args) == QQUOTE + && ok_abbrev(cdr(args))) { + ++w; + args = cadr(args); + s_goto(OP_P0_WIDTH); + } else if (car(args) == UNQUOTE + && ok_abbrev(cdr(args))) { + ++w; + args = cadr(args); + s_goto(OP_P0_WIDTH); + } else if (car(args) == UNQUOTESP + && ok_abbrev(cdr(args))) { + w += 2; + args = cadr(args); + s_goto(OP_P0_WIDTH); + } else { + ++w; + s_save(OP_P1_WIDTH, cdr(args), NIL); + args = car(args); + s_goto(OP_P0_WIDTH); + } + + case OP_P1_WIDTH: + if (ispair(args)) { + s_save(OP_P1_WIDTH, cdr(args), NIL); + ++w; + args = car(args); + s_goto(OP_P0_WIDTH); + } else { + if (args != NIL) + w += 3 + printatom(args, print_flag); + ++w; + s_return(mk_number(w)); + } + + case OP_GET_CLOSURE: /* get-closure-code */ /* a.k */ + args = car(args); + if (args == NIL) { + s_return(F); + } else if (isclosure(args)) { + s_return(cons(LAMBDA, closure_code(value))); +#ifdef USE_MACRO + } else if (ismacro(args)) { + s_return(cons(LAMBDA, closure_code(value))); +#endif + } else { + s_return(F); + } + case OP_CLOSUREP: /* closure? */ + /* + * Note, macro object is also a closure. + * Therefore, (closure? <#MACRO>) ==> #t + */ + if (car(args) == NIL) { + s_return(F); + } + s_retbool(isclosure(car(args))); +#ifdef USE_MACRO + case OP_MACROP: /* macro? */ + if (car(args) == NIL) { + s_return(F); + } + s_retbool(ismacro(car(args))); +#endif + default: + sprintf(strbuff, "%d is illegal operator", operator); + Error_0(strbuff); + } + return T; /* NOTREACHED */ +} + + + + +pointer (*dispatch_table[])() = { + opexe_0, /* OP_LOAD = 0, */ + opexe_0, /* OP_T0LVL, */ + opexe_0, /* OP_T1LVL, */ + opexe_0, /* OP_READ, */ + opexe_0, /* OP_VALUEPRINT, */ + opexe_0, /* OP_EVAL, */ + opexe_0, /* OP_E0ARGS, */ + opexe_0, /* OP_E1ARGS, */ + opexe_0, /* OP_APPLY, */ + opexe_0, /* OP_DOMACRO, */ + + opexe_0, /* OP_LAMBDA, */ + opexe_0, /* OP_QUOTE, */ + opexe_0, /* OP_DEF0, */ + opexe_0, /* OP_DEF1, */ + opexe_0, /* OP_BEGIN, */ + opexe_0, /* OP_IF0, */ + opexe_0, /* OP_IF1, */ + opexe_0, /* OP_SET0, */ + opexe_0, /* OP_SET1, */ + opexe_0, /* OP_LET0, */ + opexe_0, /* OP_LET1, */ + opexe_0, /* OP_LET2, */ + opexe_0, /* OP_LET0AST, */ + opexe_0, /* OP_LET1AST, */ + opexe_0, /* OP_LET2AST, */ + + opexe_1, /* OP_LET0REC, */ + opexe_1, /* OP_LET1REC, */ + opexe_1, /* OP_LETREC2, */ + opexe_1, /* OP_COND0, */ + opexe_1, /* OP_COND1, */ + opexe_1, /* OP_DELAY, */ + opexe_1, /* OP_AND0, */ + opexe_1, /* OP_AND1, */ + opexe_1, /* OP_OR0, */ + opexe_1, /* OP_OR1, */ + opexe_1, /* OP_C0STREAM, */ + opexe_1, /* OP_C1STREAM, */ + opexe_1, /* OP_0MACRO, */ + opexe_1, /* OP_1MACRO, */ + opexe_1, /* OP_CASE0, */ + opexe_1, /* OP_CASE1, */ + opexe_1, /* OP_CASE2, */ + + opexe_1, /* OP_PEVAL, */ + opexe_1, /* OP_PAPPLY, */ + opexe_1, /* OP_CONTINUATION, */ + + opexe_2, /* OP_ADD, */ + opexe_2, /* OP_SUB, */ + opexe_2, /* OP_MUL, */ + opexe_2, /* OP_DIV, */ + opexe_2, /* OP_REM, */ + opexe_2, /* OP_CAR, */ + opexe_2, /* OP_CDR, */ + opexe_2, /* OP_CONS, */ + opexe_2, /* OP_SETCAR, */ + opexe_2, /* OP_SETCDR, */ + + opexe_3, /* OP_NOT, */ + opexe_3, /* OP_BOOL, */ + opexe_3, /* OP_NULL, */ + opexe_3, /* OP_ZEROP, */ + opexe_3, /* OP_POSP, */ + opexe_3, /* OP_NEGP, */ + opexe_3, /* OP_NEQ, */ + opexe_3, /* OP_LESS, */ + opexe_3, /* OP_GRE, */ + opexe_3, /* OP_LEQ, */ + opexe_3, /* OP_GEQ, */ + opexe_3, /* OP_SYMBOL, */ + opexe_3, /* OP_NUMBER, */ + opexe_3, /* OP_STRING, */ + opexe_3, /* OP_PROC, */ + opexe_3, /* OP_PAIR, */ + opexe_3, /* OP_EQ, */ + opexe_3, /* OP_EQV, */ + + opexe_4, /* OP_FORCE, */ + opexe_4, /* OP_WRITE, */ + opexe_4, /* OP_DISPLAY, */ + opexe_4, /* OP_NEWLINE, */ + opexe_4, /* OP_ERR0, */ + opexe_4, /* OP_ERR1, */ + opexe_4, /* OP_REVERSE, */ + opexe_4, /* OP_APPEND, */ + opexe_4, /* OP_PUT, */ + opexe_4, /* OP_GET, */ + opexe_4, /* OP_QUIT, */ + opexe_4, /* OP_GC, */ + opexe_4, /* OP_GCVERB, */ + opexe_4, /* OP_NEWSEGMENT, */ + + opexe_5, /* OP_RDSEXPR, */ + opexe_5, /* OP_RDLIST, */ + opexe_5, /* OP_RDDOT, */ + opexe_5, /* OP_RDQUOTE, */ + opexe_5, /* OP_RDQQUOTE, */ + opexe_5, /* OP_RDUNQUOTE, */ + opexe_5, /* OP_RDUQTSP, */ + opexe_5, /* OP_P0LIST, */ + opexe_5, /* OP_P1LIST, */ + + opexe_6, /* OP_LIST_LENGTH, */ + opexe_6, /* OP_ASSQ, */ + opexe_6, /* OP_PRINT_WIDTH, */ + opexe_6, /* OP_P0_WIDTH, */ + opexe_6, /* OP_P1_WIDTH, */ + opexe_6, /* OP_GET_CLOSURE, */ + opexe_6, /* OP_CLOSUREP, */ +#ifdef USE_MACRO + opexe_6, /* OP_MACROP, */ +#endif + +}; + + +/* kernel of this intepreter */ +pointer Eval_Cycle(op) +register short op; +{ + + operator = op; + for (;;) + if ((*dispatch_table[operator])(operator) == NIL) + return NIL; +} + +/* ========== Initialization of internal keywords ========== */ + +mk_syntax(op, name) +unsigned short op; +char *name; +{ + pointer x; + + x = cons(mk_string(name), NIL); + type(x) = (T_SYNTAX | T_SYMBOL); + syntaxnum(x) = op; + oblist = cons(x, oblist); +} + +mk_proc(op, name) +unsigned short op; +char *name; +{ + pointer x, y; + + x = mk_symbol(name); + y = get_cell(NIL, NIL); + type(y) = (T_PROC | T_ATOM); + ivalue(y) = (long) op; + car(global_env) = cons(cons(x, y), car(global_env)); +} + + +init_vars_global() +{ + pointer x; + + /* init input/output file */ + infp = stdin; + outfp = stdout; + /* init NIL */ + type(NIL) = (T_ATOM | MARK); + car(NIL) = cdr(NIL) = NIL; + /* init T */ + type(T) = (T_ATOM | MARK); + car(T) = cdr(T) = T; + /* init F */ + type(F) = (T_ATOM | MARK); + car(F) = cdr(F) = F; + /* init global_env */ + global_env = cons(NIL, NIL); + /* init else */ + x = mk_symbol("else"); + car(global_env) = cons(cons(x, T), car(global_env)); +} + + +init_syntax() +{ + /* init syntax */ + mk_syntax(OP_LAMBDA, "lambda"); + mk_syntax(OP_QUOTE, "quote"); + mk_syntax(OP_DEF0, "define"); + mk_syntax(OP_IF0, "if"); + mk_syntax(OP_BEGIN, "begin"); + mk_syntax(OP_SET0, "set!"); + mk_syntax(OP_LET0, "let"); + mk_syntax(OP_LET0AST, "let*"); + mk_syntax(OP_LET0REC, "letrec"); + mk_syntax(OP_COND0, "cond"); + mk_syntax(OP_DELAY, "delay"); + mk_syntax(OP_AND0, "and"); + mk_syntax(OP_OR0, "or"); + mk_syntax(OP_C0STREAM, "cons-stream"); +#ifdef USE_MACRO + mk_syntax(OP_0MACRO, "macro"); +#endif + mk_syntax(OP_CASE0, "case"); +} + + +init_procs() +{ + /* init procedure */ + mk_proc(OP_PEVAL, "eval"); + mk_proc(OP_PAPPLY, "apply"); + mk_proc(OP_CONTINUATION, "call-with-current-continuation"); + mk_proc(OP_FORCE, "force"); + mk_proc(OP_CAR, "car"); + mk_proc(OP_CDR, "cdr"); + mk_proc(OP_CONS, "cons"); + mk_proc(OP_SETCAR, "set-car!"); + mk_proc(OP_SETCDR, "set-cdr!"); + mk_proc(OP_ADD, "+"); + mk_proc(OP_SUB, "-"); + mk_proc(OP_MUL, "*"); + mk_proc(OP_DIV, "/"); + mk_proc(OP_REM, "remainder"); + mk_proc(OP_NOT, "not"); + mk_proc(OP_BOOL, "boolean?"); + mk_proc(OP_SYMBOL, "symbol?"); + mk_proc(OP_NUMBER, "number?"); + mk_proc(OP_STRING, "string?"); + mk_proc(OP_PROC, "procedure?"); + mk_proc(OP_PAIR, "pair?"); + mk_proc(OP_EQV, "eqv?"); + mk_proc(OP_EQ, "eq?"); + mk_proc(OP_NULL, "null?"); + mk_proc(OP_ZEROP, "zero?"); + mk_proc(OP_POSP, "positive?"); + mk_proc(OP_NEGP, "negative?"); + mk_proc(OP_NEQ, "="); + mk_proc(OP_LESS, "<"); + mk_proc(OP_GRE, ">"); + mk_proc(OP_LEQ, "<="); + mk_proc(OP_GEQ, ">="); + mk_proc(OP_READ, "read"); + mk_proc(OP_WRITE, "write"); + mk_proc(OP_DISPLAY, "display"); + mk_proc(OP_NEWLINE, "newline"); + mk_proc(OP_LOAD, "load"); + mk_proc(OP_ERR0, "error"); + mk_proc(OP_REVERSE, "reverse"); + mk_proc(OP_APPEND, "append"); + mk_proc(OP_PUT, "put"); + mk_proc(OP_GET, "get"); + mk_proc(OP_GC, "gc"); + mk_proc(OP_GCVERB, "gc-verbose"); + mk_proc(OP_NEWSEGMENT, "new-segment"); + mk_proc(OP_LIST_LENGTH, "list-length"); /* a.k */ + mk_proc(OP_ASSQ, "assq"); /* a.k */ + mk_proc(OP_PRINT_WIDTH, "print-width"); /* a.k */ + mk_proc(OP_GET_CLOSURE, "get-closure-code"); /* a.k */ + mk_proc(OP_CLOSUREP, "closure?"); /* a.k */ +#ifdef USE_MACRO + mk_proc(OP_MACROP, "macro?"); /* a.k */ +#endif + mk_proc(OP_QUIT, "quit"); +} + + +/* initialize several globals */ +init_globals() +{ + init_vars_global(); + init_syntax(); + init_procs(); + /* intialization of global pointers to special symbols */ + LAMBDA = mk_symbol("lambda"); + QUOTE = mk_symbol("quote"); +#ifdef USE_QQUOTE + QQUOTE = mk_symbol("quasiquote"); + UNQUOTE = mk_symbol("unquote"); + UNQUOTESP = mk_symbol("unquote-splicing"); +#endif + +} + +/* ========== Error ========== */ + +FatalError(fmt, a, b, c) +char *fmt, *a, *b, *c; +{ + fprintf(stderr, "Fatal error: "); + fprintf(stderr, fmt, a, b, c); + fprintf(stderr, "\n"); + exit(1); +} + +#ifdef USE_SETJMP +Error(fmt, a, b, c) +char *fmt, *a, *b, *c; +{ + fprintf(stderr, "Error: "); + fprintf(stderr, fmt, a, b, c); + fprintf(stderr, "\n"); + flushinput(); + longjmp(error_jmp, OP_T0LVL); +} + +#endif + +/* ========== Main ========== */ + +main() +{ + short op = (short) OP_LOAD; + + printf(banner); + init_scheme(); + args = cons(mk_string(InitFile), NIL); +#ifdef USE_SETJMP + op = setjmp(error_jmp); +#endif + Eval_Cycle(op); +} + diff --git a/tests.scm b/tests.scm new file mode 100644 index 0000000..717a0be --- /dev/null +++ b/tests.scm @@ -0,0 +1,57 @@ +;;; Copyright (C) Peter McGoron 2024 +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, version 3 of the License. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + +;;; Test harness. +;;; Each test is a name and a function with no arguments. The function +;;; returns #t for success, #f for undefined failure, and a string for +;;; known failure. +;;; +;;; TODO: Return test results as a list. + +(define verbose? #t) + +(define run-tests + (lambda (tests) + (letrec + ((run-test + (lambda (name test) + (if verbose? + (begin + (display "running: ") + (display name) + (newline)) + #f) + (let ((result (test))) + (if (not result) + "undefined failure" + result)))) + (loop + (lambda (rest) + (if (null? rest) + #t + (let ((descr (car rest))) + (let ((return (run-test + (car descr) + (cdr descr)))) + (if (not (eq? return #t)) + return + (loop (cdr rest))))))))) + (loop tests)))) + +(load "doubly-linked-list.scm") +(display "running doubly linked list tests") +(newline) +(if (run-tests %dl:tests) + (display "doubly linked list tests: passed") + (display "doubly linked list tests: failed")) +(newline)