Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            FortranOptions opts = new FortranOptions();
            MessageCollection messages = new MessageCollection(opts);

            opts.Messages = messages;
            if (opts.Parse(args)) {
                Compiler comp = new Compiler(opts);
                comp.Messages = messages;

                foreach (string srcfile in opts.SourceFiles) {
                    if (!File.Exists(srcfile)) {
                        messages.Error(MessageCode.SOURCEFILENOTFOUND, String.Format("File '{0}' not found", srcfile));
                        break;
                    }
                    comp.Compile(srcfile);
                }
                if (messages.ErrorCount == 0) {
                    comp.Save();
                    if (opts.Run && messages.ErrorCount == 0) {
                        comp.Execute();
                    }
                }
            }
            foreach (Message msg in messages) {
                if (msg.Level == MessageLevel.Error) {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                Console.WriteLine(msg);
                Console.ResetColor();
            }
            if (messages.ErrorCount > 0) {
                Console.WriteLine(String.Format("*** {0} errors found. Compilation stopped.", messages.ErrorCount));
            }
        }
Ejemplo n.º 2
0
 // Compile the given code and return the error count.
 public static Compiler HelperCompile(string [] code, FortranOptions opts)
 {
     Compiler comp = new Compiler(opts);
     comp.CompileString(code);
     Assert.AreEqual(0, comp.Messages.ErrorCount);
     return comp;
 }
Ejemplo n.º 3
0
        public void ParseConstantDivisionByZero()
        {
            string [] code = new [] {
                "      FUNCTION TEST",
                "        INTEGER I",
                "        I = 10/0",
                "        RETURN I",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.Count);
            Assert.AreEqual(MessageCode.DIVISIONBYZERO, comp.Messages[0].Code);
            Assert.AreEqual(3, comp.Messages[0].Line);
        }
Ejemplo n.º 4
0
        public void ParseVerifyAssignments()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      INTEGER A,C",
                "      REAL B",
                "      A = 12",
                "      B = 4.67",
                "      B = A",
                "      C = 12.78",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.Count);
        }
Ejemplo n.º 5
0
        public void ValidateExceedMaximumContinuationLines()
        {
            string [] code = new [] {
                "      INTEGER A,",
                "     1B,",
                "     2C,",
                "     3D,",
                "     4E,",
                "     5F,",
                "     6G,",
                "     7H,",
                "     8I,",
                "     9J,",
                "     AK,",
                "     BL,",
                "     CM,",
                "     DN,",
                "     EO,",
                "     FP,",
                "     GQ,",
                "     HR,",
                "     IS,",
                "     JT",
                "     KU"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.ErrorCount);
            Assert.AreEqual(21, comp.Messages[0].Line);
            Assert.AreEqual(MessageCode.TOOMANYCONTINUATION, comp.Messages[0].Code);
        }
Ejemplo n.º 6
0
        public void ParseVerifyBadLogicalAssignment()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT NONE",
                "      LOGICAL L1,L2",
                "      L1 = 12",
                "      L2 = 9.67",
                "      L1 = 'Ch'",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(3, comp.Messages.Count);
            Assert.AreEqual(MessageCode.TYPEMISMATCH, comp.Messages[0].Code);
            Assert.AreEqual(MessageCode.TYPEMISMATCH, comp.Messages[1].Code);
            Assert.AreEqual(MessageCode.TYPEMISMATCH, comp.Messages[2].Code);
        }
Ejemplo n.º 7
0
        public void ArrayVerifyIllegalDeclaration()
        {
            string [] code = new [] {
                "      PROGRAM FOO",
                "      INTEGER MAXVAL",
                "      REAL ARRAY(MAXVAL)",
                "      ARRAY(2) = 4.2",
                "      TEST=FOO(ARRAY,MAXVAL)",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.ErrorCount);
            Assert.AreEqual(MessageCode.ARRAYILLEGALBOUNDS, comp.Messages[0].Code);
            Assert.AreEqual(3, comp.Messages[0].Line);
        }
Ejemplo n.º 8
0
 // Compile the given code, run the specified function and verify
 // that the integer result matches
 public static void HelperRunInteger(Compiler comp, string entryPointName, int expectedValue)
 {
     ExecutionResult execResult = comp.Execute(entryPointName);
     Assert.IsTrue(execResult.Success);
     Assert.AreEqual(expectedValue, (int)execResult.Result);
 }
Ejemplo n.º 9
0
        public void ParseVerifyImplicitOrdering()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT LOGICAL(L)",
                "      IMPLICIT NONE",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.Count);
            Assert.AreEqual(MessageCode.TOKENNOTPERMITTED, comp.Messages[0].Code);
        }
Ejemplo n.º 10
0
        public void ParseVerifyImplicitNone()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT NONE",
                "      INTEGER A",
                "      A = 20",
                "      B = A",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.Count);
            Assert.AreEqual(MessageCode.UNDEFINEDVARIABLE, comp.Messages[0].Code);
            Assert.AreEqual(5, comp.Messages[0].Line);
        }
Ejemplo n.º 11
0
        public void ParseVerifyImplicit1()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT INTEGER(A-Z)",
                "      A = 20",
                "      B = A",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.Count);
        }
