summaryrefslogtreecommitdiffstats
path: root/src/glsl/pp/sl_pp_define.c
diff options
context:
space:
mode:
authorMichal Krol <[email protected]>2009-06-22 09:05:29 +0200
committerMichal Krol <[email protected]>2009-09-07 10:11:47 +0200
commit6a11d4150cfcdd646c17f8b365b5481c2c583208 (patch)
tree9037f4dc93262d269ba126788a0d5691bb10b4cd /src/glsl/pp/sl_pp_define.c
parent5e8e3cddae9b2797cfa525c643c701debe2f4c04 (diff)
glsl: Implement macro expansion.
Diffstat (limited to 'src/glsl/pp/sl_pp_define.c')
-rw-r--r--src/glsl/pp/sl_pp_define.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/glsl/pp/sl_pp_define.c b/src/glsl/pp/sl_pp_define.c
index 5ce0f0551b8..39d14350641 100644
--- a/src/glsl/pp/sl_pp_define.c
+++ b/src/glsl/pp/sl_pp_define.c
@@ -48,6 +48,8 @@ _parse_formal_args(const struct sl_pp_token_info *input,
{
struct sl_pp_macro_formal_arg **arg;
+ macro->num_args = 0;
+
skip_whitespace(input, first, last);
if (*first < last) {
if (input[*first].token == SL_PP_RPAREN) {
@@ -78,6 +80,8 @@ _parse_formal_args(const struct sl_pp_token_info *input,
(**arg).next = NULL;
arg = &(**arg).next;
+ macro->num_args++;
+
skip_whitespace(input, first, last);
if (*first < last) {
if (input[*first].token == SL_PP_COMMA) {
@@ -104,7 +108,12 @@ sl_pp_process_define(struct sl_pp_context *context,
unsigned int last,
struct sl_pp_macro *macro)
{
+ unsigned int i;
+ unsigned int body_len;
+ unsigned int j;
+
macro->name = -1;
+ macro->num_args = -1;
macro->arg = NULL;
macro->body = NULL;
macro->next = NULL;
@@ -131,26 +140,25 @@ sl_pp_process_define(struct sl_pp_context *context,
}
}
- /* Trim whitespace from the left side. */
- skip_whitespace(input, &first, last);
+ /* Calculate body size, trim out whitespace, make room for EOF. */
+ body_len = 1;
+ for (i = first; i < last; i++) {
+ if (input[i].token != SL_PP_WHITESPACE) {
+ body_len++;
+ }
+ }
- /* Trom whitespace from the right side. */
- while (first < last && input[last - 1].token == SL_PP_WHITESPACE) {
- last--;
+ macro->body = malloc(sizeof(struct sl_pp_token_info) * body_len);
+ if (!macro->body) {
+ return -1;
}
- /* All that is left between first and last is the macro definition. */
- macro->body_len = last - first;
- if (macro->body_len) {
- macro->body = malloc(sizeof(struct sl_pp_token_info) * macro->body_len);
- if (!macro->body) {
- return -1;
+ for (j = 0, i = first; i < last; i++) {
+ if (input[i].token != SL_PP_WHITESPACE) {
+ macro->body[j++] = input[i];
}
-
- memcpy(macro->body,
- &input[first],
- sizeof(struct sl_pp_token_info) * macro->body_len);
}
+ macro->body[j++].token = SL_PP_EOF;
return 0;
}