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
|
# File 'lib/rox/core/roxx/parser.rb', line 62
def set_basic_operators
add_operator('isUndefined') do |_parser, stack, _context|
op1 = stack.pop
if op1.is_a?(TokenType)
stack.push(op1 == TokenType::UNDEFINED)
else
stack.push(false)
end
end
add_operator('now') do |_parser, stack, _context|
stack.push((Time.now.to_f * 1000).to_i)
end
add_operator('and') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
op1 = false if op1 == TokenType::UNDEFINED
op2 = false if op2 == TokenType::UNDEFINED
raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1) && Utils.boolean?(op2)
stack.push(op1 && op2)
end
add_operator('or') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
op1 = false if op1 == TokenType::UNDEFINED
op2 = false if op2 == TokenType::UNDEFINED
raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1) && Utils.boolean?(op2)
stack.push(op1 || op2)
end
add_operator('ne') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
op1 = false if op1 == TokenType::UNDEFINED
op2 = false if op2 == TokenType::UNDEFINED
stack.push(op1 != op2)
end
add_operator('eq') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
op1 = false if op1 == TokenType::UNDEFINED
op2 = false if op2 == TokenType::UNDEFINED
stack.push(op1 == op2)
end
add_operator('not') do |_parser, stack, _context|
op1 = stack.pop
op1 = false if op1 == TokenType::UNDEFINED
raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1)
stack.push(!op1)
end
add_operator('ifThen') do |_parser, stack, _context|
condition_expression = stack.pop
true_expression = stack.pop
false_expression = stack.pop
raise ArgumentError, 'should be boolean' unless Utils.boolean?(condition_expression)
if condition_expression
stack.push(true_expression)
else
stack.push(false_expression)
end
end
add_operator('inArray') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
if op2.is_a?(Array)
stack.push(op2.include?(op1))
else
stack.push(false)
end
end
add_operator('md5') do |_parser, stack, _context|
op1 = stack.pop
if op1.is_a?(String)
stack.push(Digest::MD5.hexdigest(op1))
else
stack.push(TokenType::UNDEFINED)
end
end
add_operator('concat') do |_parser, stack, _context|
op1 = stack.pop
op2 = stack.pop
if op1.is_a?(String) && op2.is_a?(String)
stack.push("#{op1}#{op2}")
else
stack.push(TokenType::UNDEFINED)
end
end
add_operator('b64d') do |_parser, stack, _context|
op1 = stack.pop
if op1.is_a?(String)
decoded = Base64.decode64(op1)
decoded = decoded.force_encoding('UTF-8')
stack.push(decoded)
else
stack.push(TokenType::UNDEFINED)
end
end
add_operator('tsToNum') do |_parser, stack, _context|
datetime = stack.pop
if datetime.is_a?(DateTime)
stack.push(datetime.to_time.to_i)
else
stack.push(TokenType::UNDEFINED)
end
end
end
|