Allowed Randomize to have any numeric expression for a seed.
authorMichael Welch <michaelgwelch@gmail.com>
Fri, 29 Dec 2006 02:44:12 +0000 (02:44 +0000)
committerMichael Welch <michaelgwelch@gmail.com>
Fri, 29 Dec 2006 02:44:12 +0000 (02:44 +0000)
Parser.cs:
Fixed parsing of Randomize to allow any numeric expression.

Randomize.cs:
Modified class to take an expression rather than an int for its seed. Modifed Emit to match this.

secrenum.mbas:
Modified program to show that even a relational expression can be used as a seed.

BuiltIns.cs:
Modified RandomizeWithSeed to take a double instead of an int.

TiBasicRuntime/BuiltIns.cs
mbasic/Parser.cs
mbasic/SyntaxTree/Randomize.cs
samples/secretnum.mbas

index 5781979..f7a43f0 100644 (file)
@@ -45,9 +45,12 @@ namespace TiBasicRuntime
             rand = new Random();
         }
 
-        public static void RandomizeWithSeed(int seed)
+        public static void RandomizeWithSeed(double seed)
         {
-            rand = new Random(seed);
+            if (seed > Int32.MaxValue) seed = Int32.MaxValue;
+            if (seed < Int32.MinValue) seed = Int32.MinValue;
+            int intSeed = Convert.ToInt32(seed);
+            rand = new Random(intSeed);
         }
 
         public static double Rnd()
index f77a205..f843765 100644 (file)
@@ -170,14 +170,10 @@ namespace mbasic
         {
             LineId line = lexer.LineId;
             Match(Token.Randomize);
-            if (lookahead == Token.Number)
-            {
-                double seedValue = lexer.NumericValue;
-                Match(Token.Number);
-                return new Randomize(Convert.ToInt32(Math.Floor(seedValue)), 
-                    line);
-            }
-            return new Randomize(line);
+
+            if (lookahead == Token.EndOfLine || lookahead == Token.EOF) return new Randomize(line);
+
+            else return new Randomize(Expression(), line);
         }
 
         private Statement IfStatement()
index 24b0e2d..205d4fc 100644 (file)
@@ -36,7 +36,8 @@ namespace mbasic.SyntaxTree
         private static readonly MethodInfo randomizeWithSeed =
             builtinsType.GetMethod("RandomizeWithSeed");
 
-        int seedValue;
+        Expression seedExpression;
+        BasicType exprType;
         bool seedSpecified;
 
         public Randomize(LineId line) : base(line) 
@@ -44,16 +45,18 @@ namespace mbasic.SyntaxTree
             seedSpecified = false;
         }
 
-        public Randomize(int seedValue, LineId line)
+        public Randomize(Expression seedExpression, LineId line)
             : base(line)
         {
-            this.seedValue = seedValue;
+            this.seedExpression = seedExpression;
             seedSpecified = true;
         }
 
         public override void CheckTypes()
         {
-            // nothing to do
+            exprType = seedExpression.GetBasicType();
+            if (exprType == BasicType.Number || exprType == BasicType.Boolean) return;
+            throw new Exception("Type error");
         }
 
         public override void Emit(ILGenerator gen)
@@ -67,7 +70,9 @@ namespace mbasic.SyntaxTree
             MarkSequencePoint(gen);
             if (seedSpecified)
             {
-                gen.Emit(OpCodes.Ldc_I4, seedValue);
+                seedExpression.Emit(gen);
+                if (exprType == BasicType.Boolean) EmitConvertToDouble(gen);
+
                 gen.Emit(OpCodes.Call, randomizeWithSeed);
             }
             else
index e5f7216..e9b706f 100644 (file)
@@ -1,4 +1,4 @@
-110 RANDOMIZE
+110 RANDOMIZE  3<5
 120 MSG1$ = "SECRET NUMBER IS"
 130 MSG2$ = "YOUR TWO NUMBERS"