1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: implement typeof(x)

This commit is contained in:
Delyan Angelov
2020-02-24 22:45:47 +02:00
committed by GitHub
parent 794ee6fc9d
commit b17ade1257
7 changed files with 124 additions and 6 deletions

View File

@ -68,8 +68,7 @@ fn (p mut Parser) bool_expression() string {
//p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type')
T := p.table.find_type(typ)
if T.parent == expected {
p.cgen.set_placeholder(start_ph,
'/*SUM TYPE CAST2*/($expected) { .obj = memdup( &($typ[]) { ')
p.cgen.set_placeholder(start_ph, '/*SUM TYPE CAST2*/ ($expected) { .obj = memdup( &($typ[]) { ')
tt := typ.all_after('_') // TODO
p.gen('}, sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }')
}
@ -824,13 +823,29 @@ fn (p mut Parser) factor() string {
// p.fgen('$sizeof_typ)')
return 'int'
}
.key_typeof {
p.next()
p.check(.lpar)
p.cgen.nogen = true
vname := if p.tok == .name && p.peek() == .rpar { p.lit } else { '' }
type_of_var := p.expression()
p.cgen.nogen = false
p.check(.rpar)
is_sum_type := type_of_var in p.table.sum_types
if is_sum_type && vname.len > 0 {
// TODO: make this work for arbitrary sumtype expressions, not just simple vars
p.gen('tos3(__SumTypeNames__${type_of_var}[${vname}.typ - 1])')
}else{
p.gen('tos3("$type_of_var")')
}
return 'string'
}
.key_nameof {
p.next()
p.check(.lpar)
mut nameof_typ := p.get_type()
p.check(.rpar)
p.gen('tos3("$nameof_typ")')
// return 'byteptr'
return 'string'
}
.key_offsetof {