blech-lang / blech

Blech is a language for developing reactive, real-time critical embedded software.
Apache License 2.0
64 stars 5 forks source link

compile time arithmetic expression not supported #5

Closed MohamedGhardallou closed 2 years ago

MohamedGhardallou commented 2 years ago

Hello compiling this program

module 

activity do_something()()
 const HOUR_IN_SEC : nat32 = 60 * 60
 await true
end

produces the following error :

error: Cannot resolve overloaded arithmetic operation on unsized literals '60' and '60'. --> prog.blc:5:30 [typing]

5 | const HOUR_IN_SEC : nat32 = 60 * 60 | ^^^^^^^ overloading not resolved

schorg commented 2 years ago

In Blech number literals are unsized. You need a "size" here, you can write const HOUR_IN_SEC = (60 : nat32) * 60 or even const HOUR_IN_SEC : nat32 = (60 : nat16) * 60 which means the expression is of type nat16 and can be assigned to a nat32. if you write const HOUR_IN_SEC : nat32 = (60 : nat8) * 60 you will get an error, because the right side does not fit into a nat8

schorg commented 2 years ago

It was a decision not to have number literals of different types. Your example is one of the exceptional cases where you need a type annotation. See also: https://www.blech-lang.org/docs/user-manual/expressions/#annotated-literals

schorg commented 2 years ago

One more thing: Compile-time expressions are evaluated by the compiler. You can see the result in the generated C code.

MohamedGhardallou commented 2 years ago

Thanks for the detailed response. Yes i see that it's replaced by a value in the generated code. Thanks