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