ast: add Expr to AsCast
parent
06df6d25a2
commit
81ce524705
|
@ -411,6 +411,7 @@ pub:
|
||||||
|
|
||||||
pub struct AsCast {
|
pub struct AsCast {
|
||||||
pub:
|
pub:
|
||||||
|
expr Expr
|
||||||
typ table.Type
|
typ table.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -308,6 +308,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
f.write(']')
|
f.write(']')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ast.AsCast {
|
||||||
|
type_str := f.table.type_to_str(it.typ)
|
||||||
|
f.expr(it.expr)
|
||||||
|
f.write(' as $type_str')
|
||||||
|
}
|
||||||
ast.AssignExpr {
|
ast.AssignExpr {
|
||||||
f.expr(it.left)
|
f.expr(it.left)
|
||||||
f.write(' $it.op.str() ')
|
f.write(' $it.op.str() ')
|
||||||
|
|
|
@ -123,6 +123,10 @@ pub mut:
|
||||||
pub_mut_field string
|
pub_mut_field string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Bar {
|
||||||
|
val int
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
reserved_types = {
|
reserved_types = {
|
||||||
'i8': true
|
'i8': true
|
||||||
|
@ -188,3 +192,13 @@ fn fn_with_match_expr() {
|
||||||
fn fn_variadic(arg int, args ...string) {
|
fn fn_variadic(arg int, args ...string) {
|
||||||
println('Do nothing')
|
println('Do nothing')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_as() {
|
||||||
|
a := sum_expr() as Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum_expr() FooBar {
|
||||||
|
return Bar{
|
||||||
|
val: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -125,6 +125,12 @@ struct Foo {
|
||||||
pub_mut_field string
|
pub_mut_field string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Bar {
|
||||||
|
val int
|
||||||
|
}
|
||||||
|
|
||||||
|
type FooBar = Foo | Bar
|
||||||
|
|
||||||
const (
|
const (
|
||||||
reserved_types = {
|
reserved_types = {
|
||||||
'i8': true
|
'i8': true
|
||||||
|
@ -188,3 +194,11 @@ fn fn_with_match_expr() {
|
||||||
fn fn_variadic(arg int, args... string) {
|
fn fn_variadic(arg int, args... string) {
|
||||||
println('Do nothing')
|
println('Do nothing')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_as() {
|
||||||
|
a := sum_expr() as Bar
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum_expr() FooBar {
|
||||||
|
return Bar{ val: 5 }
|
||||||
|
}
|
||||||
|
|
|
@ -766,6 +766,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
||||||
p.next()
|
p.next()
|
||||||
typ = p.parse_type()
|
typ = p.parse_type()
|
||||||
node = ast.AsCast{
|
node = ast.AsCast{
|
||||||
|
expr: node
|
||||||
typ: typ
|
typ: typ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue