TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

180 pointsby rethabalmost 5 years ago

9 comments

rethabalmost 5 years ago
Author here.<p>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)<p>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 &#x27;i&#x27; 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.<p>If anybody knows better workarounds, please let me know :)
评论 #23618873 未加载
评论 #23617594 未加载
评论 #23621732 未加载
评论 #23620431 未加载
评论 #23622782 未加载
评论 #23616820 未加载
tyingqalmost 5 years ago
Not the main point, and this is a very cool dancing bear. But, on this point:<p><i>&quot;since none of the awks can read binary, you first need to pipe the classfile through hexdump&quot;</i><p>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&#x27;m curious what problems the author saw with binary and gawk.
评论 #23625224 未加载
WFHRenaissancealmost 5 years ago
This is definitely an accomplishment. Someone show this to Kernighan.
sirabenalmost 5 years ago
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&#x27;ve implemented the assembler and VM from the book and extended the instruction set.[0]<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;siraben&#x2F;awk-vm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;siraben&#x2F;awk-vm</a>
ketanmaheshwarialmost 5 years ago
METHODS[m][&quot;attributes&quot;][a][&quot;data&quot;][4]<p>Is this a five dimensional array? How does it get populated?<p>EDIT: I see it now in the code. Excellent!
评论 #23616058 未加载
zsergealmost 5 years ago
AWK is a very underappreciated language from the past, simple and fun to use. You did a very nice job, thanks!
评论 #23619545 未加载
loudmaxalmost 5 years ago
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!
评论 #23622819 未加载
tyingqalmost 5 years ago
Added a pull request that wraps typeof() and calls a &quot;polyfill&quot; if it the typeof() function doesn&#x27;t exist. Gawk didn&#x27;t have that until v4.2.
exabrialalmost 5 years ago
Google is failing me, but I believe someone wrote a JVM in Excel too. When all you have is a hammer... :)