Esempio n. 1
0
        public static bool TypesMatch(TypeDef t1, TypeDef t2)
        {
            if (t1.Name != t2.Name)
            {
                return(false);
                //throw new TypeMismatchException(t1, t2);
            }

            if ((t1 is FunctionTypeDef) != (t2 is FunctionTypeDef))
            {
                return(false);
                //throw new TypeMismatchException(t1, t2);
            }

            if (t1 is FunctionTypeDef)
            {
                FunctionTypeDef t1FunctionType = (FunctionTypeDef)t1;
                FunctionTypeDef t2FunctionType = (FunctionTypeDef)t2;

                CheckExpressionTypesMatch(t1FunctionType.ReturnType, t2FunctionType.ReturnType);

                if (t1FunctionType.ArgumentTypes.Count != t2FunctionType.ArgumentTypes.Count)
                {
                    return(false);
                    //throw new TypeMismatchException(t1, t2);
                }

                for (int i = 0; i < t1FunctionType.ArgumentTypes.Count; i++)
                {
                    CheckExpressionTypesMatch(t1FunctionType.ArgumentTypes[i], t2FunctionType.ArgumentTypes[i]);
                }
            }

            return(true);
        }
Esempio n. 2
0
        public void AddFunctionSymbol(string name, ExpressionType returnType, List <ExpressionType> argumentTypes, bool isDefined, bool isExported)
        {
            if (!_inFunctionScope)
            {
                throw new Exception("Not in a function scope.");
            }

            if (_functionSymbolTable.ContainsKey(name))
            {
                var function = _functionSymbolTable[name];

                if (function.IsDefined)
                {
                    throw new DuplicateFunctionDefinitionException(name);
                }

                var functionTypeDef = new FunctionTypeDef()
                {
                    ReturnType    = returnType,
                    ArgumentTypes = argumentTypes
                };

                if (!TypeChecking.TypesMatch(functionTypeDef, function.Type))
                {
                    throw new FunctionSignatureMismatchException(name);
                }

                if (isDefined)
                {
                    function.IsDefined  = true;
                    function.IsExported = isExported;
                }
            }
            else
            {
                _functionSymbolTable.Add(
                    name,
                    new Function()
                {
                    Address = new LabelAddressValue(CreateNewLabel()),
                    //ReturnType = returnType,
                    //ParameterTypes = parameterTypes,
                    Type = new FunctionTypeDef()
                    {
                        ReturnType    = returnType,
                        ArgumentTypes = argumentTypes
                    },
                    IsDefined  = isDefined,
                    IsExported = isExported
                }
                    );
            }
        }