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

v test v => v test-compiler

This commit is contained in:
Delyan Angelov
2019-12-01 11:50:13 +02:00
committed by Alexander Medvednikov
parent 854309a7d8
commit ec15bfb7d1
29 changed files with 431 additions and 280 deletions

View File

@@ -42,7 +42,7 @@ mut:
fn new_cgen(out_name_c string) &CGen {
path := out_name_c
out := os.create(path) or {
mut out := os.create(path) or {
println('failed to create $path')
return &CGen{}
}

View File

@@ -38,7 +38,7 @@ fn generate_vh(mod string) {
os.mkdir_all(pdir)
// os.mkdir(os.realpath(dir)) or { panic(err) }
}
out := os.create(path) or { panic(err) }
mut out := os.create(path) or { panic(err) }
mod_path := mod.replace("\\", "/")
out.writeln('// $mod_path module header\n')
mod_def := if mod_path.contains('/') { mod_path.all_after('/') } else { mod_path } // "os"

View File

@@ -200,7 +200,7 @@ fn (p mut Parser) gen_fmt() {
return
}
println('generating ${p.file_name}.v')
out := os.create('/var/tmp/fmt/' + p.file_name) or {
mut out := os.create('/var/tmp/fmt/' + p.file_name) or {
verror('failed to create fmt.v')
return
}

View File

@@ -96,7 +96,7 @@ pub fn (g mut Gen) generate_elf_footer() {
// -5 is for "e8 00 00 00 00"
g.write64_at(int(g.main_fn_addr - g.code_start_pos) - 5, g.code_start_pos+1)
// Create the binary
f := os.create(g.out_name) or { panic(err) }
mut f := os.create(g.out_name) or { panic(err) }
os.chmod(g.out_name, 0775)
f.write_bytes(g.buf.data, g.buf.len)
f.close()

View File

@@ -45,7 +45,7 @@ module flag
// ```
// data object storing information about a defined flag
struct Flag {
pub struct Flag {
pub:
name string // name as it appears on command line
abbr byte // shortcut
@@ -55,7 +55,7 @@ pub:
}
//
struct FlagParser {
pub struct FlagParser {
pub mut:
args []string // the arguments to be parsed
flags []Flag // registered flags
@@ -69,7 +69,7 @@ pub mut:
args_description string
}
const (
pub const (
// used for formating usage message
SPACE = ' '
UNDERLINE = '-----------------------------------------------'
@@ -125,14 +125,13 @@ fn (fs mut FlagParser) parse_value(n string, ab byte) ?string {
c := '--$n'
for i, a in fs.args {
if a == c || (a.len == 2 && a[1] == ab) {
if fs.args.len > i+1 && fs.args[i+1].left(2) != '--' {
val := fs.args[i+1]
fs.args.delete(i+1)
fs.args.delete(i)
return val
} else {
panic('Missing argument for \'$n\'')
}
if i+1 > fs.args.len { panic('Missing argument for \'$n\'') }
nextarg := fs.args[i+1]
if nextarg.limit(2) == '--' { panic('Missing argument for \'$n\'') }
val := fs.args[i+1]
fs.args.delete(i+1)
fs.args.delete(i)
return val
} else if a.len > c.len && c == a[..c.len] && a[c.len..c.len+1] == '=' {
val := a[c.len+1..]
fs.args.delete(i)

View File

@@ -247,3 +247,20 @@ fn test_allow_abreviations() {
u := fp.usage()
assert u.contains(' -v') && u.contains(' -o') && u.contains(' -i') && u.contains(' -f')
}
fn test_allow_kebab_options() {
default_value := 'this_is_the_default_value_of_long_option'
long_option_value := 'this_is_a_long_option_value_as_argument'
mut fp := flag.new_flag_parser(['--my-long-flag', 'true', '--my-long-option', long_option_value ])
my_flag := fp.bool('my-long-flag', false, 'flag with long-kebab-name')
my_option := fp.string('my-long-option', default_value, 'string with long-kebab-name')
assert my_flag == true
assert my_option == long_option_value
u := fp.usage()
assert u.contains(' --my-long-flag')
assert u.contains(' --my-long-option')
}

View File

@@ -3,11 +3,12 @@ module log
import os
import time
import term
import filepath
pub enum LogLevel {
fatal
fatal = 1
error
warning
warn
info
debug
}
@@ -16,8 +17,8 @@ fn tag(l LogLevel) string {
return match l {
.fatal { term.red('F') }
.error { term.red('E') }
.warning { term.yellow('W') }
.info { term.white('I') }
.warn { term.yellow('W') }
.info { term.white('I') }
.debug { term.blue('D') }
else { ' ' }
}
@@ -40,17 +41,21 @@ interface Logger {
}
pub struct Log {
mut:
mut:
level LogLevel
output_label string
ofile os.File
output_to_file bool
pub:
output_file_name string
}
pub fn (l mut Log) set_level(level int){
l.level = match level {
FATAL { LogLevel.fatal }
ERROR { LogLevel.error }
WARN { LogLevel.warning }
WARN { LogLevel.warn }
INFO { LogLevel.info }
DEBUG { LogLevel.debug }
else { .debug }
@@ -61,79 +66,74 @@ pub fn (l mut Log) set_output_level(level LogLevel){
l.level = level
}
pub fn (l mut Log) set_output_label(label string) {
pub fn (l mut Log) set_full_logpath(full_log_path string) {
rlog_file := os.realpath( full_log_path )
l.set_output_label( os.filename( rlog_file ) )
l.set_output_path( os.basedir( rlog_file ) )
}
pub fn (l mut Log) set_output_label(label string){
l.output_label = label
}
pub fn (l mut Log) set_output(output string){
l.output_label = output
pub fn (l mut Log) set_output_path(output_file_path string) {
if l.ofile.is_opened() { l.ofile.close() }
l.output_to_file = true
l.output_file_name = filepath.join( os.realpath( output_file_path ) , l.output_label )
mut ofile := os.open_append( l.output_file_name ) or {
panic('error while opening log file ${l.output_file_name} for appending')
}
l.ofile = ofile
}
fn (l Log) log_file(s string, level LogLevel) {
filename := '${l.output_label}.log'.replace(' ', '')
f := os.open_append(filename) or {
panic('error reading file $filename')
}
pub fn (l mut Log) close(){
l.ofile.close()
}
fn (l mut Log) log_file(s string, level LogLevel) {
timestamp := time.now().format_ss()
e := tag(level)
f.writeln('$timestamp [$e] $s')
l.ofile.writeln('$timestamp [$e] $s')
}
fn (l Log) log_cli(s string, level LogLevel) {
fn (l mut Log) log_cli(s string, level LogLevel) {
f := tag(level)
t := time.now()
println('[$f ${t.format_ss()}] $s')
}
pub fn (l Log) fatal(s string){
if l.level == .fatal {
if l.output_to_file {
l.log_file(s, .fatal)
} else {
l.log_cli(s, .fatal)
}
panic('$l.output_label: $s')
}
}
pub fn (l Log) error(s string){
if l.level in [.info, .debug, .warning, .error] {
if l.output_to_file {
l.log_file(s, .error)
} else {
l.log_cli(s, .error)
}
}
}
pub fn (l Log) warn(s string){
if l.level in [.info, .debug, .warning] {
if l.output_to_file {
l.log_file(s, .warning)
} else {
l.log_cli(s, .warning)
}
}
}
pub fn (l Log) info(s string){
if l.level in [.info, .debug] {
if l.output_to_file {
l.log_file(s, .info)
} else {
l.log_cli(s, .info)
}
}
}
pub fn (l Log) debug(s string){
if l.level != .debug {
return
}
fn (l mut Log) send_output(s &string, level LogLevel){
if l.output_to_file {
l.log_file(s, .debug)
l.log_file(s, level)
} else {
l.log_cli(s, .debug)
l.log_cli(s, level)
}
}
pub fn (l mut Log) fatal(s string){
if l.level < .fatal { return }
l.send_output(s, .fatal)
l.ofile.close()
panic('$l.output_label: $s')
}
pub fn (l mut Log) error(s string){
if l.level < .error { return }
l.send_output(s, .error)
}
pub fn (l mut Log) warn(s string){
if l.level < .warn { return }
l.send_output(s, .warn)
}
pub fn (l mut Log) info(s string){
if l.level < .info { return }
l.send_output(s, .info)
}
pub fn (l mut Log) debug(s string){
if l.level < .debug { return }
l.send_output(s, .debug)
}

View File

@@ -33,6 +33,8 @@ pub const (
pub struct File {
cfile voidptr // Using void* instead of FILE*
mut:
opened bool
}
struct FileInfo {
@@ -68,14 +70,17 @@ fn C.getenv(byteptr) &char
fn C.sigaction(int, voidptr, int)
pub fn (f File) is_opened() bool {
return f.opened
}
// read_bytes reads an amount of bytes from the beginning of the file
pub fn (f File) read_bytes(size int) []byte {
pub fn (f mut File) read_bytes(size int) []byte {
return f.read_bytes_at(size, 0)
}
// read_bytes_at reads an amount of bytes at the given position in the file
pub fn (f File) read_bytes_at(size, pos int) []byte {
pub fn (f mut File) read_bytes_at(size, pos int) []byte {
mut arr := [`0`].repeat(size)
C.fseek(f.cfile, pos, C.SEEK_SET)
nreadbytes := C.fread(arr.data, 1, size, f.cfile)
@@ -281,6 +286,7 @@ pub fn open(path string) ?File {
if isnil(file.cfile) {
return error('failed to open file "$path"')
}
file.opened = true
return file
}
@@ -302,6 +308,7 @@ pub fn create(path string) ?File {
if isnil(file.cfile) {
return error('failed to create file "$path"')
}
file.opened = true
return file
}
@@ -322,10 +329,11 @@ pub fn open_append(path string) ?File {
if isnil(file.cfile) {
return error('failed to create(append) file "$path"')
}
file.opened = true
return file
}
pub fn (f File) write(s string) {
pub fn (f mut File) write(s string) {
C.fputs(s.str, f.cfile)
// C.fwrite(s.str, 1, s.len, f.cfile)
}
@@ -333,17 +341,18 @@ pub fn (f File) write(s string) {
// convert any value to []byte (LittleEndian) and write it
// for example if we have write(7, 4), "07 00 00 00" gets written
// write(0x1234, 2) => "34 12"
pub fn (f File) write_bytes(data voidptr, size int) {
pub fn (f mut File) write_bytes(data voidptr, size int) {
C.fwrite(data, 1, size, f.cfile)
}
pub fn (f File) write_bytes_at(data voidptr, size, pos int) {
pub fn (f mut File) write_bytes_at(data voidptr, size, pos int) {
C.fseek(f.cfile, pos, C.SEEK_SET)
C.fwrite(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_END)
}
pub fn (f File) writeln(s string) {
pub fn (f mut File) writeln(s string) {
if !f.opened { return }
// C.fwrite(s.str, 1, s.len, f.cfile)
// ss := s.clone()
// TODO perf
@@ -352,11 +361,15 @@ pub fn (f File) writeln(s string) {
C.fputs('\n', f.cfile)
}
pub fn (f File) flush() {
pub fn (f mut File) flush() {
if !f.opened { return }
C.fflush(f.cfile)
}
pub fn (f File) close() {
pub fn (f mut File) close() {
if !f.opened { return }
f.opened = false
C.fflush(f.cfile)
C.fclose(f.cfile)
}
@@ -707,7 +720,7 @@ pub fn home_dir() string {
// write_file writes `text` data to a file in `path`.
pub fn write_file(path, text string) {
f := os.create(path) or {
mut f := os.create(path) or {
return
}
f.write(text)

View File

@@ -43,7 +43,7 @@ fn test_write_and_read_bytes() {
file_name := './byte_reader_writer.tst'
payload := [`I`, `D`, `D`, `Q`, `D`]
file_write := os.create(os.realpath(file_name)) or {
mut file_write := os.create(os.realpath(file_name)) or {
eprintln('failed to create file $file_name')
return
}
@@ -56,7 +56,7 @@ fn test_write_and_read_bytes() {
assert payload.len == os.file_size(file_name)
file_read := os.open(os.realpath(file_name)) or {
mut file_read := os.open(os.realpath(file_name)) or {
eprintln('failed to open file $file_name')
return
}

View File

@@ -105,7 +105,7 @@ fn (am mut AssetManager) combine(asset_type string, to_file bool) string {
if !os.dir_exists(am.cache_dir) {
os.mkdir(am.cache_dir) or { panic(err) }
}
file := os.create(out_file) or {
mut file := os.create(out_file) or {
panic(err)
}
file.write(out)