Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
IncDecNode.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using System.Text;
17 using Irony.Parsing;
18 using Irony.Interpreter;
19 
20 namespace Irony.Interpreter.Ast {
21 
22  public class IncDecNode : AstNode {
23  public string Op;
24  public string BinaryOp; //corresponding binary operation: + for ++, - for --
25  public AstNode Argument;
26  public bool IsPostfix;
27 
28  public override void Init(ParsingContext context, ParseTreeNode treeNode) {
29  base.Init(context, treeNode);
30  FindOpAndDetectPostfix(context, treeNode);
31  int argIndex = IsPostfix? 0 : 1;
32  Argument = AddChild("Arg", treeNode.ChildNodes[argIndex]);
33  BinaryOp = Op[0].ToString(); //take a single char out of ++ or --
34  base.AsString = Op + (IsPostfix ? "(postfix)" : "(prefix)");
35  }
36 
37  private void FindOpAndDetectPostfix(ParsingContext context, ParseTreeNode treeNode) {
38  IsPostfix = false; //assume it
39  Op = treeNode.ChildNodes[0].FindTokenAndGetText();
40  if (Op == "--" || Op == "++") return;
41  IsPostfix = true;
42  Op = treeNode.ChildNodes[1].FindTokenAndGetText();
43  if (Op == "--" || Op == "++") return;
44  //report error
46  }
47 
48  public override void EvaluateNode(EvaluationContext context, AstMode mode) {
49  Argument.Evaluate(context, AstMode.Read);
50  var result = context.LastResult;
51  context.Data.Push(1);
52  context.CallDispatcher.ExecuteBinaryOperator(BinaryOp);
53  //prefix op: result of operation is the value AFTER inc/dec; so overwrite the result value
54  if (!IsPostfix)
55  result = context.LastResult;
56  Argument.Evaluate(context, AstMode.Write); //write value into variable
57  context.Data.Push(result);
58  }
59 
60  }//class
61 
62 }
A strongly-typed resource class, for looking up localized strings, etc.
ParseTreeNodeList ChildNodes
Definition: ParseTree.cs:41
override void EvaluateNode(EvaluationContext context, AstMode mode)
Definition: IncDecNode.cs:48
override void Init(ParsingContext context, ParseTreeNode treeNode)
Definition: IncDecNode.cs:28
static string ErrInvalidArgsForIncDec
Looks up a localized string similar to Invalid arguments for IncDecNode AST node: either first or sec...