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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
#!/usr/bin/env python
"""
Generate the function list for the basic NocScript functions
"""
import re
import os
import sys
from mako.template import Template
#############################################################################
# This is the interesting part: Add new functions in here
#
# Notes:
# - Lines starting with # are considered comments, and will be removed from
# the output
# - C++ comments will be copied onto the generated file if inside functions
# - Docstrings start with //! and are required
# - Function signature is RETURN_TYPE NAME(ARG_TYPE1, ARG_TYPE2, ...)
# - Function body is valid C++
# - If your function requires special includes, put them in INCLUDE_LIST
# - End of functions is delimited by s/^}/, so take care with the indents!
# - Use these substitutions:
# - ${RETURN}(...): Create a valid return value
# - ${args[n]}: Access the n-th argument
#
INCLUDE_LIST = """
#include <boost/math/special_functions/round.hpp>
#include <boost/thread/thread.hpp>
"""
FUNCTION_LIST = """
CATEGORY: Math Functions
//! Returns x + y
INT ADD(INT, INT)
{
${RETURN}(${args[0]} + ${args[1]});
}
//! Returns x + y
DOUBLE ADD(DOUBLE, DOUBLE)
{
${RETURN}(${args[0]} + ${args[1]});
}
//! Returns x * y
DOUBLE MULT(DOUBLE, DOUBLE)
{
${RETURN}(${args[0]} * ${args[1]});
}
//! Returns x * y
INT MULT(INT, INT)
{
${RETURN}(${args[0]} * ${args[1]});
}
//! Returns x / y
DOUBLE DIV(DOUBLE, DOUBLE)
{
${RETURN}(${args[0]} / ${args[1]});
}
//! Returns true if x <= y (Less or Equal)
BOOL LE(INT, INT)
{
${RETURN}(bool(${args[0]} <= ${args[1]}));
}
//! Returns true if x <= y (Less or Equal)
BOOL LE(DOUBLE, DOUBLE)
{
${RETURN}(bool(${args[0]} <= ${args[1]}));
}
//! Returns true if x >= y (Greater or Equal)
BOOL GE(INT, INT)
{
${RETURN}(bool(${args[0]} >= ${args[1]}));
}
//! Returns true if x >= y (Greater or Equal)
BOOL GE(DOUBLE, DOUBLE)
{
${RETURN}(bool(${args[0]} >= ${args[1]}));
}
//! Returns true if x < y (Less Than)
BOOL LT(INT, INT)
{
${RETURN}(bool(${args[0]} < ${args[1]}));
}
//! Returns true if x > y (Greater Than)
BOOL GT(INT, INT)
{
${RETURN}(bool(${args[0]} > ${args[1]}));
}
//! Returns true if x < y (Less Than)
BOOL LT(DOUBLE, DOUBLE)
{
${RETURN}(bool(${args[0]} < ${args[1]}));
}
//! Returns true if x > y (Greater Than)
BOOL GT(DOUBLE, DOUBLE)
{
${RETURN}(bool(${args[0]} > ${args[1]}));
}
//! Round x and return it as an integer
INT IROUND(DOUBLE)
{
${RETURN}(int(boost::math::iround(${args[0]})));
}
//! Returns true if x is a power of 2
BOOL IS_PWR_OF_2(INT)
{
if (${args[0]} < 0) return ${FALSE};
int i = ${args[0]};
while ( (i & 1) == 0 and (i > 1) ) {
i >>= 1;
}
${RETURN}(bool(i == 1));
}
//! Returns floor(log2(x)).
INT LOG2(INT)
{
if (${args[0]} < 0) {
throw uhd::runtime_error(str(
boost::format("In NocScript function ${func_name}: Cannot calculate log2() of negative number.")
));
}
int power_value = ${args[0]};
int log2_value = 0;
while ( (power_value & 1) == 0 and (power_value > 1) ) {
power_value >>= 1;
log2_value++;
}
${RETURN}(log2_value);
}
//! Returns x % y
INT MODULO(INT, INT)
{
${RETURN}(${args[0]} % ${args[1]});
}
//! Returns true if x == y
BOOL EQUAL(INT, INT)
{
${RETURN}(bool(${args[0]} == ${args[1]}));
}
//! Returns true if x == y
BOOL EQUAL(DOUBLE, DOUBLE)
{
${RETURN}(bool(${args[0]} == ${args[1]}));
}
//! Returns true if x == y
BOOL EQUAL(STRING, STRING)
{
${RETURN}(bool(${args[0]} == ${args[1]}));
}
CATEGORY: Bitwise Operations
//! Returns x >> y
INT SHIFT_RIGHT(INT, INT)
{
${RETURN}(${args[0]} >> ${args[1]});
}
//! Returns x << y
INT SHIFT_LEFT(INT, INT)
{
${RETURN}(${args[0]} << ${args[1]});
}
//! Returns x & y
INT BITWISE_AND(INT, INT)
{
${RETURN}(${args[0]} & ${args[1]});
}
//! Returns x | y
INT BITWISE_OR(INT, INT)
{
${RETURN}(${args[0]} | ${args[1]});
}
//! Returns x ^ y
INT BITWISE_XOR(INT, INT)
{
${RETURN}(${args[0]} ^ ${args[1]});
}
CATEGORY: Boolean Logic
//! Returns x xor y.
BOOL XOR(BOOL, BOOL)
{
${RETURN}(${args[0]} xor ${args[1]});
}
//! Returns !x
BOOL NOT(BOOL)
{
${RETURN}(not ${args[0]});
}
//! Always returns true
BOOL TRUE()
{
return ${TRUE};
}
//! Always returns false
BOOL FALSE()
{
return ${FALSE};
}
CATEGORY: Conditional Execution
//! Executes x, if true, execute y. Returns true if x is true.
BOOL IF(BOOL, BOOL)
{
if (${args[0]}) {
${args[1]};
${RETURN}(true);
}
${RETURN}(false);
}
//! Executes x, if true, execute y, otherwise, execute z. Returns true if x is true.
BOOL IF_ELSE(BOOL, BOOL, BOOL)
{
if (${args[0]}) {
${args[1]};
${RETURN}(true);
} else {
${args[2]};
}
${RETURN}(false);
}
CATEGORY: Execution Control
//! Sleep for x seconds. Fractions are allowed. Millisecond accuracy.
BOOL SLEEP(DOUBLE)
{
int ms = ${args[0]} / 1000;
boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
${RETURN}(true);
}
"""
# End of interesting part. The rest will take this and turn into a C++
# header file.
#############################################################################
HEADER = """<% import time %>//
///////////////////////////////////////////////////////////////////////
// This file was generated by ${file} on ${time.strftime("%c")}
///////////////////////////////////////////////////////////////////////
// Copyright 2015 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
/****************************************************************************
* This file is autogenerated! Any manual changes in here will be
* overwritten by calling nocscript_gen_basic_funcs.py!
***************************************************************************/
#include "expression.hpp"
#include "function_table.hpp"
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <boost/assign/list_of.hpp>
${INCLUDE_LIST}
#ifndef INCLUDED_LIBUHD_RFNOC_NOCSCRIPT_BASICFUNCS_HPP
#define INCLUDED_LIBUHD_RFNOC_NOCSCRIPT_BASICFUNCS_HPP
namespace uhd { namespace rfnoc { namespace nocscript {
"""
# Not a Mako template:
FOOTER="""
}}} /* namespace uhd::rfnoc::nocscript */
#endif /* INCLUDED_LIBUHD_RFNOC_NOCSCRIPT_BASICFUNCS_HPP */
"""
# Not a Mako template:
FUNC_TEMPLATE = """
expression_literal {NAME}(expression_container::expr_list_type &{ARGS})
{BODY}
"""
REGISTER_MACRO_TEMPLATE = """#define _REGISTER_ALL_FUNCS()${registry}
"""
REGISTER_COMMANDS_TEMPLATE = """
% if len(arglist):
expression_function::argtype_list_type ${func_name}_args = boost::assign::list_of
% for this_type in arglist:
(expression::TYPE_${this_type})
% endfor
;
% else:
expression_function::argtype_list_type ${func_name}_args;
% endif
register_function(
"${name}",
boost::bind(&${func_name}, _1),
expression::TYPE_${retval},
${func_name}_args
);"""
DOXY_TEMPLATE = """/*! \page page_nocscript_funcs NocScript Function Reference
% for cat, func_by_name in func_list_tree.iteritems():
- ${cat}
% for func_name, func_info_list in func_by_name.iteritems():
- ${func_name}: ${func_info_list[0]['docstring']}
% for func_info in func_info_list:
- ${func_info['arglist']} -> ${func_info['retval']}
% endfor
% endfor
% endfor
*/
"""
def parse_tmpl(_tmpl_text, **kwargs):
return Template(_tmpl_text).render(**kwargs)
def make_cxx_func_name(func_dict):
"""
Creates a unique C++ function name from a function description
"""
return "{name}__{retval}__{arglist}".format(
name=func_dict['name'],
retval=func_dict['retval'],
arglist="_".join(func_dict['arglist'])
)
def make_cxx_func_body(func_dict):
"""
Formats the function body properly
"""
type_lookup_methods = {
'INT': 'get_int',
'DOUBLE': 'get_double',
'BOOL': 'get_bool',
'STRING': 'get_string',
}
args_lookup = []
for idx, arg_type in enumerate(func_dict['arglist']):
args_lookup.append("args[{idx}]->eval().{getter}()".format(idx=idx, getter=type_lookup_methods[arg_type]))
return parse_tmpl(
func_dict['body'],
args=args_lookup,
FALSE='expression_literal(false)',
TRUE='expression_literal(true)',
RETURN='return expression_literal',
**func_dict
)
def prep_function_list():
"""
- Remove all comments
- Split the function list into individual functions
- Split the functions into return value, name, argument list and body
"""
comment_remove_re = re.compile(r'^\s*#.*$', flags=re.MULTILINE)
func_list_wo_comments = comment_remove_re.sub('', FUNCTION_LIST)
func_splitter_re = re.compile(r'(?<=^})\s*$', flags=re.MULTILINE)
func_list_split = func_splitter_re.split(func_list_wo_comments)
func_list_split = [x.strip() for x in func_list_split if len(x.strip())]
func_list = []
last_category = ''
for func in func_list_split:
split_regex = r'(^CATEGORY: (?P<cat>[^\n]*)\s*)?' \
r'//!(?P<docstring>[^\n]*)\s*' + \
r'(?P<retval>[A-Z][A-Z0-9_]*)\s+' + \
r'(?P<funcname>[A-Z][A-Z0-9_]*)\s*\((?P<arglist>[^\)]*)\)\s*' + \
r'(?P<funcbody>^{.*)'
split_re = re.compile(split_regex, flags=re.MULTILINE|re.DOTALL)
mo = split_re.match(func)
if mo.group('cat'):
last_category = mo.group('cat').strip()
func_dict = {
'docstring': mo.group('docstring').strip(),
'name': mo.group('funcname'),
'retval': mo.group('retval'),
'arglist': [x.strip() for x in mo.group('arglist').split(',') if len(x.strip())],
'body': mo.group('funcbody'),
'category': last_category,
}
func_dict['func_name'] = make_cxx_func_name(func_dict)
func_list.append(func_dict)
return func_list
def write_function_header(output_filename):
"""
Create the .hpp file that defines all the NocScript functions in C++.
"""
func_list = prep_function_list()
# Step 1: Write the prototypes
func_prototypes = ''
registry_commands = ''
for func in func_list:
func_prototypes += FUNC_TEMPLATE.format(
NAME=func['func_name'],
BODY=make_cxx_func_body(func),
ARGS="args" if len(func['arglist']) else ""
)
registry_commands += parse_tmpl(
REGISTER_COMMANDS_TEMPLATE,
**func
)
# Step 2: Write the registry process
register_func = parse_tmpl(REGISTER_MACRO_TEMPLATE, registry=registry_commands)
register_func = register_func.replace('\n', ' \\\n')
# Final step: Join parts and write to file
full_file = "\n".join((
parse_tmpl(HEADER, file = os.path.basename(__file__), INCLUDE_LIST=INCLUDE_LIST),
func_prototypes,
register_func,
FOOTER,
))
open(output_filename, 'w').write(full_file)
def write_manual_file(output_filename):
"""
Write the Doxygen file for the NocScript functions.
"""
func_list = prep_function_list()
func_list_tree = {}
for func in func_list:
if not func_list_tree.has_key(func['category']):
func_list_tree[func['category']] = {}
if not func_list_tree[func['category']].has_key(func['name']):
func_list_tree[func['category']][func['name']] = []
func_list_tree[func['category']][func['name']].append(func)
open(output_filename, 'w').write(parse_tmpl(DOXY_TEMPLATE, func_list_tree=func_list_tree))
def main():
if len(sys.argv) < 2:
print("No output file specified!")
exit(1)
outfile = sys.argv[1]
if os.path.splitext(outfile)[1] == '.dox':
write_manual_file(outfile)
else:
write_function_header(outfile)
if __name__ == "__main__":
main()
|