summaryrefslogtreecommitdiff
path: root/Jennisys/Jennisys.fs
diff options
context:
space:
mode:
Diffstat (limited to 'Jennisys/Jennisys.fs')
-rw-r--r--Jennisys/Jennisys.fs94
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