Skip to content

Commit 2ffe317

Browse files
authored
vrepl: fix type declaration (#21891)
1 parent 3bc6d30 commit 2ffe317

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

cmd/tools/vrepl.v

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import os
77
import term
88
import rand
99
import readline
10-
import regex
1110
import os.cmdline
1211
import v.util.version
1312

@@ -32,6 +31,7 @@ mut:
3231
structs []string // all the struct definitions
3332
enums []string // all the enum definitions
3433
consts []string // all the const definitions
34+
types []string // all the type definitions
3535
interfaces []string // all the interface definitions
3636
lines []string // all the other lines/statements
3737
temp_lines []string // all the temporary expressions/printlns
@@ -93,6 +93,19 @@ fn endline_if_missed(line string) string {
9393
return line + '\n'
9494
}
9595

96+
fn starts_with_type_decl(line string, type_name string) bool {
97+
if line.starts_with(type_name + ' ') || line.starts_with(type_name + '\t') {
98+
return true
99+
}
100+
if line.starts_with('pub ') || line.starts_with('pub\t') {
101+
substring := line[3..].trim_space()
102+
if substring.starts_with(type_name + ' ') || substring.starts_with(type_name + '\t') {
103+
return true
104+
}
105+
}
106+
return false
107+
}
108+
96109
fn repl_help() {
97110
println(version.full_v_version(false))
98111
println('
@@ -203,6 +216,7 @@ fn (r &Repl) current_source_code(should_add_temp_lines bool, not_add_print bool)
203216
all_lines << lines
204217
}
205218
all_lines << r.includes
219+
all_lines << r.types
206220
all_lines << r.enums
207221
all_lines << r.consts
208222
all_lines << r.structs
@@ -344,13 +358,6 @@ fn run_repl(workdir string, vrepl_prefix string) int {
344358
cleanup_files(temp_file)
345359
}
346360
mut r := new_repl(workdir)
347-
mut pub_fn_or_fn_regex := regex.regex_opt(r'^(pub\s+)?fn\s') or { panic(err) }
348-
mut pub_const_or_const_regex := regex.regex_opt(r'^(pub\s+)?const\s') or { panic(err) }
349-
mut pub_struct_or_struct_regex := regex.regex_opt(r'^(pub\s+)?struct\s') or { panic(err) }
350-
mut pub_enum_or_enum_regex := regex.regex_opt(r'^(pub\s+)?enum\s') or { panic(err) }
351-
mut pub_interface_or_interface_regex := regex.regex_opt(r'^(pub\s+)?interface\s') or {
352-
panic(err)
353-
}
354361

355362
for {
356363
if r.indent == 0 {
@@ -389,37 +396,33 @@ fn run_repl(workdir string, vrepl_prefix string) int {
389396
r.functions_name << r.line.all_before(':= fn(').trim_space()
390397
}
391398

392-
start_fn, _ := pub_fn_or_fn_regex.match_string(r.line)
393-
starts_with_fn := start_fn >= 0
399+
starts_with_fn := starts_with_type_decl(r.line, 'fn')
394400
if starts_with_fn {
395401
r.in_func = true
396402
r.functions_name << r.line.all_after('fn').all_before('(').trim_space()
397403
}
398404
was_func := r.in_func
399405

400-
start_struct, _ := pub_struct_or_struct_regex.match_string(r.line)
401-
starts_with_struct := start_struct >= 0
406+
starts_with_struct := starts_with_type_decl(r.line, 'struct')
402407
if starts_with_struct {
403408
r.in_struct = true
404409
}
405410
was_struct := r.in_struct
406411

407-
start_enum, _ := pub_enum_or_enum_regex.match_string(r.line)
408-
starts_with_enum := start_enum >= 0
412+
starts_with_enum := starts_with_type_decl(r.line, 'enum')
409413
if starts_with_enum {
410414
r.in_enum = true
411415
}
412416
was_enum := r.in_enum
413417

414-
start_interface, _ := pub_interface_or_interface_regex.match_string(r.line)
415-
starts_with_interface := start_interface >= 0
418+
starts_with_interface := starts_with_type_decl(r.line, 'interface')
416419
if starts_with_interface {
417420
r.in_interface = true
418421
}
419422
was_interface := r.in_interface
420423

421-
start_const, _ := pub_const_or_const_regex.match_string(r.line)
422-
starts_with_const := start_const >= 0
424+
starts_with_const := starts_with_type_decl(r.line, 'const')
425+
starts_with_type := starts_with_type_decl(r.line, 'type')
423426

424427
if r.checks() {
425428
for rline in r.line.split('\n') {
@@ -544,7 +547,7 @@ fn run_repl(workdir string, vrepl_prefix string) int {
544547
} else if temp_line.starts_with('#include ') {
545548
temp_source_code = '${temp_line}\n' + r.current_source_code(false, false)
546549
} else if starts_with_fn || starts_with_const || starts_with_enum || starts_with_struct
547-
|| starts_with_interface {
550+
|| starts_with_interface || starts_with_type {
548551
temp_source_code = r.current_source_code(false, false)
549552
} else {
550553
temp_source_code = r.current_source_code(true, false) + '\n${temp_line}\n'
@@ -564,6 +567,8 @@ fn run_repl(workdir string, vrepl_prefix string) int {
564567
r.consts << r.line
565568
} else if starts_with_enum {
566569
r.enums << r.line
570+
} else if starts_with_type {
571+
r.types << r.line
567572
} else if starts_with_struct {
568573
r.structs << r.line
569574
} else if starts_with_interface {

vlib/v/slow_tests/repl/type_decl.repl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a := 1
2+
pub type Int = int
3+
b := Int(a)
4+
b
5+
===output===
6+
1

0 commit comments

Comments
 (0)