Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implicit int to float conversion. #2887

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lang/builtins_math.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ let () =
match (a, b) with
| `Int a, `Int b -> Lang.int (op_int a b)
| `Float a, `Float b -> Lang.float (op_float a b)
| _ -> assert false))
| `Int a, `Float b -> Lang.float (op_float (float a) b)
| `Float a, `Int b -> Lang.float (op_float a (float b))))
in
register_op "Multiplication" "*" ( * ) ( *. );
register_op "Division" "/" ( / ) ( /. );
Expand Down
8 changes: 7 additions & 1 deletion src/lang/lang_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,17 @@ let to_string_getter t =
| _ -> assert false

let to_float t =
match (demeth t).value with Ground (Float s) -> s | _ -> assert false
match (demeth t).value with
| Ground (Float s) -> s
| Ground (Int n) -> float_of_int n
| _ -> assert false

let to_float_getter t =
match (demeth t).value with
| Ground (Float s) -> fun () -> s
| Ground (Int n) ->
let n = float_of_int n in
fun () -> n
| Fun _ | FFI _ -> (
fun () ->
match (apply t []).value with
Expand Down
28 changes: 22 additions & 6 deletions src/lang/types/ground_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module Make (S : Spec) = struct
type Type_base.custom += Type

let () = types := Type :: !types
let typ = Type
let get = function Type -> Type | _ -> assert false

let is_descr = function
Expand Down Expand Up @@ -74,18 +75,33 @@ module Make (S : Spec) = struct
Type_base.make (Type_base.Custom handler))
end

module Int = Make (struct
let name = "int"
end)

let int = Int.descr

module Float = Make (struct
let name = "float"
end)

let float = Float.descr

module Int = struct
module Int = Make (struct
let name = "int"
end)

include Int

(* Add int <: float subtyping. *)
let handler =
let subtype _ _ c = assert (c = Int.typ || c = Float.typ) in
{ handler with subtype }
smimram marked this conversation as resolved.
Show resolved Hide resolved

let descr = Type_base.Custom handler

let () =
Type_base.register_type "int" (fun () ->
Type_base.make (Type_base.Custom handler))
end

let int = Int.descr

module String = Make (struct
let name = "string"
end)
Expand Down
65 changes: 65 additions & 0 deletions tests/language/casting.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!../../liquidsoap ../test.liq

def t(x, y)
if x != y then
print("Failure: got #{x} instead of #{y}")
test.fail()
end
end

def incorrect(expr)
print("Incorrect expression #{expr}...\n")

try
let eval _ = expr
test.fail()
catch err do
print("Got err: #{err}")
end

print("\n")
end

def f() =
def double(x) = 2. * x end

t(double(3), 6.)

ignore(if false then 1. else 2 end)

incorrect("if false then 1 else 2. end")

def f(l) =
l = [1, ...l]
l = [2., ...l]
l
end

incorrect("
def f(l) =
l = [2., ...l]
l = [1, ...l]
l
end
")

def f(l) =
let [x] = l
ignore(x + 2.)
let [x] = l
ignore(x + 1)
end

incorrect("
def f(l) =
let [x] = l
ignore(x + 1)
let [x] = l
ignore(x + 2.)
end
")

test.pass()
end

test.check(f)