Skip to content

Commit

Permalink
tmpl: fix an extra newline in @for; builtin: some i64 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jul 31, 2024
1 parent 55f4412 commit c4a434b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 33 deletions.
7 changes: 5 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
12 changes: 4 additions & 8 deletions vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions vlib/builtin/int_d_new_int.v
Original file line number Diff line number Diff line change
@@ -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()
}
27 changes: 13 additions & 14 deletions vlib/v/ast/comptime_const_values.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub type ComptTimeConstValue = EmptyExpr
| i32
| i64
| i8
| int
| rune
| string
| u16
Expand Down Expand Up @@ -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 }
Expand All @@ -83,7 +82,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
i32 {
return i64(val)
}
i64, int {
i64 {
return i64(val)
}
u8 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/gen/c/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down
6 changes: 5 additions & 1 deletion vlib/v/parser/tmpl.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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..] + '{')
Expand Down
3 changes: 0 additions & 3 deletions vlib/v/tests/tmpl_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ fn test_tmpl() {
age: 25
numbers: [1, 2, 3]
1
2
3
0 - 0
2 - 1
4 - 2
Expand All @@ -45,7 +43,6 @@ numbers: [1, 2, 3]
16 - 8
18 - 9
vlang/ui, downloaded 3201 times.
vlang/vtl, downloaded 123 times.
Expand Down

0 comments on commit c4a434b

Please sign in to comment.