Merge remote branch 'origin/master' into glsl2
[profile/ivi/mesa.git] / src / glsl / s_expression.h
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 #pragma once
26 #ifndef S_EXPRESSION_H
27 #define S_EXPRESSION_H
28
29 #include "list.h"
30
31 #define SX_AS_(t,x) ((x) && ((s_expression*) x)->is_##t()) ? ((s_##t*) (x)) \
32                                                            : NULL
33 #define SX_AS_LIST(x)   SX_AS_(list, x)
34 #define SX_AS_SYMBOL(x) SX_AS_(symbol, x)
35 #define SX_AS_NUMBER(x) SX_AS_(number, x)
36 #define SX_AS_INT(x)    SX_AS_(int, x)
37
38 /* For our purposes, S-Expressions are:
39  * - <int>
40  * - <float>
41  * - symbol
42  * - (expr1 expr2 ... exprN)     where exprN is an S-Expression
43  *
44  * Unlike LISP/Scheme, we do not support (foo . bar) pairs.
45  */
46 class s_expression : public exec_node
47 {
48 public:
49    /**
50     * Read an S-Expression from the given string.
51     * Advances the supplied pointer to just after the expression read.
52     *
53     * Any allocation will be performed with 'ctx' as the talloc owner.
54     */
55    static s_expression *read_expression(void *ctx, const char *&src);
56
57    /**
58     * Print out an S-Expression.  Useful for debugging.
59     */
60    virtual void print() = 0;
61
62    virtual bool is_list()   const { return false; }
63    virtual bool is_symbol() const { return false; }
64    virtual bool is_number() const { return false; }
65    virtual bool is_int()    const { return false; }
66
67 protected:
68    s_expression() { }
69 };
70
71 /* Atoms */
72
73 class s_number : public s_expression
74 {
75 public:
76    bool is_number() const { return true; }
77
78    virtual float fvalue() = 0;
79
80 protected:
81    s_number() { }
82 };
83
84 class s_int : public s_number
85 {
86 public:
87    s_int(int x) : val(x) { }
88
89    bool is_int() const { return true; }
90
91    float fvalue() { return float(this->val); }
92    int value() { return this->val; }
93
94    void print();
95
96 private:
97    int val;
98 };
99
100 class s_float : public s_number
101 {
102 public:
103    s_float(float x) : val(x) { }
104
105    float fvalue() { return this->val; }
106
107    void print();
108
109 private:
110    float val;
111 };
112
113 class s_symbol : public s_expression
114 {
115 public:
116    s_symbol(const char *);
117
118    bool is_symbol() const { return true; }
119
120    const char *value() { return this->str; }
121
122    void print();
123
124 private:
125    char *str;
126 };
127
128 /* Lists of expressions: (expr1 ... exprN) */
129 class s_list : public s_expression
130 {
131 public:
132    s_list();
133
134    virtual bool is_list() const { return true; }
135    unsigned length() const;
136
137    void print();
138
139    exec_list subexpressions;
140 };
141
142 #endif /* S_EXPRESSION_H */