aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.c3
-rw-r--r--src/ast.h5
-rw-r--r--src/compile.c47
-rw-r--r--src/environment.c52
-rw-r--r--src/parse.c38
-rw-r--r--src/stdlib/datatypes.h3
-rw-r--r--src/stdlib/decimals.c293
-rw-r--r--src/stdlib/decimals.h58
-rw-r--r--src/stdlib/print.c93
-rw-r--r--src/stdlib/print.h4
-rw-r--r--src/stdlib/stdlib.c29
-rw-r--r--src/stdlib/tomo.h1
-rw-r--r--src/typecheck.c7
-rw-r--r--src/types.c12
-rw-r--r--src/types.h3
15 files changed, 604 insertions, 44 deletions
diff --git a/src/ast.c b/src/ast.c
index 5d8b2021..192aba0b 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -147,6 +147,7 @@ CORD ast_to_sexp(ast_t *ast)
T(Bool, "(Bool ", data.b ? "yes" : "no", ")")
T(Var, "(Var ", CORD_quoted(data.name), ")")
T(Int, "(Int ", CORD_quoted(ast_source(ast)), ")")
+ T(Dec, "(Dec ", CORD_quoted(ast_source(ast)), ")")
T(Num, "(Num ", CORD_quoted(ast_source(ast)), ")")
T(TextLiteral, CORD_quoted(data.cord))
T(TextJoin, "(Text", data.lang ? CORD_all(" :lang ", CORD_quoted(data.lang)) : CORD_EMPTY, ast_list_to_sexp(data.children), ")")
@@ -230,7 +231,7 @@ const char *ast_source(ast_t *ast)
PUREFUNC bool is_idempotent(ast_t *ast)
{
switch (ast->tag) {
- case Int: case Bool: case Num: case Var: case None: case TextLiteral: return true;
+ case Int: case Bool: case Dec: case Num: case Var: case None: case TextLiteral: return true;
case Index: {
DeclareMatch(index, ast, Index);
return is_idempotent(index->indexed) && index->index != NULL && is_idempotent(index->index);
diff --git a/src/ast.h b/src/ast.h
index 486b5818..a1137bf3 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -125,7 +125,7 @@ struct type_ast_s {
typedef enum {
Unknown = 0,
None, Bool, Var,
- Int, Num,
+ Int, Dec, Num,
TextLiteral, TextJoin,
Path,
Declare, Assign,
@@ -176,6 +176,9 @@ struct ast_s {
double n;
} Num;
struct {
+ const char *str;
+ } Dec;
+ struct {
CORD cord;
} TextLiteral;
struct {
diff --git a/src/compile.c b/src/compile.c
index 2fa9ed43..3bc2fa4a 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -517,22 +517,22 @@ static CORD compile_update_assignment(env_t *env, ast_t *ast)
CORD update_assignment = CORD_EMPTY;
switch (ast->tag) {
case PlusUpdate: {
- if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType)
+ if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType)
update_assignment = CORD_all(lhs, " += ", compile_to_type(env, update.rhs, lhs_t), ";");
break;
}
case MinusUpdate: {
- if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType)
+ if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType)
update_assignment = CORD_all(lhs, " -= ", compile_to_type(env, update.rhs, lhs_t), ";");
break;
}
case MultiplyUpdate: {
- if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType)
+ if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType)
update_assignment = CORD_all(lhs, " *= ", compile_to_type(env, update.rhs, lhs_t), ";");
break;
}
case DivideUpdate: {
- if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType)
+ if (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType)
update_assignment = CORD_all(lhs, " /= ", compile_to_type(env, update.rhs, lhs_t), ";");
break;
}
@@ -673,12 +673,12 @@ static CORD compile_binary_op(env_t *env, ast_t *ast)
return CORD_all("pow(", lhs, ", ", rhs, ")");
}
case Multiply: {
- if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType)
+ if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType && overall_t->tag != DecType)
code_err(ast, "Math operations are only supported for values of the same numeric type, not ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
return CORD_all("(", lhs, " * ", rhs, ")");
}
case Divide: {
- if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType)
+ if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType && overall_t->tag != DecType)
code_err(ast, "Math operations are only supported for values of the same numeric type, not ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
return CORD_all("(", lhs, " / ", rhs, ")");
}
@@ -693,14 +693,14 @@ static CORD compile_binary_op(env_t *env, ast_t *ast)
return CORD_all("((((", lhs, ")-1) % (", rhs, ")) + 1)");
}
case Plus: {
- if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType)
+ if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType && overall_t->tag != DecType)
code_err(ast, "Math operations are only supported for values of the same numeric type, not ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
return CORD_all("(", lhs, " + ", rhs, ")");
}
case Minus: {
if (overall_t->tag == SetType)
return CORD_all("Table$without(", lhs, ", ", rhs, ", ", compile_type_info(overall_t), ")");
- if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType)
+ if (overall_t->tag != IntType && overall_t->tag != NumType && overall_t->tag != ByteType && overall_t->tag != DecType)
code_err(ast, "Math operations are only supported for values of the same numeric type, not ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
return CORD_all("(", lhs, " - ", rhs, ")");
}
@@ -804,6 +804,7 @@ CORD compile_type(type_t *t)
case ByteType: return "Byte_t";
case CStringType: return "const char*";
case BigIntType: return "Int_t";
+ case DecType: return "Dec_t";
case IntType: return CORD_all("Int", String(Match(t, IntType)->bits), "_t");
case NumType: return Match(t, NumType)->bits == TYPE_NBITS64 ? "Num_t" : CORD_all("Num", String(Match(t, NumType)->bits), "_t");
case TextType: {
@@ -846,7 +847,7 @@ CORD compile_type(type_t *t)
return compile_type(nonnull);
case TextType:
return Match(nonnull, TextType)->lang ? compile_type(nonnull) : "OptionalText_t";
- case IntType: case BigIntType: case NumType: case BoolType: case ByteType:
+ case IntType: case BigIntType: case DecType: case NumType: case BoolType: case ByteType:
case ListType: case TableType: case SetType:
return CORD_all("Optional", compile_type(nonnull));
case StructType: {
@@ -1011,6 +1012,8 @@ CORD check_none(type_t *t, CORD value)
return CORD_all("({(", value, ").$tag == 0;})");
else
return CORD_all("((", value, ") == 0)");
+ } else if (t->tag == DecType) {
+ return CORD_all("((int64_t)(", value, ") == -1)");
}
print_err("Optional check not implemented for: ", type_to_str(t));
return CORD_EMPTY;
@@ -1397,28 +1400,28 @@ static CORD _compile_statement(env_t *env, ast_t *ast)
case PlusUpdate: {
DeclareMatch(update, ast, PlusUpdate);
type_t *lhs_t = get_type(env, update->lhs);
- if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType))
+ if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType))
return CORD_all(compile_lvalue(env, update->lhs), " += ", compile_to_type(env, update->rhs, lhs_t), ";");
return compile_update_assignment(env, ast);
}
case MinusUpdate: {
DeclareMatch(update, ast, MinusUpdate);
type_t *lhs_t = get_type(env, update->lhs);
- if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType))
+ if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType))
return CORD_all(compile_lvalue(env, update->lhs), " -= ", compile_to_type(env, update->rhs, lhs_t), ";");
return compile_update_assignment(env, ast);
}
case MultiplyUpdate: {
DeclareMatch(update, ast, MultiplyUpdate);
type_t *lhs_t = get_type(env, update->lhs);
- if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType))
+ if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType))
return CORD_all(compile_lvalue(env, update->lhs), " *= ", compile_to_type(env, update->rhs, lhs_t), ";");
return compile_update_assignment(env, ast);
}
case DivideUpdate: {
DeclareMatch(update, ast, DivideUpdate);
type_t *lhs_t = get_type(env, update->lhs);
- if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType))
+ if (is_idempotent(update->lhs) && (lhs_t->tag == IntType || lhs_t->tag == NumType || lhs_t->tag == ByteType || lhs_t->tag == DecType))
return CORD_all(compile_lvalue(env, update->lhs), " /= ", compile_to_type(env, update->rhs, lhs_t), ";");
return compile_update_assignment(env, ast);
}
@@ -2047,7 +2050,7 @@ CORD expr_as_text(CORD expr, type_t *t, CORD color)
// NOTE: this cannot use stack(), since bools may actually be bit fields:
return CORD_all("Bool$as_text((Bool_t[1]){", expr, "}, ", color, ", &Bool$info)");
case CStringType: return CORD_all("CString$as_text(stack(", expr, "), ", color, ", &CString$info)");
- case BigIntType: case IntType: case ByteType: case NumType: {
+ case BigIntType: case DecType: case IntType: case ByteType: case NumType: {
CORD name = type_to_cord(t);
return CORD_all(name, "$as_text(stack(", expr, "), ", color, ", &", name, "$info)");
}
@@ -2382,6 +2385,9 @@ CORD compile_int_to_type(env_t *env, ast_t *ast, type_t *target)
if (target->tag == BigIntType)
return compile(env, ast);
+ if (target->tag == DecType)
+ return compile(env, WrapAST(ast, Dec, .str=Match(ast, Int)->str));
+
if (target->tag == OptionalType && Match(target, OptionalType)->type)
return compile_int_to_type(env, ast, Match(target, OptionalType)->type);
@@ -2597,6 +2603,7 @@ CORD compile_none(type_t *t)
switch (t->tag) {
case BigIntType: return "NONE_INT";
+ case DecType: return "NONE_DEC";
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return "NONE_INT8";
@@ -2640,6 +2647,7 @@ CORD compile_empty(type_t *t)
switch (t->tag) {
case BigIntType: return "I(0)";
+ case DecType: return "0.0DD";
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return "I8(0)";
@@ -2769,6 +2777,9 @@ CORD compile(env_t *env, ast_t *ast)
case Num: {
return String(hex_double(Match(ast, Num)->n));
}
+ case Dec: {
+ return CORD_all(Match(ast, Dec)->str, strchr(Match(ast, Dec)->str, '.') ? CORD_EMPTY : ".", "DD");
+ }
case Not: {
ast_t *value = Match(ast, Not)->value;
type_t *t = get_type(env, value);
@@ -2805,7 +2816,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all(b->code, "(", compile_arguments(env, ast, fn->args, new(arg_ast_t, .value=value)), ")");
}
- if (t->tag == IntType || t->tag == NumType)
+ if (t->tag == IntType || t->tag == NumType || t->tag == DecType)
return CORD_all("-(", compile(env, value), ")");
code_err(ast, "I don't know how to get the negative value of type ", type_to_str(t));
@@ -2862,7 +2873,7 @@ CORD compile(env_t *env, ast_t *ast)
switch (operand_t->tag) {
case BigIntType:
return CORD_all(ast->tag == Equals ? CORD_EMPTY : "!", "Int$equal_value(", lhs, ", ", rhs, ")");
- case BoolType: case ByteType: case IntType: case NumType: case PointerType: case FunctionType:
+ case BoolType: case ByteType: case IntType: case NumType: case PointerType: case FunctionType: case DecType:
return CORD_all("(", lhs, ast->tag == Equals ? " == " : " != ", rhs, ")");
default:
return CORD_all(ast->tag == Equals ? CORD_EMPTY : "!",
@@ -2898,7 +2909,7 @@ CORD compile(env_t *env, ast_t *ast)
switch (operand_t->tag) {
case BigIntType:
return CORD_all("(Int$compare_value(", lhs, ", ", rhs, ") ", op, " 0)");
- case BoolType: case ByteType: case IntType: case NumType: case PointerType: case FunctionType:
+ case BoolType: case ByteType: case IntType: case NumType: case PointerType: case FunctionType: case DecType:
return CORD_all("(", lhs, " ", op, " ", rhs, ")");
default:
return CORD_all("(generic_compare(stack(", lhs, "), stack(", rhs, "), ",
@@ -3986,7 +3997,7 @@ CORD compile_type_info(type_t *t)
else if (t == PATH_TYPE_TYPE) return "&PathType$info";
switch (t->tag) {
- case BoolType: case ByteType: case IntType: case BigIntType: case NumType: case CStringType:
+ case BoolType: case ByteType: case IntType: case BigIntType: case DecType: case NumType: case CStringType:
return CORD_all("&", type_to_cord(t), "$info");
case TextType: {
DeclareMatch(text, t, TextType);
diff --git a/src/environment.c b/src/environment.c
index 0dbe015d..2ba99baa 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -67,6 +67,7 @@ env_t *global_env(bool source_mapping)
TEXT_TYPE = bind_type(env, "Text", Type(TextType, .lang="Text", .env=namespace_env(env, "Text")));
(void)bind_type(env, "Int", Type(BigIntType));
(void)bind_type(env, "Int32", Type(IntType, .bits=TYPE_IBITS32));
+ (void)bind_type(env, "Dec", Type(DecType));
(void)bind_type(env, "Memory", Type(MemoryType));
PATH_TYPE_TYPE = declare_type(env, "enum PathType(Relative, Absolute, Home)");
PATH_TYPE = declare_type(env, "struct Path(type:PathType, components:[Text])");
@@ -248,6 +249,18 @@ env_t *global_env(bool source_mapping)
F_opt(tan), F(tanh), F_opt(tgamma), F(trunc), F_opt(y0), F_opt(y1),
F2(atan2), F2(copysign), F2(fdim), F2(hypot), F2(nextafter),
)},
+ {"Dec", Type(DecType), "Dec_t", "Dec$info", TypedList(ns_entry_t,
+ {"divided_by", "Dec$divided_by", "func(x,y:Dec -> Dec)"},
+ {"minus", "Dec$minus", "func(x,y:Dec -> Dec)"},
+ {"modulo", "Dec$modulo", "func(x,y:Dec -> Dec)"},
+ {"modulo1", "Dec$modulo1", "func(x,y:Dec -> Dec)"},
+ {"negative", "Dec$negative", "func(x:Dec -> Dec)"},
+ {"parse", "Dec$parse", "func(text:Text -> Dec?)"},
+ {"plus", "Dec$plus", "func(x,y:Dec -> Dec)"},
+ {"power", "Dec$power", "func(base,exponent:Dec -> Dec)"},
+ {"round", "Dec$round", "func(d:Dec, digits:Int=0 -> Dec)"},
+ {"times", "Dec$times", "func(x,y:Dec -> Dec)"},
+ )},
#undef F2
#undef F_opt
#undef F
@@ -435,14 +448,16 @@ env_t *global_env(bool source_mapping)
{"Bool$from_int16", "func(i:Int16 -> Bool)"},
{"Bool$from_int32", "func(i:Int32 -> Bool)"},
{"Bool$from_int64", "func(i:Int64 -> Bool)"},
- {"Bool$from_int", "func(i:Int -> Bool)"});
+ {"Bool$from_int", "func(i:Int -> Bool)"},
+ {"Dec$as_bool", "func(d:Dec -> Bool)"});
ADD_CONSTRUCTORS("Byte",
{"Byte$from_bool", "func(b:Bool -> Byte)"},
{"Byte$from_int8", "func(i:Int8 -> Byte)"},
{"Byte$from_int16", "func(i:Int16, truncate=no -> Byte)"},
{"Byte$from_int32", "func(i:Int32, truncate=no -> Byte)"},
{"Byte$from_int64", "func(i:Int64, truncate=no -> Byte)"},
- {"Byte$from_int", "func(i:Int, truncate=no -> Byte)"});
+ {"Byte$from_int", "func(i:Int, truncate=no -> Byte)"},
+ {"Dec$as_byte", "func(d:Dec, truncate=no -> Byte)"});
ADD_CONSTRUCTORS("Int",
{"Int$from_bool", "func(b:Bool -> Int)"},
{"Int$from_byte", "func(b:Byte -> Int)"},
@@ -451,7 +466,8 @@ env_t *global_env(bool source_mapping)
{"Int$from_int32", "func(i:Int32 -> Int)"},
{"Int$from_int64", "func(i:Int64 -> Int)"},
{"Int$from_num", "func(n:Num, truncate=no -> Int)"},
- {"Int$from_num32", "func(n:Num32, truncate=no -> Int)"});
+ {"Int$from_num32", "func(n:Num32, truncate=no -> Int)"},
+ {"Dec$as_int", "func(d:Dec, truncate=no -> Int)"});
ADD_CONSTRUCTORS("Int64",
{"Int64$from_bool", "func(b:Bool -> Int64)"},
{"Int64$from_byte", "func(b:Byte -> Int64)"},
@@ -460,7 +476,8 @@ env_t *global_env(bool source_mapping)
{"Int64$from_int32", "func(i:Int32 -> Int64)"},
{"Int64$from_int", "func(i:Int, truncate=no -> Int64)"},
{"Int64$from_num", "func(n:Num, truncate=no -> Int64)"},
- {"Int64$from_num32", "func(n:Num32, truncate=no -> Int64)"});
+ {"Int64$from_num32", "func(n:Num32, truncate=no -> Int64)"},
+ {"Dec$as_int64", "func(d:Dec, truncate=no -> Int64)"});
ADD_CONSTRUCTORS("Int32",
{"Int32$from_bool", "func(b:Bool -> Int32)"},
{"Int32$from_byte", "func(b:Byte -> Int32)"},
@@ -469,7 +486,8 @@ env_t *global_env(bool source_mapping)
{"Int32$from_int64", "func(i:Int64, truncate=no -> Int32)"},
{"Int32$from_int", "func(i:Int, truncate=no -> Int32)"},
{"Int32$from_num", "func(n:Num, truncate=no -> Int32)"},
- {"Int32$from_num32", "func(n:Num32, truncate=no -> Int32)"});
+ {"Int32$from_num32", "func(n:Num32, truncate=no -> Int32)"},
+ {"Dec$as_int32", "func(d:Dec, truncate=no -> Int32)"});
ADD_CONSTRUCTORS("Int16",
{"Int16$from_bool", "func(b:Bool -> Int16)"},
{"Int16$from_byte", "func(b:Byte -> Int16)"},
@@ -478,7 +496,8 @@ env_t *global_env(bool source_mapping)
{"Int16$from_int64", "func(i:Int64, truncate=no -> Int16)"},
{"Int16$from_int", "func(i:Int, truncate=no -> Int16)"},
{"Int16$from_num", "func(n:Num, truncate=no -> Int16)"},
- {"Int16$from_num32", "func(n:Num32, truncate=no -> Int16)"});
+ {"Int16$from_num32", "func(n:Num32, truncate=no -> Int16)"},
+ {"Dec$as_int16", "func(d:Dec, truncate=no -> Int16)"});
ADD_CONSTRUCTORS("Int8",
{"Int8$from_bool", "func(b:Bool -> Int8)"},
{"Int8$from_byte", "func(b:Byte -> Int8)"},
@@ -487,7 +506,8 @@ env_t *global_env(bool source_mapping)
{"Int8$from_int64", "func(i:Int64, truncate=no -> Int8)"},
{"Int8$from_int", "func(i:Int, truncate=no -> Int8)"},
{"Int8$from_num", "func(n:Num, truncate=no -> Int8)"},
- {"Int8$from_num32", "func(n:Num32, truncate=no -> Int8)"});
+ {"Int8$from_num32", "func(n:Num32, truncate=no -> Int8)"},
+ {"Dec$as_int8", "func(d:Dec, truncate=no -> Int8)"});
ADD_CONSTRUCTORS("Num",
{"Num$from_bool", "func(b:Bool -> Num)"},
{"Num$from_byte", "func(b:Byte -> Num)"},
@@ -496,7 +516,8 @@ env_t *global_env(bool source_mapping)
{"Num$from_int32", "func(i:Int32 -> Num)"},
{"Num$from_int64", "func(i:Int64, truncate=no -> Num)"},
{"Num$from_int", "func(i:Int, truncate=no -> Num)"},
- {"Num$from_num32", "func(n:Num32 -> Num)"});
+ {"Num$from_num32", "func(n:Num32 -> Num)"},
+ {"Dec$as_num", "func(d:Dec -> Num)"});
ADD_CONSTRUCTORS("Num32",
{"Num32$from_bool", "func(b:Bool -> Num32)"},
{"Num32$from_byte", "func(b:Byte -> Num32)"},
@@ -505,7 +526,18 @@ env_t *global_env(bool source_mapping)
{"Num32$from_int32", "func(i:Int32, truncate=no -> Num32)"},
{"Num32$from_int64", "func(i:Int64, truncate=no -> Num32)"},
{"Num32$from_int", "func(i:Int, truncate=no -> Num32)"},
- {"Num32$from_num", "func(n:Num -> Num32)"});
+ {"Num32$from_num", "func(n:Num -> Num32)"},
+ {"Dec$as_num32", "func(d:Dec -> Num32)"});
+ ADD_CONSTRUCTORS("Dec",
+ {"Dec$from_bool", "func(b:Bool -> Dec)"},
+ {"Dec$from_byte", "func(b:Byte -> Dec)"},
+ {"Dec$from_int8", "func(i:Int8 -> Dec)"},
+ {"Dec$from_int16", "func(i:Int16 -> Dec)"},
+ {"Dec$from_int32", "func(i:Int32 -> Dec)"},
+ {"Dec$from_int64", "func(i:Int64 -> Dec)"},
+ {"Dec$from_num", "func(n:Num -> Dec)"},
+ {"Dec$from_num32", "func(n:Num32 -> Dec)"},
+ {"Dec$from_int", "func(i:Int -> Dec)"});
ADD_CONSTRUCTORS("Path",
{"Path$escape_text", "func(text:Text -> Path)"},
{"Path$escape_path", "func(path:Path -> Path)"},
@@ -699,7 +731,7 @@ env_t *get_namespace_by_type(env_t *env, type_t *t)
case ListType: return NULL;
case TableType: return NULL;
case CStringType:
- case BoolType: case IntType: case BigIntType: case NumType: case ByteType: {
+ case BoolType: case IntType: case BigIntType: case DecType: case NumType: case ByteType: {
binding_t *b = get_binding(env, CORD_to_const_char_star(type_to_cord(t)));
assert(b);
return Match(b->type, TypeInfoType)->env;
diff --git a/src/parse.c b/src/parse.c
index e2f267c1..be1642cf 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -103,6 +103,7 @@ static PARSER(parse_assignment);
static PARSER(parse_block);
static PARSER(parse_bool);
static PARSER(parse_convert_def);
+static PARSER(parse_dec);
static PARSER(parse_declaration);
static PARSER(parse_defer);
static PARSER(parse_do);
@@ -656,6 +657,36 @@ type_ast_t *parse_type(parse_ctx_t *ctx, const char *pos) {
return type;
}
+PARSER(parse_dec) {
+ const char *start = pos;
+ bool negative = false;
+ if (match(&pos, "-$"))
+ negative = true;
+ else if (!match(&pos, "$"))
+ return NULL;
+
+ if (!isdigit(*pos) && *pos != '.') return NULL;
+ else if (*pos == '.' && !isdigit(pos[1])) return NULL;
+
+ size_t len = strspn(pos, "0123456789_");
+ if (strncmp(pos+len, "..", 2) == 0)
+ return NULL;
+ else if (pos[len] == '.')
+ len += 1 + strspn(pos + len + 1, "0123456789_");
+
+ char *buf = GC_MALLOC_ATOMIC(negative + len + 1);
+ memset(buf, 0, len+1);
+ char *dest = buf;
+ if (negative) *(dest++) = '-';
+ for (char *src = (char*)pos; src < pos+len; ++src) {
+ if (*src != '_') *(dest++) = *src;
+ }
+ *(dest++) = '\0';
+ pos += len;
+
+ return NewAST(ctx->file, start, pos, Dec, .str=buf);
+}
+
PARSER(parse_num) {
const char *start = pos;
bool negative = match(&pos, "-");
@@ -666,7 +697,7 @@ PARSER(parse_num) {
if (strncmp(pos+len, "..", 2) == 0)
return NULL;
else if (pos[len] == '.')
- len += 1 + strspn(pos + len + 1, "0123456789");
+ len += 1 + strspn(pos + len + 1, "0123456789_");
else if (pos[len] != 'e' && pos[len] != 'f' && pos[len] != '%')
return NULL;
if (pos[len] == 'e') {
@@ -838,7 +869,7 @@ ast_t *parse_field_suffix(parse_ctx_t *ctx, ast_t *lhs) {
if (*pos == '.') return NULL;
whitespace(&pos);
bool dollar = match(&pos, "$");
- const char* field = get_id(&pos);
+ const char *field = get_id(&pos);
if (!field) return NULL;
if (dollar) field = String("$", field);
return NewAST(ctx->file, lhs->start, pos, FieldAccess, .fielded=lhs, .field=field);
@@ -1296,6 +1327,8 @@ PARSER(parse_text) {
} else if (match(&pos, "'")) { // Single quote
open_quote = '\'', close_quote = '\'', open_interp = '$';
} else if (match(&pos, "$")) { // Customized strings
+ if (isdigit(*pos))
+ return NULL;
lang = get_id(&pos);
// $"..." or $@"...."
static const char *interp_chars = "~!@#$%^&*+=\\?";
@@ -1464,6 +1497,7 @@ PARSER(parse_term_no_suffix) {
(void)(
false
|| (term=parse_none(ctx, pos))
+ || (term=parse_dec(ctx, pos)) // Must come before int
|| (term=parse_num(ctx, pos)) // Must come before int
|| (term=parse_int(ctx, pos))
|| (term=parse_negative(ctx, pos)) // Must come after num/int
diff --git a/src/stdlib/datatypes.h b/src/stdlib/datatypes.h
index fce1ea74..8ca2876d 100644
--- a/src/stdlib/datatypes.h
+++ b/src/stdlib/datatypes.h
@@ -22,6 +22,9 @@
#define Num_t double
#define Num32_t float
+#define Dec_t _Decimal64
+#define OptionalDec_t _Decimal64
+
#define Int64_t int64_t
#define Int32_t int32_t
#define Int16_t int16_t
diff --git a/src/stdlib/decimals.c b/src/stdlib/decimals.c
new file mode 100644
index 00000000..4a3f0c08
--- /dev/null
+++ b/src/stdlib/decimals.c
@@ -0,0 +1,293 @@
+// Integer type infos and methods
+#include <stdio.h> // Must be before gmp.h
+
+#include <ctype.h>
+#include <gc.h>
+#include <math.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "bytes.h"
+#include "datatypes.h"
+#include "decimals.h"
+#include "integers.h"
+#include "lists.h"
+#include "nums.h"
+#include "optionals.h"
+#include "print.h"
+#include "text.h"
+#include "types.h"
+
+public int Dec$print(FILE *f, Dec_t d) {
+ return fprint(f, d);
+}
+
+public Text_t Dec$value_as_text(Dec_t d) {
+ return Text$from_str(String(d));
+}
+
+public Text_t Dec$as_text(const void *d, bool colorize, const TypeInfo_t *info) {
+ (void)info;
+ if (!d) return Text("Dec");
+ Text_t text = Text$from_str(String(*(Dec_t*)d));
+ if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m"));
+ return text;
+}
+
+static bool Dec$is_none(const void *d, const TypeInfo_t *info)
+{
+ (void)info;
+ return *(int64_t*)d == -1;
+}
+
+public CONSTFUNC int32_t Dec$compare_value(const Dec_t x, const Dec_t y) {
+ return (x > y) - (x < y);
+}
+
+public CONSTFUNC int32_t Dec$compare(const void *x, const void *y, const TypeInfo_t *info) {
+ (void)info;
+ return Dec$compare_value(*(Dec_t*)x, *(Dec_t*)y);
+}
+
+public CONSTFUNC bool Dec$equal_value(const Dec_t x, const Dec_t y) {
+ return x == y;
+}
+
+public CONSTFUNC bool Dec$equal(const void *x, const void *y, const TypeInfo_t *info) {
+ (void)info;
+ return *(_Decimal64*)x == *(_Decimal64*)y;
+}
+
+public CONSTFUNC Dec_t Dec$plus(Dec_t x, Dec_t y) {
+ return x + y;
+}
+
+public CONSTFUNC Dec_t Dec$negative(Dec_t x) {
+ return -x;
+}
+
+public CONSTFUNC Dec_t Dec$minus(Dec_t x, Dec_t y) {
+ return x - y;
+}
+
+public CONSTFUNC Dec_t Dec$times(Dec_t x, Dec_t y) {
+ return x * y;
+}
+
+public CONSTFUNC Dec_t Dec$divided_by(Dec_t x, Dec_t y) {
+ return x / y;
+}
+
+public CONSTFUNC Dec_t Dec$modulo(Dec_t x, Dec_t modulus) {
+ // TODO: improve the accuracy of this approach:
+ return (Dec_t)Num$mod((double)x, (double)modulus);
+}
+
+public CONSTFUNC Dec_t Dec$modulo1(Dec_t x, Dec_t modulus) {
+ // TODO: improve the accuracy of this approach:
+ return (Dec_t)Num$mod1((double)x, (double)modulus);
+}
+
+public PUREFUNC OptionalDec_t Dec$from_str(const char *str) {
+ _Decimal64 n = 0.0DD;
+ const char *p = str;
+ bool negative = (*p == '-');
+ if (negative)
+ p += 1;
+ for (; *p; p++) {
+ if (*p == '.') break;
+ if (*p == '_') continue;
+ if (!isdigit(*p)) return NONE_DEC;
+ n = 10.0DD * n + (_Decimal64)(*p - '0');
+ }
+ _Decimal64 denominator = 1.0DD;
+ for (; *p; p++) {
+ if (*p == '_') continue;
+ if (!isdigit(*p)) return NONE_DEC;
+ n = 10.0DD * n + (_Decimal64)(*p - '0');
+ denominator *= 0.1DD;
+ }
+ return n * denominator;
+}
+
+public CONSTFUNC Dec_t Dec$from_int64(int64_t i) {
+ return (_Decimal64)i;
+}
+
+public Dec_t Dec$from_int(Int_t i) {
+ if likely (i.small & 1L) {
+ return Dec$from_int64(i.small >> 2L);
+ }
+ Text_t text = Int$value_as_text(i);
+ const char *str = Text$as_c_string(text);
+ return Dec$from_str(str);
+}
+
+CONSTFUNC public Dec_t Dec$from_num(double n) {
+ return (_Decimal64)n;
+}
+
+public Int_t Dec$as_int(Dec_t d, bool truncate) {
+ char *str = String(d);
+ char *decimal = strchr(str, '.');
+ if (!truncate && decimal)
+ fail("Could not convert to an integer without truncation: ", str);
+ *decimal = '\0';
+ return Int$from_str(str);
+}
+
+public int64_t Dec$as_int64(Dec_t d, bool truncate) {
+ return Int64$from_int(Dec$as_int(d, truncate), truncate);
+}
+
+public int32_t Dec$as_int32(Dec_t d, bool truncate) {
+ return Int32$from_int(Dec$as_int(d, truncate), truncate);
+}
+
+public int16_t Dec$as_int16(Dec_t d, bool truncate) {
+ return Int16$from_int(Dec$as_int(d, truncate), truncate);
+}
+
+public int8_t Dec$as_int8(Dec_t d, bool truncate) {
+ return Int8$from_int(Dec$as_int(d, truncate), truncate);
+}
+
+public Byte_t Dec$as_byte(Dec_t d, bool truncate) {
+ return Byte$from_int(Dec$as_int(d, truncate), truncate);
+}
+
+CONSTFUNC public bool Dec$as_bool(Dec_t d) {
+ return d != 0.0DD;
+}
+
+public double Dec$as_num(Dec_t d) {
+ const char *str = String(d);
+ return strtod(str, NULL);
+}
+
+#define NAN_MASK 0x7C00000000000000UL
+#define INF_MASK 0x7800000000000000UL
+#define DEC_BITS(n) ((union { uint64_t bits; _Decimal64 d; }){.d=n}).bits
+
+static bool Dec$isfinite(Dec_t d) {
+ uint64_t bits = DEC_BITS(d);
+ return (((bits & NAN_MASK) != NAN_MASK) &&
+ ((bits & INF_MASK) != INF_MASK));
+}
+
+static bool Dec$isnan(Dec_t d) {
+ uint64_t bits = DEC_BITS(d);
+ return ((bits & NAN_MASK) == NAN_MASK);
+}
+
+CONSTFUNC static Dec_t Dec$int_power(Dec_t x, int64_t exponent)
+{
+ if (exponent == 0) {
+ return 1.DD;
+ } else if (exponent == 1) {
+ return x;
+ } else if (exponent % 2 == 0) {
+ Dec_t y = Dec$int_power(x, exponent/2);
+ return y*y;
+ } else {
+ return x * Dec$int_power(x, exponent - 1);
+ }
+}
+
+public Dec_t Dec$power(Dec_t x, Dec_t y) {
+ if (x == 0.DD && y < 0.DD)
+ fail("The following math operation is not supported: ", x, "^", y);
+
+ /* For any y, including a NaN. */
+ if (x == 1.DD)
+ return x;
+
+ if (Dec$isnan(x) || Dec$isnan(y))
+ return NONE_DEC;
+
+ if (y == 0.DD)
+ return 1.DD;
+
+ if (x < 0.DD && y < 0.DD) {
+ return NONE_DEC;
+ } else if (x == 0.DD) {
+ return y < 0.DD ? NONE_DEC : 0.DD;
+ } else if (!Dec$isfinite(x)) {
+ return y < 0.DD ? 0.DD : x;
+ }
+
+ int64_t int_y = (int64_t)y;
+ if ((Dec_t)int_y == y)
+ return Dec$int_power(x, int_y);
+
+ // TODO: improve the accuracy of this approach:
+ return (Dec_t)powl((long double)x, (long double)y);
+}
+
+public Dec_t Dec$round(Dec_t d, Int_t digits) {
+ int64_t digits64 = Int64$from_int(digits, false);
+ if (digits.small != 1L) {
+ for (int64_t i = digits64; i > 0; i--)
+ d *= 10.0DD;
+ for (int64_t i = digits64; i < 0; i++)
+ d *= 0.1DD;
+ }
+ _Decimal64 truncated = (_Decimal64)(int64_t)d;
+ _Decimal64 difference = (d - truncated);
+ _Decimal64 rounded;
+ if (difference < 0.0DD) {
+ rounded = (difference < -0.5DD) ? truncated - 1.0DD : truncated;
+ } else {
+ rounded = (difference >= 0.5DD) ? truncated + 1.0DD : truncated;
+ }
+ for (int64_t i = digits64; i > 0; i--)
+ rounded *= 0.1DD;
+ for (int64_t i = digits64; i < 0; i++)
+ rounded *= 10.0DD;
+ return rounded;
+}
+
+public OptionalDec_t Dec$parse(Text_t text) {
+ return Dec$from_str(Text$as_c_string(text));
+}
+
+static void Dec$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *info)
+{
+ (void)info;
+ Dec_t d = *(Dec_t*)obj;
+ char *str = String(d);
+ int64_t len = (int64_t)strlen(str);
+ Int64$serialize(&len, out, pointers, &Int64$info);
+ if (fwrite(str, sizeof(char), (size_t)len, out) != (size_t)len)
+ fail("Could not serialize Dec value!");
+}
+
+static void Dec$deserialize(FILE *in, void *obj, List_t *pointers, const TypeInfo_t *info)
+{
+ (void)info;
+ int64_t len = 0;
+ Int64$deserialize(in, &len, pointers, &Int64$info);
+ assert(len >= 0);
+ char buf[len];
+ if (fread(buf, sizeof(char), (size_t)len, in) != (size_t)len)
+ fail("Could not deserialize Dec value!");
+ Dec_t d = Dec$from_str(buf);
+ memcpy(obj, &d, sizeof(d));
+}
+
+public const TypeInfo_t Dec$info = {
+ .size=sizeof(Dec_t),
+ .align=__alignof__(Dec_t),
+ .metamethods={
+ .compare=Dec$compare,
+ .equal=Dec$equal,
+ .as_text=Dec$as_text,
+ .is_none=Dec$is_none,
+ .serialize=Dec$serialize,
+ .deserialize=Dec$deserialize,
+ },
+};
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/src/stdlib/decimals.h b/src/stdlib/decimals.h
new file mode 100644
index 00000000..d6eeb332
--- /dev/null
+++ b/src/stdlib/decimals.h
@@ -0,0 +1,58 @@
+#pragma once
+
+// Integer type infos and methods
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "print.h"
+#include "datatypes.h"
+#include "stdlib.h"
+#include "types.h"
+#include "util.h"
+
+#define NONE_DEC (((union { int64_t i; _Decimal64 d; }){.i=-1}).d)
+
+int Dec$print(FILE *f, Dec_t d);
+Text_t Dec$value_as_text(Dec_t d);
+Text_t Dec$as_text(const void *d, bool colorize, const TypeInfo_t *info);
+CONSTFUNC int32_t Dec$compare_value(const Dec_t x, const Dec_t y);
+CONSTFUNC int32_t Dec$compare(const void *x, const void *y, const TypeInfo_t *info);
+CONSTFUNC bool Dec$equal_value(const Dec_t x, const Dec_t y);
+CONSTFUNC bool Dec$equal(const void *x, const void *y, const TypeInfo_t *info);
+CONSTFUNC Dec_t Dec$round(Dec_t d, Int_t digits);
+CONSTFUNC Dec_t Dec$power(Dec_t base, Dec_t exponent);
+CONSTFUNC Dec_t Dec$plus(Dec_t x, Dec_t y);
+CONSTFUNC Dec_t Dec$negative(Dec_t x);
+CONSTFUNC Dec_t Dec$minus(Dec_t x, Dec_t y);
+CONSTFUNC Dec_t Dec$times(Dec_t x, Dec_t y);
+CONSTFUNC Dec_t Dec$divided_by(Dec_t x, Dec_t y);
+CONSTFUNC Dec_t Dec$modulo(Dec_t x, Dec_t modulus);
+CONSTFUNC Dec_t Dec$modulo1(Dec_t x, Dec_t modulus);
+PUREFUNC Dec_t Dec$from_str(const char *str);
+OptionalDec_t Dec$parse(Text_t text);
+
+CONSTFUNC Dec_t Dec$from_int64(int64_t i);
+Dec_t Dec$from_int(Int_t i);
+CONSTFUNC Dec_t Dec$from_num(double n);
+#define Dec$from_num32(n) Dec$from_num((double)n)
+#define Dec$from_int32(i) Dec$from_int64((int64_t)i)
+#define Dec$from_int16(i) Dec$from_int64((int64_t)i)
+#define Dec$from_int8(i) Dec$from_int64((int64_t)i)
+#define Dec$from_byte(i) Dec$from_int64((int64_t)i)
+#define Dec$from_bool(i) Dec$from_int64((int64_t)i)
+
+Int_t Dec$as_int(Dec_t d, bool truncate);
+int64_t Dec$as_int64(Dec_t d, bool truncate);
+int32_t Dec$as_int32(Dec_t d, bool truncate);
+int16_t Dec$as_int16(Dec_t d, bool truncate);
+int8_t Dec$as_int8(Dec_t d, bool truncate);
+Byte_t Dec$as_byte(Dec_t d, bool truncate);
+CONSTFUNC bool Dec$as_bool(Dec_t d);
+double Dec$as_num(Dec_t d);
+#define Dec$as_num32(d) ((float)Dec$as_num(d))
+
+extern const TypeInfo_t Dec$info;
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/src/stdlib/print.c b/src/stdlib/print.c
index 3270c765..a2ef7d4e 100644
--- a/src/stdlib/print.c
+++ b/src/stdlib/print.c
@@ -22,7 +22,7 @@ public int _print_int(FILE *f, int64_t n)
if (negative)
*(p--) = '-';
- return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
+ return (int)fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
}
public int _print_uint(FILE *f, uint64_t n)
@@ -35,7 +35,7 @@ public int _print_uint(FILE *f, uint64_t n)
n /= 10;
} while (n > 0);
- return fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
+ return (int)fwrite(p + 1, sizeof(char), (size_t)(&buf[19] - p), f);
}
public int _print_hex(FILE *f, hex_format_t hex)
@@ -178,6 +178,95 @@ public int _print_char(FILE *f, char c)
#undef ESC
}
+public int _print_decimal64(FILE *f , _Decimal64 x)
+{
+ union {
+ _Decimal64 decimal;
+ struct {
+ uint64_t mantissa:52;
+ uint64_t exponent:11;
+ bool negative:1;
+ };
+ uint64_t bits;
+ } info = {.decimal = x};
+
+ if ((info.bits >> 58 & 0x1F) == 0x1E)
+ return fputs(info.negative ? "-INF" : "INF", f);
+ else if ((info.bits >> 58 & 0x1F) == 0x1F)
+ return fputs("NAN", f);
+
+ // determine exponent e, and mantissa m
+ // where e and m are depend on the bits in m2
+ uint64_t e;
+ uint64_t m;
+ uint64_t m2 = info.bits >> 61 & 0x3;
+ if (m2 == 0x3) {
+ e = info.bits >> 51 & 0x3FF;
+ m = 0x20000000000000 | (info.bits & 0x7FFFFFFFFFFFF);
+ } else {
+ e = info.bits >> 53 & 0x3FF;
+ m = info.bits & 0x1FFFFFFFFFFFFF;
+ }
+
+ if (m == 0) return fputs("0", f);
+
+ char buf[64] = {[63]=0};
+ char *p = &buf[62];
+ int64_t exponent = (int64_t)e - 398;
+
+ uint64_t n = m;
+ do {
+ *(p--) = '0' + (n % 10);
+ n /= 10;
+ } while (n > 0);
+
+ const char *digit_str = p + 1;
+
+ int printed = 0;
+ int64_t digits = (int64_t)(&buf[63] - digit_str);
+
+ if (info.negative)
+ printed += fputc('-', f);
+
+ while (exponent < 0 && digits > 1 && digit_str[digits-1] == '0') {
+ digits -= 1;
+ exponent += 1;
+ }
+
+ if (exponent >= 0) {
+ printed += (int)fwrite(digit_str, sizeof(char), (size_t)digits, f);
+ for (int64_t i = 0; i < exponent; i++)
+ printed += (int)fwrite("0", sizeof(char), 1, f);
+ } else {
+ int64_t digits_above_zero = MAX(digits + exponent, 0);
+ if (digits_above_zero > 0) {
+ printed += (int)fwrite(digit_str, sizeof(char), (size_t)digits_above_zero, f);
+ for (int64_t i = -digits; i > exponent; i--)
+ printed += fputc('0', f);
+ } else {
+ printed += fputc('0', f);
+ }
+
+ int64_t digits_below_zero = digits - digits_above_zero;
+ if (digits_below_zero > 0) {
+ const char *rest = digit_str + digits_above_zero;
+ if (*rest) {
+ printed += fputc('.', f);
+ for (int64_t i = digits_below_zero; i < -exponent; i++)
+ printed += fputc('0', f);
+ printed += fputs(digit_str+digits_above_zero, f);
+ }
+ }
+ }
+
+ return printed;
+}
+
+public int _print_decimal32(FILE *f, _Decimal32 x)
+{
+ return _print_decimal64(f, (_Decimal64)x);
+}
+
public int _print_quoted(FILE *f, quoted_t quoted)
{
#define ESC(e) "\\" e
diff --git a/src/stdlib/print.h b/src/stdlib/print.h
index 5ef5b6ed..5a4a56f2 100644
--- a/src/stdlib/print.h
+++ b/src/stdlib/print.h
@@ -83,6 +83,8 @@ int _print_double(FILE *f, double x);
int _print_hex(FILE *f, hex_format_t hex);
int _print_hex_double(FILE *f, hex_double_t hex);
int _print_oct(FILE *f, oct_format_t oct);
+int _print_decimal32(FILE *f, _Decimal32 d);
+int _print_decimal64(FILE *f, _Decimal64 d);
PRINT_FN _print_float(FILE *f, float x) { return _print_double(f, (double)x); }
PRINT_FN _print_pointer(FILE *f, void *p) { return _print_hex(f, hex((uint64_t)p)); }
PRINT_FN _print_bool(FILE *f, bool b) { return fputs(b ? "yes" : "no", f); }
@@ -116,6 +118,8 @@ extern int Int$print(FILE *f, Int_t i);
uint8_t: _print_uint, \
float: _print_float, \
double: _print_double, \
+ _Decimal32: _print_decimal32, \
+ _Decimal64: _print_decimal64, \
hex_format_t: _print_hex, \
hex_double_t: _print_hex_double, \
oct_format_t: _print_oct, \
diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c
index fa41cda6..82470c41 100644
--- a/src/stdlib/stdlib.c
+++ b/src/stdlib/stdlib.c
@@ -57,6 +57,19 @@ static _Noreturn void signal_handler(int sig, siginfo_t *info, void *userdata)
_exit(1);
}
+static _Noreturn void fpe_handler(int sig, siginfo_t *info, void *userdata)
+{
+ (void)info, (void)userdata;
+ assert(sig == SIGFPE);
+ fflush(stdout);
+ if (USE_COLOR) fputs("\x1b[31;7m ===== MATH EXCEPTION ===== \n\n\x1b[m", stderr);
+ else fputs("===== MATH EXCEPTION =====\n\n", stderr);
+ print_stacktrace(stderr, 3);
+ fflush(stderr);
+ raise(SIGABRT);
+ _exit(1);
+}
+
public void tomo_init(void)
{
GC_INIT();
@@ -67,11 +80,17 @@ public void tomo_init(void)
setlocale(LC_ALL, "");
assert(getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0) == sizeof(TOMO_HASH_KEY));
- struct sigaction sigact;
- sigact.sa_sigaction = signal_handler;
- sigemptyset(&sigact.sa_mask);
- sigact.sa_flags = 0;
- sigaction(SIGILL, &sigact, (struct sigaction *)NULL);
+ struct sigaction ill_sigaction;
+ ill_sigaction.sa_sigaction = signal_handler;
+ sigemptyset(&ill_sigaction.sa_mask);
+ ill_sigaction.sa_flags = 0;
+ sigaction(SIGILL, &ill_sigaction, (struct sigaction *)NULL);
+
+ struct sigaction fpe_sigaction;
+ fpe_sigaction.sa_sigaction = fpe_handler;
+ sigemptyset(&fpe_sigaction.sa_mask);
+ fpe_sigaction.sa_flags = 0;
+ sigaction(SIGFPE, &fpe_sigaction, (struct sigaction *)NULL);
}
static bool parse_single_arg(const TypeInfo_t *info, char *arg, void *dest)
diff --git a/src/stdlib/tomo.h b/src/stdlib/tomo.h
index 63abd2d6..62139ea4 100644
--- a/src/stdlib/tomo.h
+++ b/src/stdlib/tomo.h
@@ -11,6 +11,7 @@
#include "bytes.h"
#include "c_strings.h"
#include "datatypes.h"
+#include "decimals.h"
#include "enums.h"
#include "functiontype.h"
#include "integers.h"
diff --git a/src/typecheck.c b/src/typecheck.c
index 6fdfb1d8..6d23efce 100644
--- a/src/typecheck.c
+++ b/src/typecheck.c
@@ -638,6 +638,9 @@ type_t *get_type(env_t *env, ast_t *ast)
case Num: {
return Type(NumType, .bits=TYPE_NBITS64);
}
+ case Dec: {
+ return Type(DecType);
+ }
case HeapAllocate: {
type_t *pointed = get_type(env, Match(ast, HeapAllocate)->value);
if (has_stack_memory(pointed))
@@ -880,8 +883,8 @@ type_t *get_type(env_t *env, ast_t *ast)
binding_t *constructor = get_constructor(env, t, call->args);
if (constructor)
return t;
- else if (t->tag == StructType || t->tag == IntType || t->tag == BigIntType || t->tag == NumType
- || t->tag == ByteType || t->tag == TextType || t->tag == CStringType)
+ else if (t->tag == StructType || t->tag == IntType || t->tag == BigIntType || t->tag == DecType || t->tag == NumType
+ || t->tag == DecType || t->tag == ByteType || t->tag == TextType || t->tag == CStringType)
return t; // Constructor
code_err(call->fn, "This is not a type that has a constructor");
}
diff --git a/src/types.c b/src/types.c
index 1a2405d4..b323dbcf 100644
--- a/src/types.c
+++ b/src/types.c
@@ -32,6 +32,7 @@ CORD type_to_cord(type_t *t) {
case CStringType: return "CString";
case TextType: return Match(t, TextType)->lang ? Match(t, TextType)->lang : "Text";
case BigIntType: return "Int";
+ case DecType: return "Dec";
case IntType: return String("Int", Match(t, IntType)->bits);
case NumType: return Match(t, NumType)->bits == TYPE_NBITS32 ? "Num32" : "Num";
case ListType: {
@@ -181,6 +182,7 @@ static PUREFUNC INLINE double type_min_magnitude(type_t *t)
case BoolType: return (double)false;
case ByteType: return 0;
case BigIntType: return -1./0.;
+ case DecType: return -1./0.;
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return (double)INT8_MIN;
@@ -201,6 +203,7 @@ static PUREFUNC INLINE double type_max_magnitude(type_t *t)
case BoolType: return (double)true;
case ByteType: return (double)UINT8_MAX;
case BigIntType: return 1./0.;
+ case DecType: return 1./0.;
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS8: return (double)INT8_MAX;
@@ -247,6 +250,7 @@ PUREFUNC bool has_heap_memory(type_t *t)
case PointerType: return true;
case OptionalType: return has_heap_memory(Match(t, OptionalType)->type);
case BigIntType: return true;
+ case DecType: return true;
case StructType: {
for (arg_t *field = Match(t, StructType)->fields; field; field = field->next) {
if (has_heap_memory(field->type))
@@ -306,10 +310,10 @@ PUREFUNC bool can_promote(type_t *actual, type_t *needed)
if (actual->tag == NumType && needed->tag == IntType)
return false;
- if (actual->tag == IntType && (needed->tag == NumType || needed->tag == BigIntType))
+ if (actual->tag == IntType && (needed->tag == NumType || needed->tag == BigIntType || needed->tag == DecType))
return true;
- if (actual->tag == BigIntType && needed->tag == NumType)
+ if (actual->tag == BigIntType && (needed->tag == DecType || needed->tag == NumType))
return true;
if (actual->tag == IntType && needed->tag == IntType) {
@@ -426,7 +430,7 @@ PUREFUNC bool is_int_type(type_t *t)
PUREFUNC bool is_numeric_type(type_t *t)
{
- return t->tag == IntType || t->tag == BigIntType || t->tag == NumType || t->tag == ByteType;
+ return t->tag == IntType || t->tag == BigIntType || t->tag == DecType || t->tag == NumType || t->tag == ByteType;
}
PUREFUNC bool is_packed_data(type_t *t)
@@ -498,6 +502,7 @@ PUREFUNC size_t type_size(type_t *t)
case ByteType: return sizeof(uint8_t);
case CStringType: return sizeof(char*);
case BigIntType: return sizeof(Int_t);
+ case DecType: return sizeof(Dec_t);
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS64: return sizeof(int64_t);
@@ -589,6 +594,7 @@ PUREFUNC size_t type_align(type_t *t)
case ByteType: return __alignof__(uint8_t);
case CStringType: return __alignof__(char*);
case BigIntType: return __alignof__(Int_t);
+ case DecType: return __alignof__(Dec_t);
case IntType: {
switch (Match(t, IntType)->bits) {
case TYPE_IBITS64: return __alignof__(int64_t);
diff --git a/src/types.h b/src/types.h
index 3b789560..c29c1bb7 100644
--- a/src/types.h
+++ b/src/types.h
@@ -44,6 +44,7 @@ struct type_s {
ByteType,
BigIntType,
IntType,
+ DecType,
NumType,
CStringType,
TextType,
@@ -67,6 +68,7 @@ struct type_s {
type_t *ret;
} ReturnType;
struct {} BigIntType;
+ struct {} DecType;
struct {
enum { TYPE_IBITS8=8, TYPE_IBITS16=16, TYPE_IBITS32=32, TYPE_IBITS64=64 } bits;
} IntType;
@@ -129,6 +131,7 @@ struct type_s {
#define Type(typetag, ...) new(type_t, .tag=typetag, .__data.typetag={__VA_ARGS__})
#define INT_TYPE Type(BigIntType)
+#define DEC_TYPE Type(DecType)
#define NUM_TYPE Type(NumType, .bits=TYPE_NBITS64)
#define NewFunctionType(ret, ...) _make_function_type(ret, sizeof((arg_t[]){__VA_ARGS__})/sizeof(arg_t), (arg_t[]){__VA_ARGS__})