Esempio n. 1
0
        internal override void ResolveTypes(VariableScope varScope, PastelCompiler compiler)
        {
            this.Condition = this.Condition.ResolveType(varScope, compiler);
            if (!this.Condition.ResolvedType.IsIdentical(PType.INT))
            {
                throw new ParserException(this.Condition.FirstToken, "Only ints can be used in switch statements.");
            }

            // consider it all one scope
            for (int i = 0; i < this.Chunks.Length; ++i)
            {
                SwitchChunk chunk = this.Chunks[i];
                for (int j = 0; j < chunk.Cases.Length; ++j)
                {
                    Expression ex = chunk.Cases[j];
                    if (ex != null)
                    {
                        ex             = ex.ResolveType(varScope, compiler);
                        chunk.Cases[j] = ex;
                        if (ex.ResolvedType.RootValue != "int")
                        {
                            throw new ParserException(ex.FirstToken, "Only ints may be used.");
                        }
                    }
                }

                Executable.ResolveTypes(chunk.Code, varScope, compiler);
            }
        }
Esempio n. 2
0
        internal override void ResolveTypes(VariableScope varScope, PastelCompiler compiler)
        {
            this.Condition = this.Condition.ResolveType(varScope, compiler);
            PType conditionType = this.Condition.ResolvedType;
            bool  isInt         = conditionType.IsIdentical(compiler, PType.INT);
            bool  isChar        = !isInt && conditionType.IsIdentical(compiler, PType.CHAR);

            if (!isInt && !isChar)
            {
                throw new ParserException(this.Condition.FirstToken, "Only ints and chars can be used in switch statements.");
            }

            // consider it all one scope
            for (int i = 0; i < this.Chunks.Length; ++i)
            {
                SwitchChunk chunk = this.Chunks[i];
                for (int j = 0; j < chunk.Cases.Length; ++j)
                {
                    Expression ex = chunk.Cases[j];
                    if (ex != null)
                    {
                        ex             = ex.ResolveType(varScope, compiler);
                        chunk.Cases[j] = ex;
                        if ((isInt && ex.ResolvedType.RootValue != "int") ||
                            (isChar && ex.ResolvedType.RootValue != "char"))
                        {
                            throw new ParserException(ex.FirstToken, isInt ? "Only ints may be used." : "Only chars may be used.");
                        }
                    }
                }

                Executable.ResolveTypes(chunk.Code, varScope, compiler);
            }
        }
Esempio n. 3
0
        internal override void ResolveTypes(VariableScope varScope, PastelCompiler compiler)
        {
            this.Condition = this.Condition.ResolveType(varScope, compiler);
            if (!this.Condition.ResolvedType.IsIdentical(compiler, PType.BOOL))
            {
                throw new ParserException(this.Condition.FirstToken, "While loop must have a boolean condition.");
            }

            Executable.ResolveTypes(this.Code, varScope, compiler);
        }
Esempio n. 4
0
        internal override void ResolveTypes(VariableScope varScope, PastelCompiler compiler)
        {
            // This gets compiled as a wihle loop with the init added before the loop, so it should go in the same variable scope.
            // The implication is that multiple declarations in the init for successive loops will collide.
            Executable.ResolveTypes(this.InitCode, varScope, compiler);
            this.Condition = this.Condition.ResolveType(varScope, compiler);
            Executable.ResolveTypes(this.StepCode, varScope, compiler);
            VariableScope innerScope = new VariableScope(varScope);

            Executable.ResolveTypes(this.Code, innerScope, compiler);
        }
Esempio n. 5
0
        internal override void ResolveTypes(VariableScope varScope, PastelCompiler compiler)
        {
            this.Condition = this.Condition.ResolveType(varScope, compiler);
            if (this.Condition.ResolvedType.RootValue != "bool")
            {
                throw new ParserException(this.Condition.FirstToken, "Only booleans can be used in if statements.");
            }

            Executable.ResolveTypes(this.IfCode, new VariableScope(varScope), compiler);
            Executable.ResolveTypes(this.ElseCode, new VariableScope(varScope), compiler);
        }