v/vlib/v/parser/comptime.v

133 lines
2.3 KiB
V
Raw Normal View History

2020-02-10 14:42:57 +01:00
module parser
2020-02-17 14:15:42 +01:00
import (
v.ast
2020-03-27 14:44:30 +01:00
v.pref
2020-02-17 14:15:42 +01:00
)
2020-03-27 14:44:30 +01:00
const (
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', 'netbsd',
2020-03-27 14:44:30 +01:00
'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
)
fn (p mut Parser) comp_if() ast.CompIf {
2020-03-22 11:53:08 +01:00
pos := p.tok.position()
2020-02-17 14:15:42 +01:00
p.next()
p.check(.key_if)
is_not := p.tok.kind == .not
if is_not {
2020-02-17 14:15:42 +01:00
p.next()
}
2020-03-22 13:55:39 +01:00
val := p.check_name()
2020-03-27 14:44:30 +01:00
mut stmts := []ast.Stmt
mut skip_os := false
if val in supported_platforms {
os := os_from_string(val)
// `$if os {` for a different target, skip everything inside
// to avoid compilation errors (like including <windows.h> or calling WinAPI fns
// on non-Windows systems)
if false && ((!is_not && os != p.pref.os) || (is_not && os == p.pref.os)) && !p.pref.output_cross_c {
2020-03-27 14:44:30 +01:00
skip_os = true
p.check(.lcbr)
// p.warn('skipping $if $val os=$os p.pref.os=$p.pref.os')
2020-03-27 14:44:30 +01:00
mut stack := 1
for {
if p.tok.kind == .key_return {
p.returns = true
}
if p.tok.kind == .lcbr {
stack++
}
else if p.tok.kind == .rcbr {
stack--
}
if p.tok.kind == .eof {
break
}
if stack <= 0 && p.tok.kind == .rcbr {
// p.warn('exiting $stack')
p.next()
break
}
p.next()
}
}
}
2020-02-17 14:15:42 +01:00
if p.tok.kind == .question {
p.next()
}
2020-03-27 14:44:30 +01:00
if !skip_os {
stmts = p.parse_block()
}
mut node := ast.CompIf{
is_not: is_not
2020-03-22 11:53:08 +01:00
pos: pos
2020-03-22 13:55:39 +01:00
val: val
2020-03-27 14:44:30 +01:00
stmts: stmts
}
2020-02-17 14:15:42 +01:00
if p.tok.kind == .dollar && p.peek_tok.kind == .key_else {
p.next()
p.check(.key_else)
2020-03-22 14:54:31 +01:00
node.has_else = true
node.else_stmts = p.parse_block()
2020-02-17 14:15:42 +01:00
}
return node
2020-02-17 14:15:42 +01:00
}
2020-03-27 14:44:30 +01:00
const (
todo_delete_me = pref.OS.linux // TODO import warning bug
)
2020-03-27 14:44:30 +01:00
fn os_from_string(os string) pref.OS {
match os {
'linux' {
return .linux
}
'windows' {
return .windows
}
'mac' {
return .mac
}
'macos' {
return .mac
}
'freebsd' {
return .freebsd
}
'openbsd' {
return .openbsd
}
'netbsd' {
return .netbsd
}
'dragonfly' {
return .dragonfly
}
'js' {
return .js
}
'solaris' {
return .solaris
}
'android' {
return .android
}
'msvc' {
// notice that `-os msvc` became `-cc msvc`
verror('use the flag `-cc msvc` to build using msvc')
}
'haiku' {
return .haiku
}
'linux_or_macos' {
return .linux
}
else {
panic('bad os $os')
}
}
// println('bad os $os') // todo panic?
return .linux
}