Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / glsl / s_expression.cpp
1 /* -*- c++ -*- */
2 /*
3  * Copyright © 2010 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <assert.h>
26 #include "s_expression.h"
27
28 s_symbol::s_symbol(const char *str, size_t n)
29 {
30    /* Assume the given string is already nul-terminated and in memory that
31     * will live as long as this node.
32     */
33    assert(str[n] == '\0');
34    this->str = str;
35 }
36
37 s_list::s_list()
38 {
39 }
40
41 static void
42 skip_whitespace(const char *&src, char *&symbol_buffer)
43 {
44    size_t n = strspn(src, " \v\t\r\n");
45    src += n;
46    symbol_buffer += n;
47    /* Also skip Scheme-style comments: semi-colon 'til end of line */
48    if (src[0] == ';') {
49       n = strcspn(src, "\n");
50       src += n;
51       symbol_buffer += n;
52       skip_whitespace(src, symbol_buffer);
53    }
54 }
55
56 static s_expression *
57 read_atom(void *ctx, const char *&src, char *&symbol_buffer)
58 {
59    s_expression *expr = NULL;
60
61    skip_whitespace(src, symbol_buffer);
62
63    size_t n = strcspn(src, "( \v\t\r\n);");
64    if (n == 0)
65       return NULL; // no atom
66
67    // Check if the atom is a number.
68    char *float_end = NULL;
69    double f = glsl_strtod(src, &float_end);
70    if (float_end != src) {
71       char *int_end = NULL;
72       int i = strtol(src, &int_end, 10);
73       // If strtod matched more characters, it must have a decimal part
74       if (float_end > int_end)
75          expr = new(ctx) s_float(f);
76       else
77          expr = new(ctx) s_int(i);
78    } else {
79       // Not a number; return a symbol.
80       symbol_buffer[n] = '\0';
81       expr = new(ctx) s_symbol(symbol_buffer, n);
82    }
83
84    src += n;
85    symbol_buffer += n;
86
87    return expr;
88 }
89
90 static s_expression *
91 __read_expression(void *ctx, const char *&src, char *&symbol_buffer)
92 {
93    s_expression *atom = read_atom(ctx, src, symbol_buffer);
94    if (atom != NULL)
95       return atom;
96
97    skip_whitespace(src, symbol_buffer);
98    if (src[0] == '(') {
99       ++src;
100       ++symbol_buffer;
101
102       s_list *list = new(ctx) s_list;
103       s_expression *expr;
104
105       while ((expr = __read_expression(ctx, src, symbol_buffer)) != NULL) {
106          list->subexpressions.push_tail(expr);
107       }
108       skip_whitespace(src, symbol_buffer);
109       if (src[0] != ')') {
110          printf("Unclosed expression (check your parenthesis).\n");
111          return NULL;
112       }
113       ++src;
114       ++symbol_buffer;
115       return list;
116    }
117    return NULL;
118 }
119
120 s_expression *
121 s_expression::read_expression(void *ctx, const char *&src)
122 {
123    assert(src != NULL);
124
125    /* When we encounter a Symbol, we need to save a nul-terminated copy of
126     * the string.  However, ralloc_strndup'ing every individual Symbol is
127     * extremely expensive.  We could avoid this by simply overwriting the
128     * next character (guaranteed to be whitespace, parens, or semicolon) with
129     * a nul-byte.  But overwriting non-whitespace would mess up parsing.
130     *
131     * So, just copy the whole buffer ahead of time.  Walk both, leaving the
132     * original source string unmodified, and altering the copy to contain the
133     * necessary nul-bytes whenever we encounter a symbol.
134     */
135    char *symbol_buffer = ralloc_strdup(ctx, src);
136    return __read_expression(ctx, src, symbol_buffer);
137 }
138
139 void s_int::print()
140 {
141    printf("%d", this->val);
142 }
143
144 void s_float::print()
145 {
146    printf("%f", this->val);
147 }
148
149 void s_symbol::print()
150 {
151    printf("%s", this->str);
152 }
153
154 void s_list::print()
155 {
156    printf("(");
157    foreach_iter(exec_list_iterator, it, this->subexpressions) {
158       s_expression *expr = (s_expression*) it.get();
159       expr->print();
160       if (!expr->next->is_tail_sentinel())
161          printf(" ");
162    }
163    printf(")");
164 }
165
166 // --------------------------------------------------
167
168 bool
169 s_pattern::match(s_expression *expr)
170 {
171    switch (type)
172    {
173    case EXPR:   *p_expr = expr; break;
174    case LIST:   if (expr->is_list())   *p_list   = (s_list *)   expr; break;
175    case SYMBOL: if (expr->is_symbol()) *p_symbol = (s_symbol *) expr; break;
176    case NUMBER: if (expr->is_number()) *p_number = (s_number *) expr; break;
177    case INT:    if (expr->is_int())    *p_int    = (s_int *)    expr; break;
178    case STRING:
179       s_symbol *sym = SX_AS_SYMBOL(expr);
180       if (sym != NULL && strcmp(sym->value(), literal) == 0)
181          return true;
182       return false;
183    };
184
185    return *p_expr == expr;
186 }
187
188 bool
189 s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial)
190 {
191    s_list *list = SX_AS_LIST(top);
192    if (list == NULL)
193       return false;
194
195    unsigned i = 0;
196    foreach_iter(exec_list_iterator, it, list->subexpressions) {
197       if (i >= n)
198          return partial; /* More actual items than the pattern expected */
199
200       s_expression *expr = (s_expression *) it.get();
201       if (expr == NULL || !pattern[i].match(expr))
202          return false;
203
204       i++;
205    }
206
207    if (i < n)
208       return false; /* Less actual items than the pattern expected */
209
210    return true;
211 }