Skip to content

Commit

Permalink
Return the roll value as a string in the JS interface
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Jun 15, 2019
1 parent 6a14e87 commit 8b19ee6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/javascript.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
open Main

let toString ival = match ival with
Some x -> Main.stringIVal x
| None -> ""

module Javascript =
struct
let () =
Js.export_all
(object%js
method run src = Main.main (Lexing.from_string (Js.to_string src)) 1 (Some 1)
method run src = toString (Main.run (Lexing.from_string (Js.to_string src)) 1 (fun d -> d))
end)
end
5 changes: 3 additions & 2 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct
let dice =
let (decls,exp) = Parser.dice Lexer.token lb in
(decls, defs exp) in
let roll = fun _ -> printVal (Interpreter.rollDice (Syntax.Syntax.optimize_tco dice)) in
let roll = fun _ -> Interpreter.rollDice (Syntax.Syntax.optimize_tco dice) in
List.hd (times n roll)

let print_error = print (* TODO: To stderr? *)
Expand All @@ -49,7 +49,8 @@ struct
| None -> Random.self_init () in
try
match run source count (fun d -> d) with
_ -> ()
Some x -> printVal x
| None -> ()
with Caml.Parsing.YYexit _ -> print_error "Parser-exit\n"
| Parser.Error ->
let (lin,col)
Expand Down

0 comments on commit 8b19ee6

Please sign in to comment.