Exemplo n.º 1
0
        private FactorNode Visit(FactorNode node)
        {
            dynamic props = new ExpandoObject();

            properties.Add(node, props);
            var code = new List <CodeEntry>();

            props.code = code;
            switch (node.type)
            {
            case 1:
                if (!assignedSymbols.Contains(node.Identifier))
                {
                    var ele = (node.Child[0] as TerminalNode).Lex as IdentifierElement;
                    Warnings.Add($"{ele.LineNumber}:[{ele.StartIndex}, {ele.EndIndex}): Unassigned identifier {node.Identifier}");
                }
                props.addr = node.Identifier;
                break;

            case 2:
                props.addr = node.value;
                break;

            case 3:
                code.AddRange(properties[node.e1].code);
                props.addr = properties[node.e1].addr;
                break;
            }

            return(node);
        }
Exemplo n.º 2
0
        private void Factor(FactorNode node)
        {
            switch (next.Tag)
            {
            case Tag.ID:
                if (Lookahead(2).Tag == Tag.DL_LPAR)
                {
                    node.func = new FuncCallStmtNode();
                    FuncCallStmt(node.func);
                }
                else if (Lookahead(2).Tag == Tag.DL_LSQU)
                {
                    node.value = new IdNode(next.Value);
                    Move();
                    Move();
                    node.indexer = new ExprNode();
                    Expr(node.indexer);
                    RequiredToken(Tag.DL_RSQU);
                }
                else
                {
                    node.value = new IdNode(next.Value);
                    Move();
                }
                break;

            case Tag.NUM:
                node.value = new IntNode(IntParse(next.Value));
                Move();
                break;

            case Tag.DL_LPAR:
                Move();
                node.expr = new ExprNode();
                Expr(node.expr);
                RequiredToken(Tag.DL_RPAR);
                break;

            case Tag.KW_TRUE:
            case Tag.KW_FALSE:
                node.value = new BoolNode(bool.Parse(next.Value));
                Move();
                break;

            default:
                new UnknownTokenError().PrintErrMsg();
                break;
            }
        }
Exemplo n.º 3
0
        public IFactorNode BuildTree()
        {
            var factorNode = new FactorNode(new Token(), _tracer)
            {
                PreActionNode = string.IsNullOrEmpty(PreActionName)
               ? null
               : new ActionNode(new Token(TokenKind.Action, PreActionName), _tracer),
                PostActionNode = string.IsNullOrEmpty(PostActionName)
               ? null
               : new ActionNode(new Token(TokenKind.Action, PostActionName), _tracer)
            };

            var factExprNode = _factExpr.BuildTree();

            factorNode.FactorExpr = factExprNode;

            return(factorNode);
        }
Exemplo n.º 4
0
 private FactorNode Visit(FactorNode node)
 {
     if (node.type == 3)
     {
         Visit(node.e1);
         GetProperty(node).addr = GetProperty(node.e1).addr;
     }
     else if (node.type == 2)
     {
         GetProperty(node).addr = LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, (ulong)node.value);
     }
     else
     {
         var y = builder.BuildLoad(LookupSymbolOrAlloc(node.Identifier));
         GetProperty(node).addr = y;
     }
     return(node);
 }
Exemplo n.º 5
0
        public void FactorNode_WithParens_ReturnsCorrectString()
        {
            // Arranged:
            var parenNodeMock = new Mock <IParenNode>();

            parenNodeMock.Setup(node => node.AstNodeType).Returns(AstNodeType.Expression);
            parenNodeMock.Setup(node => node.ToString()).Returns(() => "( <U> )");

            var factNode = new FactorNode(new Token(), _tracer);

            factNode.FactorExpr = parenNodeMock.Object;

            // Act:
            var actual = factNode.ToString();

            // Assert:
            Assert.That(actual, Is.EqualTo("( <U> )"));
        }
