From c4a434bd7ffb93bbfc03d90dc9b84290bb45c6d0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 31 Jul 2024 13:30:07 +0300 Subject: [PATCH] tmpl: fix an extra newline in @for; builtin: some i64 fixes --- ROADMAP.md | 7 +++++-- vlib/builtin/int.v | 12 ++++-------- vlib/builtin/int_d_new_int.v | 7 +++++++ vlib/v/ast/comptime_const_values.v | 27 +++++++++++++-------------- vlib/v/gen/c/array.v | 18 ++++++++++++++++++ vlib/v/gen/c/auto_str_methods.v | 14 ++++++++++++++ vlib/v/gen/c/cgen.v | 8 ++++---- vlib/v/parser/comptime.v | 2 +- vlib/v/parser/tmpl.v | 6 +++++- vlib/v/tests/tmpl_test.v | 3 --- 10 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 vlib/builtin/int_d_new_int.v diff --git a/ROADMAP.md b/ROADMAP.md index b3dc164211..8413265021 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,3 +1,6 @@ +Roadmap with big features. +For a list of all features and fixes, check out the changelog. + ## [Version 0.3] - [x] gc option @@ -39,7 +42,6 @@ - [ ] `copy()` builtin function (e.g. for easier conversion from `[]Foo` to `[4]Foo`) - [x] Lambdas: `a.sort(|a, b| a > b)` - [ ] Custom attributes -- [ ] `arr.first() or { }` like `arr[0] or { }` - [ ] Contexts that are passed implicitly (e.g. for custom allocation/memory management) ## [Version 0.6] @@ -52,7 +54,8 @@ - [ ] -usecache on by default - [ ] -skip-unused on by default - [ ] ORM migrations - +- [ ] Allow `$if` everywhere: top level, inside struct definitions, etc + ## [Version 1.0] - [ ] Cross compilation of C diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index bb2d7768fd..c7067a15db 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -155,14 +155,6 @@ pub fn (n i32) str() string { 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 { return u64_to_hex(u64(nn), 8) } @@ -173,6 +165,10 @@ pub fn (n int) str() string { 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`. // Example: assert u32(20000).str() == '20000' @[direct_array_access; inline] diff --git a/vlib/builtin/int_d_new_int.v b/vlib/builtin/int_d_new_int.v new file mode 100644 index 0000000000..7fc3997c4c --- /dev/null +++ b/vlib/builtin/int_d_new_int.v @@ -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() +} diff --git a/vlib/v/ast/comptime_const_values.v b/vlib/v/ast/comptime_const_values.v index acbe8e6568..d8c52a0c4d 100644 --- a/vlib/v/ast/comptime_const_values.v +++ b/vlib/v/ast/comptime_const_values.v @@ -7,7 +7,6 @@ pub type ComptTimeConstValue = EmptyExpr | i32 | i64 | i8 - | int | rune | string | u16 @@ -62,7 +61,7 @@ pub fn (val ComptTimeConstValue) i32() ?i32 { // voidptr tries to return a `ComptTimeConstValue` as `voidptr` type. pub fn (val ComptTimeConstValue) voidptr() ?voidptr { 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)) } rune { return voidptr(u64(val)) } voidptr { return val } @@ -83,7 +82,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 { i32 { return i64(val) } - i64, int { + i64 { return i64(val) } u8 { @@ -176,11 +175,11 @@ pub fn (val ComptTimeConstValue) u64() ?u64 { return u64(val) } } - int { - if val >= 0 { - return u64(val) - } - } + // int { + // if val >= 0 { + // return u64(val) + //} + //} u8 { return u64(val) } @@ -236,9 +235,9 @@ pub fn (val ComptTimeConstValue) f64() ?f64 { i64 { return f64(val) } - int { - return f64(val) - } + // int { + // return f64(val) + //} u8 { return f64(val) } @@ -282,9 +281,9 @@ pub fn (val ComptTimeConstValue) string() ?string { i64 { return val.str() } - int { - return val.str() - } + // int { + // return val.str() + //} u8 { return val.str() } diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 1036f7b415..6009d1a449 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -939,6 +939,10 @@ fn (mut g Gen) get_array_contains_method(typ ast.Type) string { fn (mut g Gen) gen_array_contains_methods() { mut done := []ast.Type{} + mut got_int_str := false + $if new_int ? { + println(g.array_contains_types) + } for t in g.array_contains_types { left_final_sym := g.table.final_sym(t) 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) 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 { elem_type := (left_final_sym.info as ast.Array).elem_type mut elem_type_str := g.typ(elem_type) diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 456d34cdc5..a85680ab36 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -16,9 +16,23 @@ fn (mut g Gen) gen_str_default(sym ast.TypeSymbol, styp string, str_fn_name stri } mut convertor := '' mut typename_ := '' + mut got_int_str := false if sym.parent_idx in ast.integer_type_idxs { convertor = '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 { convertor = 'float' typename_ = 'f32' diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index d8b9fa1c9c..dbfeb40956 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5992,10 +5992,10 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string i32 { g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) } - int { - // XTODO int64 - g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) - } + // int { + // XTODO int64 + // g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) + //} i64 { if typ == ast.i64_type { return false diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index b6cbe87995..f73d1708fe 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -302,7 +302,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { println('>>> compiling comptime template file "${path}" for ${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') for i, line in lines { println('${path}:${i + 1}: ${line}') diff --git a/vlib/v/parser/tmpl.v b/vlib/v/parser/tmpl.v index f3745c906b..9ee2ddd495 100644 --- a/vlib/v/parser/tmpl.v +++ b/vlib/v/parser/tmpl.v @@ -87,7 +87,6 @@ fn insert_template_code(fn_name string, tmpl_str_start string, line string) stri if rline.ends_with('\\') { rline = rline[0..rline.len - 2] + trailing_bs } - return rline } @@ -252,6 +251,11 @@ fn vweb_tmpl_${fn_name}() string { continue } 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) pos := line.index('@for') or { continue } source.writeln('for ' + line[pos + 4..] + '{') diff --git a/vlib/v/tests/tmpl_test.v b/vlib/v/tests/tmpl_test.v index d9d576e42f..930f4fb62f 100644 --- a/vlib/v/tests/tmpl_test.v +++ b/vlib/v/tests/tmpl_test.v @@ -28,12 +28,10 @@ fn test_tmpl() { age: 25 numbers: [1, 2, 3] - 1 2 3 - 0 - 0 2 - 1 4 - 2 @@ -45,7 +43,6 @@ numbers: [1, 2, 3] 16 - 8 18 - 9 - vlang/ui, downloaded 3201 times. vlang/vtl, downloaded 123 times.