Skip to content

Commit

Permalink
Fix MSVC 2017 warnings
Browse files Browse the repository at this point in the history
/W4
  • Loading branch information
ccoustet committed Oct 29, 2018
1 parent f881105 commit 3f08d52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef double (*function1)(double);
static void bench(const char *expr, function1 func) {
int i, j;
volatile double d;
double tmp;
static double tmp;
clock_t start;

te_variable lk = {"a", {&tmp}, TE_VARIABLE, NULL};
Expand Down
2 changes: 1 addition & 1 deletion example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[])

/* This shows an example where the variables
* x and y are bound at eval-time. */
double x, y;
static double x, y;
te_variable vars[] = {{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};

/* This will compile the expression and check for errors. */
Expand Down
14 changes: 7 additions & 7 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ static void test_infs() {

static void test_variables() {

double x, y, test;
static double x, y, test;
te_variable lookup[] =
{{"x", {&x}, TE_VARIABLE, NULL},
{"y", {&y}, TE_VARIABLE, NULL},
Expand Down Expand Up @@ -356,7 +356,7 @@ static void test_variables() {

static void test_functions() {

double x, y;
static double x, y;
te_variable lookup[] =
{{"x", {&x}, TE_VARIABLE, NULL}, {"y", {&y}, TE_VARIABLE, NULL}};

Expand Down Expand Up @@ -390,7 +390,7 @@ static void test_functions() {
}


static double sum0() {
static double sum0(void) {
return 6;
}
static double sum1(double a) {
Expand Down Expand Up @@ -418,7 +418,7 @@ static double sum7(double a, double b, double c, double d, double e, double f, d

static void test_dynamic() {

double x, f;
static double x, f;
te_variable lookup[] = {
{"x", {&x}, TE_VARIABLE, NULL},
{"f", {&f}, TE_VARIABLE, NULL},
Expand Down Expand Up @@ -494,8 +494,8 @@ static double cell(void *context, double a) {

static void test_closure() {

double extra;
double c[] = {5,6,7,8,9};
static double extra;
static double c[] = {5,6,7,8,9};

te_variable lookup[] = {
{"c0", {.cl0=clo0}, TE_CLOSURE0, &extra},
Expand Down Expand Up @@ -602,7 +602,7 @@ static void test_pow() {
};
#endif

double a = 2, b = 3;
static double a = 2, b = 3;

te_variable lookup[] = {
{"a", {&a}, TE_VARIABLE, NULL},
Expand Down
37 changes: 29 additions & 8 deletions tinyexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ For log = natural log uncomment the next line. */
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>

#ifndef NAN
#define NAN (0.0/0.0)
Expand Down Expand Up @@ -95,6 +96,26 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
return ret;
}

static te_expr *new_expr1(const int type, te_expr *p1) {
const size_t size = sizeof(te_expr) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
assert(p1 && ARITY(type) == 1);
te_expr *ret = malloc(size);
ret->type = type;
ret->v.bound = 0;
ret->parameters[0] = p1;
return ret;
}

static te_expr *new_expr2(const int type, te_expr *p1, te_expr *p2) {
const size_t size = sizeof(te_expr) + sizeof(void*) + (IS_CLOSURE(type) ? sizeof(void*) : 0);
assert(p1 && p2 && ARITY(type) == 2);
te_expr *ret = malloc(size);
ret->type = type;
ret->v.bound = 0;
ret->parameters[0] = p1;
ret->parameters[1] = p2;
return ret;
}

static void te_free_parameters(te_expr *n) {
if (!n) return;
Expand Down Expand Up @@ -405,7 +426,7 @@ static te_expr *power(state *s) {
if (sign == 1) {
ret = base(s);
} else {
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
ret->v.f.f1 = negate;
}

Expand Down Expand Up @@ -433,19 +454,19 @@ static te_expr *factor(state *s) {

if (insertion) {
/* Make exponentiation go right-to-left. */
te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
te_expr *insert = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
insert->v.f.f2 = t;
insertion->parameters[1] = insert;
insertion = insert;
} else {
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret->v.f.f2 = t;
insertion = ret;
}
}

if (neg) {
ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
ret = new_expr1(TE_FUNCTION1 | TE_FLAG_PURE, ret);
ret->v.f.f1 = negate;
}

Expand All @@ -459,7 +480,7 @@ static te_expr *factor(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == pow)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
ret->v.f.f2 = t;
}

Expand All @@ -476,7 +497,7 @@ static te_expr *term(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == mul || s->v.f.f2 == divide || s->v.f.f2 == fmod)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
ret->v.f.f2 = t;
}

Expand All @@ -491,7 +512,7 @@ static te_expr *expr(state *s) {
while (s->type == TOK_INFIX && (s->v.f.f2 == add || s->v.f.f2 == sub)) {
te_fun2 t = s->v.f.f2;
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
ret->v.f.f2 = t;
}

Expand All @@ -505,7 +526,7 @@ static te_expr *list(state *s) {

while (s->type == TOK_SEP) {
next_token(s);
ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
ret = new_expr2(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
ret->v.f.f2 = comma;
}

Expand Down

0 comments on commit 3f08d52

Please sign in to comment.