Skip to content

Commit

Permalink
SML portability: use LargeInt for time-ms
Browse files Browse the repository at this point in the history
  • Loading branch information
fabjan authored and kanaka committed May 2, 2021
1 parent a2d6e66 commit 9798302
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
6 changes: 6 additions & 0 deletions impls/sml/LargeInt.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(* Moscow ML does not have the LargeInt structure,
* but its Int is 64 bit on 64 bit systems.
* We need 64 bit integers for the `time-ms` core function.
*)

structure LargeInt = Int
2 changes: 1 addition & 1 deletion impls/sml/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ifeq ($(sml_MODE),mosml)
SMLC = mosmlc
SMLCOUTFLAG = -o
BUILD_FILE = %.mlb
build_args = -toplevel $(shell grep "\\.sml" $1)
build_args = LargeInt.sml -toplevel $(shell grep "\\.sml" $1)
endif
ifeq ($(sml_MODE),polyml)
SMLC = polyc
Expand Down
10 changes: 5 additions & 5 deletions impls/sml/core.sml
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ val coreNs = List.concat [
| [NIL] => NIL
| _ => raise Domain),
prim "count"
(fn [LIST (l,_)] => INT (length l)
| [VECTOR (v,_)] => INT (length v)
(fn [LIST (l,_)] => INT (length l |> LargeInt.fromInt)
| [VECTOR (v,_)] => INT (length v |> LargeInt.fromInt)
| [NIL] => INT 0
| _ => raise Domain),
prim "nth"
(fn [LIST (l,_), INT n] => (List.nth (l, n) handle Subscript => raise OutOfBounds "index out of bounds")
| [VECTOR (v,_), INT n] => (List.nth (v, n) handle Subscript => raise OutOfBounds "index out of bounds")
(fn [LIST (l,_), INT n] => (List.nth (l, (Int.fromLarge n)) handle Subscript => raise OutOfBounds "index out of bounds")
| [VECTOR (v,_), INT n] => (List.nth (v, (Int.fromLarge n)) handle Subscript => raise OutOfBounds "index out of bounds")
| _ => raise Domain),
prim "first"
(fn [LIST (l,_)] => (case l of (x::_) => x | _ => NIL)
Expand Down Expand Up @@ -201,5 +201,5 @@ val coreNs = List.concat [
prim "throw"
(fn [x] => raise MalException x | _ => raise Domain),
prim "time-ms"
(fn _ => INT (Time.now () |> Time.toMilliseconds |> Int.fromLarge))
(fn _ => INT (Time.now () |> Time.toMilliseconds))
]
2 changes: 1 addition & 1 deletion impls/sml/printer.sml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fun prStr NIL = "nil"
| prStr (BOOL true) = "true"
| prStr (BOOL false) = "false"
| prStr (ATOM x) = "#<atom> (" ^ (prStr (!x)) ^ ")"
| prStr (INT i) = if i >= 0 then Int.toString i else "-" ^ (Int.toString (Int.abs i))
| prStr (INT i) = if i >= 0 then LargeInt.toString i else "-" ^ (LargeInt.toString (LargeInt.abs i))
| prStr (STRING s) = s
| prStr (KEYWORD s) = ":" ^ s
| prStr (LIST (l,_)) = "(" ^ (String.concatWith " " (map prStr l)) ^ ")" (* N.B. not tail recursive *)
Expand Down
2 changes: 1 addition & 1 deletion impls/sml/reader.sml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fun readAtom r = case next r of
SOME (LIT_ATOM "nil", r') => (NIL, r')
| SOME (LIT_ATOM "true", r') => (BOOL true, r')
| SOME (LIT_ATOM "false", r') => (BOOL false, r')
| SOME (LIT_ATOM s, r') => (Int.fromString s |> Option.map INT
| SOME (LIT_ATOM s, r') => (LargeInt.fromString s |> Option.map INT
|> optIfNone (fn () => Option.filter (String.isPrefix ":") s |> Option.map (KEYWORD o (triml 1)))
|> valIfNone (fn () => SYMBOL s), r')
| SOME (LIT_STR s, r') => (malUnescape s |> STRING, r')
Expand Down
2 changes: 1 addition & 1 deletion impls/sml/types.sml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
datatype mal_type = NIL
| SYMBOL of string
| BOOL of bool
| INT of int
| INT of LargeInt.int
| STRING of string
| KEYWORD of string
| LIST of (mal_type list * mal_meta)
Expand Down

0 comments on commit 9798302

Please sign in to comment.