blob: 26be23ea8faceab79b7b5c1fab623454fdc9f18c [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{
Carl Worth007efe52010-06-23 18:30:55 -070033 this->str = talloc_strdup (this, tmp);
34 assert(this->str != NULL);
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070035}
36
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070037s_list::s_list()
38{
39}
40
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070041unsigned
42s_list::length() const
43{
44 unsigned i = 0;
45 foreach_iter(exec_list_iterator, it, this->subexpressions) {
46 i++;
47 }
48 return i;
49}
50
51static s_expression *
Carl Worth1660a292010-06-23 18:11:51 -070052read_atom(void *ctx, const char *& src)
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070053{
54 char buf[101];
55 int n;
56 if (sscanf(src, " %100[^( \v\t\r\n)]%n", buf, &n) != 1)
57 return NULL; // no atom
58 src += n;
59
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) {
64 char *int_end = NULL;
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)
Carl Worth1660a292010-06-23 18:11:51 -070068 return new(ctx) s_float(f);
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070069
Carl Worth1660a292010-06-23 18:11:51 -070070 return new(ctx) s_int(i);
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070071 }
72 // Not a number; return a symbol.
Carl Worth1660a292010-06-23 18:11:51 -070073 return new(ctx) s_symbol(buf);
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070074}
75
76s_expression *
Carl Worth1660a292010-06-23 18:11:51 -070077s_expression::read_expression(void *ctx, const char *&src)
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070078{
79 assert(src != NULL);
80
Carl Worth1660a292010-06-23 18:11:51 -070081 s_expression *atom = read_atom(ctx, src);
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070082 if (atom != NULL)
83 return atom;
84
85 char c;
86 int n;
87 if (sscanf(src, " %c%n", &c, &n) == 1 && c == '(') {
88 src += n;
89
Carl Worth1660a292010-06-23 18:11:51 -070090 s_list *list = new(ctx) s_list;
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070091 s_expression *expr;
92
Carl Worth1660a292010-06-23 18:11:51 -070093 while ((expr = read_expression(ctx, src)) != NULL) {
Kenneth Graunke1bfe1c32010-04-07 13:39:34 -070094 list->subexpressions.push_tail(expr);
95 }
96 if (sscanf(src, " %c%n", &c, &n) != 1 || c != ')') {
97 printf("Unclosed expression (check your parenthesis).\n");
98 return NULL;
99 }
100 src += n;
101 return list;
102 }
103 return NULL;
104}
105
106void s_int::print()
107{
108 printf("%d", this->val);
109}
110
111void s_float::print()
112{
113 printf("%f", this->val);
114}
115
116void s_symbol::print()
117{
118 printf("%s", this->str);
119}
120
121void s_list::print()
122{
123 printf("(");
124 foreach_iter(exec_list_iterator, it, this->subexpressions) {
125 s_expression *expr = (s_expression*) it.get();
126 expr->print();
127 printf(" ");
128 }
129 printf(")");
130}
131