glsl: Refactor AST-to-HIR code handling variable initializers
[mesa.git] / src / glsl / s_expression.cpp
index 26be23ea8faceab79b7b5c1fab623454fdc9f18c..a922a50d3b9dc1f03cafc2c2c3fd6510d75a7452 100644 (file)
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
 #include <assert.h>
 #include "s_expression.h"
 
-s_symbol::s_symbol(const char *tmp)
+s_symbol::s_symbol(const char *tmp, size_t n)
 {
-   this->str = talloc_strdup (this, tmp);
+   this->str = ralloc_strndup (this, tmp, n);
    assert(this->str != NULL);
 }
 
@@ -38,39 +35,47 @@ s_list::s_list()
 {
 }
 
-unsigned
-s_list::length() const
+static void
+skip_whitespace(const char *& src)
 {
-   unsigned i = 0;
-   foreach_iter(exec_list_iterator, it, this->subexpressions) {
-      i++;
+   src += strspn(src, " \v\t\r\n");
+   /* Also skip Scheme-style comments: semi-colon 'til end of line */
+   if (src[0] == ';') {
+      src += strcspn(src, "\n");
+      skip_whitespace(src);
    }
-   return i;
 }
 
 static s_expression *
 read_atom(void *ctx, const char *& src)
 {
-   char buf[101];
-   int n;
-   if (sscanf(src, " %100[^( \v\t\r\n)]%n", buf, &n) != 1)
+   s_expression *expr = NULL;
+
+   skip_whitespace(src);
+
+   size_t n = strcspn(src, "( \v\t\r\n);");
+   if (n == 0)
       return NULL; // no atom
-   src += n;
 
    // Check if the atom is a number.
    char *float_end = NULL;
-   double f = strtod(buf, &float_end);
-   if (float_end != buf) {
+   double f = glsl_strtod(src, &float_end);
+   if (float_end != src) {
       char *int_end = NULL;
-      int i = strtol(buf, &int_end, 10);
+      int i = strtol(src, &int_end, 10);
       // If strtod matched more characters, it must have a decimal part
       if (float_end > int_end)
-        return new(ctx) s_float(f);
-
-      return new(ctx) s_int(i);
+        expr = new(ctx) s_float(f);
+      else
+        expr = new(ctx) s_int(i);
+   } else {
+      // Not a number; return a symbol.
+      expr = new(ctx) s_symbol(src, n);
    }
-   // Not a number; return a symbol.
-   return new(ctx) s_symbol(buf);
+
+   src += n;
+
+   return expr;
 }
 
 s_expression *
@@ -82,10 +87,9 @@ s_expression::read_expression(void *ctx, const char *&src)
    if (atom != NULL)
       return atom;
 
-   char c;
-   int n;
-   if (sscanf(src, " %c%n", &c, &n) == 1 && c == '(') {
-      src += n;
+   skip_whitespace(src);
+   if (src[0] == '(') {
+      ++src;
 
       s_list *list = new(ctx) s_list;
       s_expression *expr;
@@ -93,11 +97,12 @@ s_expression::read_expression(void *ctx, const char *&src)
       while ((expr = read_expression(ctx, src)) != NULL) {
         list->subexpressions.push_tail(expr);
       }
-      if (sscanf(src, " %c%n", &c, &n) != 1 || c != ')') {
+      skip_whitespace(src);
+      if (src[0] != ')') {
         printf("Unclosed expression (check your parenthesis).\n");
         return NULL;
       }
-      src += n;
+      ++src;
       return list;
    }
    return NULL;
@@ -124,8 +129,55 @@ void s_list::print()
    foreach_iter(exec_list_iterator, it, this->subexpressions) {
       s_expression *expr = (s_expression*) it.get();
       expr->print();
-      printf(" ");
+      if (!expr->next->is_tail_sentinel())
+        printf(" ");
    }
    printf(")");
 }
 
+// --------------------------------------------------
+
+bool
+s_pattern::match(s_expression *expr)
+{
+   switch (type)
+   {
+   case EXPR:   *p_expr = expr; break;
+   case LIST:   if (expr->is_list())   *p_list   = (s_list *)   expr; break;
+   case SYMBOL: if (expr->is_symbol()) *p_symbol = (s_symbol *) expr; break;
+   case NUMBER: if (expr->is_number()) *p_number = (s_number *) expr; break;
+   case INT:    if (expr->is_int())    *p_int    = (s_int *)    expr; break;
+   case STRING:
+      s_symbol *sym = SX_AS_SYMBOL(expr);
+      if (sym != NULL && strcmp(sym->value(), literal) == 0)
+        return true;
+      return false;
+   };
+
+   return *p_expr == expr;
+}
+
+bool
+s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial)
+{
+   s_list *list = SX_AS_LIST(top);
+   if (list == NULL)
+      return false;
+
+   unsigned i = 0;
+   foreach_iter(exec_list_iterator, it, list->subexpressions) {
+      if (i >= n)
+        return partial; /* More actual items than the pattern expected */
+
+      s_expression *expr = (s_expression *) it.get();
+      if (expr == NULL || !pattern[i].match(expr))
+        return false;
+
+      i++;
+   }
+
+   if (i < n)
+      return false; /* Less actual items than the pattern expected */
+
+   return true;
+}