mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
vrepl: fix enum and interface declarations (#21886)
This commit is contained in:
parent
1a65695a5a
commit
e9652f86c5
3 changed files with 73 additions and 10 deletions
|
@ -15,8 +15,10 @@ struct Repl {
|
||||||
mut:
|
mut:
|
||||||
readline readline.Readline
|
readline readline.Readline
|
||||||
indent int // indentation level
|
indent int // indentation level
|
||||||
in_func bool // inside a new custom user function
|
in_func bool // inside function decl
|
||||||
in_struct bool // inside a new custom user struct
|
in_struct bool // inside struct decl
|
||||||
|
in_enum bool // inside enum decl
|
||||||
|
in_interface bool // inside interface decl
|
||||||
line string // the current line entered by the user
|
line string // the current line entered by the user
|
||||||
is_pin bool // does the repl 'pin' entered source code
|
is_pin bool // does the repl 'pin' entered source code
|
||||||
folder string // the folder in which the repl will write its temporary source files
|
folder string // the folder in which the repl will write its temporary source files
|
||||||
|
@ -27,7 +29,9 @@ mut:
|
||||||
includes []string // all the #include statements
|
includes []string // all the #include statements
|
||||||
functions []string // all the user function declarations
|
functions []string // all the user function declarations
|
||||||
functions_name []string // all the user function names
|
functions_name []string // all the user function names
|
||||||
structs []string // all the structs definitions
|
structs []string // all the struct definitions
|
||||||
|
enums []string // all the enum definitions
|
||||||
|
interfaces []string // all the interface definitions
|
||||||
lines []string // all the other lines/statements
|
lines []string // all the other lines/statements
|
||||||
temp_lines []string // all the temporary expressions/printlns
|
temp_lines []string // all the temporary expressions/printlns
|
||||||
vstartup_lines []string // lines in the `VSTARTUP` file
|
vstartup_lines []string // lines in the `VSTARTUP` file
|
||||||
|
@ -132,6 +136,8 @@ fn (mut r Repl) checks() bool {
|
||||||
if r.indent == 0 {
|
if r.indent == 0 {
|
||||||
r.in_func = false
|
r.in_func = false
|
||||||
r.in_struct = false
|
r.in_struct = false
|
||||||
|
r.in_enum = false
|
||||||
|
r.in_interface = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,8 +202,10 @@ fn (r &Repl) current_source_code(should_add_temp_lines bool, not_add_print bool)
|
||||||
all_lines << lines
|
all_lines << lines
|
||||||
}
|
}
|
||||||
all_lines << r.includes
|
all_lines << r.includes
|
||||||
all_lines << r.functions
|
all_lines << r.enums
|
||||||
all_lines << r.structs
|
all_lines << r.structs
|
||||||
|
all_lines << r.interfaces
|
||||||
|
all_lines << r.functions
|
||||||
all_lines << r.lines
|
all_lines << r.lines
|
||||||
|
|
||||||
if should_add_temp_lines {
|
if should_add_temp_lines {
|
||||||
|
@ -336,6 +344,10 @@ fn run_repl(workdir string, vrepl_prefix string) int {
|
||||||
mut r := new_repl(workdir)
|
mut r := new_repl(workdir)
|
||||||
mut pub_fn_or_fn_regex := regex.regex_opt(r'^(pub\s+)?fn\s') or { panic(err) }
|
mut pub_fn_or_fn_regex := regex.regex_opt(r'^(pub\s+)?fn\s') or { panic(err) }
|
||||||
mut pub_struct_or_struct_regex := regex.regex_opt(r'^(pub\s+)?struct\s') or { panic(err) }
|
mut pub_struct_or_struct_regex := regex.regex_opt(r'^(pub\s+)?struct\s') or { panic(err) }
|
||||||
|
mut pub_enum_or_enum_regex := regex.regex_opt(r'^(pub\s+)?enum\s') or { panic(err) }
|
||||||
|
mut pub_interface_or_interface_regex := regex.regex_opt(r'^(pub\s+)?interface\s') or {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if r.indent == 0 {
|
if r.indent == 0 {
|
||||||
|
@ -387,12 +399,28 @@ fn run_repl(workdir string, vrepl_prefix string) int {
|
||||||
}
|
}
|
||||||
was_struct := r.in_struct
|
was_struct := r.in_struct
|
||||||
|
|
||||||
|
start_enum, _ := pub_enum_or_enum_regex.match_string(r.line)
|
||||||
|
if start_enum >= 0 {
|
||||||
|
r.in_enum = true
|
||||||
|
}
|
||||||
|
was_enum := r.in_enum
|
||||||
|
|
||||||
|
start_interface, _ := pub_interface_or_interface_regex.match_string(r.line)
|
||||||
|
if start_interface >= 0 {
|
||||||
|
r.in_interface = true
|
||||||
|
}
|
||||||
|
was_interface := r.in_interface
|
||||||
|
|
||||||
if r.checks() {
|
if r.checks() {
|
||||||
for rline in r.line.split('\n') {
|
for rline in r.line.split('\n') {
|
||||||
if r.in_func || was_func {
|
if r.in_func || was_func {
|
||||||
r.functions << rline
|
r.functions << rline
|
||||||
} else if r.in_struct || was_struct {
|
} else if r.in_struct || was_struct {
|
||||||
r.structs << rline
|
r.structs << rline
|
||||||
|
} else if r.in_enum || was_enum {
|
||||||
|
r.enums << rline
|
||||||
|
} else if r.in_interface || was_interface {
|
||||||
|
r.interfaces << rline
|
||||||
} else {
|
} else {
|
||||||
r.temp_lines << rline
|
r.temp_lines << rline
|
||||||
}
|
}
|
||||||
|
|
18
vlib/v/slow_tests/repl/enum.repl
Normal file
18
vlib/v/slow_tests/repl/enum.repl
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
mut age := 20
|
||||||
|
enum Info {
|
||||||
|
young
|
||||||
|
old
|
||||||
|
}
|
||||||
|
fn get_info(age int) Info {
|
||||||
|
if age < 25 {
|
||||||
|
return .young
|
||||||
|
} else {
|
||||||
|
return .old
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get_info(age)
|
||||||
|
age = 45
|
||||||
|
get_info(age)
|
||||||
|
===output===
|
||||||
|
young
|
||||||
|
old
|
17
vlib/v/slow_tests/repl/interface.repl
Normal file
17
vlib/v/slow_tests/repl/interface.repl
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
name := 'hello'
|
||||||
|
interface Foo {
|
||||||
|
get_name() string
|
||||||
|
}
|
||||||
|
struct Bar {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
fn (bar Bar) get_name() string {
|
||||||
|
return bar.name
|
||||||
|
}
|
||||||
|
fn get_name(foo Foo) string {
|
||||||
|
return foo.get_name()
|
||||||
|
}
|
||||||
|
bar := Bar{name}
|
||||||
|
get_name(bar)
|
||||||
|
===output===
|
||||||
|
hello
|
Loading…
Add table
Add a link
Reference in a new issue