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

v: split the interpreter to cmd/tools/vinterpret.v

This commit is contained in:
Delyan Angelov
2021-12-11 23:51:42 +02:00
parent adf353702e
commit 9b7a50b1a2
8 changed files with 111 additions and 88 deletions

View File

@ -20,7 +20,6 @@ pub:
compiled_dir string // contains os.real_path() of the dir of the final file beeing compiled, or the dir itself when doing `v .`
module_path string
mut:
pref &pref.Preferences
checker &checker.Checker
transformer &transformer.Transformer
out_name_c string
@ -31,6 +30,7 @@ mut:
nr_warnings int // accumulated warning count of scanner, parser, checker, and builder
nr_notices int // accumulated notice count of scanner, parser, checker, and builder
pub mut:
pref &pref.Preferences
module_search_paths []string
parsed_files []&ast.File
cached_msvc MsvcResult

View File

@ -5,35 +5,7 @@ import v.pref
import v.util
import v.gen.c
pub fn (mut b Builder) gen_c(v_files []string) string {
b.front_and_middle_stages(v_files) or {
if err.code != 9999 {
verror(err.msg)
}
return ''
}
// TODO: move gen.cgen() to c.gen()
util.timing_start('C GEN')
res := c.gen(b.parsed_files, b.table, b.pref)
util.timing_measure('C GEN')
// println('cgen done')
// println(res)
return res
}
pub fn (mut b Builder) build_c(v_files []string, out_file string) {
b.out_name_c = out_file
b.pref.out_name_c = os.real_path(out_file)
b.info('build_c($out_file)')
output2 := b.gen_c(v_files)
os.write_file(out_file, output2) or { panic(err) }
if b.pref.is_stats {
b.stats_lines = output2.count('\n') + 1
b.stats_bytes = output2.len
}
}
pub fn (mut b Builder) compile_c() {
pub fn compile_c(mut b Builder) {
// cgen.genln('// Generated by V')
// println('compile2()')
if b.pref.is_verbose {
@ -63,3 +35,31 @@ pub fn (mut b Builder) compile_c() {
b.build_c(files, out_name_c)
b.cc()
}
pub fn (mut b Builder) gen_c(v_files []string) string {
b.front_and_middle_stages(v_files) or {
if err.code != 9999 {
verror(err.msg)
}
return ''
}
// TODO: move gen.cgen() to c.gen()
util.timing_start('C GEN')
res := c.gen(b.parsed_files, b.table, b.pref)
util.timing_measure('C GEN')
// println('cgen done')
// println(res)
return res
}
pub fn (mut b Builder) build_c(v_files []string, out_file string) {
b.out_name_c = out_file
b.pref.out_name_c = os.real_path(out_file)
b.info('build_c($out_file)')
output2 := b.gen_c(v_files)
os.write_file(out_file, output2) or { panic(err) }
if b.pref.is_stats {
b.stats_lines = output2.count('\n') + 1
b.stats_bytes = output2.len
}
}

View File

@ -8,20 +8,11 @@ import os
import rand
import v.pref
import v.util
import v.eval
import v.checker
fn (mut b Builder) get_vtmp_filename(base_file_name string, postfix string) string {
vtmp := util.get_vtmp_folder()
mut uniq := ''
if !b.pref.reuse_tmpc {
uniq = '.$rand.u64()'
}
fname := os.file_name(os.real_path(base_file_name)) + '$uniq$postfix'
return os.real_path(os.join_path(vtmp, fname))
}
pub type FnBackend = fn (mut b Builder)
pub fn compile(command string, pref &pref.Preferences) {
pub fn compile(command string, pref &pref.Preferences, backend_cb FnBackend) {
odir := os.dir(pref.out_name)
// When pref.out_name is just the name of an executable, i.e. `./v -o executable main.v`
// without a folder component, just use the current folder instead:
@ -40,12 +31,7 @@ pub fn compile(command string, pref &pref.Preferences) {
// println(pref)
}
mut sw := time.new_stopwatch()
match pref.backend {
.c { b.compile_c() }
.js_node, .js_freestanding, .js_browser { b.compile_js() }
.native { b.compile_native() }
.interpret { b.interpret() }
}
backend_cb(mut b)
mut timers := util.get_timers()
timers.show_remaining()
if pref.is_stats {
@ -80,6 +66,16 @@ pub fn compile(command string, pref &pref.Preferences) {
}
}
fn (mut b Builder) get_vtmp_filename(base_file_name string, postfix string) string {
vtmp := util.get_vtmp_folder()
mut uniq := ''
if !b.pref.reuse_tmpc {
uniq = '.$rand.u64()'
}
fname := os.file_name(os.real_path(base_file_name)) + '$uniq$postfix'
return os.real_path(os.join_path(vtmp, fname))
}
// Temporary, will be done by -autofree
[unsafe]
fn (mut b Builder) myfree() {
@ -182,7 +178,7 @@ fn (mut v Builder) cleanup_run_executable_after_exit(exefile string) {
// 'strings' => 'VROOT/vlib/strings'
// 'installed_mod' => '~/.vmodules/installed_mod'
// 'local_mod' => '/path/to/current/dir/local_mod'
fn (mut v Builder) set_module_lookup_paths() {
pub fn (mut v Builder) set_module_lookup_paths() {
// Module search order:
// 0) V test files are very commonly located right inside the folder of the
// module, which they test. Adding the parent folder of the module folder
@ -349,15 +345,3 @@ pub fn (v &Builder) get_user_files() []string {
}
return user_files
}
pub fn (mut b Builder) interpret() {
mut files := b.get_builtin_files()
files << b.get_user_files()
b.set_module_lookup_paths()
b.front_and_middle_stages(files) or { return }
util.timing_start('INTERPRET')
mut e := eval.new_eval(b.table, b.pref)
e.eval(b.parsed_files)
util.timing_measure('INTERPRET')
}

View File

@ -5,6 +5,21 @@ import v.pref
import v.util
import v.gen.js
pub fn compile_js(mut b Builder) {
mut files := b.get_builtin_files()
files << b.get_user_files()
b.set_module_lookup_paths()
if b.pref.is_verbose {
println('all .v files:')
println(files)
}
mut name := b.pref.out_name
if !name.ends_with('.js') {
name += '.js'
}
b.build_js(files, name)
}
pub fn (mut b Builder) gen_js(v_files []string) string {
b.front_and_middle_stages(v_files) or { return '' }
util.timing_start('JS GEN')
@ -24,21 +39,6 @@ pub fn (mut b Builder) build_js(v_files []string, out_file string) {
}
}
pub fn (mut b Builder) compile_js() {
mut files := b.get_builtin_files()
files << b.get_user_files()
b.set_module_lookup_paths()
if b.pref.is_verbose {
println('all .v files:')
println(files)
}
mut name := b.pref.out_name
if !name.ends_with('.js') {
name += '.js'
}
b.build_js(files, name)
}
fn (mut b Builder) run_js() {
cmd := 'node ' + b.pref.out_name + '.js'
res := os.execute(cmd)

View File

@ -5,6 +5,13 @@ import v.util
import v.gen.native
import os
pub fn compile_native(mut b Builder) {
// v.files << v.v_files_from_dir(os.join_path(v.pref.vlib_path,'builtin','bare'))
files := [b.pref.path]
b.set_module_lookup_paths()
b.build_native(files, b.pref.out_name)
}
pub fn (mut b Builder) build_native(v_files []string, out_file string) {
if b.pref.os == .windows {
eprintln('Warning: v -native is experimental for Windows')
@ -28,10 +35,3 @@ pub fn (mut b Builder) build_native(v_files []string, out_file string) {
b.stats_lines, b.stats_bytes = native.gen(b.parsed_files, b.table, out_file, b.pref)
util.timing_measure('Native GEN')
}
pub fn (mut b Builder) compile_native() {
// v.files << v.v_files_from_dir(os.join_path(v.pref.vlib_path,'builtin','bare'))
files := [b.pref.path]
b.set_module_lookup_paths()
b.build_native(files, b.pref.out_name)
}