4 ms·
Show HN: Ape – an easy-to-embed programming language in two C files
- speuleralert 7y agoIsn't this just an interpreter that you need to compile before running each time?
- moftz 7y agoIn the example directory, it looks like you could compile a binary that takes a text file of ape code as a command line argument and interprets it. Sounds just like how something like python works. You could have this interpreter running on an embedded processor that could take in new ape code via serial or from a textfile on removable media or even from some sort of OTA update if you want to get fancy. It's much easier to have a small bootstrap/interpreter running on a chip that can load up new code than having to flash new firmware every time.
- kgabis 7y agoHi! This project started with me going through "Writing An Interpreter In Go" (https://interpreterbook.com https://interpreterbook.com) and "Writing A Compiler In Go" (https://compilerbook.com https://compilerbook.com) books by Thorsten Ball (however I've decided to implement it in C instead). Later I've decided to continue working on it with the ultimate goal of making an easy to use library out of it - something you can place in your source tree and immediately start tinkering with. I've also expanded upon the Monkey language (https://monkeylang.org https://monkeylang.org) by making it more mutable and adding additional constructs such as loops and modules. A lot of time was spent on making it faster by not allocating memory where possible so it should be reasonably fast (pointers are kept in IEEE double NaN payloads so there's no need for allocating memory when doing numeric computations). It's still work in progress and I expect there to be plenty of undiscovered bugs but it should be quite useful already if you need a drop-in scripting language in your application. The language itself is also quite easy to use with it's C-like syntax and useful error messages.
- rumanator 7y agoThank you for sharing your work. It's very inspiring and interesting. Kudos on getting the whole "standing on the shoulders of giants" ball rolling.
- djhworld 7y agoThis is cool! I can definitely second the recommendation for those two books, they really opened my eyes to the basics of what is going on under the hood of a compiler. Recently I've been trying to target Monkeylang to Webassembly which has been a fun, if albeit frustrating exercise :)
- giancarlostoro 7y agoThis is awesome, this is also the second really interesting project I've seen that I suspect spawned from that book the first being V: https://vlang.io/ https://vlang.io/ I could be wrong about him having based his language off that book, but when he mentioned writing it first in Go I got the vibe it might be, plus he recreated Monkey in V. It amazes me how much V has grown in a short period of time, and how much your language has grown as well. I've been wanting to buy those two books for a while, and I think I'm just going to have to based on the results. This reminds me of the CoffeeScript developer who also read a similar book years back and built CoffeeScript: http://createyourproglang.com/ http://createyourproglang.com/
- huhtenberg 7y agoGood stuff, but the "two files" qualifier is somewhat misleading if one of them is almost 350KB ;)
- cryptonector 7y agoI mean, it's also true of SQLite3.
- docandrew 7y agoI think you're talking about "The SQLite Amalgamation" - https://www.sqlite.org/amalgamation.html https://www.sqlite.org/amalgamation.html. I hadn't heard of it before, so thanks for encouraging me to look it up - seems like it might come in handy!
- kgabis 7y agoI'm using a python script and a template to merge source files into ape.c so it's quite similar to sqlite's amalgamation. It's not as simple as joining a lot of files together though because there are dependencies between files and all global variables and functions need to be declared static.
- smartstakestime 7y agono its not. What did you expect? I thought it would lack features but the 350kb makes me think that its actually a nice generic lib set for C. Basically syntactic sugar taken a step further.
- tyingq 7y agoNice and compact. It looks like there might not be a way to read or write a null byte though.
- kgabis 7y agoYou can make a custom external object (with ape_object_make_external) that serves as a byte buffer and add byte_buffer_read/write functions.
- moonchild 7y ago> const err = error("something bad happened) has a typo.
- armatav 7y agoOkay yeah two files but like fifty trillion lines each. Cool stuff though.
- big_chungus 7y ago> Math: > + - * / Why no modulus operator? While it could be implemented, it seems like a bit of an oversight.
- kgabis 7y agoThere is a modulus operator, I've just forgotten to put it in that section. It's fixed now.
- crazypython 7y agoInteresting. I would love to see a version of JavaScript that has stack scoped (RAII) allocation.
- stewbrew 7y ago"It's compiled to bytecode and executed on internal VM" What does it take a byte code interpreter to be a VM?
- saagarjha 7y agoTo my knowledge, nothing, really.
- kroltan 7y agoI suppose it's a VM if it has some sort of isolation from the actual machine (for example, a virtual/bounded memory space, among other things).
- abainbridge 7y agoI'm not sure the terms are well defined, but here's my attempt. The BASIC implementation on a ZX Spectrum (and probably most 8-bit home computers of the 80's) was an interpreter but not a VM because it did not implement any isolation. A BASIC program could access any address on the machine and jump execution to any address. A VM, in contrast, would do bounds checking on pointer accesses and limit which syscalls were allowed. You'd expect that if two instances of a VM were run at the same time, that programs in those VMs would not be able to access each other's memory in general.
- deleted 7y ago[deleted]