jsgen: implement array map and filter

pull/5340/head
spaceface777 2020-06-10 22:18:20 +02:00 committed by GitHub
parent 6f886dccca
commit e50706f25c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 5 deletions

View File

@ -1122,9 +1122,41 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
name = g.js_name(it.name)
}
g.expr(it.left)
if it.is_method {
// example: foo.bar.baz()
if it.is_method { // foo.bar.baz()
sym := g.table.get_type_symbol(it.receiver_type)
g.write('.')
if sym.kind == .array && it.name in ['map', 'filter'] {
// Prevent 'it' from getting shadowed inside the match
node := it
g.write(it.name)
g.write('(')
match node.args[0].expr {
ast.AnonFn {
g.gen_fn_decl(it.decl)
g.write(')')
return
}
ast.Ident {
if it.kind == .function {
g.write(g.js_name(it.name))
g.write(')')
return
} else if it.kind == .variable {
v_sym := g.table.get_type_symbol(it.var_info().typ)
if v_sym.kind == .function {
g.write(g.js_name(it.name))
g.write(')')
return
}
}
} else {}
}
g.write('it => ')
g.expr(node.args[0].expr)
g.write(')')
return
}
} else {
if name in builtin_globals {
g.write('builtin.')

View File

@ -1,5 +1,5 @@
// V_COMMIT_HASH 808975f
// V_CURRENT_COMMIT_HASH 564545d
// V_COMMIT_HASH 8a24d7d
// V_CURRENT_COMMIT_HASH 6f886dc
// Generated by the V compiler
"use strict";
@ -33,6 +33,24 @@ const builtin = (function () {
/** @namespace main */
const main = (function () {
/**
* @function
* @param {string} s
* @returns {string}
*/
function map_cb(s) {
return `CB: ${s}`;
}
/**
* @function
* @param {number} n
* @returns {boolean}
*/
function filter_cb(n) {
return n < 4;
}
/**
* @function
* @param {...number} args
@ -79,7 +97,7 @@ const main = (function () {
builtin.print("Back\t=> ");
builtin.println(slice4);
/** @type {number} */
const idx2 = "😀".charCodeAt(0);
const idx2 = slice4.charCodeAt(0);
builtin.print("66\t=> ");
builtin.println(idx2);
/** @type {Map<string, string>} */
@ -113,6 +131,40 @@ const main = (function () {
builtin.println(v);
}
/** @type {string[]} */
const a = arr1.map(it => `VAL: ${it}`);
/** @type {string[]} */
const b = arr1.map(map_cb);
/** @type {string[]} */
const c = arr1.map(it => map_cb(it));
/** @type {string[]} */
const d = arr1.map(function (a) {
const _tmp3 = a;
return `ANON: ${a}`;
});
/** @type {number[]} */
const e = arr1.map(it => 456);
builtin.println(a);
builtin.println(b);
builtin.println(c);
builtin.println(d);
builtin.println(e);
builtin.println("\n\n");
/** @type {number[]} */
const aa = arr2.filter(it => it < 4);
/** @type {number[]} */
const bb = arr2.filter(filter_cb);
/** @type {number[]} */
const cc = arr2.filter(it => filter_cb(it));
/** @type {number[]} */
const dd = arr2.filter(function (a) {
const _tmp4 = a;
return a < 4;
});
builtin.println(aa);
builtin.println(bb);
builtin.println(cc);
builtin.println(dd);
})();
/* module exports */

View File

@ -1,3 +1,6 @@
fn map_cb(s string) string { return 'CB: $s' }
fn filter_cb(n int) bool { return n < 4 }
fn variadic(args ...int) {
println(args)
println(args[0])
@ -70,3 +73,33 @@ println('0 to 8\t=>')
for i, _ in arr2 { println(i) }
println('\n\n4 to 5\t=> ')
for _, v in slice3 { println(v) }
println('\n\n')
// map
a := arr1.map('VAL: $it')
b := arr1.map(map_cb)
c := arr1.map(map_cb(it))
d := arr1.map(fn(a string) string { return 'ANON: $a' })
// I don't know when this would ever be used,
// but it's what the C backend does ¯\_(ツ)_/¯
e := arr1.map(456)
println(a)
println(b)
println(c)
println(d)
println(e)
println('\n\n')
// filter
aa := arr2.filter(it < 4)
bb := arr2.filter(filter_cb)
cc := arr2.filter(filter_cb(it))
dd := arr2.filter(fn(a int) bool { return a < 4 })
println(aa)
println(bb)
println(cc)
println(dd)