Exemplo n.º 6
0
        public void FactorNode_WithTerminal_ReturnsCorrectString()
        {
            // Arranged:
            var terminalNodeMock = new Mock <ITerminalNode>();

            terminalNodeMock.Setup(node => node.AstNodeType).Returns(AstNodeType.Expression);
            terminalNodeMock.Setup(node => node.ToString()).Returns(() => "\"a\"");

            var factNode = new FactorNode(new Token(TokenKind.String, "a"), _tracer)
            {
                FactorExpr = terminalNodeMock.Object
            };

            // Act:
            var actual = factNode.ToString();

            // Assert:
            Assert.That(actual, Is.EqualTo("\"a\""));
        }
Exemplo n.º 7
0
        public void FactorNode_WithIdentifier_ReturnsCorrectString()
        {
            // Arranged:
            var prodRefNode = new Mock <IProdRefNode>();

            prodRefNode.Setup(terminalNode => terminalNode.AstNodeType).Returns(AstNodeType.ProdRef);
            prodRefNode.Setup(terminalNode => terminalNode.ToString()).Returns(() => "<T>");

            var factNode = new FactorNode(new Token(TokenKind.Identifier, "T"), _tracer)
            {
                FactorExpr = prodRefNode.Object
            };

            // Act:
            var actual = factNode.ToString();

            // Assert:
            Assert.That(actual, Is.EqualTo("<T>"));
        }
Exemplo n.º 8
0
        public List <Type> bfs(BaseNode node)
        {
            if (node.GetType() == typeof(LiteralNode))
            {
                LiteralNode n    = (LiteralNode)node;
                List <Type> temp = new List <Type>();
                temp.Add(n.getReturnType());
                return(temp);
            }

            List <BaseNode> children    = node.getChildren().ToList();
            List <Type>     returnTypes = new List <Type>();

            for (int i = 0; i < children.Count(); i++)
            {
                if (children[i] == null || children[i].GetType() == typeof(PrintNode))
                {
                    continue;
                }
                List <Type> temp = bfs(children[i]);
                for (int j = 0; j < temp.Count; j++)
                {
                    returnTypes.Add(temp[j]);
                }
            }

            //check for and | or | xor
            if (node.GetType() == typeof(ExprNode))
            {
                List <Type> finalResult = new List <Type>();
                finalResult.Add(Type.Bool);
                ExprNode n = (ExprNode)node;
                if (n.getRightRelation() != null)
                {
                    RelNode left = n.getLeftRelation(), right = n.getRightRelation();
                    if (returnTypes[0] != Type.Bool && returnTypes[1] != Type.Bool)
                    {
                        throw new Exception("wrong comparison of types");
                    }

                    return(finalResult);
                }
            }

            //check for < | <= | > | >= | = | /=
            if (node.GetType() == typeof(RelNode))
            {
                List <Type> finalResult = new List <Type>();
                finalResult.Add(Type.Bool);
                RelNode n = (RelNode)node;
                if (n.getRFactor() != null && returnTypes.Count == 2)
                {
                    Type left = returnTypes[0], right = returnTypes[1];
                    if (left == Type.Int && right == Type.Int)
                    {
                    }
                    else if (left == Type.Int && right == Type.Real)
                    {
                    }
                    else if (left == Type.Real && right == Type.Int)
                    {
                    }
                    else if (left == Type.Real && right == Type.Real)
                    {
                    }
                    else
                    {
                        throw new Exception("Comparison types error");
                    }
                    return(finalResult);
                }
            }

            //check for +,-
            if (node.GetType() == typeof(FactorNode))
            {
                FactorNode n = (FactorNode)node;
                if (n.getTail() != null && returnTypes.Count > 0)
                {
                    Type        result      = returnTypes[0];
                    List <Type> finalResult = new List <Type>();
                    if (result == Type.Func)
                    {
                        result = Type.Int;
                    }



                    for (int i = 1; i < returnTypes.Count; i++)
                    {
                        Type        temp     = returnTypes[i];
                        ComplexTerm tempTerm = (ComplexTerm)children[i];
                        FactorOp    sign     = tempTerm.getop();


                        if (sign == FactorOp.Minus)
                        {
                            if (result == Type.Int && temp == Type.Int)
                            {
                            }
                            else if (result == Type.Int && temp == Type.Real)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Int)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Real)
                            {
                            }
                            else
                            {
                                throw new Exception("Wrong subtraction found");
                            }
                        }
                        else if (sign == FactorOp.Plus)
                        {
                            if (result == Type.Int && temp == Type.Int)
                            {
                            }
                            else if (result == Type.Int && temp == Type.Real)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Int)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Real)
                            {
                            }
                            else if (result == Type.String && temp == Type.String)
                            {
                            }
                            else if (result == Type.Tup && temp == Type.Tup)
                            {
                            }
                            else if (result == Type.Arr && temp == Type.Arr)
                            {
                            }
                            else
                            {
                                throw new Exception("Wrong addition found");
                            }
                        }
                    }
                    finalResult.Add(result);
                    return(finalResult);
                }
            }
            //check for /,*
            if (node.GetType() == typeof(TermNode))
            {
                TermNode n = (TermNode)node;
                if (n.getTail() != null)
                {
                    Type        result      = returnTypes[0];
                    List <Type> finalResult = new List <Type>();
                    if (result == Type.Func)
                    {
                        result = Type.Int;
                    }

                    for (int i = 1; i < returnTypes.Count; i++)
                    {
                        Type         temp      = returnTypes[i];
                        ComplexUnary tempUnary = (ComplexUnary)children[i];
                        TermOp       sign      = tempUnary.getOp();
                        if (sign == TermOp.Div)
                        {
                            if (result == Type.Int && temp == Type.Int)
                            {
                            }
                            else if (result == Type.Int && temp == Type.Real)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Int)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Real)
                            {
                            }
                            else
                            {
                                throw new Exception("Wrong division found");
                            }
                        }
                        else if (sign == TermOp.Mult)
                        {
                            if (result == Type.Int && temp == Type.Int)
                            {
                            }                                           //result stays the same
                            else if (result == Type.Int && temp == Type.Real)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Int)
                            {
                                result = Type.Real;
                            }
                            else if (result == Type.Real && temp == Type.Real)
                            {
                            }
                            else
                            {
                                throw new Exception("Wrong multiplication found");
                            }
                        }
                    }
                    finalResult.Add(result);
                    return(finalResult);
                }
            }

            /*Console.Write(node.GetType() +" : ");
             * for (int i = 0; i < returnTypes.Count; i++)
             * {
             *  Console.Write(returnTypes[i] + " ");
             * }
             * Console.WriteLine();*/
            return(returnTypes);
        }
 public F_V_Message(FactorNode factor, VariableNode variable)
 {
     this.factor = factor;
     this.variable = variable;
 }
