Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssignmentNode.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.Interpreter;
18 using Irony.Parsing;
19 
20 namespace Irony.Interpreter.Ast {
21  public class AssignmentNode : AstNode {
22  public AstNode Target;
23  public string AssignmentOp;
24  public string BaseOp; //base arithm operation (+) for augmented assignment like "+="
26 
27  public AssignmentNode() {}
28 
29  public override void Init(ParsingContext context, ParseTreeNode treeNode) {
30  base.Init(context, treeNode);
31  Target = AddChild("To", treeNode.ChildNodes[0]);
32  //Get Op and baseOp if it is combined assignment
33  AssignmentOp = treeNode.ChildNodes[1].FindTokenAndGetText();
34  if (string.IsNullOrEmpty(AssignmentOp))
35  AssignmentOp = "=";
36  if (AssignmentOp.Length > 1) {
37  //it is combined op
38  BaseOp = AssignmentOp.Replace("=", string.Empty);
39  }
40  //There maybe an "=" sign in the middle, or not - if it is marked as punctuation; so we just take the last node in child list
41  var lastIndex = treeNode.ChildNodes.Count - 1;
42  Expression = AddChild("Expr", treeNode.ChildNodes[lastIndex]);
43  AsString = AssignmentOp + " (assignment)";
44  if (string.IsNullOrEmpty(BaseOp))
45  EvaluateRef = EvaluateSimple;
46  else
47  EvaluateRef = EvaluateCombined;
48  }
49 
50  private void EvaluateSimple(EvaluationContext context, AstMode mode) {
51  Expression.Evaluate(context, AstMode.Read);
52  Target.Evaluate(context, AstMode.Write); //writes the value into the slot
53  }
54  private void EvaluateCombined(EvaluationContext context, AstMode mode) {
55  Target.Evaluate(context, AstMode.Read);
56  Expression.Evaluate(context, AstMode.Read);
57  context.CallDispatcher.ExecuteBinaryOperator(BaseOp);
58  Target.Evaluate(context, AstMode.Write); //writes the value into the slot
59  }
60 
61  }
62 }
override void Init(ParsingContext context, ParseTreeNode treeNode)
ParseTreeNodeList ChildNodes
Definition: ParseTree.cs:41