Debian Codegraph @

example/bools/ast/ast.go

raw symbols SCIP
1 //Copyright 2012 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ast
16
17 import (
18 "strconv"
19 "strings"
20
21 "github.com/goccmack/gocc/example/bools/token"
22 "github.com/goccmack/gocc/example/bools/util"
23 )
24
25 type Attrib interface{}
26
27 type Val interface {
28 Attrib
29 Eval() bool
30 String() string
31 }
32
33 type Op int
34
35 func (o Op) String() string {
36 switch o {
37 case OR:
38 return "|"
39 case AND:
40 return "&"
41 }
42 return ""
43 }
44
45 const (
46 NOOP = Op(0)
47 OR = Op(1)
48 AND = Op(2)
49 )
50
51 type BoolExpr struct {
52 A Val
53 B Val
54 Op Op
55 }
56
57 func NewBoolAndExpr(a, b Attrib) (*BoolExpr, error) {
58 return &BoolExpr{a.(Val), b.(Val), AND}, nil
59 }
60
61 func NewBoolOrExpr(a, b Attrib) (*BoolExpr, error) {
62 return &BoolExpr{a.(Val), b.(Val), OR}, nil
63 }
64
65 func NewBoolGroupExpr(a Attrib) (*BoolExpr, error) {
66 return &BoolExpr{a.(Val), nil, NOOP}, nil
67 }
68
69 func (this *BoolExpr) Eval() bool {
70 switch this.Op {
71 case OR:
72 return this.A.Eval() || this.B.Eval()
73 case AND:
74 return this.A.Eval() && this.B.Eval()
75 }
76 return this.A.Eval()
77 }
78
79 func (this *BoolExpr) String() string {
80 return this.A.String() + " " + this.Op.String() + " " + this.B.String()
81 }
82
83 type val bool
84
85 func (v val) Eval() bool {
86 return bool(v)
87 }
88
89 func (v val) String() string {
90 if v {
91 return "true"
92 }
93 return "false"
94 }
95
96 var (
97 TRUE = val(true)
98 FALSE = val(false)
99 )
100
101 type LessThanExpr struct {
102 A int64
103 B int64
104 }
105
106 func NewLessThanExpr(a, b Attrib) (*LessThanExpr, error) {
107 aint, err := util.IntValue(a.(*token.Token).Lit)
108 if err != nil {
109 return nil, err
110 }
111 bint, err := util.IntValue(b.(*token.Token).Lit)
112 if err != nil {
113 return nil, err
114 }
115 return &LessThanExpr{aint, bint}, nil
116 }
117
118 func (this *LessThanExpr) Eval() bool {
119 return this.A < this.B
120 }
121
122 func (this *LessThanExpr) String() string {
123 return strconv.FormatInt(this.A, 10) + " < " + strconv.FormatInt(this.B, 10)
124 }
125
126 type SubStringExpr struct {
127 A string
128 B string
129 }
130
131 func NewSubStringExpr(a, b Attrib) (*SubStringExpr, error) {
132 astr, err := strconv.Unquote(string(a.(*token.Token).Lit))
133 if err != nil {
134 return nil, err
135 }
136 bstr, err := strconv.Unquote(string(b.(*token.Token).Lit))
137 if err != nil {
138 return nil, err
139 }
140 return &SubStringExpr{astr, bstr}, nil
141 }
142
143 func (this *SubStringExpr) Eval() bool {
144 return strings.Contains(this.B, this.A)
145 }
146
147 func (this *SubStringExpr) String() string {
148 return this.A + " in " + this.B
149 }