Exemplo n.º 10
0
        public List <KeyValuePair <Type, object> > dfs(BaseNode node)
        {
            if (node.GetType() == typeof(LiteralNode))
            {
                LiteralNode n = (LiteralNode)node;
                if (n.type == LiteralType.Func)
                {
                    return(dfs((FuncLiteral)n.Value));
                }
                else if (n.type == LiteralType.Tup)
                {
                }
                else
                {
                    List <KeyValuePair <Type, object> > temp = new List <KeyValuePair <Type, object> >();
                    KeyValuePair <Type, object>         pair = new KeyValuePair <Type, object>(n.getReturnType(), n.Value);
                    temp.Add(pair);
                    return(temp);
                }
            }
            if (node.GetType() == typeof(IfNode))
            {
                IfNode n = (IfNode)node;

                List <KeyValuePair <Type, object> > ifUslovie = dfs(n.expr);
                bool uslovie = (bool)ifUslovie[0].Value;
                if (uslovie)
                {
                    return(dfs(n.body));
                }
                else
                {
                    if (n.else_body != null)
                    {
                        return(dfs(n.else_body));
                    }
                }
            }
            List <BaseNode> children = node.getChildren().ToList();

            List <KeyValuePair <Type, object> > returnTypes = new List <KeyValuePair <Type, object> >(); //our return objects

            for (int i = 0; i < children.Count(); i++)
            {
                if (children[i] == null)
                {
                    continue;
                }
                List <KeyValuePair <Type, object> > temp = dfs(children[i]);
                for (int j = 0; j < temp.Count; j++)
                {
                    returnTypes.Add(temp[j]);
                }
            }

            if (node.GetType() == typeof(LoopNode))
            {
                LoopNode n          = (LoopNode)node;
                LoopType loopType   = n.type;
                String   identifier = n.id;
                if (loopType == LoopType.For)
                {
                    int top = (int)returnTypes[1].Value;

                    intVariables[identifier] = (int)returnTypes[0].Value;

                    while (intVariables[identifier] < top)
                    {
                        dfs(n.fields["body"]);
                        intVariables[identifier]++;
                    }
                    while (intVariables[identifier] > top)
                    {
                        dfs(n.fields["body"]);
                        intVariables[identifier]--;
                    }
                }

                if (loopType == LoopType.While)
                {
                    bool uslovie = (bool)dfs(n.fields["expr"])[0].Value;
                    while (uslovie)
                    {
                        dfs(n.fields["body"]);
                        uslovie = (bool)dfs(n.fields["expr"])[0].Value;
                    }
                }
                return(returnTypes);
            }
            if (node.GetType() == typeof(RelNode))
            {
                RelNode n = (RelNode)node;
                if (n.getRFactor() != null)
                {
                    List <KeyValuePair <Type, object> > temp  = new List <KeyValuePair <Type, object> >();
                    KeyValuePair <Type, object>         left  = returnTypes[0],
                                                        right = returnTypes[1];
                    string op     = n.Op;
                    bool   result = false;
                    if (left.Key == Type.Int && right.Key == Type.Int)
                    {
                        if (op == "lt_t")
                        {
                            result = (int)left.Value < (int)right.Value;
                        }
                        else if (op == "gt_t")
                        {
                            result = (int)left.Value > (int)right.Value;
                        }
                        else if (op == "le_t")
                        {
                            result = (int)left.Value <= (int)right.Value;
                        }
                        else if (op == "ge_t")
                        {
                            result = (int)left.Value >= (int)right.Value;
                        }
                        else if (op == "eq_t")
                        {
                            result = (int)left.Value == (int)right.Value;
                        }
                        else if (op == "neq_t")
                        {
                            result = (int)left.Value != (int)right.Value;
                        }
                    }
                    else if (left.Key == Type.Int && right.Key == Type.Real)
                    {
                        if (op == "lt_t")
                        {
                            result = (int)left.Value < (double)right.Value;
                        }
                        else if (op == "gt_t")
                        {
                            result = (int)left.Value > (double)right.Value;
                        }
                        else if (op == "le_t")
                        {
                            result = (int)left.Value <= (double)right.Value;
                        }
                        else if (op == "ge_t")
                        {
                            result = (int)left.Value >= (double)right.Value;
                        }
                        else if (op == "eq_t")
                        {
                            result = (int)left.Value == (double)right.Value;
                        }
                        else if (op == "neq_t")
                        {
                            result = (int)left.Value != (double)right.Value;
                        }
                    }
                    else if (left.Key == Type.Real && right.Key == Type.Int)
                    {
                        if (op == "lt_t")
                        {
                            result = (double)left.Value < (int)right.Value;
                        }
                        else if (op == "gt_t")
                        {
                            result = (double)left.Value > (int)right.Value;
                        }
                        else if (op == "le_t")
                        {
                            result = (double)left.Value <= (int)right.Value;
                        }
                        else if (op == "ge_t")
                        {
                            result = (double)left.Value >= (int)right.Value;
                        }
                        else if (op == "eq_t")
                        {
                            result = (double)left.Value == (int)right.Value;
                        }
                        else if (op == "neq_t")
                        {
                            result = (double)left.Value != (int)right.Value;
                        }
                    }

                    else if (left.Key == Type.Real && right.Key == Type.Real)
                    {
                        if (op == "lt_t")
                        {
                            result = (double)left.Value < (double)right.Value;
                        }
                        else if (op == "gt_t")
                        {
                            result = (double)left.Value > (double)right.Value;
                        }
                        else if (op == "le_t")
                        {
                            result = (double)left.Value <= (double)right.Value;
                        }
                        else if (op == "ge_t")
                        {
                            result = (double)left.Value >= (double)right.Value;
                        }
                        else if (op == "eq_t")
                        {
                            result = (double)left.Value == (double)right.Value;
                        }
                        else if (op == "neq_t")
                        {
                            result = (double)left.Value != (double)right.Value;
                        }
                    }
                    else
                    {
                        throw new Exception("Wrong types comparison");
                    }
                    temp.Add(new KeyValuePair <Type, object>(Type.Bool, result));
                    return(temp);
                }
            }

            if (node.GetType() == typeof(ExprNode))
            {
                ExprNode n = (ExprNode)node;
                if (n.getRightRelation() != null)
                {
                    List <KeyValuePair <Type, object> > temp = new List <KeyValuePair <Type, object> >();
                    string op = n.getOp();
                    if (returnTypes[0].Key != Type.Bool || returnTypes[1].Key != Type.Bool)
                    {
                        throw new Exception("Wrong or, xor, and operation");
                    }
                    bool left = (bool)returnTypes[0].Value, right = (bool)returnTypes[1].Value;
                    if (op == "or_t")
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Bool, left || right));
                    }
                    if (op == "xor_t")
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Bool, left ^ right));
                    }
                    if (op == "and_t")
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Bool, left && right));
                    }
                }

                return(returnTypes);
            }

            if (node.GetType() == typeof(AssNode))
            {
                AssNode n  = (AssNode)node;
                string  id = n.prim.id;
                object  v  = returnTypes[0].Value;
                if (intVariables.ContainsKey(id))
                {
                    intVariables[id] = (int)v;
                }
                else if (stringVariables.ContainsKey(id))
                {
                    stringVariables[id] = (string)v;
                }
                else if (realVariables.ContainsKey(id))
                {
                    realVariables[id] = (double)v;
                }
                else if (integerArrays.ContainsKey(id))
                {
                    List <KeyValuePair <Type, object> > index = new List <KeyValuePair <Type, object> >(); //our return objects
                    index = dfs(n.prim.indexChildren[0]);
                    integerArrays[id][(int)index[0].Value] = (int)v;
                }
                else if (tupleVariables.ContainsKey(id))
                {
                    throw new Exception("Tuples are immutable");
                }
            }


            if (node.GetType() == typeof(LiteralNode))
            {
                LiteralNode n = (LiteralNode)node;
                if (n.type == LiteralType.Tup)
                {
                    List <KeyValuePair <Type, object> > finalResult = new List <KeyValuePair <Type, object> >(); //our return objects
                    Dictionary <int, object>            v           = new Dictionary <int, object>();
                    for (int i = 0; i < returnTypes.Count; i++)
                    {
                        v.Add(i, returnTypes[i].Value);
                    }

                    finalResult.Add(new KeyValuePair <Type, object>(Type.Tup, v));
                    return(finalResult);
                }
            }
            if (node.GetType() == typeof(DecNode))
            {
                DecNode d = (DecNode)node;
                switch (returnTypes[0].Key)
                {
                case Type.Int:
                    intVariables.Add(d.id, (int)returnTypes[0].Value);
                    break;

                case Type.Real:
                    realVariables.Add(d.id, (double)returnTypes[0].Value);
                    break;

                case Type.String:
                    stringVariables.Add(d.id, (string)returnTypes[0].Value);
                    break;

                case Type.Arr:

                    if (returnTypes[0].Value != null)
                    {
                        integerArrays.Add(d.id, (Dictionary <int, int>)returnTypes[0].Value);
                    }
                    else
                    {
                        integerArrays.Add(d.id, new Dictionary <int, int>());
                    }
                    break;

                case Type.Tup:
                    Dictionary <int, object> v = (Dictionary <int, object>)returnTypes[0].Value;
                    tupleVariables.Add(d.id, v);
                    break;

                case Type.Bool:
                    boolVariables.Add(d.id, (bool)returnTypes[0].Value);
                    break;
                }
            }

            //получение по id
            if (node.GetType() == typeof(PrimaryNode))
            {
                PrimaryNode n = (PrimaryNode)node;
                List <KeyValuePair <Type, object> > temp = new List <KeyValuePair <Type, object> >();
                if (n.type == PrimaryType.Id)
                {
                    string name = n.id;
                    if (intVariables.ContainsKey(name))
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Int, intVariables[name]));
                    }
                    else if (stringVariables.ContainsKey(name))
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.String, stringVariables[name]));
                    }
                    else if (realVariables.ContainsKey(name))
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Real, realVariables[name]));
                    }
                    else if (integerArrays.ContainsKey(name))
                    {
                        if (returnTypes.Count == 0)
                        {
                            temp.Add(new KeyValuePair <Type, object>(Type.Arr, integerArrays[name]));
                        }
                        else
                        {
                            if (!integerArrays[name].ContainsKey((int)returnTypes[0].Value))
                            {
                                integerArrays[name].Add((int)returnTypes[0].Value, -1);
                            }

                            temp.Add(new KeyValuePair <Type, object>(Type.Int,
                                                                     integerArrays[name][(int)returnTypes[0].Value]));
                        }
                    }
                    else if (boolVariables.ContainsKey(name))
                    {
                        temp.Add(new KeyValuePair <Type, object>(Type.Bool, boolVariables[name]));
                    }
                    else if (tupleVariables.ContainsKey(name))
                    {
                        Dictionary <int, object> tuple = tupleVariables[name];

                        int    index = (int)returnTypes[0].Value;
                        object o     = tuple[index];
                        if (IsInt(o))
                        {
                            temp.Add(new KeyValuePair <Type, object>(Type.Int, tuple[index]));
                        }
                        else if (IsReal(o))
                        {
                            temp.Add(new KeyValuePair <Type, object>(Type.Real, tuple[index]));
                        }
                        else if (IsString(o))
                        {
                            temp.Add(new KeyValuePair <Type, object>(Type.String, tuple[index]));
                        }
                    }

                    return(temp);
                }
                if (n.type == PrimaryType.readInt)
                {
                    temp.Add(new KeyValuePair <Type, object>(Type.Int, int.Parse(Console.ReadLine())));
                    return(temp);
                }
                if (n.type == PrimaryType.readReal)
                {
                    temp.Add(new KeyValuePair <Type, object>(Type.Real, double.Parse(Console.ReadLine())));
                    return(temp);
                }
                if (n.type == PrimaryType.readString)
                {
                    temp.Add(new KeyValuePair <Type, object>(Type.String, Console.ReadLine()));
                    return(temp);
                }
            }
            //Принт результатов
            if (node.GetType() == typeof(PrintNode))
            {
                Console.Write("Print :");
                for (int i = 0; i < returnTypes.Count; i++)
                {
                    Console.Write(" " + returnTypes[i].Value);
                }
            }

            // умножение и деление
            if (node.GetType() == typeof(TermNode))
            {
                TermNode n = (TermNode)node;
                if (n.getTail() != null)
                {
                    KeyValuePair <Type, object>         result      = returnTypes[0];
                    List <KeyValuePair <Type, object> > finalResult = new List <KeyValuePair <Type, object> >();
                    for (int i = 1; i < returnTypes.Count; i++)
                    {
                        Type         temp      = returnTypes[i].Key;
                        object       value     = returnTypes[i].Value;
                        ComplexUnary tempUnary = (ComplexUnary)children[i];
                        TermOp       sign      = tempUnary.getOp();
                        if (sign == TermOp.Div)
                        {
                            if (result.Key == Type.Int && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Int, (int)result.Value / (int)value);
                            }
                            else if (result.Key == Type.Int && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value / (int)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value / (int)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value / (int)value);
                            }
                            else
                            {
                                throw new Exception("Wrong division found");
                            }
                        }
                        else if (sign == TermOp.Mult)
                        {
                            if (result.Key == Type.Int && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Int, (int)result.Value * (int)value);
                            }//result stays the same
                            else if (result.Key == Type.Int && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value * (int)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value * (int)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (int)result.Value * (int)value);
                            }
                            else
                            {
                                throw new Exception("Wrong multiplication found");
                            }
                        }
                    }
                    finalResult.Add(result);
                    return(finalResult);
                }
            }
            //сумма и вычитание
            if (node.GetType() == typeof(FactorNode))
            {
                FactorNode n = (FactorNode)node;

                if (n.getTail() != null)
                {
                    KeyValuePair <Type, object>         result      = returnTypes[0];
                    List <KeyValuePair <Type, object> > finalResult = new List <KeyValuePair <Type, object> >();

                    for (int i = 1; i < returnTypes.Count; i++)
                    {
                        Type        temp     = returnTypes[i].Key;
                        object      value    = returnTypes[i].Value;
                        ComplexTerm tempTerm = (ComplexTerm)children[i];
                        FactorOp    sign     = tempTerm.getop();

                        if (sign == FactorOp.Minus)
                        {
                            if (result.Key == Type.Int && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Int, (int)result.Value - (int)value);
                            }
                            else if (result.Key == Type.Int && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value - (double)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value - (double)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value - (double)value);
                            }
                            else
                            {
                                throw new Exception("wrong type subtraction");
                            }
                        }
                        else if (sign == FactorOp.Plus)
                        {
                            if (result.Key == Type.Int && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Int, (int)result.Value + (int)value);
                            }
                            else if (result.Key == Type.Int && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value + (double)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Int)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value + (double)value);
                            }
                            else if (result.Key == Type.Real && temp == Type.Real)
                            {
                                result = new KeyValuePair <Type, object>(Type.Real, (double)result.Value + (double)value);
                            }
                            else if (result.Key == Type.String && temp == Type.String)
                            {
                                result = new KeyValuePair <Type, object>(Type.String, (string)result.Value + (string)value);
                            }
                            else if (result.Key == Type.Arr && temp == Type.Arr)
                            {
                                Dictionary <int, int> first  = (Dictionary <int, int>)result.Value;
                                Dictionary <int, int> second = (Dictionary <int, int>)value;
                                Dictionary <int, int> q      = (from e in first.Concat(second)
                                                                group e by e.Key into g
                                                                select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) })
                                                               .ToDictionary(item => item.Name, item => item.Count);
                                result = new KeyValuePair <Type, object>(Type.Arr, q);
                            }

                            else
                            {
                                throw new Exception("wrong type summation");
                            }
                        }
                    }
                    finalResult.Add(result);
                    return(finalResult);
                }
            }

            return(returnTypes);
        }
