"c = a + b;" is a hard assignemnt of the promoted result of "a + b"
to a variable. "if (a + b > 418)" uses the same promoted result as
part of the expression, but does not store it anywhere. As such,
in these examples, c would be 255, and the value used to compare
against 418 would be the sum of a + b as it is, promoted to u16 if
they were u8 to begin with.
>> CAlive provides <|decision|> casks for this purpose. Each
>> possible "thrown" condition can be captured on each line
>> using those casks:
>>
>> a = 200;
>> b = 219;
>> if (a + b <|overflow|my_overflow_func()||> > 418)
>> printf("Yessiree buddy!");
>>
>> In this case, the logic would be like this:
>>
>> a = 200;
>> b = 219;
>> u16 t1 = a + b;
>> if (t1 > 255)
>> my_overflow_func();
>> if (t1 > 418)
>> printf("Yessiree buddy!");
>>
>
> To be brutally honest, I /much/ prefer a syntax like this second one to
> your casks ... With the expanded "C" version, it is clear.
Code it that way. CAlive allows you to code it the first way
and auto-injects that code for you. Plus, in the GUI editor
the first line shows up like this:
if (a + b <|> > 418)
And the <|> is in a yellow background with black text, which
contrasts to the foreground color of the editor. It's also
shown in a reduced font size so it looks closer to small
diamond there.
All casks have the ability to be expanded or collapsed by a
GUI editor setting, or individually.
>> Each cask is it's own self-contained thing, and it can be
>> injected anywhere in source code, and uses information
>> relative to where it is for its assignment, and you can
>> look at the code it generates to see if you put it in
>> the correct place. The general syntax is:
>>
>> <|type|code||>
>>
>> type = overflow, underflow, nan, etc.
>
> I honestly can't help feeling this is going to be ugly and unclear.
Don't use them. CAlive will still allow code as C does, and
you can code that way to your heart's delight.
>>> (Avoid compiler switch options.)
>>
>> In my opinion, and I've heard this from several people here
>> in the C groups ... avoiding compiler switch options is an
>> absolutely ridiculous idea. I'm amazed people make such
>> claims. It honestly makes me think you're trying to hobble
>> my design, along with some of the other advice you give.
>> And yes I am serious.
>
> No, not at all - you are entirely mistaken.
>
> Consider a "brightness" function for pixel colours. If you have
> saturation on u8 arithmetic, you could write it as :
>
> u8 brightness(u8 red, u8 green, u8 blue) {
> return red + green + blue;
> }
>
> If you had two's complement wrapping on overflow, it would be completely
> wrong. Agreed so far?
I would say the algorithm is wrong already. The colors r,g,b
account for 35%, 54% and 11% relative intensity of light, so
you cannot perform the logic you have above appropriately.
It would need to be:
u8 brightness(u8 r, u8 g, u8 b) {
return((u8)((f32)r*0.34f + (f32)g*0.54f + (f32)b*0.11f));
In the case of the proper algorithm, there's no issue. :-)
> One possible way to allow programmers to choose overflow behaviour,
> between saturation and wrapping, would be to have a command-line switch.
> This may sound like a fine idea, letting programmers choose the default
> behaviour they want.
I allow this feature by enclosing the block that is to operate
as C does in a _c {..} block, a _c {{..}} block, or a _c {{..
}(_c)} non-aligning-block.
It can be messy in source code, but in the GUI editor it makes
it more tolerable and visual.
u8 brightness(u8 red, u8 green, u8 blue) {
_c {
return red + green + blue;
}
}
In this case, no auto-promotion, and things will wrap as they
do in C. Other examples:
u8 brightness(u8 red, u8 green, u8 blue) {
_c {{
return red + green + blue;
}}
}
u8 brightness(u8 red, u8 green, u8 blue) {
_c {{ return red + green + blue; }(_c)}
}
The purpose of the {{..}(x)} blocks are to allow mis-aligned
blocks. Here's a contrived example showing C code and CAlive
code blocks overlapping:
func example
| params u8 red, u8 grn, u8 blu
| returns u8 color
{
_c {{
color = red + grn + blu;
printf("Overflow value = %d, ", red);
_ca {{ // CAlive block
adhoc get_value { return(red + grn + blu); }
}(_c)}
printf("saturated = %d\n", get_value);
}}
In the GUI editor, the code would appear like this:
func example
| params u8 red, u8 grn, u8 blu
| returns u8 color
{
_c +--------------------------------------+
|color = red + grn + blu; |
|printf("Overflow value = %d, ", red); |
_ca+--------------------------------------|------+
|adhoc get_value { return(red + grn + blu); } |
+--------------------------------------+ |
| printf("saturated = %d\n", get_value); |
+---------------------------------------------+
There are different colored boxes which encapsulate the code.
And within the boxes, certain features are allowed or dis-
allowed based on many factors.
> But then the same source code does something entirely different
> depending on the command-line switch. If you copy-and-paste the same
> function source code into a different file, it might result in different
> behaviour, depending on the switches used for that compilation. If you
> look at the code in isolation, you can't tell if it is right or wrong,
> or know that it relies on a particular choice of overflow behaviour.
>
> This is /bad/.
Each source file is compiled using whatever options it should.
CAlive allows a settings input file for options, so they don't
have to be specified individually on a command line, but a single
file with options is provided.
> On the other hand, if the function is written:
>
> sat u8 brightness(sat u8 red, sat u8 green, sat u8 blue) {
> return red + green + blue;
> }
>
> or :
>
> u8 brightness(u8 red, u8 green, u8 blue) {
> return sat_add(red, green, blue);
> }
>
> or :
>
> u8 brightness(u8 red, u8 green, u8 blue) {
> return red +.sat green +.sat blue;
> }
>
> Then the code is clear, and it stands by itself. (I have no opinions as
> to which syntax is best - you pick.)
I have ways to do this already.
> Do you see what I am getting at here?
>
>
> Things like optimisation or warning choices are fine for command-line
> switches - they affect the quality and efficiency of the generated code,
> but not its correctness.
Of course. And it's as it should be in my opinion.
>> There have to be ways to turn things on or off. They can
>> be added as something like a #pragma in source code, but to
>> limit the ability to add compiler command-line switches,
>> which allow external tools to use their various settings
>> however they do to then be translated and conveyed through
>> a standard protocol ... it's absolutely wrong advice. I'm
>> amazed any intelligent developer would suggest it, unless
>> they were on an agenda to give out wrong advice as per that
>> agenda.
>>
>
> You know fine that I do not have an agenda of giving out wrong advice.
I do not know this. You often give out bad advice. I think
you honestly believe you are giving out your best advice, but
I know you are also deceived by the enemy of God into believing
wrong things, so the things you think are right are wrong, and
the reasons why you believe what you do are not your own, but
you have bought into them because you acquiesce to sin's call
by the evil spirits' leading and prompting you.
You've let them in over decades, David, and now they're there
in your mind affecting all of your thoughts, beliefs, actions,
etc.
Until you are willing to turn toward Christ, acknowledge your
sin, acknowledge your helpless state over their power because
of the ravages of sin against your very nature in sin, then you
have no hope. You will go through life believing you are right
when you are wrong, believing you are doing everything correctly
when you are being deceived into incorrect things, etc.
Jesus takes all of that away, and His Holy Spirit does guide you
rightly from within. It's why I point you to Him.
> Regardless of any arguments or disagreements we might have about
> non-technical subjects, or on technical aspects of your language, or on
> its practicality, you /know/ that I give my opinions honestly and with
> the best technical knowledge and experience I can.
I know that you believe you do this. I can only teach you that
you are wrong. I can't make you believe it. You have to have
that inner spark driving you to move, and that only comes from
God, and that only comes to those who are being saved (John 6:44).
--
Rick C. Hodgin