aboutsummaryrefslogtreecommitdiff
path: root/plugins/xB/eval
blob: 24e405059c7b8c0acea9e899ed86f7d20ea5557e (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/awk -f
#
# xB eval plugin, LISP-like expression evaluator
#
# Copyright 2013, 2014 Přemysl Eric Janouch
# See the file LICENSE for licensing information.
#

BEGIN \
{
	RS = "\r"
	ORS = "\r\n"
	IGNORECASE = 1
	srand()

	prefix = get_config("prefix")

	print "ZYKLONB register"
	fflush("")

	# All functions have to be in this particular array
	min_args["int"]    = 1
	min_args["+"]      = 1
	min_args["-"]      = 1
	min_args["*"]      = 1
	min_args["/"]      = 1
	min_args["%"]      = 1
	min_args["^"]      = 1
	min_args["**"]     = 1
	min_args["exp"]    = 1
	min_args["sin"]    = 1
	min_args["cos"]    = 1
	min_args["atan2"]  = 2
	min_args["log"]    = 1
	min_args["rand"]   = 0
	min_args["sqrt"]   = 1

	min_args["pi"]     = 0
	min_args["e"]      = 0

	min_args["min"]    = 1
	min_args["max"]    = 1

	# Whereas here their presence is only optional
	max_args["int"]    = 1
	max_args["sin"]    = 1
	max_args["cos"]    = 1
	max_args["atan2"]  = 2
	max_args["log"]    = 1
	max_args["rand"]   = 0
	max_args["sqrt"]   = 1

	max_args["pi"]     = 0
	max_args["e"]      = 0
}

{
	parse($0)
}

msg_command == "PRIVMSG" \
{
	# Context = either channel or user nickname
	match(msg_prefix, /^[^!]+/)
	ctx = substr(msg_prefix, RSTART, RLENGTH)
	if (msg_param[0] ~ /^[#&!+]/)
	{
		ctx_quote = ctx ": "
		ctx = msg_param[0]
	}
	else
		ctx_quote = ""


	if (substr(msg_param[1], 1, length(prefix)) == prefix)
	{
		keyword = "eval"
		text = substr(msg_param[1], 1 + length(prefix))
	 	if (match(text, "^" keyword "([^A-Za-z0-9].*|$)"))
			process_request(substr(text, 1 + length(keyword)))
	}
}

{
	fflush("")
}

function pmrespond (text)
{
	print "PRIVMSG " ctx " :" ctx_quote text
}

function process_request (input,    res, x)
{
	delete funs
	delete accumulator
	delete n_args

	res = ""
	fun_top = 0
	funs[0] = ""
	accumulator[0] = 0
	n_args[0] = 0

	if (match(input, "^[ \t]*"))
		input = substr(input, RLENGTH + 1)
	if (input == "")
		res = "expression missing"

	while (res == "" && input != "") {
		if (match(input, "^-?[0-9]+\\.?[0-9]*")) {
			x = substr(input, RSTART, RLENGTH)
			input = substr(input, RLENGTH + 1)

			match(input, "^ *")
			input = substr(input, RLENGTH + 1)

			res = process_argument(x)
		} else if (match(input, "^[(]([^ ()]+)")) {
			x = substr(input, RSTART + 1, RLENGTH - 1)
			input = substr(input, RLENGTH + 1)

			match(input, "^ *")
			input = substr(input, RLENGTH + 1)

			if (!(x in min_args)) {
				res = "undefined function '" x "'"
			} else {
				fun_top++
				funs[fun_top] = x
				accumulator[fun_top] = 636363
				n_args[fun_top] = 0
			}
		} else if (match(input, "^[)] *")) {
			input = substr(input, RLENGTH + 1)
			res = process_end()
		} else
			res = "invalid input at '" substr(input, 1, 10) "...'"
	}

	if (res == "") {
		if (fun_top != 0)
			res = "unclosed '" funs[fun_top] "'"
		else if (n_args[0] != 1)
			res = "internal error, expected one result" \
				", got " n_args[0] " instead"
	}

	if (res == "")
		pmrespond(accumulator[0])
	else
		pmrespond(res)
}

function process_argument (arg)
{
	if (fun_top == 0) {
		if (n_args[0]++ != 0)
			return "too many results, I only expect one"

		accumulator[0] = arg
		return ""
	}

	fun = funs[fun_top]
	if (fun in max_args && max_args[fun] <= n_args[fun_top])
		return "too many operands for " fun

	if (fun == "int") {
		accumulator[fun_top] = int(arg)
	} else if (fun == "+") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else
			accumulator[fun_top] += arg
	} else if (fun == "-") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else
			accumulator[fun_top] -= arg
	} else if (fun == "*") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else
			accumulator[fun_top] *= arg
	} else if (fun == "/") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else if (arg == 0)
			return "division by zero"
		else
			accumulator[fun_top] /= arg
	} else if (fun == "%") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else if (arg == 0)
			return "division by zero"
		else
			accumulator[fun_top] %= arg
	} else if (fun == "^" || fun == "**" || fun == "exp") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else
			accumulator[fun_top] ^= arg
	} else if (fun == "sin") {
		accumulator[fun_top] = sin(arg)
	} else if (fun == "cos") {
		accumulator[fun_top] = cos(arg)
	} else if (fun == "atan2") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else
			accumulator[fun_top] = atan2(accumulator[fun_top], arg)
	} else if (fun == "log") {
		accumulator[fun_top] = log(arg)
	} else if (fun == "rand") {
		# Just for completeness, execution never gets here
	} else if (fun == "sqrt") {
		accumulator[fun_top] = sqrt(arg)
	} else if (fun == "min") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else if (accumulator[fun_top] > arg)
			accumulator[fun_top] = arg
	} else if (fun == "max") {
		if (n_args[fun_top] == 0)
			accumulator[fun_top] = arg
		else if (accumulator[fun_top] < arg)
			accumulator[fun_top] = arg
	} else
		return "internal error, unhandled operands for " fun

	n_args[fun_top]++
	return ""
}

function process_end ()
{
	if (fun_top <= 0)
		return "extraneous ')'"

	fun = funs[fun_top]
	if (!(fun in min_args))
		return "internal error, unhandled ')' for '" fun "'"
	if (min_args[fun] > n_args[fun_top])
		return "not enough operands for '" fun "'"

	# There's no 'init' function to do it in
	if (fun == "rand")
		accumulator[fun_top] = rand()
	else if (fun == "pi")
		accumulator[fun_top] = 3.141592653589793
	else if (fun == "e")
		accumulator[fun_top] = 2.718281828459045

	return process_argument(accumulator[fun_top--])
}

function get_config (key)
{
	print "ZYKLONB get_config :" key
	fflush("")

	getline
	parse($0)
	return msg_param[0]
}

function parse (line,     s, n, id, token)
{
	s = 1
	id = 0

	# NAWK only uses the first character of RS
	if (line ~ /^\n/)
		line = substr(line, 2)

	msg_prefix = ""
	msg_command = ""
	delete msg_param

	n = match(substr(line, s), / |$/)
	while (n)
	{
		token = substr(line, s, n - 1)
		if (token ~ /^:/)
		{
			if (s == 1)
				msg_prefix = substr(token, 2)
			else
			{
				msg_param[id] = substr(line, s + 1)
				break
			}
		}
		else if (!msg_command)
			msg_command = toupper(token)
		else
			msg_param[id++] = token

		s = s + n
		n = index(substr(line, s), " ")

		if (!n)
		{
		       n = length(substr(line, s)) + 1
		       if (n == 1)
			       break;
		}
	}
}