mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: sum types: match
This commit is contained in:
parent
98fc9ab65b
commit
37504c487a
@ -344,12 +344,13 @@ mut:
|
||||
|
||||
pub struct MatchExpr {
|
||||
pub:
|
||||
tok_kind token.Kind
|
||||
cond Expr
|
||||
branches []MatchBranch
|
||||
pos token.Position
|
||||
tok_kind token.Kind
|
||||
cond Expr
|
||||
branches []MatchBranch
|
||||
pos token.Position
|
||||
mut:
|
||||
expr_type table.Type // type of `x` in `match x {`
|
||||
expr_type table.Type // type of `x` in `match x {`
|
||||
is_sum_type bool
|
||||
}
|
||||
|
||||
pub struct MatchBranch {
|
||||
|
@ -315,7 +315,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
}
|
||||
}
|
||||
ast.TypeDecl {
|
||||
g.writeln('// type')
|
||||
g.writeln('// TypeDecl')
|
||||
}
|
||||
ast.UnsafeStmt {
|
||||
g.stmts(it.stmts)
|
||||
@ -728,7 +728,10 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
}
|
||||
g.write('if (')
|
||||
for i, expr in branch.exprs {
|
||||
if type_sym.kind == .string {
|
||||
if it.is_sum_type {
|
||||
g.write('${tmp}.typ == ')
|
||||
}
|
||||
else if type_sym.kind == .string {
|
||||
g.write('string_eq($tmp, ')
|
||||
}
|
||||
else {
|
||||
@ -909,7 +912,10 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
g.write(it.field)
|
||||
}
|
||||
ast.Type {
|
||||
g.write('/* Type */')
|
||||
// match sum Type
|
||||
// g.write('/* Type */')
|
||||
g.write('_type_idx_')
|
||||
g.write(g.typ(it.typ))
|
||||
}
|
||||
else {
|
||||
// #printf("node=%d\n", node.typ);
|
||||
@ -1270,7 +1276,7 @@ fn (g mut Gen) write_sorted_types() {
|
||||
}
|
||||
|
||||
fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||
for typ in types {
|
||||
for i, typ in types {
|
||||
if typ.name.starts_with('C.') {
|
||||
continue
|
||||
}
|
||||
@ -1288,11 +1294,12 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
|
||||
// g.definitions.writeln('} $name;\n')
|
||||
//
|
||||
g.definitions.writeln('};\n')
|
||||
g.typedefs.writeln('#define _type_idx_$name $i')
|
||||
}
|
||||
table.Enum {
|
||||
g.definitions.writeln('typedef enum {')
|
||||
for i, val in it.vals {
|
||||
g.definitions.writeln('\t${name}_$val, // $i')
|
||||
for j, val in it.vals {
|
||||
g.definitions.writeln('\t${name}_$val, // $j')
|
||||
}
|
||||
g.definitions.writeln('} $name;\n')
|
||||
}
|
||||
|
@ -1,3 +1,15 @@
|
||||
struct IfExpr {
|
||||
};
|
||||
|
||||
struct IntegerLiteral {
|
||||
};
|
||||
|
||||
// Sum type
|
||||
typedef struct {
|
||||
void* obj;
|
||||
int typ;
|
||||
} Expr;
|
||||
|
||||
struct User {
|
||||
int age;
|
||||
string name;
|
||||
@ -8,6 +20,9 @@ struct User {
|
||||
typedef Option Option_int;
|
||||
Option_int get_opt();
|
||||
void User_foo(User* u);
|
||||
void println(string s);
|
||||
void handle_expr(Expr e);
|
||||
// TypeDecl
|
||||
|
||||
Option_int get_opt() {
|
||||
return opt_ok(& (int []) { 0 }, sizeof(int));
|
||||
@ -20,6 +35,26 @@ tos3(""),
|
||||
}), u->age);
|
||||
}
|
||||
|
||||
void println(string s) {
|
||||
}
|
||||
|
||||
void handle_expr(Expr e) {
|
||||
Expr tmp1 = e;
|
||||
if (tmp1.typ == _type_idx_IfExpr) {
|
||||
println(tos3("if"));
|
||||
|
||||
}
|
||||
else if (tmp1.typ == _type_idx_IntegerLiteral) {
|
||||
println(tos3("integer"));
|
||||
|
||||
}
|
||||
else {
|
||||
println(tos3("else"));
|
||||
|
||||
}
|
||||
;
|
||||
}
|
||||
|
||||
int main() {
|
||||
User user = (User){
|
||||
0};
|
||||
|
@ -1,3 +1,8 @@
|
||||
type Expr = IfExpr | IntegerLiteral
|
||||
|
||||
struct IfExpr{}
|
||||
struct IntegerLiteral{}
|
||||
|
||||
struct User {
|
||||
age int
|
||||
name string
|
||||
@ -12,6 +17,26 @@ fn (u &User) foo() {
|
||||
zzz := [''].repeat(u.age)
|
||||
}
|
||||
|
||||
fn println(s string) {}
|
||||
|
||||
fn handle_expr(e Expr) {
|
||||
match e {
|
||||
IfExpr {
|
||||
println('if')
|
||||
|
||||
|
||||
}
|
||||
IntegerLiteral {
|
||||
println('integer')
|
||||
|
||||
}
|
||||
else {
|
||||
println('else')
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
user := User{}
|
||||
user.age = 10
|
||||
|
@ -703,7 +703,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
|
||||
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
||||
.lcbr {
|
||||
p.next()
|
||||
if p.tok.kind == .string{
|
||||
if p.tok.kind == .string {
|
||||
mut keys := []ast.Expr
|
||||
mut vals := []ast.Expr
|
||||
for p.tok.kind != .rcbr && p.tok.kind != .eof {
|
||||
@ -1169,7 +1169,7 @@ fn (p mut Parser) string_expr() ast.Expr {
|
||||
return node
|
||||
}
|
||||
// Handle $ interpolation
|
||||
for p.tok.kind == .string{
|
||||
for p.tok.kind == .string {
|
||||
p.next()
|
||||
if p.tok.kind != .str_dollar {
|
||||
continue
|
||||
@ -1596,6 +1596,7 @@ fn (p mut Parser) global_decl() ast.GlobalDecl {
|
||||
fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||
p.check(.key_match)
|
||||
is_mut := p.tok.kind == .key_mut
|
||||
mut is_sum_type := false
|
||||
if is_mut {
|
||||
p.next()
|
||||
}
|
||||
@ -1626,6 +1627,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||
p.next()
|
||||
p.parse_type()
|
||||
}
|
||||
is_sum_type = true
|
||||
}
|
||||
else {
|
||||
// Expression match
|
||||
@ -1653,6 +1655,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||
return ast.MatchExpr{
|
||||
branches: branches
|
||||
cond: cond
|
||||
is_sum_type: is_sum_type
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user