diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 17:31:14 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 17:31:14 -0400 |
| commit | 9b16e1c06cad326b79e3aab8a95d5f97ea07af90 (patch) | |
| tree | 550621f67e985b2e3706ac3c5a68b903dad04849 | |
| parent | 559d7f24cf5d3a129fdf7b6cc7c75ecb6ca5a8c5 (diff) | |
Move int code to its file
| -rw-r--r-- | src/compile.c | 16 | ||||
| -rw-r--r-- | src/compile/integers.c | 18 | ||||
| -rw-r--r-- | src/compile/integers.h | 1 |
3 files changed, 20 insertions, 15 deletions
diff --git a/src/compile.c b/src/compile.c index 12717b98..4aba2008 100644 --- a/src/compile.c +++ b/src/compile.c @@ -23,7 +23,6 @@ #include "compile/types.h" #include "config.h" #include "environment.h" -#include "stdlib/integers.h" #include "stdlib/tables.h" #include "stdlib/text.h" #include "stdlib/util.h" @@ -95,20 +94,7 @@ Text_t compile(env_t *env, ast_t *ast) { // return Texts("_$", Match(ast, Var)->name); code_err(ast, "I don't know of any variable by this name"); } - case Int: { - const char *str = Match(ast, Int)->str; - OptionalInt_t int_val = Int$from_str(str); - if (int_val.small == 0) code_err(ast, "Failed to parse this integer"); - mpz_t i; - mpz_init_set_int(i, int_val); - if (mpz_cmpabs_ui(i, BIGGEST_SMALL_INT) <= 0) { - return Texts("I_small(", str, ")"); - } else if (mpz_cmp_si(i, INT64_MAX) <= 0 && mpz_cmp_si(i, INT64_MIN) >= 0) { - return Texts("Int$from_int64(", str, ")"); - } else { - return Texts("Int$from_str(\"", str, "\")"); - } - } + case Int: return compile_int(ast); case Num: { return Text$from_str(String(hex_double(Match(ast, Num)->n))); } diff --git a/src/compile/integers.c b/src/compile/integers.c index fc615c3b..23d3830c 100644 --- a/src/compile/integers.c +++ b/src/compile/integers.c @@ -6,10 +6,12 @@ #include "../stdlib/datatypes.h" #include "../stdlib/integers.h" #include "../stdlib/text.h" +#include "../stdlib/util.h" #include "../typecheck.h" #include "../types.h" #include "promotions.h" +public Text_t compile_int_to_type(env_t *env, ast_t *ast, type_t *target) { if (ast->tag != Int) { Text_t code = compile(env, ast); @@ -73,3 +75,19 @@ Text_t compile_int_to_type(env_t *env, ast_t *ast, type_t *target) { } return EMPTY_TEXT; } + +public +Text_t compile_int(ast_t *ast) { + const char *str = Match(ast, Int)->str; + OptionalInt_t int_val = Int$from_str(str); + if (int_val.small == 0) code_err(ast, "Failed to parse this integer"); + mpz_t i; + mpz_init_set_int(i, int_val); + if (mpz_cmpabs_ui(i, BIGGEST_SMALL_INT) <= 0) { + return Texts("I_small(", str, ")"); + } else if (mpz_cmp_si(i, INT64_MAX) <= 0 && mpz_cmp_si(i, INT64_MIN) >= 0) { + return Texts("Int$from_int64(", str, ")"); + } else { + return Texts("Int$from_str(\"", str, "\")"); + } +} diff --git a/src/compile/integers.h b/src/compile/integers.h index f4479b3e..06b47413 100644 --- a/src/compile/integers.h +++ b/src/compile/integers.h @@ -4,3 +4,4 @@ #include "../types.h" Text_t compile_int_to_type(env_t *env, ast_t *ast, type_t *target); +Text_t compile_int(ast_t *ast); |
