In "Bitwise Extra, Day 27: Static Assembler, Part 2" (~14:09) pervognsen is accessing a field of a struct that's nested in other structs:
https://www.youtube.com/watch?v=DRKUJBc4y6A&t=14m09s
I think a union could be used to create an alias to a frequently used field which is nested, say 2-3 levels:
Using the struct Parser as an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | struct Parser {
union {
lexer: Lexer;
// alias parser.t = parser.lexer.token;
struct {
dummy1: uint8_t[offsetof(Lexer, token)];
// t: typeof(Lexer, token);
t: Token;
}
// alias parser.td = parser.lexer.token.data
struct {
dummy2: uint8_t[offsetof(Lexer, token) + offsetof(Token, data)];
// td: typeof(Token, data);
td: TokedData;
}
}
// ...
}
func is_token(parser: Parser*, kind: TokenKind): bool { return parser.lexer.token.kind == kind; }
func is_token(parser: Parser*, kind: TokenKind): bool { return parser.t.kind == kind; }
xreg := parser.lexer.token.data.xreg;
xreg := parser.td.xreg;
|