expression: set floats as f64 by default

pull/4072/head
SleepyRoy 2020-03-19 14:24:49 +08:00 committed by GitHub
parent 969765435e
commit f798a0937a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 30 deletions

View File

@ -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

View File

@ -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

View File

@ -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'
}

View File

@ -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'
}

View File

@ -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`')

View File

@ -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{}

View File

@ -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 ()"

View File

@ -7,8 +7,8 @@ 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

View File

@ -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'

View File

@ -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)