2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-12-08 20:06:00 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module compiler
|
|
|
|
|
|
|
|
fn (p mut Parser) string_expr() {
|
|
|
|
is_raw := p.tok == .name && p.lit == 'r'
|
|
|
|
is_cstr := p.tok == .name && p.lit == 'c'
|
|
|
|
if is_raw || is_cstr {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
str := p.lit
|
|
|
|
// No ${}, just return a simple string
|
|
|
|
if p.peek() != .str_dollar || is_raw {
|
2020-02-09 18:29:05 +03:00
|
|
|
f := if is_raw { cescaped_path(str).replace('"', '\\"') } else { format_str(str) }
|
2019-12-08 20:06:00 +03:00
|
|
|
// `C.puts('hi')` => `puts("hi");`
|
|
|
|
/*
|
|
|
|
Calling a C function sometimes requires a call to a string method
|
|
|
|
C.fun('ssss'.to_wide()) => fun(string_to_wide(tos3("ssss")))
|
|
|
|
*/
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-12-08 20:06:00 +03:00
|
|
|
if (p.calling_c && p.peek() != .dot) || is_cstr || (p.pref.translated && p.mod == 'main') {
|
2020-01-04 02:24:30 +03:00
|
|
|
if p.os == .windows && p.mod == 'ui' {
|
2020-01-04 02:06:01 +03:00
|
|
|
p.gen('L"$f"')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('"$f"')
|
|
|
|
}
|
2019-12-08 20:06:00 +03:00
|
|
|
}
|
|
|
|
else if p.is_sql {
|
|
|
|
p.gen("'$str'")
|
|
|
|
}
|
|
|
|
else if p.is_js {
|
|
|
|
p.gen('tos("$f")')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('tos3("$f")')
|
|
|
|
}
|
|
|
|
p.next()
|
2020-01-11 01:33:35 +03:00
|
|
|
if p.scanner.is_fmt && p.tok == .not {
|
|
|
|
// handle '$age'!
|
|
|
|
// TODO remove this hack, do this automatically
|
|
|
|
p.fgen(' ')
|
|
|
|
p.check(.not)
|
|
|
|
}
|
2019-12-08 20:06:00 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
$if js {
|
|
|
|
p.error('js backend does not support string formatting yet')
|
|
|
|
}
|
|
|
|
p.is_alloc = true // $ interpolation means there's allocation
|
|
|
|
mut args := '"'
|
|
|
|
mut format := '"'
|
2019-12-18 21:56:30 +03:00
|
|
|
mut complex_inter := false // for vfmt
|
2020-03-15 02:46:08 +03:00
|
|
|
for p.tok == .string{
|
2019-12-08 20:06:00 +03:00
|
|
|
// Add the string between %d's
|
|
|
|
p.lit = p.lit.replace('%', '%%')
|
|
|
|
format += format_str(p.lit)
|
2019-12-18 21:56:30 +03:00
|
|
|
p.next() // skip $
|
2019-12-08 20:06:00 +03:00
|
|
|
if p.tok != .str_dollar {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Handle .dollar
|
|
|
|
p.check(.str_dollar)
|
|
|
|
// If there's no string after current token, it means we are in
|
|
|
|
// a complex expression (`${...}`)
|
2020-03-15 02:46:08 +03:00
|
|
|
if p.peek() != .string{
|
2019-12-08 20:06:00 +03:00
|
|
|
p.fgen('{')
|
|
|
|
complex_inter = true
|
|
|
|
}
|
|
|
|
// Get bool expr inside a temp var
|
2019-12-18 21:56:30 +03:00
|
|
|
typ,val_ := p.tmp_expr()
|
2019-12-08 20:06:00 +03:00
|
|
|
val := val_.trim_space()
|
|
|
|
args += ', $val'
|
|
|
|
if typ == 'string' {
|
|
|
|
// args += '.str'
|
|
|
|
// printf("%.*s", a.len, a.str) syntax
|
|
|
|
args += '.len, ${val}.str'
|
|
|
|
}
|
|
|
|
if typ == 'ustring' {
|
|
|
|
args += '.len, ${val}.s.str'
|
|
|
|
}
|
|
|
|
if typ == 'bool' {
|
2019-12-18 21:56:30 +03:00
|
|
|
// args += '.len, ${val}.str'
|
2019-12-08 20:06:00 +03:00
|
|
|
}
|
|
|
|
// Custom format? ${t.hour:02d}
|
|
|
|
custom := p.tok == .colon
|
|
|
|
if custom {
|
|
|
|
mut cformat := ''
|
|
|
|
p.next()
|
|
|
|
if p.tok == .dot {
|
|
|
|
cformat += '.'
|
|
|
|
p.next()
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
if p.tok == .minus {
|
|
|
|
// support for left aligned formatting
|
2019-12-08 20:06:00 +03:00
|
|
|
cformat += '-'
|
|
|
|
p.next()
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
cformat += p.lit // 02
|
2019-12-08 20:06:00 +03:00
|
|
|
p.next()
|
|
|
|
fspec := p.lit // f
|
|
|
|
cformat += fspec
|
|
|
|
if fspec == 's' {
|
2019-12-18 21:56:30 +03:00
|
|
|
// println('custom str F=$cformat | format_specifier: "$fspec" | typ: $typ ')
|
2019-12-08 20:06:00 +03:00
|
|
|
if typ != 'string' {
|
|
|
|
p.error('only V strings can be formatted with a :${cformat} format, but you have given "${val}", which has type ${typ}')
|
|
|
|
}
|
|
|
|
args = args.all_before_last('${val}.len, ${val}.str') + '${val}.str'
|
|
|
|
}
|
|
|
|
format += '%$cformat'
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f := p.typ_to_fmt(typ, 0)
|
|
|
|
if f == '' {
|
2020-03-07 16:13:53 +03:00
|
|
|
has_str_method, styp := p.gen_default_str_method_if_missing( typ )
|
|
|
|
if has_str_method {
|
2019-12-08 20:06:00 +03:00
|
|
|
tmp_var := p.get_tmp()
|
2020-03-07 16:13:53 +03:00
|
|
|
p.cgen.insert_before('string $tmp_var = ${styp}_str(${val});')
|
2019-12-08 20:06:00 +03:00
|
|
|
args = args.all_before_last(val) + '${tmp_var}.len, ${tmp_var}.str'
|
|
|
|
format += '%.*s '
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error('unhandled sprintf format "$typ" ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
format += f
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
// println('interpolation format is: |${format}| args are: |${args}| ')
|
2019-12-08 20:06:00 +03:00
|
|
|
}
|
|
|
|
if complex_inter {
|
|
|
|
p.fgen('}')
|
|
|
|
}
|
2020-03-07 16:13:53 +03:00
|
|
|
|
2019-12-18 21:56:30 +03:00
|
|
|
// p.fgen('\'')
|
2019-12-08 20:06:00 +03:00
|
|
|
// println("hello %d", num) optimization.
|
|
|
|
if p.cgen.nogen {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// println: don't allocate a new string, just print it.
|
|
|
|
$if !windows {
|
|
|
|
cur_line := p.cgen.cur_line.trim_space()
|
|
|
|
if cur_line == 'println (' && p.tok != .plus {
|
|
|
|
p.cgen.resetln(cur_line.replace('println (', 'printf('))
|
|
|
|
p.gen('$format\\n$args')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// '$age'! means the user wants this to be a tmp string (uses global buffer, no allocation,
|
|
|
|
// won't be used again)
|
|
|
|
// TODO remove this hack, do this automatically
|
|
|
|
if p.tok == .not {
|
2020-01-11 01:33:35 +03:00
|
|
|
p.fgen(' ')
|
2019-12-08 20:06:00 +03:00
|
|
|
p.check(.not)
|
|
|
|
p.gen('_STR_TMP($format$args)')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise do len counting + allocation + sprintf
|
|
|
|
p.gen('_STR($format$args)')
|
|
|
|
}
|
|
|
|
}
|