v.gen.js: codegen fixes, add a few builtins, port more array tests (#11049)

pull/11051/head
playX 2021-08-04 12:46:24 +03:00 committed by GitHub
parent 800c0e5092
commit 8743b616a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 444 additions and 67 deletions

View File

@ -181,6 +181,10 @@ pub fn (a array) reverse() array {
return res
}
pub fn (mut a array) reverse_in_place() {
#a.arr.reverse()
}
#array.prototype.$includes = function (elem) { return this.arr.find(function(e) { return vEq(elem,e); }) !== undefined;}
// reduce executes a given reducer function on each element of the array,
@ -193,3 +197,36 @@ pub fn (a array) reduce(iter fn (int, int) int, accum_start int) int {
return accum_
}
pub fn (mut a array) pop() voidptr {
mut res := voidptr(0)
#res = a.arr.pop()
return res
}
pub fn (a array) first() voidptr {
mut res := voidptr(0)
#res = a.arr[0]
return res
}
#array.prototype.toString = function () {
#let res = "["
#for (let i = 0; i < this.arr.length;i++) {
#res += this.arr[i].toString();
#if (i != this.arr.length-1)
#res += ', '
#}
#res += ']'
#return res;
#
#}
pub fn (a array) contains(key voidptr) bool {
#for (let i = 0; i < a.arr.length;i++)
#if (vEq(a.arr[i],key)) return new bool(true);
return false
}

View File

@ -834,6 +834,7 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
.xor_assign, .mod_assign, .or_assign, .and_assign, .right_shift_assign,
.left_shift_assign,
]
val := stmt.right[i]
mut is_mut := false
if left is ast.Ident {
@ -848,6 +849,7 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
}
}
mut styp := g.typ(stmt.left_types[i])
l_sym := g.table.get_type_symbol(stmt.left_types[i])
if !g.inside_loop && styp.len > 0 {
g.doc.gen_typ(styp)
}
@ -874,7 +876,11 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
g.write(')')
} else {
if is_assign {
g.write('.val')
if l_sym.kind == .string {
g.write('.str')
} else {
g.write('.val')
}
g.write(' = ')
g.expr(left)
@ -901,10 +907,10 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
g.write(' & ')
}
.right_shift_assign {
g.write(' << ')
g.write(' >> ')
}
.left_shift_assign {
g.write(' >> ')
g.write(' << ')
}
.or_assign {
g.write(' | ')

View File

@ -3,8 +3,8 @@
2
4
255
1
512
256
2
4
131
4
@ -14,18 +14,18 @@
5
4
4
[1,5,2,3,4]
[1, 5, 2, 3, 4]
5
4
5
[1,5,2,3,4]
[5,2,3,4]
[1, 5, 2, 3, 4]
[5, 2, 3, 4]
4
[5,3,4]
[5, 3, 4]
3
[5,3]
[5, 3]
2
[2.5,3.25,4.5,5.75]
[2.5, 3.25, 4.5, 5.75]
true
true
true
@ -47,7 +47,7 @@ true
0
1
3
[1,3]
[1, 3]
3
2
3
@ -60,16 +60,16 @@ true
0
1
1.1
[1,2,3,4]
[1,5,6,2,3,4]
[1, 2, 3, 4]
[1, 5, 6, 2, 3, 4]
0
1
1
0
1
1.1
[1,2,3,4]
[5,6,1,2,3,4]
[1, 2, 3, 4]
[5, 6, 1, 2, 3, 4]
5
true
1.1
@ -109,7 +109,7 @@ abc
1
4
6
[4,3,2,1]
[4, 3, 2, 1]
true
true
true
@ -118,14 +118,14 @@ true
true
true
0
[0,0,0,0]
[0,7,0,0]
[0, 0, 0, 0]
[0, 7, 0, 0]
0
[2,4,6,8,10,12,14,16,18,20]
[2,4,6,8,10,12,14,16,18,20]
[2,4,6,8,10]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[2, 4, 6, 8, 10]
2
[1,2]
[1, 2]
0
1
-1
@ -157,44 +157,44 @@ true
0
0
0
[2,4,6]
["is","awesome"]
[2,3,4,6,8,9,10]
[4,5,6]
[5,10]
[2,4]
[2,4]
[1,2,3,4,5,6]
["v","is","awesome"]
[0,0,0,0,0,0]
[2, 4, 6]
[is, awesome]
[2, 3, 4, 6, 8, 9, 10]
[4, 5, 6]
[5, 10]
[2, 4]
[2, 4]
[1, 2, 3, 4, 5, 6]
[v, is, awesome]
[0, 0, 0, 0, 0, 0]
0
[10,20,30,40,50,60]
[1,4,9,16,25,36]
["1","2","3","4","5","6"]
[false,true,false,true,false,true]
["V","IS","AWESOME"]
[false,false,true]
[true,true,false]
[7,7,7]
[1,4,9,16,25,36]
[3,4,5,6,7,8]
[3,9,4,6,12,7]
[10, 20, 30, 40, 50, 60]
[1, 4, 9, 16, 25, 36]
[1, 2, 3, 4, 5, 6]
[false, true, false, true, false, true]
[V, IS, AWESOME]
[false, false, true]
[true, true, false]
[7, 7, 7]
[1, 4, 9, 16, 25, 36]
[3, 4, 5, 6, 7, 8]
[3, 9, 4, 6, 12, 7]
[]
[true,true,true,true,true,true]
["1a","2a","3a","4a","5a","6a"]
[2,3,4,5,6,7]
[2,3,8]
["1v","2is","3awesome"]
[1,4,9,16,25,36]
[1,25,100]
[1,2,3,4,5,6]
["v","is","awesome"]
[2,3,4]
[2,3,4]
[3,4,5]
[2,3,4]
["1","2","3"]
["1","2","3"]
[true, true, true, true, true, true]
[1a, 2a, 3a, 4a, 5a, 6a]
[2, 3, 4, 5, 6, 7]
[2, 3, 8]
[1v, 2is, 3awesome]
[1, 4, 9, 16, 25, 36]
[1, 25, 100]
[1, 2, 3, 4, 5, 6]
[v, is, awesome]
[2, 3, 4]
[2, 3, 4]
[3, 4, 5]
[2, 3, 4]
[1, 2, 3]
[1, 2, 3]
true
true
true
@ -223,18 +223,18 @@ true
true
true
true
["1","3","5","hi"]
[-3,7,42,67,108]
["a","b","c","d","e","f"]
[1, 3, 5, hi]
[-3, 7, 42, 67, 108]
[a, b, c, d, e, f]
0
1
79
[0,1,15,27,38,50,79]
[0,1,15,27,38,50,79]
[0, 1, 15, 27, 38, 50, 79]
[0, 1, 15, 27, 38, 50, 79]
3
[14,2,3]
[14, 2, 3]
test b
[true,false,true]
[true, false, true]
1,1
2,2
3,3
@ -244,4 +244,53 @@ test b
3,3
4,4
6
[2,0,2,2]
[2, 0, 2, 2]
[[1, 0, 0], [0, 0, 0]]
[[1, 0, 0], [1, 0, 0]]
[abc]
[0, 0, 0, 0]
[2, 2]
[1, 2, 3, 4]
[4, 3, 2, 1]
[c, b, a]
[[5, 6], [3, 4], [1, 2]]
5,5
4
1
xyz
def
abc
3
1
abc
a
3
4
def
a
11
33
[21, 24, 14, 20]
2
3
4
123
123
[[1, 2, 3]]
[[1, 2, 3]]
[[1, 2, 3]]
[[1, 2, 3]]
true
true
true
true
true
true
true
true
true
true
true
0
[[], [], [], []]
[[], [], [123], []]

View File

@ -10,6 +10,30 @@ const (
c_n = 5
)
struct Coord {
x int
y int
z int
}
struct Person {
name string
nums []int
kv map[string]string
}
// test array add in function with mut argument
fn add_nums(mut arr []int) {
arr << 4
}
const (
grid_size_1 = 2
grid_size_2 = 3
grid_size_3 = 4
cell_value = 123
)
struct User {
age int
name string
@ -20,6 +44,27 @@ mut:
bar []int
}
fn array_in_mut(mut a []int) {
if 1 in a {
a[0] = 2
}
}
fn mut_arr_with_eq_in_fn(mut a []int) {
if a == [1, 2, 3, 4] {
a[0] = 0
}
if [0, 2, 3, 4] == a {
a[1] = 0
}
if !(a != [0, 0, 3, 4]) {
a[2] = 0
}
if !([0, 0, 0, 4] != a) {
a[3] = 0
}
}
fn map_test_helper_1(i int) int {
return i * i
}
@ -848,4 +893,244 @@ fn main() {
arr << 2 * 1
println(arr)
}
{
// todo(playX): what we should do with cap on the JS backend?
/*
// test array with cap
a4 := []int{len: 1, cap: 10}
println(a4.cap)
assert a4.len == 1
assert a4.cap == 10
a5 := []int{len: 1, cap: 10}
assert a5.len == 1
assert a5.cap == 10
*/
}
{
// test multi array index
mut a := [][]int{len: 2, init: []int{len: 3, init: 0}}
a[0][0] = 1
println('$a')
mut b := [[0].repeat(3)].repeat(2)
b[0][0] = 1
println('$b')
}
{
// test plus assign string
mut a := ['']
a[0] += 'abc'
println(a)
}
{
// test mut arr with eq in fn
mut a := [1, 2, 3, 4]
mut_arr_with_eq_in_fn(mut a)
println(a)
}
{
// test array in mut
mut a := [1, 2]
array_in_mut(mut a)
println(a)
}
{
mut nums := [1, 2, 3]
add_nums(mut nums)
println(nums)
}
{
// test reverse in place
mut a := [1, 2, 3, 4]
a.reverse_in_place()
println(a)
mut b := ['a', 'b', 'c']
b.reverse_in_place()
println(b)
mut c := [[1, 2], [3, 4], [5, 6]]
c.reverse_in_place()
println(c)
}
{
// test array int pop
mut a := [1, 2, 3, 4, 5]
assert a.len == 5
x := a.last()
y := a.pop()
println('$x,$y')
assert a.len == 4
z := a.pop()
assert a.len == 3
println(z)
a.pop()
a.pop()
final := a.pop()
println(final)
}
{
// test array string pop
mut a := ['abc', 'def', 'xyz']
assert a.len == 3
println(a.pop())
println(a.pop())
println(a.pop())
assert a.len == 0
}
{
// test array first
a := [3]
println(a.first())
b := [1, 2, 3, 4]
println(b.first())
c := ['abc', 'def']
println(c.first())
// todo(playX): we should implement byte str cmp
s := [Chunk{'a'}]
println(s.first().val)
}
{
// test array last
a := [3]
println(a.last())
b := [1, 2, 3, 4]
println(b.last())
c := ['abc', 'def']
println(c.last())
s := [Chunk{'a'}]
println(s.last().val)
}
{
// test direct array access
mut a := [11, 22, 33, 44]
println(a[0])
println(a[2])
x := a[0]
a[0] = 21
a[1] += 2
a[2] = x + 3
a[3] -= a[1]
println(a)
}
{
// test multidimensional array initialization with consts
mut data := [][][]int{len: grid_size_1, init: [][]int{len: grid_size_2, init: []int{len: grid_size_3, init: cell_value}}}
println(data.len)
println(data[0].len)
println(data[0][0].len)
println(data[0][0][0])
println(data[1][1][1])
}
{
// test multi array prepend
mut a := [][]int{}
a.prepend([1, 2, 3])
println(a)
mut b := [][]int{}
b.prepend([[1, 2, 3]])
println(b)
}
{
// test multi array insert
mut a := [][]int{}
a.insert(0, [1, 2, 3])
println(a)
mut b := [][]int{}
b.insert(0, [[1, 2, 3]])
println(b)
}
{
// test multi array in
a := [[1]]
println([1] in a)
}
{
// test any type array contains
a := [true, false]
println(a.contains(true))
println(true in a)
println(a.contains(false))
println(false in a)
b := [i64(2), 3, 4]
println(b.contains(i64(3)))
println(5 !in b)
c := [[1], [2]]
println(c.contains([1]))
println([2] in c)
println([3] !in c)
}
{
// test struct array of multi type in
ivan := Person{
name: 'ivan'
nums: [1, 2, 3]
kv: map{
'aaa': '111'
}
}
people := [Person{
name: 'ivan'
nums: [1, 2, 3]
kv: map{
'aaa': '111'
}
}, Person{
name: 'bob'
nums: [2]
kv: map{
'bbb': '222'
}
}]
println(ivan in people)
}
{
// test struct array of multi type index
ivan := Person{
name: 'ivan'
nums: [1, 2, 3]
kv: map{
'aaa': '111'
}
}
people := [Person{
name: 'ivan'
nums: [1, 2, 3]
kv: map{
'aaa': '111'
}
}, Person{
name: 'bob'
nums: [2]
kv: map{
'bbb': '222'
}
}]
println(people.index(ivan))
assert people.index(ivan) == 0
}
{
// test array struct contains
/*
todo: does not work
mut coords := []Coord{}
coord_1 := Coord{
x: 1
y: 2
z: -1
}
coords << coord_1
exists := coord_1 in coords
not_exists := coord_1 !in coords
println('`exists`: $exists and `not exists`: $not_exists')
assert exists == true
assert not_exists == false
*/
}
{
// test array of array append
mut x := [][]int{len: 4}
println(x) // OK
x[2] << 123 // RTE
println(x)
}
}