diff options
author | Rustan Leino <leino@microsoft.com> | 2011-04-07 18:33:22 -0700 |
---|---|---|
committer | Rustan Leino <leino@microsoft.com> | 2011-04-07 18:33:22 -0700 |
commit | 52275bb43cffab6c261f99f737960ba230a420db (patch) | |
tree | 917e81d0ce656d2ce2e860d0540ddeeb859d836a /Jennisys/Jennisys.fs | |
parent | 79a46755ba3768ce489db9188fb66e6285de82a5 (diff) |
Jennisys: Parse and print
Diffstat (limited to 'Jennisys/Jennisys.fs')
-rw-r--r-- | Jennisys/Jennisys.fs | 94 |
1 files changed, 43 insertions, 51 deletions
diff --git a/Jennisys/Jennisys.fs b/Jennisys/Jennisys.fs index 6d060f3b..d266a065 100644 --- a/Jennisys/Jennisys.fs +++ b/Jennisys/Jennisys.fs @@ -4,60 +4,52 @@ // This posting is provided "AS IS" with no warranties, and confers no rights.
open System
+open System.IO
open Microsoft.FSharp.Text.Lexing
open Ast
open Lexer
open Parser
-
-/// Evaluate a factor
-let rec evalFactor factor =
- match factor with
- | Float x -> x
- | Integer x -> float x
- | ParenEx x -> evalExpr x
-
-/// Evaluate a term
-and evalTerm term =
- match term with
- | Times (term, fact) -> (evalTerm term) * (evalFactor fact)
- | Divide (term, fact) -> (evalTerm term) / (evalFactor fact)
- | Factor fact -> evalFactor fact
-
-/// Evaluate an expression
-and evalExpr expr =
- match expr with
- | Plus (expr, term) -> (evalExpr expr) + (evalTerm term)
- | Minus (expr, term) -> (evalExpr expr) - (evalTerm term)
- | Term term -> evalTerm term
-
-/// Evaluate an equation
-and evalEquation eq =
- match eq with
- | Equation expr -> evalExpr expr
-
-printfn "Calculator"
-
-let rec readAndProcess() =
- printf ":"
- match Console.ReadLine() with
- | "quit" -> ()
- | expr ->
+open Printer
+
+
+let readAndProcess tracing (filename: string) =
+ try
+ printfn "Jennisys, Copyright (c) 2011, Microsoft."
+ // lex
+ let f = if filename = null then Console.In else new StreamReader(filename) :> TextReader
+ let lexbuf = LexBuffer<char>.FromTextReader(f)
+ lexbuf.EndPos <- { pos_bol = 0;
+ pos_fname=if filename = null then "stdin" else filename;
+ pos_cnum=0;
+ pos_lnum=1 }
try
- printfn "Lexing [%s]" expr
- let lexbuff = LexBuffer<char>.FromString(expr)
-
- printfn "Parsing..."
- let equation = Parser.start Lexer.tokenize lexbuff
-
- printfn "Evaluating Equation..."
- let result = evalEquation equation
-
- printfn "Result: %s" (result.ToString())
-
- with ex ->
- printfn "Unhandled Exception: %s" ex.Message
-
- readAndProcess()
-
-readAndProcess()
\ No newline at end of file + // parse
+ let prog = Parser.start Lexer.tokenize lexbuf
+ // print the given Jennisys program
+ if tracing then
+ printfn "---------- Given Jennisys program ----------"
+ Print prog
+ else ()
+ // that's it
+ if tracing then printfn "----------" else ()
+ with
+ | ex ->
+ let pos = lexbuf.EndPos
+ printfn "%s(%d,%d): %s" pos.FileName pos.Line pos.Column ex.Message
+
+ with
+ | ex ->
+ printfn "Unhandled Exception: %s" ex.Message
+
+let rec start n (args: string []) tracing filename =
+ if n < args.Length then
+ let arg = args.[n]
+ if arg = "/break" then ignore (System.Diagnostics.Debugger.Launch()) else ()
+ let filename = if arg.StartsWith "/" then filename else arg
+ start (n+1) args (tracing || arg = "/trace") filename
+ else
+ readAndProcess tracing filename
+
+let args = Environment.GetCommandLineArgs()
+start 1 args false null
|