blob: 4022dfab7a73888dee4e0d4933ae8c3dea2f1e69 [file] [log] [blame]
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -07001/* -*- 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#include <cstdio>
26#include <cstdlib>
27#include <cstring>
28#include <assert.h>
29#include "s_expression.h"
30
31s_symbol::s_symbol(const char *tmp)
32{
33 this->str = new char [strlen(tmp) + 1];
34 strcpy(this->str, tmp);
35}
36
37s_symbol::~s_symbol()
38{
39 delete [] this->str;
40 this->str = NULL;
41}
42
43s_list::s_list()
44{
45}
46
47s_list::~s_list()
48{
49 exec_list_iterator it(this->subexpressions.iterator());
50 while (it.has_next())
51 it.remove();
52
53 assert(this->subexpressions.is_empty());
54}
55
56unsigned
57s_list::length() const
58{
59 unsigned i = 0;
60 foreach_iter(exec_list_iterator, it, this->subexpressions) {
61 i++;
62 }
63 return i;
64}
65
66static s_expression *
67read_atom(const char *& src)
68{
69 char buf[101];
70 int n;
71 if (sscanf(src, " %100[^( \v\t\r\n)]%n", buf, &n) != 1)
72 return NULL; // no atom
73 src += n;
74
75 // Check if the atom is a number.
76 char *float_end = NULL;
77 double f = strtod(buf, &float_end);
78 if (float_end != buf) {
79 char *int_end = NULL;
80 int i = strtol(buf, &int_end, 10);
81 // If strtod matched more characters, it must have a decimal part
82 if (float_end > int_end)
83 return new s_float(f);
84
85 return new s_int(i);
86 }
87 // Not a number; return a symbol.
88 return new s_symbol(buf);
89}
90
91s_expression *
92s_expression::read_expression(const char *&src)
93{
94 assert(src != NULL);
95
96 s_expression *atom = read_atom(src);
97 if (atom != NULL)
98 return atom;
99
100 char c;
101 int n;
102 if (sscanf(src, " %c%n", &c, &n) == 1 && c == '(') {
103 src += n;
104
105 s_list *list = new s_list;
106 s_expression *expr;
107
108 while ((expr = read_expression(src)) != NULL) {
109 list->subexpressions.push_tail(expr);
110 }
111 if (sscanf(src, " %c%n", &c, &n) != 1 || c != ')') {
112 printf("Unclosed expression (check your parenthesis).\n");
113 return NULL;
114 }
115 src += n;
116 return list;
117 }
118 return NULL;
119}
120
121void s_int::print()
122{
123 printf("%d", this->val);
124}
125
126void s_float::print()
127{
128 printf("%f", this->val);
129}
130
131void s_symbol::print()
132{
133 printf("%s", this->str);
134}
135
136void s_list::print()
137{
138 printf("(");
139 foreach_iter(exec_list_iterator, it, this->subexpressions) {
140 s_expression *expr = (s_expression*) it.get();
141 expr->print();
142 printf(" ");
143 }
144 printf(")");
145}
146