Skip to content

Instantly share code, notes, and snippets.

@tjhann

tjhann/example.i Secret

Created Oct 7, 2021
Embed
What would you like to do?
Gordon code
import std.dyna;
import std.mem;
/* There is no /* std.io */ right now. */
extern(C) {
int puts(const ubyte* s);
int printf(const ubyte* fmt, ...);
}
struct Pack {
int id;
const ubyte* desc;
ubyte~ buffer; // dynamic array, a pointer
}
int main()
{
ubyte* buf = mem.alloc(1024); // macro, yields `calloc(1024, 1)`
auto pack = mem.alloc1(Pack);
*pack = {
id : 4,
desc : "wolves",
buffer : empty(ubyte), // no allocation done here but allows calling push
};
scope pack.buffer.dispose();
pack.buffer.push('G');
pack.buffer.pusha("ordon"); // dynas maintain 0-termination iff elem size is 1
printf("pack: %d (%s)\n", pack.id, pack.desc);
printf("buffer: %s\n", pack.buffer.ptr);
const ubyte* ptr = "gnc has a handmade lexer and parser!";
const ubyte[] slice = "and it's self hosting.";
ubyte[5] fixa = "dang!"; // fixed length array, string auto-converts
#setup[linux] puts("it's linux!");
#setup[windows] puts("it's windows!");
#setup[macos] puts("it's macos!");
define A = 256; // compile time constant
define B = 512;
int m = max(A, B) * 2; // constant args, folds to 1024 before codegen
return 0;
}
// Macro arguments are evaluated once, from left to right.
macro max(a,b) { a > b ? a : b } // Macros always yield an expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment