revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / mesa / src / glsl / s_expression.cpp
blobc5b092b65e4a3caabb5ed01450abd264ee94b918
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
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:
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.
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.
25 #include <assert.h>
26 #include "s_expression.h"
28 #if defined(__AROS__)
29 #include <aros/debug.h>
30 #define printf(fmt, ...) bug(fmt, ##__VA_ARGS__)
31 #endif
33 s_symbol::s_symbol(const char *tmp, size_t n)
35 this->str = ralloc_strndup (this, tmp, n);
36 assert(this->str != NULL);
39 s_list::s_list()
43 static void
44 skip_whitespace(const char *& src)
46 src += strspn(src, " \v\t\r\n");
47 /* Also skip Scheme-style comments: semi-colon 'til end of line */
48 if (src[0] == ';') {
49 src += strcspn(src, "\n");
50 skip_whitespace(src);
54 static s_expression *
55 read_atom(void *ctx, const char *& src)
57 s_expression *expr = NULL;
59 skip_whitespace(src);
61 size_t n = strcspn(src, "( \v\t\r\n);");
62 if (n == 0)
63 return NULL; // no atom
65 // Check if the atom is a number.
66 char *float_end = NULL;
67 double f = glsl_strtod(src, &float_end);
68 if (float_end != src) {
69 char *int_end = NULL;
70 int i = strtol(src, &int_end, 10);
71 // If strtod matched more characters, it must have a decimal part
72 if (float_end > int_end)
73 expr = new(ctx) s_float(f);
74 else
75 expr = new(ctx) s_int(i);
76 } else {
77 // Not a number; return a symbol.
78 expr = new(ctx) s_symbol(src, n);
81 src += n;
83 return expr;
86 s_expression *
87 s_expression::read_expression(void *ctx, const char *&src)
89 assert(src != NULL);
91 s_expression *atom = read_atom(ctx, src);
92 if (atom != NULL)
93 return atom;
95 skip_whitespace(src);
96 if (src[0] == '(') {
97 ++src;
99 s_list *list = new(ctx) s_list;
100 s_expression *expr;
102 while ((expr = read_expression(ctx, src)) != NULL) {
103 list->subexpressions.push_tail(expr);
105 skip_whitespace(src);
106 if (src[0] != ')') {
107 printf("Unclosed expression (check your parenthesis).\n");
108 return NULL;
110 ++src;
111 return list;
113 return NULL;
116 void s_int::print()
118 printf("%d", this->val);
121 void s_float::print()
123 printf("%f", this->val);
126 void s_symbol::print()
128 printf("%s", this->str);
131 void s_list::print()
133 printf("(");
134 foreach_iter(exec_list_iterator, it, this->subexpressions) {
135 s_expression *expr = (s_expression*) it.get();
136 expr->print();
137 if (!expr->next->is_tail_sentinel())
138 printf(" ");
140 printf(")");
143 // --------------------------------------------------
145 bool
146 s_pattern::match(s_expression *expr)
148 switch (type)
150 case EXPR: *p_expr = expr; break;
151 case LIST: if (expr->is_list()) *p_list = (s_list *) expr; break;
152 case SYMBOL: if (expr->is_symbol()) *p_symbol = (s_symbol *) expr; break;
153 case NUMBER: if (expr->is_number()) *p_number = (s_number *) expr; break;
154 case INT: if (expr->is_int()) *p_int = (s_int *) expr; break;
155 case STRING:
156 s_symbol *sym = SX_AS_SYMBOL(expr);
157 if (sym != NULL && strcmp(sym->value(), literal) == 0)
158 return true;
159 return false;
162 return *p_expr == expr;
165 bool
166 s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial)
168 s_list *list = SX_AS_LIST(top);
169 if (list == NULL)
170 return false;
172 unsigned i = 0;
173 foreach_iter(exec_list_iterator, it, list->subexpressions) {
174 if (i >= n)
175 return partial; /* More actual items than the pattern expected */
177 s_expression *expr = (s_expression *) it.get();
178 if (expr == NULL || !pattern[i].match(expr))
179 return false;
181 i++;
184 if (i < n)
185 return false; /* Less actual items than the pattern expected */
187 return true;