Fixed the MarkSequencePoints in the statements.
authorMichael Welch <michaelgwelch@gmail.com>
Tue, 26 Dec 2006 03:45:03 +0000 (03:45 +0000)
committerMichael Welch <michaelgwelch@gmail.com>
Tue, 26 Dec 2006 03:45:03 +0000 (03:45 +0000)
Modified the relational operators so that they weren't quite as kludgy.

mbasic/Parser.cs
mbasic/SyntaxTree/Assign.cs
mbasic/SyntaxTree/Equals.cs
mbasic/SyntaxTree/GreaterThan.cs [moved from mbasic/SyntaxTree/LessThanEqual.cs with 58% similarity]
mbasic/SyntaxTree/If.cs
mbasic/SyntaxTree/LessThan.cs
mbasic/SyntaxTree/Not.cs [deleted file]
mbasic/SyntaxTree/Print.cs
mbasic/SyntaxTree/RelationalExpression.cs [new file with mode: 0644]
mbasic/mbasic.csproj

index e88aeb9..0366540 100644 (file)
@@ -216,12 +216,12 @@ namespace mbasic
                     Token l = lookahead;\r
                     Match(lookahead);\r
                     r2 = RelationalExpr();\r
-                    if (l == Token.Equals) r3 = new Equals(r1, r2, line);\r
-                    else if (l == Token.LessThan) r3 = new LessThan(r1, r2, line);\r
-                    else if (l == Token.LessThanEqual) r3 = new LessThanEqual(r1, r2, line);\r
-                    else if (l == Token.GreaterThan) r3 = new Not(new LessThanEqual(r1, r2, line), line);\r
-                    else if (l == Token.GreaterThanEqual) r3 = new Not(new LessThan(r1, r2, line), line);\r
-                    else if (l == Token.NotEquals) r3 = new Not(new Equals(r1, r2, line), line);\r
+                    if (l == Token.Equals) r3 = RelationalExpression.CompareEquals(r1, r2, line);\r
+                    else if (l == Token.LessThan) r3 = RelationalExpression.CompareLessThan(r1, r2, line);\r
+                    else if (l == Token.LessThanEqual) r3 = RelationalExpression.CompareLessThanEquals(r1, r2, line);\r
+                    else if (l == Token.GreaterThan) r3 = RelationalExpression.CompareGreaterThan(r1, r2, line);\r
+                    else if (l == Token.GreaterThanEqual) r3 = RelationalExpression.CompareGreaterThanEquals(r1, r2, line);\r
+                    else if (l == Token.NotEquals) r3 = RelationalExpression.CompareNotEquals(r1, r2, line);\r
                     return MoreRelationalExpr(r3);\r
                 default:\r
                     break;\r
@@ -430,7 +430,7 @@ namespace mbasic
 \r
             Assign init = new Assign(index, startVal, line);\r
             Assign update = new Assign(index, new Increment(index, line), endLine);\r
-            Expression comparison = new LessThanEqual(new VariableReference(index, line), endVal, line);\r
+            Expression comparison = new GreaterThan(new VariableReference(index, line), endVal, false, line);\r
             return new For(init, comparison, update, block);\r
         }\r
 \r
index cc41a5c..678ef90 100644 (file)
@@ -42,8 +42,8 @@ namespace mbasic.SyntaxTree
         public override void Emit(ILGenerator gen, bool labelSetAlready)\r
         {\r
             if (!labelSetAlready) MarkLabel(gen);\r
-            value.Emit(gen);\r
             MarkSequencePoint(gen);\r
+            value.Emit(gen);\r
             gen.Emit(OpCodes.Stloc, locals[localIndex]);\r
         }\r
 \r
index c310d31..4992c01 100644 (file)
@@ -27,44 +27,20 @@ using System.Reflection;
 \r
 namespace mbasic.SyntaxTree\r
 {\r
-    class Equals : Expression\r
+    class Equals : RelationalExpression\r
     {\r
         private static readonly MethodInfo equalsMethod =\r
             typeof(Object).GetMethod("Equals", new Type[] { typeof(Object), typeof(Object) });\r
-        Expression e1;\r
-        Expression e2;\r
-        BasicType argType;\r
 \r
-        public Equals(Expression e1, Expression e2, int line)\r
-            : base(line)\r
+        public Equals(Expression e1, Expression e2, bool not, int line)\r
+            : base(e1, e2, not, line)\r
         {\r
-            this.e1 = e1;\r
-            this.e2 = e2;\r
         }\r
 \r
-        public override BasicType GetBasicType()\r
+        protected override void EmitOperation(ILGenerator gen)\r
         {\r
-            BasicType t1 = e1.GetBasicType();\r
-            BasicType t2 = e2.GetBasicType();\r
-            if (t1 == t2)\r
-            {\r
-                argType = t1;\r
-                return BasicType.Number;\r
-            }\r
-            return BasicType.Error;\r
-        }\r
-\r
-        public override void Emit(ILGenerator gen)\r
-        {\r
-            e1.Emit(gen);\r
-            e2.Emit(gen);\r
             if (argType == BasicType.Number) gen.Emit(OpCodes.Ceq);\r
             else gen.Emit(OpCodes.Call, equalsMethod);\r
-\r
-            // TI Basic uses -1/0, .NET uses 1/0, plus we need to convert from Int32 to double\r
-            gen.Emit(OpCodes.Conv_R8);\r
-            gen.Emit(OpCodes.Neg);\r
-\r
         }\r
     }\r
 }\r
similarity index 58%
rename from mbasic/SyntaxTree/LessThanEqual.cs
rename to mbasic/SyntaxTree/GreaterThan.cs
index 0a239a3..4acd229 100644 (file)
@@ -26,32 +26,31 @@ using System.Reflection.Emit;
 \r
 namespace mbasic.SyntaxTree\r
 {\r
-    class LessThanEqual : Expression\r
+    class GreaterThan : RelationalExpression\r
     {\r
-        Expression e1;\r
-        Expression e2;\r
-        public LessThanEqual(Expression expr1, Expression expr2, int line)\r
-            : base(line)\r
+        \r
+        public GreaterThan(Expression expr1, Expression expr2, bool not,\r
+            int line)\r
+            : base(expr1, expr2, not, line)\r
         {\r
-            this.e1 = expr1;\r
-            this.e2 = expr2;\r
         }\r
 \r
-        public override void Emit(ILGenerator gen)\r
+        protected override void EmitOperation(ILGenerator gen)\r
         {\r
-            e1.Emit(gen);\r
-            e2.Emit(gen);\r
-            gen.Emit(OpCodes.Cgt);\r
-            gen.Emit(OpCodes.Ldc_I4_0);\r
-            gen.Emit(OpCodes.Ceq);\r
-            // TI Basic uses -1/0, .NET uses 1/0, plus we need to convert from Int32 to double\r
-            gen.Emit(OpCodes.Conv_R8);\r
-            gen.Emit(OpCodes.Neg);\r
-        }\r
+            if (argType == BasicType.Number)\r
+            {\r
+                // Do a > comparison\r
+                gen.Emit(OpCodes.Cgt);\r
+            }\r
+            else\r
+            {\r
+                // Do a > comparison\r
+                gen.Emit(OpCodes.Call, compareMethod);\r
+                gen.Emit(OpCodes.Ldc_I4_1);\r
+                gen.Emit(OpCodes.Ceq);\r
+            }\r
 \r
-        public override BasicType GetBasicType()\r
-        {\r
-            return BasicType.Number;\r
         }\r
+\r
     }\r
 }\r
index d11e52c..45bb814 100644 (file)
@@ -59,9 +59,9 @@ namespace mbasic.SyntaxTree
         public override void Emit(ILGenerator gen, bool labelSetAlready)\r
         {\r
             if (!labelSetAlready) MarkLabel(gen);\r
+            MarkSequencePoint(gen);\r
             Label falseCase = gen.DefineLabel();\r
             conditional.Emit(gen);\r
-            MarkSequencePoint(gen);\r
             gen.Emit(OpCodes.Brfalse_S, falseCase);\r
             jmp.Emit(gen, true);\r
             gen.MarkLabel(falseCase);\r
index ca2b3f4..0e23c83 100644 (file)
@@ -22,39 +22,28 @@ using System;
 using System.Collections.Generic;\r
 using System.Text;\r
 using System.Reflection.Emit;\r
+using System.Reflection;\r
 namespace mbasic.SyntaxTree\r
 {\r
-    class LessThan : Expression\r
+    class LessThan : RelationalExpression\r
     {\r
-        Expression e1;\r
-        Expression e2;\r
-        BasicType type;\r
 \r
-        public LessThan(Expression e1, Expression e2, int line)\r
-            : base(line)\r
+        public LessThan(Expression e1, Expression e2, bool not, int line)\r
+            : base(e1, e2, not, line)\r
         {\r
-            this.e1 = e1;\r
-            this.e2 = e2;\r
         }\r
 \r
-        public override BasicType GetBasicType()\r
-        {\r
-            type = BasicType.Number;\r
-            if (e1.GetBasicType() == BasicType.Number && e2.GetBasicType() == BasicType.Number)\r
-                return type;\r
 \r
-            TypeMismtach();\r
-            return type;\r
-        }\r
 \r
-        public override void Emit(ILGenerator gen)\r
+        protected override void EmitOperation(ILGenerator gen)\r
         {\r
-            e1.Emit(gen);\r
-            e2.Emit(gen);\r
-            gen.Emit(OpCodes.Clt);\r
-            // TI Basic uses -1/0, .NET uses 1/0, plus we need to convert from Int32 to double\r
-            gen.Emit(OpCodes.Conv_R8);\r
-            gen.Emit(OpCodes.Neg);\r
+            if (argType == BasicType.Number) gen.Emit(OpCodes.Clt);\r
+            else\r
+            {\r
+                gen.Emit(OpCodes.Call, compareMethod);\r
+                gen.Emit(OpCodes.Ldc_I4, -1);\r
+                gen.Emit(OpCodes.Ceq);\r
+            }\r
         }\r
     }\r
 }\r
diff --git a/mbasic/SyntaxTree/Not.cs b/mbasic/SyntaxTree/Not.cs
deleted file mode 100644 (file)
index 5976dcf..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*******************************************************************************\r
-    Copyright 2006 Michael Welch\r
-    \r
-    This file is part of MBasic99.\r
-\r
-    MBasic99 is free software; you can redistribute it and/or modify\r
-    it under the terms of the GNU General Public License as published by\r
-    the Free Software Foundation; either version 2 of the License, or\r
-    (at your option) any later version.\r
-\r
-    MBasic99 is distributed in the hope that it will be useful,\r
-    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-    GNU General Public License for more details.\r
-\r
-    You should have received a copy of the GNU General Public License\r
-    along with MBasic99; if not, write to the Free Software\r
-    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*******************************************************************************/\r
-\r
-\r
-using System;\r
-using System.Collections.Generic;\r
-using System.Text;\r
-using System.Reflection.Emit;\r
-\r
-namespace mbasic.SyntaxTree\r
-{\r
-    class Not : Expression\r
-    {\r
-        Expression val;\r
-        BasicType type;\r
-\r
-        public Not(Expression e, int line)\r
-            : base(line)\r
-        {\r
-            this.val = e;\r
-        }\r
-        public override BasicType GetBasicType()\r
-        {\r
-            type = BasicType.Number;\r
-            if (val.GetBasicType() == type) return type;\r
-            return BasicType.Error;\r
-        }\r
-\r
-        public override void Emit(ILGenerator gen)\r
-        {\r
-            val.Emit(gen);\r
-            // OKAY, We don't want to NOT a double\r
-            // So we need to convert to an Int32 and then Not\r
-            // and then convert to double and then negate.\r
-            gen.Emit(OpCodes.Conv_I4);\r
-            gen.Emit(OpCodes.Not);\r
-            gen.Emit(OpCodes.Conv_R8);\r
-            gen.Emit(OpCodes.Neg);\r
-        }\r
-    }\r
-}\r
index 668ed3b..32d473b 100644 (file)
@@ -49,8 +49,8 @@ namespace mbasic.SyntaxTree
         public override void Emit(ILGenerator gen, bool labelSetAlready)\r
         {\r
             if (!labelSetAlready) MarkLabel(gen);\r
-            value.Emit(gen);\r
             MarkSequencePoint(gen);\r
+            value.Emit(gen);\r
             if (printItemType == BasicType.String) gen.EmitCall(OpCodes.Call, methodInfoString, new Type[0]);\r
             else gen.EmitCall(OpCodes.Call, methodInfoNum, new Type[0]);\r
 \r
diff --git a/mbasic/SyntaxTree/RelationalExpression.cs b/mbasic/SyntaxTree/RelationalExpression.cs
new file mode 100644 (file)
index 0000000..c7f29ec
--- /dev/null
@@ -0,0 +1,123 @@
+/*******************************************************************************\r
+    Copyright 2006 Michael Welch\r
+    \r
+    This file is part of MBasic99.\r
+\r
+    MBasic99 is free software; you can redistribute it and/or modify\r
+    it under the terms of the GNU General Public License as published by\r
+    the Free Software Foundation; either version 2 of the License, or\r
+    (at your option) any later version.\r
+\r
+    MBasic99 is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    GNU General Public License for more details.\r
+\r
+    You should have received a copy of the GNU General Public License\r
+    along with MBasic99; if not, write to the Free Software\r
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
+*******************************************************************************/\r
+\r
+\r
+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Reflection.Emit;\r
+using System.Reflection;\r
+\r
+namespace mbasic.SyntaxTree\r
+{\r
+    abstract class RelationalExpression : Expression\r
+    {\r
+        Expression e1;\r
+        Expression e2;\r
+        protected BasicType argType;\r
+        bool not;\r
+\r
+        protected static readonly MethodInfo compareMethod =\r
+            typeof(string).GetMethod("Compare",\r
+            new Type[] { typeof(string), typeof(string) });\r
+\r
+        protected RelationalExpression(Expression e1,\r
+            Expression e2, bool not, int line)\r
+            : base(line)\r
+        {\r
+            this.e1 = e1;\r
+            this.e2 = e2;\r
+            this.not = not;\r
+        }\r
+        public override BasicType GetBasicType()\r
+        {\r
+            BasicType t1 = e1.GetBasicType();\r
+            BasicType t2 = e2.GetBasicType();\r
+            if (t1 == t2)\r
+            {\r
+                argType = t1;\r
+                return BasicType.Number;\r
+            }\r
+            return BasicType.Error;\r
+        }\r
+\r
+        public override void Emit(ILGenerator gen)\r
+        {\r
+            e1.Emit(gen);\r
+            e2.Emit(gen);\r
+\r
+            EmitOperation(gen);\r
+\r
+            if (not) EmitNot(gen);\r
+\r
+            // TI Basic uses -1/0, .NET uses 1/0, \r
+            // plus we need to convert from Int32 to double\r
+            gen.Emit(OpCodes.Conv_R8);\r
+            gen.Emit(OpCodes.Neg);\r
+\r
+        }\r
+\r
+        private void EmitNot(ILGenerator gen)\r
+        {\r
+            gen.Emit(OpCodes.Ldc_I4_0);\r
+            gen.Emit(OpCodes.Ceq);\r
+        }\r
+        protected abstract void EmitOperation(ILGenerator gen);\r
+\r
+        public static RelationalExpression CompareEquals(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new Equals(e1, e2, false, line);\r
+        }\r
+\r
+        public static RelationalExpression CompareNotEquals(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new Equals(e1, e2, true, line);\r
+        }\r
+\r
+        public static RelationalExpression CompareLessThan(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new LessThan(e1, e2, false, line);\r
+        }\r
+\r
+        public static RelationalExpression CompareLessThanEquals(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new GreaterThan(e1, e2, true, line);\r
+        }\r
+\r
+        public static RelationalExpression CompareGreaterThan(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new GreaterThan(e1, e2, false, line);\r
+        }\r
+\r
+        public static RelationalExpression CompareGreaterThanEquals(\r
+            Expression e1, Expression e2, int line)\r
+        {\r
+            return new LessThan(e1, e2, true, line);\r
+        }\r
+\r
+    }\r
+\r
+    \r
+}\r
index e03fdef..554f665 100644 (file)
     <Compile Include="SyntaxTree\LessThan.cs" />\r
     <Compile Include="SyntaxTree\Multiply.cs" />\r
     <Compile Include="SyntaxTree\Negative.cs" />\r
-    <Compile Include="SyntaxTree\Not.cs" />\r
     <Compile Include="SyntaxTree\NumberLiteral.cs" />\r
-    <Compile Include="SyntaxTree\LessThanEqual.cs" />\r
+    <Compile Include="SyntaxTree\GreaterThan.cs" />\r
     <Compile Include="SyntaxTree\Node.cs" />\r
     <Compile Include="SyntaxTree\Power.cs" />\r
     <Compile Include="SyntaxTree\Randomize.cs" />\r
+    <Compile Include="SyntaxTree\RelationalExpression.cs" />\r
     <Compile Include="SyntaxTree\Statement.cs" />\r
     <Compile Include="SyntaxTree\Subroutine.cs" />\r
     <Compile Include="SyntaxTree\Subtract.cs" />\r