Repository navigation
Expand file tree
/
Copy pathbuilders_expr.go
More file actions
163 lines (145 loc) · 4.23 KB
/
Copy pathbuilders_expr.go
File metadata and controls
163 lines (145 loc) · 4.23 KB
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
package uir
// PackageBuilder provides fluent API for building PackageNode
// Var creates a variable reference
func Var(name string) ScopedVariableRef {
return ScopedVariableRef{
VariableRef: VariableRef{Name: name},
}
}
// VarExpr creates a variable reference expression
func VarExpr(name string) ExprStmt {
v := Var(name)
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
Variable: &v,
}
}
// ObjectExpr creates an inline object-literal expression from ordered
// key/value entries, e.g. `{ "service.id": ServiceID, Country: Country }`.
// Use this instead of VarExpr for object literals — VarExpr names are
// sanitized into identifiers by emitters, which mangles the literal.
func ObjectExpr(entries ...ObjectLiteralEntry) ExprStmt {
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
ObjectLiteral: &ObjectLiteralStmt{
statementBase: statementBase{Type: ASTStatementTypeObjectLiteral},
Entries: entries,
},
}
}
// ObjectEntry creates a single key/value pair for ObjectExpr.
func ObjectEntry(key string, value ExprStmt) ObjectLiteralEntry {
return ObjectLiteralEntry{Key: key, Value: value}
}
// Literal creates a literal statement
func Literal(value string, fieldType RecordFieldType) LiteralStmt {
return LiteralStmt{
statementBase: statementBase{Type: ASTStatementTypeLiteral},
Value: &value,
FieldType: fieldType,
}
}
// StringLit creates a string literal
func StringLit(s string) LiteralStmt {
return Literal(s, RecordFieldTypeString)
}
// IntLit creates an integer literal
func IntLit(i int) LiteralStmt {
val := string(rune(i))
return Literal(val, RecordFieldTypeNumber)
}
// BoolLit creates a boolean literal
func BoolLit(b bool) LiteralStmt {
val := "false"
if b {
val = "true"
}
return Literal(val, RecordFieldTypeBoolean)
}
// LitExpr creates a literal expression
func LitExpr(value string, fieldType RecordFieldType) ExprStmt {
lit := Literal(value, fieldType)
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
Literal: &lit,
}
}
// Field creates a record field
func Field(name string, fieldType RecordFieldType) RecordField {
val := RecordField{
FieldType: fieldType,
}
val.Field = name
val.NodeType = NodeTypeField
return val
}
// NewCondition creates a condition statement
func NewCondition(expr ExprStmt) ConditionStmt {
return ConditionStmt{
statementBase: statementBase{Type: ASTStatmentTypeCondition},
Expr: expr,
}
}
// BinaryExpr creates a binary expression
func BinaryExpr(left ExprStmt, op BinaryOp, right ExprStmt) ExprStmt {
binary := BinaryStmt{
statementBase: statementBase{Type: ASTStatementTypeBinary},
Left: left,
Operator: op,
Right: right,
}
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
Binary: &binary,
}
}
// UnaryExpr creates a unary expression
func UnaryExpr(op UnaryOp, operand ExprStmt) ExprStmt {
unary := UnaryStmt{
statementBase: statementBase{Type: ASTStatementTypeUnary},
Operator: op,
Operand: operand,
}
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
Unary: &unary,
}
}
// CastExpr creates a cast expression
func CastExpr(expr ExprStmt, targetType string) ExprStmt {
cast := CastStmt{
statementBase: statementBase{Type: ASTStatementTypeCast},
Expr: expr,
TargetType: targetType,
}
return ExprStmt{
statementBase: statementBase{Type: ASTStatementTypeExpression},
Cast: &cast,
}
}
// NewReturn creates a return statement
func NewReturn(value ExprStmt) ReturnStmt {
return ReturnStmt{
statementBase: statementBase{Type: ASTStatementTypeReturn},
Value: value,
}
}
// NewBreak creates a break statement
func NewBreak() BreakStmt {
return BreakStmt{
statementBase: statementBase{Type: ASTStatementTypeBreak},
}
}
// NewContinue creates a continue statement
func NewContinue() ContinueStmt {
return ContinueStmt{
statementBase: statementBase{Type: ASTStatementTypeContinue},
}
}
// NewThrow creates a throw statement
func NewThrow(exception ExprStmt) ThrowStmt {
return ThrowStmt{
statementBase: statementBase{Type: ASTStatementTypeThrow},
Exception: exception,
}
}