aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-19 01:22:17 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-19 01:22:17 -0400
commit08d6385674d7035f4e41e2f30e4f65f39c1cbf7e (patch)
treed30d9fd4476a9334c7d0e76ea74dbed5069a51e6
parent70d6a18f59548a96f6bf7080d637c0f1f7519b2a (diff)
Add -deg suffix: `90deg` for specifying degrees. Also fixed percent
suffix
-rw-r--r--parse.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/parse.c b/parse.c
index c8e91d00..831b40cc 100644
--- a/parse.c
+++ b/parse.c
@@ -4,6 +4,7 @@
#include <gmp.h>
#include <libgen.h>
#include <linux/limits.h>
+#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -22,6 +23,7 @@
#define PARSE_CACHE_SIZE 100
#endif
+static const double RADIANS_PER_DEGREE = 0.0174532925199432957692369076848861271344287188854172545609719144;
static const char closing[128] = {['(']=')', ['[']=']', ['<']='>', ['{']='}'};
typedef struct {
@@ -443,7 +445,6 @@ PARSER(parse_int) {
const char *start = pos;
(void)match(&pos, "-");
if (!isdigit(*pos)) return false;
- int64_t i = 0;
if (match(&pos, "0x")) { // Hex
pos += strspn(pos, "0123456789abcdefABCDEF_");
} else if (match(&pos, "0b")) { // Binary
@@ -463,8 +464,11 @@ PARSER(parse_int) {
return NULL;
if (match(&pos, "%")) {
- double d = (double)i / 100.;
- return NewAST(ctx->file, start, pos, Num, .n=d, .bits=64);
+ double n = strtod(str, NULL) / 100.;
+ return NewAST(ctx->file, start, pos, Num, .n=n, .bits=64);
+ } else if (match(&pos, "deg")) {
+ double n = strtod(str, NULL) * RADIANS_PER_DEGREE;
+ return NewAST(ctx->file, start, pos, Num, .n=n, .bits=64);
}
match(&pos, "_");
@@ -631,6 +635,8 @@ PARSER(parse_num) {
if (match(&pos, "%"))
d /= 100.;
+ else if (match(&pos, "deg"))
+ d *= RADIANS_PER_DEGREE;
return NewAST(ctx->file, start, pos, Num, .n=d, .bits=bits);
}