mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fix hexadecimal constants + freestanding fixes
This commit is contained in:
parent
92f920b2b8
commit
5c217b9e61
@ -7,14 +7,26 @@ const (
|
||||
sample_text_file1 = ""
|
||||
)
|
||||
|
||||
fn fork_test (test_fn fn(), name string) {
|
||||
//print ("checking")
|
||||
// a := "$name"
|
||||
println (name)
|
||||
child := sys_fork()
|
||||
if child == 0 {
|
||||
test_fn()
|
||||
sys_exit(0)
|
||||
}
|
||||
// pid := sys_wait(0)
|
||||
// assert
|
||||
}
|
||||
|
||||
fn check_read_write_pipe() {
|
||||
/*
|
||||
Checks the following system calls:
|
||||
sys_pipe
|
||||
sys_write
|
||||
sys_read
|
||||
sys_close
|
||||
*/
|
||||
// Checks the following system calls:
|
||||
// sys_pipe
|
||||
// sys_write
|
||||
// sys_read
|
||||
// sys_close
|
||||
//
|
||||
println ("checking pipe read/write")
|
||||
fd[0] = -1
|
||||
fd[1] = -1
|
||||
@ -59,12 +71,14 @@ fn check_read_file() {
|
||||
sys_read
|
||||
sys_write
|
||||
sys_close
|
||||
sys_open
|
||||
*/
|
||||
test_file := "sample_text1.txt"
|
||||
sample_text := "Do not change this text.\n"
|
||||
println ("checking read file")
|
||||
fd := sys_open(test_file.str, int(fcntl.o_rdonly), 0)
|
||||
fd, ec := sys_open(test_file.str, .o_rdonly, 0)
|
||||
assert fd > 0
|
||||
assert ec == .enoerror
|
||||
n := sample_text.len
|
||||
c := sys_read(fd, buffer, u64(n*2))
|
||||
assert c == n
|
||||
@ -72,12 +86,37 @@ fn check_read_file() {
|
||||
assert sample_text[i] == buffer[i]
|
||||
}
|
||||
assert 0 == sys_close(fd)
|
||||
|
||||
println("read file passed")
|
||||
}
|
||||
|
||||
fn check_open_file_fail() {
|
||||
println ("checking 'open file fail'")
|
||||
fd1, ec1 := sys_open("./nofilehere".str, .o_rdonly, 0)
|
||||
assert fd1 == -1
|
||||
assert ec1 == .enoent
|
||||
println ("'open file fail' check passed")
|
||||
}
|
||||
|
||||
/*
|
||||
fn check_print() {
|
||||
println ("checking print and println")
|
||||
|
||||
a := sys_pipe(intptr(fd))
|
||||
assert a != -1
|
||||
assert fd[0] != -1
|
||||
assert fd[1] != -1
|
||||
|
||||
//sys_dup2
|
||||
println ("print and println passed")
|
||||
}
|
||||
*/
|
||||
|
||||
fn main() {
|
||||
check_read_write_pipe()
|
||||
check_read_file()
|
||||
// check_print()
|
||||
check_open_file_fail()
|
||||
sys_exit(0)
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
module main
|
||||
|
||||
__global buffer [128]byte
|
||||
|
||||
fn check_string_eq () {
|
||||
println ("checking string_eq")
|
||||
assert "monkey" != "rat"
|
||||
@ -8,8 +10,23 @@ fn check_string_eq () {
|
||||
println ("string_eq passed")
|
||||
}
|
||||
|
||||
fn check_i64_tos() {
|
||||
s0 := i64_tos(buffer, 70, 140, 10)
|
||||
assert s0 == "140"
|
||||
|
||||
s1 := i64_tos(buffer, 70, -160, 10)
|
||||
assert s1 == "-160"
|
||||
|
||||
s2 := i64_tos(buffer, 70, 65537, 16)
|
||||
assert s2 == "10001"
|
||||
|
||||
s3 := i64_tos(buffer, 70, -160000, 10)
|
||||
assert s3 == "-160000"
|
||||
}
|
||||
|
||||
fn main () {
|
||||
check_string_eq ()
|
||||
check_i64_tos()
|
||||
sys_exit(0)
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,13 @@ pub fn print(s string) {
|
||||
|
||||
pub fn println(s string) {
|
||||
print(s)
|
||||
sys_write(1, "\n".str, 1)
|
||||
print("\n")
|
||||
}
|
||||
|
||||
pub fn panic(s string) {
|
||||
print('V panic: ')
|
||||
println(s)
|
||||
sys_exit(1)
|
||||
}
|
||||
|
||||
// replaces panic when -debug arg is passed
|
||||
@ -29,16 +35,21 @@ fn panic_debug(line_no int, file, mod, fn_name, s string) {
|
||||
println('=========================================')
|
||||
sys_exit(1)
|
||||
}
|
||||
pub fn eprint(s string) {
|
||||
if isnil(s.str) {
|
||||
panic('eprint(NIL)')
|
||||
}
|
||||
sys_write(2, s.str, u64(s.len))
|
||||
}
|
||||
|
||||
pub fn panic(s string) {
|
||||
print('V panic: ')
|
||||
println(s)
|
||||
sys_exit(1)
|
||||
pub fn eprint_ln(s string) {
|
||||
eprint(s)
|
||||
eprint("\n")
|
||||
}
|
||||
|
||||
pub fn eprintln(s string) {
|
||||
if isnil(s.str) {
|
||||
panic('eprintln(NIL)')
|
||||
}
|
||||
println(s)
|
||||
eprint_ln(s)
|
||||
}
|
||||
|
@ -1,5 +1,24 @@
|
||||
module builtin
|
||||
|
||||
pub enum wp_sys {
|
||||
wnohang = 0x00000001
|
||||
wuntraced = 0x00000002
|
||||
wstopped = 0x00000002
|
||||
wexited = 0x00000004
|
||||
wcontinued = 0x00000008
|
||||
wnowait = 0x01000000 // don't reap, just poll status.
|
||||
__wnothread = 0x20000000 // don't wait on children of other threads in this group
|
||||
__wall = 0x40000000 // wait on all children, regardless of type
|
||||
__wclone = 0x80000000 // wait only on non-sigchld children
|
||||
}
|
||||
|
||||
// First argument to waitid:
|
||||
pub enum wi_sys {
|
||||
p_all = 0
|
||||
p_pid = 1
|
||||
p_pgid = 2
|
||||
}
|
||||
|
||||
pub enum fcntl {
|
||||
fd_cloexec = 0x00000001
|
||||
f_dupfd = 0x00000000
|
||||
@ -61,6 +80,7 @@ pub enum fcntl {
|
||||
}
|
||||
|
||||
pub enum errno {
|
||||
enoerror = 0x00000000
|
||||
e2big = 0x00000007
|
||||
eacces = 0x0000000d
|
||||
eagain = 0x0000000b
|
||||
@ -195,9 +215,13 @@ pub fn sys_write(fd int, buf byteptr, count u64) i64 {
|
||||
return i64(sys_call3(1, u64(fd), u64(buf), count))
|
||||
}
|
||||
|
||||
pub fn sys_open(filename byteptr, flags int, mode int) int {
|
||||
pub fn sys_open(filename byteptr, flags fcntl, mode int) (int, errno) {
|
||||
//2 sys_open const char *filename int flags int mode
|
||||
return int(sys_call3(2, u64(filename), u64(flags), u64(mode)))
|
||||
rc := int(sys_call3(2, u64(filename), u64(flags), u64(mode)))
|
||||
if rc < 0 {
|
||||
return -1, errno(-rc)
|
||||
}
|
||||
return rc, errno.enoerror
|
||||
}
|
||||
|
||||
pub fn sys_close(fd int) int {
|
||||
|
@ -64,6 +64,39 @@ pub fn string_ne (s1, s2 string) bool {
|
||||
return !string_eq(s1,s2)
|
||||
}
|
||||
|
||||
|
||||
pub fn i64_tos(buf byteptr, len int, n0 i64, base int) string {
|
||||
if base < 2 { panic("base must be >= 2")}
|
||||
if base > 36 { panic("base must be <= 36")}
|
||||
|
||||
mut b := tos(buf, len)
|
||||
mut i := len-1
|
||||
|
||||
mut n := n0
|
||||
neg := n < 0
|
||||
if neg { n = -n }
|
||||
|
||||
b[i--] = 0
|
||||
|
||||
for {
|
||||
c := (n%base) + 48
|
||||
b[i--] = if c > 57 {c+7} else {c}
|
||||
if i < 0 { panic ("buffer to small") }
|
||||
n /= base
|
||||
if n < 1 {break}
|
||||
}
|
||||
if (neg) {
|
||||
if i < 0 { panic ("buffer to small") }
|
||||
b[i--] = 45
|
||||
}
|
||||
offset := i+1
|
||||
b.str = b.str + offset
|
||||
b.len -= (offset+1)
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
pub fn (a string) clone() string {
|
||||
mut b := string {
|
||||
|
@ -185,31 +185,11 @@ pub fn (s string) replace(rep, with string) string {
|
||||
return tos(b, new_len)
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn (s string) int() int {
|
||||
return strconv.parse_int(s, 0, 32)
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn (s string) int() int {
|
||||
mut neg := false
|
||||
mut i := 0
|
||||
if s[0] == `-` {
|
||||
neg = true
|
||||
i++
|
||||
}
|
||||
else if s[0] == `+` {
|
||||
i++
|
||||
}
|
||||
mut n := 0
|
||||
for C.isdigit(s[i]) {
|
||||
n = 10 * n - int(s[i] - `0`)
|
||||
i++
|
||||
}
|
||||
return if neg { n } else { -n }
|
||||
return int(strconv.parse_int(s,0,32))
|
||||
}
|
||||
|
||||
|
||||
pub fn (s string) i64() i64 {
|
||||
return strconv.parse_int(s, 0, 64)
|
||||
}
|
||||
@ -223,23 +203,7 @@ pub fn (s string) f64() f64 {
|
||||
}
|
||||
|
||||
pub fn (s string) u32() u32 {
|
||||
mut neg := false
|
||||
mut i := 0
|
||||
if s[0] == `-` {
|
||||
neg = true
|
||||
i++
|
||||
}
|
||||
else if s[0] == `+` {
|
||||
i++
|
||||
}
|
||||
mut n := u32(0)
|
||||
for C.isdigit(s[i]) {
|
||||
n = u32(10) * n - u32(s[i] - `0`)
|
||||
i++
|
||||
}
|
||||
return if neg { n } else { -n }
|
||||
//return C.atol(*char(s.str))
|
||||
//return strconv.parse_uint(s, 0, 32)
|
||||
return u32(strconv.parse_uint(s, 0, 32))
|
||||
}
|
||||
|
||||
pub fn (s string) u64() u64 {
|
||||
|
33
vlib/compiler/tests/enum_hex_test.v
Normal file
33
vlib/compiler/tests/enum_hex_test.v
Normal file
@ -0,0 +1,33 @@
|
||||
enum w_hex {
|
||||
a = 0x001
|
||||
b = 0x010
|
||||
c = 0x100
|
||||
}
|
||||
|
||||
enum w_decimal {
|
||||
a = 1
|
||||
b = 16
|
||||
c = 256
|
||||
}
|
||||
|
||||
const (
|
||||
ca = 1
|
||||
cb = 16
|
||||
cc = 256
|
||||
)
|
||||
|
||||
|
||||
fn test_enum_hex() {
|
||||
assert ca == int(w_decimal.a)
|
||||
assert cb == int(w_decimal.b)
|
||||
assert cc == int(w_decimal.c)
|
||||
|
||||
assert int(w_hex.a) == ca
|
||||
assert int(w_hex.b) == cb
|
||||
assert int(w_hex.c) == cc
|
||||
|
||||
assert int(w_hex.a) == int(w_decimal.a)
|
||||
assert int(w_hex.b) == int(w_decimal.b)
|
||||
assert int(w_hex.c) == int(w_decimal.c)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user