1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

main: vrepl interactive shell update

This commit is contained in:
Shivanjan Chakravorty 2019-07-06 15:37:44 +05:30 committed by Alexander Medvednikov
parent 0f8682d2f0
commit f630d3f342

View File

@ -963,6 +963,7 @@ fn run_repl() []string {
println('Use Ctrl-C or `exit` to exit')
println('For now you have to use println() to print values, this will be fixed soon\n')
file := TmpPath + '/vrepl.v'
temp_file := TmpPath + '/vrepl_temp.v'
mut lines := []string
for {
print('>>> ')
@ -982,15 +983,54 @@ fn run_repl() []string {
lines << void_line
source_code := lines.join('\n') + '\n' + line
os.write_file(file, source_code)
mut v := new_v( ['v', '-repl', file])
v.compile()
s := os.exec(TmpPath + '/vrepl')
s := os.exec('v run '+TmpPath+'/vrepl.v')
mut vals := s.split('\n')
if s.contains('panic: ') {
if !s.contains('declared and not used') {
for i:=1; i<vals.len; i++ {
println(vals[i])
}
}
else {
println(s)
}
}
else {
for i:=0; i<vals.len-1; i++ {
println(vals[i])
}
}
}
else {
mut temp_line := line
mut temp_flag := false
if !(line.contains(' ') || line.contains(':') || line.contains('=') || line.contains(',') ){
temp_line = 'println('+line+')'
temp_flag = true
}
temp_source_code := lines.join('\n') + '\n' + temp_line
os.write_file(temp_file, temp_source_code)
s := os.exec('v run '+TmpPath+'/vrepl_temp.v')
if s.contains('panic: ') {
if !s.contains('declared and not used') {
mut vals := s.split('\n')
for i:=1; i<vals.len; i++ {
println(vals[i])
}
}
else {
lines << line
}
}
else {
lines << line
mut vals := s.split('\n')
for i:=0; i<vals.len-1; i++ {
println(vals[i])
}
}
}
}
return lines
}