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 sy := 0.0
mut dx := 0.0 mut dx := 0.0
mut dy := 0.0 mut dy := 0.0
lh := 0.0 lh := f32(0.0)
white := C.sfons_rgba(255, 255, 255, 255) white := C.sfons_rgba(255, 255, 255, 255)
black := C.sfons_rgba(0, 0, 0, 255) black := C.sfons_rgba(0, 0, 0, 255)
brown := C.sfons_rgba(192, 128, 0, 128) brown := C.sfons_rgba(192, 128, 0, 128)
@ -91,8 +91,8 @@ fn (state &AppState) render_font() {
dy = sy dy = sy
state.fons.set_font(state.font_normal) state.fons.set_font(state.font_normal)
state.fons.set_size(100.0) state.fons.set_size(100.0)
ascender := 0.0 ascender := f32(0.0)
descender := 0.0 descender := f32(0.0)
state.fons.vert_metrics(&ascender, &descender, &lh) state.fons.vert_metrics(&ascender, &descender, &lh)
dx = sx dx = sx
dy += lh dy += lh

View File

@ -190,10 +190,10 @@ fn test_repeat() {
assert a[9] == 123 assert a[9] == 123
} }
{ {
a := [f64(1.1)].repeat(10) a := [1.1].repeat(10)
assert a[0] == f64(1.1) assert a[0] == 1.1
assert a[5] == f64(1.1) assert a[5] == 1.1
assert a[9] == f64(1.1) assert a[9] == 1.1
} }
{ {
a := [1, 2].repeat(2) a := [1, 2].repeat(2)
@ -501,7 +501,7 @@ fn test_sort() {
} }
fn test_f32_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) f.sort_with_compare(compare_f32)
assert f[0] == 0.0 assert f[0] == 0.0
assert f[1] == 1.0 assert f[1] == 1.0
@ -509,7 +509,7 @@ fn test_f32_sort() {
} }
fn test_f64_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) f.sort_with_compare(compare_f64)
assert f[0] == 0.0 assert f[0] == 0.0
assert f[1] == 1.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 x9 := -000
assert x9 == 0 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 return p.expected_type
} }
.number { .number {
typ = 'int' // Check if float (`1.0`, `1e+3`) but not if is hexa (e.g. 0xEE contains `E` but is not float)
// 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[..2] in ['0x', '0X']) {
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 = 'f64'
typ = 'f32'
// typ = 'f64' // TODO
} }
else { else {
v_u64 := p.lit.u64() v_u64 := p.lit.u64()
if u64(u32(v_u64)) < v_u64 { typ = if u64(u32(v_u64)) < v_u64 { 'u64' } else { 'int' }
typ = 'u64'
}
} }
if p.expected_type != '' && !is_valid_int_const(p.lit, p.expected_type) { if p.expected_type != '' && !is_valid_int_const(p.lit, p.expected_type) {
p.error('constant `$p.lit` overflows `$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(){ fn test_fixed_array_can_be_assigned(){
x := 2.32 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]!! v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!!
assert v[1] == x assert v[1] == x
} }
@ -15,7 +15,7 @@ fn test_fixed_array_can_be_used_in_declaration(){
struct Context { struct Context {
pub mut: pub mut:
vb [8]f32 vb [8]f64
} }
fn test_fixed_array_can_be_assigned_to_a_struct_field(){ fn test_fixed_array_can_be_assigned_to_a_struct_field(){
mut ctx := Context{} mut ctx := Context{}

View File

@ -9,7 +9,7 @@ struct FunkyStruct{ }
fn test_nameof_on_various_types_in_generic() { fn test_nameof_on_various_types_in_generic() {
assert simple(42) == "int" assert simple(42) == "int"
assert simple(3.14) == "f32" assert simple(3.14) == "f64"
assert simple("FuBar") == "string" assert simple("FuBar") == "string"
assert simple(FunkyStruct{}) == "FunkyStruct" assert simple(FunkyStruct{}) == "FunkyStruct"
assert simple(test_nameof_on_various_types_in_generic) == "fn ()" assert simple(test_nameof_on_various_types_in_generic) == "fn ()"

View File

@ -6,9 +6,9 @@ type Myf64 f64
fn test_type_alias() { fn test_type_alias() {
i := Myint(10) i := Myint(10)
assert i + 100 == 110 assert i + 100 == 110
f := Myf32(1.0) f := Myf64(10.4)
assert f + 3.14 == 4.14 assert f + 0.5 == 10.9

View File

@ -2,9 +2,9 @@
fn test_typeof_on_simple_expressions() { fn test_typeof_on_simple_expressions() {
a := 123 a := 123
assert typeof(42) == 'int' assert typeof(42) == 'int'
assert typeof(3.14) == 'f32' assert typeof(3.14) == 'f64'
assert typeof(2+2*10) == 'int' assert typeof(2+2*10) == 'int'
assert typeof(1.0 * 12.2) == 'f32' assert typeof(1.0 * 12.2) == 'f64'
assert typeof(a) == 'int' assert typeof(a) == 'int'
} }
@ -39,7 +39,7 @@ pub fn (ms MySumType) str() string {
fn test_typeof_on_sumtypes(){ fn test_typeof_on_sumtypes(){
a := MySumType(32) a := MySumType(32)
b := MySumType(123.0) b := MySumType(f32(123.0))
c := MySumType(FooBar{x:43}) c := MySumType(FooBar{x:43})
assert typeof(a) == 'int' assert typeof(a) == 'int'
assert typeof(b) == 'f32' assert typeof(b) == 'f32'
@ -84,4 +84,4 @@ fn myfn2() {}
fn test_typeof_on_fn() { fn test_typeof_on_fn() {
assert typeof(myfn) == 'fn (int) int' assert typeof(myfn) == 'fn (int) int'
assert typeof(myfn2) == 'fn ()' assert typeof(myfn2) == 'fn ()'
} }

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) { pub fn (gg &GG) draw_text(x, y int, text string, cfg gx.TextCfg) {
gg.fons.set_font(gg.font_normal) gg.fons.set_font(gg.font_normal)
gg.fons.set_size(cfg.size) gg.fons.set_size(cfg.size)
ascender := 0.0 ascender := f32(0.0)
descender := 0.0 descender := f32(0.0)
lh := 0.0 lh := f32(0.0)
gg.fons.vert_metrics(&ascender, &descender, &lh) gg.fons.vert_metrics(&ascender, &descender, &lh)
color:= C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255) color:= C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
C.fonsSetColor(gg.fons, color) C.fonsSetColor(gg.fons, color)