From 412487f2df5f3244c123c468e14d7f5f4c1e4c38 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 15 Nov 2025 13:00:48 -0500 Subject: Constructive real functionality --- src/compile/expressions.c | 11 ++++++++++- src/compile/types.c | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'src/compile') diff --git a/src/compile/expressions.c b/src/compile/expressions.c index ce44dafc..28f91500 100644 --- a/src/compile/expressions.c +++ b/src/compile/expressions.c @@ -77,7 +77,16 @@ Text_t compile(env_t *env, ast_t *ast) { } case Int: return compile_int(ast); case Num: { - return Text$from_str(String(hex_double(Match(ast, Num)->n))); + // return Text$from_str(String(hex_double(Match(ast, Num)->n))); + Text_t original = Text$from_str(String(string_slice(ast->start, (size_t)(ast->end - ast->start)))); + Text_t roundtrip = Text$from_str(String(Match(ast, Num)->n)); + original = Text$replace(original, Text("_"), EMPTY_TEXT); + original = Text$without_suffix(original, Text(".")); + if (Text$equal_values(original, roundtrip)) { + return Texts("Real$from_float64(", Text$from_str(String(hex_double(Match(ast, Num)->n))), ")"); + } else { + return Texts("Real$parse(Text(\"", original, "\"), NULL)"); + } } case Not: { ast_t *value = Match(ast, Not)->value; diff --git a/src/compile/types.c b/src/compile/types.c index 94da1c49..24790e46 100644 --- a/src/compile/types.c +++ b/src/compile/types.c @@ -25,6 +25,7 @@ Text_t compile_type(type_t *t) { case BigIntType: return Text("Int_t"); case IntType: return Texts("Int", (int32_t)Match(t, IntType)->bits, "_t"); case FloatType: return Texts("Float", (int32_t)Match(t, FloatType)->bits, "_t"); + case RealType: return Text("Real_t"); case TextType: { DeclareMatch(text, t, TextType); if (!text->lang || streq(text->lang, "Text")) return Text("Text_t"); @@ -65,6 +66,7 @@ Text_t compile_type(type_t *t) { case IntType: case BigIntType: case FloatType: + case RealType: case BoolType: case ByteType: case ListType: @@ -96,6 +98,7 @@ Text_t compile_type_info(type_t *t) { case IntType: case BigIntType: case FloatType: + case RealType: case CStringType: return Texts("&", type_to_text(t), "$info"); case TextType: { DeclareMatch(text, t, TextType); -- cgit v1.2.3