| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 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 | #include <cstdio> |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | #include <assert.h> |
| 29 | #include "s_expression.h" |
| 30 | |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 31 | s_symbol::s_symbol(const char *tmp, size_t n) |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 32 | { |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 33 | this->str = talloc_strndup (this, tmp, n); |
| Carl Worth | 007efe5 | 2010-06-23 18:30:55 -0700 | [diff] [blame] | 34 | assert(this->str != NULL); |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 37 | s_list::s_list() |
| 38 | { |
| 39 | } |
| 40 | |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 41 | unsigned |
| 42 | s_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 | |
| 51 | static s_expression * |
| Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 52 | read_atom(void *ctx, const char *& src) |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 53 | { |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 54 | s_expression *expr = NULL; |
| 55 | |
| 56 | // Skip leading spaces. |
| 57 | src += strspn(src, " \v\t\r\n"); |
| 58 | |
| 59 | size_t n = strcspn(src, "( \v\t\r\n)"); |
| 60 | if (n == 0) |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 61 | return NULL; // no atom |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 62 | |
| 63 | // Check if the atom is a number. |
| 64 | char *float_end = NULL; |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 65 | double f = strtod(src, &float_end); |
| 66 | if (float_end != src) { |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 67 | char *int_end = NULL; |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 68 | int i = strtol(src, &int_end, 10); |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 69 | // If strtod matched more characters, it must have a decimal part |
| 70 | if (float_end > int_end) |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 71 | expr = new(ctx) s_float(f); |
| 72 | else |
| 73 | expr = new(ctx) s_int(i); |
| 74 | } else { |
| 75 | // Not a number; return a symbol. |
| 76 | expr = new(ctx) s_symbol(src, n); |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 77 | } |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 78 | |
| 79 | src += n; |
| 80 | |
| 81 | return expr; |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | s_expression * |
| Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 85 | s_expression::read_expression(void *ctx, const char *&src) |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 86 | { |
| 87 | assert(src != NULL); |
| 88 | |
| Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 89 | s_expression *atom = read_atom(ctx, src); |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 90 | if (atom != NULL) |
| 91 | return atom; |
| 92 | |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 93 | // Skip leading spaces. |
| 94 | src += strspn(src, " \v\t\r\n"); |
| 95 | if (src[0] == '(') { |
| 96 | ++src; |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 97 | |
| Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 98 | s_list *list = new(ctx) s_list; |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 99 | s_expression *expr; |
| 100 | |
| Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 101 | while ((expr = read_expression(ctx, src)) != NULL) { |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 102 | list->subexpressions.push_tail(expr); |
| 103 | } |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 104 | src += strspn(src, " \v\t\r\n"); |
| 105 | if (src[0] != ')') { |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 106 | printf("Unclosed expression (check your parenthesis).\n"); |
| 107 | return NULL; |
| 108 | } |
| Kenneth Graunke | 365ce61 | 2010-08-18 18:03:22 -0700 | [diff] [blame] | 109 | ++src; |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 110 | return list; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | void s_int::print() |
| 116 | { |
| 117 | printf("%d", this->val); |
| 118 | } |
| 119 | |
| 120 | void s_float::print() |
| 121 | { |
| 122 | printf("%f", this->val); |
| 123 | } |
| 124 | |
| 125 | void s_symbol::print() |
| 126 | { |
| 127 | printf("%s", this->str); |
| 128 | } |
| 129 | |
| 130 | void s_list::print() |
| 131 | { |
| 132 | printf("("); |
| 133 | foreach_iter(exec_list_iterator, it, this->subexpressions) { |
| 134 | s_expression *expr = (s_expression*) it.get(); |
| 135 | expr->print(); |
| Kenneth Graunke | d2c23ac | 2010-10-30 21:45:34 -0700 | [diff] [blame] | 136 | if (!expr->next->is_tail_sentinel()) |
| 137 | printf(" "); |
| Kenneth Graunke | 1bfe1c3 | 2010-04-07 13:39:34 -0700 | [diff] [blame] | 138 | } |
| 139 | printf(")"); |
| 140 | } |
| 141 | |