FFI interface #83
Replies: 28 comments 4 replies
-
Hi @hiperiondev! Yes, it has native function support i.e. functions written in C. //declared in toy_literal.h
typedef int (*Toy_NativeFn)(struct Toy_Interpreter* interpreter, struct Toy_LiteralArray* arguments);
typedef int (*Toy_HookFn)(struct Toy_Interpreter* interpreter, struct Toy_Literal identifier, struct Toy_Literal alias);
//declared in toy_interpreter.h
TOY_API bool Toy_injectNativeFn(Toy_Interpreter* interpreter, const char* name, Toy_NativeFn func);
TOY_API bool Toy_injectNativeHook(Toy_Interpreter* interpreter, const char* name, Toy_HookFn hook); These allow you to define native functions which are simply "injected" into the interpreter's current scope - alternatively, a hook is invoked with the Toy keyword
A few libraries are made available this way within the repl folder; they start with the filename "lib_". This is the intended way to expand the language's functionality - but I haven't written many libraries beyond what is currently provided (help would be greatly appreciated ;) ) Is there anything else you need help with? |
Beta Was this translation helpful? Give feedback.
-
Thanks for your quick response! |
Beta Was this translation helpful? Give feedback.
-
Thank you so much! The full docs are on the website https://toylang.com, if you need to look up exactly why something exists the way it does. And of course, I'm always available for any questions you have. |
Beta Was this translation helpful? Give feedback.
-
First attempt to embedd on ESP32: https://github.com/hiperiondev/esp32_toy I have made some changes
An FTP server and a custom little filesystem are added to improve usability on a microcontroller. It would be useful to modify the file accesses with macros (fopen, fclose, etc) to facilitate the use of native filesystems on microcontrollers. Other possible changes:
That would be all for now. I'm going to finish the functionality test (for now only compile) and I'll keep you informed. |
Beta Was this translation helpful? Give feedback.
-
Added test_interpreter and test_compiler. Pass all test without errors! |
Beta Was this translation helpful? Give feedback.
-
(I won't get into spaces vs. tabs - only madness lies there.) I get the pragma change, but was separating headers & source just a preference? License - the code is already under the zlib license, which is basically exactly the same as the MIT license, just less famous. I've documented what each function does on the docs website, but I suppose it's a pain to go look it up. You're in luck - the interpreter operates completely independently of the compiler, and vice-versa. Once the compiler collates the bytecode, the bytecode can be saved to a file and executed separately. I highly recommend reading the docs site (you can skip the quickstart section for now) - https://toylang.com/ - it'll explain a lot of my thoughts about what Toy, and might help a lot. |
Beta Was this translation helpful? Give feedback.
-
Spaces vs. tabs: Bloody wars have started for much less!
Virtually all changes are personal preference. In the case of headers, it is convenient if they are self-documenting. It is a single place to look for references.
That's good, I hadn't noticed because there is no reference in any file as a comment in the header. It would be nice to add it in each file
Not only does it result in the inconvenience of needing an external reference, but using Doxygen-style comments you can automate the process of creating external documentation and the code is left with its own documentation.
That's very good. I will check it. Perhaps it would be convenient to separate them into two directories to make explicit the choice of each component in the implementation
I'll do that. Thank you so much |
Beta Was this translation helpful? Give feedback.
-
I have separated the compiler and interpreter code. It is more comfortable to compile only the interpreter in an embedded system. Now everything seems to work correctly. I will try to do something useful to test. |
Beta Was this translation helpful? Give feedback.
-
I didn't think to separate the compiler from the interpreter, partly because they both rely one the same literal/array/dictionary structures - but you do you. If and when I make updates, are you planning on importing the updates to your branch? Or staying at 1.1.6? Because there are still small niggling corner cases that need fixing eventually (the issue tracker is full of them lol). I'll look into doxygen - I suppose it's just another personal preference to have a separate site. I made the site super easy to update by storing it in a separate branch within the main repo - so it's basically sitting next to the source it documents. There's a lot of assumed preferences in my code that I wasn't aware of - I suppose it's the result of my inexperience working with other coders. You never stop learning I guess. I might convert this issue into a discussion soon, since that would make more sense to me. |
Beta Was this translation helpful? Give feedback.
-
Here are my final modifications in structure: https://github.com/hiperiondev/esp32_toy/tree/main/components/toy_lang/Toy
I find the Toy language very interesting for embedded applications due to its similarity with C and simplicity of implementation, it is an excellent option as a glue language. But on a microcontroller resources are scarce and precious (ESP32 has 200k of RAM, with FreeRTOS OS plus a few things to work hopefully 80/90k are free for the program!)
At first I can follow "by hand" the changes that are made. If you think my changes are correct we could try to integrate this way. Your project, your decision.
Doxygen not only allows you to automate, with a very clear syntax, the documentation, but also integrates very well with other systems such as Sphinx (widely used by https://readthedocs.org/ for example). If you want to involve more people in development, it is a very useful tool.
No problem. The decisions depend a lot on whether you intend to allow and organize the participation of more people in the development or follow it only as a personal project.
I agree with that. |
Beta Was this translation helpful? Give feedback.
-
I think dictionary should be in common, and builtin should be in the interpreter folder - otherwise, cool layout.
That's probably the best approach, since the directory layout changed so much. I'll keep mine how it is for now (because Box and Skylands depend on it, and I'm not in the mood to tweak those too), but it's possible I'll follow suit at some point.
Cool, thanks - I'll have a peek soon.
It started out as only a side-project, something to reduce stress, but it's kind of taken off - people really seem to like it, and others have already contributed in the past with things like building on MacOS and error testing (though the error testers were a group of people who... let's not get into that here). I'd love more people to be involved, and it honestly makes me really happy what you're doing. |
Beta Was this translation helpful? Give feedback.
-
Converted the issue to a discussion. Edit: Also cleaned up the discussions in general. |
Beta Was this translation helpful? Give feedback.
-
The idea of the separation is to be able to compile only the interpreter without the compiler.
No problem. For now the modifications are more of style than functionality.
Thank you so much. You have really done an excellent job! For the topic of using Toy in embedded systems, a deeper documentation on the operation of the interpreter and its internal components would be very useful. Probably some modification should be made to improve memory usage. |
Beta Was this translation helpful? Give feedback.
-
New project: https://github.com/hiperiondev/uC-TOY |
Beta Was this translation helpful? Give feedback.
-
I have finished the skeleton of doxygen documentation. |
Beta Was this translation helpful? Give feedback.
-
I have added all the documentation available. There are many undocumented things |
Beta Was this translation helpful? Give feedback.
-
It's more a feeling of "this is one of the languages building blocks" - it's the same feeling I get from arrays and literals.
That means so much to me.
I'll see what I can do during my pass to add comments.
Ohh interesting. So you're going to tweak it specifically for microcontrollers?
Cool - I actually want to talk to you about that in #84.
I'll need to resolve that going forward - the documentation that is there is intended largely to aid people in using Toy, rather than building on top of it. I guess I'll need to change the approach there. |
Beta Was this translation helpful? Give feedback.
-
It is not my real intention but a microcontroller requires particular optimizations due to its limited resources and direct handling of the hardware. Some elements must be minimal or static (for example it is not a good idea to have 64bit data handling directly in the interpreter, it is also not a good practice to use int for everything, intx_t or uintx_t is advisable). It's not clear to me what else they're using Toy for, but you've told me that other projects use it and, for that reason, you can't change its structure.
I will follow this topic in that place.
I think that this is mandatory to adapt it in a more generic way. For this reason I had advised you to see the internal documentation of LUA, since it was created specifically for this (in fact, it was originally made to work inside television in Brazil and send programs through the TV Ginga standard) https://www.lua.org/notes/ |
Beta Was this translation helpful? Give feedback.
-
NOTE: In Issues only have Bug Report and Feature Request. A category for Questions about language use would be useful. |
Beta Was this translation helpful? Give feedback.
-
This is on the TODO list, actually marked as a much-needed element for the next major version.
It's actually embedded into a game engine - that was always it's initial purpose: to allow easy modding of video games. The engine is here: https://github.com/Ratstail91/Box They're still in early development, so forgive how bare-bones it is.
Oh wow, that's quite the read. I'll start digging into this soon.
No worries, will add that now. |
Beta Was this translation helpful? Give feedback.
-
@hiperiondev I should emphasize that Toy's development is done in a way that tries to reduce stress on me, as I'm suffering from some mental health issues. So I might not be able to work as fast as is needed, but I'll do my best to provide what help you need. For now, I might not get too much done until the weekend. I also have another major project called Egg Trainer which, while in an ok state, also needs my attention from time to time. I felt like I should let you know this stuff to pre-empt any issues with how fast things get done (and to remind myself to take it easy). |
Beta Was this translation helpful? Give feedback.
-
Don't worry about this, health always comes first!
I do my project in my free time (because my work is quite a lot!). |
Beta Was this translation helpful? Give feedback.
-
Thanks for understanding.
I totally agree :) |
Beta Was this translation helpful? Give feedback.
-
@hiperiondev Before I forget
A quick find and replace should fix that. I've also bumped the version number, but you can leave updating your stuff until I actually do a release. |
Beta Was this translation helpful? Give feedback.
-
I think this will be interesting for you: |
Beta Was this translation helpful? Give feedback.
-
This week I will finish integrating the minimum of HAL: fs, wifi, and GPIO |
Beta Was this translation helpful? Give feedback.
-
https://toylang.com/ - docs updated for clarity. |
Beta Was this translation helpful? Give feedback.
-
Good! |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you’d like
I've been reviewing the code a bit and it seems to be a very good language to embed in microcontrollers.
Does Toy have any FFI interface?
Beta Was this translation helpful? Give feedback.
All reactions