top | item 23612910

Show HN: Awk-JVM – A toy JVM in Awk

180 points| rethab | 5 years ago |github.com | reply

27 comments

order
[+] rethab|5 years ago|reply
Author here.

As I wrote in the README, this uses GAWK instead of plain AWK. Conveniences of GAWK over AWK in a nutshell: - functions - several additional functions (eg. bit shifting)

But even GAWK lacks some things that are very common in other languages: - no variable scope: imagine a calling another function in a for loop and the other function again running a for loop. if both loops use 'i' as the counter, good luck. the workaround for this is to declare the local variables as parameters that are not passed (and separate them with four spaces) - cannot return array from a function. the workaround is to use pass-by-reference (not sure if the precise definition is applicable here) - arrays cannot be assigned to another variable. workaround is to loop over array and assign it value by value.

If anybody knows better workarounds, please let me know :)

[+] wahern|5 years ago|reply
It might be useful to provide an example using POSIX od(1) instead of hexdump(1). hexdump is actually a BSD utility. The Debian Linux version is ported from FreeBSD, while the Red Hat version is a pale imitation provided as a wrapper, IIRC, around the od implementation.[1] `od -An -tu1 -v` should suffice.

[1] I wrote a single-file hexdump implementation so know far too much about it: https://25thandclement.com/~william/projects/hexdump.c.html

[+] flukus|5 years ago|reply
> the workaround for this is to declare the local variables as parameters that are not passed (and separate them with four spaces) - cannot return array from a function.

Does anyone know the history of this? It sounds like something that's to crazy to have been designed and came about as a hack that got widely adopted. It would be nice if they added a local keyword or a prefix or something.

[+] userbinator|5 years ago|reply
I thought from the if/else chain in the mainloop that it appears to lack a switch() statement too, but the GAWK documentation indicates that it does have one.
[+] ufo|5 years ago|reply
Honestly, maybe the most robust workaround is to rewrite it in another language... Lua might be a good candidate: it has a similar syntax and also uses 1-based arrays.
[+] tyingq|5 years ago|reply
Not the main point, and this is a very cool dancing bear. But, on this point:

"since none of the awks can read binary, you first need to pipe the classfile through hexdump"

Gawk works fine with binary files for me. Using FIELDWIDTHS for fixed length records or the readfile() extension to slurp in a whole file works fine. The readline() function can also be paired with FIELDWIDTHS to read a fixed number of bytes. Newline separated records with nulls in them also read as expected. I'm curious what problems the author saw with binary and gawk.

[+] rethab|5 years ago|reply
I actually simply didn't know it was possible based on some googling. But thanks for hint. Someone also opened an issue on github giving me a hint on how to replace hexdump[0]. I'll definitely give this a try :)

0. https://github.com/rethab/awk-jvm/issues/1

[+] WFHRenaissance|5 years ago|reply
This is definitely an accomplishment. Someone show this to Kernighan.
[+] siraben|5 years ago|reply
The AWK Programming Language book is great not just for learning AWK but also has chapters on data processing, generating tables and graphs, relational databases and even a VM! I've implemented the assembler and VM from the book and extended the instruction set.[0]

[0] https://github.com/siraben/awk-vm

[+] zserge|5 years ago|reply
AWK is a very underappreciated language from the past, simple and fun to use. You did a very nice job, thanks!
[+] enriquto|5 years ago|reply
What do you mean by "from the past"? All existing languages are from the past.

EDIT: what would be a language that is "not from the past"? Certainly Python(1991) is from the past if Akw(1977) is. The origin of Python is twice closer to the origin of awk than to the present time.

[+] loudmax|5 years ago|reply
My first thought reading the headline was that this was an implementation of Awk that ran on a JVM. But no, this is the reverse of that. This is way is far less useful but infinitely more interesting. Bravo!
[+] userbinator|5 years ago|reply
Such a thing does exist: http://jawk.sourceforge.net/

...and that naturally leads to pondering whether an implementation of AWK that runs on the JVM can then run an implementation of the JVM that runs on AWK, and vice-versa...

[+] tyingq|5 years ago|reply
Added a pull request that wraps typeof() and calls a "polyfill" if it the typeof() function doesn't exist. Gawk didn't have that until v4.2.
[+] exabrial|5 years ago|reply
Google is failing me, but I believe someone wrote a JVM in Excel too. When all you have is a hammer... :)