Exemple #1
0
        /// <summary>
        /// Creates a new ArrayType given a BaseType and the first dimension
        /// </summary>
        /// <param name="type">PrimitiveType contained within the array</param>
        /// <param name="firstSize">First dimension of the array</param>
        public ArrayType(BaseType type, string firstSize)
        {
            // Initialize members
            this.type = type as PrimitiveType;
            dimensions = 1;
            sizes = new List<int>();

            // Add first dimension
            sizes.Add(int.Parse(firstSize));
        }
Exemple #2
0
        /// <summary>
        /// Creates a signature from a CallExpression object
        /// </summary>
        /// <param name="callExpr">CallExpresion object</param>
        public Signature(CallExpression callExpr)
        {
            // Set fully qualified name and return type
            name = callExpr.methodName;
            returnType = callExpr.returnType;
            arguments = new List<BaseType>();

            // Populate argument list
            foreach (Expression expr in callExpr.arguments)
            {
                arguments.Add(expr.returnType);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a signature from a MethodInfo object (used by Reflection)
        /// </summary>
        /// <param name="methodInfo">MethodInfo object</param>
        public Signature(MethodInfo methodInfo)
        {
            // Set fully qualified name and return type
            name = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
            returnType = BaseType.FromType(methodInfo.ReturnType);
            arguments = new List<BaseType>();

            // Populate argument list
            foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
            {
                arguments.Add(BaseType.FromType(parameterInfo.ParameterType));
            }
        }
Exemple #4
0
 void Type(out BaseType type)
 {
     Primitive(out type);
     Token keepT = type.t;
     if (la.kind == 37) {
     Get();
     Expect(2);
     type = new ArrayType(type, t.val);
     type.t = keepT;
     while (la.kind == 31) {
         Get();
         Expect(2);
         (type as ArrayType).AddDimension(t.val);
     }
     Expect(44);
     }
 }
Exemple #5
0
        void Primitive(out BaseType type)
        {
            type = null;
            if (la.kind == 21) {
            Get();
            type = PrimitiveType.INT;
            type.t = t;

            } else if (la.kind == 22) {
            Get();
            type = PrimitiveType.DOUBLE;
            type.t = t;

            } else if (la.kind == 23) {
            Get();
            type = PrimitiveType.STRING;
            type.t = t;

            } else if (la.kind == 20) {
            Get();
            type = PrimitiveType.BOOL;
            type.t = t;

            } else SynErr(53);
        }
Exemple #6
0
 /// <summary>
 /// Creates a new CastExpression given a type to cast to
 /// </summary>
 /// <param name="type">BaseType to cast to</param>
 public CastExpression(BaseType type)
 {
     // Set return type
     returnType = type;
 }
Exemple #7
0
 /// <summary>
 /// Returns a value indicating whether this type can be implicitly typecasted to
 /// the given type
 /// </summary>
 /// <param name="type">BaseType object to test against</param>
 /// <returns>True if an implicit typecast is possible</returns>
 public abstract bool IsCompatible(BaseType type);
Exemple #8
0
 /// <summary>
 /// Returns a value indicating whether this type can be implicitly typecasted to
 /// the given type
 /// </summary>
 /// <param name="type">BaseType object to test against</param>
 /// <returns>True if an implicit typecast is possible</returns>        
 public override bool IsCompatible(BaseType type)
 {
     // Convert to CLR type and perform check
     return IsCompatible(type.ToCLRType());
 }
Exemple #9
0
 /// <summary>
 /// Overriden - arrays cannot be compatible types because they cannot be typecasted
 /// </summary>
 public override bool IsCompatible(BaseType type)
 {
     return false;
 }
Exemple #10
0
 /// <summary>
 /// Creates a new FunctionDeclaration given a parent scope
 /// </summary>
 /// <param name="parentScope">Parent scope of this function</param>
 private FunctionDeclaration(Scope parentScope)
 {
     // Initialize private members
     localScope = new LocalScope(parentScope);
     arguments = new List<BaseType>();
     returnType = PrimitiveType.VOID;
 }
Exemple #11
0
 /// <summary>
 /// Creates a new VariableDeclaration given a token and a type
 /// </summary>
 /// <param name="t">Declaration token</param>
 /// <param name="type">Variable type</param>
 public VariableDeclaration(Token t, BaseType type)
 {
     // Set members
     this.t = t;
     this.name = t.val;
     this.type = type;
 }
Exemple #12
0
        /// <summary>
        /// Evaluates this node and all of its children
        /// </summary>
        public override void Evaluate()
        {
            // Set return type to void if it is not set
            if (returnType == null)
            {
                returnType = PrimitiveType.VOID;
            }

            localScope.returnType = returnType;

            // Evaluate function body
            body.Evaluate(localScope);

            // Check if body doesn't return
            // Functions which return void don't have to contain an explicit return statement
            if (!body.returns)
            {
                // Check if return type is void
                if ((returnType is PrimitiveType) && (returnType as PrimitiveType).type == Primitive.Void)
                {
                    // Insert return statement
                    body.AddStatement(new ReturnStatement());
                }
                else
                {
                    // If return type is not void, function must return - issue error
                    Compiler.Compiler.errors.SemErr(t.line, t.col, "not all code paths return a value");
                }
            }
        }
Exemple #13
0
 /// <summary>
 /// Adds an argument to the function's argument list
 /// </summary>
 /// <param name="t">Argument token</param>
 /// <param name="type">Argument type</param>
 public void AddArgument(Token t, BaseType type)
 {
     // Add argument to the local symbol table as a variable declaration
     localScope.AddArgument(new VariableDeclaration(t, type));
 }