Exemplo n.º 1
0
        private bool IsSameTypeOrNullPtr(TypeAST typeToCheck, TypeAST typeAssign)
        {
            var nullType = Enum.GetName(typeof(Keyword), Keyword.NULL).ToLower();

            var isSameType = typeToCheck.ToString() == typeAssign.ToString();
            var isNullPtr  = (typeToCheck.ToString().StartsWith("^") && typeAssign.ToString() == nullType);

            return(isSameType || isNullPtr);
        }
Exemplo n.º 2
0
        private string GetTypeOf(TypeAST currentType, TypeAST otherType)
        {
            if (currentType == null)
            {
                return(otherType.ToString());
            }

            return(ComputeType(currentType.ToString(), otherType.ToString()));
        }
Exemplo n.º 3
0
        public override void Visit(NumberAST number)
        {
            var numberType = new TypeAST
            {
                TypeName = number.ExplicitType != string.Empty ?
                           number.ExplicitType :
                           Enum.GetName(typeof(Keyword), Keyword.S32).ToLower()
            };

            _stateInfo.currentType = numberType;
        }
Exemplo n.º 4
0
        private TypeAST CastType(TypeAST currentType, TypeAST otherType)
        {
            var currentTypeString = currentType.ToString();
            var otherTypeString   = otherType.ToString();

            if (currentTypeString == otherTypeString)
            {
                return(otherType);
            }

            if (otherType.ToString() == "[] char" && currentType.ToString() == "^char" ||
                currentType.ToString() == "[] char" && otherType.ToString() == "^char")
            {
                return(otherType);
            }

            if ((Vocabulary.Types.All(x => x != currentTypeString) ||
                 Vocabulary.Types.All(x => x != otherTypeString)) &&
                currentType.TypeName != "Enum" &&
                otherType.TypeName != "Enum")
            {
                throw new Exception("Cannot Cast type " + currentTypeString + " to " + otherTypeString);
            }

            if (currentTypeString == "str" || otherTypeString == "str")
            {
                throw new Exception("Cannot Cast type " + currentTypeString + " to " + otherTypeString);
            }

            if (currentType.TypeName == "Enum" &&
                (otherTypeString.StartsWith("u") || otherTypeString.StartsWith("s")))
            {
                return(otherType);
            }

            if (otherType.TypeName == "Enum" &&
                (currentTypeString.StartsWith("u") || currentTypeString.StartsWith("s")))
            {
                return(otherType);
            }

            if (currentType.TypeName == "Enum" || otherType.TypeName == "Enum")
            {
                throw new Exception("Cannot Cast type " + currentTypeString + " to " + otherTypeString);
            }

            return(otherType);
        }
Exemplo n.º 5
0
        public override void Visit(NewExprAST newStatement)
        {
            if (_stateInfo.isConst)
            {
                throw new Exception("Expression must be constant");
            }

            TypeAST type = newStatement.Type;

            if (type is FunctionTypeAST)
            {
                throw new Exception("Cannot allocate a function type");
            }

            ArrayTypeAST arrayType;

            while ((arrayType = type as ArrayTypeAST) != null)
            {
                type = arrayType.TypeOfContainedValues;
            }

            if (type.ToString().StartsWith("^"))
            {
                throw new Exception("Cannot allocate a pointer");
            }

            if (type.ToString() == Enum.GetName(typeof(Keyword), Keyword.VOID).ToLower())
            {
                throw new Exception("Cannot allocate void");
            }

            if (!(type is FunctionTypeAST) && !Vocabulary.Types.Contains(type.ToString()))
            {
                var typeExists = _symTableManager.LookupTypeInfo(_stateInfo.currentFile, type.ToString());

                if (typeExists == null || typeExists.kind == TypeKind.ENUM)
                {
                    throw new Exception(string.Format("New statement : type : {0} is not a struct", type.ToString()));
                }
            }

            _stateInfo.currentType = new PtrTypeAST
            {
                Type     = newStatement.Type,
                TypeName = "Pointer"
            };
        }
Exemplo n.º 6
0
 public override void Visit(TypeAST type)
 {
 }
Exemplo n.º 7
0
 public virtual void Visit(TypeAST type)
 {
 }
Exemplo n.º 8
0
 public override void Visit(FunctionBodyAST functionBody)
 {
     Visit(functionBody.Prototype);
     Visit(functionBody.Scope);
     _returnType = null;
 }
Exemplo n.º 9
0
 public override void Visit(FunctionProtoAST functionProto)
 {
     _returnType = functionProto.ReturnType;
 }