cake icon indicating copy to clipboard operation
cake copied to clipboard

Feature request: define user numerical types with their arithmetic operators

Open cblc opened this issue 1 year ago • 9 comments

One of the features I miss the most in C is being able to define for example my own floating point data type and provide its arithmetic operators implementation. There's a proposal in WG14: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3051.pdf (and I believe the LCC win32 compiler implements that).

But, aside from that proposal, given that Cake translates to C99, I believe the implementation could be quite straightforward, because the generated C99 code would just need to call the function pointers of the operators implementation provided by the user. Then maybe the data storage could be provided by an opaque struct defined by the user and its sizeof() result.

cblc avatar Jun 16 '23 07:06 cblc

One alternative is to convert expressions using operators to function call syntax. For instance the expression

a + b * c

becomes

add(a , mult(b, c));

the names add,mult etc can be modified according with the types, for instance, using struct tag.

Having that the programmer just provide the function implementation. It also can be a function like macro. With _Generic the same name add etc can be preserved.

thradams avatar Jun 16 '23 11:06 thradams

Yes, I did that in my code. And my code became a nightmare to understand. So, what I'm saying is a feature in Cake which would do the conversion for you, so that you don't need to make your code unreadable.

cblc avatar Jun 16 '23 12:06 cblc

C23 included the header <stdckdint.h>

bool ckd_add(type1 *result, type2 a, type3 b);
bool ckd_sub(type1 *result, type2 a, type3 b);
bool ckd_mul(type1 *result, type2 a, type3 b);

Maybe cake could automatic convert from expression syntax inside _safe_check( ) to a function adding the checks.

int main() {
  int r = _safe_check(a + b *c);
  if (errno != 0) printf("oops");
}

/*generated function*/
static int _expression_1(int a, int b, int c) {
  int r;
  if (ckd_mul(&r, b, c) != 0) errno = 1;
  if (ckd_add(&r, a,  r) != 0) errno = 1;
  return r;
}

int main() {
  int r = _expression_1(a, b, c);
  if (errno != 0) printf("oops");
}

thradams avatar Jun 16 '23 12:06 thradams

I don't know that header yet. But I think that all the operators that can operate on integers and floating point types would be potentially necessary. Maybe even unary operators.

The purpose would be to let the client program implement its own integer and floating point types.

cblc avatar Jun 16 '23 12:06 cblc

obs: C23 also added _BitInt(N) and _Decimal32, _Decimal64, and _Decimal128. C99 has complex.

Converting any of these types to C89 would require a review in expressions and this work could be reused for operators overloading.

thradams avatar Jun 16 '23 13:06 thradams

Indeed, I feel that every C compiler should have offered this feature for years now, because it's really useful.

cblc avatar Jun 17 '23 14:06 cblc

Is there a place in the Cake source code where I could just modify the AST replacing every operator occurrence by a function invocation taking one or two arguments depending on whether the operator is unary or binary? (I left at a side the tertiary conditional operator because I don't think it makes sense to overload it: the only interest would be in inner expressions with other operators inside the tertiary operator, but not in the tertiary operator itself).

cblc avatar Aug 13 '23 10:08 cblc

At visit.c is where the conversion happens we can do expr1 operator expr2

We need

  • convert token operator to ','
  • convert the first token of expr1 to "op_name(" expr1
  • convert the last token of expr2 to ")" Then
a + b*c
a + mult(a, c)
add(a, mult(a, c))

thradams avatar Aug 09 '24 13:08 thradams

This is also useful to create a debug "checked runtime" against oveflows. Maybe attribute [[overflow]] expression;

thradams avatar Aug 09 '24 13:08 thradams