vlib: fix missing `else{}` in match statements

pull/3006/head
Alexander Medvednikov 2019-12-07 17:13:25 +03:00
parent 2fb7fba856
commit ad6adf327e
15 changed files with 88 additions and 85 deletions

View File

@ -402,6 +402,7 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
game.state = .running game.state = .running
} }
} }
else {}
} }
if game.state != .running { if game.state != .running {
@ -434,5 +435,6 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
glfw.KeyDown { glfw.KeyDown {
game.move_tetro() // drop faster when the player presses <down> game.move_tetro() // drop faster when the player presses <down>
} }
else { }
} }
} }

View File

@ -117,6 +117,9 @@ fn test_hamming() {
input1.setbit(i) input1.setbit(i)
input2.setbit(i) input2.setbit(i)
} }
else {
}
} }
} }
assert count == bitfield.hamming(input1, input2) assert count == bitfield.hamming(input1, input2)

View File

@ -120,7 +120,7 @@ pub fn string_from_wide(_wstr &u16) string {
pub fn string_from_wide2(_wstr &u16, len int) string { pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows { $if windows {
num_chars := C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, 0, 0, 0, 0)) num_chars := C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, 0, 0, 0, 0)
mut str_to := malloc(num_chars + 1) mut str_to := malloc(num_chars + 1)
if !isnil(str_to) { if !isnil(str_to) {
C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, str_to, num_chars, 0, 0) C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, str_to, num_chars, 0, 0)

View File

@ -203,14 +203,8 @@ fn (p mut Parser) match_statement(is_expr bool) string {
i++ i++
p.fgen_nl() p.fgen_nl()
} }
p.error('match expression requires `else`')
//if is_expr { //p.returns = false // only get here when no default, so return is not guaranteed
// we get here if no else found, ternary requires "else" branch
p.warn('match expression requires `else`')
//}
p.returns = false // only get here when no default, so return is not guaranteed
return '' return ''
} }

View File

