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