forked from rsc/c2go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmacro.go
228 lines (208 loc) · 4.52 KB
/
macro.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"fmt"
"go/ast"
"go/token"
"github.com/andybalholm/c2go/cc"
)
type expressionMacro struct {
args []string
variadic bool
expr ast.Expr
}
func (m expressionMacro) instantiate(args []*cc.Expr) (*cc.Expr, error) {
if len(args) < len(m.args) || (len(args) > len(m.args) && !m.variadic) {
return nil, fmt.Errorf("expected %d arguments, got %d", len(m.args), len(args))
}
argMap := make(map[string]*cc.Expr, len(args))
for i, a := range m.args {
argMap[a] = args[i]
}
if m.variadic {
argMap["..."] = &cc.Expr{
Op: cc.Comma,
List: args[len(m.args):],
}
}
return renderMacroExpr(m.expr, argMap)
}
type statementMacro struct {
args []string
variadic bool
stmts []ast.Stmt
}
func (m statementMacro) instantiate(args []*cc.Expr) ([]*cc.Stmt, error) {
if len(args) < len(m.args) || (len(args) > len(m.args) && !m.variadic) {
return nil, fmt.Errorf("expected %d arguments, got %d", len(m.args), len(args))
}
argMap := make(map[string]*cc.Expr, len(args))
for i, a := range m.args {
argMap[a] = args[i]
}
if m.variadic {
argMap["..."] = &cc.Expr{
Op: cc.Comma,
List: args[len(m.args):],
}
}
list := make([]*cc.Stmt, len(m.stmts))
for i, s := range m.stmts {
stmt, err := renderMacroStmt(s, argMap)
if err != nil {
return nil, err
}
list[i] = stmt
}
return list, nil
}
var operatorMap = map[token.Token]cc.ExprOp{
token.ADD: cc.Add,
token.SUB: cc.Sub,
token.MUL: cc.Mul,
token.QUO: cc.Div,
token.REM: cc.Mod,
token.AND: cc.And,
token.OR: cc.Or,
token.XOR: cc.Xor,
token.SHL: cc.Lsh,
token.SHR: cc.Rsh,
token.LAND: cc.AndAnd,
token.LOR: cc.OrOr,
token.EQL: cc.EqEq,
token.LSS: cc.Lt,
token.GTR: cc.Gt,
token.LEQ: cc.LtEq,
token.GEQ: cc.GtEq,
}
func renderMacroExpr(e ast.Expr, args map[string]*cc.Expr) (ret *cc.Expr, err error) {
switch e := e.(type) {
case *ast.Ident:
if arg, ok := args[e.Name]; ok {
return arg, nil
}
return &cc.Expr{
Op: cc.Name,
Text: e.Name,
}, nil
case *ast.BasicLit:
switch e.Kind {
case token.INT, token.FLOAT, token.IMAG, token.CHAR:
return &cc.Expr{
Op: cc.Number,
Text: e.Value,
}, nil
case token.STRING:
return &cc.Expr{
Op: cc.String,
Texts: []string{e.Value},
}, nil
default:
return nil, fmt.Errorf("unsupported Go literal type %v", e.Kind)
}
case *ast.BinaryExpr:
x, err := renderMacroExpr(e.X, args)
if err != nil {
return nil, err
}
y, err := renderMacroExpr(e.Y, args)
if err != nil {
return nil, err
}
op, ok := operatorMap[e.Op]
if !ok {
return nil, fmt.Errorf("unsupported Go operator %v", e.Op)
}
return &cc.Expr{
Op: op,
Left: x,
Right: y,
}, nil
case *ast.SelectorExpr:
x, err := renderMacroExpr(e.X, args)
if err != nil {
return nil, err
}
return &cc.Expr{
Op: cc.Dot,
Left: x,
Text: e.Sel.Name,
}, nil
case *ast.CallExpr:
fun, err := renderMacroExpr(e.Fun, args)
if err != nil {
return nil, err
}
var list []*cc.Expr
for _, a := range e.Args {
if a, ok := a.(*ast.Ident); ok && a.Name == "__VA_ARGS__" && args["..."] != nil {
list = append(list, args["..."].List...)
continue
}
arg, err := renderMacroExpr(a, args)
if err != nil {
return nil, err
}
list = append(list, arg)
}
return &cc.Expr{
Op: cc.Call,
Left: fun,
List: list,
}, nil
default:
return nil, fmt.Errorf("unsupported Go AST type in macro: %T", e)
}
}
func renderMacroStmt(s ast.Stmt, args map[string]*cc.Expr) (*cc.Stmt, error) {
switch s := s.(type) {
case *ast.BlockStmt:
var list []*cc.Stmt
for _, item := range s.List {
stmt, err := renderMacroStmt(item, args)
if err != nil {
return nil, err
}
list = append(list, stmt)
}
return &cc.Stmt{
Op: cc.Block,
Block: list,
}, nil
case *ast.ExprStmt:
x, err := renderMacroExpr(s.X, args)
if err != nil {
return nil, err
}
return &cc.Stmt{
Op: cc.StmtExpr,
Expr: x,
}, nil
case *ast.IfStmt:
if s.Init != nil {
return nil, fmt.Errorf("initializer for if statement is not supported")
}
cond, err := renderMacroExpr(s.Cond, args)
if err != nil {
return nil, err
}
body, err := renderMacroStmt(s.Body, args)
if err != nil {
return nil, err
}
var els *cc.Stmt
if s.Else != nil {
els, err = renderMacroStmt(s.Else, args)
if err != nil {
return nil, err
}
}
return &cc.Stmt{
Op: cc.If,
Expr: cond,
Body: body,
Else: els,
}, nil
default:
return nil, fmt.Errorf("unsupported Go AST type in macro: %T", s)
}
}