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