1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov 2019-07-17 10:54:24 +02:00
parent 55b8a9acb9
commit b9f3f2d622
2 changed files with 41 additions and 19 deletions

View File

@ -67,6 +67,7 @@ mut:
is_c_struct_init bool
can_chash bool
attr string
v_script bool // "V bash", import all os functions into global space
}
const (
@ -1258,6 +1259,14 @@ fn (p mut Parser) bterm() string {
case Token.gt: p.cgen.set_placeholder(ph, 'string_gt(')
case Token.lt: p.cgen.set_placeholder(ph, 'string_lt(')
}
/*
Token.eq => p.cgen.set_placeholder(ph, 'string_eq(')
Token.ne => p.cgen.set_placeholder(ph, 'string_ne(')
Token.le => p.cgen.set_placeholder(ph, 'string_le(')
Token.ge => p.cgen.set_placeholder(ph, 'string_ge(')
Token.gt => p.cgen.set_placeholder(ph, 'string_gt(')
Token.lt => p.cgen.set_placeholder(ph, 'string_lt(')
*/
}
}
return typ
@ -1442,10 +1451,16 @@ fn (p mut Parser) name_expr() string {
return typ
}
// Function (not method btw, methods are handled in dot())
f := p.table.find_fn(name)
mut f := p.table.find_fn(name)
if f.name == '' {
// We are in a second pass, that means this function was not defined, throw an error.
if !p.first_run() {
// V script? Try os module.
if p.v_script {
name = name.replace('main__', 'os__')
f = p.table.find_fn(name)
}
if f.name == '' {
// If orig_name is a pkg, then printing undefined: `pkg` tells us nothing
// if p.table.known_pkg(orig_name) {
if p.table.known_pkg(orig_name) || p.import_table.known_alias(orig_name) {
@ -1456,10 +1471,12 @@ fn (p mut Parser) name_expr() string {
p.error('undefined: `$orig_name`')
}
}
} else {
p.next()
// First pass, the function can be defined later.
return 'void'
}
}
// no () after func, so func is an argument, just gen its name
// TODO verify this and handle errors
if p.peek() != .lpar {
@ -2770,6 +2787,10 @@ fn (p mut Parser) chash() {
// Move defines on top
p.cgen.includes << '#$hash'
}
else if hash == 'v' {
println('v script')
//p.v_script = true
}
else {
if !p.can_chash {
p.error('bad token `#` (embedding C code is no longer supported)')

View File

@ -483,16 +483,17 @@ pub fn user_os() string {
return 'unknown'
}
// hostname returns hostname
// hostname returns the host name reported by the kernel.
pub fn hostname() ?string {
mut hname := [256]byte
mut name := [256]byte
// https://www.ietf.org/rfc/rfc1035.txt
// The host name is returned as a null-terminated string.
res := C.gethostname(&hname, 256)
res := C.gethostname(&name, 256)
if res != 0 {
return error('os: hostname cannot get host name of PC.')
return error('os.hostname() cannot get the host name')
}
return tos_clone(hname)
return tos_clone(name)
}
// home_dir returns path to user's home directory.