-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.c
134 lines (111 loc) · 3.07 KB
/
math.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <gnumake.h>
#define YES 1
#define NO 0
#define ERROR_PREFIX "*** ARITHMETIC ERROR: "
int plugin_is_GPL_compatible;
void throw_error(const char *str, int *error)
{
fprintf(stderr, ERROR_PREFIX "String '%s' is not representable as a long!\n", str);
*error = YES;
}
long strtol_or_die(const char *str, int *error)
{
char *end;
long ret;
errno = 0;
ret = strtol(str, &end, 0);
if (*end != '\0' ||
errno == EINVAL ||
errno == ERANGE) {
throw_error(str, error);
return 0;
}
*error = NO;
return ret;
}
char *add(const char *nm, unsigned argc, char **argv)
{
long i, temp, sum = 0;
int err = 0;
for (i = 0; i < argc; ++i) {
temp = strtol_or_die(argv[i], &err);
if (err == YES)
return NULL;
sum += temp;
}
int size = snprintf(NULL, 0, "%ld", sum) + 1;
char *buf = gmk_alloc(size + 1); // + 1 for terminating null
snprintf(buf, size, "%ld", sum);
return buf;
}
char *sub(const char *nm, unsigned argc, char **argv)
{
long i, temp, difference;
int err = 0;
difference = strtol_or_die(argv[0], &err);
if (err == YES)
return NULL;
for (i = 1; i < argc; ++i) {
temp = strtol_or_die(argv[i], &err);
if (err == YES)
return NULL;
difference -= temp;
}
int size = snprintf(NULL, 0, "%ld", difference) + 1;
char *buf = gmk_alloc(size + 1); // + 1 for terminating null
snprintf(buf, size, "%ld", difference);
return buf;
}
char *mul(const char *nm, unsigned argc, char **argv)
{
long i, temp, product = 1;
int err = 0;
for (i = 0; i < argc; ++i) {
temp = strtol_or_die(argv[i], &err);
if (err == YES)
return NULL;
product *= temp;
}
int size = snprintf(NULL, 0, "%ld", product) + 1;
char *buf = gmk_alloc(size + 1); // + 1 for terminating null
snprintf(buf, size, "%ld", product);
return buf;
}
char *divide(const char *nm, unsigned argc, char **argv)
{
long i, temp, quotient;
int err = 0;
quotient = strtol_or_die(argv[0], &err);
if (err == YES)
return NULL;
for (i = 1; i < argc; ++i) {
temp = strtol_or_die(argv[i], &err);
if (err == YES)
return NULL;
if (temp == 0) {
fprintf(stderr, ERROR_PREFIX "Cannot divide by zero!\n");
return NULL;
}
quotient /= temp;
}
int size = snprintf(NULL, 0, "%ld", quotient) + 1;
char *buf = gmk_alloc(size + 1); // + 1 for terminating null
snprintf(buf, size, "%ld", quotient);
return buf;
}
int math_gmk_setup()
{
// 0 means no limit
// name, func_ptr, min args, max args, expand or no?
gmk_add_function ("add", add, 2, 0, GMK_FUNC_DEFAULT);
gmk_add_function ("sub", sub, 2, 0, GMK_FUNC_DEFAULT);
gmk_add_function ("mul", mul, 2, 0, GMK_FUNC_DEFAULT);
gmk_add_function ("div", divide, 2, 0, GMK_FUNC_DEFAULT);
return 1;
}