cgen: generate #ifdefs

pull/4103/head
Alexander Medvednikov 2020-03-22 13:55:39 +01:00
parent 8d8907b61e
commit c2ce06eba7
4 changed files with 70 additions and 6 deletions

View File

@ -386,7 +386,8 @@ pub:
pub struct CompIf {
pub:
cond Expr
// cond Expr
val string
stmts []Stmt
pos token.Position
mut:

View File

@ -530,7 +530,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
}
// ast.Attr {}
ast.CompIf {
c.expr(it.cond)
// c.expr(it.cond)
c.stmts(it.stmts)
}
ast.ConstDecl {

View File

@ -268,12 +268,13 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.const_decl(it)
}
ast.CompIf {
g.writeln('//#ifdef ')
g.expr(it.cond)
g.writeln('\n#ifdef ' + comp_if_to_ifdef(it.val))
g.writeln('// #if $it.val')
// g.expr(it.cond)
// println('comp if stmts $g.file.path:$it.pos.line_nr')
g.stmts(it.stmts)
// println('done')
g.writeln('//#endif')
g.writeln('#endif')
}
ast.DeferStmt {
g.writeln('// defer')
@ -1965,3 +1966,64 @@ fn op_to_fn_name(name string) string {
'bad op $name'}
}
}
fn comp_if_to_ifdef(name string) string {
match name {
'windows' {
return '_WIN32'
}
'mac' {
return '__APPLE__'
}
'macos' {
return '__APPLE__'
}
'linux' {
return '__linux__'
}
'freebsd' {
return '__FreeBSD__'
}
'openbsd' {
return '__OpenBSD__'
}
'netbsd' {
return '__NetBSD__'
}
'dragonfly' {
return '__DragonFly__'
}
'msvc' {
return '_MSC_VER'
}
'android' {
return '__ANDROID__'
}
'js' {
return '_VJS'
}
'solaris' {
return '__sun'
}
'haiku' {
return '__haiku__'
}
'tinyc' {
return 'tinyc'
}
'debug' {
return '_VDEBUG'
}
'linux_or_macos' {
return ''
}
'no_bounds_checking' {
return 'NO_BOUNDS_CHECK'
}
else {
verror('bad os ifdef name "$name"')
}
}
// verror('bad os ifdef name "$name"')
return ''
}

View File

@ -11,13 +11,14 @@ pub fn (p mut Parser) comp_if() ast.CompIf {
if p.tok.kind == .not {
p.next()
}
p.check_name()
val := p.check_name()
if p.tok.kind == .question {
p.next()
}
mut node := ast.CompIf{
stmts: p.parse_block()
pos: pos
val: val
}
if p.tok.kind == .dollar && p.peek_tok.kind == .key_else {
p.next()