3 * Copyright © 2010 Intel Corporation
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:
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
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.
29 #include "s_expression.h"
31 s_symbol::s_symbol(const char *tmp
)
33 this->str
= talloc_strdup (this, tmp
);
34 assert(this->str
!= NULL
);
42 s_list::length() const
45 foreach_iter(exec_list_iterator
, it
, this->subexpressions
) {
52 read_atom(void *ctx
, const char *& src
)
56 if (sscanf(src
, " %100[^( \v\t\r\n)]%n", buf
, &n
) != 1)
57 return NULL
; // no atom
60 // Check if the atom is a number.
61 char *float_end
= NULL
;
62 double f
= strtod(buf
, &float_end
);
63 if (float_end
!= buf
) {
65 int i
= strtol(buf
, &int_end
, 10);
66 // If strtod matched more characters, it must have a decimal part
67 if (float_end
> int_end
)
68 return new(ctx
) s_float(f
);
70 return new(ctx
) s_int(i
);
72 // Not a number; return a symbol.
73 return new(ctx
) s_symbol(buf
);
77 s_expression::read_expression(void *ctx
, const char *&src
)
81 s_expression
*atom
= read_atom(ctx
, src
);
87 if (sscanf(src
, " %c%n", &c
, &n
) == 1 && c
== '(') {
90 s_list
*list
= new(ctx
) s_list
;
93 while ((expr
= read_expression(ctx
, src
)) != NULL
) {
94 list
->subexpressions
.push_tail(expr
);
96 if (sscanf(src
, " %c%n", &c
, &n
) != 1 || c
!= ')') {
97 printf("Unclosed expression (check your parenthesis).\n");
108 printf("%d", this->val
);
111 void s_float::print()
113 printf("%f", this->val
);
116 void s_symbol::print()
118 printf("%s", this->str
);
124 foreach_iter(exec_list_iterator
, it
, this->subexpressions
) {
125 s_expression
*expr
= (s_expression
*) it
.get();