Gift is a festive esoteric programming language that takes your wishlist as input, and outputs one of two things:
- the first character of each line, one after the other
- the string
coal
, if you ask for something that is too expensive
For each line in the input, a hash is computed (32-bit FNV-1a). If the hash result is a multiple of 4, the interpreter
outputs coal
and halts. Otherwise, it carries on. If it doesn't output coal
by the time it reaches the end, the
interpreter outputs the first character of each line.
The hash calculation is case-insensitive and ignores whitespace.
gift <FILE>
gift
: interprets a string and returns the output as a string (malloc'ed)gift_result_free
: frees the memory used by a string returned bygift
(this function is just an alias forfree
)
Note that even if the string is coal
(i.e. compile-time constant), it is still malloc'ed in order to provide consistent behavior.
The interpreter can be compiled to WASM via Emscripten. See the CMakeLists.txt file for the necessary
function exports if you don't want to use CMake. After compilation, 2 files will be generated: one .js
and one .wasm
.
You can import the js file and use it like this:
// Import the js file via <script src="...">, require(), or import (should also work in Node.js)
// You can then write a wrapper like this:
const gift = (src) => {
srcPtr = Module.allocate(Module.intArrayFromString(src), Module.ALLOC_NORMAL);
var result = Module._gift(srcPtr);
Module._free(srcPtr);
var str = Module.UTF8ToString(result);
Module._gift_free_result(result);
return str;
}
// Now you can call gift("source code here")
Note that the gift
function (Module._gift
) allocates memory via malloc
. The gift_free_result
function was
provided for convenience (Module._gift_free_result
). Using ccall
or cwrap
isn't a good idea in this context
as they will lead to memory leaks.
This implementation (i.e. the code) is licensed under the MIT license.