public VMOp Pop() { VMOp ret = this.Peek(); this.Stack[this.StackP] = null; this.StackP--; return(ret); }
private void RecTree(YuTreeNode node) { if (node.Children[0].Token.Type == ETokenType.ExpAdd) { this.RecTree(node.Children[0]); } else { if (node.Children[0].Token.Type == ETokenType.Symbol) { YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token); if (this.SymbolTable.IsContain(row) == false) { throw new YuSymbolUndefinedException(); } VMOp op = new VMOp(EOpCode.PushAddr, -1); this.OpLst.Add(op); this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token)); } else { this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[0].Token.Text))); } } if (node.Children[1].Token.Type == ETokenType.ExpAdd) { this.RecTree(node.Children[1]); } else { if (node.Children[1].Token.Type == ETokenType.Symbol) { YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[1].Token); if (this.SymbolTable.IsContain(row) == false) { throw new YuSymbolUndefinedException(); } VMOp op = new VMOp(EOpCode.PushAddr, -1); this.OpLst.Add(op); this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[1].Token)); } else { this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Token.Text))); } } this.OpLst.Add(new VMOp(EOpCode.Add, 0)); }
public void TranslateTree(YuTreeNode node) { if (node.Token.Type == ETokenType.ExpAdd) { this.RecTree(node); } else if (node.Token.Type == ETokenType.Var) { YuSymbolTableRow sr = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token); this.SymbolTable.Add(sr); } else if (node.Token.Type == ETokenType.Equal) { /* * if (node.Children[1].Children[0].Token.Type == ETokenType.Number) * { * this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Children[0].Token.Text))); * this.OpLst.Add(new VMOp(EOpCode.Push, int.Parse(node.Children[1].Children[1].Token.Text))); * this.OpLst.Add(new VMOp(EOpCode.Add, 0)); * } */ this.RecTree(node.Children[1]); YuSymbolTableRow row = new YuSymbolTableRow(ESymbolType.Var, node.Children[0].Token); if (this.SymbolTable.IsContain(row) == false) { throw new YuSymbolUndefinedException(); } VMOp op = new VMOp(EOpCode.Pop, -1); this.OpLst.Add(op); this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token)); } else if (node.Token.Type == ETokenType.Def) { YuSymbolTableRow sr = new YuSymbolTableRow(ESymbolType.Proc, node.Children[0].Token, node); this.SymbolTable.Add(sr); } else if (node.Token.Type == ETokenType.Call) { VMOp op = new VMOp(EOpCode.Call, -1); this.OpLst.Add(op); this.NeedResolvLst.Add(new Tuple <VMOp, YuToken>(op, node.Children[0].Token)); } else if (node.Token.Type == ETokenType.Eof) { this.OpLst.Add(new VMOp(EOpCode.End, 0)); } }
public void Push(VMOp x) { if (this.Stack.Count == 0) { this.Stack.Add(x); this.StackP = 0; } else if ((this.StackP + 1) == this.Stack.Count) { this.Stack.Add(x); this.StackP++; } else { this.StackP++; this.Stack[this.StackP] = x; } }
public bool RunStep() { bool ret = true; if (this.Pc >= this.Mem.Count) { this.Stack.ClearStack(); return(false); } VMOp currop = this.Mem[this.Pc]; switch (currop.OpCd) { case EOpCode.Nop: break; case EOpCode.Push: this.Stack.Push(new VMOp(EOpCode.Data, currop.OpRnd)); break; case EOpCode.PushAddr: this.Stack.Push(this.Mem[currop.OpRnd]); break; case EOpCode.Pop: this.Mem[currop.OpRnd] = this.Stack.Pop(); break; case EOpCode.Peek: this.Mem[currop.OpRnd] = this.Stack.Peek(); break; case EOpCode.End: this.Stack.ClearStack(); ret = false; break; case EOpCode.Add: int a = this.Stack.Pop().OpRnd; int b = this.Stack.Pop().OpRnd; this.Stack.Push(new VMOp(EOpCode.Data, a + b)); break; case EOpCode.Call: case EOpCode.Return: break; case EOpCode.Data: throw new YuDataExecutionException(); case EOpCode.Out: throw new NotImplementedException(); default: throw new YuUnKnownOpCodeException(); } if (currop.OpCd == EOpCode.Call) { this.Stack.Push(new VMOp(EOpCode.Data, this.Pc + 1)); this.Pc = currop.OpRnd; } else if (currop.OpCd == EOpCode.Return) { this.Pc = this.Stack.Pop().OpRnd; } else { this.Pc++; } return(ret); }