tmpl: fix an extra newline in @for; builtin: some i64 fixes

This commit is contained in:
Alexander Medvednikov 2024-07-31 13:30:07 +03:00
parent 55f4412e5a
commit c4a434bd7f
10 changed files with 71 additions and 33 deletions

View File

@ -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

View File

@ -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]

View 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()
}

View File

@ -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()
}

View File

@ -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)

View File

@ -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'

View File

@ -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

View File

@ -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}')

View File

@ -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..] + '{')

View File

@ -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.