Ejemplo n.º 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();
        }
Ejemplo n.º 2
0
 public void visit(FunctionDeclaration that)
 {
     that.Type.visit(this);
     Console.Write(" {0}(", that.Name);
     foreach (ParameterDeclaration parameter in that.Parameters)
     {
         parameter.visit(this);
         if (parameter != that.Parameters[that.Parameters.Length - 1])
             Console.Write(", ");
     }
     Console.WriteLine(")");
     Console.WriteLine("{");
     Console.Write("return ");
     if (that.Body != null)
         that.Body.visit(this);
     else
         Console.Write("null");
     Console.WriteLine(";");
     Console.WriteLine("}");
 }
Ejemplo n.º 3
0
        public void visit(FunctionDeclaration that)
        {
            System.Text.StringBuilder mangled = new System.Text.StringBuilder(64);
            that.Encode(mangled);
            string name = mangled.ToString();

            if (!_symbols.Insert(name, that))
                throw new CheckerError(that.Position, "Duplicate name '" + Demangler.Decode(name) + "' detected in function declaration");
            _symbols.EnterScope(name);

            that.Type.visit(this);

            foreach (ParameterDeclaration parameter in that.Parameters)
            {
                // insert each parameter into the current scope so that it becomes visible to the code
                if (!_symbols.Insert(parameter.Name, parameter))
                    throw new CheckerError(parameter.Position, "Duplicate name '" + parameter.Name + "' detected in parameter");

                parameter.visit(this);
            }

            if (that.Body != null)
                that.Body.visit(this);

            _symbols.LeaveScope(name);
        }