Exemple #1
0
        private Declaration[] CreateStandardEnvironment(bool insert = false)
        {
            List<Declaration> result = new List<Declaration>();

            // signal that the following symbols are part of the standard library
            Position position = new Position("(library)", 0, 0);
            Declaration declaration;
            Expression expression;

            // enter the predefined constant 'maxint' into the symbol table
            expression = new IntegerExpression(position, int.MaxValue);
            declaration = new ConstantDeclaration(position, "maxint", new IntegerType(position), expression);
            result.Add(declaration);

            // enter the predefined constants 'false' and 'true' into the symbol table
            expression = new BooleanExpression(position, false);
            declaration = new ConstantDeclaration(position, "false", new BooleanType(position), expression);
            result.Add(declaration);

            expression = new BooleanExpression(position, true);
            declaration = new ConstantDeclaration(position, "true", new BooleanType(position), expression);
            result.Add(declaration);

            // enter the predefined operators into the symbol table
            // ... the \ operator
            declaration = new FunctionDeclaration(
                position,
                "\\",
                new BooleanType(position),
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new BooleanType(position)) },
                null        // note: the code generator must handle these predefined functions so no body is defined
            );
            result.Add(declaration);

            // ... all Triangle operators of the form Boolean x Boolean -> Boolean
            string[] boolean_and_boolean_to_boolean_operators = { "/\\", "\\/", "=", "\\=" };
            foreach (string @operator in boolean_and_boolean_to_boolean_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new BooleanType(position),
                    new ParameterDeclaration[]
                    {
                        new ParameterDeclaration(position, "first", new BooleanType(position)),
                        new ParameterDeclaration(position, "other", new BooleanType(position))
                    },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                );
                result.Add(declaration);
            }

            // ... all Triangle operators of the form Integer x Integer -> Integer
            string[] integer_and_integer_to_integer_operators = { "+", "-", "*", "/", "//" };
            foreach (string @operator in integer_and_integer_to_integer_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new IntegerType(position),
                    new ParameterDeclaration[]
                    {
                        new ParameterDeclaration(position, "first", new IntegerType(position)),
                        new ParameterDeclaration(position, "other", new IntegerType(position))
                    },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                );
                result.Add(declaration);
            }

            // ... all Triangle operators of the form Integer x Integer -> Boolean
            string[] integer_and_integer_to_boolean_operators = { "<", "<=", ">", ">=", "=", "\\=" };
            foreach (string @operator in integer_and_integer_to_boolean_operators)
            {
                declaration = new FunctionDeclaration(
                    position,
                    @operator,
                    new BooleanType(position),
                    new ParameterDeclaration[]
                    {
                        new ParameterDeclaration(position, "first", new IntegerType(position)),
                        new ParameterDeclaration(position, "other", new IntegerType(position))
                    },
                    null    // note: the code generator must handle these predefined functions so no body is defined
                );
                result.Add(declaration);
            }

            // enter the predefined functions (getint and putint) into the symbol table
            declaration = new FunctionDeclaration(
                position,
                "getint",
                new IntegerType(position),
            #if false
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new IntegerType(position))},
            #else
                new ParameterDeclaration[0],
            #endif
                null        // note: the code generator must handle these predefined functions so no body is defined
            );
            result.Add(declaration);

            declaration = new FunctionDeclaration(
                position,
                "putint",
                new IntegerType(position),
                new ParameterDeclaration[] { new ParameterDeclaration(position, "value", new IntegerType(position))},
                null        // note: the code generator must handle these predefined functions so no body is defined
            );
            result.Add(declaration);

            return result.ToArray();
        }
Exemple #2
0
 public void visit(ConstantDeclaration that)
 {
     Console.Write("const ");
     that.Type.visit(this);
     Console.Write(" {0} = ", that.Name);
     that.Expression.visit(this);
     Console.WriteLine(";");
 }
Exemple #3
0
        public void visit(ConstantDeclaration that)
        {
            // resolve the type of the expression
            that.Expression.visit(this);

            // hack: fix up the likely incorrect type that the Parser created for the constant (always "Integer")
            switch (that.Expression.Type.Kind)
            {
                case TypeKind.Boolean:
                    that.Type = new BooleanType(that.Expression.Position);
                    break;

                case TypeKind.Integer:
                    // simply keep the default integer type
                    break;

                case TypeKind.String:
                    that.Type = new StringType(that.Expression.Position);
                    break;

                default:
                    throw new CheckerError(that.Position, "Unknown type encountered: " + that.Expression.Type.Kind.ToString());
            }

            // nothing to check as we've just computed the type of the constant (cannot mismatch)

            if (!_symbols.Insert(that.Name, that))
                throw new CheckerError(that.Position, "Duplicate name '" + that.Name + "' detected in constant declaration");
        }