diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-19 01:22:17 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-19 01:22:17 -0400 |
| commit | 08d6385674d7035f4e41e2f30e4f65f39c1cbf7e (patch) | |
| tree | d30d9fd4476a9334c7d0e76ea74dbed5069a51e6 /parse.c | |
| parent | 70d6a18f59548a96f6bf7080d637c0f1f7519b2a (diff) | |
Add -deg suffix: `90deg` for specifying degrees. Also fixed percent
suffix
Diffstat (limited to 'parse.c')
| -rw-r--r-- | parse.c | 12 |
1 files changed, 9 insertions, 3 deletions
@@ -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); } |
