Exemplo n.º 1
0
        public static List<Error> ResloveExpression(Node root, Expression expr, out IType type)
        {
            var errors = new List<Error>();

            var list = ExpressionBuilder.Build(expr);

            foreach (var exprNode in list.Where(exprNode => exprNode.ExprType == ExprType.Var))
            {
                errors.AddRange(IdentifierResolver.ResloveIdentifierType(root, exprNode.Tokens, out type));
                exprNode.Type = type;
            }

            type = list.First().Type;
            foreach (var node in list)
            {
                if ((node.Type != null) && (node.Type.ToString() != type.ToString()))
                {
                    errors.Add(new Error(root.GetSourceIdentifier(), node.Tokens.First(),
                                         string.Format(ErrorMessages.TypeMismatch, node.Type.ToString(), type.ToString())));
                }
            }

            return errors;
        }
Exemplo n.º 2
0
 private static string MakeString(Expression expr)
 {
     var list = ExpressionBuilder.Build(expr);
     return string.Join("", ExpressionBuilder.Raw.Select(x => x.ToString()));
 }
Exemplo n.º 3
0
        public static List<ExprNode> Build(Expression expr)
        {
            Raw = new List<ExprNode>();
            var postFix = new List<ExprNode>();
            ExprNode arrival;
            var oprerator = new Stack<ExprNode>();
            var list = new List<IToken>();
            for(var i=0; i<expr.List.Count; ++i)
            {
                var token = expr.List[i];
                if (IsInteger(token))
                {
                    postFix.Add(new ExprNode(token, ExprType.Num, new PrimitiveType(new CommonToken(23, "integer"))));

                    //for emitter
                    Raw.Add(new ExprNode(token, ExprType.Num, new PrimitiveType(new CommonToken(23, "integer"))));
                }
                else if (IsReal(token))
                {
                    postFix.Add(new ExprNode(token, ExprType.Num, new PrimitiveType(new CommonToken(23, "real"))));

                    //for emitter
                    Raw.Add(new ExprNode(token, ExprType.Num, new PrimitiveType(new CommonToken(23, "real"))));
                }
                else if (IsVariable(token))
                {
                    list.Add(token);
                    while (true)
                    {
                        var i1 = i;
                        i1 += 1;
                        if (i1 < expr.List.Count)
                        {
                            if (IsVariable(expr.List[i1]))
                            {
                                list.Add(expr.List[i1]);
                                i = i1;
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    postFix.Add(new ExprNode(list, ExprType.Var));

                    //for emitter
                    Raw.Add(new ExprNode(list, ExprType.Var));
                    list.Clear();
                }
                else
                {
                    switch (token.Text)
                    {
                        case "(":
                            oprerator.Push(new ExprNode(token, ExprType.Undefined));
                            //for emitter
                            Raw.Add(new ExprNode(token, ExprType.Undefined));
                            break;
                        case ")":
                            arrival = oprerator.Pop();

                            //for emitter
                            Raw.Add(new ExprNode(token, ExprType.Undefined));
                            while (arrival.Tokens.First().Text != "(")
                            {
                                postFix.Add(arrival);
                                arrival = oprerator.Pop();
                            }
                            break;
                        default:
                            //for emitter
                            Raw.Add(new ExprNode(token, ExprType.Undefined));

                            if (oprerator.Count != 0 && Predecessor(oprerator.Peek(), new ExprNode(token, ExprType.Undefined)))
                            {
                                arrival = oprerator.Pop();
                                while (Predecessor(arrival, new ExprNode(token,ExprType.Undefined)))
                                {
                                    postFix.Add(arrival);

                                    if (oprerator.Count == 0)
                                        break;

                                    arrival = oprerator.Pop();
                                }
                                oprerator.Push(new ExprNode(token,ExprType.Undefined));
                            }
                            else
                                oprerator.Push(new ExprNode(token, ExprType.Undefined));
                            break;
                    }
                }
            }
            while (oprerator.Count > 0)
            {
                arrival = oprerator.Pop();
                postFix.Add(arrival);
            }
            return postFix;
        }
Exemplo n.º 4
0
 public void Append(Expression expr)
 {
     List.AddRange(expr.List);
 }