mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
tmpl: fix an extra newline in @for; builtin: some i64 fixes
This commit is contained in:
parent
55f4412e5a
commit
c4a434bd7f
@ -1,3 +1,6 @@
|
|||||||
|
Roadmap with big features.
|
||||||
|
For a list of all features and fixes, check out the changelog.
|
||||||
|
|
||||||
## [Version 0.3]
|
## [Version 0.3]
|
||||||
|
|
||||||
- [x] gc option
|
- [x] gc option
|
||||||
@ -39,7 +42,6 @@
|
|||||||
- [ ] `copy()` builtin function (e.g. for easier conversion from `[]Foo` to `[4]Foo`)
|
- [ ] `copy()` builtin function (e.g. for easier conversion from `[]Foo` to `[4]Foo`)
|
||||||
- [x] Lambdas: `a.sort(|a, b| a > b)`
|
- [x] Lambdas: `a.sort(|a, b| a > b)`
|
||||||
- [ ] Custom attributes
|
- [ ] Custom attributes
|
||||||
- [ ] `arr.first() or { }` like `arr[0] or { }`
|
|
||||||
- [ ] Contexts that are passed implicitly (e.g. for custom allocation/memory management)
|
- [ ] Contexts that are passed implicitly (e.g. for custom allocation/memory management)
|
||||||
|
|
||||||
## [Version 0.6]
|
## [Version 0.6]
|
||||||
@ -52,7 +54,8 @@
|
|||||||
- [ ] -usecache on by default
|
- [ ] -usecache on by default
|
||||||
- [ ] -skip-unused on by default
|
- [ ] -skip-unused on by default
|
||||||
- [ ] ORM migrations
|
- [ ] ORM migrations
|
||||||
|
- [ ] Allow `$if` everywhere: top level, inside struct definitions, etc
|
||||||
|
|
||||||
## [Version 1.0]
|
## [Version 1.0]
|
||||||
|
|
||||||
- [ ] Cross compilation of C
|
- [ ] Cross compilation of C
|
||||||
|
@ -155,14 +155,6 @@ pub fn (n i32) str() string {
|
|||||||
return int(n).str_l(12)
|
return int(n).str_l(12)
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the value of the `int` as a `string`.
|
|
||||||
// Example: assert int(-2020).str() == '-2020'
|
|
||||||
/*
|
|
||||||
pub fn int_str(n int) string {
|
|
||||||
return i64(n).str()
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
pub fn (nn int) hex_full() string {
|
pub fn (nn int) hex_full() string {
|
||||||
return u64_to_hex(u64(nn), 8)
|
return u64_to_hex(u64(nn), 8)
|
||||||
}
|
}
|
||||||
@ -173,6 +165,10 @@ pub fn (n int) str() string {
|
|||||||
return n.str_l(12)
|
return n.str_l(12)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn int_str(n int) string {
|
||||||
|
// return i64(n).str()
|
||||||
|
//}
|
||||||
|
|
||||||
// str returns the value of the `u32` as a `string`.
|
// str returns the value of the `u32` as a `string`.
|
||||||
// Example: assert u32(20000).str() == '20000'
|
// Example: assert u32(20000).str() == '20000'
|
||||||
@[direct_array_access; inline]
|
@[direct_array_access; inline]
|
||||||
|
7
vlib/builtin/int_d_new_int.v
Normal file
7
vlib/builtin/int_d_new_int.v
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module builtin
|
||||||
|
|
||||||
|
// str returns the value of the `int` as a `string`.
|
||||||
|
// Example: assert int(-2020).str() == '-2020'
|
||||||
|
pub fn int_str(n int) string {
|
||||||
|
return i64(n).str()
|
||||||
|
}
|
@ -7,7 +7,6 @@ pub type ComptTimeConstValue = EmptyExpr
|
|||||||
| i32
|
| i32
|
||||||
| i64
|
| i64
|
||||||
| i8
|
| i8
|
||||||
| int
|
|
||||||
| rune
|
| rune
|
||||||
| string
|
| string
|
||||||
| u16
|
| u16
|
||||||
@ -62,7 +61,7 @@ pub fn (val ComptTimeConstValue) i32() ?i32 {
|
|||||||
// voidptr tries to return a `ComptTimeConstValue` as `voidptr` type.
|
// voidptr tries to return a `ComptTimeConstValue` as `voidptr` type.
|
||||||
pub fn (val ComptTimeConstValue) voidptr() ?voidptr {
|
pub fn (val ComptTimeConstValue) voidptr() ?voidptr {
|
||||||
match val {
|
match val {
|
||||||
i8, i16, i32, i64, int { return voidptr(i64(val)) }
|
i8, i16, i32, i64 { return voidptr(i64(val)) }
|
||||||
u8, u16, u32, u64 { return voidptr(u64(val)) }
|
u8, u16, u32, u64 { return voidptr(u64(val)) }
|
||||||
rune { return voidptr(u64(val)) }
|
rune { return voidptr(u64(val)) }
|
||||||
voidptr { return val }
|
voidptr { return val }
|
||||||
@ -83,7 +82,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
|
|||||||
i32 {
|
i32 {
|
||||||
return i64(val)
|
return i64(val)
|
||||||
}
|
}
|
||||||
i64, int {
|
i64 {
|
||||||
return i64(val)
|
return i64(val)
|
||||||
}
|
}
|
||||||
u8 {
|
u8 {
|
||||||
@ -176,11 +175,11 @@ pub fn (val ComptTimeConstValue) u64() ?u64 {
|
|||||||
return u64(val)
|
return u64(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int {
|
// int {
|
||||||
if val >= 0 {
|
// if val >= 0 {
|
||||||
return u64(val)
|
// return u64(val)
|
||||||
}
|
//}
|
||||||
}
|
//}
|
||||||
u8 {
|
u8 {
|
||||||
return u64(val)
|
return u64(val)
|
||||||
}
|
}
|
||||||
@ -236,9 +235,9 @@ pub fn (val ComptTimeConstValue) f64() ?f64 {
|
|||||||
i64 {
|
i64 {
|
||||||
return f64(val)
|
return f64(val)
|
||||||
}
|
}
|
||||||
int {
|
// int {
|
||||||
return f64(val)
|
// return f64(val)
|
||||||
}
|
//}
|
||||||
u8 {
|
u8 {
|
||||||
return f64(val)
|
return f64(val)
|
||||||
}
|
}
|
||||||
@ -282,9 +281,9 @@ pub fn (val ComptTimeConstValue) string() ?string {
|
|||||||
i64 {
|
i64 {
|
||||||
return val.str()
|
return val.str()
|
||||||
}
|
}
|
||||||
int {
|
// int {
|
||||||
return val.str()
|
// return val.str()
|
||||||
}
|
//}
|
||||||
u8 {
|
u8 {
|
||||||
return val.str()
|
return val.str()
|
||||||
}
|
}
|
||||||
|
@ -939,6 +939,10 @@ fn (mut g Gen) get_array_contains_method(typ ast.Type) string {
|
|||||||
|
|
||||||
fn (mut g Gen) gen_array_contains_methods() {
|
fn (mut g Gen) gen_array_contains_methods() {
|
||||||
mut done := []ast.Type{}
|
mut done := []ast.Type{}
|
||||||
|
mut got_int_str := false
|
||||||
|
$if new_int ? {
|
||||||
|
println(g.array_contains_types)
|
||||||
|
}
|
||||||
for t in g.array_contains_types {
|
for t in g.array_contains_types {
|
||||||
left_final_sym := g.table.final_sym(t)
|
left_final_sym := g.table.final_sym(t)
|
||||||
if left_final_sym.idx in done || g.table.sym(t).has_method('contains') {
|
if left_final_sym.idx in done || g.table.sym(t).has_method('contains') {
|
||||||
@ -949,6 +953,20 @@ fn (mut g Gen) gen_array_contains_methods() {
|
|||||||
mut left_type_str := g.typ(t)
|
mut left_type_str := g.typ(t)
|
||||||
fn_name := '${left_type_str}_contains'
|
fn_name := '${left_type_str}_contains'
|
||||||
|
|
||||||
|
$if new_int ? {
|
||||||
|
if fn_name == 'Array_i64_contains' {
|
||||||
|
if got_int_str {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
got_int_str = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if t == ast.int_type_idx || t == ast.i64_type_idx {
|
||||||
|
// continue
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
if left_final_sym.kind == .array {
|
if left_final_sym.kind == .array {
|
||||||
elem_type := (left_final_sym.info as ast.Array).elem_type
|
elem_type := (left_final_sym.info as ast.Array).elem_type
|
||||||
mut elem_type_str := g.typ(elem_type)
|
mut elem_type_str := g.typ(elem_type)
|
||||||
|
@ -16,9 +16,23 @@ fn (mut g Gen) gen_str_default(sym ast.TypeSymbol, styp string, str_fn_name stri
|
|||||||
}
|
}
|
||||||
mut convertor := ''
|
mut convertor := ''
|
||||||
mut typename_ := ''
|
mut typename_ := ''
|
||||||
|
mut got_int_str := false
|
||||||
if sym.parent_idx in ast.integer_type_idxs {
|
if sym.parent_idx in ast.integer_type_idxs {
|
||||||
convertor = 'int'
|
convertor = 'int'
|
||||||
typename_ = 'int'
|
typename_ = 'int'
|
||||||
|
$if new_int ? {
|
||||||
|
if str_fn_name == 'i64_str' {
|
||||||
|
if got_int_str {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
got_int_str = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if sym.parent_idx == ast.int_type_idx {
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
}
|
||||||
} else if sym.parent_idx == ast.f32_type_idx {
|
} else if sym.parent_idx == ast.f32_type_idx {
|
||||||
convertor = 'float'
|
convertor = 'float'
|
||||||
typename_ = 'f32'
|
typename_ = 'f32'
|
||||||
|
@ -5992,10 +5992,10 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
|
|||||||
i32 {
|
i32 {
|
||||||
g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
|
g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
|
||||||
}
|
}
|
||||||
int {
|
// int {
|
||||||
// XTODO int64
|
// XTODO int64
|
||||||
g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
|
// g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
|
||||||
}
|
//}
|
||||||
i64 {
|
i64 {
|
||||||
if typ == ast.i64_type {
|
if typ == ast.i64_type {
|
||||||
return false
|
return false
|
||||||
|
@ -302,7 +302,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
|
|||||||
println('>>> compiling comptime template file "${path}" for ${tmp_fn_name}')
|
println('>>> compiling comptime template file "${path}" for ${tmp_fn_name}')
|
||||||
}
|
}
|
||||||
v_code := p.compile_template_file(path, tmp_fn_name)
|
v_code := p.compile_template_file(path, tmp_fn_name)
|
||||||
$if print_vweb_template_expansions ? {
|
$if print_veb_template_expansions ? {
|
||||||
lines := v_code.split('\n')
|
lines := v_code.split('\n')
|
||||||
for i, line in lines {
|
for i, line in lines {
|
||||||
println('${path}:${i + 1}: ${line}')
|
println('${path}:${i + 1}: ${line}')
|
||||||
|
@ -87,7 +87,6 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri
|
|||||||
if rline.ends_with('\\') {
|
if rline.ends_with('\\') {
|
||||||
rline = rline[0..rline.len - 2] + trailing_bs
|
rline = rline[0..rline.len - 2] + trailing_bs
|
||||||
}
|
}
|
||||||
|
|
||||||
return rline
|
return rline
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,6 +251,11 @@ fn vweb_tmpl_${fn_name}() string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if line.contains('@for') {
|
if line.contains('@for') {
|
||||||
|
// Remove an extra unnecessary newline added before in state == .simple
|
||||||
|
// Can break stuff like Markdown
|
||||||
|
if source.len > 1 {
|
||||||
|
source.go_back(1)
|
||||||
|
}
|
||||||
source.writeln(parser.tmpl_str_end)
|
source.writeln(parser.tmpl_str_end)
|
||||||
pos := line.index('@for') or { continue }
|
pos := line.index('@for') or { continue }
|
||||||
source.writeln('for ' + line[pos + 4..] + '{')
|
source.writeln('for ' + line[pos + 4..] + '{')
|
||||||
|
@ -28,12 +28,10 @@ fn test_tmpl() {
|
|||||||
age: 25
|
age: 25
|
||||||
numbers: [1, 2, 3]
|
numbers: [1, 2, 3]
|
||||||
|
|
||||||
|
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
|
|
||||||
|
|
||||||
0 - 0
|
0 - 0
|
||||||
2 - 1
|
2 - 1
|
||||||
4 - 2
|
4 - 2
|
||||||
@ -45,7 +43,6 @@ numbers: [1, 2, 3]
|
|||||||
16 - 8
|
16 - 8
|
||||||
18 - 9
|
18 - 9
|
||||||
|
|
||||||
|
|
||||||
vlang/ui, downloaded 3201 times.
|
vlang/ui, downloaded 3201 times.
|
||||||
vlang/vtl, downloaded 123 times.
|
vlang/vtl, downloaded 123 times.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user