glsl: Refactor AST-to-HIR code handling variable initializers
[mesa.git] / src / glsl / s_expression.cpp
index a00bfa73ed164162971670b5da72d52417a06a73..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, size_t n)
 {
-   this->str = talloc_strndup (this, tmp, n);
+   this->str = ralloc_strndup (this, tmp, n);
    assert(this->str != NULL);
 }
 
@@ -38,14 +35,15 @@ 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 *
@@ -53,10 +51,9 @@ read_atom(void *ctx, const char *& src)
 {
    s_expression *expr = NULL;
 
-   // Skip leading spaces.
-   src += strspn(src, " \v\t\r\n");
+   skip_whitespace(src);
 
-   size_t n = strcspn(src, "( \v\t\r\n)");
+   size_t n = strcspn(src, "( \v\t\r\n);");
    if (n == 0)
       return NULL; // no atom
 
@@ -90,8 +87,7 @@ s_expression::read_expression(void *ctx, const char *&src)
    if (atom != NULL)
       return atom;
 
-   // Skip leading spaces.
-   src += strspn(src, " \v\t\r\n");
+   skip_whitespace(src);
    if (src[0] == '(') {
       ++src;
 
@@ -101,7 +97,7 @@ s_expression::read_expression(void *ctx, const char *&src)
       while ((expr = read_expression(ctx, src)) != NULL) {
         list->subexpressions.push_tail(expr);
       }
-      src += strspn(src, " \v\t\r\n");
+      skip_whitespace(src);
       if (src[0] != ')') {
         printf("Unclosed expression (check your parenthesis).\n");
         return NULL;