#ArrayList, appending struct causes overwrite on previous values

1 messages · Page 1 of 1 (latest)

sterile granite
#

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:

velvet latch
#

make sure that the arraylist is the same arraylist each time, aka you aren't copying the structure

sterile granite
#

i don't think I am anywhere

velvet latch
#

ah I see

#

sorry I thought you meant actually overwriting it like replacing it, I was expecting to see "HAI" somewhere

sterile granite
#

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

sterile granite
#

{ 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

velvet latch
#

have you tried stepping through it with a debugger?

#

breakpoint on .append

sterile granite
#

no, but thats a good idea haha

velvet latch
#

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

woven sleet
#

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()

noble portal
#

That would certainly be an issue though if it had at least one.

noble portal