v2 / vlib / v / fmt / tests / expressions_input.vv
102 lines · 93 sloc · 2.96 KB · 2332ecff4811b8c97dfda8e825170e9397962519
Raw
1import v.checker
2import v.ast
3import v.table
4import v.gen
5import v.token
6
7fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) table.Type {
8 for i, expr in node.exprs {
9 ftyp := c.expr(expr)
10 node.expr_types << ftyp
11 typ := c.table.unalias_num_type(ftyp)
12 mut fmt := node.fmts[i]
13 // analyze and validate format specifier
14 if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`,
15 `d`, `u`, `x`, `X`, `o`, `c`, `s`, `p`, `_`] {
16 c.error('unknown format specifier `${fmt:c}`',
17 node.fmt_poss[i])
18 }
19 if node.precisions[i] != 987698 &&
20 !typ.is_float() {
21 c.error('precision specification only valid for float types',
22 node.fmt_poss[i])
23 }
24 if node.pluss[i] && !typ.is_number() {
25 c.error('plus prefix only allowed for numbers', node.fmt_poss[i])
26 }
27 if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() &&
28 fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal()
29 && fmt !in [`d`, `c`, `x`, `X`, `o`,
30 `u`, `x`, `X`, `o`]) || (typ.is_float() && fmt !in [`E`, `F`,
31 `G`, `e`, `f`, `g`]) || (typ.is_pointer() &&
32 fmt !in [`p`, `x`, `X`]) || (typ.is_string() && fmt != `s`)
33 || (typ.idx() in [table.i64_type_idx,
34 table.f64_type_idx
35 ] && fmt == `c`) {
36 c.error('illegal format specifier `${fmt:c}` for type `${c.table.get_type_name(ftyp)}`',
37 node.fmt_poss[i])
38 }
39 node.need_fmts[i] = fmt != c.get_default_fmt(ftyp, typ)
40 }
41
42 return table.string_type
43}
44
45fn get_some_val(a_test, b_test, c_test, d_test, e_test, f_test f64) f64 {
46 return a_test*b_test*c_test*
47 d_test+e_test*f_test*a_test*d_test+a_test*
48 b_test*c_test
49}
50
51fn main() {
52 a, b, r, d := 5.3, 7.5, 4.4, 6.6
53 if a+b+
54 r*d+a+b+r*
55 d > a+b+r*d+
56 a*b+r {
57 println('ok')
58 }
59 v_str := 'v'
60 s := []string{}
61 s << ' `${v_str}`'
62 println(s)
63 println('this is quite a long string' + ' that is followed by an even longer part that should go to another line')
64 if (a == b && b > r) || (d > r) || (a < b) || (b< d && a+b > r) || (a+b+d >= 0 && r < 0) || (a > b && d-r < b) {
65 println('ok')
66 }
67}
68
69fn gen_str_for_multi_return(mut g gen.Gen,
70 info table.MultiReturn, styp, str_fn_name string) {
71for i, _ in info.types {
72 println('\tstrings__Builder_write(&sb, _STR("\'%.*s\\000\'", 2, a.arg${i}));')
73 }
74}
75
76struct Parser {
77 peek_tok token.Token
78 peek_tok2 token.Token
79 peek_tok3 token.Token
80}
81
82fn (mut p Parser) name_expr() {
83 if p.peek_tok.kind ==
84 .lpar || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name &&
85 p.peek_tok3.kind == .gt) {
86 println(p.peek_tok.lit)
87 }
88}
89
90fn set_nr_muls(t table.Type, nr_muls int) table.Type {
91 return int(t) &
92 0xff00ffff | (nr_muls << 16)
93}
94
95// Test what exprs are treated as multiline. The ternary if only functions as a wrapper.
96// When one expr in a branch doesn't fit a single line, the whole if will be unwrapped.
97fn multiline_exprs() {
98 // StructInit with at least one field
99 _ := if true { Foo{} } else { Foo{ val: 123} }
100 // ConcatExpr with a multiline child expr
101 _, _ := if true { 1, Foo{val: 123} } else { 2, Foo{} }
102}
103