Write initial Main method to read path from command line
authorMichael Welch <michaelgwelch@gmail.com>
Fri, 17 Feb 2012 03:32:44 +0000 (21:32 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Fri, 17 Feb 2012 03:32:44 +0000 (21:32 -0600)
src.net/Brainmess/Main.cs

index b1249ae..01554a1 100644 (file)
 \feffusing System;
+using System.IO;
 
 namespace Welch.Brainmess
 {
-    // Note: Testing is important. But so is thinking. In this program I've proved the correctness
-    // of the only two "algorithms" I had to write. Testing is great to try to make sure you 
-    // didn't miss anything. But the upfront thinking and proof will help you construct a better solution.
+
     class MainClass
     {
         public static void Main(string[] args)
         {
-
-            var program = new Program(
-                @"+++++++++++ number of digits to output
-> #1
-+ initial number
->>>> #5
-++++++++++++++++++++++++++++++++++++++++++++ (comma)
-> #6
-++++++++++++++++++++++++++++++++ (space)
-<<<<<< #0
-[
-> #1
-3
-copy #1 to #7
-[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]
-<
-divide #7 by 10 (begins in #7)
-[
-  >
-  ++++++++++  set the divisor #8
-  [
-    subtract from the dividend and divisor
-    -<-
-    if dividend reaches zero break out
-      copy dividend to #9
-      [>>+>+<<<-]>>>[<<<+>>>-]
-      set #10
-      +
-      if #9 clear #10
-      <[>[-]<[-]]
-      if #10 move remaining divisor to #11
-      >[<<[>>>+<<<-]>>[-]]
-    jump back to #8 (divisor possition)
-<< ]
-  if #11 is empty (no remainder)
-  increment the quotient #12
-  >>> #11
-  copy to #13
-  [>>+>+<<<-]>>>[<<<+>>>-]
-  set #14
-  +
-  if #13 clear #14
-  <[>[-]<[-]]
-  if #14 increment quotient
-  >[<<+>>[-]]
-  <<<<<<< #7
-]
-quotient is in #12 and remainder is in #11
->>>>> #12
-if #12 output value plus offset to ascii 0
-[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]
-subtract #11 from 10
-++++++++++  #12 is now 10
-< #11
-[->-<]
-> #12
-4
-output #12 even if it’s zero
-  ++++++++++++++++++++++++++++++++++++++++++++++++.[-]
-  <<<<<<<<<<< #1
-  check for final number
-  copy #0 to #3
-  <[>>>+>+<<<<-]>>>>[<<<<+>>>>-]
-  <- #3
-  if #3 output (comma) and (space)
-  [>>.>.<<<[-]]
-  << #1
-  [>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-
-]");
-            /* 
-                program = new Program(@",
-    ------------------------------------------------
-    [->++<]
-    >
-    ++++++++++++++++++++++++++++++++++++++++++++++++.");
-        */
-
-            program = new Program(@"
-++++++++[<+++++++++>-]<.>+++++[<++++++>-]<-.
-+++++++..+++.>++++++++[<<++++>>-]<<.
->>++++[<------>-]<.>++++[<++++++>-]<.
-+++.------.--------.<+.
-");
-            var tape = Tape.Default;
-
-            var input = Console.In;
-            var output = Console.Out;
-
-            Interpreter interpreter = new Interpreter(program, tape, input, output);
-            interpreter.Run();
-            /*
-            output.Close();
-            input.Close();*/
-
+            CommandLine commandLine = CommandLine.Read(args);
+            if (!commandLine.Valid)
+            {
+                Console.WriteLine("Usage: brainmess script.bm");
+            }
+
+            if (File.Exists(commandLine.Path))
+            {
+                var file = File.OpenRead(commandLine.Path).CreateReader();
+                var interpreter = new Interpreter(new Program(file.ReadToEnd()));
+                interpreter.Run();
+            }
         }
 
 
+        public class CommandLine    
+        {
+            public string Path { get; private set; }
+            public bool Valid { get; private set; }
+            public static CommandLine Read(string[] args)
+            {
+                var valid = args.Length == 1;
+                return valid
+                           ? new CommandLine {Valid = true, Path = args[0]}
+                           : new CommandLine();
+            }
+        }
 
 
 
+    }
 
+    public static class StreamExtensions
+    {
+        public static TextReader CreateReader(this Stream stream)
+        {
+            return new StreamReader(stream);
+        }
     }
 }