2010-04-07 13:39:34 -07:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2015-03-04 19:17:57 -07:00
|
|
|
#include <stdio.h>
|
2015-01-12 22:35:53 -08:00
|
|
|
#include <math.h>
|
2018-04-08 13:13:08 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-04-07 13:39:34 -07:00
|
|
|
#include "s_expression.h"
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
s_symbol::s_symbol(const char *str, size_t n)
|
2010-04-07 13:39:34 -07:00
|
|
|
{
|
2011-07-15 02:27:49 -07:00
|
|
|
/* Assume the given string is already nul-terminated and in memory that
|
|
|
|
* will live as long as this node.
|
|
|
|
*/
|
|
|
|
assert(str[n] == '\0');
|
|
|
|
this->str = str;
|
2010-04-07 13:39:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
s_list::s_list()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-01 12:43:03 -08:00
|
|
|
static void
|
2011-07-15 02:27:49 -07:00
|
|
|
skip_whitespace(const char *&src, char *&symbol_buffer)
|
2011-01-01 12:43:03 -08:00
|
|
|
{
|
2011-07-15 02:27:49 -07:00
|
|
|
size_t n = strspn(src, " \v\t\r\n");
|
|
|
|
src += n;
|
|
|
|
symbol_buffer += n;
|
2011-01-01 12:43:03 -08:00
|
|
|
/* Also skip Scheme-style comments: semi-colon 'til end of line */
|
|
|
|
if (src[0] == ';') {
|
2011-07-15 02:27:49 -07:00
|
|
|
n = strcspn(src, "\n");
|
|
|
|
src += n;
|
|
|
|
symbol_buffer += n;
|
|
|
|
skip_whitespace(src, symbol_buffer);
|
2011-01-01 12:43:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 13:39:34 -07:00
|
|
|
static s_expression *
|
2011-07-15 02:27:49 -07:00
|
|
|
read_atom(void *ctx, const char *&src, char *&symbol_buffer)
|
2010-04-07 13:39:34 -07:00
|
|
|
{
|
2010-08-18 18:03:22 -07:00
|
|
|
s_expression *expr = NULL;
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
skip_whitespace(src, symbol_buffer);
|
2010-08-18 18:03:22 -07:00
|
|
|
|
2011-01-01 12:43:03 -08:00
|
|
|
size_t n = strcspn(src, "( \v\t\r\n);");
|
2010-08-18 18:03:22 -07:00
|
|
|
if (n == 0)
|
2010-04-07 13:39:34 -07:00
|
|
|
return NULL; // no atom
|
|
|
|
|
2012-01-17 10:28:10 -08:00
|
|
|
// Check for the special symbol '+INF', which means +Infinity. Note: C99
|
2013-01-22 17:50:53 -07:00
|
|
|
// requires strtof to parse '+INF' as +Infinity, but we still support some
|
2012-01-17 10:28:10 -08:00
|
|
|
// non-C99-compliant compilers (e.g. MSVC).
|
|
|
|
if (n == 4 && strncmp(src, "+INF", 4) == 0) {
|
2015-01-12 22:35:53 -08:00
|
|
|
expr = new(ctx) s_float(INFINITY);
|
2010-08-18 18:03:22 -07:00
|
|
|
} else {
|
2012-01-17 10:28:10 -08:00
|
|
|
// Check if the atom is a number.
|
|
|
|
char *float_end = NULL;
|
2014-08-20 14:40:22 +08:00
|
|
|
float f = _mesa_strtof(src, &float_end);
|
2012-01-17 10:28:10 -08:00
|
|
|
if (float_end != src) {
|
|
|
|
char *int_end = NULL;
|
|
|
|
int i = strtol(src, &int_end, 10);
|
2013-01-22 17:50:53 -07:00
|
|
|
// If strtof matched more characters, it must have a decimal part
|
2012-01-17 10:28:10 -08:00
|
|
|
if (float_end > int_end)
|
|
|
|
expr = new(ctx) s_float(f);
|
|
|
|
else
|
|
|
|
expr = new(ctx) s_int(i);
|
|
|
|
} else {
|
|
|
|
// Not a number; return a symbol.
|
|
|
|
symbol_buffer[n] = '\0';
|
|
|
|
expr = new(ctx) s_symbol(symbol_buffer, n);
|
|
|
|
}
|
2010-04-07 13:39:34 -07:00
|
|
|
}
|
2010-08-18 18:03:22 -07:00
|
|
|
|
|
|
|
src += n;
|
2011-07-15 02:27:49 -07:00
|
|
|
symbol_buffer += n;
|
2010-08-18 18:03:22 -07:00
|
|
|
|
|
|
|
return expr;
|
2010-04-07 13:39:34 -07:00
|
|
|
}
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
static s_expression *
|
|
|
|
__read_expression(void *ctx, const char *&src, char *&symbol_buffer)
|
2010-04-07 13:39:34 -07:00
|
|
|
{
|
2011-07-15 02:27:49 -07:00
|
|
|
s_expression *atom = read_atom(ctx, src, symbol_buffer);
|
2010-04-07 13:39:34 -07:00
|
|
|
if (atom != NULL)
|
|
|
|
return atom;
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
skip_whitespace(src, symbol_buffer);
|
2010-08-18 18:03:22 -07:00
|
|
|
if (src[0] == '(') {
|
|
|
|
++src;
|
2011-07-15 02:27:49 -07:00
|
|
|
++symbol_buffer;
|
2010-04-07 13:39:34 -07:00
|
|
|
|
2010-06-23 18:11:51 -07:00
|
|
|
s_list *list = new(ctx) s_list;
|
2010-04-07 13:39:34 -07:00
|
|
|
s_expression *expr;
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
while ((expr = __read_expression(ctx, src, symbol_buffer)) != NULL) {
|
2010-04-07 13:39:34 -07:00
|
|
|
list->subexpressions.push_tail(expr);
|
|
|
|
}
|
2011-07-15 02:27:49 -07:00
|
|
|
skip_whitespace(src, symbol_buffer);
|
2010-08-18 18:03:22 -07:00
|
|
|
if (src[0] != ')') {
|
2010-04-07 13:39:34 -07:00
|
|
|
printf("Unclosed expression (check your parenthesis).\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-18 18:03:22 -07:00
|
|
|
++src;
|
2011-07-15 02:27:49 -07:00
|
|
|
++symbol_buffer;
|
2010-04-07 13:39:34 -07:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-15 02:27:49 -07:00
|
|
|
s_expression *
|
|
|
|
s_expression::read_expression(void *ctx, const char *&src)
|
|
|
|
{
|
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
/* When we encounter a Symbol, we need to save a nul-terminated copy of
|
|
|
|
* the string. However, ralloc_strndup'ing every individual Symbol is
|
|
|
|
* extremely expensive. We could avoid this by simply overwriting the
|
|
|
|
* next character (guaranteed to be whitespace, parens, or semicolon) with
|
|
|
|
* a nul-byte. But overwriting non-whitespace would mess up parsing.
|
|
|
|
*
|
|
|
|
* So, just copy the whole buffer ahead of time. Walk both, leaving the
|
|
|
|
* original source string unmodified, and altering the copy to contain the
|
|
|
|
* necessary nul-bytes whenever we encounter a symbol.
|
|
|
|
*/
|
|
|
|
char *symbol_buffer = ralloc_strdup(ctx, src);
|
|
|
|
return __read_expression(ctx, src, symbol_buffer);
|
|
|
|
}
|
|
|
|
|
2010-04-07 13:39:34 -07:00
|
|
|
void s_int::print()
|
|
|
|
{
|
|
|
|
printf("%d", this->val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void s_float::print()
|
|
|
|
{
|
|
|
|
printf("%f", this->val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void s_symbol::print()
|
|
|
|
{
|
|
|
|
printf("%s", this->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void s_list::print()
|
|
|
|
{
|
|
|
|
printf("(");
|
2014-06-24 21:34:05 -07:00
|
|
|
foreach_in_list(s_expression, expr, &this->subexpressions) {
|
2010-04-07 13:39:34 -07:00
|
|
|
expr->print();
|
2010-10-30 21:45:34 -07:00
|
|
|
if (!expr->next->is_tail_sentinel())
|
|
|
|
printf(" ");
|
2010-04-07 13:39:34 -07:00
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
}
|
|
|
|
|
ir_reader: Add a pattern matching system and use it everywhere.
Previously, the IR reader was riddled with code that:
1. Checked for the right number of list elements (via a linked list walk)
2. Retrieved references to each component (via ->next->next pointers)
3. Downcasted as necessary to make sure that each sub-component was the
right type (i.e. symbol, int, list).
4. Checking that the tag (i.e. "declare") was correct.
This was all very ad-hoc and a bit ugly. Error checking had to be done
at both steps 1, 3, and 4. Most code didn't even check the tag, relying
on the caller to do so. Not all callers did.
The new pattern matching module performs the whole process in a single
straightforward function call, resulting in shorter, more readable code.
Unfortunately, MSVC does not support C99-style anonymous arrays, so the
pattern must be declared outside of the match call.
2010-11-03 12:47:06 -07:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
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;
|
2014-06-24 21:34:05 -07:00
|
|
|
foreach_in_list(s_expression, expr, &list->subexpressions) {
|
ir_reader: Add a pattern matching system and use it everywhere.
Previously, the IR reader was riddled with code that:
1. Checked for the right number of list elements (via a linked list walk)
2. Retrieved references to each component (via ->next->next pointers)
3. Downcasted as necessary to make sure that each sub-component was the
right type (i.e. symbol, int, list).
4. Checking that the tag (i.e. "declare") was correct.
This was all very ad-hoc and a bit ugly. Error checking had to be done
at both steps 1, 3, and 4. Most code didn't even check the tag, relying
on the caller to do so. Not all callers did.
The new pattern matching module performs the whole process in a single
straightforward function call, resulting in shorter, more readable code.
Unfortunately, MSVC does not support C99-style anonymous arrays, so the
pattern must be declared outside of the match call.
2010-11-03 12:47:06 -07:00
|
|
|
if (i >= n)
|
|
|
|
return partial; /* More actual items than the pattern expected */
|
|
|
|
|
|
|
|
if (expr == NULL || !pattern[i].match(expr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < n)
|
|
|
|
return false; /* Less actual items than the pattern expected */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|