File: evaluator.c

package info (click to toggle)
autopartkit 1.07
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,060 kB
  • ctags: 198
  • sloc: ansic: 2,489; sh: 843; makefile: 103
file content (117 lines) | stat: -rw-r--r-- 2,541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Small infix evaluator, originally by Rune Holm <runeholm@student.matnat.uio.no>,
 * adapted for d-i by Steinar H. Gunderson <sgunderson@bigfoot.com>. Used with
 * permission.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>

#include "autopartkit.h"

static const char *origstr;
static const char *str;
static int token;

/* forward declarations, as it is called recursively */
static double expr(void);

static void eval_error(void)
{
    autopartkit_error(1, "Could not parse expression '%s', "
		      "error at position %d\n",
		      origstr, str - origstr);
}

static void match(int expected_token)
{
    if(token == expected_token)
        token = *str++; 
    else
        eval_error();
}

static double factor(void)
{
    char buf[256];
    int buflen = 0;
    
    double temp;
    if (token == '(') {
        match('(');
        temp = expr();
        match(')');
    } else if (isdigit(token) || token == '.' || token == '-') {
        do {
            buf[buflen++] = token;
            buf[buflen] = 0;
            token = *str++;
        } while (buflen < 255 && (isdigit(token) || token == '.'));

        if(!sscanf(buf, "%lf", &temp)) eval_error();
    } else if (token == '$') {
        token = *str++;
       	while (buflen < 255 && isalpha(token)) {
            buf[buflen++] = token;
            buf[buflen] = 0;
            token = *str++;
        };

        /* add more variables here as we need them */
        if (strcmp(buf, "RAMSIZE") == 0) {
	    assert(get_ram_size());
            temp = get_ram_size();
        } else {
            autopartkit_error(1, "Unknown variable $%s\n", buf);
        }
    } else {
        eval_error();
    }
    
    return temp;
}

static double term(void)
{
    double temp = factor();
    while ((token == '*') || (token == '/'))
        switch(token) {
        case '*': 
            match('*');
            temp *= factor();
            break;
        case '/':
            match('/');
            temp /= factor();
            break;
    }
    return temp;
}

static double expr(void)
{
    double temp = term();
    while ((token == '+') || (token == '-')) {
        switch(token) {
        case '+': 
            match('+');
            temp += term();
            break;
        case '-':
            match('-');
            temp -= term();
            break;
        }
    }
    return temp;
}

double evaluate(const char *expression)
{
    str = origstr = expression;
    token = *str++;
    return expr();
}