init commit
This commit is contained in:
commit
6300e42bb2
28 files changed
+104005
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
/build/
|
||||||
|
/config.h
|
||||||
|
/config.log
|
||||||
|
/config.mk
|
||||||
@@ -0,0 +1,235 @@
|
|||||||
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
"Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
Ensifer
|
||||||
|
Copyright (C) 2026 divorce
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see <http://www.gnu.org/licenses/>.
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
.DEFAULT_GOAL := all
|
||||||
|
-include config.mk
|
||||||
|
|
||||||
|
ifeq ($(wildcard config.mk),)
|
||||||
|
$(error Run ./configure first)
|
||||||
|
endif
|
||||||
|
|
||||||
|
PREFIX ?= /usr/local
|
||||||
|
BINDIR ?= $(PREFIX)/bin
|
||||||
|
LIBDIR ?= $(PREFIX)/lib
|
||||||
|
INCLUDEDIR ?= $(PREFIX)/include/ensifer
|
||||||
|
DATADIR ?= $(PREFIX)/share/ensifer
|
||||||
|
DOCDIR ?= $(PREFIX)/share/doc/ensifer
|
||||||
|
MANDIR ?= $(PREFIX)/share/man
|
||||||
|
|
||||||
|
BUILD = build
|
||||||
|
OBJDIR = $(BUILD)/obj
|
||||||
|
LIBDIR_BUILD = $(BUILD)/lib
|
||||||
|
BINDIR_BUILD = $(BUILD)/bin
|
||||||
|
CORE_OBJS = $(OBJDIR)/enfilade.o $(OBJDIR)/tumbler.o
|
||||||
|
S7_OBJS = $(OBJDIR)/enfilade_s7.o $(CORE_OBJS)
|
||||||
|
|
||||||
|
.PHONY: all clean distclean check test install uninstall version
|
||||||
|
|
||||||
|
version:
|
||||||
|
@printf 'ensifer version %s\n' '$(VERSION)'
|
||||||
|
|
||||||
|
all: $(LIBDIR_BUILD)/libensifer.a $(LIBDIR_BUILD)/enfilade.so $(BINDIR_BUILD)/logan
|
||||||
|
|
||||||
|
$(OBJDIR) $(LIBDIR_BUILD) $(BINDIR_BUILD):
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: src/%.c | $(OBJDIR)
|
||||||
|
$(CC) $(CPPFLAGS) -Iinc $(CFLAGS) -fPIC -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJDIR)/s7.o: src/s7.c inc/s7.h | $(OBJDIR)
|
||||||
|
$(CC) $(CPPFLAGS) $(S7_CPPFLAGS) -Iinc $(CFLAGS) -w -c $< -o $@
|
||||||
|
|
||||||
|
$(LIBDIR_BUILD)/libensifer.a: $(CORE_OBJS) | $(LIBDIR_BUILD)
|
||||||
|
$(AR) rcs $@ $(CORE_OBJS)
|
||||||
|
|
||||||
|
$(LIBDIR_BUILD)/enfilade.so: $(S7_OBJS) | $(LIBDIR_BUILD)
|
||||||
|
$(CC) -shared $(LDFLAGS) -o $@ $(S7_OBJS) -lm
|
||||||
|
|
||||||
|
$(BINDIR_BUILD)/logan: src/logan.c $(OBJDIR)/s7.o $(S7_OBJS) inc/enfilade_s7.h | $(BINDIR_BUILD)
|
||||||
|
$(CC) $(CPPFLAGS) $(S7_CPPFLAGS) -Iinc $(CFLAGS) $(LDFLAGS) -o $@ \
|
||||||
|
src/logan.c $(OBJDIR)/s7.o $(S7_OBJS) -lm $(S7_LIBS)
|
||||||
|
|
||||||
|
$(BUILD)/tumbler-test: test/tumbler-test.c $(OBJDIR)/tumbler.o | $(BUILD)
|
||||||
|
$(CC) $(CPPFLAGS) -Iinc $(CFLAGS) $(LDFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
$(BUILD)/enfilade-test: test/enfilade-test.c $(CORE_OBJS) | $(BUILD)
|
||||||
|
$(CC) $(CPPFLAGS) -Iinc $(CFLAGS) $(LDFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
$(BUILD)/enfload: test/enfload.c $(OBJDIR)/s7.o $(S7_OBJS) inc/enfilade_s7.h | $(BUILD)
|
||||||
|
$(CC) $(CPPFLAGS) $(S7_CPPFLAGS) -Iinc $(CFLAGS) $(LDFLAGS) -o $@ \
|
||||||
|
test/enfload.c $(OBJDIR)/s7.o $(S7_OBJS) -lm $(S7_LIBS)
|
||||||
|
|
||||||
|
$(BUILD):
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
check test: $(BUILD)/tumbler-test $(BUILD)/enfilade-test $(BUILD)/enfload
|
||||||
|
$(BUILD)/tumbler-test
|
||||||
|
$(BUILD)/enfilade-test
|
||||||
|
$(BUILD)/enfload
|
||||||
|
|
||||||
|
install: all
|
||||||
|
$(INSTALL) -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) \
|
||||||
|
$(DESTDIR)$(DATADIR)/scheme $(DESTDIR)$(DOCDIR) $(DESTDIR)$(MANDIR)/man3 $(DESTDIR)$(MANDIR)/man7
|
||||||
|
$(INSTALL) -m 755 $(BINDIR_BUILD)/logan $(DESTDIR)$(BINDIR)/logan
|
||||||
|
$(INSTALL) -m 644 $(LIBDIR_BUILD)/libensifer.a $(DESTDIR)$(LIBDIR)/libensifer.a
|
||||||
|
$(INSTALL) -m 755 $(LIBDIR_BUILD)/enfilade.so $(DESTDIR)$(LIBDIR)/enfilade.so
|
||||||
|
$(INSTALL) -m 644 inc/enfilade.h inc/tumbler.h inc/s7.h inc/enfilade_s7.h $(DESTDIR)$(INCLUDEDIR)/
|
||||||
|
$(INSTALL) -m 644 scheme/enf.scm scheme/cload.scm scheme/libc.scm scheme/libdl.scm $(DESTDIR)$(DATADIR)/scheme/
|
||||||
|
$(INSTALL) -m 644 README.md doc/USAGE $(DESTDIR)$(DOCDIR)/
|
||||||
|
$(INSTALL) -m 644 doc/enfilade.3 doc/tumbler.3 $(DESTDIR)$(MANDIR)/man3/
|
||||||
|
$(INSTALL) -m 644 doc/enfilade-s7.7 doc/ensifer.7 doc/tumbler-s7.7 $(DESTDIR)$(MANDIR)/man7/
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm -f $(DESTDIR)$(BINDIR)/logan $(DESTDIR)$(LIBDIR)/libensifer.a $(DESTDIR)$(LIBDIR)/enfilade.so
|
||||||
|
rm -f $(DESTDIR)$(INCLUDEDIR)/enfilade.h $(DESTDIR)$(INCLUDEDIR)/tumbler.h $(DESTDIR)$(INCLUDEDIR)/s7.h $(DESTDIR)$(INCLUDEDIR)/enfilade_s7.h
|
||||||
|
rm -f $(DESTDIR)$(MANDIR)/man3/enfilade.3 $(DESTDIR)$(MANDIR)/man3/tumbler.3
|
||||||
|
rm -f $(DESTDIR)$(MANDIR)/man7/enfilade-s7.7 $(DESTDIR)$(MANDIR)/man7/ensifer.7 $(DESTDIR)$(MANDIR)/man7/tumbler-s7.7
|
||||||
|
rm -rf $(DESTDIR)$(DATADIR) $(DESTDIR)$(DOCDIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD)
|
||||||
|
|
||||||
|
distclean: clean
|
||||||
|
rm -f config.mk config.h config.log
|
||||||
@@ -0,0 +1,298 @@
|
|||||||
|
ENSIFER 0.1.20
|
||||||
|
============
|
||||||
|
|
||||||
|
A small Model-T-style enfilade in C with an S7 Scheme FFI.
|
||||||
|
|
||||||
|
The point of this package is to make the core mechanism usable without making
|
||||||
|
the reader learn the whole Xanadu/Green vocabulary first.
|
||||||
|
|
||||||
|
Start with [`doc/USAGE`](doc/USAGE) if you are new to the code. Read
|
||||||
|
[`doc/ensifer.7`](doc/ensifer.7) for the conceptual overview,
|
||||||
|
[`doc/enfilade.3`](doc/enfilade.3) for the C API, and
|
||||||
|
[`doc/enfilade-s7.7`](doc/enfilade-s7.7) for Scheme. The tumbler primitive is
|
||||||
|
documented separately in [`doc/tumbler.3`](doc/tumbler.3) and
|
||||||
|
[`doc/tumbler-s7.7`](doc/tumbler-s7.7).
|
||||||
|
|
||||||
|
THE IDEA
|
||||||
|
--------
|
||||||
|
|
||||||
|
An enfilade is a tree indexed by tumbler fields.
|
||||||
|
|
||||||
|
A tumbler is written as a dotted sequence of unsigned integers:
|
||||||
|
|
||||||
|
1.2.3
|
||||||
|
|
||||||
|
Putting:
|
||||||
|
|
||||||
|
1.2.3 -> datum
|
||||||
|
|
||||||
|
creates the path automatically:
|
||||||
|
|
||||||
|
root
|
||||||
|
|
|
||||||
|
+-- 1
|
||||||
|
|
|
||||||
|
+-- 2
|
||||||
|
|
|
||||||
|
+-- 3 -> datum
|
||||||
|
|
||||||
|
A key may have both a datum and a child enfilade. Lookup returns the datum at
|
||||||
|
that key first, then the contents of the child.
|
||||||
|
|
||||||
|
That is the whole basic mechanism.
|
||||||
|
|
||||||
|
QUICK START
|
||||||
|
-----------
|
||||||
|
|
||||||
|
Configure and build the library, S7 module, and bundled REPL:
|
||||||
|
|
||||||
|
./configure
|
||||||
|
make
|
||||||
|
|
||||||
|
Start the REPL; the enfilade functions are initialized automatically:
|
||||||
|
|
||||||
|
./build/bin/logan
|
||||||
|
|
||||||
|
Install under `/usr/local` (or set another prefix with
|
||||||
|
`./configure --prefix=/path`):
|
||||||
|
|
||||||
|
make install
|
||||||
|
|
||||||
|
Stage an install for packaging or inspection with `DESTDIR`:
|
||||||
|
|
||||||
|
make install DESTDIR=/tmp/ensifer-stage
|
||||||
|
|
||||||
|
Run the regression suite with `make check`. `make clean` removes generated
|
||||||
|
build files; `make distclean` also removes configure output.
|
||||||
|
|
||||||
|
To load the shared module into another S7 host:
|
||||||
|
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
RANGE
|
||||||
|
-----
|
||||||
|
-----
|
||||||
|
|
||||||
|
`enfilade-get-range` is deliberately unchanged from the existing interface.
|
||||||
|
It ranges over the numeric keys at the current enfilade level:
|
||||||
|
|
||||||
|
(enfilade-get-range e 1 3)
|
||||||
|
|
||||||
|
The bounds are inclusive. A negative end means "through the end".
|
||||||
|
|
||||||
|
This is not a dotted-tumbler interval expression.
|
||||||
|
|
||||||
|
TUMBLERS
|
||||||
|
--------
|
||||||
|
|
||||||
|
The C layer now has a small standalone tumbler module.
|
||||||
|
|
||||||
|
The public primitive is:
|
||||||
|
|
||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
It supplies:
|
||||||
|
|
||||||
|
tumbler_parse()
|
||||||
|
tumbler_free()
|
||||||
|
tumbler_to_string()
|
||||||
|
tumbler_compare()
|
||||||
|
tumbler_add()
|
||||||
|
tumbler_subtract_strong()
|
||||||
|
tumbler_subtract_weak()
|
||||||
|
tumbler_subtract()
|
||||||
|
|
||||||
|
The implementation uses `uint64_t` fields. It therefore has finite field
|
||||||
|
width even though the historical Xanadu representation used arbitrary-precision
|
||||||
|
Humbers. This is a deliberate boundary for the small reference build.
|
||||||
|
|
||||||
|
Whole-tumbler comparison follows the infinitesimal-style ordering used by the
|
||||||
|
small tumbler implementations derived from Udanax work. Missing fields are
|
||||||
|
compared as zero, giving the familiar ordering:
|
||||||
|
|
||||||
|
1 < 1.1 < 1.1.1 < 1.2 < 2
|
||||||
|
|
||||||
|
Arithmetic implements Xanadu's non-commutative tumbler addition and the strong,
|
||||||
|
weak, and generalized difference operations. See `doc/tumbler.3` for the
|
||||||
|
rules and examples.
|
||||||
|
|
||||||
|
C DATA LIFETIME
|
||||||
|
---------------
|
||||||
|
|
||||||
|
`enfilade_t` never owns the datum pointer supplied to `put_enfilade()`.
|
||||||
|
|
||||||
|
For ordinary C code:
|
||||||
|
|
||||||
|
int value = 42;
|
||||||
|
enfilade_t *e = create_enfilade();
|
||||||
|
|
||||||
|
put_enfilade(e, "1.2.3", &value);
|
||||||
|
/* use e while value remains alive */
|
||||||
|
|
||||||
|
destroy_enfilade(e);
|
||||||
|
|
||||||
|
/* value may now go out of scope */
|
||||||
|
|
||||||
|
The enfilade frees its own index nodes, never the datum pointed to by an entry.
|
||||||
|
The caller therefore controls datum lifetime.
|
||||||
|
|
||||||
|
For S7, the datum is an S7 object. The c-object mark function walks every
|
||||||
|
stored datum and marks it, so Scheme objects remain reachable through the
|
||||||
|
enfilade and are reclaimed normally after the enfilade itself is released.
|
||||||
|
|
||||||
|
C API
|
||||||
|
-----
|
||||||
|
|
||||||
|
Create and destroy:
|
||||||
|
|
||||||
|
enfilade_t *create_enfilade(void);
|
||||||
|
void destroy_enfilade(enfilade_t *enf);
|
||||||
|
|
||||||
|
Store and remove:
|
||||||
|
|
||||||
|
enf_result_t put_enfilade(enfilade_t *enf,
|
||||||
|
const char *tumbler,
|
||||||
|
void *datum);
|
||||||
|
|
||||||
|
enf_result_t remove_enfilade(enfilade_t *enf,
|
||||||
|
const char *tumbler);
|
||||||
|
|
||||||
|
Lookup:
|
||||||
|
|
||||||
|
void *getXtumbler_enfilade(enfilade_t *enf, const char *tumbler);
|
||||||
|
|
||||||
|
enf_result_t getXtumbler_values_enfilade(enfilade_t *enf,
|
||||||
|
const char *tumbler,
|
||||||
|
void ***items,
|
||||||
|
size_t *count);
|
||||||
|
|
||||||
|
Range:
|
||||||
|
|
||||||
|
enf_result_t getXrange_values_enfilade(enfilade_t *enf,
|
||||||
|
int64_t start,
|
||||||
|
int64_t end,
|
||||||
|
void ***items,
|
||||||
|
size_t *count);
|
||||||
|
|
||||||
|
Traversal:
|
||||||
|
|
||||||
|
void enfilade_visit_data(enfilade_t *enf,
|
||||||
|
enfilade_datum_visitor visitor,
|
||||||
|
void *context);
|
||||||
|
|
||||||
|
The two array-returning functions allocate the array of datum pointers. The
|
||||||
|
caller frees that array with `free()`. The datum pointers are not transferred
|
||||||
|
or freed.
|
||||||
|
|
||||||
|
`remove_enfilade()` removes only the datum at the exact final key. If that key
|
||||||
|
also names a child enfilade, the child remains. Empty intermediate children
|
||||||
|
are pruned.
|
||||||
|
|
||||||
|
S7 FFI
|
||||||
|
------
|
||||||
|
|
||||||
|
The module defines the c-object type `<enfilade>` and these Scheme functions:
|
||||||
|
|
||||||
|
make-enfilade
|
||||||
|
enfilade-put-data
|
||||||
|
enfilade-remove
|
||||||
|
enfilade-get-by-tumbler
|
||||||
|
enfilade-get-range
|
||||||
|
tumbler-compare
|
||||||
|
tumbler-add
|
||||||
|
tumbler-subtract
|
||||||
|
|
||||||
|
Load the shared object with:
|
||||||
|
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
|
||||||
|
The module does not depend on S7's `s7_repl()` startup path or on `cload.scm`.
|
||||||
|
`build/bin/logan` is a direct small REPL with the module initialized at startup.
|
||||||
|
|
||||||
|
ERRORS
|
||||||
|
------
|
||||||
|
|
||||||
|
Malformed tumbler text and a missing intermediate child are reported by the
|
||||||
|
S7 binding as `TumblerAddressException`.
|
||||||
|
|
||||||
|
A valid tumbler whose final key has no datum or child returns `()`.
|
||||||
|
|
||||||
|
`enfilade-remove` returns `#t` when a datum was removed and `#f` when the exact
|
||||||
|
final datum was not present. Malformed addresses and allocation failures are
|
||||||
|
errors.
|
||||||
|
|
||||||
|
REFERENCE IMPLEMENTATION
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
The immediate behavioral reference for the tree is:
|
||||||
|
|
||||||
|
https://raw.githubusercontent.com/enkiv2/ds-lib/refs/heads/master/Enfilade.py
|
||||||
|
|
||||||
|
The corresponding core operations are its `putValue`, `getByTumbler`, `get`,
|
||||||
|
and `getRange` behavior.
|
||||||
|
|
||||||
|
The C implementation deliberately keeps `range` as the existing numeric-key
|
||||||
|
range operation. Its ordered merge visits a key shared by a datum and child
|
||||||
|
once; the Python reference can visit that key twice because it combines the two
|
||||||
|
key lists before sorting.
|
||||||
|
|
||||||
|
WHAT THIS IS
|
||||||
|
------------
|
||||||
|
|
||||||
|
This is a small reference enfilade: a useful, inspectable building block for
|
||||||
|
other structures that need a tumbler-addressed index.
|
||||||
|
|
||||||
|
It is not the complete Udanax Green backend. The larger literature adds
|
||||||
|
specialized enfilades and machinery for WIDs, DISPs, rearrangement, versioning,
|
||||||
|
spans, virtual copies, persistent structures, and the Green storage model.
|
||||||
|
Those are layers to build on top of this primitive, not requirements for using
|
||||||
|
this one.
|
||||||
|
|
||||||
|
For a ZigZag cell catalogue, the natural simple pattern is:
|
||||||
|
|
||||||
|
cell ID (tumbler)
|
||||||
|
|
|
||||||
|
v
|
||||||
|
enfilade
|
||||||
|
|
|
||||||
|
v
|
||||||
|
cell pointer
|
||||||
|
|
||||||
|
New cells can therefore be catalogued under newly allocated tumbler IDs without
|
||||||
|
making the catalogue itself understand the cell's internal representation.
|
||||||
|
|
||||||
|
FILES
|
||||||
|
-----
|
||||||
|
|
||||||
|
inc/ public C and S7 headers
|
||||||
|
src/ C implementation, bundled S7, and REPL sources
|
||||||
|
test/ C regression tests and S7 smoke test
|
||||||
|
scheme/ Scheme reference implementation and S7 support scripts
|
||||||
|
doc/ usage guide and groff manual pages
|
||||||
|
configure compiler and shared-library capability checks
|
||||||
|
Makefile build, check, install, uninstall, and clean targets
|
||||||
|
|
||||||
|
TESTS
|
||||||
|
-----
|
||||||
|
|
||||||
|
Run all regression checks with:
|
||||||
|
|
||||||
|
make check
|
||||||
|
|
||||||
|
This builds and runs the pure C tests and the bundled S7 smoke test.
|
||||||
|
|
||||||
|
HISTORICAL REFERENCES
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Xanadu Hypertext Documents, especially "Enfilade Theory" and "Tumblers and
|
||||||
|
Humbers":
|
||||||
|
https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
|
|
||||||
|
* Udanax Green / Xanalogical Structure overview:
|
||||||
|
https://www.mprove.de/visionreality/media/xuDation.html
|
||||||
|
|
||||||
|
* Ted Nelson and Udanax developer archive:
|
||||||
|
https://www.xanadu.com.au/mail/udanax/
|
||||||
|
|
||||||
|
* Jeff Rush's `xanalogica.tumbler`, a Python implementation explicitly derived
|
||||||
|
from Udanax Green's C source:
|
||||||
|
https://pypi.org/project/xanalogica.tumbler/
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
prefix=/usr/local
|
||||||
|
cc=${CC:-cc}
|
||||||
|
make_cmd=${MAKE:-make}
|
||||||
|
install_cmd=${INSTALL:-install}
|
||||||
|
ar=${AR:-ar}
|
||||||
|
version=$(sed -n 's/^#define ENSIFER_VERSION "\([^"]*\)"/\1/p' inc/enfilade.h)
|
||||||
|
if [ -z "$version" ]; then
|
||||||
|
echo "configure: ENSIFER_VERSION missing from inc/enfilade.h" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat <<'EOF'
|
||||||
|
Usage: ./configure [OPTIONS]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--prefix=PATH installation prefix (default: /usr/local)
|
||||||
|
--prefix PATH
|
||||||
|
-V, --version print configure version
|
||||||
|
-h, --help show this help
|
||||||
|
|
||||||
|
Environment overrides: CC, CFLAGS, CPPFLAGS, LDFLAGS, AR, INSTALL, MAKE
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
while [ "$#" -gt 0 ]; do
|
||||||
|
case $1 in
|
||||||
|
--prefix=*) prefix=${1#*=} ;;
|
||||||
|
--prefix)
|
||||||
|
shift
|
||||||
|
[ "$#" -gt 0 ] || { echo "configure: --prefix requires a path" >&2; exit 2; }
|
||||||
|
prefix=$1
|
||||||
|
;;
|
||||||
|
-V|--version) printf 'configure version %s\n' "$version"; exit 0 ;;
|
||||||
|
-h|--help) usage; exit 0 ;;
|
||||||
|
*) echo "configure: unknown option: $1" >&2; usage >&2; exit 2 ;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
case $prefix in
|
||||||
|
"") echo "configure: installation prefix cannot be empty" >&2; exit 2 ;;
|
||||||
|
*' '*) echo "configure: paths containing spaces are not supported by make" >&2; exit 2 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
command -v "$cc" >/dev/null 2>&1 || { echo "configure: C compiler not found: $cc" >&2; exit 1; }
|
||||||
|
command -v "$make_cmd" >/dev/null 2>&1 || { echo "configure: make not found: $make_cmd" >&2; exit 1; }
|
||||||
|
command -v "$install_cmd" >/dev/null 2>&1 || { echo "configure: install not found: $install_cmd" >&2; exit 1; }
|
||||||
|
command -v "$ar" >/dev/null 2>&1 || { echo "configure: archiver not found: $ar" >&2; exit 1; }
|
||||||
|
|
||||||
|
tmp=${TMPDIR:-/tmp}/ensifer-configure-$$
|
||||||
|
mkdir "$tmp" || exit 1
|
||||||
|
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
|
||||||
|
cat >"$tmp/conftest.c" <<'EOF'
|
||||||
|
int ensifer_configure_probe(void) { return 0; }
|
||||||
|
EOF
|
||||||
|
if ! "$cc" ${CPPFLAGS:-} ${CFLAGS:-} -fPIC -shared -o "$tmp/conftest.so" "$tmp/conftest.c" ${LDFLAGS:-} >"$tmp/output" 2>&1; then
|
||||||
|
cat "$tmp/output" >&2
|
||||||
|
echo "configure: compiler does not support building shared libraries with -fPIC -shared" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
dl_flags=
|
||||||
|
cat >"$tmp/dltest.c" <<'EOF'
|
||||||
|
#include <dlfcn.h>
|
||||||
|
int main(void) { return dlopen(0, RTLD_NOW) == 0; }
|
||||||
|
EOF
|
||||||
|
if "$cc" ${CPPFLAGS:-} ${CFLAGS:-} -o "$tmp/dltest" "$tmp/dltest.c" -ldl ${LDFLAGS:-} >/dev/null 2>&1; then
|
||||||
|
dl_flags='-DWITH_DL'
|
||||||
|
dl_libs=-ldl
|
||||||
|
else
|
||||||
|
dl_libs=
|
||||||
|
fi
|
||||||
|
|
||||||
|
{
|
||||||
|
echo "# Generated by ./configure; edit configure options instead."
|
||||||
|
printf 'VERSION := %s\n' "$version"
|
||||||
|
printf 'CC := %s\n' "$cc"
|
||||||
|
printf 'AR := %s\n' "$ar"
|
||||||
|
printf 'INSTALL := %s\n' "$install_cmd"
|
||||||
|
printf 'PREFIX := %s\n' "$prefix"
|
||||||
|
printf 'CFLAGS ?= %s\n' "${CFLAGS:--O2 -Wall -Wextra -std=c11}"
|
||||||
|
printf 'CPPFLAGS ?= %s\n' "${CPPFLAGS:-}"
|
||||||
|
printf 'LDFLAGS ?= %s\n' "${LDFLAGS:-}"
|
||||||
|
printf 'S7_CPPFLAGS := %s -DWITH_SYSTEM_EXTRAS=0\n' "$dl_flags"
|
||||||
|
printf 'S7_LIBS := %s\n' "$dl_libs"
|
||||||
|
} > config.mk
|
||||||
|
|
||||||
|
cat > config.h <<EOF
|
||||||
|
#ifndef ENSIFER_CONFIG_H
|
||||||
|
#define ENSIFER_CONFIG_H
|
||||||
|
#define ENSIFER_VERSION "$version"
|
||||||
|
#endif
|
||||||
|
EOF
|
||||||
|
|
||||||
|
{
|
||||||
|
echo "Ensifer configure"
|
||||||
|
echo "version: $version"
|
||||||
|
echo "prefix: $prefix"
|
||||||
|
echo "compiler: $cc"
|
||||||
|
echo "make: $make_cmd"
|
||||||
|
echo "install: $install_cmd"
|
||||||
|
echo "S7 dynamic loading: ${dl_flags:-disabled}"
|
||||||
|
echo "shared library support: yes (-fPIC -shared)"
|
||||||
|
} > config.log
|
||||||
|
|
||||||
|
trap - EXIT HUP INT TERM
|
||||||
|
rm -rf "$tmp"
|
||||||
|
echo "configure: ready to build Ensifer $version"
|
||||||
|
echo " prefix: $prefix"
|
||||||
|
echo " run: make"
|
||||||
@@ -0,0 +1,367 @@
|
|||||||
|
ENSIFER ENFILADE — BEGINNER'S GUIDE
|
||||||
|
===================================
|
||||||
|
|
||||||
|
This guide assumes you have never worked with an enfilade before.
|
||||||
|
|
||||||
|
The shortest description is:
|
||||||
|
|
||||||
|
A tumbler is an address.
|
||||||
|
An enfilade is the tree that stores things at those addresses.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
1.2.3
|
||||||
|
|
||||||
|
is one tumbler address.
|
||||||
|
|
||||||
|
If we store:
|
||||||
|
|
||||||
|
1.2.3 -> 42
|
||||||
|
|
||||||
|
the enfilade creates this path automatically:
|
||||||
|
|
||||||
|
root
|
||||||
|
|
|
||||||
|
+-- 1
|
||||||
|
|
|
||||||
|
+-- 2
|
||||||
|
|
|
||||||
|
+-- 3 -> 42
|
||||||
|
|
||||||
|
You do not construct those intermediate nodes yourself.
|
||||||
|
|
||||||
|
1. BUILD AND RUN IT
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
From the Ensifer directory, configure and build:
|
||||||
|
|
||||||
|
./configure
|
||||||
|
make
|
||||||
|
|
||||||
|
Then start the REPL:
|
||||||
|
|
||||||
|
./build/bin/logan
|
||||||
|
|
||||||
|
To load the module into a different S7 host:
|
||||||
|
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
|
||||||
|
2. CREATE ONE
|
||||||
|
-------------
|
||||||
|
|
||||||
|
(define e (make-enfilade))
|
||||||
|
|
||||||
|
`e` is now the enfilade.
|
||||||
|
|
||||||
|
3. PUT SOMETHING IN IT
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
(enfilade-put-data e "1.2.3" 42)
|
||||||
|
|
||||||
|
There is now a datum at address `1.2.3`.
|
||||||
|
|
||||||
|
4. GET IT BACK
|
||||||
|
--------------
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
(42)
|
||||||
|
|
||||||
|
The result is a Scheme list because a tumbler lookup can produce more than one
|
||||||
|
datum.
|
||||||
|
|
||||||
|
5. PUT MORE IN
|
||||||
|
--------------
|
||||||
|
|
||||||
|
(enfilade-put-data e "1.2.4" 43)
|
||||||
|
(enfilade-put-data e "1.3.1" 99)
|
||||||
|
|
||||||
|
Now:
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
; => (42)
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2")
|
||||||
|
; => (42 43)
|
||||||
|
|
||||||
|
The second lookup stops at key `2` inside the `1` branch and returns the data
|
||||||
|
at that key followed by the data beneath its child enfilade.
|
||||||
|
|
||||||
|
6. A KEY MAY HAVE BOTH A DATUM AND CHILDREN
|
||||||
|
--------------------------------------------
|
||||||
|
|
||||||
|
This is important.
|
||||||
|
|
||||||
|
(enfilade-put-data e "1.2" 99)
|
||||||
|
(enfilade-put-data e "1.2.5" 50)
|
||||||
|
|
||||||
|
Now key `1.2` has both:
|
||||||
|
|
||||||
|
datum = 99
|
||||||
|
child = 5 -> 50
|
||||||
|
|
||||||
|
So:
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2")
|
||||||
|
; => (99 42 43 50)
|
||||||
|
|
||||||
|
The datum at the exact key comes first. The child is then flattened in numeric
|
||||||
|
key order.
|
||||||
|
|
||||||
|
7. REPLACE A DATUM
|
||||||
|
------------------
|
||||||
|
|
||||||
|
A final key contains one datum. Putting another datum at the same final key
|
||||||
|
replaces the old one:
|
||||||
|
|
||||||
|
(enfilade-put-data e "1.2.3" 500)
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
; => (500)
|
||||||
|
|
||||||
|
8. REMOVE A DATUM
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
(enfilade-remove e "1.2.3")
|
||||||
|
; => #t
|
||||||
|
|
||||||
|
That removes only the datum stored exactly at `1.2.3`.
|
||||||
|
|
||||||
|
It does not delete an unrelated child at that key.
|
||||||
|
|
||||||
|
Removing a nonexistent final datum returns `#f`:
|
||||||
|
|
||||||
|
(enfilade-remove e "1.2.3")
|
||||||
|
; => #f
|
||||||
|
|
||||||
|
If an intermediate child does not exist, the operation reports a
|
||||||
|
`TumblerAddressException` instead.
|
||||||
|
|
||||||
|
9. RANGE
|
||||||
|
--------
|
||||||
|
|
||||||
|
Keep this distinction in mind:
|
||||||
|
|
||||||
|
tumbler lookup = dotted address
|
||||||
|
range = numeric keys at one enfilade level
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
(enfilade-get-range e 1 3)
|
||||||
|
|
||||||
|
means “visit numeric keys 1, 2, and 3 at this level, inclusive, and flatten
|
||||||
|
their contents.”
|
||||||
|
|
||||||
|
A negative end means “through the end”:
|
||||||
|
|
||||||
|
(enfilade-get-range e 2 -1)
|
||||||
|
|
||||||
|
The `range` interface is intentionally kept exactly as the existing API.
|
||||||
|
|
||||||
|
10. TUMBLERS ARE MORE THAN STRINGS
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
The external notation is a string:
|
||||||
|
|
||||||
|
"1.2.3"
|
||||||
|
|
||||||
|
but the package now has a real C tumbler primitive underneath it.
|
||||||
|
|
||||||
|
The S7 side exposes the useful basic operations too.
|
||||||
|
|
||||||
|
Compare:
|
||||||
|
|
||||||
|
(tumbler-compare "1" "1.1")
|
||||||
|
; => -1
|
||||||
|
|
||||||
|
(tumbler-compare "1.1" "1.1.1")
|
||||||
|
; => -1
|
||||||
|
|
||||||
|
(tumbler-compare "1.2" "1.1.9")
|
||||||
|
; => 1
|
||||||
|
|
||||||
|
Add:
|
||||||
|
|
||||||
|
(tumbler-add "3" "2.16.3")
|
||||||
|
; => "5.16.3"
|
||||||
|
|
||||||
|
(tumbler-add "25.6.46.93" "0.0.3.1.21")
|
||||||
|
; => "25.6.49.1.21"
|
||||||
|
|
||||||
|
Subtract / difference:
|
||||||
|
|
||||||
|
(tumbler-subtract "1.4.3" "1.1.1")
|
||||||
|
; => "0.3.3"
|
||||||
|
|
||||||
|
These are the historical tumbler operations, not ordinary decimal-number
|
||||||
|
addition and subtraction.
|
||||||
|
|
||||||
|
11. WHY THIS MATTERS FOR ZIGZAG
|
||||||
|
-------------------------------
|
||||||
|
|
||||||
|
An enfilade does not need to understand what a datum means.
|
||||||
|
|
||||||
|
That is useful when the datum is a ZigZag cell pointer.
|
||||||
|
|
||||||
|
Conceptually:
|
||||||
|
|
||||||
|
cell ID
|
||||||
|
|
|
||||||
|
v
|
||||||
|
tumbler
|
||||||
|
|
|
||||||
|
v
|
||||||
|
enfilade
|
||||||
|
|
|
||||||
|
v
|
||||||
|
cell
|
||||||
|
|
||||||
|
The catalogue can therefore answer:
|
||||||
|
|
||||||
|
“What cell is stored at this ID?”
|
||||||
|
|
||||||
|
without embedding ZigZag cell structure into the indexing mechanism.
|
||||||
|
|
||||||
|
12. THE C VERSION
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Include:
|
||||||
|
|
||||||
|
#include "enfilade.h"
|
||||||
|
|
||||||
|
Create:
|
||||||
|
|
||||||
|
enfilade_t *e = create_enfilade();
|
||||||
|
|
||||||
|
Store a pointer:
|
||||||
|
|
||||||
|
int value = 42;
|
||||||
|
put_enfilade(e, "1.2.3", &value);
|
||||||
|
|
||||||
|
Use it while the pointed-to object is alive:
|
||||||
|
|
||||||
|
int *found = getXtumbler_enfilade(e, "1.2.3");
|
||||||
|
|
||||||
|
printf("%d\n", *found);
|
||||||
|
|
||||||
|
Destroy the index before destroying the object it points to:
|
||||||
|
|
||||||
|
destroy_enfilade(e);
|
||||||
|
|
||||||
|
The enfilade never frees `value`.
|
||||||
|
|
||||||
|
The important rule is therefore:
|
||||||
|
|
||||||
|
the enfilade owns its tree;
|
||||||
|
the caller owns its datum.
|
||||||
|
|
||||||
|
13. TUMBLER C API
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Include:
|
||||||
|
|
||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
Parse a tumbler string:
|
||||||
|
|
||||||
|
tumbler_t t = {0};
|
||||||
|
tumbler_result_t r = tumbler_parse("1.2.3", &t);
|
||||||
|
|
||||||
|
Use it:
|
||||||
|
|
||||||
|
int cmp = tumbler_compare(&t, &t);
|
||||||
|
|
||||||
|
Release it:
|
||||||
|
|
||||||
|
tumbler_free(&t);
|
||||||
|
|
||||||
|
Arithmetic produces another heap-owned tumbler whose storage the caller must
|
||||||
|
also release:
|
||||||
|
|
||||||
|
tumbler_t a = {0}, b = {0}, out = {0};
|
||||||
|
|
||||||
|
tumbler_parse("25.6.46.93", &a);
|
||||||
|
tumbler_parse("0.0.3.1.21", &b);
|
||||||
|
tumbler_add(&a, &b, &out);
|
||||||
|
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
tumbler_free(&out);
|
||||||
|
|
||||||
|
14. VALID TUMBLERS
|
||||||
|
------------------
|
||||||
|
|
||||||
|
The current C representation accepts one or more unsigned decimal fields:
|
||||||
|
|
||||||
|
1
|
||||||
|
1.2
|
||||||
|
1.2.3
|
||||||
|
0
|
||||||
|
1.0.3
|
||||||
|
100.0.7
|
||||||
|
|
||||||
|
Malformed examples:
|
||||||
|
|
||||||
|
.1
|
||||||
|
1.
|
||||||
|
1..2
|
||||||
|
1.a.2
|
||||||
|
-1.2
|
||||||
|
|
||||||
|
The zero field is legal. Historical Xanadu addressing uses zero fields as
|
||||||
|
delimiters in some larger address formats; this small enfilade does not assign
|
||||||
|
zero any additional structural meaning.
|
||||||
|
|
||||||
|
15. ERRORS
|
||||||
|
----------
|
||||||
|
|
||||||
|
Malformed tumbler text:
|
||||||
|
|
||||||
|
TumblerAddressException
|
||||||
|
|
||||||
|
Missing intermediate child during lookup/removal:
|
||||||
|
|
||||||
|
TumblerAddressException
|
||||||
|
|
||||||
|
Valid final key with no datum or descendants:
|
||||||
|
|
||||||
|
()
|
||||||
|
|
||||||
|
Allocation failure:
|
||||||
|
|
||||||
|
memory-error
|
||||||
|
|
||||||
|
16. WHAT YOU DO NOT NEED TO LEARN YET
|
||||||
|
--------------------------------------
|
||||||
|
|
||||||
|
You do not need CRUM, POOM, Granfilade, Spanfilade, WID, DISP, orgl, loaf, or
|
||||||
|
Green's virtual-memory machinery to use this object.
|
||||||
|
|
||||||
|
Those names describe the larger historical system and specialized data
|
||||||
|
structures built around enfilades. They are important when implementing those
|
||||||
|
higher layers, but they are not prerequisites for understanding this small
|
||||||
|
primitive.
|
||||||
|
|
||||||
|
The right mental model for this package is simply:
|
||||||
|
|
||||||
|
tumbler -> path through an enfilade -> datum(s)
|
||||||
|
|
||||||
|
17. REFERENCES
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Immediate behavioral reference:
|
||||||
|
|
||||||
|
https://raw.githubusercontent.com/enkiv2/ds-lib/refs/heads/master/Enfilade.py
|
||||||
|
|
||||||
|
Historical Xanadu technical material:
|
||||||
|
|
||||||
|
https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
|
|
||||||
|
Udanax Green / Xanalogical Structure:
|
||||||
|
|
||||||
|
https://www.mprove.de/visionreality/media/xuDation.html
|
||||||
|
|
||||||
|
Jeff Rush's tumbler implementation:
|
||||||
|
|
||||||
|
https://pypi.org/project/xanalogica.tumbler/
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
.Dd September 24, 2026
|
||||||
|
.Dt ENFILADE-S7 7
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm enfilade-s7
|
||||||
|
.Nd S7 Scheme bindings for the small enfilade
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
|
||||||
|
(define e (make-enfilade))
|
||||||
|
(enfilade-put-data e "1.2.3" 42)
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
.Ed
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The module wraps the C
|
||||||
|
.Vt enfilade_t
|
||||||
|
in an S7 c-object of type
|
||||||
|
.Ql <enfilade> .
|
||||||
|
The Scheme datum passed to
|
||||||
|
.Fn enfilade-put-data
|
||||||
|
is retained in the C tree as an S7 pointer.
|
||||||
|
The c-object mark callback marks every stored datum during garbage collection.
|
||||||
|
.Sh LOADING
|
||||||
|
Configure and build the S7 module:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
./configure
|
||||||
|
make
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Load it into an S7 host with:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
.Ed
|
||||||
|
.Sh FUNCTIONS
|
||||||
|
.Bl -tag -width enfilade-get-by-tumbler
|
||||||
|
.It Fn make-enfilade
|
||||||
|
Returns a new
|
||||||
|
.Ql <enfilade>
|
||||||
|
object.
|
||||||
|
.It Fn enfilade-put-data enfilade tumbler datum
|
||||||
|
Stores
|
||||||
|
.Fa datum
|
||||||
|
at
|
||||||
|
.Fa tumbler .
|
||||||
|
The tumbler is a string of unsigned decimal fields separated by periods.
|
||||||
|
Missing intermediate child enfilades are created automatically.
|
||||||
|
The return value is unspecified.
|
||||||
|
.It Fn enfilade-remove enfilade tumbler
|
||||||
|
Removes the datum at the exact final key.
|
||||||
|
Returns
|
||||||
|
.Ql #t
|
||||||
|
when a datum was removed and
|
||||||
|
.Ql #f
|
||||||
|
when the exact final datum was not present.
|
||||||
|
A child subtree at that key is retained.
|
||||||
|
.It Fn enfilade-get-by-tumbler enfilade tumbler
|
||||||
|
Returns a Scheme list containing the datum at the exact final key, followed by
|
||||||
|
the flattened contents of a child subtree at that key, if present.
|
||||||
|
Missing intermediate children and malformed tumblers raise
|
||||||
|
.Ql TumblerAddressException .
|
||||||
|
A valid final key with nothing stored there returns
|
||||||
|
.Ql () .
|
||||||
|
.It Fn enfilade-get-range enfilade start end
|
||||||
|
Visits numeric keys from
|
||||||
|
.Fa start
|
||||||
|
through
|
||||||
|
.Fa end ,
|
||||||
|
inclusive, and flattens their contents.
|
||||||
|
A negative end means through the end.
|
||||||
|
This is a one-level numeric range, not a dotted tumbler interval.
|
||||||
|
.It Fn tumbler-compare a b
|
||||||
|
Returns a negative integer, zero, or a positive integer according to the
|
||||||
|
whole-tumbler ordering of the two strings.
|
||||||
|
.It Fn tumbler-add position offset
|
||||||
|
Returns the dotted-string result of Xanadu tumbler addition.
|
||||||
|
.It Fn tumbler-subtract a b
|
||||||
|
Returns the generalized non-negative tumbler difference.
|
||||||
|
.El
|
||||||
|
.Sh EXAMPLE
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
(define e (make-enfilade))
|
||||||
|
|
||||||
|
(enfilade-put-data e "1.2.3" 42)
|
||||||
|
(enfilade-put-data e "1.2.4" 43)
|
||||||
|
(enfilade-put-data e "1.3.1" 99)
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
; => (42)
|
||||||
|
|
||||||
|
(enfilade-get-by-tumbler e "1.2")
|
||||||
|
; => (42 43)
|
||||||
|
|
||||||
|
(enfilade-get-range e 1 3)
|
||||||
|
; => (42 43 99)
|
||||||
|
|
||||||
|
(enfilade-remove e "1.2.3")
|
||||||
|
; => #t
|
||||||
|
.Ed
|
||||||
|
.Sh DATUM LIFETIME
|
||||||
|
The C tree does not copy or free Scheme datums.
|
||||||
|
The S7 c-object retains them by marking them during garbage collection.
|
||||||
|
When the c-object is finally freed, the C tree is destroyed.
|
||||||
|
Do not call
|
||||||
|
.Fn destroy_enfilade
|
||||||
|
on the pointer owned by a live S7 c-object.
|
||||||
|
.Sh ERRORS
|
||||||
|
Malformed tumbler strings and missing intermediate children raise
|
||||||
|
.Ql TumblerAddressException .
|
||||||
|
Allocation failures raise
|
||||||
|
.Ql memory-error .
|
||||||
|
Wrong Scheme argument types use S7's normal wrong-type condition.
|
||||||
|
.Sh FILES
|
||||||
|
.Bl -tag -width src/enfilade_s7.c
|
||||||
|
.It Pa src/enfilade_s7.c
|
||||||
|
S7 binding implementation.
|
||||||
|
.It Pa src/logan.c
|
||||||
|
Direct minimal REPL for the module.
|
||||||
|
.It Pa test/enfload.c
|
||||||
|
Non-interactive S7 smoke test.
|
||||||
|
.El
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr tumbler-s7 7 ,
|
||||||
|
.Xr enfilade 3 ,
|
||||||
|
.Xr tumbler 3 ,
|
||||||
|
.Xr ensifer 7
|
||||||
|
.Sh REFERENCES
|
||||||
|
.Lk https://raw.githubusercontent.com/enkiv2/ds-lib/refs/heads/master/Enfilade.py
|
||||||
+155
@@ -0,0 +1,155 @@
|
|||||||
|
.Dd September 24, 2026
|
||||||
|
.Dt ENFILADE 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm enfilade
|
||||||
|
.Nd small Model-T-style tumbler-addressed enfilade
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In enfilade.h
|
||||||
|
.Ft enfilade_t *
|
||||||
|
.Fn create_enfilade void
|
||||||
|
.Ft void
|
||||||
|
.Fn destroy_enfilade "enfilade_t *enf"
|
||||||
|
.Ft enf_result_t
|
||||||
|
.Fn put_enfilade "enfilade_t *enf" "const char *tumbler" "void *datum"
|
||||||
|
.Ft enf_result_t
|
||||||
|
.Fn remove_enfilade "enfilade_t *enf" "const char *tumbler"
|
||||||
|
.Ft void *
|
||||||
|
.Fn getXtumbler_enfilade "enfilade_t *enf" "const char *tumbler"
|
||||||
|
.Ft enf_result_t
|
||||||
|
.Fn getXtumbler_values_enfilade "enfilade_t *enf" "const char *tumbler" "void ***items" "size_t *count"
|
||||||
|
.Ft enf_result_t
|
||||||
|
.Fn getXrange_values_enfilade "enfilade_t *enf" "int64_t start" "int64_t end" "void ***items" "size_t *count"
|
||||||
|
.Ft void
|
||||||
|
.Fn enfilade_visit_data "enfilade_t *enf" "enfilade_datum_visitor visitor" "void *context"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
An
|
||||||
|
.Nm
|
||||||
|
is a tree whose local keys are unsigned integer tumbler fields.
|
||||||
|
Each node can contain terminal datum values and child enfilades at the same
|
||||||
|
numeric keys.
|
||||||
|
.Pp
|
||||||
|
Putting
|
||||||
|
.Ql 1.2.3
|
||||||
|
creates the path through keys 1, 2, and 3. The datum is stored at the final
|
||||||
|
key.
|
||||||
|
.Pp
|
||||||
|
The interface is intentionally small and follows the behavior of the
|
||||||
|
Model-T-style Python reference implementation used by this project.
|
||||||
|
.Sh CREATION AND LIFETIME
|
||||||
|
.Fn create_enfilade
|
||||||
|
returns an empty enfilade or NULL on allocation failure.
|
||||||
|
.Fn destroy_enfilade
|
||||||
|
recursively frees the enfilade tree.
|
||||||
|
.Pp
|
||||||
|
The enfilade owns its tree nodes but does not own datum pointers supplied by
|
||||||
|
the caller. The caller must keep every datum alive for as long as it may be
|
||||||
|
returned by a lookup and must destroy the enfilade before those datum objects
|
||||||
|
are destroyed.
|
||||||
|
.Sh INSERTION
|
||||||
|
.Fn put_enfilade
|
||||||
|
stores
|
||||||
|
.Fa datum
|
||||||
|
at the exact final field of
|
||||||
|
.Fa tumbler .
|
||||||
|
Missing intermediate child enfilades are created automatically.
|
||||||
|
.Pp
|
||||||
|
A second insertion at the same final key replaces the existing datum.
|
||||||
|
.Pp
|
||||||
|
If a final key already contains a datum and a child subtree, insertion replaces
|
||||||
|
only the datum.
|
||||||
|
.Sh REMOVAL
|
||||||
|
.Fn remove_enfilade
|
||||||
|
removes only the datum stored at the exact final key.
|
||||||
|
.Pp
|
||||||
|
A child subtree at that same key is retained.
|
||||||
|
Empty intermediate child enfilades are pruned after successful removal.
|
||||||
|
The datum pointer itself is never freed.
|
||||||
|
.Pp
|
||||||
|
A missing exact datum returns
|
||||||
|
.Dv ENF_ERR_NOT_FOUND .
|
||||||
|
.Sh EXACT LOOKUP
|
||||||
|
.Fn getXtumbler_enfilade
|
||||||
|
returns only the datum stored at the exact final key, or NULL if none exists or
|
||||||
|
the path is invalid.
|
||||||
|
.Pp
|
||||||
|
For callers that need reference-style flattened lookup,
|
||||||
|
.Fn getXtumbler_values_enfilade
|
||||||
|
walks every component except the last as a child path, then returns a newly
|
||||||
|
allocated array containing the datum at the final key followed by all data in
|
||||||
|
its child subtree.
|
||||||
|
.Pp
|
||||||
|
The caller owns the returned array and releases it with
|
||||||
|
.Xr free 3 .
|
||||||
|
The datum pointers remain owned by the caller.
|
||||||
|
.Sh RANGE
|
||||||
|
.Fn getXrange_values_enfilade
|
||||||
|
walks numeric keys at the current enfilade level from
|
||||||
|
.Fa start
|
||||||
|
through
|
||||||
|
.Fa end ,
|
||||||
|
inclusive, and recursively flattens their contents.
|
||||||
|
A negative
|
||||||
|
.Fa end
|
||||||
|
means there is no upper bound.
|
||||||
|
.Pp
|
||||||
|
This operation deliberately remains the existing
|
||||||
|
.Dq range
|
||||||
|
interface. It is not a parser for dotted tumbler intervals.
|
||||||
|
.Sh TRAVERSAL
|
||||||
|
.Fn enfilade_visit_data
|
||||||
|
visits every stored datum. The visitor callback is not retained after the
|
||||||
|
call.
|
||||||
|
The callback must not be assumed to outlive the traversal call.
|
||||||
|
.Sh EXAMPLE
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
#include "enfilade.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
enfilade_t *e = create_enfilade();
|
||||||
|
int value = 42;
|
||||||
|
void *found;
|
||||||
|
|
||||||
|
if (!e) return 1;
|
||||||
|
if (put_enfilade(e, "1.2.3", &value).code != ENF_OK)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
found = getXtumbler_enfilade(e, "1.2.3");
|
||||||
|
if (found)
|
||||||
|
printf("%d\\n", *(int *)found);
|
||||||
|
|
||||||
|
destroy_enfilade(e);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
.Ed
|
||||||
|
.Sh ERRORS
|
||||||
|
.Bl -tag -width ENF_ERR_NOT_FOUND
|
||||||
|
.It Dv ENF_OK
|
||||||
|
Success.
|
||||||
|
.It Dv ENF_ERR_NULL
|
||||||
|
A required pointer was null.
|
||||||
|
.It Dv ENF_ERR_ADDR
|
||||||
|
The tumbler text is malformed.
|
||||||
|
.It Dv ENF_ERR_NOT_FOUND
|
||||||
|
A requested datum or intermediate child was absent.
|
||||||
|
.It Dv ENF_ERR_NOMEM
|
||||||
|
An allocation failed.
|
||||||
|
.El
|
||||||
|
.Sh THREADING
|
||||||
|
The enfilade structure does not provide internal locking.
|
||||||
|
External synchronization is required if multiple threads may access the same
|
||||||
|
instance concurrently.
|
||||||
|
.Sh SCOPE
|
||||||
|
This package implements the small tumbler-keyed tree only. It does not
|
||||||
|
implement the broader Green machinery such as WIDs, DISPs, orgls, POOMs,
|
||||||
|
Spanfilades, persistent CRUM storage, rearrangement, or version management.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr tumbler 3 ,
|
||||||
|
.Xr enfilade-s7 7 ,
|
||||||
|
.Xr ensifer 7
|
||||||
|
.Sh REFERENCES
|
||||||
|
.Lk https://raw.githubusercontent.com/enkiv2/ds-lib/refs/heads/master/Enfilade.py
|
||||||
|
.Pp
|
||||||
|
.Lk https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
+212
@@ -0,0 +1,212 @@
|
|||||||
|
.Dd September 24, 2026
|
||||||
|
.Dt ENSIFER 7
|
||||||
|
.Os
|
||||||
|
.ds V 0.1.20
|
||||||
|
.Sh NAME
|
||||||
|
.Nm ensifer
|
||||||
|
.Nd small Model-T-style enfilade for C and S7 Scheme
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
./build/bin/logan
|
||||||
|
|
||||||
|
(define e (make-enfilade))
|
||||||
|
(enfilade-put-data e "1.2.3" 42)
|
||||||
|
(enfilade-get-by-tumbler e "1.2.3")
|
||||||
|
.Ed
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
.Nm ensifer
|
||||||
|
is a small, working enfilade layer intended to be understandable and reusable
|
||||||
|
without requiring the larger Xanadu/Green architecture to be introduced first.
|
||||||
|
.Pp
|
||||||
|
The central idea is simple: a dotted tumbler is a path through an indexed tree.
|
||||||
|
Storing:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1.2.3 -> 42
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
conceptually produces:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
root
|
||||||
|
|
|
||||||
|
+-- 1
|
||||||
|
|
|
||||||
|
+-- 2
|
||||||
|
|
|
||||||
|
+-- 3 -> 42
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
A node can contain a datum and a child at the same numeric key. Lookup returns
|
||||||
|
the exact-key datum first and then recursively flattens the child.
|
||||||
|
.Sh FIRST USE
|
||||||
|
Configure and build the complete package:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
./configure
|
||||||
|
make
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Run the REPL, which initializes the enfilade bindings automatically:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
./build/bin/logan
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Install with
|
||||||
|
.Cm make install .
|
||||||
|
Use
|
||||||
|
.Cm ./configure --prefix=PATH
|
||||||
|
for a non-default prefix, or set
|
||||||
|
.Ev DESTDIR
|
||||||
|
when staging an install.
|
||||||
|
.Sh CORE OPERATIONS
|
||||||
|
The small reference consists of:
|
||||||
|
.Bl -tag -width tumbler-subtract
|
||||||
|
.It Fn make-enfilade
|
||||||
|
Create an empty enfilade.
|
||||||
|
.It Fn enfilade-put-data
|
||||||
|
Insert or replace a datum at a tumbler.
|
||||||
|
.It Fn enfilade-remove
|
||||||
|
Remove the datum at an exact tumbler key.
|
||||||
|
.It Fn enfilade-get-by-tumbler
|
||||||
|
Resolve a dotted tumbler and flatten the matching subtree.
|
||||||
|
.It Fn enfilade-get-range
|
||||||
|
Range over numeric keys at one enfilade level.
|
||||||
|
.It Fn tumbler-compare
|
||||||
|
Compare complete tumbler addresses.
|
||||||
|
.It Fn tumbler-add
|
||||||
|
Perform Xanadu tumbler addition.
|
||||||
|
.It Fn tumbler-subtract
|
||||||
|
Perform the generalized tumbler difference.
|
||||||
|
.El
|
||||||
|
.Sh TUMBLERS
|
||||||
|
Tumblers are unsigned integer fields separated by periods:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1
|
||||||
|
1.2
|
||||||
|
1.2.3
|
||||||
|
1.0.7
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
The package stores each field as
|
||||||
|
.Vt uint64_t .
|
||||||
|
It does not implement the historical arbitrary-precision Humber encoding.
|
||||||
|
.Pp
|
||||||
|
Whole-tumbler comparison uses the infinitesimal-style order represented by the
|
||||||
|
small Udanax-derived implementations:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1 < 1.1 < 1.1.1 < 1.2 < 2
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
The C tumbler module also implements Xanadu's non-commutative addition and
|
||||||
|
strong, weak, and generalized subtraction. See
|
||||||
|
.Xr tumbler 3 .
|
||||||
|
.Sh RANGE
|
||||||
|
The name
|
||||||
|
.Dq range
|
||||||
|
is intentionally retained.
|
||||||
|
.Pp
|
||||||
|
.Fn enfilade-get-range
|
||||||
|
operates on the numeric keys of the current enfilade level, with inclusive
|
||||||
|
bounds. A negative upper bound means through the end.
|
||||||
|
.Pp
|
||||||
|
It is not a dotted tumbler interval expression.
|
||||||
|
.Sh REMOVAL
|
||||||
|
.Fn enfilade-remove
|
||||||
|
removes only the datum at the exact final key.
|
||||||
|
If a child subtree exists at that key, it remains.
|
||||||
|
Empty intermediate children are pruned.
|
||||||
|
.Pp
|
||||||
|
In Scheme the operation returns
|
||||||
|
.Ql #t
|
||||||
|
on removal and
|
||||||
|
.Ql #f
|
||||||
|
when there was no datum at the exact key.
|
||||||
|
.Sh DATUM OWNERSHIP
|
||||||
|
C callers own the objects pointed to by datum entries.
|
||||||
|
The enfilade owns only its index nodes and never frees a datum pointer.
|
||||||
|
.Pp
|
||||||
|
In S7, datums are Scheme objects. The c-object mark callback visits every
|
||||||
|
stored datum, making them reachable to the garbage collector while the
|
||||||
|
enfilade remains live.
|
||||||
|
.Sh WHAT THIS IS
|
||||||
|
This is a small tumbler-addressed Model-T-style enfilade.
|
||||||
|
It is intended as a primitive that other structures can use directly.
|
||||||
|
.Pp
|
||||||
|
It is not a complete implementation of Udanax Green.
|
||||||
|
The broader historical system includes specialized enfilades and additional
|
||||||
|
machinery for WIDs, DISPs, rearrangement, versioning, spans, virtual copies,
|
||||||
|
and persistence.
|
||||||
|
.Pp
|
||||||
|
Those concepts are intentionally outside this layer so that the fundamental
|
||||||
|
indexing mechanism remains visible.
|
||||||
|
.Sh REFERENCE IMPLEMENTATION
|
||||||
|
The immediate behavioral reference is the `Enfilade.py` implementation in
|
||||||
|
.Dq ds-lib .
|
||||||
|
.Pp
|
||||||
|
Its `putValue`, `getByTumbler`, `get`, and `getRange` behavior correspond to the
|
||||||
|
operations in this package.
|
||||||
|
.Pp
|
||||||
|
The C `range` implementation deliberately merges a node's value keys and child
|
||||||
|
keys rather than traversing a duplicated combined-key list. Thus a key shared
|
||||||
|
by a datum and child is visited once during a range traversal; the Python
|
||||||
|
reference can visit it twice in that particular case. Exact tumbler lookup
|
||||||
|
retains the reference's datum-then-child result order.
|
||||||
|
.Sh ZIGZAG USE
|
||||||
|
A simple catalogue can use the enfilade without teaching the enfilade anything
|
||||||
|
about cells:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
cell ID (tumbler)
|
||||||
|
|
|
||||||
|
v
|
||||||
|
enfilade
|
||||||
|
|
|
||||||
|
v
|
||||||
|
cell pointer
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
This makes the enfilade a general index beneath a cell store rather than a
|
||||||
|
second representation of the cell itself.
|
||||||
|
.Sh FILES
|
||||||
|
.Bl -tag -width src/enfilade_s7.c
|
||||||
|
.It Pa src/enfilade.c
|
||||||
|
Enfilade tree implementation.
|
||||||
|
.It Pa inc/enfilade.h
|
||||||
|
Public C API.
|
||||||
|
.It Pa src/tumbler.c
|
||||||
|
Tumbler parser, comparison, and arithmetic.
|
||||||
|
.It Pa inc/tumbler.h
|
||||||
|
Public tumbler API.
|
||||||
|
.It Pa src/enfilade_s7.c
|
||||||
|
S7 bindings and c-object type.
|
||||||
|
.It Pa src/logan.c
|
||||||
|
Direct REPL.
|
||||||
|
.It Pa test/enfload.c
|
||||||
|
S7 smoke test.
|
||||||
|
.It Pa scheme/enf.scm
|
||||||
|
Scheme smoke-test procedure.
|
||||||
|
.It Pa test/tumbler-test.c
|
||||||
|
Tumbler regression tests.
|
||||||
|
.It Pa test/enfilade-test.c
|
||||||
|
Enfilade regression tests.
|
||||||
|
.El
|
||||||
|
.Sh VERSION
|
||||||
|
The package version is available as
|
||||||
|
.Dv ENSIFER_VERSION
|
||||||
|
from
|
||||||
|
.Pa inc/enfilade.h
|
||||||
|
and from
|
||||||
|
.Nm logan --version .
|
||||||
|
The current version is 0.1.20.
|
||||||
|
.Sh TRIVIA
|
||||||
|
Ensifer is called after the greatest frost mage on Zuluhed. He would duel glads naked & win.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr enfilade 3 ,
|
||||||
|
.Xr tumbler 3 ,
|
||||||
|
.Xr enfilade-s7 7 ,
|
||||||
|
.Xr tumbler-s7 7
|
||||||
|
.Sh REFERENCES
|
||||||
|
.Lk https://raw.githubusercontent.com/enkiv2/ds-lib/refs/heads/master/Enfilade.py
|
||||||
|
.Pp
|
||||||
|
.Lk https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
|
.Pp
|
||||||
|
.Lk https://www.mprove.de/visionreality/media/xuDation.html
|
||||||
|
.Pp
|
||||||
|
.Lk https://pypi.org/project/xanalogica.tumbler/
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
.Dd September 24, 2026
|
||||||
|
.Dt TUMBLER-S7 7
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm tumbler-s7
|
||||||
|
.Nd S7 Scheme interface to the tumbler primitive
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
Bd -literal -offset indent
|
||||||
|
(load "./build/lib/enfilade.so" (inlet 'init_func 'init_enfilade_s7))
|
||||||
|
|
||||||
|
(tumbler-compare "1" "1.1")
|
||||||
|
(tumbler-add "3" "2.16.3")
|
||||||
|
(tumbler-subtract "1.4.3" "1.1.1")
|
||||||
|
.Ed
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The module exposes the small C tumbler implementation to S7 without requiring
|
||||||
|
a separate Scheme tumbler object type. Tumblers cross the FFI as dotted
|
||||||
|
unsigned-integer strings.
|
||||||
|
.Sh FUNCTIONS
|
||||||
|
.Bl -tag -width tumbler-subtract
|
||||||
|
.It Fn tumbler-compare a b
|
||||||
|
Returns a negative integer, zero, or a positive integer according to the
|
||||||
|
whole-tumbler ordering of
|
||||||
|
.Fa a
|
||||||
|
and
|
||||||
|
.Fa b .
|
||||||
|
.It Fn tumbler-add position offset
|
||||||
|
Returns a new tumbler string containing Xanadu tumbler addition.
|
||||||
|
.It Fn tumbler-subtract a b
|
||||||
|
Returns the generalized non-negative tumbler difference.
|
||||||
|
.El
|
||||||
|
.Sh EXAMPLES
|
||||||
|
.Bd -literal
|
||||||
|
(tumbler-compare "1" "1.1")
|
||||||
|
; => -1
|
||||||
|
|
||||||
|
(tumbler-add "3" "2.16.3")
|
||||||
|
; => "5.16.3"
|
||||||
|
|
||||||
|
(tumbler-add "25.6.46.93" "0.0.3.1.21")
|
||||||
|
; => "25.6.49.1.21"
|
||||||
|
|
||||||
|
(tumbler-subtract "1.4.3" "1.1.1")
|
||||||
|
; => "0.3.3"
|
||||||
|
.Ed
|
||||||
|
.Sh ERRORS
|
||||||
|
Malformed tumbler strings raise
|
||||||
|
.Ql TumblerAddressException .
|
||||||
|
Allocation failures raise
|
||||||
|
.Ql memory-error .
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr tumbler 3 ,
|
||||||
|
.Xr enfilade-s7 7
|
||||||
|
.Sh REFERENCES
|
||||||
|
.Lk https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
+179
@@ -0,0 +1,179 @@
|
|||||||
|
.Dd September 24, 2026
|
||||||
|
.Dt TUMBLER 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm tumbler
|
||||||
|
.Nd dotted integer coordinates and Xanadu tumbler arithmetic
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In tumbler.h
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_parse "const char *text" "tumbler_t *out"
|
||||||
|
.Ft void
|
||||||
|
.Fn tumbler_free "tumbler_t *t"
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_to_string "const tumbler_t *t" "char **out"
|
||||||
|
.Ft int
|
||||||
|
.Fn tumbler_compare "const tumbler_t *a" "const tumbler_t *b"
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_add "const tumbler_t *position" "const tumbler_t *offset" "tumbler_t *out"
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_subtract_strong "const tumbler_t *position" "const tumbler_t *offset" "tumbler_t *out"
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_subtract_weak "const tumbler_t *position" "const tumbler_t *offset" "tumbler_t *out"
|
||||||
|
.Ft tumbler_result_t
|
||||||
|
.Fn tumbler_subtract "const tumbler_t *a" "const tumbler_t *b" "tumbler_t *out"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
module provides the small tumbler primitive used by the enfilade.
|
||||||
|
A tumbler is a finite sequence of unsigned integer fields written in dotted
|
||||||
|
form, such as
|
||||||
|
.Ql 1.2.3 .
|
||||||
|
.Pp
|
||||||
|
The implementation stores each field as a
|
||||||
|
.Vt uint64_t .
|
||||||
|
The historical Xanadu system used arbitrary-precision Humbers; this package
|
||||||
|
intentionally does not implement that representation.
|
||||||
|
.Sh PARSING
|
||||||
|
.Fn tumbler_parse
|
||||||
|
parses one or more unsigned decimal fields separated by periods.
|
||||||
|
On success it allocates the field array in
|
||||||
|
.Vt tumbler_t .
|
||||||
|
The caller must release it with
|
||||||
|
.Fn tumbler_free .
|
||||||
|
.Pp
|
||||||
|
Examples:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1
|
||||||
|
1.2
|
||||||
|
1.2.3
|
||||||
|
0.1.0.7
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Malformed input includes an empty string, a leading or trailing period, two
|
||||||
|
periods in succession, non-decimal characters, and negative fields.
|
||||||
|
.Sh COMPARISON
|
||||||
|
.Fn tumbler_compare
|
||||||
|
returns a negative value, zero, or a positive value according as
|
||||||
|
.Fa a
|
||||||
|
is less than, equal to, or greater than
|
||||||
|
.Fa b .
|
||||||
|
.Pp
|
||||||
|
Comparison is performed from the most significant field toward the least
|
||||||
|
significant. A missing field is treated as zero for comparison. Thus:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1 < 1.1 < 1.1.1 < 1.2 < 2
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
This is the infinitesimal-style tumbler ordering used by the small Udanax-derived
|
||||||
|
tumbler implementations. In this implementation, trailing zero fields do not
|
||||||
|
change comparison order.
|
||||||
|
.Sh ADDITION
|
||||||
|
.Fn tumbler_add
|
||||||
|
implements Xanadu tumbler addition. The operands have different roles:
|
||||||
|
.Fa position
|
||||||
|
is a position and
|
||||||
|
.Fa offset
|
||||||
|
is a distance to move forward.
|
||||||
|
.Pp
|
||||||
|
The procedure is:
|
||||||
|
.Bl -enum
|
||||||
|
.It
|
||||||
|
Copy fields from the position while the corresponding offset fields are zero.
|
||||||
|
.It
|
||||||
|
At the first non-zero offset field, add it to the corresponding position field.
|
||||||
|
.It
|
||||||
|
Copy the remaining offset fields into the result.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
For example:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
3 + 2.16.3
|
||||||
|
= 5.16.3
|
||||||
|
|
||||||
|
25.6.46.93 + 0.0.3.1.21
|
||||||
|
= 25.6.49.1.21
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Addition is non-commutative.
|
||||||
|
.Sh SUBTRACTION
|
||||||
|
The historical system distinguishes strong and weak subtraction.
|
||||||
|
.Fn tumbler_subtract_strong
|
||||||
|
implements strong subtraction and
|
||||||
|
.Fn tumbler_subtract_weak
|
||||||
|
implements weak subtraction.
|
||||||
|
.Pp
|
||||||
|
Strong subtraction preserves matching leading fields as zero until the first
|
||||||
|
difference, subtracts there, then copies the remaining fields from the
|
||||||
|
position. It requires
|
||||||
|
.Fa position
|
||||||
|
not be less than
|
||||||
|
.Fa offset .
|
||||||
|
.Pp
|
||||||
|
For example:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1.4.3 s- 1.1.1
|
||||||
|
= 0.3.3
|
||||||
|
|
||||||
|
0.1.2.3.4.5.6 s- 0.1.2.3.3.3.3
|
||||||
|
= 0.0.0.0.1.5.6
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
Weak subtraction preserves the position through leading zero offset fields,
|
||||||
|
then subtracts the first non-zero offset field and stops.
|
||||||
|
For example:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
1.4.3 w- 1.1.1
|
||||||
|
= 0
|
||||||
|
|
||||||
|
0.3.3.3.4.5.6 w- 0.1.1.3.3.3.3
|
||||||
|
= 0.2
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
.Fn tumbler_subtract
|
||||||
|
is the generalized difference operation used by the Xanadu system. If
|
||||||
|
.Fa a
|
||||||
|
is greater than
|
||||||
|
.Fa b ,
|
||||||
|
strong subtraction computes
|
||||||
|
.Ql a - b .
|
||||||
|
Otherwise weak subtraction computes
|
||||||
|
.Ql b - a .
|
||||||
|
The result is therefore never negative.
|
||||||
|
.Sh MEMORY
|
||||||
|
All parsed and arithmetic result tumblers own heap storage for their digit
|
||||||
|
arrays. The caller is responsible for calling
|
||||||
|
.Fn tumbler_free .
|
||||||
|
.Pp
|
||||||
|
Strings returned by
|
||||||
|
.Fn tumbler_to_string
|
||||||
|
are allocated with
|
||||||
|
.Xr malloc 3
|
||||||
|
and are released with
|
||||||
|
.Xr free 3 .
|
||||||
|
.Sh ERRORS
|
||||||
|
The result structure uses these codes:
|
||||||
|
.Bl -tag -width TUMBLER_ERR_UNDERFLOW
|
||||||
|
.It Dv TUMBLER_OK
|
||||||
|
Success.
|
||||||
|
.It Dv TUMBLER_ERR_NULL
|
||||||
|
A required pointer was null.
|
||||||
|
.It Dv TUMBLER_ERR_INVALID
|
||||||
|
The tumbler representation is malformed.
|
||||||
|
.It Dv TUMBLER_ERR_NOMEM
|
||||||
|
Memory allocation failed.
|
||||||
|
.It Dv TUMBLER_ERR_OVERFLOW
|
||||||
|
An integer field or arithmetic result exceeded
|
||||||
|
.Vt uint64_t .
|
||||||
|
.It Dv TUMBLER_ERR_UNDERFLOW
|
||||||
|
A subtraction would require a negative field.
|
||||||
|
.El
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr enfilade 3 ,
|
||||||
|
.Xr tumbler-s7 7 ,
|
||||||
|
.Xr enfilade-s7 7
|
||||||
|
.Sh REFERENCES
|
||||||
|
.Pp
|
||||||
|
.Lk https://sentido-labs.com/en/library/201904240732/Xanadu%20Hypertext%20Documents.html
|
||||||
|
.Pp
|
||||||
|
.Lk https://pypi.org/project/xanalogica.tumbler/
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
#ifndef ENFILADE_H
|
||||||
|
#define ENFILADE_H
|
||||||
|
|
||||||
|
#define ENSIFER_VERSION "0.1.20"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct enfilade enfilade_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
ENF_OK = 0,
|
||||||
|
ENF_ERR_NULL,
|
||||||
|
ENF_ERR_ADDR,
|
||||||
|
ENF_ERR_NOT_FOUND,
|
||||||
|
ENF_ERR_NOMEM
|
||||||
|
} enf_error_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
enf_error_t code;
|
||||||
|
const char *msg;
|
||||||
|
} enf_result_t;
|
||||||
|
|
||||||
|
enfilade_t *create_enfilade(void);
|
||||||
|
void destroy_enfilade(enfilade_t *enf);
|
||||||
|
|
||||||
|
/* Store datum at a dotted tumbler address. Missing intermediate children are
|
||||||
|
* created automatically. Replacing the final datum is allowed. */
|
||||||
|
enf_result_t put_enfilade(enfilade_t *enf, const char *tumbler, void *datum);
|
||||||
|
|
||||||
|
/* Remove only the datum stored at the exact final key. A child subtree at the
|
||||||
|
* same key is retained. Empty intermediate child nodes are pruned. */
|
||||||
|
enf_result_t remove_enfilade(enfilade_t *enf, const char *tumbler);
|
||||||
|
|
||||||
|
/* Exact terminal value, without descendant traversal. */
|
||||||
|
void *getXtumbler_enfilade(enfilade_t *enf, const char *tumbler);
|
||||||
|
|
||||||
|
/* Resolve a tumbler using the Enfilade.py getByTumbler/get semantics.
|
||||||
|
* Returns a newly allocated array of datum pointers. Caller frees the array;
|
||||||
|
* the datum pointers remain owned by the application/S7. */
|
||||||
|
enf_result_t getXtumbler_values_enfilade(enfilade_t *enf,
|
||||||
|
const char *tumbler,
|
||||||
|
void ***items,
|
||||||
|
size_t *count);
|
||||||
|
|
||||||
|
/* Flatten all values in numeric key order for the inclusive range. A negative
|
||||||
|
* end means "everything", matching the Python default end=-1. */
|
||||||
|
enf_result_t getXrange_values_enfilade(enfilade_t *enf,
|
||||||
|
int64_t start,
|
||||||
|
int64_t end,
|
||||||
|
void ***items,
|
||||||
|
size_t *count);
|
||||||
|
|
||||||
|
typedef void (*enfilade_datum_visitor)(void *datum, void *context);
|
||||||
|
void enfilade_visit_data(enfilade_t *enf,
|
||||||
|
enfilade_datum_visitor visitor,
|
||||||
|
void *context);
|
||||||
|
|
||||||
|
#endif /* ENFILADE_H */
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef ENSIFER_ENFILADE_S7_H
|
||||||
|
#define ENSIFER_ENFILADE_S7_H
|
||||||
|
|
||||||
|
#include "s7.h"
|
||||||
|
|
||||||
|
void init_enfilade_s7(s7_scheme *sc);
|
||||||
|
|
||||||
|
#endif /* ENSIFER_ENFILADE_S7_H */
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef TUMBLER_H
|
||||||
|
#define TUMBLER_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint64_t *digits;
|
||||||
|
size_t count;
|
||||||
|
} tumbler_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TUMBLER_OK = 0,
|
||||||
|
TUMBLER_ERR_NULL,
|
||||||
|
TUMBLER_ERR_INVALID,
|
||||||
|
TUMBLER_ERR_NOMEM,
|
||||||
|
TUMBLER_ERR_OVERFLOW,
|
||||||
|
TUMBLER_ERR_UNDERFLOW
|
||||||
|
} tumbler_error_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
tumbler_error_t code;
|
||||||
|
const char *msg;
|
||||||
|
} tumbler_result_t;
|
||||||
|
|
||||||
|
/* Parse the dotted decimal representation into a heap-owned tumbler.
|
||||||
|
* On success, caller owns out->digits and must call tumbler_free(). */
|
||||||
|
tumbler_result_t tumbler_parse(const char *text, tumbler_t *out);
|
||||||
|
void tumbler_free(tumbler_t *t);
|
||||||
|
|
||||||
|
/* Format a tumbler as a newly allocated dotted decimal string.
|
||||||
|
* Caller frees the returned string. */
|
||||||
|
tumbler_result_t tumbler_to_string(const tumbler_t *t, char **out);
|
||||||
|
|
||||||
|
/* Whole-tumbler numeric ordering. Missing fields compare as zero, so
|
||||||
|
* 1 < 1.1 < 1.1.1 and trailing zero fields do not affect ordering. */
|
||||||
|
int tumbler_compare(const tumbler_t *a, const tumbler_t *b);
|
||||||
|
|
||||||
|
/* Xanadu tumbler addition: position + offset. */
|
||||||
|
tumbler_result_t tumbler_add(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out);
|
||||||
|
|
||||||
|
/* Strong and weak tumbler subtraction, as described in the historical
|
||||||
|
* Xanadu material. Both require non-negative results at their first
|
||||||
|
* differing field. */
|
||||||
|
tumbler_result_t tumbler_subtract_strong(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out);
|
||||||
|
tumbler_result_t tumbler_subtract_weak(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out);
|
||||||
|
|
||||||
|
/* Generalized tumbler difference used by the Xanadu system:
|
||||||
|
* if a > b, strong-subtract b from a; otherwise weak-subtract a from b. */
|
||||||
|
tumbler_result_t tumbler_subtract(const tumbler_t *a,
|
||||||
|
const tumbler_t *b,
|
||||||
|
tumbler_t *out);
|
||||||
|
|
||||||
|
#endif /* TUMBLER_H */
|
||||||
@@ -0,0 +1,798 @@
|
|||||||
|
(provide 'cload.scm)
|
||||||
|
|
||||||
|
;;; --------------------------------------------------------------------------------
|
||||||
|
;;; automatically link a C function into s7 (there are a bunch of examples below)
|
||||||
|
;;; (c-define '(double j0 (double)) "m" "math.h")
|
||||||
|
;;; means link the name m:j0 to the math library function j0 passing a double arg and getting a double result (reals in s7)
|
||||||
|
;;;
|
||||||
|
;;; (c-define c-info prefix headers cflags ldflags)
|
||||||
|
;;; prefix is some arbitrary prefix (it can be "") that you want prepended to various names.
|
||||||
|
;;; headers is a list of headers (as strings) that the c-info relies on, (("math.h") for example).
|
||||||
|
;;; cflags are any special C compiler flags that are needed ("-I." in particular).
|
||||||
|
;;; ldflags is the similar case for the loader.
|
||||||
|
;;; c-info is a list that describes the C entities that you want to tie into s7.
|
||||||
|
;;; it can be either one list describing one entity, or a list of such lists.
|
||||||
|
;;; Each description has the form: (return-type entity-name-in-C (argument-type...))
|
||||||
|
;;; where each entry is a symbol, and C names are used throughout. So, in the j0
|
||||||
|
;;; example above, (double j0 (double)) says we want access to j0, it returns
|
||||||
|
;;; a C double, and takes one argument, also a C double. s7 tries to figure out
|
||||||
|
;;; what the corresponding s7 type is, but in tricky cases, you should tell it
|
||||||
|
;;; by replacing the bare type name with a list: (C-type underlying-C-type). For example,
|
||||||
|
;;; the Snd function set_graph_style takes an (enum) argument of type graph_style_t.
|
||||||
|
;;; This is actually an int, so we use (graph_style_t int) as the type:
|
||||||
|
;;; (void set_graph_style ((graph_style_t int)))
|
||||||
|
;;; If the C entity is a constant, then the descriptor list has just two entries,
|
||||||
|
;;; the C-type and the entity name: (int F_OK) for example. The entity name can also be a list
|
||||||
|
;;; (an enum listing for example).
|
||||||
|
;;; If the C type has a space ("struct tm*" for example), use (symbol "struct tm*")
|
||||||
|
;;; to construct the corresponding symbol.
|
||||||
|
;;; The entity is placed in the current s7 environment under the name (string-append prefix ":" name)
|
||||||
|
;;; where the ":" is omitted if the prefix is null. So in the j0 example, we get in s7 the function m:j0.
|
||||||
|
;;;
|
||||||
|
;;; some examples:
|
||||||
|
;;;
|
||||||
|
;;; (c-define '((double j0 (double))
|
||||||
|
;;; (double j1 (double))
|
||||||
|
;;; (double erf (double))
|
||||||
|
;;; (double erfc (double))
|
||||||
|
;;; (double lgamma (double)))
|
||||||
|
;;; "m" "math.h")
|
||||||
|
;;;
|
||||||
|
;;;
|
||||||
|
;;; (c-define '(char* getenv (char*)))
|
||||||
|
;;; (c-define '(int setenv (char* char* int)))
|
||||||
|
;;; (define get-environment-variable (let () (c-define '(char* getenv (char*))) getenv))
|
||||||
|
;;;
|
||||||
|
;;; (define file-exists? (let () (c-define '((int F_OK) (int access (char* int))) "" "unistd.h") (lambda (arg) (= (access arg F_OK) 0))))
|
||||||
|
;;; (define delete-file (let () (c-define '(int unlink (char*)) "" "unistd.h") (lambda (file) (= (unlink file) 0)))) ; 0=success, -1=failure
|
||||||
|
;;;
|
||||||
|
;;;
|
||||||
|
;;; these pick up Snd stuff:
|
||||||
|
;;; (c-define '(char* version_info ()) "" "snd.h" "-I.")
|
||||||
|
;;; (c-define '(mus_float_t mus_degrees_to_radians (mus_float_t)) "" "snd.h" "-I.")
|
||||||
|
;;;
|
||||||
|
;;; (c-define '(snd_info* any_selected_sound ()) "" "snd.h" "-I.")
|
||||||
|
;;; (c-define '(void select_channel (snd_info* int)) "" "snd.h" "-I.")
|
||||||
|
;;; (c-define '(((graph_style_t int) (GRAPH_LINES GRAPH_DOTS GRAPH_FILLED GRAPH_DOTS_AND_LINES GRAPH_LOLLIPOPS))
|
||||||
|
;;; (void set_graph_style ((graph_style_t int))))
|
||||||
|
;;; "" "snd.h" "-I.")
|
||||||
|
;;;
|
||||||
|
;;;
|
||||||
|
;;; (c-define '(char* getcwd (char* size_t)) "" "unistd.h")
|
||||||
|
;;; :(let ((str (make-string 32))) (getcwd str 32) str)
|
||||||
|
;;; "/home/bil/cl\x00 "
|
||||||
|
;;; so it works in a sense -- there is a memory leak here
|
||||||
|
;;;
|
||||||
|
;;;
|
||||||
|
;;; (c-define (list '(time_t* calloc (size_t size_t))
|
||||||
|
;;; '(void free (time_t*))
|
||||||
|
;;; '(void time (time_t*))
|
||||||
|
;;; (list (symbol "struct tm*") 'localtime '(time_t*))
|
||||||
|
;;; (list 'size_t 'strftime (list 'char* 'size_t 'char* (symbol "struct tm*"))))
|
||||||
|
;;; "" "time.h")
|
||||||
|
;;; > (let ((p (calloc 1 8)) (str (make-string 32)))
|
||||||
|
;;; (time p) (let ((bytes (strftime str 32 "%a %d-%b-%Y %H:%M %Z" (localtime p)))) (free p) (substring str 0 bytes)))
|
||||||
|
;;; "Sat 11-Aug-2012 08:55 PDT
|
||||||
|
;;;
|
||||||
|
;;;
|
||||||
|
;;; (c-define '((int closedir (DIR*))
|
||||||
|
;;; (DIR* opendir (char*))
|
||||||
|
;;; (in-C "static char *read_dir(DIR *p) \
|
||||||
|
;;; { \
|
||||||
|
;;; struct dirent *dirp; \
|
||||||
|
;;; dirp = readdir(p); \
|
||||||
|
;;; if (!dirp) return(NULL); \
|
||||||
|
;;; else return(dirp->d_name); \
|
||||||
|
;;; }")
|
||||||
|
;;; (char* read_dir (DIR*)))
|
||||||
|
;;; "" '("sys/types.h" "dirent.h"))
|
||||||
|
;;;
|
||||||
|
;;; (define (memory-usage)
|
||||||
|
;;; (with-let *libc*
|
||||||
|
;;; (let ((v (rusage.make)))
|
||||||
|
;;; (getrusage RUSAGE_SELF v)
|
||||||
|
;;; (let ((mem (rusage.ru_maxrss v)))
|
||||||
|
;;; (free v)
|
||||||
|
;;; (* 1024 mem)))))
|
||||||
|
;;; --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(define *cload-cflags* (if (provided? 'clang) "-fPIC" ""))
|
||||||
|
(define *cload-ldflags* "")
|
||||||
|
(unless (defined? '*cload-directory*)
|
||||||
|
(define *cload-directory* ""))
|
||||||
|
(unless (defined? '*cload-library-name*)
|
||||||
|
(define *cload-library-name* #f))
|
||||||
|
|
||||||
|
(define *cload-c-compiler* ;(if (provided? 'tcc) ; how to get tcc to write a .so file?
|
||||||
|
;"tcc"
|
||||||
|
(if (and (provided? 'gcc)
|
||||||
|
(not (provided? 'openbsd)))
|
||||||
|
"gcc"
|
||||||
|
(if (provided? 'clang)
|
||||||
|
"clang"
|
||||||
|
"cc")));)
|
||||||
|
|
||||||
|
(define-macro (defvar name value)
|
||||||
|
`(if (not (defined? ',name))
|
||||||
|
(define ,name ,value)))
|
||||||
|
|
||||||
|
(defvar c-define-output-file-counter 0) ; ugly, but I can't find a way around this (dlopen/dlsym stupidity)
|
||||||
|
|
||||||
|
|
||||||
|
;;; to place the new function in the caller's current environment, we need to pass the environment in explicitly:
|
||||||
|
(define-macro (c-define . args)
|
||||||
|
(cons 'c-define-1 (cons '(curlet) args)))
|
||||||
|
|
||||||
|
|
||||||
|
(define* (c-define-1 cur-env function-info (prefix "") (headers ()) (cflags "") (ldflags "") output-name)
|
||||||
|
;; write a C shared library module that links in the functions in function-info
|
||||||
|
;; function info is either a list: (return-type c-name arg-type) or a list thereof
|
||||||
|
;; the new functions are placed in cur-env
|
||||||
|
|
||||||
|
(define handlers (list '(integer s7_is_integer s7_integer s7_make_integer s7_int)
|
||||||
|
'(boolean s7_is_boolean s7_boolean s7_make_boolean bool)
|
||||||
|
'(real s7_is_real s7_number_to_real_with_caller s7_make_real s7_double)
|
||||||
|
|
||||||
|
;; '(complex s7_is_complex #f s7_make_complex s7_Complex)
|
||||||
|
;; the typedef is around line 6116 in s7.c, but we also need s7_complex which requires the s7_Complex type
|
||||||
|
;; xen.h uses (s7_real_part(a) + s7_imag_part(a) * _Complex_I) instead since c++ won't let use define s7_Complex in s7.h
|
||||||
|
|
||||||
|
'(string s7_is_string s7_string s7_make_string char*)
|
||||||
|
(list 'character 's7_is_character 's7_character 's7_make_character (symbol "unsigned char"))
|
||||||
|
'(c_pointer s7_is_c_pointer s7_c_pointer s7_make_c_pointer_with_type void*)
|
||||||
|
'(s7_pointer #f #f #f s7_pointer)
|
||||||
|
))
|
||||||
|
|
||||||
|
(define (C-type->s7-type type)
|
||||||
|
|
||||||
|
(if (pair? type) ; in case the type name does not make its C type obvious: (graph_style_t int)
|
||||||
|
(C-type->s7-type (cadr type))
|
||||||
|
(let ((type-name (symbol->string type)))
|
||||||
|
(cond ((string-position "**" type-name) ; any complicated C pointer is uninterpreted
|
||||||
|
'c_pointer)
|
||||||
|
|
||||||
|
((string=? "s7_pointer" type-name)
|
||||||
|
's7_pointer)
|
||||||
|
|
||||||
|
((string-position "char*" type-name) ; but not char** (caught above)
|
||||||
|
'string)
|
||||||
|
|
||||||
|
((or (string-position "*" type-name) ; float* etc
|
||||||
|
(string-position "pointer" type-name))
|
||||||
|
'c_pointer)
|
||||||
|
|
||||||
|
((assoc type-name '(("char" . character)
|
||||||
|
("bool" . boolean)) string-position)
|
||||||
|
=> cdr)
|
||||||
|
|
||||||
|
;; ((string-position "complex" type-name)
|
||||||
|
;; 'complex) ; double complex or complex double (mus_edot_product in clm.c uses the latter)
|
||||||
|
|
||||||
|
((or (string-position "float" type-name)
|
||||||
|
(string-position "double" type-name))
|
||||||
|
'real)
|
||||||
|
|
||||||
|
((or (string-position "int" type-name)
|
||||||
|
(string-position "long" type-name) ; assuming not "long double" here so we need to look for it first (above)
|
||||||
|
(string-position "short" type-name)
|
||||||
|
(string-position "size" type-name)
|
||||||
|
(string-position "byte" type-name))
|
||||||
|
'integer)
|
||||||
|
|
||||||
|
(#t #t)))))
|
||||||
|
|
||||||
|
(define (find-handler type choice)
|
||||||
|
(cond ((assq (C-type->s7-type type) handlers) => choice) (else #t)))
|
||||||
|
|
||||||
|
(define (C->s7-cast type)
|
||||||
|
(find-handler type (lambda (p) (list-ref p 4))))
|
||||||
|
|
||||||
|
(define (C->s7 type)
|
||||||
|
(find-handler type cadddr))
|
||||||
|
|
||||||
|
(define (s7->C type)
|
||||||
|
(find-handler type caddr))
|
||||||
|
|
||||||
|
(define (checker type)
|
||||||
|
(find-handler type cadr))
|
||||||
|
|
||||||
|
(define (signature->pl type)
|
||||||
|
(case type
|
||||||
|
((integer?) #\i)
|
||||||
|
((boolean?) #\b)
|
||||||
|
((real?) #\r)
|
||||||
|
((float?) #\d)
|
||||||
|
((char?) #\c)
|
||||||
|
((string?) #\s)
|
||||||
|
((c-pointer?) #\x)
|
||||||
|
(else #\t)))
|
||||||
|
|
||||||
|
(set! c-define-output-file-counter (+ c-define-output-file-counter 1))
|
||||||
|
|
||||||
|
(let ((file-name (string-append *cload-directory*
|
||||||
|
(if (and (> (length *cload-directory*) 0)
|
||||||
|
(not (char=? (string-ref *cload-directory* (- (length *cload-directory*) 1)) #\/)))
|
||||||
|
"/" "")
|
||||||
|
(or output-name (format #f "temp-s7-output-~D" c-define-output-file-counter)))))
|
||||||
|
(let ((c-file-name (string-append file-name ".c"))
|
||||||
|
(o-file-name (string-append file-name ".o"))
|
||||||
|
(so-file-name (string-append file-name ".so"))
|
||||||
|
(init-name (if (string? output-name)
|
||||||
|
(string-append output-name "_init")
|
||||||
|
(string-append "init_" (number->string c-define-output-file-counter))))
|
||||||
|
(functions ())
|
||||||
|
(constants ())
|
||||||
|
(macros ()) ; these are protected by #ifdef ... #endif
|
||||||
|
(inits ()) ; C code (a string in s7) inserted in the library initialization function
|
||||||
|
(type-symbols ())
|
||||||
|
(p #f)
|
||||||
|
(pp (open-output-string))
|
||||||
|
(int-funcs ()) ; functions guaranteed to return int
|
||||||
|
(double-funcs ()) ; functions returning double, all args double
|
||||||
|
(double-int-funcs ()) ; functions return double, args are (integer double)
|
||||||
|
(sig-symbols (list (cons 'integer? 0) (cons 'boolean? 0) (cons 'real? 0) (cons 'float? 0)
|
||||||
|
(cons 'char? 0) (cons 'string? 0) (cons 'c-pointer? 0) (cons 't 0)))
|
||||||
|
(signatures (make-hash-table)))
|
||||||
|
|
||||||
|
(define make-signature
|
||||||
|
(let ((cload->signature
|
||||||
|
(lambda* (type rtn)
|
||||||
|
(case (C-type->s7-type type)
|
||||||
|
((real) (if rtn 'float? 'real?))
|
||||||
|
((integer) 'integer?)
|
||||||
|
((string) 'string?)
|
||||||
|
((boolean) 'boolean?)
|
||||||
|
((character) 'char?)
|
||||||
|
((c_pointer) 'c-pointer?)
|
||||||
|
(else #t)))))
|
||||||
|
(lambda (rtn args)
|
||||||
|
(let ((sig (list (cload->signature rtn #t)))
|
||||||
|
(cyclic #f))
|
||||||
|
(for-each
|
||||||
|
(lambda (arg)
|
||||||
|
(set! sig (cons (cload->signature arg) sig)))
|
||||||
|
args)
|
||||||
|
(let ((len (length sig)))
|
||||||
|
(set! sig (do ((sig sig (cdr sig)))
|
||||||
|
((not (and (pair? sig)
|
||||||
|
(pair? (cdr sig))
|
||||||
|
(eq? (car sig) (cadr sig))))
|
||||||
|
sig)))
|
||||||
|
(set! cyclic (not (= len (length sig)))))
|
||||||
|
(set! sig (cons cyclic (reverse sig))) ; need to include cyclic in key else trailing same-type args are dropped from the signature
|
||||||
|
(unless (signatures sig) ; it's not in our collection yet
|
||||||
|
(let ((pl (make-string (+ (if cyclic 3 2) (length sig))))
|
||||||
|
(loc (if cyclic 4 3)))
|
||||||
|
(set! (pl 0) #\p)
|
||||||
|
(if cyclic
|
||||||
|
(begin (set! (pl 1) #\c) (set! (pl 2) #\l) (set! (pl 3) #\_))
|
||||||
|
(begin (set! (pl 1) #\l) (set! (pl 2) #\_)))
|
||||||
|
(for-each
|
||||||
|
(lambda (typer)
|
||||||
|
(set! (pl loc) (signature->pl typer))
|
||||||
|
(let ((count (or (assq typer sig-symbols)
|
||||||
|
(assq 't sig-symbols))))
|
||||||
|
(set-cdr! count (+ (cdr count) 1)))
|
||||||
|
(set! loc (+ loc 1)))
|
||||||
|
(cdr sig))
|
||||||
|
(set! (signatures sig) pl)))
|
||||||
|
sig))))
|
||||||
|
|
||||||
|
(define (initialize-c-file)
|
||||||
|
;; C header stuff
|
||||||
|
(set! p (open-output-file c-file-name))
|
||||||
|
(format p "#include <stdlib.h>~%")
|
||||||
|
(format p "#include <stdio.h>~%")
|
||||||
|
(format p "#include <string.h>~%")
|
||||||
|
(if (string? headers)
|
||||||
|
(format p "#include <~A>~%" headers)
|
||||||
|
(for-each
|
||||||
|
(lambda (header)
|
||||||
|
(format p "#include <~A>~%" header))
|
||||||
|
headers))
|
||||||
|
(format p "#include \"s7.h\"~%~%")
|
||||||
|
(format p "static s7_pointer c_pointer_string, string_string, character_string, boolean_string, real_string, complex_string, integer_string;~%"))
|
||||||
|
|
||||||
|
(define collides?
|
||||||
|
(let ((all-names (hash-table)))
|
||||||
|
(lambda (name)
|
||||||
|
(if (hash-table-ref all-names name)
|
||||||
|
(format *stderr* "~A twice?~%" name)
|
||||||
|
(hash-table-set! all-names name #t))
|
||||||
|
name)))
|
||||||
|
|
||||||
|
(define (hyphen->space type)
|
||||||
|
(let* ((name (symbol->string type))
|
||||||
|
(pos (char-position #\- name)))
|
||||||
|
(when pos
|
||||||
|
(string-set! name pos #\space)
|
||||||
|
(set! pos (char-position #\- name (+ pos 1)))
|
||||||
|
(if pos
|
||||||
|
(string-set! name pos #\space)))
|
||||||
|
name))
|
||||||
|
|
||||||
|
(define (type->type-symbol type)
|
||||||
|
(cond ((assoc type type-symbols) => cdr)
|
||||||
|
(else
|
||||||
|
(let ((name (symbol->string type)))
|
||||||
|
(do ((len (length name))
|
||||||
|
(i 0 (+ i 1)))
|
||||||
|
((= i len))
|
||||||
|
(if (memv (name i) '(#\* #\- #\space)) ; "struct tm*" for example
|
||||||
|
(set! (name i) #\_)))
|
||||||
|
(set! name (symbol name "_symbol"))
|
||||||
|
(set! type-symbols (cons (cons type name) type-symbols))
|
||||||
|
name))))
|
||||||
|
|
||||||
|
(define add-one-function
|
||||||
|
(let ()
|
||||||
|
(define (sig-every? f sequence)
|
||||||
|
(do ((arg sequence (cdr arg)))
|
||||||
|
((not (and (pair? arg)
|
||||||
|
(f (car arg))))
|
||||||
|
(null? arg))))
|
||||||
|
|
||||||
|
(lambda* (return-type name arg-types doc)
|
||||||
|
;; (format *stderr* "~A ~A ~A~%" return-type name arg-types): double j0 (double) for example
|
||||||
|
;; C function -> scheme
|
||||||
|
(let ((func-name (symbol->string (collides? name))))
|
||||||
|
(let ((num-args (length arg-types))
|
||||||
|
(base-name (string-append (if (> (length prefix) 0) prefix "s7_") "_" func-name)) ; not "g" -- collides with glib
|
||||||
|
(scheme-name (string-append prefix (if (> (length prefix) 0) ":" "") func-name)))
|
||||||
|
|
||||||
|
(if (and (= num-args 1)
|
||||||
|
(eq? (car arg-types) 'void))
|
||||||
|
(set! num-args 0))
|
||||||
|
(format pp "~%/* -------- ~A -------- */~%" func-name)
|
||||||
|
(format pp "static s7_pointer ~A(s7_scheme *sc, s7_pointer args)~%" base-name)
|
||||||
|
(format pp "{~%")
|
||||||
|
|
||||||
|
;; get the Scheme args, check their types, assign to local C variables
|
||||||
|
(when (positive? num-args)
|
||||||
|
(format pp " s7_pointer p, arg;~%")
|
||||||
|
(do ((i 0 (+ i 1))
|
||||||
|
(type arg-types (cdr type)))
|
||||||
|
((= i num-args))
|
||||||
|
(format pp " ~A ~A_~D;~%" (hyphen->space ((if (pair? (car type)) caar car) type)) base-name i))
|
||||||
|
(format pp " p = args;~%")
|
||||||
|
(do ((i 0 (+ i 1))
|
||||||
|
(type arg-types (cdr type)))
|
||||||
|
((= i num-args))
|
||||||
|
(let* ((nominal-type ((if (pair? (car type)) caar car) type)) ; double in the example
|
||||||
|
(true-type ((if (pair? (car type)) cadar car) type))
|
||||||
|
(s7-type (C-type->s7-type true-type))) ; real
|
||||||
|
(format pp " arg = s7_car(p);~%")
|
||||||
|
(if (eq? true-type 's7_pointer)
|
||||||
|
(format pp " ~A_~D = arg;~%" base-name i)
|
||||||
|
(if (eq? s7-type 'c_pointer)
|
||||||
|
(format pp " ~A_~D = (~A)s7_c_pointer_with_type(sc, arg, ~S, __func__, ~S);~%"
|
||||||
|
base-name i
|
||||||
|
(hyphen->space nominal-type)
|
||||||
|
(type->type-symbol nominal-type) ;(symbol->string nominal-type)
|
||||||
|
(if (= num-args 1) 0 (+ i 1)))
|
||||||
|
|
||||||
|
(let ((function-name (if *cload-library-name*
|
||||||
|
(string-append "(" *cload-library-name* " '" scheme-name ")")
|
||||||
|
(if (> (length prefix) 0)
|
||||||
|
scheme-name
|
||||||
|
(substring base-name 4)))))
|
||||||
|
(format pp " if (~A(arg))~%" (checker true-type))
|
||||||
|
(format pp " ~A_~D = (~A)~A(~Aarg~A);~%"
|
||||||
|
base-name i
|
||||||
|
(hyphen->space nominal-type)
|
||||||
|
(s7->C true-type) ; s7_number_to_real which requires
|
||||||
|
(if (memq s7-type '(boolean real)) ; the extra sc arg
|
||||||
|
"sc, "
|
||||||
|
"")
|
||||||
|
(if (memq s7-type '(boolean real)) ; the extra trailing caller arg
|
||||||
|
", __func__"
|
||||||
|
""))
|
||||||
|
(format pp " else return(s7_wrong_type_error(sc, s7_make_string_wrapper_with_length(sc, ~S, ~D), ~D, arg, ~A_string));~%"
|
||||||
|
function-name
|
||||||
|
(length function-name)
|
||||||
|
(if (= num-args 1) 0 (+ i 1))
|
||||||
|
s7-type))))
|
||||||
|
|
||||||
|
(if (< i (- num-args 1))
|
||||||
|
(format pp " p = s7_cdr(p);~%")))))
|
||||||
|
|
||||||
|
;; return C value to Scheme
|
||||||
|
(if (pair? return-type)
|
||||||
|
(set! return-type (cadr return-type)))
|
||||||
|
(let ((return-translator (C->s7 return-type)))
|
||||||
|
(format pp " ")
|
||||||
|
(if (not (eq? return-translator #t))
|
||||||
|
(format pp "return("))
|
||||||
|
(if (symbol? return-translator)
|
||||||
|
(format pp "~A(sc, (~A)" return-translator (C->s7-cast return-type)))
|
||||||
|
(format pp "~A(" func-name)
|
||||||
|
(do ((i 0 (+ i 1)))
|
||||||
|
((>= i (- num-args 1)))
|
||||||
|
(format pp "~A_~D, " base-name i))
|
||||||
|
(if (positive? num-args)
|
||||||
|
(format pp "~A_~D" base-name (- num-args 1)))
|
||||||
|
(format pp ")")
|
||||||
|
|
||||||
|
(if (eq? return-translator 's7_make_c_pointer_with_type)
|
||||||
|
(format pp ", ~S, s7_f(sc))" (type->type-symbol return-type))
|
||||||
|
(if (symbol? return-translator)
|
||||||
|
(format pp ")")))
|
||||||
|
(format pp (if (not (eq? return-translator #t))
|
||||||
|
");~%"
|
||||||
|
";~% return(s7_unspecified(sc));~%"))
|
||||||
|
(format pp "}~%"))
|
||||||
|
|
||||||
|
;; add optimizer connection
|
||||||
|
(when (and (eq? return-type 'double)
|
||||||
|
(< num-args 5)
|
||||||
|
(sig-every? (lambda (p) (eq? p 'double)) arg-types)
|
||||||
|
(not (defined? (symbol scheme-name) rootlet))) ; see below, double-funcs entry not used if already defined
|
||||||
|
(let ((local-name #f))
|
||||||
|
(case num-args
|
||||||
|
((0)
|
||||||
|
(set! local-name "_d")
|
||||||
|
(format pp "static s7_double ~A~A(void) {return(~A());}~%"
|
||||||
|
func-name local-name func-name))
|
||||||
|
((1)
|
||||||
|
(set! local-name "_d_d")
|
||||||
|
(format pp "static s7_double ~A~A(s7_double x) {return(~A(x));}~%"
|
||||||
|
func-name local-name func-name))
|
||||||
|
((2)
|
||||||
|
(set! local-name "_d_dd")
|
||||||
|
(format pp "static s7_double ~A~A(s7_double x1, s7_double x2) {return(~A(x1, x2));}~%"
|
||||||
|
func-name local-name func-name))
|
||||||
|
((3)
|
||||||
|
(set! local-name "_d_ddd")
|
||||||
|
(format pp "static s7_double ~A~A(s7_double x1, s7_double x2, s7_double x3) {return(~A(x1, x2, x3));}~%"
|
||||||
|
func-name local-name func-name))
|
||||||
|
((4)
|
||||||
|
(set! local-name "_d_dddd")
|
||||||
|
(format pp "static s7_double ~A~A(s7_double x1, s7_double x2, s7_double x3, s7_double x4) {return(~A(x1, x2, x3, x4));}~%"
|
||||||
|
func-name local-name func-name)))
|
||||||
|
(set! double-funcs (cons (list func-name scheme-name local-name) double-funcs))))
|
||||||
|
|
||||||
|
(when (and (memq return-type '(int size_t)) ; int (f int|double|void)
|
||||||
|
(not (defined? (symbol scheme-name) rootlet)) ; see below, int-funcs entry not used if already defined
|
||||||
|
(or ;(= num-args 0)
|
||||||
|
(and (= num-args 1)
|
||||||
|
(memq (car arg-types) '(int size_t double)))
|
||||||
|
(and (= num-args 2)
|
||||||
|
(memq (car arg-types) '(int size_t))
|
||||||
|
(memq (cadr arg-types) '(int size_t)))))
|
||||||
|
(let ((local-name #f))
|
||||||
|
(case (car arg-types)
|
||||||
|
((void)
|
||||||
|
(set! local-name "_i")
|
||||||
|
(format pp "static s7_int ~A~A(void) {return(~A());}~%" func-name local-name func-name))
|
||||||
|
((double)
|
||||||
|
(set! local-name "_i_7d")
|
||||||
|
(format pp "static s7_int ~A~A(s7_scheme *sc, s7_double x) {return(~A(x));}~%" func-name local-name func-name))
|
||||||
|
((int size_t)
|
||||||
|
(if (= num-args 1)
|
||||||
|
(begin
|
||||||
|
(set! local-name "_i_i")
|
||||||
|
(format pp "static s7_int ~A~A(s7_int i1) {return(~A(i1));}~%" func-name local-name (if (string=? func-name "abs") "llabs" func-name)))
|
||||||
|
(begin
|
||||||
|
(set! local-name "_i_ii")
|
||||||
|
(format pp "static s7_int ~A~A(s7_int i1, s7_int i2) {return(~A(i1, i2));}~%" func-name local-name func-name)))))
|
||||||
|
(set! int-funcs (cons (list func-name scheme-name local-name) int-funcs))))
|
||||||
|
|
||||||
|
(when (and (eq? return-type 'double)
|
||||||
|
(= num-args 2)
|
||||||
|
(memq (car arg-types) '(int size_t))
|
||||||
|
(eq? (cadr arg-types) 'double))
|
||||||
|
(format pp "static s7_double ~A~A(s7_int x1, s7_double x2) {return(~A(x1, x2));}~%" func-name "_d_id" func-name)
|
||||||
|
(set! double-int-funcs (cons (list func-name scheme-name "_d_id") double-int-funcs)))
|
||||||
|
|
||||||
|
;; other possibilities: d_7pi|pii p=double* etc piid=checks in s7 (assumes float-vector)
|
||||||
|
;; d_pd [lots of d_pdd, d_p, p_i and i_p]
|
||||||
|
;; but how to recognize the "p" portions? (d_7pi with p="s7_pointer" gets no hits in libgsl)
|
||||||
|
|
||||||
|
(format pp "~%")
|
||||||
|
(set! functions (cons (list scheme-name base-name
|
||||||
|
(if (and (string? doc)
|
||||||
|
(> (length doc) 0))
|
||||||
|
doc
|
||||||
|
(format #f "~A ~A~A" return-type func-name arg-types))
|
||||||
|
num-args 0
|
||||||
|
(make-signature return-type arg-types))
|
||||||
|
functions)))))))
|
||||||
|
|
||||||
|
(define (end-c-file)
|
||||||
|
(when (pair? type-symbols)
|
||||||
|
(format p "static s7_pointer ")
|
||||||
|
(let ((len (length type-symbols))
|
||||||
|
(loc 1))
|
||||||
|
(for-each
|
||||||
|
(lambda (sym)
|
||||||
|
(format p "~A~A~A" (cdr sym) (if (< loc len) (values "," " ") (values ";" #\newline)))
|
||||||
|
(set! loc (+ loc 1)))
|
||||||
|
type-symbols)))
|
||||||
|
(newline p)
|
||||||
|
|
||||||
|
(display (get-output-string pp) p)
|
||||||
|
(close-output-port pp)
|
||||||
|
|
||||||
|
;; now the init function
|
||||||
|
;; the new scheme variables and functions are placed in the current environment
|
||||||
|
|
||||||
|
(format p "void ~A(s7_scheme *sc);~%" init-name)
|
||||||
|
(format p "void ~A(s7_scheme *sc)~%" init-name)
|
||||||
|
(format p "{~%")
|
||||||
|
(format p " s7_pointer cur_env;~%")
|
||||||
|
|
||||||
|
(when (> (hash-table-entries signatures) 0) ; maybe just constants (no functions)
|
||||||
|
(format p " s7_pointer ")
|
||||||
|
(let ((pls (hash-table-entries signatures))
|
||||||
|
(loc 1))
|
||||||
|
(for-each
|
||||||
|
(lambda (s)
|
||||||
|
(format p "~A~A~A" (cdr s) (if (< loc pls) (values "," " ") (values ";" #\newline)))
|
||||||
|
(set! loc (+ loc 1)))
|
||||||
|
signatures))
|
||||||
|
|
||||||
|
(let ((syms ())
|
||||||
|
(names ()))
|
||||||
|
(for-each
|
||||||
|
(lambda (q)
|
||||||
|
(when (positive? (cdr q))
|
||||||
|
(set! syms (cons (car q) syms))
|
||||||
|
(set! names (cons (signature->pl (car q)) names))))
|
||||||
|
sig-symbols)
|
||||||
|
(when (pair? syms)
|
||||||
|
(format p " {~% s7_pointer ~{~C~^, ~};~%" names)
|
||||||
|
(for-each
|
||||||
|
(lambda (name sym)
|
||||||
|
(format p (if (eq? sym 't)
|
||||||
|
" t = s7_t(sc);~%"
|
||||||
|
(values " ~C = s7_make_symbol(sc, ~S);~%" name (symbol->string sym)))))
|
||||||
|
names syms)))
|
||||||
|
(format p "~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (sig)
|
||||||
|
(let ((siglen (length (cdar sig)))
|
||||||
|
(cyclic (char=? ((cdr sig) 1) #\c)))
|
||||||
|
(format p (if cyclic
|
||||||
|
(values " ~A = s7_make_circular_signature(sc, ~D, ~D" (cdr sig) (- siglen 1) siglen)
|
||||||
|
(values " ~A = s7_make_signature(sc, ~D" (cdr sig) siglen)))
|
||||||
|
(format p "~{~^, ~C~}" (substring (cdr sig) (if cyclic 4 3)))
|
||||||
|
(format p ");~%")))
|
||||||
|
signatures)
|
||||||
|
(format p " }~%~%"))
|
||||||
|
|
||||||
|
(format p " string_string = s7_make_semipermanent_string(sc, \"a string\");~%")
|
||||||
|
(format p " c_pointer_string = s7_make_semipermanent_string(sc, \"a c-pointer\");~%")
|
||||||
|
(format p " character_string = s7_make_semipermanent_string(sc, \"a character\");~%")
|
||||||
|
(format p " boolean_string = s7_make_semipermanent_string(sc, \"a boolean\");~%")
|
||||||
|
(format p " real_string = s7_make_semipermanent_string(sc, \"a real\");~%")
|
||||||
|
(format p " complex_string = s7_make_semipermanent_string(sc, \"a complex number\");~%")
|
||||||
|
(format p " integer_string = s7_make_semipermanent_string(sc, \"an integer\");~%")
|
||||||
|
|
||||||
|
(format p " cur_env = s7_curlet(sc);~%") ; changed from s7_outlet(s7_curlet) 20-Aug-17
|
||||||
|
|
||||||
|
;; send out any special initialization code
|
||||||
|
(for-each
|
||||||
|
(lambda (init-str)
|
||||||
|
(format p " ~A~%" init-str))
|
||||||
|
(reverse inits))
|
||||||
|
|
||||||
|
(when (pair? type-symbols)
|
||||||
|
(newline p)
|
||||||
|
(for-each
|
||||||
|
(lambda (sym)
|
||||||
|
(format p " ~S = s7_make_symbol(sc, ~S);~%" (cdr sym) (symbol->string (car sym))))
|
||||||
|
type-symbols))
|
||||||
|
|
||||||
|
;; constants
|
||||||
|
(when (pair? constants)
|
||||||
|
(format p "~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (c)
|
||||||
|
(let* ((type (c 0))
|
||||||
|
(c-name (c 1))
|
||||||
|
(scheme-name (string-append prefix (if (> (length prefix) 0) ":" "") c-name))
|
||||||
|
(trans (C->s7 type)))
|
||||||
|
(if (eq? trans 's7_make_c_pointer_with_type)
|
||||||
|
(format p " s7_define(sc, cur_env, s7_make_symbol(sc, ~S), ~A(sc, (~A)~A, s7_make_symbol(sc, ~S), s7_f(sc)));~%"
|
||||||
|
scheme-name
|
||||||
|
trans
|
||||||
|
(C->s7-cast type)
|
||||||
|
c-name
|
||||||
|
(if (eq? type 'c-pointer) "void*" (symbol->string type)))
|
||||||
|
(format p " s7_define(sc, cur_env, s7_make_symbol(sc, ~S), ~A(sc, (~A)~A));~%"
|
||||||
|
scheme-name
|
||||||
|
trans
|
||||||
|
(C->s7-cast type)
|
||||||
|
c-name))))
|
||||||
|
constants))
|
||||||
|
|
||||||
|
;; C macros -- need #ifdef name #endif wrapper
|
||||||
|
(when (pair? macros)
|
||||||
|
(format p "~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (c)
|
||||||
|
(let* ((type (c 0))
|
||||||
|
(c-name (c 1))
|
||||||
|
(scheme-name (string-append prefix (if (> (length prefix) 0) ":" "") c-name))
|
||||||
|
(trans (C->s7 type)))
|
||||||
|
(format p "#ifdef ~A~%" c-name)
|
||||||
|
(if (eq? trans 's7_make_c_pointer_with_type)
|
||||||
|
(format p " s7_define(sc, cur_env, s7_make_symbol(sc, ~S), s7_make_c_pointer_with_type(sc, (~A)~A, s7_make_symbol(sc, \"~S\"), s7_f(sc)));~%"
|
||||||
|
scheme-name
|
||||||
|
(C->s7-cast type)
|
||||||
|
c-name
|
||||||
|
type)
|
||||||
|
(format p " s7_define(sc, cur_env, s7_make_symbol(sc, ~S), ~A(sc, (~A)~A));~%"
|
||||||
|
scheme-name
|
||||||
|
trans
|
||||||
|
(C->s7-cast type)
|
||||||
|
c-name))
|
||||||
|
(format p "#endif~%")))
|
||||||
|
macros))
|
||||||
|
|
||||||
|
;; functions
|
||||||
|
(for-each
|
||||||
|
(lambda (f)
|
||||||
|
(let ((scheme-name (f 0))
|
||||||
|
(base-name (f 1))
|
||||||
|
(helpf (f 2))
|
||||||
|
(num-args (f 3))
|
||||||
|
(opt-args (if (> (length f) 4) (f 4) 0))
|
||||||
|
(sig (and (> (length f) 5) (f 5))))
|
||||||
|
(format p "~% s7_define(sc, cur_env,~% s7_make_symbol(sc, ~S),~%" scheme-name)
|
||||||
|
(format p " s7_make_typed_function(sc, ~S, ~A, ~D, ~D, false, ~S, ~A));~%"
|
||||||
|
scheme-name
|
||||||
|
base-name
|
||||||
|
num-args
|
||||||
|
opt-args
|
||||||
|
helpf
|
||||||
|
(if (pair? sig) (signatures sig) 'NULL))))
|
||||||
|
functions)
|
||||||
|
|
||||||
|
;; optimizer connection
|
||||||
|
(when (pair? double-funcs)
|
||||||
|
(format p "~% /* double optimizer connections */~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (f)
|
||||||
|
(unless (defined? (symbol (cadr f)) (rootlet))
|
||||||
|
(format p " s7_set~A_function(sc, s7_name_to_value(sc, ~S), ~A~A);~%" (caddr f) (cadr f) (car f) (caddr f))))
|
||||||
|
double-funcs))
|
||||||
|
|
||||||
|
(when (pair? int-funcs)
|
||||||
|
(format p "~% /* int optimizer connections */~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (f)
|
||||||
|
(unless (defined? (symbol (cadr f)) (rootlet))
|
||||||
|
(format p " s7_set~A_function(sc, s7_name_to_value(sc, ~S), ~A~A);~%" (caddr f) (cadr f) (car f) (caddr f))))
|
||||||
|
int-funcs))
|
||||||
|
|
||||||
|
(when (pair? double-int-funcs)
|
||||||
|
(format p "~% /* double-int optimizer connections */~%")
|
||||||
|
(for-each
|
||||||
|
(lambda (f)
|
||||||
|
(unless (defined? (symbol (cadr f)) (rootlet))
|
||||||
|
(format p " s7_set~A_function(sc, s7_name_to_value(sc, ~S), ~A~A);~%" (caddr f) (cadr f) (car f) (caddr f))))
|
||||||
|
double-int-funcs))
|
||||||
|
|
||||||
|
(format p "}~%")
|
||||||
|
(close-output-port p)
|
||||||
|
|
||||||
|
(unless (or (file-exists? "s7.h")
|
||||||
|
(not (pair? *load-path*)))
|
||||||
|
(set! *cload-cflags* (append *cload-cflags* (format #f " -I~A" (car *load-path*)))))
|
||||||
|
|
||||||
|
;; now we have the module .c file -- make it into a shared object
|
||||||
|
|
||||||
|
(cond ((provided? 'osx)
|
||||||
|
;; I assume the caller is also compiled with these flags?
|
||||||
|
(system (format #f "~A -c ~A -o ~A ~A ~A"
|
||||||
|
*cload-c-compiler* c-file-name o-file-name *cload-cflags* cflags))
|
||||||
|
(system (format #f "~A ~A -o ~A -dynamic -bundle -undefined suppress -flat_namespace ~A ~A"
|
||||||
|
*cload-c-compiler* o-file-name so-file-name *cload-ldflags* ldflags)))
|
||||||
|
|
||||||
|
((provided? 'freebsd)
|
||||||
|
(system (format #f "cc -fPIC -c ~A -o ~A ~A ~A"
|
||||||
|
c-file-name o-file-name *cload-cflags* cflags))
|
||||||
|
(system (format #f "cc ~A -shared -o ~A ~A ~A"
|
||||||
|
o-file-name so-file-name *cload-ldflags* ldflags)))
|
||||||
|
|
||||||
|
((provided? 'openbsd)
|
||||||
|
(system (format #f "~A -fPIC -c ~A -o ~A ~A ~A"
|
||||||
|
*cload-c-compiler* c-file-name o-file-name *cload-cflags* cflags))
|
||||||
|
(system (format #f "~A ~A -shared -o ~A ~A ~A"
|
||||||
|
*cload-c-compiler* o-file-name so-file-name *cload-ldflags* ldflags)))
|
||||||
|
|
||||||
|
((provided? 'sunpro_c) ; just guessing here...
|
||||||
|
(system (format #f "cc -c ~A -o ~A ~A ~A"
|
||||||
|
c-file-name o-file-name *cload-cflags* cflags))
|
||||||
|
(system (format #f "cc ~A -G -o ~A ~A ~A"
|
||||||
|
o-file-name so-file-name *cload-ldflags* ldflags)))
|
||||||
|
|
||||||
|
((or (provided? 'mingw) (provided? 'msys2)) ; from chai xiaoxiang
|
||||||
|
;; you'll need dlfcn which can be installed with pacman, and remember to build s7 with -DWITH_C_LOADER=1
|
||||||
|
;; in msys2: gcc s7.c -o s7 -DWITH_MAIN -DWITH_C_LOADER=1 -I. -O2 -g -ldl -lm -Wl,-export-all-symbols,--out-implib,s7.lib
|
||||||
|
(system (format #f "gcc ~A s7.lib -shared -o ~A -I. ~A ~A"
|
||||||
|
c-file-name so-file-name cflags ldflags)))
|
||||||
|
|
||||||
|
(else ; linux netbsd
|
||||||
|
(system (format #f "~A -fPIC -c ~A -o ~A ~A ~A"
|
||||||
|
*cload-c-compiler* c-file-name o-file-name *cload-cflags* cflags))
|
||||||
|
(system (format #f "~A ~A -shared -o ~A ~A ~A"
|
||||||
|
*cload-c-compiler* o-file-name so-file-name *cload-ldflags* ldflags)))))
|
||||||
|
|
||||||
|
(define handle-declaration
|
||||||
|
(let ()
|
||||||
|
(define (add-one-constant type name)
|
||||||
|
;; C constant -> scheme
|
||||||
|
(let ((c-type (if (pair? type) (cadr type) type)))
|
||||||
|
(if (symbol? name)
|
||||||
|
(set! constants (cons (list c-type (symbol->string (collides? name))) constants))
|
||||||
|
(for-each
|
||||||
|
(lambda (c)
|
||||||
|
(set! constants (cons (list c-type (symbol->string (collides? c))) constants)))
|
||||||
|
name))))
|
||||||
|
|
||||||
|
(define (add-one-macro type name)
|
||||||
|
;; C macro (with definition check) -> scheme
|
||||||
|
(let ((c-type (if (pair? type) (cadr type) type)))
|
||||||
|
(if (symbol? name)
|
||||||
|
(set! macros (cons (list c-type (symbol->string (collides? name))) macros))
|
||||||
|
(for-each
|
||||||
|
(lambda (c)
|
||||||
|
(set! macros (cons (list c-type (symbol->string (collides? c))) macros)))
|
||||||
|
name))))
|
||||||
|
|
||||||
|
(define (check-doc func-data)
|
||||||
|
(let ((doc (caddr func-data)))
|
||||||
|
(if (and (string? doc)
|
||||||
|
(> (length doc) 0))
|
||||||
|
func-data
|
||||||
|
(append (list (car func-data) (cadr func-data) (car func-data)) (cdddr func-data)))))
|
||||||
|
|
||||||
|
(lambda (func)
|
||||||
|
;; functions
|
||||||
|
(if (pair? func)
|
||||||
|
(if (>= (length func) 3)
|
||||||
|
(apply add-one-function func)
|
||||||
|
(case (car func)
|
||||||
|
((in-C) (format pp "~A~%" (cadr func)))
|
||||||
|
((C-init) (set! inits (cons (cadr func) inits)))
|
||||||
|
((C-macro) (apply add-one-macro (cadr func)))
|
||||||
|
((C-function) (collides? (caadr func)) (set! functions (cons (check-doc (cadr func)) functions)))
|
||||||
|
(else (apply add-one-constant func))))
|
||||||
|
(error 'wrong-type-arg "~S (func arg to handle-declaration in cload.scm) should be a pair" func)))))
|
||||||
|
|
||||||
|
|
||||||
|
;; c-define-1 (called in c-define macro above)
|
||||||
|
(unless (and output-name
|
||||||
|
(file-exists? c-file-name)
|
||||||
|
(file-exists? so-file-name)
|
||||||
|
(provided? 'system-extras)
|
||||||
|
(>= (file-mtime so-file-name) (file-mtime c-file-name))
|
||||||
|
(not (and (file-exists? (port-filename))
|
||||||
|
(< (file-mtime so-file-name) (file-mtime (port-filename))))))
|
||||||
|
(format *stderr* "writing ~A~%" c-file-name)
|
||||||
|
;; write a new C file and compile it
|
||||||
|
(initialize-c-file)
|
||||||
|
|
||||||
|
(if (and (pair? (cdr function-info))
|
||||||
|
(symbol? (cadr function-info)))
|
||||||
|
(handle-declaration function-info)
|
||||||
|
(for-each handle-declaration function-info))
|
||||||
|
|
||||||
|
(end-c-file)
|
||||||
|
(delete-file o-file-name))
|
||||||
|
|
||||||
|
;; load the object file, clean up
|
||||||
|
(varlet cur-env 'init_func (string->symbol init-name))
|
||||||
|
(format *stderr* "loading ~A~%" so-file-name)
|
||||||
|
(load so-file-name cur-env))))
|
||||||
|
|
||||||
|
|
||||||
|
#|
|
||||||
|
(let ((cd (symbol "complex double"))
|
||||||
|
(cd* (symbol "complex double *")))
|
||||||
|
(c-define (list cd 'mus_edot_product (list cd cd* 'int))))
|
||||||
|
|
||||||
|
;complex double mus_edot_product(complex double freq, complex double *data, mus_long_t size)
|
||||||
|
|#
|
||||||
+137
@@ -0,0 +1,137 @@
|
|||||||
|
; enf.scm -- pure Scheme enfilade reference
|
||||||
|
(define (make-enfilade) (vector 'enfilade '() '()))
|
||||||
|
(define (enfilade-children e) (vector-ref e 1))
|
||||||
|
(define (enfilade-values e) (vector-ref e 2))
|
||||||
|
(define (set-enfilade-children! e x) (vector-set! e 1 x))
|
||||||
|
(define (set-enfilade-values! e x) (vector-set! e 2 x))
|
||||||
|
|
||||||
|
(define (assoc-number k xs)
|
||||||
|
(cond ((null? xs) #f)
|
||||||
|
((= k (caar xs)) (car xs))
|
||||||
|
(else (assoc-number k (cdr xs)))))
|
||||||
|
|
||||||
|
(define (alist-put xs k v)
|
||||||
|
(let ((p (assoc-number k xs)))
|
||||||
|
(if p (begin (set-cdr! p v) xs) (cons (cons k v) xs))))
|
||||||
|
|
||||||
|
(define (alist-remove xs k)
|
||||||
|
(cond ((null? xs) '())
|
||||||
|
((= k (caar xs)) (cdr xs))
|
||||||
|
(else (cons (car xs) (alist-remove (cdr xs) k)))))
|
||||||
|
|
||||||
|
(define (insert-number n xs)
|
||||||
|
(cond ((null? xs) (list n))
|
||||||
|
((<= n (car xs)) (cons n xs))
|
||||||
|
(else (cons (car xs) (insert-number n (cdr xs))))))
|
||||||
|
|
||||||
|
(define (sort-numbers xs)
|
||||||
|
(if (null? xs) '()
|
||||||
|
(insert-number (car xs) (sort-numbers (cdr xs)))))
|
||||||
|
|
||||||
|
(define (unique-numbers xs)
|
||||||
|
(cond ((null? xs) '())
|
||||||
|
((and (pair? (cdr xs)) (= (car xs) (cadr xs)))
|
||||||
|
(unique-numbers (cdr xs)))
|
||||||
|
(else (cons (car xs) (unique-numbers (cdr xs))))))
|
||||||
|
|
||||||
|
(define (tumbler-parse s)
|
||||||
|
(if (= (string-length s) 0) '()
|
||||||
|
(let loop ((i 0) (start 0) (out '()))
|
||||||
|
(if (= i (string-length s))
|
||||||
|
(reverse (cons (string->number (substring s start i)) out))
|
||||||
|
(if (char=? (string-ref s i) #\.)
|
||||||
|
(loop (+ i 1) (+ i 1)
|
||||||
|
(cons (string->number (substring s start i)) out))
|
||||||
|
(loop (+ i 1) start out))))))
|
||||||
|
|
||||||
|
(define (tumbler->list t)
|
||||||
|
(cond ((string? t) (tumbler-parse t))
|
||||||
|
((list? t) t)
|
||||||
|
(else (error 'wrong-type-arg "tumbler must be a string or list"))))
|
||||||
|
|
||||||
|
(define (tumbler-compare a b)
|
||||||
|
(let loop ((a (tumbler->list a)) (b (tumbler->list b)))
|
||||||
|
(cond ((and (null? a) (null? b)) 0)
|
||||||
|
((null? a) -1)
|
||||||
|
((null? b) 1)
|
||||||
|
((< (car a) (car b)) -1)
|
||||||
|
((> (car a) (car b)) 1)
|
||||||
|
(else (loop (cdr a) (cdr b))))))
|
||||||
|
|
||||||
|
(define (enfilade-get e key)
|
||||||
|
(let ((v (assoc-number key (enfilade-values e)))
|
||||||
|
(c (assoc-number key (enfilade-children e))))
|
||||||
|
(append (if v (list (cdr v)) '())
|
||||||
|
(if c (enfilade-get-range (cdr c)) '()))))
|
||||||
|
|
||||||
|
(define (enfilade-get-by-tumbler e t)
|
||||||
|
(let loop ((node e) (parts (tumbler->list t)))
|
||||||
|
(cond ((null? parts) (enfilade-get-range node))
|
||||||
|
((null? (cdr parts)) (enfilade-get node (car parts)))
|
||||||
|
(else
|
||||||
|
(let ((c (assoc-number (car parts) (enfilade-children node))))
|
||||||
|
(if c (loop (cdr c) (cdr parts)) '()))))))
|
||||||
|
|
||||||
|
(define (enfilade-put-data e t datum)
|
||||||
|
(let ((parts (tumbler->list t)))
|
||||||
|
(if (null? parts) (error 'out-of-range "empty tumbler"))
|
||||||
|
(let loop ((node e) (parts parts))
|
||||||
|
(if (null? (cdr parts))
|
||||||
|
(set-enfilade-values!
|
||||||
|
node (alist-put (enfilade-values node) (car parts) datum))
|
||||||
|
(let* ((k (car parts))
|
||||||
|
(p (assoc-number k (enfilade-children node)))
|
||||||
|
(child (if p (cdr p) (make-enfilade))))
|
||||||
|
(if (not p)
|
||||||
|
(set-enfilade-children!
|
||||||
|
node (alist-put (enfilade-children node) k child)))
|
||||||
|
(loop child (cdr parts)))))
|
||||||
|
datum))
|
||||||
|
|
||||||
|
(define (enfilade-empty? e)
|
||||||
|
(and (null? (enfilade-children e)) (null? (enfilade-values e))))
|
||||||
|
|
||||||
|
(define (enfilade-remove e t)
|
||||||
|
(let ((parts (tumbler->list t)))
|
||||||
|
(if (null? parts) #f
|
||||||
|
(let remove-at ((node e) (parts parts))
|
||||||
|
(let ((k (car parts)))
|
||||||
|
(if (null? (cdr parts))
|
||||||
|
(if (assoc-number k (enfilade-values node))
|
||||||
|
(begin
|
||||||
|
(set-enfilade-values!
|
||||||
|
node (alist-remove (enfilade-values node) k))
|
||||||
|
#t)
|
||||||
|
#f)
|
||||||
|
(let ((p (assoc-number k (enfilade-children node))))
|
||||||
|
(if (not p) #f
|
||||||
|
(let* ((child (cdr p))
|
||||||
|
(removed (remove-at child (cdr parts))))
|
||||||
|
(if (and removed (enfilade-empty? child))
|
||||||
|
(set-enfilade-children!
|
||||||
|
node (alist-remove (enfilade-children node) k)))
|
||||||
|
removed)))))))))
|
||||||
|
|
||||||
|
(define (enfilade-keys e)
|
||||||
|
(unique-numbers
|
||||||
|
(sort-numbers
|
||||||
|
(append (map car (enfilade-values e))
|
||||||
|
(map car (enfilade-children e))))))
|
||||||
|
|
||||||
|
(define (enfilade-get-range e . bounds)
|
||||||
|
(let ((start (if (null? bounds) #f (car bounds)))
|
||||||
|
(end (if (or (null? bounds) (null? (cdr bounds))) #f (cadr bounds))))
|
||||||
|
(let loop ((ks (enfilade-keys e)) (out '()))
|
||||||
|
(if (null? ks) out
|
||||||
|
(let ((k (car ks)))
|
||||||
|
(loop (cdr ks)
|
||||||
|
(if (and (or (not start) (>= k start))
|
||||||
|
(or (not end) (<= k end)))
|
||||||
|
(append out (enfilade-get e k))
|
||||||
|
out)))))))
|
||||||
|
|
||||||
|
; Example:
|
||||||
|
; (define e (make-enfilade))
|
||||||
|
; (enfilade-put-data e "1.2.3" 42)
|
||||||
|
; (enfilade-put-data e "1.2.4" 43)
|
||||||
|
; (enfilade-get-by-tumbler e "1.2") ; => (42 43)
|
||||||
+1880
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,34 @@
|
|||||||
|
;;; libdl.scm
|
||||||
|
;;;
|
||||||
|
;;; tie the dynamic loader library into the *libdl* environment
|
||||||
|
|
||||||
|
(require cload.scm)
|
||||||
|
(provide 'libdl.scm)
|
||||||
|
|
||||||
|
;; if loading from a different directory, pass that info to C
|
||||||
|
(let ((directory (let ((current-file (port-filename)))
|
||||||
|
(and (memv (current-file 0) '(#\/ #\~))
|
||||||
|
(substring current-file 0 (- (length current-file) 9))))))
|
||||||
|
(when (and directory (not (member directory *load-path*)))
|
||||||
|
(set! *load-path* (cons directory *load-path*)))
|
||||||
|
(with-let (rootlet)
|
||||||
|
(require cload.scm))
|
||||||
|
(when (and directory (not (string-position directory *cload-cflags*)))
|
||||||
|
(set! *cload-cflags* (string-append "-I" directory " " *cload-cflags*))))
|
||||||
|
|
||||||
|
|
||||||
|
(if (not (defined? '*libdl*))
|
||||||
|
(define *libdl*
|
||||||
|
(with-let (unlet)
|
||||||
|
(set! *libraries* (cons (cons "libdl.scm" (curlet)) *libraries*))
|
||||||
|
(set! *cload-library-name* "*libdl*")
|
||||||
|
(c-define '((void* dlopen (char* int))
|
||||||
|
(int dlclose (void*))
|
||||||
|
(void* dlsym (void* char*))
|
||||||
|
(char* dlerror (void))
|
||||||
|
(C-macro (int (RTLD_LAZY RTLD_NOW RTLD_BINDING_MASK RTLD_NOLOAD RTLD_DEEPBIND RTLD_GLOBAL RTLD_LOCAL RTLD_NODELETE))))
|
||||||
|
"" "dlfcn.h" "" "" "libdl_s7")
|
||||||
|
(curlet))))
|
||||||
|
|
||||||
|
*libdl*
|
||||||
|
;; the loader will return *libdl*
|
||||||
+444
@@ -0,0 +1,444 @@
|
|||||||
|
#include "enfilade.h"
|
||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define ENF_MSG_NULL "null enfilade"
|
||||||
|
#define ENF_MSG_ADDR "invalid tumbler address"
|
||||||
|
#define ENF_MSG_NOT_FOUND "datum not found at tumbler address"
|
||||||
|
#define ENF_MSG_NOMEM "out of memory"
|
||||||
|
|
||||||
|
typedef struct enf_value {
|
||||||
|
uint64_t key;
|
||||||
|
void *datum;
|
||||||
|
struct enf_value *next;
|
||||||
|
} enf_value_t;
|
||||||
|
|
||||||
|
typedef struct enf_child {
|
||||||
|
uint64_t key;
|
||||||
|
struct enfilade *node;
|
||||||
|
struct enf_child *next;
|
||||||
|
} enf_child_t;
|
||||||
|
|
||||||
|
struct enfilade {
|
||||||
|
enf_child_t *children;
|
||||||
|
enf_value_t *vals;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void **items;
|
||||||
|
size_t count;
|
||||||
|
size_t capacity;
|
||||||
|
} item_list_t;
|
||||||
|
|
||||||
|
static enf_result_t result(enf_error_t code, const char *msg)
|
||||||
|
{
|
||||||
|
enf_result_t r = {code, msg};
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tumbler_ok(const tumbler_result_t *r)
|
||||||
|
{
|
||||||
|
return r && r->code == TUMBLER_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enf_result_t map_tumbler_error(tumbler_result_t r)
|
||||||
|
{
|
||||||
|
if (r.code == TUMBLER_ERR_NOMEM || r.code == TUMBLER_ERR_OVERFLOW)
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
return result(ENF_ERR_ADDR, ENF_MSG_ADDR);
|
||||||
|
}
|
||||||
|
|
||||||
|
static enf_result_t parse_address(const char *text, tumbler_t *out)
|
||||||
|
{
|
||||||
|
tumbler_result_t r = tumbler_parse(text, out);
|
||||||
|
if (tumbler_ok(&r)) return result(ENF_OK, NULL);
|
||||||
|
return map_tumbler_error(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
static enf_child_t *find_child(enfilade_t *enf, uint64_t key)
|
||||||
|
{
|
||||||
|
enf_child_t *it = enf->children;
|
||||||
|
while (it && it->key < key) it = it->next;
|
||||||
|
return (it && it->key == key) ? it : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enf_value_t *find_value(enfilade_t *enf, uint64_t key)
|
||||||
|
{
|
||||||
|
enf_value_t *it = enf->vals;
|
||||||
|
while (it && it->key < key) it = it->next;
|
||||||
|
return (it && it->key == key) ? it : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enf_child_t *ensure_child(enfilade_t *enf, uint64_t key)
|
||||||
|
{
|
||||||
|
enf_child_t **link = &enf->children;
|
||||||
|
enf_child_t *it;
|
||||||
|
|
||||||
|
while ((it = *link) && it->key < key) link = &it->next;
|
||||||
|
if (it && it->key == key) return it;
|
||||||
|
|
||||||
|
it = calloc(1, sizeof(*it));
|
||||||
|
if (!it) return NULL;
|
||||||
|
|
||||||
|
it->node = create_enfilade();
|
||||||
|
if (!it->node) {
|
||||||
|
free(it);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
it->key = key;
|
||||||
|
it->next = *link;
|
||||||
|
*link = it;
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int set_value(enfilade_t *enf, uint64_t key, void *datum)
|
||||||
|
{
|
||||||
|
enf_value_t **link = &enf->vals;
|
||||||
|
enf_value_t *it;
|
||||||
|
|
||||||
|
while ((it = *link) && it->key < key) link = &it->next;
|
||||||
|
if (it && it->key == key) {
|
||||||
|
it->datum = datum;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
it = calloc(1, sizeof(*it));
|
||||||
|
if (!it) return -1;
|
||||||
|
it->key = key;
|
||||||
|
it->datum = datum;
|
||||||
|
it->next = *link;
|
||||||
|
*link = it;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int node_empty(const enfilade_t *enf)
|
||||||
|
{
|
||||||
|
return enf && !enf->children && !enf->vals;
|
||||||
|
}
|
||||||
|
|
||||||
|
enfilade_t *create_enfilade(void)
|
||||||
|
{
|
||||||
|
return calloc(1, sizeof(enfilade_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_enfilade(enfilade_t *enf)
|
||||||
|
{
|
||||||
|
if (!enf) return;
|
||||||
|
|
||||||
|
while (enf->children) {
|
||||||
|
enf_child_t *child = enf->children;
|
||||||
|
enf->children = child->next;
|
||||||
|
destroy_enfilade(child->node);
|
||||||
|
free(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (enf->vals) {
|
||||||
|
enf_value_t *value = enf->vals;
|
||||||
|
enf->vals = value->next;
|
||||||
|
free(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(enf);
|
||||||
|
}
|
||||||
|
|
||||||
|
enf_result_t put_enfilade(enfilade_t *enf, const char *tumbler, void *datum)
|
||||||
|
{
|
||||||
|
tumbler_t t = {0};
|
||||||
|
enfilade_t *current;
|
||||||
|
enf_result_t parsed;
|
||||||
|
|
||||||
|
if (!enf) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
parsed = parse_address(tumbler, &t);
|
||||||
|
if (parsed.code != ENF_OK) return parsed;
|
||||||
|
|
||||||
|
current = enf;
|
||||||
|
for (size_t i = 0; i + 1 < t.count; i++) {
|
||||||
|
enf_child_t *child = ensure_child(current, t.digits[i]);
|
||||||
|
if (!child) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
current = child->node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (set_value(current, t.digits[t.count - 1], datum) != 0) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int unlink_empty_child(enfilade_t *parent, uint64_t key)
|
||||||
|
{
|
||||||
|
enf_child_t **link = &parent->children;
|
||||||
|
enf_child_t *it;
|
||||||
|
|
||||||
|
while ((it = *link) && it->key < key) link = &it->next;
|
||||||
|
if (!it || it->key != key || !node_empty(it->node)) return 0;
|
||||||
|
|
||||||
|
*link = it->next;
|
||||||
|
destroy_enfilade(it->node);
|
||||||
|
free(it);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int remove_at(enfilade_t *node, const tumbler_t *t, size_t depth)
|
||||||
|
{
|
||||||
|
uint64_t key = t->digits[depth];
|
||||||
|
|
||||||
|
if (depth + 1 == t->count) {
|
||||||
|
enf_value_t **link = &node->vals;
|
||||||
|
enf_value_t *it;
|
||||||
|
|
||||||
|
while ((it = *link) && it->key < key) link = &it->next;
|
||||||
|
if (!it || it->key != key) return 0;
|
||||||
|
|
||||||
|
*link = it->next;
|
||||||
|
free(it);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
enf_child_t *child = find_child(node, key);
|
||||||
|
if (!child) return 0;
|
||||||
|
if (!remove_at(child->node, t, depth + 1)) return 0;
|
||||||
|
unlink_empty_child(node, key);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enf_result_t remove_enfilade(enfilade_t *enf, const char *tumbler)
|
||||||
|
{
|
||||||
|
tumbler_t t = {0};
|
||||||
|
enf_result_t parsed;
|
||||||
|
|
||||||
|
if (!enf) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
parsed = parse_address(tumbler, &t);
|
||||||
|
if (parsed.code != ENF_OK) return parsed;
|
||||||
|
|
||||||
|
if (!remove_at(enf, &t, 0)) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_ERR_NOT_FOUND, ENF_MSG_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *getXtumbler_enfilade(enfilade_t *enf, const char *tumbler)
|
||||||
|
{
|
||||||
|
tumbler_t t = {0};
|
||||||
|
enfilade_t *current;
|
||||||
|
void *datum = NULL;
|
||||||
|
enf_result_t parsed;
|
||||||
|
|
||||||
|
if (!enf) return NULL;
|
||||||
|
parsed = parse_address(tumbler, &t);
|
||||||
|
if (parsed.code != ENF_OK) return NULL;
|
||||||
|
|
||||||
|
current = enf;
|
||||||
|
for (size_t i = 0; i + 1 < t.count; i++) {
|
||||||
|
enf_child_t *child = find_child(current, t.digits[i]);
|
||||||
|
if (!child) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
current = child->node;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
enf_value_t *value = find_value(current, t.digits[t.count - 1]);
|
||||||
|
if (value) datum = value->datum;
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_free(&t);
|
||||||
|
return datum;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int item_list_push(item_list_t *list, void *datum)
|
||||||
|
{
|
||||||
|
if (list->count == list->capacity) {
|
||||||
|
size_t new_capacity;
|
||||||
|
void **new_items;
|
||||||
|
|
||||||
|
if (list->capacity > SIZE_MAX / 2)
|
||||||
|
return -1;
|
||||||
|
new_capacity = list->capacity ? list->capacity * 2 : 8;
|
||||||
|
if (new_capacity > SIZE_MAX / sizeof(*new_items))
|
||||||
|
return -1;
|
||||||
|
new_items = realloc(list->items,
|
||||||
|
new_capacity * sizeof(*new_items));
|
||||||
|
if (!new_items) return -1;
|
||||||
|
list->items = new_items;
|
||||||
|
list->capacity = new_capacity;
|
||||||
|
}
|
||||||
|
list->items[list->count++] = datum;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int collect_all(enfilade_t *enf, item_list_t *list)
|
||||||
|
{
|
||||||
|
enf_value_t *v = enf->vals;
|
||||||
|
enf_child_t *c = enf->children;
|
||||||
|
|
||||||
|
while (v || c) {
|
||||||
|
uint64_t key;
|
||||||
|
int has_v = (v != NULL);
|
||||||
|
int has_c = (c != NULL);
|
||||||
|
|
||||||
|
if (!has_c || (has_v && v->key < c->key)) key = v->key;
|
||||||
|
else key = c->key;
|
||||||
|
|
||||||
|
if (has_v && v->key == key) {
|
||||||
|
if (item_list_push(list, v->datum) != 0) return -1;
|
||||||
|
v = v->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (has_c && c->key == key) {
|
||||||
|
if (collect_all(c->node, list) != 0) return -1;
|
||||||
|
c = c->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int collect_range(enfilade_t *enf,
|
||||||
|
uint64_t start,
|
||||||
|
uint64_t end,
|
||||||
|
int unlimited_end,
|
||||||
|
item_list_t *list)
|
||||||
|
{
|
||||||
|
enf_value_t *v = enf->vals;
|
||||||
|
enf_child_t *c = enf->children;
|
||||||
|
|
||||||
|
while (v || c) {
|
||||||
|
uint64_t key;
|
||||||
|
int has_v = (v != NULL);
|
||||||
|
int has_c = (c != NULL);
|
||||||
|
|
||||||
|
if (!has_c || (has_v && v->key < c->key)) key = v->key;
|
||||||
|
else key = c->key;
|
||||||
|
|
||||||
|
if (!unlimited_end && key > end) break;
|
||||||
|
|
||||||
|
if (key >= start) {
|
||||||
|
if (has_v && v->key == key) {
|
||||||
|
if (item_list_push(list, v->datum) != 0) return -1;
|
||||||
|
}
|
||||||
|
if (has_c && c->key == key) {
|
||||||
|
if (collect_all(c->node, list) != 0) return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (has_v && v->key == key) v = v->next;
|
||||||
|
if (has_c && c->key == key) c = c->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
enf_result_t getXtumbler_values_enfilade(enfilade_t *enf,
|
||||||
|
const char *tumbler,
|
||||||
|
void ***items,
|
||||||
|
size_t *count)
|
||||||
|
{
|
||||||
|
tumbler_t t = {0};
|
||||||
|
enfilade_t *current;
|
||||||
|
item_list_t list = {0};
|
||||||
|
enf_result_t parsed;
|
||||||
|
uint64_t key;
|
||||||
|
|
||||||
|
if (!items || !count) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
*items = NULL;
|
||||||
|
*count = 0;
|
||||||
|
if (!enf) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
|
||||||
|
parsed = parse_address(tumbler, &t);
|
||||||
|
if (parsed.code != ENF_OK) return parsed;
|
||||||
|
|
||||||
|
current = enf;
|
||||||
|
for (size_t i = 0; i + 1 < t.count; i++) {
|
||||||
|
enf_child_t *child = find_child(current, t.digits[i]);
|
||||||
|
if (!child) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
return result(ENF_ERR_NOT_FOUND, ENF_MSG_NOT_FOUND);
|
||||||
|
}
|
||||||
|
current = child->node;
|
||||||
|
}
|
||||||
|
|
||||||
|
key = t.digits[t.count - 1];
|
||||||
|
{
|
||||||
|
enf_value_t *value = find_value(current, key);
|
||||||
|
enf_child_t *child = find_child(current, key);
|
||||||
|
if (value && item_list_push(&list, value->datum) != 0) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
free(list.items);
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
if (child && collect_all(child->node, &list) != 0) {
|
||||||
|
tumbler_free(&t);
|
||||||
|
free(list.items);
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_free(&t);
|
||||||
|
*items = list.items;
|
||||||
|
*count = list.count;
|
||||||
|
return result(ENF_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
enf_result_t getXrange_values_enfilade(enfilade_t *enf,
|
||||||
|
int64_t start,
|
||||||
|
int64_t end,
|
||||||
|
void ***items,
|
||||||
|
size_t *count)
|
||||||
|
{
|
||||||
|
item_list_t list = {0};
|
||||||
|
int unlimited_end;
|
||||||
|
uint64_t ustart;
|
||||||
|
uint64_t uend;
|
||||||
|
|
||||||
|
if (!items || !count) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
*items = NULL;
|
||||||
|
*count = 0;
|
||||||
|
if (!enf) return result(ENF_ERR_NULL, ENF_MSG_NULL);
|
||||||
|
if (end >= 0 && start > end) return result(ENF_OK, NULL);
|
||||||
|
|
||||||
|
ustart = (start < 0) ? 0 : (uint64_t)start;
|
||||||
|
unlimited_end = (end < 0);
|
||||||
|
uend = unlimited_end ? UINT64_MAX : (uint64_t)end;
|
||||||
|
|
||||||
|
if (collect_range(enf, ustart, uend, unlimited_end, &list) != 0) {
|
||||||
|
free(list.items);
|
||||||
|
return result(ENF_ERR_NOMEM, ENF_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
|
||||||
|
*items = list.items;
|
||||||
|
*count = list.count;
|
||||||
|
return result(ENF_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void visit_data_node(enfilade_t *enf,
|
||||||
|
enfilade_datum_visitor visitor,
|
||||||
|
void *context)
|
||||||
|
{
|
||||||
|
for (enf_value_t *value = enf->vals; value; value = value->next)
|
||||||
|
visitor(value->datum, context);
|
||||||
|
for (enf_child_t *child = enf->children; child; child = child->next)
|
||||||
|
visit_data_node(child->node, visitor, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
void enfilade_visit_data(enfilade_t *enf,
|
||||||
|
enfilade_datum_visitor visitor,
|
||||||
|
void *context)
|
||||||
|
{
|
||||||
|
if (!enf || !visitor) return;
|
||||||
|
visit_data_node(enf, visitor, context);
|
||||||
|
}
|
||||||
@@ -0,0 +1,307 @@
|
|||||||
|
#include "enfilade.h"
|
||||||
|
#include "enfilade_s7.h"
|
||||||
|
#include "s7.h"
|
||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static s7_int g_enfilade_type;
|
||||||
|
|
||||||
|
static void mark_one_datum(void *datum, void *context)
|
||||||
|
{
|
||||||
|
(void)context;
|
||||||
|
if (datum) s7_mark((s7_pointer)datum);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void s7_enfilade_mark(void *value)
|
||||||
|
{
|
||||||
|
if (value)
|
||||||
|
enfilade_visit_data((enfilade_t *)value, mark_one_datum, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void s7_enfilade_free(void *value)
|
||||||
|
{
|
||||||
|
if (value) destroy_enfilade((enfilade_t *)value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer tumbler_error(s7_scheme *sc, const char *message)
|
||||||
|
{
|
||||||
|
return s7_error(sc,
|
||||||
|
s7_make_symbol(sc, "TumblerAddressException"),
|
||||||
|
s7_list(sc, 1, s7_make_string(sc, message)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer tumbler_memory_error(s7_scheme *sc, const char *message)
|
||||||
|
{
|
||||||
|
return s7_error(sc,
|
||||||
|
s7_make_symbol(sc, "memory-error"),
|
||||||
|
s7_list(sc, 1, s7_make_string(sc, message)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static enfilade_t *enfilade_arg(s7_scheme *sc, s7_pointer obj, int arg_n)
|
||||||
|
{
|
||||||
|
enfilade_t *enf = (enfilade_t *)s7_c_object_value_checked(obj, g_enfilade_type);
|
||||||
|
if (!enf) {
|
||||||
|
s7_wrong_type_arg_error(sc, __func__, arg_n, obj, "<enfilade>");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return enf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer string_arg(s7_scheme *sc, s7_pointer obj, int arg_n)
|
||||||
|
{
|
||||||
|
if (!s7_is_string(obj))
|
||||||
|
return s7_wrong_type_arg_error(sc, __func__, arg_n, obj, "a string");
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_make_enfilade(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
enfilade_t *enf;
|
||||||
|
(void)args;
|
||||||
|
|
||||||
|
enf = create_enfilade();
|
||||||
|
if (!enf) return tumbler_memory_error(sc, "out of memory");
|
||||||
|
|
||||||
|
return s7_make_c_object(sc, g_enfilade_type, enf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_enfilade_put(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
s7_pointer obj = s7_car(args);
|
||||||
|
s7_pointer tumbler = s7_cadr(args);
|
||||||
|
s7_pointer datum = s7_caddr(args);
|
||||||
|
enfilade_t *enf = enfilade_arg(sc, obj, 1);
|
||||||
|
enf_result_t r;
|
||||||
|
|
||||||
|
if (!enf) return s7_nil(sc);
|
||||||
|
if (!string_arg(sc, tumbler, 2)) return s7_nil(sc);
|
||||||
|
|
||||||
|
r = put_enfilade(enf, s7_string(tumbler), (void *)datum);
|
||||||
|
if (r.code == ENF_OK) return s7_unspecified(sc);
|
||||||
|
if (r.code == ENF_ERR_NOMEM) return tumbler_memory_error(sc, r.msg);
|
||||||
|
return tumbler_error(sc, r.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_enfilade_remove(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
s7_pointer obj = s7_car(args);
|
||||||
|
s7_pointer tumbler = s7_cadr(args);
|
||||||
|
enfilade_t *enf = enfilade_arg(sc, obj, 1);
|
||||||
|
enf_result_t r;
|
||||||
|
|
||||||
|
if (!enf) return s7_f(sc);
|
||||||
|
if (!string_arg(sc, tumbler, 2)) return s7_f(sc);
|
||||||
|
|
||||||
|
r = remove_enfilade(enf, s7_string(tumbler));
|
||||||
|
if (r.code == ENF_OK) return s7_t(sc);
|
||||||
|
if (r.code == ENF_ERR_NOT_FOUND) return s7_f(sc);
|
||||||
|
if (r.code == ENF_ERR_NOMEM) return tumbler_memory_error(sc, r.msg);
|
||||||
|
return tumbler_error(sc, r.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer make_datum_list(s7_scheme *sc, void **items, size_t count)
|
||||||
|
{
|
||||||
|
s7_pointer list = s7_nil(sc);
|
||||||
|
for (size_t i = count; i > 0; i--)
|
||||||
|
list = s7_cons(sc, (s7_pointer)items[i - 1], list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_enfilade_get(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
s7_pointer obj = s7_car(args);
|
||||||
|
s7_pointer tumbler = s7_cadr(args);
|
||||||
|
enfilade_t *enf = enfilade_arg(sc, obj, 1);
|
||||||
|
void **items = NULL;
|
||||||
|
size_t count = 0;
|
||||||
|
enf_result_t r;
|
||||||
|
s7_pointer out;
|
||||||
|
|
||||||
|
if (!enf) return s7_nil(sc);
|
||||||
|
if (!string_arg(sc, tumbler, 2)) return s7_nil(sc);
|
||||||
|
|
||||||
|
r = getXtumbler_values_enfilade(enf, s7_string(tumbler), &items, &count);
|
||||||
|
if (r.code == ENF_ERR_NOT_FOUND || r.code == ENF_ERR_ADDR)
|
||||||
|
return tumbler_error(sc, r.msg);
|
||||||
|
if (r.code == ENF_ERR_NOMEM)
|
||||||
|
return tumbler_memory_error(sc, r.msg);
|
||||||
|
if (r.code != ENF_OK)
|
||||||
|
return s7_error(sc, s7_make_symbol(sc, "enfilade-error"),
|
||||||
|
s7_list(sc, 1, s7_make_string(sc, r.msg)));
|
||||||
|
|
||||||
|
out = make_datum_list(sc, items, count);
|
||||||
|
free(items);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_enfilade_get_range(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
s7_pointer obj = s7_car(args);
|
||||||
|
s7_pointer start_arg = s7_cadr(args);
|
||||||
|
s7_pointer end_arg = s7_caddr(args);
|
||||||
|
enfilade_t *enf = enfilade_arg(sc, obj, 1);
|
||||||
|
void **items = NULL;
|
||||||
|
size_t count = 0;
|
||||||
|
int64_t start;
|
||||||
|
int64_t end;
|
||||||
|
enf_result_t r;
|
||||||
|
s7_pointer out;
|
||||||
|
|
||||||
|
if (!enf) return s7_nil(sc);
|
||||||
|
if (!s7_is_integer(start_arg))
|
||||||
|
return s7_wrong_type_arg_error(sc, __func__, 2, start_arg, "an integer");
|
||||||
|
if (!s7_is_integer(end_arg))
|
||||||
|
return s7_wrong_type_arg_error(sc, __func__, 3, end_arg, "an integer");
|
||||||
|
|
||||||
|
start = (int64_t)s7_integer(start_arg);
|
||||||
|
end = (int64_t)s7_integer(end_arg);
|
||||||
|
|
||||||
|
r = getXrange_values_enfilade(enf, start, end, &items, &count);
|
||||||
|
if (r.code == ENF_ERR_NOMEM)
|
||||||
|
return tumbler_memory_error(sc, r.msg);
|
||||||
|
if (r.code != ENF_OK)
|
||||||
|
return s7_error(sc, s7_make_symbol(sc, "enfilade-error"),
|
||||||
|
s7_list(sc, 1, s7_make_string(sc, r.msg)));
|
||||||
|
|
||||||
|
out = make_datum_list(sc, items, count);
|
||||||
|
free(items);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer parse_tumbler_arg(s7_scheme *sc, s7_pointer arg, int arg_n,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
tumbler_result_t r;
|
||||||
|
|
||||||
|
if (!s7_is_string(arg)) {
|
||||||
|
s7_wrong_type_arg_error(sc, __func__, arg_n, arg, "a string");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = tumbler_parse(s7_string(arg), out);
|
||||||
|
if (r.code == TUMBLER_ERR_NOMEM || r.code == TUMBLER_ERR_OVERFLOW) {
|
||||||
|
tumbler_memory_error(sc, r.msg);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (r.code != TUMBLER_OK) {
|
||||||
|
tumbler_error(sc, r.msg);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer tumbler_result_string(s7_scheme *sc, tumbler_result_t r,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
char *text = NULL;
|
||||||
|
tumbler_result_t sr;
|
||||||
|
|
||||||
|
if (r.code != TUMBLER_OK) {
|
||||||
|
if (r.code == TUMBLER_ERR_NOMEM || r.code == TUMBLER_ERR_OVERFLOW)
|
||||||
|
return tumbler_memory_error(sc, r.msg);
|
||||||
|
return tumbler_error(sc, r.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
sr = tumbler_to_string(out, &text);
|
||||||
|
if (sr.code != TUMBLER_OK) {
|
||||||
|
if (sr.code == TUMBLER_ERR_NOMEM)
|
||||||
|
return tumbler_memory_error(sc, sr.msg);
|
||||||
|
return tumbler_error(sc, sr.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
s7_pointer s = s7_make_string(sc, text);
|
||||||
|
free(text);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_tumbler_compare(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
tumbler_t a = {0}, b = {0};
|
||||||
|
s7_pointer a_arg = s7_car(args);
|
||||||
|
s7_pointer b_arg = s7_cadr(args);
|
||||||
|
int cmp;
|
||||||
|
|
||||||
|
if (!parse_tumbler_arg(sc, a_arg, 1, &a)) return s7_nil(sc);
|
||||||
|
if (!parse_tumbler_arg(sc, b_arg, 2, &b)) {
|
||||||
|
tumbler_free(&a);
|
||||||
|
return s7_nil(sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp = tumbler_compare(&a, &b);
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
return s7_make_integer(sc, cmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_tumbler_add(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
tumbler_t a = {0}, b = {0}, out = {0};
|
||||||
|
s7_pointer a_arg = s7_car(args);
|
||||||
|
s7_pointer b_arg = s7_cadr(args);
|
||||||
|
tumbler_result_t r;
|
||||||
|
s7_pointer result_string;
|
||||||
|
|
||||||
|
if (!parse_tumbler_arg(sc, a_arg, 1, &a)) return s7_nil(sc);
|
||||||
|
if (!parse_tumbler_arg(sc, b_arg, 2, &b)) {
|
||||||
|
tumbler_free(&a);
|
||||||
|
return s7_nil(sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
r = tumbler_add(&a, &b, &out);
|
||||||
|
result_string = tumbler_result_string(sc, r, &out);
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
tumbler_free(&out);
|
||||||
|
return result_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
static s7_pointer s7_tumbler_subtract(s7_scheme *sc, s7_pointer args)
|
||||||
|
{
|
||||||
|
tumbler_t a = {0}, b = {0}, out = {0};
|
||||||
|
s7_pointer a_arg = s7_car(args);
|
||||||
|
s7_pointer b_arg = s7_cadr(args);
|
||||||
|
tumbler_result_t r;
|
||||||
|
s7_pointer result_string;
|
||||||
|
|
||||||
|
if (!parse_tumbler_arg(sc, a_arg, 1, &a)) return s7_nil(sc);
|
||||||
|
if (!parse_tumbler_arg(sc, b_arg, 2, &b)) {
|
||||||
|
tumbler_free(&a);
|
||||||
|
return s7_nil(sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
r = tumbler_subtract(&a, &b, &out);
|
||||||
|
result_string = tumbler_result_string(sc, r, &out);
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
tumbler_free(&out);
|
||||||
|
return result_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_enfilade_s7(s7_scheme *sc)
|
||||||
|
{
|
||||||
|
g_enfilade_type = s7_make_c_type(sc, "<enfilade>");
|
||||||
|
s7_c_type_set_free(sc, g_enfilade_type, s7_enfilade_free);
|
||||||
|
s7_c_type_set_mark(sc, g_enfilade_type, s7_enfilade_mark);
|
||||||
|
|
||||||
|
s7_define_function(sc, "make-enfilade", s7_make_enfilade,
|
||||||
|
0, 0, false, "create a Model-T style enfilade");
|
||||||
|
s7_define_function(sc, "enfilade-put-data", s7_enfilade_put,
|
||||||
|
3, 0, false, "store a datum at a tumbler address");
|
||||||
|
s7_define_function(sc, "enfilade-remove", s7_enfilade_remove,
|
||||||
|
2, 0, false, "remove a datum at a tumbler address");
|
||||||
|
s7_define_function(sc, "enfilade-get-by-tumbler", s7_enfilade_get,
|
||||||
|
2, 0, false, "resolve a tumbler to a flat list of data");
|
||||||
|
s7_define_function(sc, "enfilade-get-range", s7_enfilade_get_range,
|
||||||
|
3, 0, false, "flatten data in an inclusive numeric range");
|
||||||
|
s7_define_function(sc, "tumbler-compare", s7_tumbler_compare,
|
||||||
|
2, 0, false, "compare two tumbler addresses");
|
||||||
|
s7_define_function(sc, "tumbler-add", s7_tumbler_add,
|
||||||
|
2, 0, false, "add a tumbler offset to a position");
|
||||||
|
s7_define_function(sc, "tumbler-subtract", s7_tumbler_subtract,
|
||||||
|
2, 0, false, "compute the generalized tumbler difference");
|
||||||
|
}
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
#include "enfilade.h"
|
||||||
|
#include "enfilade_s7.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void repl(s7_scheme *sc)
|
||||||
|
{
|
||||||
|
char buffer[4096];
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
fputs("> ", stdout);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if (!fgets(buffer, sizeof(buffer), stdin)) {
|
||||||
|
fputc('\n', stdout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buffer[0] == '\n' && buffer[1] == '\0')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
{
|
||||||
|
s7_pointer value = s7_eval_c_string(sc, buffer);
|
||||||
|
printf("%s\n", s7_object_to_c_string(sc, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc == 2 && (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
|
||||||
|
printf("ensifer version %s\n", ENSIFER_VERSION);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
|
||||||
|
puts("Usage: logan [-h|--help] [-V|--version]");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc != 1) {
|
||||||
|
fprintf(stderr, "Usage: logan [-h|--help] [-V|--version]\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
s7_scheme *sc = s7_init();
|
||||||
|
|
||||||
|
init_enfilade_s7(sc);
|
||||||
|
repl(sc);
|
||||||
|
s7_free(sc);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+112
@@ -0,0 +1,112 @@
|
|||||||
|
/* gcc -o repl repl.c s7.o -Wl,-export-dynamic -lm -I. -ldl */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "s7.h"
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
static char *realdir(s7_scheme *sc, const char *filename)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
if (!strchr(filename, '/'))
|
||||||
|
{
|
||||||
|
if (access("libc_s7.so", F_OK) != 0)
|
||||||
|
{
|
||||||
|
if ((access("libc.scm", F_OK) == 0) &&
|
||||||
|
(access("cload.scm", F_OK) == 0))
|
||||||
|
{
|
||||||
|
s7_load(sc, "cload.scm");
|
||||||
|
s7_load(sc, "libc.scm");
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "%s needs libc_s7.so (give the explicit repl pathname or build it by running: repl libc.scm)\n", filename); /* env PATH=/home/bil/cl repl */
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
return(NULL); /* we're in the libc_s7.so directory, I hope (user could start a version of s7 that does not match the local libc_s7.so...) */
|
||||||
|
}
|
||||||
|
if (!(path = realpath(filename, NULL)))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s\n", strerror(errno), filename);
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
if (!(p = strrchr(path, '/')))
|
||||||
|
{
|
||||||
|
free(path);
|
||||||
|
fprintf(stderr, "please provide the full pathname for %s\n", filename);
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
if (p > path) *p = '\0'; else p[1] = 0;
|
||||||
|
return(path);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
s7_scheme *sc;
|
||||||
|
|
||||||
|
sc = s7_init();
|
||||||
|
if (argc >= 2)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[1], "-e") == 0) /* repl -e '(+ 1 2)' */
|
||||||
|
{
|
||||||
|
s7_pointer x;
|
||||||
|
x = s7_eval_c_string(sc, argv[2]);
|
||||||
|
fprintf(stdout, "%s\n", s7_object_to_c_string(sc, x));
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
if (strcmp(argv[1], "--version") == 0)
|
||||||
|
{
|
||||||
|
fprintf(stdout, "s7: %s, %s\n", S7_VERSION, S7_DATE);
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "load %s\n", argv[1]); /* repl test.scm */
|
||||||
|
errno = 0;
|
||||||
|
if (!s7_load(sc, argv[1]))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s\n", strerror(errno), argv[1]);
|
||||||
|
return(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
char buffer[512];
|
||||||
|
fprintf(stdout, "\n> ");
|
||||||
|
if (!fgets(buffer, 512, stdin)) break; /* error or ctrl-D */
|
||||||
|
if (((buffer[0] != '\n') || (strlen(buffer) > 1)))
|
||||||
|
{
|
||||||
|
char response[1024];
|
||||||
|
snprintf(response, 1024, "(write %s)", buffer);
|
||||||
|
s7_eval_c_string(sc, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fprintf(stdout, "\n");
|
||||||
|
if (ferror(stdin))
|
||||||
|
fprintf(stderr, "read error on stdin\n");
|
||||||
|
#else
|
||||||
|
#ifdef S7_LOAD_PATH
|
||||||
|
s7_add_to_load_path(sc, S7_LOAD_PATH);
|
||||||
|
#else
|
||||||
|
char *dir;
|
||||||
|
dir = realdir(sc, argv[0]);
|
||||||
|
if (dir)
|
||||||
|
{
|
||||||
|
s7_add_to_load_path(sc, dir);
|
||||||
|
free(dir);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
s7_repl(sc);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
+357
@@ -0,0 +1,357 @@
|
|||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define TUMBLER_MSG_NULL "null tumbler"
|
||||||
|
#define TUMBLER_MSG_INVALID "invalid tumbler"
|
||||||
|
#define TUMBLER_MSG_NOMEM "out of memory"
|
||||||
|
#define TUMBLER_MSG_OVERFLOW "tumbler arithmetic overflow"
|
||||||
|
#define TUMBLER_MSG_UNDERFLOW "tumbler subtraction underflow"
|
||||||
|
|
||||||
|
static tumbler_result_t result(tumbler_error_t code, const char *msg)
|
||||||
|
{
|
||||||
|
tumbler_result_t r = {code, msg};
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tumbler_free(tumbler_t *t)
|
||||||
|
{
|
||||||
|
if (!t) return;
|
||||||
|
free(t->digits);
|
||||||
|
t->digits = NULL;
|
||||||
|
t->count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tumbler_digit(const tumbler_t *t, size_t i)
|
||||||
|
{
|
||||||
|
return (t && i < t->count) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t tumbler_field(const tumbler_t *t, size_t i)
|
||||||
|
{
|
||||||
|
return (tumbler_digit(t, i)) ? t->digits[i] : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_parse(const char *text, tumbler_t *out)
|
||||||
|
{
|
||||||
|
const char *p;
|
||||||
|
size_t count = 0;
|
||||||
|
uint64_t *digits;
|
||||||
|
|
||||||
|
if (!out) return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
out->digits = NULL;
|
||||||
|
out->count = 0;
|
||||||
|
|
||||||
|
if (!text || !*text)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
|
||||||
|
p = text;
|
||||||
|
while (*p) {
|
||||||
|
const char *q = p;
|
||||||
|
if (*q < '0' || *q > '9')
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
while (*q >= '0' && *q <= '9') q++;
|
||||||
|
count++;
|
||||||
|
if (*q == '\0') break;
|
||||||
|
if (*q != '.')
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
p = q + 1;
|
||||||
|
if (!*p)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count > SIZE_MAX / sizeof(*digits))
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
|
||||||
|
digits = calloc(count, sizeof(*digits));
|
||||||
|
if (!digits)
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
|
||||||
|
p = text;
|
||||||
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
char *endp;
|
||||||
|
unsigned long long value;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
value = strtoull(p, &endp, 10);
|
||||||
|
if (errno == ERANGE)
|
||||||
|
goto overflow;
|
||||||
|
if (endp == p || value > UINT64_MAX)
|
||||||
|
goto invalid;
|
||||||
|
|
||||||
|
digits[i] = (uint64_t)value;
|
||||||
|
if (i + 1 < count) {
|
||||||
|
if (*endp != '.')
|
||||||
|
goto invalid;
|
||||||
|
p = endp + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out->digits = digits;
|
||||||
|
out->count = count;
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
|
||||||
|
overflow:
|
||||||
|
free(digits);
|
||||||
|
return result(TUMBLER_ERR_OVERFLOW, TUMBLER_MSG_OVERFLOW);
|
||||||
|
invalid:
|
||||||
|
free(digits);
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_to_string(const tumbler_t *t, char **out)
|
||||||
|
{
|
||||||
|
size_t length = 1;
|
||||||
|
char *text;
|
||||||
|
size_t used = 0;
|
||||||
|
|
||||||
|
if (!out) return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
*out = NULL;
|
||||||
|
if (!t || !t->digits || t->count == 0)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < t->count; i++) {
|
||||||
|
int n = snprintf(NULL, 0, "%" PRIu64, t->digits[i]);
|
||||||
|
if (n < 0 || (size_t)n > SIZE_MAX - length - (i ? 1 : 0))
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
length += (size_t)n;
|
||||||
|
if (i) length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
text = malloc(length);
|
||||||
|
if (!text)
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < t->count; i++) {
|
||||||
|
int n = snprintf(text + used, length - used, "%s%" PRIu64,
|
||||||
|
i ? "." : "", t->digits[i]);
|
||||||
|
if (n < 0 || (size_t)n >= length - used) {
|
||||||
|
free(text);
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
}
|
||||||
|
used += (size_t)n;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = text;
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tumbler_compare(const tumbler_t *a, const tumbler_t *b)
|
||||||
|
{
|
||||||
|
size_t count;
|
||||||
|
|
||||||
|
if (a == b) return 0;
|
||||||
|
if (!a || !b) return a ? 1 : -1;
|
||||||
|
|
||||||
|
count = (a->count > b->count) ? a->count : b->count;
|
||||||
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
uint64_t af = tumbler_field(a, i);
|
||||||
|
uint64_t bf = tumbler_field(b, i);
|
||||||
|
if (af < bf) return -1;
|
||||||
|
if (af > bf) return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static tumbler_result_t alloc_result(size_t count, tumbler_t *out)
|
||||||
|
{
|
||||||
|
if (!out) return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
out->digits = NULL;
|
||||||
|
out->count = 0;
|
||||||
|
|
||||||
|
if (count == 0) count = 1;
|
||||||
|
if (count > SIZE_MAX / sizeof(*out->digits))
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
|
||||||
|
out->digits = calloc(count, sizeof(*out->digits));
|
||||||
|
if (!out->digits)
|
||||||
|
return result(TUMBLER_ERR_NOMEM, TUMBLER_MSG_NOMEM);
|
||||||
|
out->count = count;
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This helper is only called after all source reads are complete. */
|
||||||
|
static void assign_result(tumbler_t *out, tumbler_t *tmp,
|
||||||
|
const tumbler_t *a, const tumbler_t *b)
|
||||||
|
{
|
||||||
|
uint64_t *old = out->digits;
|
||||||
|
(void)a;
|
||||||
|
(void)b;
|
||||||
|
out->digits = tmp->digits;
|
||||||
|
out->count = tmp->count;
|
||||||
|
tmp->digits = old;
|
||||||
|
tmp->count = 0;
|
||||||
|
free(tmp->digits);
|
||||||
|
tmp->digits = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_add(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
tumbler_t tmp = {0};
|
||||||
|
size_t first = SIZE_MAX;
|
||||||
|
size_t count;
|
||||||
|
tumbler_result_t r;
|
||||||
|
|
||||||
|
if (!position || !offset || !out)
|
||||||
|
return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
if (!position->digits || !position->count ||
|
||||||
|
!offset->digits || !offset->count)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < offset->count; i++) {
|
||||||
|
if (offset->digits[i] != 0) {
|
||||||
|
first = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first == SIZE_MAX) {
|
||||||
|
r = alloc_result(position->count, &tmp);
|
||||||
|
if (r.code != TUMBLER_OK) return r;
|
||||||
|
memcpy(tmp.digits, position->digits,
|
||||||
|
position->count * sizeof(*tmp.digits));
|
||||||
|
assign_result(out, &tmp, position, offset);
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
count = offset->count;
|
||||||
|
if (count < first + 1) count = first + 1;
|
||||||
|
r = alloc_result(count, &tmp);
|
||||||
|
if (r.code != TUMBLER_OK) return r;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < first; i++)
|
||||||
|
tmp.digits[i] = tumbler_field(position, i);
|
||||||
|
|
||||||
|
{
|
||||||
|
uint64_t base = tumbler_field(position, first);
|
||||||
|
uint64_t add = offset->digits[first];
|
||||||
|
if (UINT64_MAX - base < add) {
|
||||||
|
tumbler_free(&tmp);
|
||||||
|
return result(TUMBLER_ERR_OVERFLOW, TUMBLER_MSG_OVERFLOW);
|
||||||
|
}
|
||||||
|
tmp.digits[first] = base + add;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = first + 1; i < offset->count; i++)
|
||||||
|
tmp.digits[i] = offset->digits[i];
|
||||||
|
|
||||||
|
assign_result(out, &tmp, position, offset);
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_subtract_strong(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
tumbler_t tmp = {0};
|
||||||
|
size_t first = SIZE_MAX;
|
||||||
|
tumbler_result_t r;
|
||||||
|
|
||||||
|
if (!position || !offset || !out)
|
||||||
|
return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
if (!position->digits || !position->count ||
|
||||||
|
!offset->digits || !offset->count)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
|
||||||
|
if (tumbler_compare(position, offset) < 0)
|
||||||
|
return result(TUMBLER_ERR_UNDERFLOW, TUMBLER_MSG_UNDERFLOW);
|
||||||
|
|
||||||
|
r = alloc_result(position->count, &tmp);
|
||||||
|
if (r.code != TUMBLER_OK) return r;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < position->count; i++) {
|
||||||
|
uint64_t pf = position->digits[i];
|
||||||
|
uint64_t of = tumbler_field(offset, i);
|
||||||
|
if (pf != of) {
|
||||||
|
first = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tmp.digits[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first == SIZE_MAX) {
|
||||||
|
/* Equal tumblers: the generalized difference is exactly zero. */
|
||||||
|
tmp.count = 1;
|
||||||
|
tmp.digits[0] = 0;
|
||||||
|
} else {
|
||||||
|
tmp.digits[first] = position->digits[first] - tumbler_field(offset, first);
|
||||||
|
for (size_t i = first + 1; i < position->count; i++)
|
||||||
|
tmp.digits[i] = position->digits[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
assign_result(out, &tmp, position, offset);
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_subtract_weak(const tumbler_t *position,
|
||||||
|
const tumbler_t *offset,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
tumbler_t tmp = {0};
|
||||||
|
size_t first = SIZE_MAX;
|
||||||
|
size_t count;
|
||||||
|
tumbler_result_t r;
|
||||||
|
|
||||||
|
if (!position || !offset || !out)
|
||||||
|
return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
if (!position->digits || !position->count ||
|
||||||
|
!offset->digits || !offset->count)
|
||||||
|
return result(TUMBLER_ERR_INVALID, TUMBLER_MSG_INVALID);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < offset->count; i++) {
|
||||||
|
if (offset->digits[i] != 0) {
|
||||||
|
first = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first == SIZE_MAX) {
|
||||||
|
r = alloc_result(position->count, &tmp);
|
||||||
|
if (r.code != TUMBLER_OK) return r;
|
||||||
|
memcpy(tmp.digits, position->digits,
|
||||||
|
position->count * sizeof(*tmp.digits));
|
||||||
|
assign_result(out, &tmp, position, offset);
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
count = first + 1;
|
||||||
|
r = alloc_result(count, &tmp);
|
||||||
|
if (r.code != TUMBLER_OK) return r;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < first; i++)
|
||||||
|
tmp.digits[i] = tumbler_field(position, i);
|
||||||
|
|
||||||
|
{
|
||||||
|
uint64_t base = tumbler_field(position, first);
|
||||||
|
uint64_t sub = offset->digits[first];
|
||||||
|
if (base < sub) {
|
||||||
|
tumbler_free(&tmp);
|
||||||
|
return result(TUMBLER_ERR_UNDERFLOW, TUMBLER_MSG_UNDERFLOW);
|
||||||
|
}
|
||||||
|
tmp.digits[first] = base - sub;
|
||||||
|
}
|
||||||
|
|
||||||
|
assign_result(out, &tmp, position, offset);
|
||||||
|
return result(TUMBLER_OK, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
tumbler_result_t tumbler_subtract(const tumbler_t *a,
|
||||||
|
const tumbler_t *b,
|
||||||
|
tumbler_t *out)
|
||||||
|
{
|
||||||
|
int cmp;
|
||||||
|
|
||||||
|
if (!a || !b || !out)
|
||||||
|
return result(TUMBLER_ERR_NULL, TUMBLER_MSG_NULL);
|
||||||
|
cmp = tumbler_compare(a, b);
|
||||||
|
if (cmp > 0)
|
||||||
|
return tumbler_subtract_strong(a, b, out);
|
||||||
|
return tumbler_subtract_weak(b, a, out);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#include "enfilade.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void get_values(enfilade_t *e, const char *address,
|
||||||
|
void ***items, size_t *count)
|
||||||
|
{
|
||||||
|
enf_result_t r = getXtumbler_values_enfilade(e, address, items, count);
|
||||||
|
assert(r.code == ENF_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
enfilade_t *e = create_enfilade();
|
||||||
|
int a = 41, b = 42, c = 43, d = 99;
|
||||||
|
void **items = NULL;
|
||||||
|
size_t count = 0;
|
||||||
|
enf_result_t r;
|
||||||
|
|
||||||
|
assert(e);
|
||||||
|
|
||||||
|
assert(put_enfilade(e, "1.2.3", &a).code == ENF_OK);
|
||||||
|
assert(put_enfilade(e, "1.2.4", &b).code == ENF_OK);
|
||||||
|
assert(put_enfilade(e, "1.2.5", &c).code == ENF_OK);
|
||||||
|
assert(put_enfilade(e, "1.2", &d).code == ENF_OK);
|
||||||
|
|
||||||
|
get_values(e, "1.2", &items, &count);
|
||||||
|
assert(count == 4);
|
||||||
|
assert(*(int *)items[0] == 99);
|
||||||
|
assert(*(int *)items[1] == 41);
|
||||||
|
assert(*(int *)items[2] == 42);
|
||||||
|
assert(*(int *)items[3] == 43);
|
||||||
|
free(items);
|
||||||
|
|
||||||
|
assert(getXtumbler_enfilade(e, "1.2.4") == &b);
|
||||||
|
|
||||||
|
r = remove_enfilade(e, "1.2");
|
||||||
|
assert(r.code == ENF_OK);
|
||||||
|
get_values(e, "1.2", &items, &count);
|
||||||
|
assert(count == 3);
|
||||||
|
assert(*(int *)items[0] == 41);
|
||||||
|
free(items);
|
||||||
|
|
||||||
|
r = remove_enfilade(e, "1.2.3");
|
||||||
|
assert(r.code == ENF_OK);
|
||||||
|
assert(getXtumbler_enfilade(e, "1.2.3") == NULL);
|
||||||
|
assert(getXtumbler_enfilade(e, "1.2.4") == &b);
|
||||||
|
|
||||||
|
r = remove_enfilade(e, "1.2.3");
|
||||||
|
assert(r.code == ENF_ERR_NOT_FOUND);
|
||||||
|
|
||||||
|
assert(put_enfilade(e, "9.9.9", &a).code == ENF_OK);
|
||||||
|
assert(remove_enfilade(e, "9.9.9").code == ENF_OK);
|
||||||
|
assert(getXtumbler_enfilade(e, "9.9") == NULL);
|
||||||
|
|
||||||
|
destroy_enfilade(e);
|
||||||
|
puts("enfilade tests: ok");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "enfilade_s7.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
s7_scheme *sc = s7_init();
|
||||||
|
s7_pointer value;
|
||||||
|
|
||||||
|
init_enfilade_s7(sc);
|
||||||
|
|
||||||
|
value = s7_eval_c_string(sc,
|
||||||
|
"(let ((e (make-enfilade)))"
|
||||||
|
" (enfilade-put-data e \"1.2.3\" 42)"
|
||||||
|
" (enfilade-put-data e \"1.2.4\" 43)"
|
||||||
|
" (enfilade-put-data e \"1.3.1\" 99)"
|
||||||
|
" (list"
|
||||||
|
" (enfilade-get-by-tumbler e \"1.2\")"
|
||||||
|
" (enfilade-get-range e 1 3)"
|
||||||
|
" (tumbler-compare \"1.2\" \"1.2.1\")"
|
||||||
|
" (tumbler-add \"25.6.46.93\" \"0.0.3.1.21\")"
|
||||||
|
" (tumbler-subtract \"1.4.3\" \"1.1.1\")"
|
||||||
|
" (enfilade-remove e \"1.2.3\")"
|
||||||
|
" (enfilade-get-by-tumbler e \"1.2.3\")))");
|
||||||
|
|
||||||
|
puts(s7_object_to_c_string(sc, value));
|
||||||
|
s7_free(sc);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#include "tumbler.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static void expect_string(const tumbler_t *t, const char *want)
|
||||||
|
{
|
||||||
|
char *got = NULL;
|
||||||
|
tumbler_result_t r = tumbler_to_string(t, &got);
|
||||||
|
assert(r.code == TUMBLER_OK);
|
||||||
|
assert(strcmp(got, want) == 0);
|
||||||
|
free(got);
|
||||||
|
}
|
||||||
|
|
||||||
|
static tumbler_t parse(const char *s)
|
||||||
|
{
|
||||||
|
tumbler_t t = {0};
|
||||||
|
tumbler_result_t r = tumbler_parse(s, &t);
|
||||||
|
assert(r.code == TUMBLER_OK);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_compare(void)
|
||||||
|
{
|
||||||
|
const char *line[] = {
|
||||||
|
"1", "1.1", "1.1.1", "1.1.2", "1.1.3",
|
||||||
|
"1.2", "1.2.1", "1.2.2", "1.3", "1.4", "2"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (size_t i = 0; i + 1 < sizeof(line) / sizeof(line[0]); i++) {
|
||||||
|
tumbler_t a = parse(line[i]);
|
||||||
|
tumbler_t b = parse(line[i + 1]);
|
||||||
|
assert(tumbler_compare(&a, &b) < 0);
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
tumbler_t a = parse("1");
|
||||||
|
tumbler_t b = parse("1.0.0");
|
||||||
|
assert(tumbler_compare(&a, &b) == 0);
|
||||||
|
tumbler_free(&a);
|
||||||
|
tumbler_free(&b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_arithmetic(void)
|
||||||
|
{
|
||||||
|
tumbler_t a, b, out = {0};
|
||||||
|
|
||||||
|
a = parse("3"); b = parse("2.16.3");
|
||||||
|
assert(tumbler_add(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "5.16.3");
|
||||||
|
tumbler_free(&a); tumbler_free(&b); tumbler_free(&out);
|
||||||
|
|
||||||
|
a = parse("25.6.46.93"); b = parse("0.0.3.1.21");
|
||||||
|
assert(tumbler_add(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "25.6.49.1.21");
|
||||||
|
tumbler_free(&a); tumbler_free(&b); tumbler_free(&out);
|
||||||
|
|
||||||
|
a = parse("1.4.3"); b = parse("1.1.1");
|
||||||
|
assert(tumbler_subtract_strong(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "0.3.3");
|
||||||
|
tumbler_free(&out);
|
||||||
|
assert(tumbler_subtract(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "0.3.3");
|
||||||
|
tumbler_free(&a); tumbler_free(&b); tumbler_free(&out);
|
||||||
|
|
||||||
|
a = parse("0.3.3.3.4.5.6"); b = parse("0.1.1.3.3.3.3");
|
||||||
|
assert(tumbler_subtract_weak(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "0.2");
|
||||||
|
tumbler_free(&a); tumbler_free(&b); tumbler_free(&out);
|
||||||
|
|
||||||
|
a = parse("1.2.3"); b = parse("1.1");
|
||||||
|
assert(tumbler_subtract(&a, &b, &out).code == TUMBLER_OK);
|
||||||
|
expect_string(&out, "0.1.3");
|
||||||
|
tumbler_free(&a); tumbler_free(&b); tumbler_free(&out);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
test_compare();
|
||||||
|
test_arithmetic();
|
||||||
|
puts("tumbler tests: ok");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user