-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathast.go
197 lines (164 loc) · 3.89 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gosql
import (
"fmt"
"strings"
)
type ExpressionKind uint
const (
LiteralKind ExpressionKind = iota
BinaryKind
)
type BinaryExpression struct {
A Expression
B Expression
Op Token
}
func (be BinaryExpression) GenerateCode() string {
return fmt.Sprintf("(%s %s %s)", be.A.GenerateCode(), be.Op.Value, be.B.GenerateCode())
}
type Expression struct {
Literal *Token
Binary *BinaryExpression
Kind ExpressionKind
}
func (e Expression) GenerateCode() string {
switch e.Kind {
case LiteralKind:
switch e.Literal.Kind {
case IdentifierKind:
return fmt.Sprintf("\"%s\"", e.Literal.Value)
case StringKind:
return fmt.Sprintf("'%s'", e.Literal.Value)
default:
return fmt.Sprintf(e.Literal.Value)
}
case BinaryKind:
return e.Binary.GenerateCode()
}
return ""
}
type SelectItem struct {
Exp *Expression
Asterisk bool // for *
As *Token
}
type SelectStatement struct {
Item *[]*SelectItem
From *Token
Where *Expression
Limit *Expression
Offset *Expression
}
func (ss SelectStatement) GenerateCode() string {
item := []string{}
for _, i := range *ss.Item {
s := "\t*"
if !i.Asterisk {
s = "\t" + i.Exp.GenerateCode()
if i.As != nil {
s = fmt.Sprintf("\t%s AS \"%s\"", s, i.As.Value)
}
}
item = append(item, s)
}
code := "SELECT\n" + strings.Join(item, ",\n")
if ss.From != nil {
code += fmt.Sprintf("\nFROM\n\t\"%s\"", ss.From.Value)
}
if ss.Where != nil {
code += "\nWHERE\n\t" + ss.Where.GenerateCode()
}
if ss.Limit != nil {
code += "\nLIMIT\n\t" + ss.Limit.GenerateCode()
}
if ss.Offset != nil {
code += "\nOFFSET\n\t" + ss.Limit.GenerateCode()
}
return code + ";"
}
type ColumnDefinition struct {
Name Token
Datatype Token
PrimaryKey bool
}
type CreateTableStatement struct {
Name Token
Cols *[]*ColumnDefinition
}
func (cts CreateTableStatement) GenerateCode() string {
cols := []string{}
for _, col := range *cts.Cols {
modifiers := ""
if col.PrimaryKey {
modifiers += " " + "PRIMARY KEY"
}
spec := fmt.Sprintf("\t\"%s\" %s%s", col.Name.Value, strings.ToUpper(col.Datatype.Value), modifiers)
cols = append(cols, spec)
}
return fmt.Sprintf("CREATE TABLE \"%s\" (\n%s\n);", cts.Name.Value, strings.Join(cols, ",\n"))
}
type CreateIndexStatement struct {
Name Token
Unique bool
PrimaryKey bool
Table Token
Exp Expression
}
func (cis CreateIndexStatement) GenerateCode() string {
unique := ""
if cis.Unique {
unique = " UNIQUE"
}
return fmt.Sprintf("CREATE%s INDEX \"%s\" ON \"%s\" (%s);", unique, cis.Name.Value, cis.Table.Value, cis.Exp.GenerateCode())
}
type DropTableStatement struct {
Name Token
}
func (dts DropTableStatement) GenerateCode() string {
return fmt.Sprintf("DROP TABLE \"%s\";", dts.Name.Value)
}
type InsertStatement struct {
Table Token
Values *[]*Expression
}
func (is InsertStatement) GenerateCode() string {
values := []string{}
for _, exp := range *is.Values {
values = append(values, exp.GenerateCode())
}
return fmt.Sprintf("INSERT INTO \"%s\" VALUES (%s);", is.Table.Value, strings.Join(values, ", "))
}
type AstKind uint
const (
SelectKind AstKind = iota
CreateTableKind
CreateIndexKind
DropTableKind
InsertKind
)
type Statement struct {
SelectStatement *SelectStatement
CreateTableStatement *CreateTableStatement
CreateIndexStatement *CreateIndexStatement
DropTableStatement *DropTableStatement
InsertStatement *InsertStatement
Kind AstKind
}
func (s Statement) GenerateCode() string {
switch s.Kind {
case SelectKind:
return s.SelectStatement.GenerateCode()
case CreateTableKind:
return s.CreateTableStatement.GenerateCode()
case CreateIndexKind:
return s.CreateIndexStatement.GenerateCode()
case DropTableKind:
return s.DropTableStatement.GenerateCode()
case InsertKind:
return s.InsertStatement.GenerateCode()
}
return "?unknown?"
}
type Ast struct {
Statements []*Statement
}