Skip to content

Commit

Permalink
use isize/usize for Go int/uint (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov authored Aug 24, 2024
1 parent 6d48fc2 commit 78070cc
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 33 deletions.
7 changes: 3 additions & 4 deletions array_map.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ fn (mut app App) array_init(c CompositeLit) {
app.gen(len_val)
}
app.gen(']')
app.gen(c.typ.elt.name)
app.gen(go2v_type(c.typ.elt.name))
app.gen('{}')
} else {
// [1,2,3]
app.gen('[')
elt_name := go2v_type(c.typ.elt.name)
for i, elt in c.elts {
if i == 0 && elt_name != '' && elt_name != 'string' && elt_name != 'int'
&& !elt_name.starts_with_capital() {
if i == 0 && elt_name != '' && elt_name != 'string' && !elt_name.starts_with_capital() {
// specify type in the first element
// [u8(1), 2, 3]
app.gen('${elt_name}(')
Expand All @@ -45,7 +44,7 @@ fn (mut app App) array_init(c CompositeLit) {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'int' { app.gen('0') }
'isize', 'usize' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
}
Expand Down
13 changes: 10 additions & 3 deletions assign_stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ fn (mut app App) gen_append(args []Expr) {
}

for i := 1; i < args.len; i++ {
if i == 1 && args[i] is BasicLit {
arg_i := args[i]
if i == 1 {
app.gen('[')
if arg_i is BasicLit {
app.gen('${go2v_type(arg_i.kind)}(')
}
}
app.expr(arg_i)
if i == 1 && arg_i is BasicLit {
app.gen(')')
}
app.expr(args[i])
if i < args.len - 1 {
app.gen(',')
} else if i == args.len - 1 && args[i] is BasicLit {
} else if i == args.len - 1 {
app.gen(']')
}
}
Expand Down
2 changes: 1 addition & 1 deletion expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn (mut app App) key_value_expr(expr KeyValueExpr) {

fn (mut app App) array_type(node ArrayType) {
if node.elt is Ident {
app.gen('[]${node.elt.name}')
app.gen('[]${go2v_type(node.elt.name)}')
} else if node.elt is StarExpr {
app.gen('[]')
app.star_expr(node.elt)
Expand Down
8 changes: 4 additions & 4 deletions tests/append/append.vv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module main

fn main() {
mut s := []int{}
mut s := []isize{}
s << 1
s << [2, 3, 4]
mut a := [4, 5, 6]
mut b := [1, 2, 3]
s << [isize(2), 3, 4]
mut a := [isize(4), 5, 6]
mut b := [isize(1), 2, 3]
b << a
}
8 changes: 4 additions & 4 deletions tests/array_fixed_size/array_fixed_size.vv
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ struct Ok {
}

fn main() {
mut full := [4, 5, 23, 55]!
mut one_missing := [4, 5, 23, 55, 0]!
mut full := [isize(4), 5, 23, 55]!
mut one_missing := [isize(4), 5, 23, 55, 0]!
mut missing := ['John', 'Paul', 'George', 'Ringo', '', '']!
mut missing_empty := [2]int{}
mut abc := [1, 2, 3]!
mut missing_empty := [2]isize{}
mut abc := [isize(1), 2, 3]!
mut bytes := [u8(1), 2, 3]!
}
2 changes: 1 addition & 1 deletion tests/array_type/array_type.vv
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct Ok {

fn main() {
mut a := [u8(1), 2, 3]
mut b := [1, 2, 3]
mut b := [isize(1), 2, 3]
mut c := ['a', 'b', 'c']
mut d := [Ok{}, Ok{}, Ok{}]
mut e := [i32(1), 2, 3]
Expand Down
2 changes: 1 addition & 1 deletion tests/fn/fn.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module main

fn abc(a int, b []string, c rune) string {
fn abc(a isize, b []string, c rune) string {
}
4 changes: 2 additions & 2 deletions tests/fn_embedded_struct/fn_embedded_struct.vv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module main

fn abc(a int, b []string, c Ok) {
fn abc(a isize, b []string, c Ok) {
}

struct Ok {
pub mut:
a int
a isize
b []string
}
2 changes: 1 addition & 1 deletion tests/fn_pub/fn_pub.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module main

pub fn abc(a int, b []string, c rune) string {
pub fn abc(a isize, b []string, c rune) string {
}
2 changes: 1 addition & 1 deletion tests/fn_return_multiple/fn_return_multiple.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module main

fn abc(a int, b []string, c rune) (int, string, []int) {
fn abc(a isize, b []string, c rune) (isize, string, []isize) {
}
4 changes: 2 additions & 2 deletions tests/for_in/for_in.vv
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn main() {
for _ in strings {
println('hello')
}
for i, _ in [2]int{} {
for j, _ in [2]int{} {
for i, _ in [2]isize{} {
for j, _ in [2]isize{} {
println('hello2')
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/len_cap/len_cap.vv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module main

fn main() {
mut array := [2, 3, 5, 7, 11]
mut array := [isize(2), 3, 5, 7, 11]
println(array.len)
println(array.cap)
}
4 changes: 2 additions & 2 deletions tests/method/method.vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module main

struct Abc {
pub mut:
a int
a isize
}

fn (a Abc) ok(d int, b []string, c rune) string {
fn (a Abc) ok(d isize, b []string, c rune) string {
}
4 changes: 2 additions & 2 deletions tests/method_embedded/method_embedded.vv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module main

fn (a Abc) ok(d int, b []string, c rune) string {
fn (a Abc) ok(d isize, b []string, c rune) string {
}

struct Abc {
pub mut:
a int
a isize
}
2 changes: 1 addition & 1 deletion tests/struct/struct.vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module main

struct Struct1 {
pub mut:
a int
a isize
b []string
}
2 changes: 1 addition & 1 deletion tests/struct_simple/struct_simple.vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module main

struct Struct1 {
pub mut:
a int
a isize
b []string
}

Expand Down
7 changes: 5 additions & 2 deletions util.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
module main

fn go2v_type(typ string) string {
match typ {
match typ.to_lower() {
'byte' {
return 'u8'
}
'int' {
return 'isize'
}
'int8' {
return 'i8'
}
Expand All @@ -20,7 +23,7 @@ fn go2v_type(typ string) string {
return 'i64'
}
'uint' {
return 'u32'
return 'usize'
}
'uint8' {
return 'u8'
Expand Down

0 comments on commit 78070cc

Please sign in to comment.