-
-
Save tjhann/f26dac0b7c99e850c210a56d61193620 to your computer and use it in GitHub Desktop.
Gordon code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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