public override TypeNode EvaluateType(API api, TypeNode type, bool isStatic)
        {
            if (identifier.ToString() == "ToString")
            {
                Console.Write("");
            }
            TypeNode t = null;

            try{
                if (type == null)
                {
                    List <string> argumentsTypes = new List <string>();
                    foreach (var arg in arguments)
                    {
                        TypeNode argType = arg.expression.EvaluateType(api, null, true);
                        argumentsTypes.Add(argType.ToString());
                    }

                    string     methodName = identifier.ToString() + "(" + string.Join(",", argumentsTypes) + ")";
                    MethodNode f          = api.contextManager.findFunction(methodName);
                    if (f != null)
                    {
                        t = f.methodHeaderNode.returnType.DataType;
                        if (api.validateModifier(f.Modifier, TokenType.RW_STATIC) == isStatic)
                        {
                            api.isNextStaticContext = false;
                        }
                    }
                    else
                    {
                        t = api.getTypeForIdentifier(identifier.ToString());
                        if (t != null)
                        {
                            api.isNextStaticContext = true;
                        }
                    }
                }
                else
                {
                    bool accept = false;
                    if (!(type is ClassTypeNode))
                    {
                        type   = api.getTypeForIdentifier(type.ToString());
                        accept = true;
                    }
                    Context staticContext = api.buildContextForTypeDeclaration(type);
                    staticContext.setLastParent(api.contextManager.getObjectContext());
                    List <string> argumentsTypes = new List <string>();
                    foreach (var arg in arguments)
                    {
                        TypeNode argType = arg.expression.EvaluateType(api, null, true);
                        argumentsTypes.Add(argType.ToString());
                    }

                    if (identifier.ToString() == "Nextfloat")
                    {
                        Console.Write("");
                    }

                    string     methodName = identifier.ToString() + "(" + string.Join(",", argumentsTypes) + ")";
                    MethodNode f          = staticContext.findFunction(methodName, Utils.privateLevel, Utils.protectedLevel);
                    bool       passed     = api.validateModifier(f.Modifier, TokenType.RW_STATIC) == isStatic;
                    if (accept)
                    {
                        passed = true;
                    }

                    if (f != null && passed)
                    {
                        t = f.methodHeaderNode.returnType.DataType;
                    }
                }

                if (t == null)
                {
                    Utils.ThrowError("Function or method '" + identifier.ToString() + "' could not be found in the current context. ");
                }
            }catch (SemanticException ex) {
                Utils.ThrowError(ex.Message + token.getLine());
            }
            return(t);
        }
Exemplo n.º 2
0
        public override TypeNode EvaluateType(API api, TypeNode type, bool isStatic)
        {
            if (identifier.ToString() == "students")
            {
                Console.Write("");
            }
            TypeNode arrayType = null;

            try{
                if (type == null)
                {
                    FieldNode f = api.contextManager.findVariable(identifier.ToString());
                    if (f != null)
                    {
                        arrayType = f.type;
                        if (f.isStatic == isStatic)
                        {
                            api.isNextStaticContext = false;
                        }
                    }
                    else
                    {
                        arrayType = api.getTypeForIdentifier(identifier.ToString());
                        if (arrayType != null)
                        {
                            api.isNextStaticContext = true;
                        }
                    }
                }
                else
                {
                    bool accept = false;
                    if (!(type is ClassTypeNode))
                    {
                        type   = api.getTypeForIdentifier(type.ToString());
                        accept = true;
                    }

                    Context   staticContext = api.buildContextForTypeDeclaration(type);
                    FieldNode f             = staticContext.findVariable(identifier.ToString(), Utils.privateLevel, Utils.protectedLevel);
                    bool      passed        = f.isStatic == isStatic;
                    if (accept)
                    {
                        passed = true;
                    }

                    if (f != null && passed)
                    {
                        arrayType = f.type;
                    }
                }

                if (arrayType == null)
                {
                    Utils.ThrowError("Array variable '" + identifier.ToString() + "' could not be found in the current context. ");
                }

                var arr = arrayType as ArrayTypeNode;
                int arraysOfArraysCounter = 0;

                while (arraysOfArraysCounter < arrayAccessList.Count)
                {
                    if (arraysOfArraysCounter > arr.multidimsArrays.Count)
                    {
                        Utils.ThrowError("Cannot apply indexing with [] to an expression of type '" + arr.DataType.ToString()
                                         + "' [" + api.currentNamespace.Identifier.Name + "]");
                    }
                    if (arr.multidimsArrays[arraysOfArraysCounter].dimensions != arrayAccessList[arraysOfArraysCounter].Count)
                    {
                        Utils.ThrowError("Wrong number of indices inside []; expected " +
                                         arr.multidimsArrays[arraysOfArraysCounter].dimensions + " [" + api.currentNamespace.Identifier.Name + "]");
                    }
                    arraysOfArraysCounter++;
                }

                if (arraysOfArraysCounter == arr.multidimsArrays.Count)
                {
                    arrayType = arr.DataType;
                }
                else
                {
                    var dimensions = new List <MultidimensionArrayTypeNode>();
                    while (arraysOfArraysCounter < arr.multidimsArrays.Count)
                    {
                        dimensions.Add(new MultidimensionArrayTypeNode(arr.multidimsArrays[arraysOfArraysCounter].dimensions, null));
                        arraysOfArraysCounter++;
                    }
                    arrayType = new ArrayTypeNode(arr.DataType, dimensions, null);
                }
            }catch (SemanticException ex) {
                Utils.ThrowError(ex.Message + token.getLine());
            }
            return(arrayType);
        }