Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
UnaryOperationNode.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 UnaryOperationNode : AstNode {
23  public string Op;
24  public string UnaryOp;
25  public AstNode Argument;
26 
27  public UnaryOperationNode() { }
28  public override void Init(ParsingContext context, ParseTreeNode treeNode) {
29  base.Init(context, treeNode);
30  Op = treeNode.ChildNodes[0].FindTokenAndGetText();
31  Argument = AddChild("Arg", treeNode.ChildNodes[1]);
32  base.AsString = Op + "(unary op)";
33  // setup evaluation method;
34  switch (Op) {
35  case "+": EvaluateRef = EvaluatePlus; break;
36  case "-": EvaluateRef = EvaluateMinus; break;
37  case "!": EvaluateRef = EvaluateNot; break;
38  default:
39  string msg = string.Format(Resources.ErrNoImplForUnaryOp, Op);
40  throw new AstException(this, msg);
41  }//switch
42  }
43 
44  #region Evaluation methods
45 
46  private void EvaluatePlus(EvaluationContext context, AstMode mode) {
47  Argument.Evaluate(context, AstMode.Read);
48  }
49 
50  private void EvaluateMinus(EvaluationContext context, AstMode mode) {
51  context.Data.Push((byte)0);
52  Argument.Evaluate(context, AstMode.Read);
53  context.CallDispatcher.ExecuteBinaryOperator("-");
54  }
55 
56  private void EvaluateNot(EvaluationContext context, AstMode mode) {
57  Argument.Evaluate(context, AstMode.Read);
58  var value = context.Data.Pop();
59  var bValue = (bool) context.Runtime.BoolResultConverter(value);
60  context.Data.Push(!bValue);
61  }
62  #endregion
63 
64  }//class
65 }//namespace
ParseTreeNodeList ChildNodes
Definition: ParseTree.cs:41
static string ErrNoImplForUnaryOp
Looks up a localized string similar to UnExprNode: no implementation for unary operator '{0}'...
override void Init(ParsingContext context, ParseTreeNode treeNode)