ralloc: a new MIT-licensed recursive memory allocator.
[mesa.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 <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <assert.h>
29 #include "s_expression.h"
30
31 s_symbol::s_symbol(const char *tmp, size_t n)
32 {
33 this->str = ralloc_strndup (this, tmp, n);
34 assert(this->str != NULL);
35 }
36
37 s_list::s_list()
38 {
39 }
40
41 static void
42 skip_whitespace(const char *& src)
43 {
44 src += strspn(src, " \v\t\r\n");
45 /* Also skip Scheme-style comments: semi-colon 'til end of line */
46 if (src[0] == ';') {
47 src += strcspn(src, "\n");
48 skip_whitespace(src);
49 }
50 }
51
52 static s_expression *
53 read_atom(void *ctx, const char *& src)
54 {
55 s_expression *expr = NULL;
56
57 skip_whitespace(src);
58
59 size_t n = strcspn(src, "( \v\t\r\n);");
60 if (n == 0)
61 return NULL; // no atom
62
63 // Check if the atom is a number.
64 char *float_end = NULL;
65 double f = glsl_strtod(src, &float_end);
66 if (float_end != src) {
67 char *int_end = NULL;
68 int i = strtol(src, &int_end, 10);
69 // If strtod matched more characters, it must have a decimal part
70 if (float_end > int_end)
71 expr = new(ctx) s_float(f);
72 else
73 expr = new(ctx) s_int(i);
74 } else {
75 // Not a number; return a symbol.
76 expr = new(ctx) s_symbol(src, n);
77 }
78
79 src += n;
80
81 return expr;
82 }
83
84 s_expression *
85 s_expression::read_expression(void *ctx, const char *&src)
86 {
87 assert(src != NULL);
88
89 s_expression *atom = read_atom(ctx, src);
90 if (atom != NULL)
91 return atom;
92
93 skip_whitespace(src);
94 if (src[0] == '(') {
95 ++src;
96
97 s_list *list = new(ctx) s_list;
98 s_expression *expr;
99
100 while ((expr = read_expression(ctx, src)) != NULL) {
101 list->subexpressions.push_tail(expr);
102 }
103 skip_whitespace(src);
104 if (src[0] != ')') {
105 printf("Unclosed expression (check your parenthesis).\n");
106 return NULL;
107 }
108 ++src;
109 return list;
110 }
111 return NULL;
112 }
113
114 void s_int::print()
115 {
116 printf("%d", this->val);
117 }
118
119 void s_float::print()
120 {
121 printf("%f", this->val);
122 }
123
124 void s_symbol::print()
125 {
126 printf("%s", this->str);
127 }
128
129 void s_list::print()
130 {
131 printf("(");
132 foreach_iter(exec_list_iterator, it, this->subexpressions) {
133 s_expression *expr = (s_expression*) it.get();
134 expr->print();
135 if (!expr->next->is_tail_sentinel())
136 printf(" ");
137 }
138 printf(")");
139 }
140
141 // --------------------------------------------------
142
143 bool
144 s_pattern::match(s_expression *expr)
145 {
146 switch (type)
147 {
148 case EXPR: *p_expr = expr; break;
149 case LIST: if (expr->is_list()) *p_list = (s_list *) expr; break;
150 case SYMBOL: if (expr->is_symbol()) *p_symbol = (s_symbol *) expr; break;
151 case NUMBER: if (expr->is_number()) *p_number = (s_number *) expr; break;
152 case INT: if (expr->is_int()) *p_int = (s_int *) expr; break;
153 case STRING:
154 s_symbol *sym = SX_AS_SYMBOL(expr);
155 if (sym != NULL && strcmp(sym->value(), literal) == 0)
156 return true;
157 return false;
158 };
159
160 return *p_expr == expr;
161 }
162
163 bool
164 s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial)
165 {
166 s_list *list = SX_AS_LIST(top);
167 if (list == NULL)
168 return false;
169
170 unsigned i = 0;
171 foreach_iter(exec_list_iterator, it, list->subexpressions) {
172 if (i >= n)
173 return partial; /* More actual items than the pattern expected */
174
175 s_expression *expr = (s_expression *) it.get();
176 if (expr == NULL || !pattern[i].match(expr))
177 return false;
178
179 i++;
180 }
181
182 if (i < n)
183 return false; /* Less actual items than the pattern expected */
184
185 return true;
186 }