@ -21,7 +21,7 @@ fn test_match_integers() {
2 { 3 } 2 { 3 }
else { 5 } else { 5 }
} == 3 } == 3
assert match 0 { assert match 0 {
1 { 2 } 1 { 2 }
2 { 3 } 2 { 3 }
@ -31,7 +31,7 @@ fn test_match_integers() {
assert match 1 { assert match 1 {
else { 5 } else { 5 }
} == 5 } == 5
a = 0 a = 0
match 2 { match 2 {
0 { a = 1 } 0 { a = 1 }
@ -42,7 +42,7 @@ fn test_match_integers() {
} }
} }
assert a == 3 assert a == 3
a = 0 a = 0
match 1 { match 1 {
0 { a = 1 } 0 { a = 1 }
@ -51,6 +51,7 @@ fn test_match_integers() {
a = a + 2 a = a + 2
a = a + 2 a = a + 2
} }
else {}
} }
assert a == 6 assert a == 6
@ -61,7 +62,7 @@ fn test_match_integers() {
} }
} }
assert a == -2 assert a == -2
} }
fn test_match_enums(){ fn test_match_enums(){
mut b := Color.red mut b := Color.red

View File

@ -5,7 +5,7 @@
module base64 module base64
const ( const (
Index = [int(0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Index = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0,
0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

View File

@ -39,7 +39,7 @@ pub fn (w mut Writer) write(record []string) ?bool {
} }
w.sb.write('"') w.sb.write('"')
for field.len > 0 { for field.len > 0 {
mut i := field.index_any('"\r\n') mut i := field.index_any('"\r\n')
if i < 0 { if i < 0 {
@ -52,12 +52,13 @@ pub fn (w mut Writer) write(record []string) ?bool {
if field.len > 0 { if field.len > 0 {
z := field[0] z := field[0]
match z { match z {
`"` { `"` {
w.sb.write('""') w.sb.write('""')
} }
`\r`, `\n` { `\r`, `\n` {
w.sb.write(le) w.sb.write(le)
} }
else {}
} }
field = field[1..] field = field[1..]
} }

View File

@ -47,7 +47,7 @@ pub fn u_to_upper(s ustring) ustring {
// to_lower return an lowercase string from a string // to_lower return an lowercase string from a string
pub fn to_lower(s string) string { pub fn to_lower(s string) string {
return up_low(s, false) return up_low(s, false)
} }
// u_to_lower return an lowercase string from a ustring // u_to_lower return an lowercase string from a ustring
@ -69,10 +69,10 @@ fn utf8util_char_len(b byte) int {
} }
// //
// if upper_flag == true then make low ==> upper conversion // if upper_flag == true then make low ==> upper conversion
// if upper_flag == false then make upper ==> low conversion // if upper_flag == false then make upper ==> low conversion
// //
// up_low make the dirt job // up_low make the dirt job
fn up_low(s string, upper_flag bool) string { fn up_low(s string, upper_flag bool) string {
mut _index := 0 mut _index := 0
@ -90,14 +90,14 @@ fn up_low(s string, upper_flag bool) string {
} }
} }
else if ch_len > 1 && ch_len < 5{ else if ch_len > 1 && ch_len < 5{
mut lword := int(0) mut lword := 0
for i:=0; i < ch_len ; i++ { for i:=0; i < ch_len ; i++ {
lword = (lword << 8 ) | int( s.str[_index + i] ) lword = (lword << 8 ) | int( s.str[_index + i] )
} }
//C.printf(" #%d (%x) ", _index, lword) //C.printf(" #%d (%x) ", _index, lword)
mut res := int(0) mut res := int(0)
// 2 byte utf-8 // 2 byte utf-8
@ -110,14 +110,14 @@ fn up_low(s string, upper_flag bool) string {
// byte format: 1110xxxx 10xxxxxx 10xxxxxx // byte format: 1110xxxx 10xxxxxx 10xxxxxx
// //
else if ch_len == 3 { else if ch_len == 3 {
res = ( lword & 0x0f0000 ) >> 4 | ( lword & 0x3f00 ) >> 2 | ( lword & 0x3f ) res = ( lword & 0x0f0000 ) >> 4 | ( lword & 0x3f00 ) >> 2 | ( lword & 0x3f )
} }
// 4 byte utf-8 // 4 byte utf-8
// byte format: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // byte format: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// //
else if ch_len == 4 { else if ch_len == 4 {
res = (( lword & 0x07000000 ) >> 6) | (( lword & 0x003f0000 ) >> 4) | res = (( lword & 0x07000000 ) >> 6) | (( lword & 0x003f0000 ) >> 4) |
(( lword & 0x00003F00 ) >> 2 ) | ( lword & 0x0000003f ) (( lword & 0x00003F00 ) >> 2 ) | ( lword & 0x0000003f )
} }
//C.printf("len: %d code: %04x ",ch_len,res) //C.printf("len: %d code: %04x ",ch_len,res)
@ -125,15 +125,15 @@ fn up_low(s string, upper_flag bool) string {
//C.printf(" utf8 index: %d ",ch_index) //C.printf(" utf8 index: %d ",ch_index)
// char not in table, no need of conversion // char not in table, no need of conversion
if ch_index == 0 { if ch_index == 0 {
for i in 0..ch_len { for i in 0..ch_len {
str_res[_index + i] = s.str[_index + i] str_res[_index + i] = s.str[_index + i]
} }
//C.printf("\n") //C.printf("\n")
}else{ }else{
tab_char := u16(unicode_con_table_up_to_low[ch_index]) tab_char := u16(unicode_con_table_up_to_low[ch_index])
//C.printf("tab_char: %04x ",tab_char) //C.printf("tab_char: %04x ",tab_char)
if ch_len == 2 { if ch_len == 2 {
ch0 := byte( (tab_char >> 6) & 0x1f ) | 0xc0 /*110x xxxx*/ ch0 := byte( (tab_char >> 6) & 0x1f ) | 0xc0 /*110x xxxx*/
ch1 := byte( (tab_char >> 0) & 0x3f ) | 0x80 /*10xx xxxx*/ ch1 := byte( (tab_char >> 0) & 0x3f ) | 0x80 /*10xx xxxx*/
@ -141,12 +141,12 @@ fn up_low(s string, upper_flag bool) string {
str_res[ _index + 0 ] = ch0 str_res[ _index + 0 ] = ch0
str_res[ _index + 1 ] = ch1 str_res[ _index + 1 ] = ch1
//**************************************************************** //****************************************************************
// BUG: doesn't compile, workaround use shitf to right of 0 bit // BUG: doesn't compile, workaround use shitf to right of 0 bit
//**************************************************************** //****************************************************************
//str_res[_index + 1 ] = byte( tab_char & 0xbf ) /*1011 1111*/ //str_res[_index + 1 ] = byte( tab_char & 0xbf ) /*1011 1111*/
} }
else if ch_len == 3 { else if ch_len == 3 {
ch0 := byte( (tab_char >> 12) & 0x0f ) | 0xe0 /*1110 xxxx*/ ch0 := byte( (tab_char >> 12) & 0x0f ) | 0xe0 /*1110 xxxx*/
@ -160,24 +160,24 @@ fn up_low(s string, upper_flag bool) string {
} }
// TODO: write if needed // TODO: write if needed
else if ch_len == 4 { else if ch_len == 4 {
// place holder!! // place holder!!
// at the present time simply copy the utf8 char // at the present time simply copy the utf8 char
for i in 0..ch_len { for i in 0..ch_len {
str_res[_index + i] = s.str[_index + i] str_res[_index + i] = s.str[_index + i]
} }
} }
} }
} }
// other cases, just copy the string // other cases, just copy the string
else{ else{
for i in 0..ch_len { for i in 0..ch_len {
str_res[_index + i] = s.str[_index + i] str_res[_index + i] = s.str[_index + i]
} }
} }
old_index = _index old_index = _index
_index += ch_len _index += ch_len
// we are done, exit the loop // we are done, exit the loop
if _index >= s.len { if _index >= s.len {
@ -201,7 +201,7 @@ fn find_char_in_table( in_code u16, upper_flag bool) int {
mut first_index := int(0) // first index of our utf8 char range mut first_index := int(0) // first index of our utf8 char range
mut last_index := int(unicode_con_table_up_to_low.len >> 1) // last+1 index of our utf8 char range mut last_index := int(unicode_con_table_up_to_low.len >> 1) // last+1 index of our utf8 char range
mut index := int(0) mut index := int(0)
mut x := u16(0) mut x := u16(0)
mut offset:=int(0) // up to low mut offset:=int(0) // up to low
@ -215,7 +215,7 @@ fn find_char_in_table( in_code u16, upper_flag bool) int {
for { for {
index = (first_index+last_index) >> 1 index = (first_index+last_index) >> 1
x = unicode_con_table_up_to_low[ (index<<1)+offset ] x = unicode_con_table_up_to_low[ (index<<1)+offset ]
//C.printf("(%d..%d) index:%d base[%04x]==>[%04x]\n",first_index,last_index,index,in_code,x) //C.printf("(%d..%d) index:%d base[%04x]==>[%04x]\n",first_index,last_index,index,in_code,x)
if x == in_code { if x == in_code {
@ -223,7 +223,7 @@ fn find_char_in_table( in_code u16, upper_flag bool) int {
return int( (index<<1) + i_step) return int( (index<<1) + i_step)
} }
else if x>in_code { else if x>in_code {
last_index=index last_index=index
}else { }else {
first_index=index first_index=index
@ -246,7 +246,7 @@ fn find_char_in_table( in_code u16, upper_flag bool) int {
* source: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/nls/rbagslowtoupmaptable.htm?view=embed * source: https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/nls/rbagslowtoupmaptable.htm?view=embed
* term of use: https://www.ibm.com/legal?lnk=flg-tous-usen * term of use: https://www.ibm.com/legal?lnk=flg-tous-usen
* license: not stated, general fair use license applied * license: not stated, general fair use license applied
* *
* regex expresion => replace from html table to V : * regex expresion => replace from html table to V :
* src: ([A-F\d]+)\s+([A-F\d]+)\s+(.*) * src: ([A-F\d]+)\s+([A-F\d]+)\s+(.*)
* dst: 0x$1, 0x$2, // $3 * dst: 0x$1, 0x$2, // $3

View File

@ -382,7 +382,7 @@ pub fn (ctx &GG) draw_line_c(x, y, x2, y2 f32, color gx.Color) {
C.glDeleteBuffers(1, &ctx.vbo) C.glDeleteBuffers(1, &ctx.vbo)
ctx.shader.use() ctx.shader.use()
ctx.shader.set_color('color', color) ctx.shader.set_color('color', color)
vertices := [f32(x), f32(y), f32(x2), f32(y2)] ! vertices := [x, y, x2, y2] !
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW) gl.set_vbo(ctx.vbo, vertices, C.GL_STATIC_DRAW)
gl.vertex_attrib_pointer(0, 2, C.GL_FLOAT, false, 2, 0) gl.vertex_attrib_pointer(0, 2, C.GL_FLOAT, false, 2, 0)
@ -433,7 +433,7 @@ pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) {
gl.enable_vertex_attrib_array(1) gl.enable_vertex_attrib_array(1)
gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6) gl.vertex_attrib_pointer(2, 2, C.GL_FLOAT, false, 8, 6)
gl.enable_vertex_attrib_array(2) gl.enable_vertex_attrib_array(2)
gl.bind_2d_texture(u32(tex_id)) gl.bind_2d_texture(tex_id)
gl.bind_vao(ctx.vao) gl.bind_vao(ctx.vao)
gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0) gl.draw_elements(C.GL_TRIANGLES, 6, C.GL_UNSIGNED_INT, 0)
} }

View File

@ -6,7 +6,7 @@ module glm
import math import math
/* /*
#flag -lmyglm #flag -lmyglm
# f32* myglm_ortho(f32, f32, f32, f32); # f32* myglm_ortho(f32, f32, f32, f32);
# f32* myglm_translate(f32, f32, f32); # f32* myglm_translate(f32, f32, f32);
@ -117,11 +117,11 @@ fn (a Vec3) print() {
x := a.x x := a.x
y := a.y y := a.y
z := a.z z := a.z
C.printf('vec3{%f,%f,%f}\n',x,y,z) C.printf('vec3{%f,%f,%f}\n',x,y,z)
// println('vec3{$x,$y,$z}') // println('vec3{$x,$y,$z}')
} }
/* /*
fn rotate(m Mat4, angle f32, vec Vec3) Mat4 { fn rotate(m Mat4, angle f32, vec Vec3) Mat4 {
// # t_mat4 m; // # t_mat4 m;
// println('rotate done') // println('rotate done')
@ -154,7 +154,7 @@ pub fn translate(m Mat4, v Vec3) Mat4 {
return mat4(out) return mat4(out)
} }
/* /*
fn normalize(vec Vec3) Vec3 { fn normalize(vec Vec3) Vec3 {
# return myglm_normalize(vec); # return myglm_normalize(vec);
return Vec3{} return Vec3{}
@ -165,13 +165,13 @@ pub fn ortho(left, right, bottom, top f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)') //println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1)); // mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16 n := 16
mut res := f32_calloc(n) mut res := f32_calloc(n)
res[0] = 2.0 / f32(right - left) res[0] = 2.0 / (right - left)
res[5] = 2.0 / f32(top - bottom) res[5] = 2.0 / (top - bottom)
res[10] = 1.0 res[10] = 1.0
res[12] = - (right + left) / (right - left) res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom) res[13] = - (top + bottom) / (top - bottom)
res[15] = 1.0 res[15] = 1.0
return mat4(res) return mat4(res)
} }
@ -277,24 +277,24 @@ fn ortho_js(left, right, bottom, top f32) &f32 {
lr := 1.0 / (left - right) lr := 1.0 / (left - right)
bt := 1.0 / (bottom - top) bt := 1.0 / (bottom - top)
nf := 1.0 / 1.0// (mynear -myfar) nf := 1.0 / 1.0// (mynear -myfar)
mut out := (*f32)( malloc (sizeof(f32) * 16)) mut out := (*f32)( malloc (sizeof(f32) * 16))
out[0] = -2.0 * lr out[0] = -2.0 * lr
out[1] = 0 out[1] = 0
out[2] = 0 out[2] = 0
out[3] = 0 out[3] = 0
out[4] = 0 out[4] = 0
out[5] = -2.0 * bt out[5] = -2.0 * bt
out[6] = 0 out[6] = 0
out[7] = 0 out[7] = 0
out[8] = 0 out[8] = 0
out[9] = 0 out[9] = 0
out[10] = 2.0 * nf out[10] = 2.0 * nf
out[11] = 0 out[11] = 0
out[12] = (left + right) * lr out[12] = (left + right) * lr
out[13] = (top + bottom) * bt out[13] = (top + bottom) * bt
out[14] = 1.0 * nf//(far + near) * nf; out[14] = 1.0 * nf//(far + near) * nf;
out[15] = 1 out[15] = 1
return out return out
//f := 0.0 //f := 0.0
//return &f //return &f
} }
@ -307,7 +307,7 @@ fn cross(a, b Vec3) Vec3 {
return Vec3{} return Vec3{}
} }
/* /*
fn perspective(degrees f32, ratio f32, a, b f32) Mat4 { fn perspective(degrees f32, ratio f32, a, b f32) Mat4 {
// println('lang per degrees=$degrees ratio=$ratio a=$a b=$b') // println('lang per degrees=$degrees ratio=$ratio a=$a b=$b')
// # printf("lang pers degrees=%f ratio=%f a=%f b=%f\n", degrees, ratio, a,b); // # printf("lang pers degrees=%f ratio=%f a=%f b=%f\n", degrees, ratio, a,b);

View File

@ -74,7 +74,7 @@ fn jsdecode_f32(root &C.cJSON) f32 {
if isnil(root) { if isnil(root) {
return f32(0) return f32(0)
} }
return f32(root.valuedouble) return root.valuedouble
} }
fn jsdecode_f64(root &C.cJSON) f64 { fn jsdecode_f64(root &C.cJSON) f64 {

View File

@ -12,8 +12,8 @@ const (
mask = 0x7FF mask = 0x7FF
shift = 64 - 11 - 1 shift = 64 - 11 - 1
bias = 1023 bias = 1023
sign_mask = u64(u64(1) << 63) sign_mask = (u64(1) << 63)
frac_mask = u64(u64(u64(1)<<u64(shift)) - u64(1)) frac_mask = ((u64(1)<<u64(shift)) - u64(1))
) )
// inf returns positive infinity if sign >= 0, negative infinity if sign < 0. // inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

View File

@ -140,9 +140,9 @@ pub fn factorial(n f64) f64 {
return max_f64 return max_f64
} }
/* Otherwise return n!. */ // Otherwise return n!.
if n == f64(i64(n)) && n >= 0.0 { if n == f64(i64(n)) && n >= 0.0 {
return f64(factorials[i64(n)]) return factorials[i64(n)]
} }
return gamma(n + 1.0) return gamma(n + 1.0)

View File

@ -277,7 +277,7 @@ pub fn (s Socket) read_line() string {
mut res := '' // The final result, including the ending \n. mut res := '' // The final result, including the ending \n.
for { for {
mut line := '' // The current line. Can be a partial without \n in it. mut line := '' // The current line. Can be a partial without \n in it.
n := int(C.recv(s.sockfd, buf, MAX_READ-1, MSG_PEEK)) n := C.recv(s.sockfd, buf, MAX_READ-1, MSG_PEEK)
if n == -1 { return res } if n == -1 { return res }
if n == 0 { return res } if n == 0 { return res }
buf[n] = `\0` buf[n] = `\0`

View File

@ -103,8 +103,9 @@ fn should_escape(c byte, mode EncodingMode) bool {
// everything, so escape nothing. // everything, so escape nothing.
return false return false
} }
else {}
} }
} } else {}
} }
if mode == .encode_fragment { if mode == .encode_fragment {
@ -118,6 +119,7 @@ fn should_escape(c byte, mode EncodingMode) bool {
`!`, `(`, `)`, `*`{ `!`, `(`, `)`, `*`{
return false return false
} }
else {}
} }
} }
@ -184,7 +186,7 @@ fn unescape(s_ string, mode EncodingMode) ?string {
// That is, you can use escaping in the zone identifier but not // That is, you can use escaping in the zone identifier but not
// to introduce bytes you couldn't just write directly. // to introduce bytes you couldn't just write directly.
// But Windows puts spaces here! Yay. // But Windows puts spaces here! Yay.
v := byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])) v := (unhex(s[i+1])<<byte(4) | unhex(s[i+2]))
if s[i..i+3] != '%25' && v != ` ` && should_escape(v, .encode_host) { if s[i..i+3] != '%25' && v != ` ` && should_escape(v, .encode_host) {
error(error_msg(err_msg_escape, s[i..i+3])) error(error_msg(err_msg_escape, s[i..i+3]))
} }
@ -212,7 +214,7 @@ fn unescape(s_ string, mode EncodingMode) ?string {
x := s[i] x := s[i]
match x { match x {
`%` { `%` {
t.write( byte(unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() ) t.write((unhex(s[i+1])<<byte(4) | unhex(s[i+2])).str() )
i += 2 i += 2
} }
`+` { `+` {