Exemplo n.º 11
0
 public TermFactorNode(FactorNode Factor)
 {
     this.Factor = Factor;
 }
Exemplo n.º 12
0
 public TermDivNode(FactorNode Factor, TermNode Term)
 {
     this.Factor = Factor;
     this.Term   = Term;
 }
Exemplo n.º 13
0
        private Mock <IAstNodeFactory> BuildNodeFactory()
        {
            var mock = new Mock <IAstNodeFactory>();

            // Syntax
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Syntax),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new SyntaxNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Statement
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Statement),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new StatementNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Expression
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Expression),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new ExpressionNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Term
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Term),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new TermNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Factor
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Factor),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new FactorNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Paren
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Paren),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new ParenNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Option
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Option),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new OptionNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Kleene
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.KleeneStar),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new KleeneNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // ProdRef
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.ProdRef),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new ProdRefNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Terminal
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Terminal),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new TerminalNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            });

            // Action
            mock.Setup(factory =>
                       factory.Create(It.Is <AstNodeType>(nodeType => nodeType == AstNodeType.Action),
                                      It.IsAny <IToken>()))
            .Returns((AstNodeType nodeType, IToken token) =>
            {
                var node = new ActionNode(token, _tracerMock.Object);
                _allNodes.Add(node);
                return(node);
            }); mock.Setup(factory => factory.AllNodes).Returns(_allNodes);

            return(mock);
        }
 public V_F_Message(VariableNode variable, FactorNode factor)
 {
     this.factor = factor;
     this.variable = variable;
 }