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

cgen: operator overloading

This commit is contained in:
Alexander Medvednikov
2020-04-08 22:12:42 +02:00
parent 3521b7ff89
commit a08e64f37f
6 changed files with 85 additions and 45 deletions

View File

@@ -3,8 +3,10 @@
// that can be found in the LICENSE file.
module util
import os
import v.pref
import (
os
v.pref
)
pub const (
v_version = '0.1.26'
@@ -22,7 +24,7 @@ pub fn full_hash() string {
build_hash := vhash()
current_hash := githash(false)
if build_hash == current_hash {
return build_hash
return build_hash
}
return '${build_hash}.${current_hash}'
}
@@ -41,7 +43,7 @@ pub fn full_v_version() string {
// NB: githash(true) must be called only when v detects that it builds itself.
// For all other programs, githash(false) should be used.
pub fn githash(should_get_from_filesystem bool) string {
for {
for {
// The `for` construct here is used as a goto substitute.
// The code in this function will break out of the `for`
// if it detects an error and can not continue.
@@ -50,7 +52,7 @@ pub fn githash(should_get_from_filesystem bool) string {
vroot := os.dir(vexe)
// .git/HEAD
git_head_file := os.join_path(vroot, '.git', 'HEAD')
if !os.exists( git_head_file ) {
if !os.exists(git_head_file) {
break
}
// 'ref: refs/heads/master' ... the current branch name
@@ -60,11 +62,11 @@ pub fn githash(should_get_from_filesystem bool) string {
gcbranch_rel_path := head_content.replace('ref: ', '').trim_space()
gcbranch_file := os.join_path(vroot, '.git', gcbranch_rel_path)
// .git/refs/heads/master
if !os.exists( gcbranch_file ) {
if !os.exists(gcbranch_file) {
break
}
// get the full commit hash contained in the ref heads file
current_branch_hash := os.read_file( gcbranch_file ) or {
current_branch_hash := os.read_file(gcbranch_file) or {
break
}
desired_hash_length := 7
@@ -81,20 +83,18 @@ pub fn githash(should_get_from_filesystem bool) string {
}
//
fn set_vroot_folder(vroot_path string) {
// Preparation for the compiler module:
// VEXE env variable is needed so that compiler.vexe_path()
// can return it later to whoever needs it:
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
os.setenv('VEXE', os.real_path(os.join_path( vroot_path, vname)), true)
os.setenv('VEXE', os.real_path(os.join_path(vroot_path, vname)), true)
}
pub fn launch_tool(is_verbose bool, tool_name string) {
vexe := pref.vexe_path()
vroot := os.dir(vexe)
set_vroot_folder(vroot)
tool_args := os.args[1..].join(' ')
tool_exe := path_of_executable(os.real_path('$vroot/cmd/tools/$tool_name'))
tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v')
@@ -105,7 +105,6 @@ pub fn launch_tool(is_verbose bool, tool_name string) {
println('launch_tool tool_args : $tool_args')
println('launch_tool tool_command: $tool_command')
}
// TODO Caching should be done on the `vlib/v` level.
mut should_compile := false
if !os.exists(tool_exe) {
@@ -115,7 +114,6 @@ pub fn launch_tool(is_verbose bool, tool_name string) {
// v was recompiled, maybe after v up ...
// rebuild the tool too just in case
should_compile = true
if tool_name == 'vself' || tool_name == 'vup' {
// The purpose of vself/up is to update and recompile v itself.
// After the first 'v self' execution, v will be modified, so
@@ -133,14 +131,15 @@ pub fn launch_tool(is_verbose bool, tool_name string) {
if is_verbose {
println('launch_tool should_compile: $should_compile')
}
if should_compile {
mut compilation_command := '"$vexe" '
compilation_command += '"$tool_source"'
if is_verbose {
println('Compiling $tool_name with: "$compilation_command"')
}
tool_compilation := os.exec(compilation_command) or { panic(err) }
tool_compilation := os.exec(compilation_command) or {
panic(err)
}
if tool_compilation.exit_code != 0 {
mut err := 'Permission denied'
if !tool_compilation.output.contains('Permission denied') {
@@ -153,7 +152,6 @@ pub fn launch_tool(is_verbose bool, tool_name string) {
if is_verbose {
println('launch_tool running tool command: $tool_command ...')
}
exit(os.system(tool_command))
}
@@ -180,13 +178,42 @@ pub fn read_file(file_path string) ?string {
return raw_text
}
[inline]
fn imin(a, b int) int {
return if a < b { a } else { b }
return if a < b {
a
} else {
b
}
}
[inline]
fn imax(a, b int) int {
return if a > b { a } else { b }
return if a > b {
a
} else {
b
}
}
fn replace_op(s string) string {
last_char := s[s.len - 1]
suffix := match last_char {
`+` {
'_plus'
}
`-` {
'_minus'
}
`*` {
'_mult'
}
`/` {
'_div'
}
else {
''
}
}
return s[..s.len - 1] + suffix
}