1/* -*- c++ -*- */
2/*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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.
15 *
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.
23 */
24
25#ifndef S_EXPRESSION_H
26#define S_EXPRESSION_H
27
28#include "util/strtod.h"
29#include "list.h"
30
31/* Type-safe downcasting macros (also safe to pass NULL) */
32#define SX_AS_(t,x) ((x) && ((s_expression*) x)->is_##t()) ? ((s_##t*) (x)) \
33                                                           : NULL
34#define SX_AS_LIST(x)   SX_AS_(list, x)
35#define SX_AS_SYMBOL(x) SX_AS_(symbol, x)
36#define SX_AS_NUMBER(x) SX_AS_(number, x)
37#define SX_AS_INT(x)    SX_AS_(int, x)
38
39/* Pattern matching macros */
40#define MATCH(list, pat) s_match(list, ARRAY_SIZE(pat), pat, false)
41#define PARTIAL_MATCH(list, pat) s_match(list, ARRAY_SIZE(pat), pat, true)
42
43/* For our purposes, S-Expressions are:
44 * - <int>
45 * - <float>
46 * - symbol
47 * - (expr1 expr2 ... exprN)     where exprN is an S-Expression
48 *
49 * Unlike LISP/Scheme, we do not support (foo . bar) pairs.
50 */
51class s_expression : public exec_node
52{
53public:
54   /**
55    * Read an S-Expression from the given string.
56    * Advances the supplied pointer to just after the expression read.
57    *
58    * Any allocation will be performed with 'ctx' as the ralloc owner.
59    */
60   static s_expression *read_expression(void *ctx, const char *&src);
61
62   /**
63    * Print out an S-Expression.  Useful for debugging.
64    */
65   virtual void print() = 0;
66
67   virtual bool is_list()   const { return false; }
68   virtual bool is_symbol() const { return false; }
69   virtual bool is_number() const { return false; }
70   virtual bool is_int()    const { return false; }
71
72protected:
73   s_expression() { }
74};
75
76/* Atoms */
77
78class s_number : public s_expression
79{
80public:
81   bool is_number() const { return true; }
82
83   virtual float fvalue() = 0;
84
85protected:
86   s_number() { }
87};
88
89class s_int : public s_number
90{
91public:
92   s_int(int x) : val(x) { }
93
94   bool is_int() const { return true; }
95
96   float fvalue() { return float(this->val); }
97   int value() { return this->val; }
98
99   void print();
100
101private:
102   int val;
103};
104
105class s_float : public s_number
106{
107public:
108   s_float(float x) : val(x) { }
109
110   float fvalue() { return this->val; }
111
112   void print();
113
114private:
115   float val;
116};
117
118class s_symbol : public s_expression
119{
120public:
121   s_symbol(const char *, size_t);
122
123   bool is_symbol() const { return true; }
124
125   const char *value() { return this->str; }
126
127   void print();
128
129private:
130   const char *str;
131};
132
133/* Lists of expressions: (expr1 ... exprN) */
134class s_list : public s_expression
135{
136public:
137   s_list();
138
139   virtual bool is_list() const { return true; }
140
141   void print();
142
143   exec_list subexpressions;
144};
145
146// ------------------------------------------------------------
147
148/**
149 * Part of a pattern to match - essentially a record holding a pointer to the
150 * storage for the component to match, along with the appropriate type.
151 */
152class s_pattern {
153public:
154   s_pattern(s_expression *&s) : p_expr(&s),   type(EXPR)   { }
155   s_pattern(s_list       *&s) : p_list(&s),   type(LIST)   { }
156   s_pattern(s_symbol     *&s) : p_symbol(&s), type(SYMBOL) { }
157   s_pattern(s_number     *&s) : p_number(&s), type(NUMBER) { }
158   s_pattern(s_int        *&s) : p_int(&s),    type(INT)    { }
159   s_pattern(const char *str)  : literal(str), type(STRING) { }
160
161   bool match(s_expression *expr);
162
163private:
164   union {
165      s_expression **p_expr;
166      s_list       **p_list;
167      s_symbol     **p_symbol;
168      s_number     **p_number;
169      s_int        **p_int;
170      const char *literal;
171   };
172   enum { EXPR, LIST, SYMBOL, NUMBER, INT, STRING } type;
173};
174
175bool
176s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial);
177
178#endif /* S_EXPRESSION_H */
179