From 0f87caac50dcda42a747349c0cf744ea653c63b4 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 4 Nov 2014 14:00:01 -0600 Subject: BAS: Another file is closer to the NuttX coding standard --- apps/interpreters/bas/var.c | 481 ++++++++++++++++++++++++++++++-------------- 1 file changed, 328 insertions(+), 153 deletions(-) (limited to 'apps/interpreters') diff --git a/apps/interpreters/bas/var.c b/apps/interpreters/bas/var.c index 0a8271b2b..bf90c99c8 100644 --- a/apps/interpreters/bas/var.c +++ b/apps/interpreters/bas/var.c @@ -1,3 +1,61 @@ +/**************************************************************************** + * apps/examples/interpreters/bas/var.c + * + * Copyright (c) 1999-2014 Michael Haardt + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Adapted to NuttX and re-released under a 3-clause BSD license: + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Authors: Alan Carvalho de Assis + * Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + /**************************************************************************** * Included Files ****************************************************************************/ @@ -21,7 +79,8 @@ * Public Functions ****************************************************************************/ -struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, const unsigned int *geometry, int base) +struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, + const unsigned int *geometry, int base) { unsigned int i; size_t newsize; @@ -35,15 +94,24 @@ struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, con return (struct Var *)0; this->size = newsize; } + if ((newsize *= sizeof(struct Value)) < this->size) - return (struct Var *)0; + { + return (struct Var *)0; + } + if ((this->value = malloc(newsize)) == (struct Value *)0) - return (struct Var *)0; + { + return (struct Var *)0; + } + if (dim) { this->geometry = malloc(sizeof(unsigned int) * dim); for (i = 0; i < dim; ++i) - this->geometry[i] = geometry[i]; + { + this->geometry[i] = geometry[i]; + } } else { @@ -51,7 +119,9 @@ struct Var *Var_new(struct Var *this, enum ValueType type, unsigned int dim, con } for (i = 0; i < this->size; ++i) - Value_new_null(&(this->value[i]), type); + { + Value_new_null(&(this->value[i]), type); + } return this; } @@ -68,7 +138,9 @@ struct Var *Var_new_scalar(struct Var *this) void Var_destroy(struct Var *this) { while (this->size--) - Value_destroy(&(this->value[this->size])); + { + Value_destroy(&(this->value[this->size])); + } free(this->value); this->value = (struct Value *)0; @@ -92,14 +164,17 @@ void Var_retype(struct Var *this, enum ValueType type) } } -struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], struct Value *value) +struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], + struct Value *value) { unsigned int offset; unsigned int i; assert(this->value); if (dim != this->dim) - return Value_new_ERROR(value, DIMENSION); + { + return Value_new_ERROR(value, DIMENSION); + } for (offset = 0, i = 0; i < dim; ++i) { @@ -107,6 +182,7 @@ struct Value *Var_value(struct Var *this, unsigned int dim, int idx[], struct Va { return Value_new_ERROR(value, OUTOFRANGE, _("array index")); } + offset = offset * this->geometry[i] + (idx[i] - this->base); } @@ -134,7 +210,8 @@ void Var_clear(struct Var *this) Value_new_null(&(this->value[0]), this->type); } -struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err, int work) +struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err, + int work) { enum ValueType thisType = this->type; @@ -147,31 +224,39 @@ struct Value *Var_mat_assign(struct Var *this, struct Var *x, struct Value *err, assert(x->base == 0 || x->base == 1); assert(x->dim == 1 || x->dim == 2); if (this == x) - return (struct Value *)0; + { + return (struct Value *)0; + } + Var_destroy(this); Var_new(this, thisType, x->dim, x->geometry, x->base); g0 = x->geometry[0]; g1 = x->dim == 1 ? unused + 1 : x->geometry[1]; for (i = unused; i < g0; ++i) - for (j = unused; j < g1; ++j) - { - unsigned int element = x->dim == 1 ? i : i * g1 + j; - - Value_destroy(&(this->value[element])); - Value_clone(&(this->value[element]), &(x->value[element])); - Value_retype(&(this->value[element]), thisType); - } + { + for (j = unused; j < g1; ++j) + { + unsigned int element = x->dim == 1 ? i : i * g1 + j; + + Value_destroy(&(this->value[element])); + Value_clone(&(this->value[element]), &(x->value[element])); + Value_retype(&(this->value[element]), thisType); + } + } } else { if (Value_commonType[this->type][x->type] == V_ERROR) - return Value_new_typeError(err, this->type, x->type); + { + return Value_new_typeError(err, this->type, x->type); + } } return (struct Value *)0; } -struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int add, struct Value *err, int work) +struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, + int add, struct Value *err, int work) { enum ValueType thisType = this->type; struct Value foo, bar; @@ -185,10 +270,11 @@ struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int assert(x->base == 0 || x->base == 1); assert(x->dim == 1 || x->dim == 2); if (x->base != y->base || x->dim != y->dim || - x->geometry[0] != y->geometry[0] || (x->dim == 2 && - x->geometry[1] != - y->geometry[1])) - return Value_new_ERROR(err, DIMENSION); + x->geometry[0] != y->geometry[0] || + (x->dim == 2 && x->geometry[1] != y->geometry[1])) + { + return Value_new_ERROR(err, DIMENSION); + } if (this != x && this != y) { @@ -199,37 +285,51 @@ struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int g0 = x->geometry[0]; g1 = x->dim == 1 ? unused + 1 : x->geometry[1]; for (i = unused; i < g0; ++i) - for (j = unused; j < g1; ++j) - { - unsigned int element = x->dim == 1 ? i : i * g1 + j; - - Value_clone(&foo, &(x->value[element])); - Value_clone(&bar, &(y->value[element])); - if (add) - Value_add(&foo, &bar, 1); - else - Value_sub(&foo, &bar, 1); - if (foo.type == V_ERROR) - { - *err = foo; - Value_destroy(&bar); - return err; - } - Value_destroy(&bar); - Value_destroy(&(this->value[element])); - this->value[element] = *Value_retype(&foo, thisType); - } + { + for (j = unused; j < g1; ++j) + { + unsigned int element = x->dim == 1 ? i : i * g1 + j; + + Value_clone(&foo, &(x->value[element])); + Value_clone(&bar, &(y->value[element])); + if (add) + { + Value_add(&foo, &bar, 1); + } + else + { + Value_sub(&foo, &bar, 1); + } + + if (foo.type == V_ERROR) + { + *err = foo; + Value_destroy(&bar); + return err; + } + + Value_destroy(&bar); + Value_destroy(&(this->value[element])); + this->value[element] = *Value_retype(&foo, thisType); + } + } } else { Value_clone(err, x->value); if (add) - Value_add(err, y->value, 0); + { + Value_add(err, y->value, 0); + } else - Value_sub(err, y->value, 0); + { + Value_sub(err, y->value, 0); + } if (err->type == V_ERROR) - return err; + { + return err; + } Value_destroy(err); } @@ -237,7 +337,8 @@ struct Value *Var_mat_addsub(struct Var *this, struct Var *x, struct Var *y, int return (struct Value *)0; } -struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struct Value *err, int work) +struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, + struct Value *err, int work) { enum ValueType thisType = this->type; struct Var foo; @@ -251,34 +352,40 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc assert(x->base == 0 || x->base == 1); if (x->dim != 2 || y->dim != 2 || x->base != y->base || x->geometry[1] != y->geometry[0]) - return Value_new_ERROR(err, DIMENSION); + { + return Value_new_ERROR(err, DIMENSION); + } newdim[0] = x->geometry[0]; newdim[1] = y->geometry[1]; Var_new(&foo, thisType, 2, newdim, 0); for (i = unused; i < newdim[0]; ++i) - for (j = unused; j < newdim[1]; ++j) - { - struct Value *dp = &foo.value[i * newdim[1] + j]; - - Value_new_null(dp, thisType); - for (k = unused; k < x->geometry[1]; ++k) - { - struct Value p; - - Value_clone(&p, &(x->value[i * x->geometry[1] + k])); - Value_mult(&p, &(y->value[k * y->geometry[1] + j]), 1); - if (p.type == V_ERROR) - { - *err = p; - Var_destroy(&foo); - return err; - } - Value_add(dp, &p, 1); - Value_destroy(&p); - } - Value_retype(dp, thisType); - } + { + for (j = unused; j < newdim[1]; ++j) + { + struct Value *dp = &foo.value[i * newdim[1] + j]; + + Value_new_null(dp, thisType); + for (k = unused; k < x->geometry[1]; ++k) + { + struct Value p; + + Value_clone(&p, &(x->value[i * x->geometry[1] + k])); + Value_mult(&p, &(y->value[k * y->geometry[1] + j]), 1); + if (p.type == V_ERROR) + { + *err = p; + Var_destroy(&foo); + return err; + } + + Value_add(dp, &p, 1); + Value_destroy(&p); + } + + Value_retype(dp, thisType); + } + } Var_destroy(this); *this = foo; @@ -288,7 +395,9 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc Value_clone(err, x->value); Value_mult(err, y->value, 0); if (err->type == V_ERROR) - return err; + { + return err; + } Value_destroy(err); } @@ -296,7 +405,8 @@ struct Value *Var_mat_mult(struct Var *this, struct Var *x, struct Var *y, struc return (struct Value *)0; } -struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct Var *x, int work) +struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, + struct Var *x, int work) { enum ValueType thisType = this->type; @@ -317,28 +427,32 @@ struct Value *Var_mat_scalarMult(struct Var *this, struct Value *factor, struct g0 = x->geometry[0]; g1 = x->dim == 1 ? unused + 1 : x->geometry[1]; for (i = unused; i < g0; ++i) - for (j = unused; j < g1; ++j) - { - unsigned int element = x->dim == 1 ? i : i * g1 + j; - struct Value foo; - - Value_clone(&foo, &(x->value[element])); - Value_mult(&foo, factor, 1); - if (foo.type == V_ERROR) - { - Value_destroy(factor); - *factor = foo; - return factor; - } - - Value_destroy(&(this->value[element])); - this->value[element] = *Value_retype(&foo, thisType); - } + { + for (j = unused; j < g1; ++j) + { + unsigned int element = x->dim == 1 ? i : i * g1 + j; + struct Value foo; + + Value_clone(&foo, &(x->value[element])); + Value_mult(&foo, factor, 1); + if (foo.type == V_ERROR) + { + Value_destroy(factor); + *factor = foo; + return factor; + } + + Value_destroy(&(this->value[element])); + this->value[element] = *Value_retype(&foo, thisType); + } + } } else { if (Value_mult(factor, this->value, 0)->type == V_ERROR) - return factor; + { + return factor; + } } return (struct Value *)0; @@ -355,19 +469,22 @@ void Var_mat_transpose(struct Var *this, struct Var *x) geometry[1] = x->geometry[0]; Var_new(&foo, thisType, 2, geometry, 0); for (i = 0; i < x->geometry[0]; ++i) - for (j = 0; j < x->geometry[1]; ++j) - { - Value_destroy(&foo.value[j * x->geometry[0] + i]); - Value_clone(&foo.value[j * x->geometry[0] + i], - &(x->value[i * x->geometry[1] + j])); - Value_retype(&foo.value[j * x->geometry[0] + i], thisType); - } + { + for (j = 0; j < x->geometry[1]; ++j) + { + Value_destroy(&foo.value[j * x->geometry[0] + i]); + Value_clone(&foo.value[j * x->geometry[0] + i], + &(x->value[i * x->geometry[1] + j])); + Value_retype(&foo.value[j * x->geometry[0] + i], thisType); + } + } Var_destroy(this); *this = foo; } -struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, struct Value *err) +struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, + struct Value *err) { enum ValueType thisType = this->type; int n, i, j, k, max; @@ -375,42 +492,59 @@ struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, int unused = 1 - x->base; if (x->type != V_INTEGER && x->type != V_REAL) - return Value_new_ERROR(err, TYPEMISMATCH5); + { + return Value_new_ERROR(err, TYPEMISMATCH5); + } assert(x->base == 0 || x->base == 1); if (x->geometry[0] != x->geometry[1]) - return Value_new_ERROR(err, DIMENSION); + { + return Value_new_ERROR(err, DIMENSION); + } n = x->geometry[0] - unused; a = malloc(sizeof(double) * n * n); u = malloc(sizeof(double) * n * n); for (i = 0; i < n; ++i) - for (j = 0; j < n; ++j) - { - if (x->type == V_INTEGER) - a[i * n + j] = - x->value[(i + unused) * (n + unused) + j + unused].u.integer; - else - a[i * n + j] = - x->value[(i + unused) * (n + unused) + j + unused].u.real; - u[i * n + j] = (i == j ? 1.0 : 0.0); - } + { + for (j = 0; j < n; ++j) + { + if (x->type == V_INTEGER) + { + a[i * n + j] = + x->value[(i + unused) * (n + unused) + j + unused].u.integer; + } + else + { + a[i * n + j] = + x->value[(i + unused) * (n + unused) + j + unused].u.real; + } + + u[i * n + j] = (i == j ? 1.0 : 0.0); + } + } d = 1.0; for (i = 0; i < n; ++i) /* get zeroes in column i below the main - * diagonale */ + * diagonal */ { max = i; for (j = i + 1; j < n; ++j) - if (fabs(a[j * n + i]) > fabs(a[max * n + i])) - max = j; + { + if (fabs(a[j * n + i]) > fabs(a[max * n + i])) + { + max = j; + } + } /* exchanging row i against row max */ if (i != max) - d = -d; + { + d = -d; + } for (k = i; k < n; ++k) { @@ -436,35 +570,47 @@ struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, for (j = i + 1; j < n; ++j) { t = a[j * n + i] / a[i * n + i]; - /* substract row i*t from row j */ + + /* Subtract row i*t from row j */ + for (k = i; k < n; ++k) - a[j * n + k] -= a[i * n + k] * t; + { + a[j * n + k] -= a[i * n + k] * t; + } + for (k = 0; k < n; ++k) - u[j * n + k] -= u[i * n + k] * t; + { + u[j * n + k] -= u[i * n + k] * t; + } } } for (i = 0; i < n; ++i) - d *= a[i * n + i]; /* compute determinant */ + { + d *= a[i * n + i]; /* compute determinant */ + } - for (i = n - 1; i >= 0; --i) /* get zeroes in column i above the main - * diagonale */ + for (i = n - 1; i >= 0; --i) /* get zeroes in column i above the main diagonal */ { for (j = 0; j < i; ++j) { t = a[j * n + i] / a[i * n + i]; - /* subtract row i*t from row j */ + /* Subtract row i*t from row j */ a[j * n + i] = 0.0; /* a[j*n+i]-=a[i*n+i]*t; */ for (k = 0; k < n; ++k) - u[j * n + k] -= u[i * n + k] * t; + { + u[j * n + k] -= u[i * n + k] * t; + } } t = a[i * n + i]; a[i * n + i] = 1.0; /* a[i*n+i]/=t; */ for (k = 0; k < n; ++k) - u[i * n + k] /= t; + { + u[i * n + k] /= t; + } } free(a); @@ -475,29 +621,41 @@ struct Value *Var_mat_invert(struct Var *this, struct Var *x, struct Value *det, } for (i = 0; i < n; ++i) - for (j = 0; j < n; ++j) - { - Value_destroy(&this->value[(i + unused) * (n + unused) + j + unused]); - if (thisType == V_INTEGER) - Value_new_INTEGER(&this-> - value[(i + unused) * (n + unused) + j + unused], - u[i * n + j]); - else - Value_new_REAL(&this->value[(i + unused) * (n + unused) + j + unused], - u[i * n + j]); - } + { + for (j = 0; j < n; ++j) + { + Value_destroy(&this->value[(i + unused) * (n + unused) + j + unused]); + if (thisType == V_INTEGER) + { + Value_new_INTEGER(&this->value + [(i + unused) * (n + unused) + j + unused], + u[i * n + j]); + } + else + { + Value_new_REAL(&this-> + value[(i + unused) * (n + unused) + j + unused], + u[i * n + j]); + } + } + } free(u); Value_destroy(det); if (thisType == V_INTEGER) - Value_new_INTEGER(det, d); + { + Value_new_INTEGER(det, d); + } else - Value_new_REAL(det, d); + { + Value_new_REAL(det, d); + } return (struct Value *)0; } -struct Value *Var_mat_redim(struct Var *this, unsigned int dim, const unsigned int *geometry, struct Value *err) +struct Value *Var_mat_redim(struct Var *this, unsigned int dim, + const unsigned int *geometry, struct Value *err) { unsigned int i, j, size; struct Value *value; @@ -505,35 +663,52 @@ struct Value *Var_mat_redim(struct Var *this, unsigned int dim, const unsigned i int g0, g1; if (this->dim > 0 && this->dim != dim) - return Value_new_ERROR(err, DIMENSION); + { + return Value_new_ERROR(err, DIMENSION); + } for (size = 1, i = 0; i < dim; ++i) - size *= geometry[i]; + { + size *= geometry[i]; + } value = malloc(sizeof(struct Value) * size); g0 = geometry[0]; g1 = dim == 1 ? 1 : geometry[1]; for (i = 0; i < g0; ++i) - for (j = 0; j < g1; ++j) - { - if (this->dim == 0 || i < unused || (dim == 2 && j < unused) || - i >= this->geometry[0] || (this->dim == 2 && - j >= this->geometry[1])) - Value_new_null(&(value[i * g1 + j]), this->type); - else - Value_clone(&value[dim == 1 ? i : i * g1 + j], - &this->value[dim == 1 ? i : i * this->geometry[1] + j]); - } + { + for (j = 0; j < g1; ++j) + { + if (this->dim == 0 || i < unused || (dim == 2 && j < unused) || + i >= this->geometry[0] || (this->dim == 2 && + j >= this->geometry[1])) + { + Value_new_null(&(value[i * g1 + j]), this->type); + } + else + { + Value_clone(&value[dim == 1 ? i : i * g1 + j], + &this->value[dim == + 1 ? i : i * this->geometry[1] + j]); + } + } + } for (i = 0; i < this->size; ++i) - Value_destroy(&this->value[i]); + { + Value_destroy(&this->value[i]); + } free(this->value); if (this->geometry == (unsigned int *)0) - this->geometry = malloc(sizeof(unsigned int) * dim); + { + this->geometry = malloc(sizeof(unsigned int) * dim); + } for (i = 0; i < dim; ++i) - this->geometry[i] = geometry[i]; + { + this->geometry[i] = geometry[i]; + } this->dim = dim; this->size = size; -- cgit v1.2.3