Permalink
Browse files

Minor cleanup of Program.fs, input/output handling.

  • Loading branch information...
1 parent aa13226 commit f5245a78c7d9352f5f14e7127ea54de0984d526a @CraigStuntz committed Jan 25, 2012
@@ -3,7 +3,7 @@
open System
open System.IO
- type Cradle(keyReader: unit -> char, output : TextWriter) as x =
+ type Cradle(input : TextReader, errorWriter : TextWriter) as x =
let mutable _look = '\u0000'
let tab = "\t"
@@ -16,11 +16,11 @@
and set value = _look <- value
member x.getChar() =
- x.look <- char( keyReader() )
+ x.look <- char( input.Read() )
member x.error(s : string) =
- output.WriteLine()
- output.WriteLine("Error: {0}.", s)
+ errorWriter.WriteLine()
+ errorWriter.WriteLine("Error: {0}.", s)
member x.abort(s : string) =
x.error(s)
@@ -4,8 +4,8 @@
open System.IO
open IL
- type ExpressionParsing(keyReader: unit -> char, output : TextWriter) =
- inherit Cradle(keyReader, output)
+ type ExpressionParsing(input : TextReader, errorWriter : TextWriter) =
+ inherit Cradle(input, errorWriter)
// Introduced later, but F# likes it above members.
@@ -8,22 +8,15 @@ let run(reader, writer) =
parser.expression()
let runInteractive() =
- let outputStream = Console.Out
- let keyReader() =
- let cki = Console.ReadKey()
- cki.KeyChar
- let il = run(keyReader, outputStream)
+ let il = run(Console.In, Console.Error)
printfn "%A" il
Console.ReadLine() |> ignore
let runWithFiles inFile outFile =
- let outputStream = Console.Out
let input = File.ReadAllText(inFile)
let reader = new StringReader(input)
- let charReader() =
- Convert.ToChar(reader.Read())
- let il = run(charReader, outputStream)
+ let il = run(reader, Console.Error)
let moduleName = match outFile with
| Some s -> s
| None -> IO.Path.ChangeExtension(inFile, ".exe")
@@ -34,10 +27,9 @@ let runWithFiles inFile outFile =
let main(args) =
let arguments = CommandLine.parse(Array.append [|"Lbac.Compiler.exe"|] args, Console.Out, Console.Error)
if arguments.Valid then
- if arguments.InFile.IsSome then
- runWithFiles arguments.InFile.Value arguments.OutFile
- else
- runInteractive()
+ match arguments.InFile with
+ | Some filename -> runWithFiles filename arguments.OutFile
+ | _ -> runInteractive()
0
else
1
@@ -11,9 +11,8 @@
member x.testInit() =
let actual = new StringBuilder()
let sw = new StringWriter(actual)
- let kr() = '1';
- let cradle = new Cradle(kr, sw)
+ let cradle = new Cradle(new StringReader("1"), sw)
// the introduction "cradle" code doesn't actually do anything.
Assert.AreEqual("", actual.ToString())
@@ -12,22 +12,15 @@
let actual = new StringBuilder()
let sw = new StringWriter(actual)
let sr = new StringReader(input)
- let kr() =
- let c = sr.Read()
- match c with
- | -1 ->
- '\u0000'
- | _ -> System.Convert.ToChar(c);
- let parser = new ExpressionParsing(kr, sw)
+ let parser = new ExpressionParsing(sr, sw)
let il = parser.expression()
IL.execute<System.Int32> (il, None) // change false to true to save assembly to disk -- useful for running PEVerify.
[<TestMethod>]
member x.testTerm() =
let actual = new StringBuilder()
let sw = new StringWriter(actual)
- let kr() = '1';
- let parser = new ExpressionParsing(kr, sw)
+ let parser = new ExpressionParsing(new StringReader("1"), sw)
let actual = parser.term()
let arg = match actual.Head with

0 comments on commit f5245a7

Please sign in to comment.