/// <summary>
        /// Parses the If statment
        /// </summary>
        internal void ParseIfStatementDeclaration(out Expr expr)
        {
            expr = null;
            var ifKeyWord = _scanner.GetToken();
            if (!ifKeyWord.IsReserverdWord())
            {
                throw new NireExecutionException("If key word not found... Parse broke");
            }

            var lparen = _scanner.GetToken();

            using (new ParserContext(ParserContextEnum.IfDecl))
            {
                expr = ParseIfCondition(expr, lparen);

                var bracket = _scanner.GetToken();

                if (bracket.Kind == ByteCodeIdentifiers.TokenCode.LBracket)
                {
                    using (new ParserContext(ParserContextEnum.IfBody))
                    {
                        bracket = ParseToRightBracket(expr, bracket, boe.Peek());
                        boe.Pop();
                    }

                    var isElse = _scanner.Peek();
                    if (isElse.IsReserverdWord() && isElse.IdentiferName.Equals("else"))
                    {
                        _scanner.GetToken();
                        var isIf = _scanner.Peek();
                        if (isIf.IsReserverdWord() && isIf.IdentiferName.Equals("if"))
                        {
                            using (new ParserContext(ParserContextEnum.ElseIfBody))
                            {
                                ParseStatement();
                            }
                        }
                        else
                        {
                            bracket = _scanner.GetToken();
                            if (bracket.Kind == ByteCodeIdentifiers.TokenCode.LBracket)
                            {
                                using (new ParserContext(ParserContextEnum.ElseBody))
                                {
                                    var trueExpression = new BinaryOperatorExpression(true);
                                    ParseToRightBracket(expr, bracket, trueExpression);
                                }
                            }
                        }
                    }
                }
            }
        }
 private Token ParseToRightBracket(Expr expr, Token bracket, BinaryOperatorExpression boe)
 {
     while (bracket.Kind != ByteCodeIdentifiers.TokenCode.RBracket)
     {
         ParseStatementExpression();
         ((IfDeclExpr)expr).AddStatementToCondition(boe, this.BinaryExpression);
         bracket = _scanner.GetToken();
     }
     return bracket;
 }
        /// <summary>
        /// Adds the body statements to the condition map
        /// </summary>
        /// <param name="boe">binary expression</param>
        /// <param name="stmt">expression statement</param>
        internal void AddStatementToCondition(BinaryOperatorExpression boe, Expr stmt)
        {
            if (ParserContext.Context == ParserContextEnum.ElseBody)
            {
                if (!conditionExprMap.ContainsKey(boe))
                {
                    this.AddCondition(boe);
                    conditionExprMap[boe].Add(stmt);
                    return;
                }
            }

            conditionExprMap[boe].Add(stmt);
        }
 internal void AddCondition(BinaryOperatorExpression boe)
 {
     conditionExprMap.Add(boe, new List<Expr>());
 }
Example #5
0
        private void ConstructAst(Stack<Expr> operators, Stack<Expr> identfiers)
        {
            var bin = new BinaryOperatorExpression();
            bool bFirst = true;

            //If there are zero operators then assume it is a function call
            // i.e. foo();
            // with no return value.
            if (operators.Count == 0)
            {
                Expr expr = identfiers.Pop();
                AddStatementExpressionTreesToFunctionBlock(expr);
                return;
            }

            while (operators.Count > 0)
            {
                var op = (OperatorExpression) operators.Pop();
                if (op.Arity == Arity.binary)
                {
                    Expr left;
                    Expr right;

                    if (bFirst)
                    {
                        right = identfiers.Pop();
                        left = identfiers.Pop();

                        bFirst = false;

                        bin = new BinaryOperatorExpression(
                            op,
                            left,
                            right);

                        if (operators.Count > 0)
                        {
                            var temp =
                                new BinaryOperatorExpression(
                                new OperatorExpression(
                                    operators.Pop().GetToken())) {Right = bin};

                            bin = temp;
                        }
                    }
                    else
                    {
                        var temp = new BinaryOperatorExpression(op);

                        if (bin.Right == null)
                        {
                            right = identfiers.Pop();

                            bin.Right = right;
                            temp.Right = bin;
                            bin = temp;
                        }
                        else if (bin.Left == null)
                        {
                            left = identfiers.Pop();

                            bin.Left = left;
                            temp.Right = bin;
                            bin = temp;
                        }
                    }
                }
            }
            if (identfiers.Count > 0)
            {
                Expr expr = identfiers.Pop();
                if (bin.Left == null)
                {
                    bin.Left = expr;
                }
            }

            AddStatementExpressionTreesToFunctionBlock(bin);
            this.BinaryExpression = bin;
        }
Example #6
0
        private void GenerateBinaryExpression(
      TypeBuilder typeBuilder,
      BinaryOperatorExpression bin)
        {
            if (bin != null)
              {
            ILGenerator generator = this.methodbuilder.GetILGenerator();
            if (generator != null)
            {
              LocalBuilder localBuilder = generator.DeclareLocal(typeof (Int32));
              localBuilder.SetLocalSymInfo("x");

              generator.Emit(OpCodes.Ldc_I4_3);

              EmitStoreLocWithIndex(localBuilder.LocalIndex, generator);
            }
              }
        }