我有一个尖锐的代码,用于每个循环打印list.Code中的项目
static bool Process_component_statement(List<Evl_component> components,
Evl_statement s)
{
Debug.Assert(!(s.type == Evl_statement.Statement_type.ENDMODULE));
State_Type state = State_Type.INIT;
Evl_component comp = new Evl_component();
Evl_pin pin = new Evl_pin();
while(s.tokens.Count != 0 && (state != State_Type.DONE))
{
Evl_token t = s.tokens.First();
s.tokens.Remove(s.tokens.First());
// branches here to compute state transitions
if (state == State_Type.INIT)
{
if (t.type == Evl_token.Token_type.NAME)
{
comp.type = t.str;
comp.name = "NONE";
state = State_Type.TYPE;
}
}
else if (state == State_Type.TYPE)
{
if (t.type == Evl_token.Token_type.NAME)
{
comp.name = t.str;
state = State_Type.NAME;
}
else if (t.str == "(")
{
state = State_Type.PINS;
}
}
else if (state == State_Type.NAME)
{
if (t.str == "(")
{
state = State_Type.PINS;
}
}
else if (state == State_Type.PINS)
{
if (t.type == Evl_token.Token_type.NAME)
{
pin.name = t.str; pin.bus_msb = -1; pin.bus_lsb = -1;
state = State_Type.PIN_NAME;
}
}
else if (state == State_Type.PIN_NAME)
{
if (t.str == ",")
{
comp.pins.Add(pin);
state = State_Type.PINS;
}
else if (t.str == ")")
{
comp.pins.Add(pin);
state = State_Type.PINS_DONE;
}
}
else if (state == State_Type.PINS_DONE)
{
if (t.str == ";")
{
state = State_Type.DONE;
}
}
else if (state == State_Type.DONE)
{
return true;
}
else
{
Debug.Assert(false); // shouldn't reach here;
}
}
components.Add(comp);
if (s.tokens.Count != 0 || (state != State_Type.DONE))
{
Console.Error.WriteLine("something wrong with the statement");
return false;
}
return true;
}
并且在main中如果我使用输入,输出我看到的只是最后一个元素的重复,似乎只有最后一个元素在列表中添加并且它在输出中重复。
component not NONE 3
pin in2 -1 -1
pin in2 -1 -1
pin in2 -1 -1
如何获得类似
的输出component not NONE 3
pin out -1 -1
pin in1 -1 -1
pin in2 -1 -1
答案 0 :(得分:1)
更改看起来像的所有代码:
comp.pins.Add(pin);
为:
comp.pins.Add(pin);
pin = new Evl_pin();
您的代码(按原样)的问题是您重复添加相同的引脚。所以问题不在于output i see is repetition of only last element
- 它是你的第一,第二,第三元素等都是相同的元素。上述变化应该解决这个问题。