Esempio n. 1
0
        private LocalScope CreateLocalScope(Scope parent, Vector <Parameter> parameters)
        {
            var locals = new LocalScope(parent);

            foreach (var parameter in parameters)
            {
                if (!locals.TryIncludeUniqueBinding(parameter))
                {
                    LogError(CompilerError.DuplicateIdentifier(parameter.Position, parameter));
                }
            }

            return(locals);
        }
Esempio n. 2
0
        private ClassScope CreateClassScope(Scope parent, Vector <Function> methods)
        {
            var members = new ClassScope(parent);

            foreach (var method in methods)
            {
                if (!members.TryIncludeUniqueBinding(method.Name.Identifier, typeRegistry.DeclaredType(method)))
                {
                    LogError(CompilerError.DuplicateIdentifier(method.Position, method));
                }
            }

            return(members);
        }
Esempio n. 3
0
        private GlobalScope CreateGlobalScope(Vector <Class> classes, Vector <Function> functions)
        {
            var globals = new GlobalScope();

            foreach (var @class in classes)
            {
                if (!globals.TryIncludeUniqueBinding(@class.Name.Identifier, ConstructorType(@class.Name)))
                {
                    LogError(CompilerError.DuplicateIdentifier(@class.Position, @class));
                }
            }

            foreach (var function in functions)
            {
                if (!globals.TryIncludeUniqueBinding(function.Name.Identifier, typeRegistry.DeclaredType(function)))
                {
                    LogError(CompilerError.DuplicateIdentifier(function.Position, function));
                }
            }

            return(globals);
        }
Esempio n. 4
0
        public Expression TypeCheck(Block block, Scope scope)
        {
            var position             = block.Position;
            var variableDeclarations = block.VariableDeclarations;
            var innerExpressions     = block.InnerExpressions;

            var localScope = new LocalScope(scope);

            var typedVariableDeclarations = new List <VariableDeclaration>();

            foreach (var variable in variableDeclarations)
            {
                var typedValue = TypeCheck(variable.Value, localScope);

                var bindingType = variable.IsImplicitlyTyped
                    ? typedValue.Type /*Replaces implicit type.*/
                    : TypeOf(variable.DeclaredTypeName);

                var binding = new VariableDeclaration(variable.Position, variable.DeclaredTypeName, variable.Identifier, typedValue, bindingType);

                if (!localScope.TryIncludeUniqueBinding(binding))
                {
                    LogError(CompilerError.DuplicateIdentifier(binding.Position, binding));
                }

                Unify(typedValue.Position, bindingType, typedValue.Type);

                typedVariableDeclarations.Add(binding);
            }

            var typedInnerExpressions = TypeCheck(innerExpressions, localScope);

            var blockType = typedInnerExpressions.Last().Type;

            return(new Block(position, typedVariableDeclarations.ToVector(), typedInnerExpressions, blockType));
        }