Skip to content

Commit b826745

Browse files
authored
Prelude split into multiple files (#133)
Now the Prelude is split into several files. The two most notable files are `Base/Types` with some standard types and `Base/Operators` with some standard operators. Each of other files contains only methods that should be visible without importing anything. There is no `Base/List` file with standard methods on list. The user must import `List` module by himself. Other minor changes are the following: - Added LICENSE header in each library file - Added comparison operators for Bool type - Added unary minus to `Int` type - Removed `makeString` method (function?), as it seemed broken.
1 parent 88754bf commit b826745

10 files changed

+168
-102
lines changed

dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
(install
66
(section lib)
7-
(files (glob_files (lib/*.fram with_prefix stdlib))))
7+
(files (glob_files_rec (lib/*.fram with_prefix stdlib))))

lib/Base/Bool.fram

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
import open Types
6+
7+
pub method toString = if self then "True" else "False"
8+
9+
pub method neg =
10+
if self then False else True
11+
12+
pub method equal (oth : Bool) =
13+
if self then oth else oth.neg
14+
15+
pub method neq (oth : Bool) =
16+
if self then oth.neg else oth
17+
18+
pub method gt (oth : Bool) =
19+
self && oth.neg
20+
21+
pub method lt {self : Bool} oth =
22+
self.neg && oth
23+
24+
pub method ge (oth : Bool) =
25+
self || oth.neg
26+
27+
pub method le {self : Bool} oth =
28+
self.neg || oth

lib/Base/Char.fram

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
import open /Base/Types
6+
7+
pub method code = (extern dbl_chrCode : Char -> Int) self
8+
9+
pub method equal = (extern dbl_eqInt : Char -> Char -> Bool) self
10+
pub method neq = (extern dbl_neqInt : Char -> Char -> Bool) self
11+
pub method gt = (extern dbl_gtInt : Char -> Char -> Bool) self
12+
pub method lt = (extern dbl_ltInt : Char -> Char -> Bool) self
13+
pub method ge = (extern dbl_geInt : Char -> Char -> Bool) self
14+
pub method le = (extern dbl_leInt : Char -> Char -> Bool) self
15+
16+
pub method toString = (extern dbl_chrToString : Char -> String) self

lib/Base/Int.fram

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
import open Types
6+
import open Operators
7+
8+
pub method toString = (extern dbl_intToString : Int -> String) self
9+
10+
pub method equal = (extern dbl_eqInt : Int -> Int -> Bool) self
11+
pub method neq = (extern dbl_neqInt : Int -> Int -> Bool) self
12+
pub method gt = (extern dbl_gtInt : Int -> Int -> Bool) self
13+
pub method lt = (extern dbl_ltInt : Int -> Int -> Bool) self
14+
pub method ge = (extern dbl_geInt : Int -> Int -> Bool) self
15+
pub method le = (extern dbl_leInt : Int -> Int -> Bool) self
16+
17+
pub method add = (extern dbl_addInt : Int -> Int -> Int) self
18+
pub method sub = (extern dbl_subInt : Int -> Int -> Int) self
19+
pub method mul = (extern dbl_mulInt : Int -> Int -> Int) self
20+
21+
pub method div {`re : {type X} -> Unit ->[|_] X} (n : Int) =
22+
if n.equal 0 then `re ()
23+
else (extern dbl_divInt : Int -> Int -> Int) self n
24+
25+
pub method mod {`re : {type X} -> Unit ->[|_] X} (n : Int) =
26+
if n.equal 0 then `re ()
27+
else (extern dbl_modInt : Int -> Int -> Int) self n
28+
29+
pub method neg {self : Int} = 0 - self
30+
31+
pub method land = (extern dbl_andInt : Int -> Int -> Int) self
32+
pub method lor = (extern dbl_orInt : Int -> Int -> Int) self
33+
pub method lxor = (extern dbl_xorInt : Int -> Int -> Int) self
34+
35+
pub method shiftl = (extern dbl_lslInt : Int -> Int -> Int) self
36+
pub method shiftr = (extern dbl_lsrInt : Int -> Int -> Int) self
37+
pub method ashiftr = (extern dbl_asrInt : Int -> Int -> Int) self

lib/Base/Operators.fram

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
pub method fn (- .) = neg
6+
7+
pub method fn (+) = add
8+
pub method fn (-) = sub
9+
pub method fn (%) = mod
10+
pub method fn (/) = div
11+
pub method fn ( * ) = mul
12+
13+
pub method fn (==) = equal
14+
pub method fn (!=) = neq
15+
pub method fn (>) = gt
16+
pub method fn (>=) = ge
17+
pub method fn (<) = lt
18+
pub method fn (<=) = le
19+
20+
pub method fn (&&&) = land
21+
pub method fn (^^^) = lxor
22+
pub method fn (|||) = lor
23+
pub method fn (<<) = shiftl
24+
pub method fn (>>) = shiftr
25+
pub method fn (>>>) = ashiftr
26+
27+
pub method fn (:=) = set

lib/Base/String.fram

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
import open /Base/Types
6+
import open /Base/Operators
7+
import /Base/Int
8+
9+
pub method add = (extern dbl_strCat : String -> String -> String) self
10+
11+
pub method equal = (extern dbl_eqStr : String -> String -> Bool) self
12+
pub method neq = (extern dbl_neqStr : String -> String -> Bool) self
13+
pub method gt = (extern dbl_gtStr : String -> String -> Bool) self
14+
pub method lt = (extern dbl_ltStr : String -> String -> Bool) self
15+
pub method ge = (extern dbl_geStr : String -> String -> Bool) self
16+
pub method le = (extern dbl_leStr : String -> String -> Bool) self
17+
18+
pub method length = (extern dbl_strLen : String -> Int) self
19+
pub method get {`re : {type X} -> Unit ->[|_] X, self : String} (n : Int) =
20+
if n >= 0 && n < self.length then
21+
(extern dbl_strGet : String -> Int -> Char) self n
22+
else `re ()
23+
24+
pub method toList {self : String} =
25+
let getChar = extern dbl_strGet : String -> Int -> Char in
26+
let rec iter (n : Int) acc =
27+
if n == 0 then
28+
acc
29+
else
30+
iter (n - 1) (getChar self (n - 1) :: acc)
31+
in iter self.length []

lib/Base/Types.fram

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
5+
pub data Bool = False | True
6+
7+
pub data Option A = None | Some of A
8+
9+
pub data rec List A = [] | (::) of A, List A
10+
11+
pub data Pair X Y = (,) of X, Y
12+
13+
pub data Either X Y = Left of X | Right of Y

lib/List.fram

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
4+
15
pub let isEmpty xs =
26
match xs with
37
| [] => True

lib/Prelude.fram

+11-100
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
pub data Bool = False | True
1+
(* This file is part of DBL, released under MIT license.
2+
* See LICENSE for details.
3+
*)
24

3-
pub data Option A = None | Some of A
5+
import /Base/Types
6+
import /Base/Operators
7+
import /Base/Bool
8+
import /Base/Int
9+
import /Base/Char
10+
import /Base/String
411

5-
pub data rec List A = [] | (::) of A, List A
6-
7-
pub data Pair X Y = (,) of X, Y
8-
9-
pub data Either X Y = Left of X | Right of Y
12+
pub open Types
13+
pub open Operators
1014

1115
pub let id x = x
1216

@@ -17,107 +21,14 @@ pub let snd (_, y) = y
1721

1822
pub let not b = if b then False else True
1923

20-
pub method toString = if self then "True" else "False"
21-
22-
pub method fn (+) = add
23-
pub method fn (-) = sub
24-
pub method fn (%) = mod
25-
pub method fn (/) = div
26-
pub method fn ( * ) = mul
27-
28-
pub method fn (==) = equal
29-
pub method fn (!=) = neq
30-
pub method fn (>) = gt
31-
pub method fn (>=) = ge
32-
pub method fn (<) = lt
33-
pub method fn (<=) = le
34-
35-
pub method fn (&&&) = land
36-
pub method fn (^^^) = lxor
37-
pub method fn (|||) = lor
38-
pub method fn (<<) = shiftl
39-
pub method fn (>>) = shiftr
40-
pub method fn (>>>) = ashiftr
41-
42-
pub method equal = (extern dbl_eqInt : Int -> Int -> Bool) self
43-
pub method neq = (extern dbl_neqInt : Int -> Int -> Bool) self
44-
pub method gt = (extern dbl_gtInt : Int -> Int -> Bool) self
45-
pub method lt = (extern dbl_ltInt : Int -> Int -> Bool) self
46-
pub method ge = (extern dbl_geInt : Int -> Int -> Bool) self
47-
pub method le = (extern dbl_leInt : Int -> Int -> Bool) self
48-
49-
pub method toString = (extern dbl_intToString : Int -> String) self
50-
51-
pub method add = (extern dbl_addInt : Int -> Int -> Int) self
52-
pub method sub = (extern dbl_subInt : Int -> Int -> Int) self
53-
pub method mul = (extern dbl_mulInt : Int -> Int -> Int) self
54-
55-
pub method div {`re : {type X} -> Unit ->[|_] X} (n : Int) =
56-
if n.equal 0 then `re ()
57-
else (extern dbl_divInt : Int -> Int -> Int) self n
58-
59-
pub method mod {`re : {type X} -> Unit ->[|_] X} (n : Int) =
60-
if n.equal 0 then `re ()
61-
else (extern dbl_modInt : Int -> Int -> Int) self n
62-
63-
pub method land = (extern dbl_andInt : Int -> Int -> Int) self
64-
pub method lor = (extern dbl_orInt : Int -> Int -> Int) self
65-
pub method lxor = (extern dbl_xorInt : Int -> Int -> Int) self
66-
67-
pub method shiftl = (extern dbl_lslInt : Int -> Int -> Int) self
68-
pub method shiftr = (extern dbl_lsrInt : Int -> Int -> Int) self
69-
pub method ashiftr = (extern dbl_asrInt : Int -> Int -> Int) self
70-
71-
pub method add = (extern dbl_strCat : String -> String -> String) self
72-
73-
pub method equal = (extern dbl_eqStr : String -> String -> Bool) self
74-
pub method neq = (extern dbl_neqStr : String -> String -> Bool) self
75-
pub method gt = (extern dbl_gtStr : String -> String -> Bool) self
76-
pub method lt = (extern dbl_ltStr : String -> String -> Bool) self
77-
pub method ge = (extern dbl_geStr : String -> String -> Bool) self
78-
pub method le = (extern dbl_leStr : String -> String -> Bool) self
79-
80-
pub method length = (extern dbl_strLen : String -> Int) self
81-
pub method get {`re : {type X} -> Unit ->[|_] X, self : String} (n : Int) =
82-
if n >= 0 && n < self.length then
83-
(extern dbl_strGet : String -> Int -> Char) self n
84-
else `re ()
85-
86-
pub method makeString {`re : {type X} -> Unit ->[|_] X, self : String}
87-
(n : Int) =
88-
if n >= 0 && n < 256 then
89-
(extern dbl_strMake : Int -> String) n
90-
else `re ()
91-
92-
pub method toList {self : String} =
93-
let getChar = extern dbl_strGet : String -> Int -> Char in
94-
let rec iter (n : Int) acc =
95-
if n == 0 then
96-
acc
97-
else
98-
iter (n - 1) (getChar self (n - 1) :: acc)
99-
in iter self.length []
100-
10124
pub let charListToStr = (extern dbl_chrListToStr : List Char -> String)
10225

103-
pub method code {self : Char} =
104-
extern dbl_chrCode : Char -> Int
105-
10626
pub let chr {`re : {type X} -> Unit ->[|_] X} (n : Int) =
10727
if n >= 0 && n < 256 then
10828
(extern dbl_intToChr : Int -> Char) n
10929
else
11030
`re ()
11131

112-
pub method equal = (extern dbl_eqInt : Char -> Char -> Bool) self
113-
pub method neq = (extern dbl_neqInt : Char -> Char -> Bool) self
114-
pub method gt = (extern dbl_gtInt : Char -> Char -> Bool) self
115-
pub method lt = (extern dbl_ltInt : Char -> Char -> Bool) self
116-
pub method ge = (extern dbl_geInt : Char -> Char -> Bool) self
117-
pub method le = (extern dbl_leInt : Char -> Char -> Bool) self
118-
119-
pub method toString = (extern dbl_chrToString : Char -> String) self
120-
12132
pub let printStrLn = extern dbl_printStrLn : String ->[IO] Unit
12233
pub let printStr = extern dbl_printStr : String ->[IO] Unit
12334
pub let printInt = extern dbl_printInt : Int ->[IO] Unit

src/Eval.ml

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ let extern_map =
101101
"dbl_leStr", str_cmpop ( <= );
102102
"dbl_strLen", str_fun (fun s -> VNum (String.length s));
103103
"dbl_strGet", str_fun (fun s -> int_fun (fun n -> VNum (Char.code s.[n])));
104-
"dbl_strMake", int_fun (fun n -> VStr (String.make 1 (Char.chr n)));
105104
"dbl_chrToString", int_fun (fun c -> VStr (Char.escaped (Char.chr c)));
106105
"dbl_chrListToStr", list_chr_fun (fun xs -> VStr (List.to_seq xs |> String.of_seq));
107106
"dbl_chrCode", int_fun (fun c -> VNum c);

0 commit comments

Comments
 (0)