cgen: allow for `const ( x = opt() ? )`

pull/7128/head
Delyan Angelov 2020-12-04 12:44:16 +02:00
parent d0a2992335
commit 52ccdd747f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 55 additions and 1 deletions

View File

@ -0,0 +1,17 @@
import regex
const (
a_or_b = regex.regex_opt('a|b') ?
)
fn f(s string) bool {
mut re := a_or_b
start, _ := re.match_string(s)
return start != -1
}
fn test_const_regex_works() {
assert f('a') == true
assert f('b') == true
assert f('c') == false
}

View File

@ -726,6 +726,10 @@ pub fn (mut g Gen) new_tmp_var() string {
return '_t$g.tmp_count' return '_t$g.tmp_count'
} }
pub fn (mut g Gen) current_tmp_var() string {
return '_t$g.tmp_count'
}
/* /*
pub fn (mut g Gen) new_tmp_var2() string { pub fn (mut g Gen) new_tmp_var2() string {
g.tmp_count2++ g.tmp_count2++
@ -4125,6 +4129,14 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
g.stringliterals.writeln('\t_const_$name = $val;') g.stringliterals.writeln('\t_const_$name = $val;')
} }
} }
ast.CallExpr {
if val.starts_with('Option_') {
g.inits[field.mod].writeln(val)
g.const_decl_init_later(field.mod, name, g.current_tmp_var(), field.typ)
} else {
g.const_decl_init_later(field.mod, name, val, field.typ)
}
}
else { else {
g.const_decl_init_later(field.mod, name, val, field.typ) g.const_decl_init_later(field.mod, name, val, field.typ)
} }
@ -5086,7 +5098,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
g.stmts(stmts) g.stmts(stmts)
} }
} else if or_block.kind == .propagate { } else if or_block.kind == .propagate {
if g.file.mod.name == 'main' && g.fn_decl.name == 'main.main' { if g.file.mod.name == 'main' && (isnil(g.fn_decl) || g.fn_decl.name == 'main.main') {
// In main(), an `opt()?` call is sugar for `opt() or { panic(err) }` // In main(), an `opt()?` call is sugar for `opt() or { panic(err) }`
if g.pref.is_debug { if g.pref.is_debug {
paline, pafile, pamod, pafn := g.panic_debug_info(or_block.pos) paline, pafile, pamod, pafn := g.panic_debug_info(or_block.pos)
@ -5849,6 +5861,9 @@ fn (mut g Gen) interface_call(typ table.Type, interface_type table.Type) {
fn (mut g Gen) panic_debug_info(pos token.Position) (int, string, string, string) { fn (mut g Gen) panic_debug_info(pos token.Position) (int, string, string, string) {
paline := pos.line_nr + 1 paline := pos.line_nr + 1
if isnil(g.fn_decl) {
return paline, '', 'main', 'C._vinit'
}
pafile := g.fn_decl.file.replace('\\', '/') pafile := g.fn_decl.file.replace('\\', '/')
pafn := g.fn_decl.name.after('.') pafn := g.fn_decl.name.after('.')
pamod := g.fn_decl.modname() pamod := g.fn_decl.modname()

View File

@ -0,0 +1,22 @@
const (
aaa = iopt() ?
bbb = sopt() ?
)
fn iopt() ?int {
return 1234
}
fn sopt() ?string {
return 'xyz'
}
fn test_iconsts_are_resolved() {
z := aaa
assert z == 1234
}
fn test_sconsts_are_resolved() {
z := bbb
assert z == 'xyz'
}