Skip to content

Instantly share code, notes, and snippets.

@zeux
zeux / lnumprint.cpp
Last active January 28, 2026 17:03
lnumprint.cpp from Luau language (with removed dependencies) implementing Schubfach algorithm. Original source: https://github.com/luau-lang/luau/blob/master/VM/src/lnumprint.cpp
// This file is extracted from Luau programming language and licensed under MIT License (see end of file).
// It exports one symbol that can be used to convert double-precision value to a string, and returns the
// 'end' pointer (the string is not null terminated):
//
// char* luai_num2str(char* buf, double n);
//
// The function requires a buffer of 48 characters to avoid overflows.
#include <assert.h>
#include <stdint.h>
@o11c
o11c / every-vm-tutorial-you-ever-studied-is-wrong.md
Last active January 5, 2026 17:30
Every VM tutorial you ever studied is wrong (and other compiler/interpreter-related knowledge)

Note: this was originally several Reddit posts, chained and linked. But now that Reddit is dying I've finally moved them out. Sorry about the mess.


URL: https://www.reddit.com/r/ProgrammingLanguages/comments/up206c/stack_machines_for_compilers/i8ikupw/ Summary: stack-based vs register-based in general.

There are a wide variety of machines that can be described as "stack-based" or "register-based", but not all of them are practical. And there are a lot of other decisions that affect that practicality (do variables have names or only address/indexes? fixed-width or variable-width instructions? are you interpreting the bytecode (and if so, are you using machine stack frames?) or turning it into machine code? how many registers are there, and how many are special? how do you represent multiple types of variable? how many scopes are there(various kinds of global, local, member, ...)? how much effort/complexity can you afford to put into your machine? etc.)

  • a pure stack VM can only access the top elemen