Go to the first, previous, next, last section, table of contents.
The code produced by this example will create a two dimensional array of 100x100 and initialize all the elements to zero.
type_node * tn = block::parse_type("double [100][100]"); block A(block::new_sym(tn, "A")); block i(block::new_sym(cst_int, "i")); block j(block::new_sym(cst_int, "j")); block code(block::FOR(i, block(0), block(99), block::FOR(j, block(0), block(99), block(A[i][j] = block(0)))));
The code produced by this example will sort a one-dimensional array of N doubles. Both the array and the constant variable N are provided.
var_sym * symA = (a one-dimensional array of N doubles) var_sym * symN = (the integer value N) block A(symA); block N(symN); block i(block::new_sym(cst_int, "i")); block j(block::new_sym(cst_int, "j")); block tmp(block::new_sym(cst_double, "tmp")); block code(block::FOR(i, block(0), block(N - block(2)), block::FOR(j, block(i + block(1)), block(N - block(1)), block::IF(block(A[i] > A[j]), block(block(tmp = A[i]), block(A[i] = A[j]), block(A[j] = tmp))))));
A pointer to a head of a singly linked list and the value of the
value
field that needs to be matched are given as input.
The generated code will search through the linked list until it finds
an element with the value
filed matching the value and a pointer
to that element is returned. If no such element is found or if the list
is empty, a NULL pointer is returned.
type_node * ptr_type = (pointer to element type where "next" is the pointer to next field "value" is the value to be checked) var_sym * symHead = (head of the list) var_sym * symVal = (value to be found) block head(symHead); block val(symVal); block tmpptr(block::new_sym(ptr_type, "tmp_ptr")); block code(block(block(tmpptr = head), block::WHILE(tmpptr, block::IF(block(tmpptr.dref().field("value") == val)), block::RETURN(tmpptr)), block(tmpptr = tmpptr.dref().field("next"))), block::return(block(0))));
Note: The current release of the builder does not support
the A.dref().field()
sequence of commands. It only supports
obtaining a structure field of a variable, but does not support
structure field access on any computed values such as dereferencing.
So this example will not work with the current release.
Go to the first, previous, next, last section, table of contents.