diff --git a/impls/sml/core.sml b/impls/sml/core.sml index 79ee4b96..d937a57b 100644 --- a/impls/sml/core.sml +++ b/impls/sml/core.sml @@ -118,7 +118,13 @@ val coreIo = [ FN (fn args => args |> map prReadableStr |> String.concatWith " " |> malPrint), SYMBOL "println", - FN (fn args => args |> map prStr |> String.concatWith " " |> malPrint) + FN (fn args => args |> map prStr |> String.concatWith " " |> malPrint), + + SYMBOL "readline", + FN (fn [STRING prompt] => (TextIO.print prompt; + valOrElse (TextIO.inputLine TextIO.stdIn |> Option.map (STRING o (trimr 1))) + (fn () => NIL)) + | _ => raise NotApplicable "readline requires a string") ] fun arithFolder n f (INT next, INT prev) = INT (f (prev, next)) diff --git a/impls/sml/util.sml b/impls/sml/util.sml index 629e1015..b410fe07 100644 --- a/impls/sml/util.sml +++ b/impls/sml/util.sml @@ -26,6 +26,8 @@ fun identity x = x fun triml k s = String.extract (s, k, NONE) +fun trimr k s = String.substring (s, 0, String.size s - k) + fun malEscape s = String.translate (fn #"\"" => "\\\"" | #"\n" => "\\n" | #"\\" => "\\\\"