From f798a0937a63e1d0d9d2fd05de56014f42392fac Mon Sep 17 00:00:00 2001 From: SleepyRoy <47302112+SleepyRoy@users.noreply.github.com> Date: Thu, 19 Mar 2020 14:24:49 +0800 Subject: [PATCH] expression: set floats as f64 by default --- examples/sokol_examples/fonts.v | 6 ++--- vlib/builtin/array_test.v | 12 ++++----- vlib/builtin/float_test.v | 34 ++++++++++++++++++++++++++ vlib/builtin/int_test.v | 17 +++++++++++++ vlib/compiler/expression.v | 12 +++------ vlib/compiler/tests/fixed_array_test.v | 4 +-- vlib/compiler/tests/nameof_test.v | 2 +- vlib/compiler/tests/type_alias_test.v | 6 ++--- vlib/compiler/tests/typeof_test.v | 8 +++--- vlib/gg2/gg.v | 6 ++--- 10 files changed, 77 insertions(+), 30 deletions(-) create mode 100644 vlib/builtin/float_test.v diff --git a/examples/sokol_examples/fonts.v b/examples/sokol_examples/fonts.v index 4533230a48..1d44775672 100644 --- a/examples/sokol_examples/fonts.v +++ b/examples/sokol_examples/fonts.v @@ -76,7 +76,7 @@ fn (state &AppState) render_font() { mut sy := 0.0 mut dx := 0.0 mut dy := 0.0 - lh := 0.0 + lh := f32(0.0) white := C.sfons_rgba(255, 255, 255, 255) black := C.sfons_rgba(0, 0, 0, 255) brown := C.sfons_rgba(192, 128, 0, 128) @@ -91,8 +91,8 @@ fn (state &AppState) render_font() { dy = sy state.fons.set_font(state.font_normal) state.fons.set_size(100.0) - ascender := 0.0 - descender := 0.0 + ascender := f32(0.0) + descender := f32(0.0) state.fons.vert_metrics(&ascender, &descender, &lh) dx = sx dy += lh diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 304d613b6c..1f07207a3c 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -190,10 +190,10 @@ fn test_repeat() { assert a[9] == 123 } { - a := [f64(1.1)].repeat(10) - assert a[0] == f64(1.1) - assert a[5] == f64(1.1) - assert a[9] == f64(1.1) + a := [1.1].repeat(10) + assert a[0] == 1.1 + assert a[5] == 1.1 + assert a[9] == 1.1 } { a := [1, 2].repeat(2) @@ -501,7 +501,7 @@ fn test_sort() { } fn test_f32_sort() { - mut f := [50.0, 15, 1, 79, 38, 0, 27] + mut f := [f32(50.0), 15, 1, 79, 38, 0, 27] f.sort_with_compare(compare_f32) assert f[0] == 0.0 assert f[1] == 1.0 @@ -509,7 +509,7 @@ fn test_f32_sort() { } fn test_f64_sort() { - mut f := [f64(50.0), 15, 1, 79, 38, 0, 27] + mut f := [50.0, 15, 1, 79, 38, 0, 27] f.sort_with_compare(compare_f64) assert f[0] == 0.0 assert f[1] == 1.0 diff --git a/vlib/builtin/float_test.v b/vlib/builtin/float_test.v new file mode 100644 index 0000000000..850d6824ab --- /dev/null +++ b/vlib/builtin/float_test.v @@ -0,0 +1,34 @@ +fn test_float_decl() { + x1 := 1e10 + x2 := -2e16 + x3 := 1e-15 + x4 := -9e-4 + assert typeof(x1) == 'f64' + assert typeof(x2) == 'f64' + assert typeof(x3) == 'f64' + assert typeof(x4) == 'f64' + x5 := 4e108 + x6 := -7e99 + x7 := 3e-205 + x8 := -6e-147 + assert typeof(x5) == 'f64' + assert typeof(x6) == 'f64' + assert typeof(x7) == 'f64' + assert typeof(x8) == 'f64' + x9 := 312874834.77 + x10 := -22399994.06 + x11 := 0.0000000019 + x12 := -0.00000000008 + assert typeof(x9) == 'f64' + assert typeof(x10) == 'f64' + assert typeof(x11) == 'f64' + assert typeof(x12) == 'f64' + x13 := 34234234809890890898903213154353453453253253243432413232228908902183918392183902432432438980380123021983901392183921389083913890389089031.0 + x14 := -39999999999999999999222212128182813294989082302832183928343325325233253242312331324392839238239829389038097438248932789371837218372837293.8 + x15 := 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002 + x16 := -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 + assert typeof(x13) == 'f64' + assert typeof(x14) == 'f64' + assert typeof(x15) == 'f64' + assert typeof(x16) == 'f64' +} diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index 28c4636697..6a6b7ffab8 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -138,3 +138,20 @@ fn test_oct() { x9 := -000 assert x9 == 0 } + +fn test_int_decl() { + x1 := 0 + x2 := 1333 + x3 := -88955 + x4 := 2000000000 + x5 := -1999999999 + assert typeof(x1) == 'int' + assert typeof(x2) == 'int' + assert typeof(x3) == 'int' + assert typeof(x4) == 'int' + assert typeof(x5) == 'int' + x6 := 989898932113111 + x7 := -321314588900011 + assert typeof(x6) == 'u64' + assert typeof(x7) == 'u64' +} diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index 53136ebb06..b311dfe4cf 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -786,17 +786,13 @@ fn (p mut Parser) factor() string { return p.expected_type } .number { - typ = 'int' - // Check if float (`1.0`, `1e+3`) but not if is hexa - if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) && !(p.lit[0] == `0` && (p.lit[1] == `x` || p.lit[1] == `X`)) { - typ = 'f32' - // typ = 'f64' // TODO + // Check if float (`1.0`, `1e+3`) but not if is hexa (e.g. 0xEE contains `E` but is not float) + if (p.lit.contains('.') || p.lit.contains('e') || p.lit.contains('E')) && !(p.lit[..2] in ['0x', '0X']) { + typ = 'f64' } else { v_u64 := p.lit.u64() - if u64(u32(v_u64)) < v_u64 { - typ = 'u64' - } + typ = if u64(u32(v_u64)) < v_u64 { 'u64' } else { 'int' } } if p.expected_type != '' && !is_valid_int_const(p.lit, p.expected_type) { p.error('constant `$p.lit` overflows `$p.expected_type`') diff --git a/vlib/compiler/tests/fixed_array_test.v b/vlib/compiler/tests/fixed_array_test.v index f953050af0..cc2261a260 100644 --- a/vlib/compiler/tests/fixed_array_test.v +++ b/vlib/compiler/tests/fixed_array_test.v @@ -1,7 +1,7 @@ fn test_fixed_array_can_be_assigned(){ x := 2.32 - mut v := [8]f32 + mut v := [8]f64 v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!! assert v[1] == x } @@ -15,7 +15,7 @@ fn test_fixed_array_can_be_used_in_declaration(){ struct Context { pub mut: - vb [8]f32 + vb [8]f64 } fn test_fixed_array_can_be_assigned_to_a_struct_field(){ mut ctx := Context{} diff --git a/vlib/compiler/tests/nameof_test.v b/vlib/compiler/tests/nameof_test.v index 9006181898..e315c71f1c 100644 --- a/vlib/compiler/tests/nameof_test.v +++ b/vlib/compiler/tests/nameof_test.v @@ -9,7 +9,7 @@ struct FunkyStruct{ } fn test_nameof_on_various_types_in_generic() { assert simple(42) == "int" - assert simple(3.14) == "f32" + assert simple(3.14) == "f64" assert simple("FuBar") == "string" assert simple(FunkyStruct{}) == "FunkyStruct" assert simple(test_nameof_on_various_types_in_generic) == "fn ()" diff --git a/vlib/compiler/tests/type_alias_test.v b/vlib/compiler/tests/type_alias_test.v index 690e3be4d2..641a485fc3 100644 --- a/vlib/compiler/tests/type_alias_test.v +++ b/vlib/compiler/tests/type_alias_test.v @@ -6,9 +6,9 @@ type Myf64 f64 fn test_type_alias() { i := Myint(10) assert i + 100 == 110 - - f := Myf32(1.0) - assert f + 3.14 == 4.14 + + f := Myf64(10.4) + assert f + 0.5 == 10.9 diff --git a/vlib/compiler/tests/typeof_test.v b/vlib/compiler/tests/typeof_test.v index 1c12c6817e..e326c223ee 100644 --- a/vlib/compiler/tests/typeof_test.v +++ b/vlib/compiler/tests/typeof_test.v @@ -2,9 +2,9 @@ fn test_typeof_on_simple_expressions() { a := 123 assert typeof(42) == 'int' - assert typeof(3.14) == 'f32' + assert typeof(3.14) == 'f64' assert typeof(2+2*10) == 'int' - assert typeof(1.0 * 12.2) == 'f32' + assert typeof(1.0 * 12.2) == 'f64' assert typeof(a) == 'int' } @@ -39,7 +39,7 @@ pub fn (ms MySumType) str() string { fn test_typeof_on_sumtypes(){ a := MySumType(32) - b := MySumType(123.0) + b := MySumType(f32(123.0)) c := MySumType(FooBar{x:43}) assert typeof(a) == 'int' assert typeof(b) == 'f32' @@ -84,4 +84,4 @@ fn myfn2() {} fn test_typeof_on_fn() { assert typeof(myfn) == 'fn (int) int' assert typeof(myfn2) == 'fn ()' -} \ No newline at end of file +} diff --git a/vlib/gg2/gg.v b/vlib/gg2/gg.v index 8af1b0b69a..aed3db864a 100644 --- a/vlib/gg2/gg.v +++ b/vlib/gg2/gg.v @@ -107,9 +107,9 @@ pub fn new_context(cfg Config) &GG { pub fn (gg &GG) draw_text(x, y int, text string, cfg gx.TextCfg) { gg.fons.set_font(gg.font_normal) gg.fons.set_size(cfg.size) - ascender := 0.0 - descender := 0.0 - lh := 0.0 + ascender := f32(0.0) + descender := f32(0.0) + lh := f32(0.0) gg.fons.vert_metrics(&ascender, &descender, &lh) color:= C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255) C.fonsSetColor(gg.fons, color)