Skip to content

Commit

Permalink
Minor cleanup of Program.fs, input/output handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigStuntz committed Jan 25, 2012
1 parent aa13226 commit f5245a7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 30 deletions.
8 changes: 4 additions & 4 deletions Lbac.Compiler/Part01-Introduction.fs
Expand Up @@ -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"

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Lbac.Compiler/Part02-ExpressionParsing.fs
Expand Up @@ -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.

Expand Down
18 changes: 5 additions & 13 deletions Lbac.Compiler/Program.fs
Expand Up @@ -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")
Expand All @@ -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
3 changes: 1 addition & 2 deletions Lbac.Tests.FSharp/Part01Tests.fs
Expand Up @@ -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())
Expand Down
11 changes: 2 additions & 9 deletions Lbac.Tests.FSharp/Part02Tests.fs
Expand Up @@ -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
Expand Down

0 comments on commit f5245a7

Please sign in to comment.