-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Writing a Macro
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/dependencies/genpacket)
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:
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
# 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
As you can see, this will result in a specialized function for reading/writing a certain type of packet
to be continued
Intro
Getting Started
- Install
- Docs
- Curated Packages
- Editor Support
- Unofficial FAQ
- Nim for C programmers
- Nim for Python programmers
- Nim for TypeScript programmers
- Nim for D programmers
- Nim for Java programmers
- Nim for Haskell programmers
Developing
- Build
- Contribute
- Creating a release
- Compiler module reference
- Consts defined by the compiler
- Debugging the compiler
- GitHub Actions/Travis CI/Circle CI/Appveyor
- GitLab CI setup
- Standard library and the JavaScript backend
Misc