Hi everyone,
I did some hack on Jon's code to provide the private interactive mode
running on the SML/NJ interactive mode.
( I failed to make it stand-alone. It can run alone, but I don't know
how to autoload libraries such as +)
It takes streams, parses them into ast, unparse ast back to a string,
then evaluates such a string to a value.
For example,
- Main.main ("",[""]);
Interactive Mode
# fun fact n = if n = 0 then 1 else n * fact (n-1)
val fact = fn : int -> int
# fact 5
val it = 120 : int
So what? For what reason do we need another SML/NJ interactive mode?
With this machinery, possibly, we can have ...
1. Nested interactive mode by chaining global environments...?
Currently, it is not nested. It globally shares the environment
with the native interactive mode.
2. Embedded interactive mode for a DSL by replacing
Parse.parseString.
We may implement Ast-to-Ast transformation.
It is getting interesting...:-)
p.s.
I am attaching some related code:
------------------------------------------------------------------------------------------------------------
structure Main : sig
val main : string * string list -> OS.Process.status
end = struct
fun evalString str = Backend.Interact.useStream (TextIO.openString
str);
fun main (self, args) =
let val _ = print "Interactive Mode\n"
fun run () =
let val _ = print "# "
val source = getOpt (T.inputLine T.stdIn, "")
val ast = Parse.parseString source
val source' = Parse.unparseAst ast
val _ = evalString source'
in run ()
end
handle e => (TextIO.output (TextIO.stdErr, General.exnMessage e
^ "\n");
run ())
in (run (); OS.Process.success)
end
end
------------------------------------------------------------------------------------------------------------
It calls unparseAst which is my version of ppAstDec:
------------------------------------------------------------------------------------------------------------
fun ppAstDec0 dec =
let val {static=statenv, ...} = getenv ()
fun printfn ppstrm dec = PPAst.ppDec (statenv, NONE) ppstrm
(dec, valOf Int.maxInt)
in PP.pp_to_string 0 printfn dec
end (* ppAstDe0 *)
------------------------------------------------------------------------------------------------------------
where the Parse Module looks like ...
------------------------------------------------------------------------------------------------------------
structure Parse : sig
val sourceFromStream : TextIO.instream -> Source.inputSource
val parseOneStream : TextIO.instream -> unit -> Ast.dec option
val parseOneString : string -> unit -> Ast.dec option
val parseStream : TextIO.instream -> Ast.dec
val parseString : string -> Ast.dec
val unparseAst : Ast.dec -> string (* ppAstDec0 *)
end = struct
...
end
------------------------------------------------------------------------------------------------------------
On Feb 24, 2:10 am, Wonseok Chae <
wsc...@gmail.com> wrote:
> Hi everyone,
> Jon challenged us to write a DSL using his transformer. That is what I
> will do soon.
> Jon, you did a good job.
>
> Wonseok
>
> p.s.
> While I was running the code, I ran into the following error message:
> -- Error: Compiler bug: UnpickMod: stub lookup failed
>
> Anyone who sees this message would like to see the post at "
http://www.nabble.com/compiling-smlnj-td20765432.html". (Thanks Jon)