Skip to content

feat: add infix operators and warnings#232

Draft
stringhandler wants to merge 1 commit intoBlockstreamResearch:masterfrom
stringhandler:feat/in-fix
Draft

feat: add infix operators and warnings#232
stringhandler wants to merge 1 commit intoBlockstreamResearch:masterfrom
stringhandler:feat/in-fix

Conversation

@stringhandler
Copy link
Contributor

Looking mainly for a Concept ACK here. I've added two big changes here. The first is to add infix operators, the second is to add warnings. I'm happy to try separate into two PRs, with the warnings standalone. I feel like without this context, the warnings don't really make sense.

The problem with using infix operators instead of jets is that the jets return carry and borrow bools for over/underflow. To try address this, the actual injected simplicity code (for example let c:u8 = a+b;) is basically equivalent to:

{
  let (carry, c) : (bool, u8) = jet::add_u8(a, b);
  if carry {
   panic!();
  }
  c
}

So if it would overflow the program will fail.

To alert the developer of this possibly unintended side-effect, I've added the idea of warnings. The program still compiles, but alerts the developer to add checks.

NOTE: In a future PR, I'd like to add a way of allowing and denying individual warnings. For now you can opt to deny-warnings as a command line, which will error on any warning.

Additional warning were also added for division, which also injects code to panic on divide by 0.

A small addition is also to mark errors in red and warnings in yellow.

Warnings look like below (note the program is still ouputed:

warning: This operator panics if the result overflows. To handle overflow, use the jet directly and destructure the (bool, uN) result.
 --> .\examples\infix_operators.simf:16:20
   |
16 |     let sum: u8 = a + b;
   |                    ^^^^^

warning: This operator panics if the result overflows. To handle overflow, use the jet directly and destructure the (bool, uN) result.
 --> .\examples\infix_operators.simf:20:21
   |
20 |     let diff: u8 = a - b;
   |                     ^^^^^

warning: This operator panics if the divisor is zero. To handle division by zero, use a jet and check the divisor before using it.
 --> .\examples\infix_operators.simf:28:25
   |
28 |     let quotient: u8 = a / b;
   |                         ^^^^^

warning: This operator panics if the divisor is zero. To handle division by zero, use a jet and check the divisor before using it.
 --> .\examples\infix_operators.simf:32:26
   |
32 |     let remainder: u8 = a % b;
   |                          ^^^^^

warning: `.\examples\infix_operators.simf` generated 4 warnings
Program:
5pk2AoCEChNgDAhAoQYcaijS0EHzZ0QznIWef/b40z+a+nNzThyQhoT+7cjE0....................

Example

See examples/infix_operators.simf for a full demonstration of all operators.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this would be better, but you can put warnings into Scope struct, and collect them from scope.destruct(). I tried similar approach, but with errors in #217.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants