-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
str.v
155 lines (145 loc) · 3.76 KB
/
str.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
module js
import v.ast
/*
fn (mut g JsGen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
is_shared := etype.has_flag(.shared_f)
mut typ := etype
if is_shared {
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
}
mut sym := g.table.sym(typ)
// when type is alias, print the aliased value
if mut sym.info is ast.Alias {
parent_sym := g.table.sym(sym.info.parent_type)
if parent_sym.has_method('str') {
typ = sym.info.parent_type
sym = parent_sym
}
}
sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
is_var_mut := expr.is_auto_deref_var()
if typ.has_flag(.variadic) {
str_fn_name := g.gen_str_for_type(typ)
g.write('${str_fn_name}(')
g.expr(expr)
g.write(')')
} else if typ == ast.string_type {
g.expr(expr)
} else if typ == ast.bool_type {
g.write('new string((')
g.expr(expr)
g.write(').valueOf()? "true" : "false")')
} else if sym.kind == .none_ {
g.write('new string("<none>")')
} else if sym.kind == .enum_ {
if expr !is ast.EnumVal {
str_fn_name := g.gen_str_for_type(typ)
g.write('${str_fn_name}(')
g.expr(expr)
g.write(')')
} else {
g.write('new string("')
g.expr(expr)
g.write('")')
}
} else if sym_has_str_method
|| sym.kind in [.array, .array_fixed, .map, .struct_, .multi_return, .sum_type, .interface_] {
is_ptr := typ.is_ptr()
str_fn_name := g.gen_str_for_type(typ)
g.write('${str_fn_name}(')
if str_method_expects_ptr && !is_ptr {
g.write('new \$ref(')
g.expr(expr)
g.write(')')
} else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
g.expr(expr)
g.gen_deref_ptr(etype)
}
g.expr(expr)
g.write(')')
} else {
str_fn_name := g.gen_str_for_type(typ)
g.write('${str_fn_name}(')
if sym.kind != .function {
g.expr(expr)
if expr.is_auto_deref_var() {
g.write('.val')
}
}
g.write(')')
}
}*/
fn (mut g JsGen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
is_shared := etype.has_flag(.shared_f)
mut typ := etype
if is_shared {
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
}
mut sym := g.table.sym(typ)
// when type is alias, print the aliased value
if mut sym.info is ast.Alias {
parent_sym := g.table.sym(sym.info.parent_type)
if parent_sym.has_method('str') {
typ = sym.info.parent_type
sym = parent_sym
}
}
sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
if typ.has_flag(.variadic) {
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
g.expr(expr)
g.write(')')
} else if typ == ast.string_type {
g.expr(expr)
} else if typ == ast.bool_type {
g.expr(expr)
g.write('.valueOf()? new string("true") : new string("false")')
} else if sym.kind == .none_ {
g.write('new string("<none>")')
} else if sym.kind == .enum_ {
if expr !is ast.EnumVal {
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
g.expr(expr)
g.write(')')
} else {
g.write('new string("')
g.expr(expr)
g.write('")')
}
} else if sym_has_str_method
|| sym.kind in [.array, .array_fixed, .map, .struct_, .multi_return, .sum_type, .interface_] {
is_ptr := typ.is_ptr()
is_var_mut := expr.is_auto_deref_var()
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
if str_method_expects_ptr && !is_ptr {
g.write('new \$ref(')
}
if typ == ast.u32_type && expr is ast.CastExpr {
g.write('new u32(')
}
g.expr(expr)
if typ == ast.u32_type && expr is ast.CastExpr {
g.write(')')
}
if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
g.write('.val')
}
g.write(')')
if str_method_expects_ptr && !is_ptr {
g.write(')')
}
} else {
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
if sym.kind != .function {
g.expr(expr)
}
if expr.is_auto_deref_var() {
g.write('.val')
}
g.write(')')
}
}