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 all 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Changed:
`youtube-dl` protocol (#2827). Use `yt-dlp` as default binary for the
protocol.
- The `sleeper` operator is now scripted (#2899).
- Allow implicit casting of an integer as a float (#2887).

Fixed:

Expand Down
3 changes: 3 additions & 0 deletions doc/content/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ so that `3` and `3.` are not the same thing: the former is an integer and the
latter is a float. This is a source of errors for beginners, but is necessary for
typing to work well.

Since version 2.2 of Liquidsoap, you can use an integer where a float is
expected, for instance you can type `sin(3)` instead of `sin(3.)`.

### Strings

Strings are written between double or single quotes,
Expand Down
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
4 changes: 2 additions & 2 deletions tests/core/meth.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ let () =
(* Test subtyping. *)
let () =
(* Make sure unifying variables sees top-level methods:
We do: t = ('a).{ f : int } <: t' = int.{ ff : int, f : float }
We do: t = ('a).{ f : int } <: t' = int.{ ff : int, f : string }
and make sure that this fails. *)
let t = Type.var () in
let t = Type.meth "f" ([], Type.make Type.Ground.int) t in
let t' = Type.make Type.Ground.int in
let t' = Type.meth "f" ([], Type.make Type.Ground.float) t' in
let t' = Type.meth "f" ([], Type.make Type.Ground.string) t' in
let t' = Type.meth "ff" ([], Type.make Type.Ground.int) t' in
assert (
try
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)