示例#1
0
        public static CodeTypeReference GetTypeRef(ParserIdentifier property)
        {
            CodeTypeReference typeRef;

            switch (property.Type)
            {
            case "bool":
                typeRef = CodeHelper.TypeRef <bool>();
                break;

            case "string":
                typeRef = CodeHelper.TypeRef <string>();
                break;

            case "decimal":
                typeRef = CodeHelper.TypeRef <decimal>();
                break;

            default:
                typeRef = CodeHelper.TypeRef(property.Type);
                break;
            }

            return(property.IsList ? GetListTypeRef(typeRef) : typeRef);
        }
示例#2
0
        private CodeStatementCollection GenerateImperativeStatement(IdentifierExpression node)
        {
            var parserId = ParserIdentifier.FromIdentifierExpression(node);

            if (parserId.Name != "Error")
            {
                var typeRef = CodeHelper.TypeRef(parserId.Type ?? _config.BaseClass);
                typeRef = parserId.IsList ? ParserCode.GetListTypeRef(typeRef) : typeRef;
                var init    = new CodeDefaultValueExpression(typeRef);
                var varDecl = new CodeVariableDeclarationStatement(typeRef, node.Identifier, init);
                _scope.Add(node.Identifier, AphidObject.Complex());

                return(new CodeStatementCollection(new[] { varDecl }));
            }

            return(new CodeStatementCollection(new[]
            {
                new CodeThrowExceptionStatement(
                    new CodeObjectCreateExpression(
                        _config.ExceptionClass,
                        CodeHelper.VarRef("CurrentToken")))
            }));
        }
示例#3
0
        protected override List<AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (IsStatement && expression.Type == AphidNodeType.IdentifierExpression)
            {
                var idExp = expression.ToIdentifier();
                var currentId = ParserIdentifier.FromIdentifierExpression(idExp);
                ParserIdentifier inferredId;

                if (!_idTable.TryResolve(idExp.Identifier, out inferredId))
                {
                    _idTable.Add(idExp.Identifier, currentId);
                    hasChanged = true;
                }
                else
                {
                    if (!currentId.IsList && inferredId.IsList)
                    {
                        currentId.IsList = inferredId.IsList;
                        currentId.Type = inferredId.Type;
                        hasChanged = true;

                        return new List<AphidExpression>
                        {
                            new BinaryOperatorExpression(
                                currentId.ToIdentifierExpression(),
                                AphidTokenType.AssignmentOperator,
                                new IdentifierExpression(
                                    currentId.Type ?? _config.BaseClass,
                                    new List<IdentifierExpression> { new IdentifierExpression("list") }))
                        };
                    }
                }
            }

            CallExpression funcExp;
            BinaryOperatorExpression binOpExp;

            if (expression.Type == AphidNodeType.CallExpression &&
                (funcExp = expression.ToCall()).FunctionExpression.Type == AphidNodeType.BinaryOperatorExpression &&
                ((binOpExp = funcExp.FunctionExpression.ToBinaryOperator()).Operator == AphidTokenType.MemberOperator) &&
                binOpExp.LeftOperand.Type == AphidNodeType.IdentifierExpression &&
                binOpExp.RightOperand.Type == AphidNodeType.IdentifierExpression &&
                binOpExp.RightOperand.ToIdentifier().Identifier == "Add")
            {
                var id = binOpExp.LeftOperand.ToIdentifier().Identifier;
                ParserIdentifier ids;

                if (!_idTable.TryResolve(id, out ids))
                {
                    _idTable.Add(id, ids = new ParserIdentifier() { Name = id });
                }

                if (!ids.IsList)
                {
                    ids.IsList = true;

                    if (ids.Type == null)
                    {
                        ids.Type = _config.BaseClass;
                    }

                    hasChanged = true;
                }
            }

            UnaryOperatorExpression unOpExp;

            if (expression.Type == AphidNodeType.UnaryOperatorExpression &&
                (unOpExp = expression.ToUnaryOperator()).Operator == AphidTokenType.retKeyword)
            {
                if (unOpExp.Operand.Type == AphidNodeType.IdentifierExpression)
                {
                    var retId = unOpExp.Operand.ToIdentifier();

                    if (!_returnIds.Contains(retId.Identifier))
                    {
                        hasChanged = true;
                        _returnIds.Add(retId.Identifier);
                    }
                }
            }
            //else if (expression.Type == AphidNodeType.BinaryOperatorExpression &&
            //    (binOpExp = expression.ToBinaryOperator()).

            return new List<AphidExpression> { expression };
            //throw new NotImplementedException();
        }
示例#4
0
 public RuleStruct(string name, string baseName, ParserIdentifier[] properties)
 {
     Name = name;
     BaseName = baseName;
     Properties = properties;
 }
示例#5
0
        private CodeTypeReference GetTypeRef(ParserIdentifier property)
        {
            CodeTypeReference typeRef;

            switch (property.Type)
            {
                case "bool":
                    typeRef = CodeHelper.TypeRef<bool>();
                    break;

                case "string":
                    typeRef = CodeHelper.TypeRef<string>();
                    break;

                default:
                    typeRef = CodeHelper.TypeRef(property.Type);
                    break;
            }

            if (property.IsList)
            {
                return GetListTypeRef(typeRef);
            }

            return typeRef;
        }