Skip to content

Commit

Permalink
my changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alkubaig committed Apr 6, 2018
1 parent d7c81bd commit 0ea5072
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pervasives.eff
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ external ( ^ ) : string -> string -> string = "^"

external string_length : string -> int = "string_length"

external trim : string -> string = "trim"

external split_on_char : char -> string -> string list = "split_on_char"

external to_string : 'a -> string = "to_string"

type 'a option = None | Some of 'a
Expand Down
17 changes: 15 additions & 2 deletions src/runtime/external.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module V = Value

let from_char c = V.Const (Const.of_char c)

let from_bool b = V.Const (Const.of_boolean b)

let from_int n = V.Const (Const.of_integer n)
Expand All @@ -10,6 +12,8 @@ let from_float f = V.Const (Const.of_float f)

let from_fun f = V.Closure f

let value_list b = V.Value (Tuple b)

let value_bool b = V.Value (from_bool b)

let value_int n = V.Value (from_int n)
Expand Down Expand Up @@ -158,10 +162,19 @@ let arithmetic_operations =
; ("/.", float_float_to_float ( /. )) ]


let string_string_to_list f =
let ff v1 v2 = value_list (List.map from_str (f (V.to_char v1) (V.to_str v2))) in
binary_closure ff


let string_operations =
[ ("^", binary_closure (fun v1 v2 -> value_str (V.to_str v1 ^ V.to_str v2)))
; ( "string_length"
, from_fun (fun v -> value_int (String.length (V.to_str v))) ) ]
; ( "string_length", from_fun (fun v -> value_int (String.length (V.to_str v))) )
; ( "trim", from_fun (fun v -> value_str (String.trim (V.to_str v))) )
; ( "split_on_char", string_string_to_list String.split_on_char)
]




let conversion_functions =
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ let to_out_channel = function
| _ -> Error.runtime "A out_channel value expected."


let to_char = function
| Const Const.Char b -> b
| _ -> Error.runtime "A char value expected."

let to_bool = function
| Const Const.Boolean b -> b
| _ -> Error.runtime "A boolean value expected."
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/value.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ val second : value -> value
val to_out_channel : value -> out_channel
val to_in_channel : value -> in_channel

val to_char : value -> char

val to_bool : value -> bool

val to_int : value -> int
Expand Down
1 change: 1 addition & 0 deletions src/typing/infer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ let ty_of_const = function
| Const.Float _ -> Type.float_ty
| Const.Out_channel _ -> Type.out_channel_ty
| Const.In_channel _ -> Type.in_channel_ty
| Const.Char _ -> Type.char_ty


(* [infer_pattern cstr pp] infers the type of pattern [pp]. It returns the list of
Expand Down
1 change: 1 addition & 0 deletions src/typing/tctx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type tyctx = (OldUtils.tyname, Type.ty_param list * tydef) OldUtils.assoc
let initial : tyctx =
[ ("in_channel", ([], Inline T.in_channel_ty))
; ("out_channel", ([], Inline T.out_channel_ty))
; ("char", ([], Inline T.char_ty))
; ("bool", ([], Inline T.bool_ty))
; ("unit", ([], Inline T.unit_ty))
; ("int", ([], Inline T.int_ty))
Expand Down
2 changes: 2 additions & 0 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ let out_channel_ty = Basic "out_channel"

let universal_ty = Basic "_"

let char_ty = Basic "char"

let int_ty = Basic "int"

let string_ty = Basic "string"
Expand Down
5 changes: 5 additions & 0 deletions src/utils/const.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type t =
| Char of char
| Integer of int
| String of string
| Boolean of bool
Expand All @@ -10,6 +11,8 @@ type t =
let of_in_channel n = In_channel n
let of_out_channel n = Out_channel n

let of_char c = Char c

let of_integer n = Integer n

let of_string s = String s
Expand All @@ -24,6 +27,7 @@ let of_false = of_boolean false

let print c ppf =
match c with
| Char k -> Format.fprintf ppf "%c" k
| Integer k -> Format.fprintf ppf "%d" k
| String s -> Format.fprintf ppf "%S" s
| Boolean b -> Format.fprintf ppf "%B" b
Expand All @@ -44,6 +48,7 @@ let compare c1 c2 =
| String s1, String s2 -> cmp s1 s2
| Boolean b1, Boolean b2 -> cmp b1 b2
| Float x1, Float x2 -> cmp x1 x2
| Char x1, Char x2 -> cmp x1 x2
| _ -> Error.runtime "Incomparable constants %t and %t" (print c1) (print c2)


Expand Down
3 changes: 3 additions & 0 deletions src/utils/const.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type t = private
| Char of char
| Integer of int
| String of string
| Boolean of bool
Expand All @@ -9,6 +10,8 @@ type t = private
val of_in_channel : in_channel -> t
val of_out_channel : out_channel -> t

val of_char : char -> t

val of_integer : int -> t

val of_string : string -> t
Expand Down

0 comments on commit 0ea5072

Please sign in to comment.