Ejemplo n.º 12
0
        public void ParseVerifyIllegalChar()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      INTEGER A",
                "      CHARACTER B",
                "      B = 'A'",
                "      A = B",
                "      B = 89",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(2, comp.Messages.Count);
            Assert.AreEqual(MessageCode.TYPEMISMATCH, comp.Messages[0].Code);
            Assert.AreEqual(5, comp.Messages[0].Line);
            Assert.AreEqual(MessageCode.TYPEMISMATCH, comp.Messages[1].Code);
            Assert.AreEqual(6, comp.Messages[1].Line);
        }
Ejemplo n.º 13
0
        public void ParseVerifyExpressionCollapsing()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT NONE",
                "      INTEGER A(12+34), B(90-10), C(4-2:9-3)",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.ErrorCount);
        }
Ejemplo n.º 14
0
        public void ParseVerifyConstantAssignment()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      PARAMETER (I=12)",
                "      I = 45",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(1, comp.Messages.Count);
            Assert.AreEqual(MessageCode.CANNOTASSIGNTOCONST, comp.Messages[0].Code);
            Assert.AreEqual(3, comp.Messages[0].Line);
        }
Ejemplo n.º 15
0
        public void ArithDivisionByZero()
        {
            string [] code = new [] {
                "      FUNCTION ITEST",
                "        INTEGER I,J",
                "        I = 10",
                "        J = 0",
                "        RETURN I/J",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.ErrorCount);
            Assert.Throws(typeof(System.DivideByZeroException), delegate { comp.Execute("ITEST"); });
        }
Ejemplo n.º 16
0
        public void ValidateMaximumContinuationLines()
        {
            string [] code = new [] {
                "      INTEGER A,",
                "     1B,",
                "     2C,",
                "     3D,",
                "     4E,",
                "     5F,",
                "     6G,",
                "     7H,",
                "     8I,",
                "     9J,",
                "     AK,",
                "     BL,",
                "     CM,",
                "     DN,",
                "     EO,",
                "     FP,",
                "     GQ,",
                "     HR,",
                "     IS,",
                "     JT"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.ErrorCount);
        }
Ejemplo n.º 17
0
 // Compile the given code, run the specified function and verify
 // that the float result matches
 public static void HelperRunFloat(Compiler comp, string entryPointName, float expectedValue)
 {
     ExecutionResult execResult = comp.Execute(entryPointName);
     Assert.IsTrue(execResult.Success);
     Assert.IsTrue(FloatCompare(expectedValue, (float)execResult.Result), "Saw " + execResult.Result + " but expected " + expectedValue);
 }
Ejemplo n.º 18
0
        public void ParseVerifyLogicalAssignment()
        {
            string [] code = new [] {
                "      PROGRAM PARSETEST",
                "      IMPLICIT NONE",
                "      LOGICAL L1,L2",
                "      L1 = .TRUE.",
                "      L2 = .FALSE.",
                "      L1 = L2",
                "      L1 = 90 .GT. 45",
                "      END"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.Count);
        }
Ejemplo n.º 19
0
 // Compile the given code, run the specified function and verify
 // that the string result matches
 public static void HelperRunString(Compiler comp, string entryPointName, string expectedValue)
 {
     ExecutionResult execResult = comp.Execute(entryPointName);
     Assert.IsTrue(execResult.Success);
     Assert.AreEqual(expectedValue, execResult.Result.ToString());
 }
Ejemplo n.º 20
0
        public void ParseVerifyRedefinitions()
        {
            string [] code = new [] {
                "      INTEGER A",
                "      REAL B",
                "      CHARACTER B",
                "      DOUBLE PRECISION B",
                "      LOGICAL F,G,H"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(2, comp.Messages.ErrorCount);
            Assert.AreEqual(MessageCode.IDENTIFIERREDEFINITION, comp.Messages[0].Code);
            Assert.AreEqual(3, comp.Messages[0].Line);
            Assert.AreEqual(MessageCode.IDENTIFIERREDEFINITION, comp.Messages[1].Code);
            Assert.AreEqual(4, comp.Messages[1].Line);
        }
Ejemplo n.º 21
0
 // Compile the given code, run the specified function and verify
 // that the double precision result matches
 public static void HelperRunDouble(Compiler comp, string entryPointName, double expectedValue)
 {
     ExecutionResult execResult = comp.Execute(entryPointName);
     Assert.IsTrue(execResult.Success);
     Assert.IsTrue(DoubleCompare(expectedValue, (double)execResult.Result), "Saw " + execResult.Result + " but expected " + expectedValue);
 }
Ejemplo n.º 22
0
        public void ParseVerifyTypeFormats()
        {
            string [] code = new [] {
                "      INTEGER A",
                "      REAL B",
                "      CHARACTER C*5,D",
                "      DOUBLE PRECISION E",
                "      LOGICAL F,G,H"
            };

            Compiler comp = new Compiler(new FortranOptions());
            comp.CompileString(code);
            Assert.AreEqual(0, comp.Messages.ErrorCount);
        }