From 9b16e1c06cad326b79e3aab8a95d5f97ea07af90 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 24 Aug 2025 17:31:14 -0400 Subject: Move int code to its file --- src/compile.c | 16 +--------------- src/compile/integers.c | 18 ++++++++++++++++++ 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); -- cgit v1.2.3