aboutsummaryrefslogtreecommitdiff
path: root/src/compile
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-15 13:00:48 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-15 13:00:48 -0500
commit412487f2df5f3244c123c468e14d7f5f4c1e4c38 (patch)
tree4c15895c80f23ed9d27d82d9c4b0a9f10f011565 /src/compile
parentcf90418e05ced81499318c8079362053369af810 (diff)
Constructive real functionality
Diffstat (limited to 'src/compile')
-rw-r--r--src/compile/expressions.c11
-rw-r--r--src/compile/types.c3
2 files changed, 13 insertions, 1 deletions
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);