Hi, I've been stuck on this issue for a while (i am new to zig) I am making a compiler and am trying to compile this statement: const contents = "HAI 1.2\nSUM OF 1 AN 2, SUM OF IT AN 3, KTHXBYE"; My parser correctly creates all of the nodes, it first creates an expression node with the values 1 and 2 and then on with it and 3. But when I append the statements, while (!self.isAtEnd()) { self.stmts.append(self.parse_statement() catch { self.create_error(ParserError{ .message = "Expected valid statement line", .token = self.peek() }); return ast.ProgramNode{ .statements = self.stmts.toOwnedSlice() catch &[_]ast.StatementNode{} }; }) catch {}; }, the first node gets overwritten with the second node as follows in this output:
#ArrayList, appending struct causes overwrite on previous values
1 messages · Page 1 of 1 (latest)
make sure that the arraylist is the same arraylist each time, aka you aren't copying the structure
i don't think I am anywhere
ah I see
sorry I thought you meant actually overwriting it like replacing it, I was expecting to see "HAI" somewhere
interestingly, now that i am realizing this, those two sum lines (which are expression nodes) are overwritting each other, but the end kthxbye node is not overwritting
yeah i just auto consume that at the start
{ lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .Expression = lib.parser.ast.ExpressionNode{ ... } } } }
{ lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .Expression = lib.parser.ast.ExpressionNode{ ... } } }, lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .Expression = lib.parser.ast.ExpressionNode{
... } } } }
{ lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .Expression = lib.parser.ast.ExpressionNode{ ... } } }, lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .Expression = lib.parser.ast.ExpressionNode{
... } } }, lib.parser.ast.StatementNode{ .option = lib.parser.ast.StatementNodeValueOption{ .KTHXBYE_Word = lib.parser.ast.KTHXBYE_WordNode{ ... } } } }
^ when printing out stmts.items as the program runs
no, but thats a good idea haha
yeah it's just I'm not sure there's much I can really gather from code snippet without having the entire thing at hand
but if you can see what the debugger says is in items that would be helpful, or even just seeing what the actual output of parse_statement is on each iteration
do you expect the catch &[_]ast.StatementNode{} to be triggered often?
cuz that will be a pointer to stack memory which can change later as different functions get called
also can i see parse_statement()
&.{} should be okay, because it contains no elements.
That would certainly be an issue though if it had at least one.
I'd need to see more of the context to really be able to diagnose this.