1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

'$foo()' string interpolation

This commit is contained in:
Alexander Medvednikov 2019-12-08 22:22:47 +03:00
parent ef821379da
commit 8f9b6ac248
11 changed files with 57 additions and 42 deletions

View File

@ -242,7 +242,7 @@ fn (p mut Parser) mutable_arg_error(i int, arg Var, f Fn) {
dots_example = dots_example + ',..' dots_example = dots_example + ',..'
} }
p.error('`$arg.name` is a mutable argument, you need to provide `mut`: ' + p.error('`$arg.name` is a mutable argument, you need to provide `mut`: ' +
'`$f.name($dots_example)`') '`$f.name ($dots_example)`')
} }
const ( const (

View File

@ -279,7 +279,7 @@ fn (p mut Parser) comptime_method_call(typ Type) {
p.gen(' else ') p.gen(' else ')
} }
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + p.genln('if ( string_eq($var, _STR("$method.name")) ) ' +
'${typ.name}_$method.name($amp $p.expr_var.name);') '${typ.name}_$method.name ($amp $p.expr_var.name);')
j++ j++
} }
p.check(.lpar) p.check(.lpar)

View File

@ -455,7 +455,7 @@ fn (p mut Parser) fn_decl() {
} }
if is_fn_header { if is_fn_header {
p.genln('$typ $fn_name_cgen($str_args);') p.genln('$typ $fn_name_cgen ($str_args);')
p.fgen_nl() p.fgen_nl()
} }
if is_c { if is_c {
@ -495,7 +495,7 @@ fn (p mut Parser) fn_decl() {
fn_name_cgen = '(* $fn_name_cgen )' fn_name_cgen = '(* $fn_name_cgen )'
} }
// Function definition that goes to the top of the C file. // Function definition that goes to the top of the C file.
mut fn_decl := '$dll_export_linkage$typ $fn_name_cgen($str_args)' mut fn_decl := '$dll_export_linkage$typ $fn_name_cgen ($str_args)'
if p.pref.obfuscate { if p.pref.obfuscate {
fn_decl += '; // $f.name' fn_decl += '; // $f.name'
} }
@ -692,7 +692,7 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
if p.os == .windows { if p.os == .windows {
wrapper_type = 'DWORD WINAPI' wrapper_type = 'DWORD WINAPI'
} }
wrapper_text := '$wrapper_type $wrapper_name($arg_struct_name * arg) {$fn_name( /*f*/$str_args ); return 0; }' wrapper_text := '$wrapper_type $wrapper_name ($arg_struct_name * arg) {$fn_name ( /*f*/$str_args ); return 0; }'
p.cgen.register_thread_fn(wrapper_name, wrapper_text, arg_struct) p.cgen.register_thread_fn(wrapper_name, wrapper_text, arg_struct)
// Create thread object // Create thread object
tmp_nr := p.get_tmp_counter() tmp_nr := p.get_tmp_counter()
@ -1024,13 +1024,15 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
p.mutable_arg_error(i, arg, f) p.mutable_arg_error(i, arg, f)
} }
if p.peek() != .name { if p.peek() != .name {
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`') p.error('`$arg.name` is a mutable argument, you need to ' +
'provide a variable to modify: `${f.name}(... mut a...)`')
} }
p.check(.key_mut) p.check(.key_mut)
p.fspace() p.fspace()
var_name := p.lit var_name := p.lit
v := p.find_var(var_name) or { v := p.find_var(var_name) or {
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`') p.error('`$arg.name` is a mutable argument, you need to ' +
'provide a variable to modify: `${f.name}(... mut a...)`')
exit(1) exit(1)
} }
if !v.is_changed { if !v.is_changed {
@ -1156,7 +1158,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
nr = 'third' nr = 'third'
} }
p.error('cannot use type `$typ` as type `$arg.typ` in $nr ' + p.error('cannot use type `$typ` as type `$arg.typ` in $nr ' +
'argument to `$f.name()`') 'argument to `${f.name}()`')
} else { } else {
saved_args << '' saved_args << ''
} }
@ -1575,7 +1577,7 @@ fn (fns []Fn) contains(f Fn) bool {
return false return false
} }
fn (p &Parser) fn_signature(f &Fn) string { fn (p &Parser) fn_signature(f &Fn) string {
return '$f.typ $f.name(${f.str_args(p.table)})' return '$f.typ ${f.name}(${f.str_args(p.table)})'
} }
pub fn (f &Fn) v_fn_module() string { pub fn (f &Fn) v_fn_module() string {

View File

@ -60,7 +60,7 @@ fn (p mut Parser) gen_fn_decl(f Fn, typ, str_args string) {
} }
fn_name_cgen := p.table.fn_gen_name(f) fn_name_cgen := p.table.fn_gen_name(f)
//str_args := f.str_args(p.table) //str_args := f.str_args(p.table)
p.genln('$dll_export_linkage$typ $fn_name_cgen($str_args) {') p.genln('$dll_export_linkage$typ $fn_name_cgen ($str_args) {')
} }
// blank identifer assignment `_ = 111` // blank identifer assignment `_ = 111`
@ -74,7 +74,7 @@ fn (p mut Parser) gen_blank_identifier_assign() {
p.is_var_decl = true p.is_var_decl = true
typ := p.bool_expression() typ := p.bool_expression()
if typ == 'void' { if typ == 'void' {
p.error_with_token_index('$next_expr() $err_used_as_value', p.token_idx-2) p.error_with_token_index('${next_expr}() $err_used_as_value', p.token_idx-2)
} }
p.is_var_decl = false p.is_var_decl = false
if !is_indexer && !is_fn_call { if !is_indexer && !is_fn_call {
@ -417,7 +417,7 @@ fn (p mut Parser) gen_array_init(typ string, no_alloc bool, new_arr_ph int, nr_e
// Need to do this in the second pass, otherwise it goes to the very top of the out.c file // Need to do this in the second pass, otherwise it goes to the very top of the out.c file
if !p.first_pass() { if !p.first_pass() {
p.cgen.set_placeholder(new_arr_ph, p.cgen.set_placeholder(new_arr_ph,
'$new_arr($nr_elems, $nr_elems, sizeof($typ), EMPTY_ARRAY_OF_ELEMS( $typ, $nr_elems ) { ') '${new_arr}($nr_elems, $nr_elems, sizeof($typ), EMPTY_ARRAY_OF_ELEMS( $typ, $nr_elems ) { ')
} }
} }
@ -494,7 +494,7 @@ fn (p mut Parser) gen_struct_init(typ string, t Type) bool {
else { else {
if p.tok == .not { if p.tok == .not {
// old &User{!} ==> 0 hack // old &User{!} ==> 0 hack
p.error('use `$t.name(0)` instead of `&$t.name{!}`') p.error('use `${t.name}(0)` instead of `&$t.name{!}`')
/* /*
p.next() p.next()
p.gen('0') p.gen('0')

View File

@ -57,8 +57,8 @@ fn (p mut Parser) gen_json_for_type(typ Type) {
p.table.register_fn(enc_fn) p.table.register_fn(enc_fn)
// Code gen decoder // Code gen decoder
dec += ' dec += '
//$t $dec_fn.name(cJSON* root) { //$t $dec_fn.name (cJSON* root) {
Option $dec_fn.name(cJSON* root, $t* res) { Option ${dec_fn.name}(cJSON* root, $t* res) {
// $t res; // $t res;
if (!root) { if (!root) {
const char *error_ptr = cJSON_GetErrorPtr(); const char *error_ptr = cJSON_GetErrorPtr();
@ -71,7 +71,7 @@ Option $dec_fn.name(cJSON* root, $t* res) {
' '
// Code gen encoder // Code gen encoder
enc += ' enc += '
cJSON* $enc_fn.name($t val) { cJSON* $enc_fn.name ($t val) {
cJSON *o = cJSON_CreateObject(); cJSON *o = cJSON_CreateObject();
string res = tos2(""); string res = tos2("");
' '
@ -96,24 +96,24 @@ string res = tos2("");
if field.attr == 'raw' { if field.attr == 'raw' {
dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' + dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' +
'js_get(root, "$name")));\n' 'js_get(root, "$name")));\n'
} else { } else {
// Now generate decoders for all field types in this struct // Now generate decoders for all field types in this struct
// need to do it here so that these functions are generated first // need to do it here so that these functions are generated first
p.gen_json_for_type(field_type) p.gen_json_for_type(field_type)
dec_name := js_dec_name(_typ) dec_name := js_dec_name(_typ)
if is_js_prim(_typ) { if is_js_prim(_typ) {
dec += ' res->$field.name = $dec_name(js_get(' + dec += ' res->$field.name = $dec_name (js_get(' +
'root, "$name"))' 'root, "$name"))'
} }
else { else {
dec += ' $dec_name(js_get(root, "$name"), & (res->$field.name))' dec += ' $dec_name (js_get(root, "$name"), & (res->$field.name))'
} }
dec += ';\n' dec += ';\n'
} }
enc += ' cJSON_AddItemToObject(o, "$name",$enc_name(val.$field.name)); \n' enc += ' cJSON_AddItemToObject(o, "$name",$enc_name (val.$field.name)); \n'
} }
// cJSON_delete // cJSON_delete
//p.cgen.fns << '$dec return opt_ok(res); \n}' //p.cgen.fns << '$dec return opt_ok(res); \n}'
@ -136,10 +136,10 @@ fn (p mut Parser) decode_array(array_type string) string {
p.gen_json_for_type(t) p.gen_json_for_type(t)
mut s := '' mut s := ''
if is_js_prim(typ) { if is_js_prim(typ) {
s = '$typ val= $fn_name(jsval); ' s = '$typ val= $fn_name (jsval); '
} }
else { else {
s = ' $typ val; $fn_name(jsval, &val); ' s = ' $typ val; $fn_name (jsval, &val); '
} }
return ' return '
*res = new_array(0, 0, sizeof($typ)); *res = new_array(0, 0, sizeof($typ));
@ -168,7 +168,7 @@ fn (p &Parser) encode_array(array_type string) string {
return ' return '
o = cJSON_CreateArray(); o = cJSON_CreateArray();
for (int i = 0; i < val.len; i++){ for (int i = 0; i < val.len; i++){
cJSON_AddItemToArray(o, $fn_name( (($typ*)val.data)[i] )); cJSON_AddItemToArray(o, $fn_name ( (($typ*)val.data)[i] ));
} }
' '
} }

View File

@ -502,13 +502,15 @@ pub fn (v mut V) generate_main() {
v.gen_main_start(false) v.gen_main_start(false)
if v.pref.is_stats { if v.pref.is_stats {
cgen.genln('BenchedTests bt = main__start_testing();') // QQQ
//cgen.genln('BenchedTests bt = main__start_testing();')
} }
for _, f in v.table.fns { for _, f in v.table.fns {
if f.name.starts_with('main__test_') { if f.name.starts_with('main__test_') {
if v.pref.is_stats { cgen.genln('BenchedTests_testing_step_start(&bt, tos3("$f.name"));') } if v.pref.is_stats {
cgen.genln('$f.name();') cgen.genln('BenchedTests_testing_step_start(&bt, tos3("$f.name"));') }
cgen.genln('$f.name ();')
if v.pref.is_stats { cgen.genln('BenchedTests_testing_step_end(&bt);') } if v.pref.is_stats { cgen.genln('BenchedTests_testing_step_end(&bt);') }
} }
} }

View File

@ -1183,11 +1183,11 @@ fn (p mut Parser) free_var(v Var) {
if !v.is_returned && v.typ != 'FILE*' { //!v.is_c { if !v.is_returned && v.typ != 'FILE*' { //!v.is_c {
prev_line := p.cgen.lines[p.cgen.lines.len-2] prev_line := p.cgen.lines[p.cgen.lines.len-2]
p.cgen.lines[p.cgen.lines.len-2] = p.cgen.lines[p.cgen.lines.len-2] =
'$free_fn($v.name); /* :) close_scope free $v.typ */' + prev_line '$free_fn ($v.name); /* :) close_scope free $v.typ */' + prev_line
} }
} else { } else {
//if p.fileis('mem.v') {println(v.name)} //if p.fileis('mem.v') {println(v.name)}
p.genln('$free_fn($v.name); // close_scope free') p.genln('$free_fn ($v.name); // close_scope free')
} }
} }
@ -1408,7 +1408,7 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
//} //}
if expr_type == 'void' { if expr_type == 'void' {
_, fn_name := p.is_expr_fn_call(p.token_idx-3) _, fn_name := p.is_expr_fn_call(p.token_idx-3)
p.error_with_token_index('$fn_name() $err_used_as_value', p.token_idx-2) p.error_with_token_index('${fn_name}() $err_used_as_value', p.token_idx-2)
} }
// Allow `num = 4` where `num` is an `?int` // Allow `num = 4` where `num` is an `?int`
if p.assigned_type.starts_with('Option_') && if p.assigned_type.starts_with('Option_') &&
@ -1517,7 +1517,7 @@ fn (p mut Parser) var_decl() {
t := p.gen_var_decl(p.var_decl_name, is_static) t := p.gen_var_decl(p.var_decl_name, is_static)
if t == 'void' { if t == 'void' {
_, fn_name := p.is_expr_fn_call(p.token_idx-3) _, fn_name := p.is_expr_fn_call(p.token_idx-3)
p.error_with_token_index('$fn_name() $err_used_as_value', p.token_idx-2) p.error_with_token_index('${fn_name}() $err_used_as_value', p.token_idx-2)
} }
mut var_types := [t] mut var_types := [t]
// multiple returns types // multiple returns types
@ -1698,7 +1698,7 @@ fn (p mut Parser) get_c_func_type(name string) string {
// Do not allow it. // Do not allow it.
if !name.starts_with('gl') && !name.starts_with('glad') { if !name.starts_with('gl') && !name.starts_with('glad') {
p.error('undefined C function `$f.name`\n' + p.error('undefined C function `$f.name`\n' +
'define it with `fn C.$name([args]) [return_type]`') 'define it with `fn C.${name}([args]) [return_type]`')
} }
return 'void*' return 'void*'
} }
@ -2534,7 +2534,7 @@ if (!$tmp) {
tos3("$filename"), tos3("$filename"),
$p.scanner.line_nr, $p.scanner.line_nr,
tos3("$sourceline"), tos3("$sourceline"),
tos3("$p.cur_fn.name()") tos3("${p.cur_fn.name}()")
); );
exit(1); exit(1);
// TODO // TODO
@ -2545,7 +2545,7 @@ if (!$tmp) {
tos3("$filename"), tos3("$filename"),
$p.scanner.line_nr, $p.scanner.line_nr,
tos3("$sourceline"), tos3("$sourceline"),
tos3("$p.cur_fn.name()") tos3("${p.cur_fn.name}()")
); );
} }
@ -2745,7 +2745,7 @@ fn (p mut Parser) js_decode() string {
decl += 'cJSON* $cjson_tmp = json__json_parse($expr);' decl += 'cJSON* $cjson_tmp = json__json_parse($expr);'
p.cgen.insert_before(decl) p.cgen.insert_before(decl)
// p.gen('jsdecode_$typ(json_parse($expr), &$tmp);') // p.gen('jsdecode_$typ(json_parse($expr), &$tmp);')
p.gen('json__jsdecode_$typ($cjson_tmp, &$tmp); cJSON_Delete($cjson_tmp);') p.gen('json__jsdecode_${typ}($cjson_tmp, &$tmp); cJSON_Delete($cjson_tmp);')
opt_type := 'Option_$typ' opt_type := 'Option_$typ'
p.cgen.typedefs << 'typedef Option $opt_type;' p.cgen.typedefs << 'typedef Option $opt_type;'
p.table.register_builtin(opt_type) p.table.register_builtin(opt_type)
@ -2757,7 +2757,7 @@ fn (p mut Parser) js_decode() string {
T := p.table.find_type(typ) T := p.table.find_type(typ)
p.gen_json_for_type(T) p.gen_json_for_type(T)
p.check(.rpar) p.check(.rpar)
p.gen('json__json_print(json__jsencode_$typ($expr))') p.gen('json__json_print(json__jsencode_${typ}($expr))')
return 'string' return 'string'
} }
else { else {

View File

@ -123,7 +123,8 @@ fn (p mut Parser) select_query(fn_ph int) string {
if field.typ == 'int' { if field.typ == 'int' {
cast = 'v_string_int' cast = 'v_string_int'
} }
obj_gen.writeln('${qprefix}$tmp . $field.name = $cast( *(string*)array_get(${qprefix}row.vals, $i) );') obj_gen.writeln('${qprefix}${tmp}.$field.name = ' +
'${cast}(*(string*)array_get(${qprefix}row.vals, $i));')
} }
// One object // One object
if query_one { if query_one {

View File

@ -299,7 +299,8 @@ fn (s mut Scanner) scan() ScanRes {
} }
} }
// end of `$expr` // end of `$expr`
if s.inter_start && next_char != `.` { // allow `'$a.b'` and `'$a.c()'`
if s.inter_start && next_char != `.` && next_char != `(` {
s.inter_end = true s.inter_end = true
s.inter_start = false s.inter_start = false
} }
@ -315,6 +316,16 @@ fn (s mut Scanner) scan() ScanRes {
num := s.ident_number() num := s.ident_number()
return scan_res(.number, num) return scan_res(.number, num)
} }
// Handle `'$fn()'`
if c == `)` && s.inter_start {
s.inter_end = true
s.inter_start = false
next_char := if s.pos + 1 < s.text.len { s.text[s.pos + 1] } else { `\0` }
if next_char == s.quote {
s.inside_string = false
}
return scan_res(.rpar, '')
}
// all other tokens // all other tokens
match c { match c {
`+` { `+` {
@ -383,8 +394,7 @@ fn (s mut Scanner) scan() ScanRes {
return scan_res(.rsbr, '') return scan_res(.rsbr, '')
} }
`{` { `{` {
// Skip { in ${ in strings // Skip { in `${` in strings
// }
if s.inside_string { if s.inside_string {
return s.scan() return s.scan()
} }

View File

@ -188,7 +188,7 @@ const (
fn (f Fn) str() string { fn (f Fn) str() string {
t := Table{} t := Table{}
str_args := f.str_args(t) str_args := f.str_args(t)
return '$f.name($str_args) $f.typ' return '${f.name}($str_args) $f.typ'
} }
pub fn (t &Table) debug_fns() string { pub fn (t &Table) debug_fns() string {

View File

@ -676,7 +676,7 @@ fn (g mut Game) generate_tetro() {
g.pos_x = FieldWidth / 2 - TetroSize / 2 g.pos_x = FieldWidth / 2 - TetroSize / 2
g.tetro_idx = g.rand_tetro() g.tetro_idx = g.rand_tetro()
// println('idx=${g.tetro_idx}') // println('idx=${g.tetro_idx}')
g.tetro_stats[g.tetro_idx] += 1 g.tetro_stats[g.tetro_idx]+= 2 -1
g.tetro_total++ g.tetro_total++
g.rotation_idx = 0 g.rotation_idx = 0
g.get_tetro() g.get_tetro()