Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Ape – an easy-to-embed programming language in two C files (github.com/kgabis)
90 points by kgabis on March 30, 2020 | hide | past | favorite | 22 comments



Hi!

This project started with me going through "Writing An Interpreter In Go" (https://interpreterbook.com) and "Writing A Compiler In Go" (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) 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.


This 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/

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/


This 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 :)


Thank you for sharing your work. It's very inspiring and interesting. Kudos on getting the whole "standing on the shoulders of giants" ball rolling.


Good stuff, but the "two files" qualifier is somewhat misleading if one of them is almost 350KB ;)


I mean, it's also true of SQLite3.


I think you're talking about "The SQLite Amalgamation" - 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!


I'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.


no 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.


> Math: > + - * /

Why no modulus operator? While it could be implemented, it seems like a bit of an oversight.


There is a modulus operator, I've just forgotten to put it in that section. It's fixed now.


Isn't this just an interpreter that you need to compile before running each time?


In 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.


"It's compiled to bytecode and executed on internal VM"

What does it take a byte code interpreter to be a VM?


I'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.


I 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).


To my knowledge, nothing, really.


Interesting. I would love to see a version of JavaScript that has stack scoped (RAII) allocation.


Nice and compact. It looks like there might not be a way to read or write a null byte though.


You can make a custom external object (with ape_object_make_external) that serves as a byte buffer and add byte_buffer_read/write functions.


Okay yeah two files but like fifty trillion lines each.

Cool stuff though.


> const err = error("something bad happened)

has a typo.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: