Modified Read.cs to just be a collection of Assign statements. That way we have less...
authorMichael Welch <michaelgwelch@gmail.com>
Mon, 8 Jan 2007 00:32:26 +0000 (00:32 +0000)
committerMichael Welch <michaelgwelch@gmail.com>
Mon, 8 Jan 2007 00:32:26 +0000 (00:32 +0000)
TiBasicRuntime/BuiltIns.cs
mbasic/Parser.cs
mbasic/SyntaxTree/Assign.cs
mbasic/SyntaxTree/BuiltInsMethodCall.cs [new file with mode: 0644]
mbasic/SyntaxTree/Read.cs
mbasic/mbasic.csproj

index 96ead9b..f6bfef3 100644 (file)
@@ -299,12 +299,14 @@ namespace TiBasicRuntime
             data.Add(label, objects);
         }
 
-        public static void ReadDouble(out double d)
+        public static double ReadNumberFromData()
         {
+            double d;
             object o = Read();
             if (o is double)
             {
                 d = (double)o;
+                return d;
             }
             else
             {
@@ -312,8 +314,9 @@ namespace TiBasicRuntime
             }
         }
 
-        public static void ReadString(out string s)
+        public static string ReadStringFromData()
         {
+            string s;
             object o = Read();
             if (o is string)
             {
@@ -323,6 +326,7 @@ namespace TiBasicRuntime
             {
                 s = Radix100.ToString((double)o);
             }
+            return s;
         }
 
         private static object Read()
index 78565a2..5a88c7b 100644 (file)
@@ -35,10 +35,12 @@ namespace mbasic
         public Lexer lexer;
         SortedList<string, object[]> data;
         Token lookahead;
+        SymbolTable symbols;
         public Parser(Stream stream, SymbolTable symbols, SortedList<string, object[]> data)
         {
             lexer = new Lexer(stream, symbols);
             this.data = data;
+            this.symbols = symbols;
         }
 
         public Statement Parse()
@@ -180,15 +182,23 @@ namespace mbasic
         {
             Match(Token.Read);
             LineId line = lexer.LineId;
-            List<int> indexes = new List<int>();
+            List<Assign> reads = new List<Assign>();
             while (lookahead != Token.EOF && lookahead != Token.EndOfLine)
             {
-                indexes.Add(lexer.SymbolIndex); // The variable to read into.
+                int symbolIndex = lexer.SymbolIndex;
+                switch (symbols[symbolIndex].BasicType)
+                {
+                    case BasicType.Number:
+                        reads.Add(Assign.ReadNumberFromData(symbolIndex, line));
+                        break;
+                    case BasicType.String:
+                        reads.Add(Assign.ReadStringFromData(symbolIndex, line));
+                        break;
+                }
                 Match(Token.Variable);
                 if (lookahead == Token.Comma) Match(Token.Comma);
             }
-            Read read = new Read(indexes.ToArray(), line);
-            return read;
+            return new Read(reads.ToArray(), line);
         }
 
         private Statement DataStatement()
index 697fdae..1e0a59d 100644 (file)
@@ -22,6 +22,9 @@ using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Reflection.Emit;
+using System.Reflection;
+using TiBasicRuntime;
+
 namespace mbasic.SyntaxTree
 {
     class Assign : Statement
@@ -40,6 +43,7 @@ namespace mbasic.SyntaxTree
         {
             Emit(gen, false);
         }
+
         public override void Emit(ILGenerator gen, bool labelSetAlready)
         {
             if (!labelSetAlready) MarkLabel(gen);
@@ -66,5 +70,26 @@ namespace mbasic.SyntaxTree
             
         }
 
+
+        public static Assign ReadStringFromData(int symbolIndex, LineId line)
+        {
+            return new Assign(symbolIndex, BuiltInsMethodCall.ReadStringFromData(), line);
+        }
+
+        public static Assign ReadNumberFromData(int symbolIndex, LineId line)
+        {
+            return new Assign(symbolIndex, BuiltInsMethodCall.ReadNumberFromData(), line);
+        }
+
+        public static Assign ReadStringFromConsole(int symbolIndex, LineId line)
+        {
+            throw new NotImplementedException();
+        }
+
+        public static Assign ReadNumberFromConsole(int symbolIndex, LineId line)
+        {
+            throw new NotImplementedException();
+        }
+
     }
 }
diff --git a/mbasic/SyntaxTree/BuiltInsMethodCall.cs b/mbasic/SyntaxTree/BuiltInsMethodCall.cs
new file mode 100644 (file)
index 0000000..1b4382d
--- /dev/null
@@ -0,0 +1,48 @@
+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Reflection;\r
+using System.Reflection.Emit;\r
+using TiBasicRuntime;\r
+namespace mbasic.SyntaxTree\r
+{\r
+    class BuiltInsMethodCall : Expression\r
+    {\r
+        private static readonly Type builtInsType = typeof(BuiltIns);\r
+        private static readonly MethodInfo readStringFromData =\r
+            builtInsType.GetMethod("ReadStringFromData");\r
+        private static readonly MethodInfo readNumberFromData =\r
+            builtInsType.GetMethod("ReadNumberFromData");\r
+\r
+        private MethodInfo method;\r
+        private BasicType type;\r
+        private BuiltInsMethodCall(MethodInfo method)\r
+            : base(LineId.None)\r
+        {\r
+            this.method = method;\r
+            if (this.method.ReturnType == typeof(string)) type = BasicType.String;\r
+            else type = BasicType.Number;\r
+        }\r
+\r
+        public override void Emit(ILGenerator gen)\r
+        {\r
+            gen.Emit(OpCodes.Call, method);\r
+        }\r
+\r
+        public override BasicType GetBasicType()\r
+        {\r
+            return type;\r
+        }\r
+\r
+        public static BuiltInsMethodCall ReadStringFromData()\r
+        {\r
+            return new BuiltInsMethodCall(readStringFromData);\r
+        }\r
+\r
+        public static BuiltInsMethodCall ReadNumberFromData()\r
+        {\r
+            return new BuiltInsMethodCall(readNumberFromData);\r
+        }\r
+\r
+    }\r
+}\r
index 09b27fd..f2dcaa4 100644 (file)
@@ -9,51 +9,28 @@ namespace mbasic.SyntaxTree
 {
     class Read : Statement
     {
-        int[] indexes;
-        BasicType[] varType;
+        Assign[] assignments;
 
-        private static readonly MethodInfo readNumberMethod =
-            typeof(BuiltIns).GetMethod("ReadDouble");
-
-        private static readonly MethodInfo readStringMethod =
-            typeof(BuiltIns).GetMethod("ReadString");
-
-        public Read(int[] indexes, LineId line)
+        public Read(Assign[] assignments, LineId line)
             : base(line)
         {
-            this.indexes = indexes;
-            varType = new BasicType[indexes.Length];
+            this.assignments = assignments;
         }
 
         public override void CheckTypes()
         {
-            for (int i = 0; i < indexes.Length; i++)
-            {
-                Type t = locals[indexes[i]].LocalType;
-
-                if (t == typeof(string)) varType[i] = BasicType.String;
-                else varType[i] = BasicType.Number;
-            }
+            // Nothing to do. The parser creates assigns that have matching
+            // variables and value types.
         }
 
         public override void Emit(ILGenerator gen, bool labelSetAlready)
         {
             if (!labelSetAlready) MarkLabel(gen);
             MarkSequencePoint(gen);
-            for (int i = 0; i < indexes.Length; i++)
+            for (int i = 0; i < assignments.Length; i++)
             {
-                gen.Emit(OpCodes.Ldloca, locals[indexes[i]]);
-
-                if (varType[i] == BasicType.Number)
-                {
-                    gen.Emit(OpCodes.Call, readNumberMethod);
-                }
-                else if (varType[i] == BasicType.String)
-                {
-                    gen.Emit(OpCodes.Call, readStringMethod);
-                }
+                assignments[i].Emit(gen, true);
             }
-                
         }
     }
 }
index d444e50..f118e7b 100644 (file)
@@ -44,6 +44,7 @@
     <Compile Include="SyntaxTree\BasicType.cs" />\r
     <Compile Include="SyntaxTree\BinaryOperator.cs" />\r
     <Compile Include="SyntaxTree\Block.cs" />\r
+    <Compile Include="SyntaxTree\BuiltInsMethodCall.cs" />\r
     <Compile Include="SyntaxTree\Concatenate.cs" />\r
     <Compile Include="SyntaxTree\Data.cs" />\r
     <Compile Include="SyntaxTree\Division.cs" />\r