From f4429c29c91214692b87260f6a2373fd7e7c87f1 Mon Sep 17 00:00:00 2001 From: Billingsly Wetherfordshire Date: Sun, 26 May 2013 14:28:10 -0700 Subject: [PATCH] Created Writing a Macro (markdown) --- Writing-a-Macro.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Writing-a-Macro.md diff --git a/Writing-a-Macro.md b/Writing-a-Macro.md new file mode 100644 index 0000000..6af7f4a --- /dev/null +++ b/Writing-a-Macro.md @@ -0,0 +1,23 @@ +Macros are complex and powerful, allowing you to build the AST imperatively. So how does one build AST? It's not as hard as it sounds. + +I'm going to go over the process I used to write the marshaling macro used in keineSchweine (github/fowlmouth/keineSchweine/lib/genpacket.nim) + +Okay, so I have described a packet as a object type holding the information needed, for example a packet that gets sent out to clients when a new client connects might look like this: +```nimrod +type TClientJoined = object + playerID: int + alias: string +``` + +To make this useful, I'd like to build the TClientJoined with the information needed and write it a stream, and have a function that takes a stream and reads from it into a TClientJoined +```nimrod +# writeBE and readBE are reading/writing functions that will read/write in big endian +proc write (packet: var TClientJoined; stream: PStream) = + writeBE stream, packet.playerID + writeBE stream, packet.alias +proc readClientJoined (stream: PStream): TClientJoined = + readBE stream, result.playerID + readBE stream, result.alias +``` + +