Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import haxe.macro.Type;
- import haxe.macro.Expr;
- import haxe.macro.Context;
- import haxe.io.Bytes;
- import sys.io.File;
- class Parser
- {
- var data : haxe.io.Bytes;
- var at : Int = 0;
- static public function expr2name(name : Expr) : String
- {
- return switch (name.expr)
- {
- case EConst(cdata): switch (cdata) { case CIdent(id): id; default: "<ERROR>"; }
- default: "<ERROR>";
- };
- }
- macro static public function READ(name : Expr, type : Expr, size : Int = 4) : Expr
- {
- var nameStr = expr2name(name);
- var typeStr = expr2name(type);
- switch (typeStr)
- {
- case "Int32":
- return macro
- {
- var $nameStr : Int = data.getInt32(at);
- at += 4;
- trace($v{nameStr + " = "} + $i{nameStr});
- };
- case "Str":
- return macro
- {
- var $nameStr : String = data.getString(at, $v{size});
- at += $v{size};
- trace($v{nameStr + " = "} + $i{nameStr});
- };
- default:
- return macro {};
- }
- }
- function new()
- {
- data = sys.io.File.getBytes("loop.wav");
- READ(magic, Str, 4);
- READ(size, Int32);
- }
- static public function main():Void
- {
- var parser = new Parser();
- trace("Hello World");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement