| 1 | package ast |
| 2 | |
| 3 | import ( |
| 4 | "github.com/goccmack/gocc/example/sr/token" |
| 5 | ) |
| 6 | |
| 7 | type ( |
| 8 | Attrib interface{} |
| 9 | |
| 10 | Stmt interface { |
| 11 | Equals(Stmt) bool |
| 12 | MatchIf(cnd string, s Stmt) bool |
| 13 | MatchIfElse(cnd string, s1, s2 Stmt) bool |
| 14 | MatchId(s string) bool |
| 15 | String() string |
| 16 | } |
| 17 | |
| 18 | If struct { |
| 19 | C string |
| 20 | S Stmt |
| 21 | } |
| 22 | |
| 23 | IfElse struct { |
| 24 | C string |
| 25 | S1, S2 Stmt |
| 26 | } |
| 27 | |
| 28 | IdStmt string |
| 29 | ) |
| 30 | |
| 31 | func NewIf(cnd, stmt Attrib) (ifs *If) { |
| 32 | ifs = &If{ |
| 33 | C: string(cnd.(*token.Token).Lit), |
| 34 | S: stmt.(Stmt), |
| 35 | } |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | func NewIfElse(cnd, stmt1, stmt2 Attrib) (ies *IfElse) { |
| 40 | ies = &IfElse{ |
| 41 | C: string(cnd.(*token.Token).Lit), |
| 42 | S1: stmt1.(Stmt), |
| 43 | S2: stmt2.(Stmt), |
| 44 | } |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | func NewIdStmt(s Attrib) (is IdStmt) { |
| 49 | is = IdStmt(s.(*token.Token).Lit) |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | func (this *If) Equals(that Stmt) bool { |
| 54 | if this == that { |
| 55 | return true |
| 56 | } |
| 57 | that1, ok := that.(*If) |
| 58 | if !ok { |
| 59 | return false |
| 60 | } |
| 61 | return this.C == that1.C && this.S.Equals(that1.S) |
| 62 | } |
| 63 | |
| 64 | func (this *IfElse) Equals(that Stmt) bool { |
| 65 | if this == that { |
| 66 | return true |
| 67 | } |
| 68 | that1, ok := that.(*IfElse) |
| 69 | if !ok { |
| 70 | return false |
| 71 | } |
| 72 | return this.C == that1.C && this.S1.Equals(that1.S1) && this.S2.Equals(that1.S2) |
| 73 | } |
| 74 | |
| 75 | func (this IdStmt) Equals(that Stmt) bool { |
| 76 | if this == "" { |
| 77 | return false |
| 78 | } |
| 79 | that1, ok := that.(IdStmt) |
| 80 | if !ok { |
| 81 | return false |
| 82 | } |
| 83 | return this == that1 |
| 84 | } |
| 85 | |
| 86 | func (this *If) MatchIf(cnd string, s Stmt) bool { |
| 87 | return this.C == cnd && this.S.Equals(s) |
| 88 | } |
| 89 | |
| 90 | func (this *IfElse) MatchIf(cnd string, s Stmt) bool { |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | func (this IdStmt) MatchIf(cnd string, s Stmt) bool { |
| 95 | return false |
| 96 | } |
| 97 | |
| 98 | func (this *If) MatchIfElse(cnd string, s1, s2 Stmt) bool { |
| 99 | return false |
| 100 | } |
| 101 | |
| 102 | func (this *IfElse) MatchIfElse(cnd string, s1, s2 Stmt) bool { |
| 103 | return this.C == cnd && this.S1.Equals(s1) && this.S2.Equals(s2) |
| 104 | } |
| 105 | |
| 106 | func (this IdStmt) MatchIfElse(cnd string, s1, s2 Stmt) bool { |
| 107 | return false |
| 108 | } |
| 109 | |
| 110 | func (this *If) MatchId(s string) bool { |
| 111 | return false |
| 112 | } |
| 113 | |
| 114 | func (this *IfElse) MatchId(s string) bool { |
| 115 | return false |
| 116 | } |
| 117 | |
| 118 | func (this IdStmt) MatchId(s string) bool { |
| 119 | return this == IdStmt(s) |
| 120 | } |
| 121 | |
| 122 | func (this *If) String() string { |
| 123 | return "*If: " + this.C + this.S.String() |
| 124 | } |
| 125 | |
| 126 | func (this *IfElse) String() string { |
| 127 | return "*IfElse: " + this.C + this.S1.String() + this.S2.String() |
| 128 | } |
| 129 | |
| 130 | func (this IdStmt) String() string { |
| 131 | return "IdStmt: " + string(this) |
| 132 | } |