Browse Source

Merge remote-tracking branch 'upstream/master'

master
Luna 2 years ago
parent
commit
2cc4ebfa77
  1. 1
      compiler/main.v
  2. 3
      compiler/parser.v
  3. 1
      compiler/tests/fn_test.v
  4. 2
      compiler/tests/repl/.gitattributes
  5. 89
      compiler/tests/repl/repl_test.v
  6. 5
      examples/database/pg/customer.v
  7. 8
      make_msvc.bat
  8. 13
      make_tests.bat
  9. 43
      vlib/builtin/string.v
  10. 75
      vlib/builtin/string_test.v
  11. 3
      vlib/freetype/freetype.v
  12. 2
      vlib/math/stats/stats.v
  13. 24
      vlib/os/os.v
  14. 3
      vlib/time/time.v

1
compiler/main.v

@ -791,7 +791,6 @@ fn new_v(args[]string) *V {
dir: dir
lang_dir: vroot
table: new_table(obfuscate)
out_name: out_name
out_name_c: out_name_c
cgen: new_cgen(out_name_c)
vroot: vroot

3
compiler/parser.v

@ -2856,6 +2856,9 @@ p.gen('($no_star*)memdup(&($no_star) {') //sizeof(Node));
if !t.has_field(field) {
p.error('`$t.name` has no field `$field`')
}
if inited_fields.contains(field) {
p.error('already initialized field `$field` in `$t.name`')
}
f := t.find_field(field)
inited_fields << field
p.gen('.$field = ')

1
compiler/tests/fn_test.v

@ -76,6 +76,7 @@ fn test_mut_array() {
mut nums := [1, 2, 3]
modify_array(mut nums)
//assert nums.len == 4
println(nums)
assert nums[0] == 20
assert nums[1] == 4
assert nums[2] == 6

2
compiler/tests/repl/.gitattributes

@ -0,0 +1,2 @@
*.repl text=auto eol=lf

89
compiler/tests/repl/repl_test.v

@ -1,30 +1,67 @@
import os
fn test_repl() {
fn full_path_to_v() string {
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.PathSeparator + vname
return vexec
}
fn test_the_v_compiler_can_be_invoked() {
vexec := full_path_to_v()
println('vexecutable: $vexec')
/*
args := os.args
vreal := os.realpath('v')
myself := os.realpath( os.executable() )
wd := os.getwd() + os.PathSeparator
println('args are: $args')
println('vreal : $vreal')
println('myself : $myself')
println('wd : $wd')
*/
assert vexec != ''
vcmd := '$vexec --version'
r := os.exec(vcmd) or { panic(err) }
//println('"$vcmd" exit_code: $r.exit_code | output: $r.output')
assert r.exit_code == 0
vcmd_error := '$vexec nonexisting.v'
r_error := os.exec(vcmd_error) or { panic(err) }
//println('"$vcmd_error" exit_code: $r_error.exit_code | output: $r_error.output')
assert r_error.exit_code == 1
assert r_error.output == '`nonexisting.v` does not exist'
}
fn test_the_v_repl() {
test_files := os.walk_ext('.', '.repl')
wd := os.getwd() + '/'
for file in test_files {
content := os.read_file(file) or {
assert false
break
}
input_temporary_filename := 'input_temporary_filename.txt'
input := content.all_before('===output===\n')
output := content.all_after('===output===\n')
os.write_file(input_temporary_filename, input)
defer {
os.rm(input_temporary_filename)
}
r := os.exec('./v < $input_temporary_filename') or {
assert false
break
}
result := r.output.replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace( wd, '' )
assert result == output
if result != output {
println(file)
println('Got : $result')
println('Expected : $output')
}
}
wd := os.getwd() + os.PathSeparator
vexec := full_path_to_v()
for file in test_files {
fcontent := os.read_file(file) or {
assert false
break
}
content := fcontent.replace('\r', '')
input := content.all_before('===output===\n')
output := content.all_after('===output===\n')
input_temporary_filename := 'input_temporary_filename.txt'
os.write_file(input_temporary_filename, input)
defer { os.rm(input_temporary_filename) }
r := os.exec('$vexec < $input_temporary_filename') or {
assert false
break
}
result := r.output.replace('\r','').replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace( wd, '' )
assert result == output
if result != output {
println(file)
println('Got : |$result|')
println('Expected : |$output|')
} else {
println('Repl file $file is OK')
}
}
}

5
examples/database/pg/customer.v

@ -1,3 +1,5 @@
/*
module main
import pg
@ -52,5 +54,6 @@ fn main() {
db.insert(nc)
*/
}
*/

8
make_msvc.bat

@ -39,7 +39,6 @@ del v2.exe
echo Vore has been built.
exit
:nomsvc
echo Cannot find an msvc installation
@ -49,6 +48,11 @@ goto :error
echo Failed to compile - Create an issue at 'https://gitdab.com/vorelang/vorelang/issues'!
goto :error
:error
echo Exiting from error
echo fail
exit /b 1
:done
echo pass

13
make_tests.bat

@ -18,18 +18,19 @@ echo build v using vc
vc.exe -o v.exe compiler
if %ERRORLEVEL% NEQ 0 goto :fail
echo build vc.msvc using vc
vc.exe -os msvc -o v.msvc.exe compiler
if %ERRORLEVEL% NEQ 0 goto :fail
setlocal EnableDelayedExpansion
echo testing v
v test v
if %ERRORLEVEL% NEQ 0 goto :fail
echo testing v.msvc -os msvc
v.msvc.exe -os msvc test v
if %ERRORLEVEL% NEQ 0 goto :fail
echo skipping build vc.msvc using vc
REM vc.exe -os msvc -o v.msvc.exe compiler
REM if %ERRORLEVEL% NEQ 0 goto :fail
echo skipping testing v.msvc -os msvc
REM v.msvc.exe -os msvc test v
REM if %ERRORLEVEL% NEQ 0 goto :fail
goto :done

43
vlib/builtin/string.v

@ -321,7 +321,7 @@ pub fn (s string) left(n int) string {
}
return s.substr(0, n)
}
// 'hello'.right(2) => 'llo'
pub fn (s string) right(n int) string {
if n >= s.len {
return ''
@ -447,6 +447,9 @@ pub fn (s string) count(substr string) int {
if s.len == 0 || substr.len == 0 {
return 0
}
if substr.len > s.len {
return 0
}
mut n := 0
mut i := 0
for {
@ -480,7 +483,7 @@ pub fn (s string) ends_with(p string) bool {
// TODO only works with ASCII
pub fn (s string) to_lower() string {
mut b := malloc(s.len)// TODO + 1 ??
mut b := malloc(s.len + 1)
for i := 0; i < s.len; i++ {
b[i] = C.tolower(s.str[i])
}
@ -488,13 +491,31 @@ pub fn (s string) to_lower() string {
}
pub fn (s string) to_upper() string {
mut b := malloc(s.len)// TODO + 1 ??
mut b := malloc(s.len + 1)
for i := 0; i < s.len; i++ {
b[i] = C.toupper(s.str[i])
}
return tos(b, s.len)
}
pub fn (s string) capitalize() string {
sl := s.to_lower()
cap := sl[0].str().to_upper() + sl.right(1)
return cap
}
pub fn (s string) title() string {
words := s.split(' ')
mut tit := []string
for word in words {
tit << word.capitalize()
}
title := tit.join(' ')
return title
}
// 'hey [man] how you doin'
// find_between('[', ']') == 'man'
pub fn (s string) find_between(start, end string) string {
@ -587,22 +608,24 @@ pub fn (s string) trim(c byte) string {
}
pub fn (s string) trim_left(cutset string) string {
mut start := s.index(cutset)
if start != 0 {
if s.len == 0 || cutset.len == 0 {
return s
}
for start < s.len - 1 && s[start] == cutset[0] {
start++
mut pos := 0
cs_arr := cutset.bytes()
for s[pos] in cs_arr {
pos++
}
return s.right(start)
return s.right(pos)
}
pub fn (s string) trim_right(cutset string) string {
if s.len == 0 {
if s.len == 0 || cutset.len == 0 {
return s
}
mut pos := s.len - 1
for s[pos] == cutset[0] {
cs_arr := cutset.bytes()
for s[pos] in cs_arr {
pos--
}
return s.left(pos+1)

75
vlib/builtin/string_test.v

@ -39,12 +39,12 @@ fn test_lt() {
d := 'b'
e := 'aa'
f := 'ab'
assert a<(b)
assert !(b<c)
assert c<(d)
assert !(d<e)
assert c<(e)
assert e<(f)
assert a < (b)
assert !(b < c)
assert c < (d)
assert !(d < e)
assert c < (e)
assert e < (f)
}
fn test_ge() {
@ -53,11 +53,11 @@ fn test_ge() {
c := 'ab'
d := 'abc'
e := 'aaa'
assert b>=(a)
assert c>=(b)
assert d>=(c)
assert !(c>=d)
assert e>=(a)
assert b >= (a)
assert c >= (b)
assert d >= (c)
assert !(c >= d)
assert e >= (a)
}
fn test_compare_strings() {
@ -91,15 +91,15 @@ fn test_split() {
mut s := 'volt/twitch.v:34'
mut vals := s.split(':')
assert vals.len == 2
assert vals[0]== 'volt/twitch.v'
assert vals[1]== '34'
// /////////
assert vals[0] == 'volt/twitch.v'
assert vals[1] == '34'
// /////////
s = '2018-01-01z13:01:02'
vals = s.split('z')
assert vals.len == 2
assert vals[0]=='2018-01-01'
assert vals[1]== '13:01:02'
// /////////////
assert vals[0] =='2018-01-01'
assert vals[1] == '13:01:02'
// //////////
s = '4627a862c3dec29fb3182a06b8965e0025759e18___1530207969___blue'
vals = s.split('___')
assert vals.len == 3
@ -214,7 +214,6 @@ fn test_runes() {
assert u.substr(1, 2) == 'р'
assert s2.ustring().at(1) == 'r'
assert u.at(1) == 'р'
// ///////
first := u.at(0)
last := u.at(u.len - 1)
assert first.len == 2
@ -236,6 +235,22 @@ fn test_lower() {
assert s.to_lower() == 'hi'
}
fn test_upper() {
mut s := 'a'
assert s.to_upper() == 'A'
assert s.to_upper().len == 1
s = 'hello'
assert s.to_upper() == 'HELLO'
assert s.to_upper().len == 5
s = 'Aloha'
assert s.to_upper() == 'ALOHA'
s = 'have a nice day!'
assert s.to_upper() == 'HAVE A NICE DAY!'
s = 'hi'
assert s.to_upper() == 'HI'
}
fn test_left_right() {
s := 'ALOHA'
assert s.left(3) == 'ALO'
@ -292,6 +307,9 @@ fn test_trim_left() {
assert s.trim_left(' ') == 'module main'
s = ' module main'
assert s.trim_left(' ') == 'module main'
// test cutset
s = 'banana'
assert s.trim_left('ba') == 'nana'
}
fn test_trim_right() {
@ -299,6 +317,9 @@ fn test_trim_right() {
assert s.trim_right(' ') == 'module main'
s = 'module main '
assert s.trim_right(' ') == 'module main'
// test cutset
s = 'banana'
assert s.trim_right('na') == 'b'
}
fn test_all_after() {
@ -355,3 +376,21 @@ fn test_count() {
assert 'aabbaa'.count('aa') == 2
assert 'bbaabb'.count('aa') == 1
}
fn test_capitalize() {
mut s := 'hello'
assert s.capitalize() == 'Hello'
s = 'test'
assert s.capitalize() == 'Test'
s = 'i am ray'
assert s.capitalize() == 'I am ray'
}
fn test_title() {
mut s := 'hello world'
assert s.title() == 'Hello World'
s.to_upper()
assert s.title() == 'Hello World'
s.to_lower()
assert s.title() == 'Hello World'
}

3
vlib/freetype/freetype.v

@ -13,6 +13,9 @@ import (
gl
)
#flag windows -I @VROOT/thirdparty/freetype/include
#flag windows -L @VROOT/thirdparty/freetype/win64
#flag darwin -I/usr/local/include/freetype2
#flag darwin -I/opt/local/include/freetype2
#flag -lfreetype

2
vlib/math/stats/stats.v

@ -2,6 +2,8 @@ module stats
import math
// TODO: Implement all of them with generics
// This module defines the following statistical operations on f64 array
// ---------------------------
// | Summary of Functions |

24
vlib/os/os.v

@ -325,7 +325,7 @@ fn pclose(f *FILE) int {
return C._pclose(f)
}
$else {
return C.pclose(f)
return C.pclose(f) / 256 // WEXITSTATUS()
}
}
@ -349,7 +349,7 @@ pub fn exec(cmd string) ?Result {
res += tos(buf, strlen(buf))
}
res = res.trim_space()
exit_code := pclose(f)/256
exit_code := pclose(f)
//if exit_code != 0 {
//return error(res)
//}
@ -509,19 +509,13 @@ pub fn get_line() string {
// get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string {
$if windows {
max := 512 // MAX_PATH * sizeof(wchar_t)
buf := &u16(malloc(max*2))
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
if h_input == INVALID_HANDLE_VALUE {
panic('get_raw_line() error getting input handle.')
}
mut nr_chars := 0
C.ReadConsole(h_input, buf, max, &nr_chars, 0)
if nr_chars == 0 {
return ''
}
return string_from_wide2(buf, nr_chars)
}
maxlinechars := 256
buf := &u16(malloc(maxlinechars*2))
res := int( C.fgetws(buf, maxlinechars, C.stdin ) )
len := int( C.wcslen(buf) )
if 0 != res { return string_from_wide2( buf, len ) }
return ''
}
$else {
//u64 is used because C.getline needs a size_t as second argument
//Otherwise, it would cause a valgrind warning and may be dangerous

3
vlib/time/time.v

@ -15,7 +15,8 @@ const (
$if !windows {
#include <sys/time.h>
#include <sys/wait.h>
//#include <sys/wait.h>
/// ^^^^ including this makes the windows build fail.
}
struct Time {

Loading…
Cancel
Save