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

ast: add Expr to AsCast

This commit is contained in:
Alexey 2020-03-07 00:12:15 +03:00 committed by GitHub
parent 06df6d25a2
commit 81ce524705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 0 deletions

View File

@ -411,6 +411,7 @@ pub:
pub struct AsCast {
pub:
expr Expr
typ table.Type
}

View File

@ -308,6 +308,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(']')
}
}
ast.AsCast {
type_str := f.table.type_to_str(it.typ)
f.expr(it.expr)
f.write(' as $type_str')
}
ast.AssignExpr {
f.expr(it.left)
f.write(' $it.op.str() ')

View File

@ -123,6 +123,10 @@ pub mut:
pub_mut_field string
}
struct Bar {
val int
}
const (
reserved_types = {
'i8': true
@ -188,3 +192,13 @@ fn fn_with_match_expr() {
fn fn_variadic(arg int, args ...string) {
println('Do nothing')
}
fn test_as() {
a := sum_expr() as Bar
}
fn sum_expr() FooBar {
return Bar{
val: 5
}
}

View File

@ -125,6 +125,12 @@ struct Foo {
pub_mut_field string
}
struct Bar {
val int
}
type FooBar = Foo | Bar
const (
reserved_types = {
'i8': true
@ -188,3 +194,11 @@ fn fn_with_match_expr() {
fn fn_variadic(arg int, args... string) {
println('Do nothing')
}
fn test_as() {
a := sum_expr() as Bar
}
fn sum_expr() FooBar {
return Bar{ val: 5 }
}

View File

@ -766,6 +766,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
p.next()
typ = p.parse_type()
node = ast.AsCast{
expr: node
typ: typ
}
}