I'm working on a new pet project and i came across this error when trying to store functions in a hash in zig.
the error happens on the try p.nud_handlers.put(Tokens.IDENT, p.parseIdentifier); line
the thing is, i do have the parseIdentifier defined inside of the Parser struct
Stuff that i tried:
- Defining the function outside of the parse struct. (Whilst this did work, i would like to declare the function inside of the struct)
- Appending the function to the hash inside of some other function that isnt init (Did not work, same error)
Zig version is 0.15.0 (Master)
error:
parser.zig:102:48: error: no field named 'parseIdentifier' in struct 'parser.Parser'
try p.nud_handlers.put(Tokens.IDENT, p.parseIdentifier);
const NudParseFn = *const fn (*Parser) *AST.Expression;
const LedParseFn = *const fn (*Parser, *AST.Expression) *AST.Expression;
pub const Parser = struct {
lexer: *Lexer,
cur_token: Token = undefined,
peek_token: Token = undefined,
nud_handlers: std.AutoHashMap(Tokens, NudParseFn) = undefined,
led_handlers: std.AutoHashMap(Tokens, LedParseFn) = undefined,
pub fn init(lexer: *Lexer) !Parser {
var parser: Parser = Parser{ .lexer = lexer };
parser.nud_handlers = std.AutoHashMap(Tokens, NudParseFn).init(std.heap.page_allocator);
parser.led_handlers = std.AutoHashMap(Tokens, LedParseFn).init(std.heap.page_allocator);
// ... some other code here
try p.nud_handlers.put(Tokens.IDENT, parser.parseIdentifier); // the error happens here
return parser;
}
pub fn parseIdentifier(p: *Parser) *AST.Expression {
return &AST.Expression{ .identifier_expr = AST.Identifier{ .token = p.cur_token, .literal = p.cur_token.literal } };
}
// Other definitions below...
}