-
Mar 5th, 2015, 04:14 PM
#1
Help Determining Assignment and Equality Symbols
I'm creating my own interpreter and I'm still in the process of really narrowing down the syntax. Right now I'm stuck with trying to figure out which symbol to use for my assignment operator and equality operator. Just in case you do not know the difference:
Assignment:
Equality:
My language will be strongly typed so unlike JavaScript I do not need to worry about the equal type operator because implicit conversions are not allowed. On the same hand I want non-programmers to be able to read the code easily and while a single equal sign is simple to understand a double equal sign may not be as easy to understand. And to be honest, the majority of bugs that I have when writing C# code is missing semicolons and confusing the assignment operator with the equality operator.
At first I was thinking about using the single equal sign for assignment and the keyword Is or Equals for equality, but then I decided against it because I'm trying to keep the syntax as language independent as possible and those two keywords are English.
I'm interested in what y'all would like to see.
-
Mar 5th, 2015, 04:19 PM
#2
Re: Help Determining Assignment and Equality Symbols
So are "if" and "Then" ... the other alternative is to use a different token... that's what most languages use. Pascal has ":=" for assignment and "=" for comparison. C uses "=" and "==" ... which leads to a lot of confusion and fun. Otherwise your parser has to be smart enough to figure out the context... and then depending on what your constructs allow, you can have an assignment and an equality check all at the same time.
x = ((a+b) = c) where x is boolean and the rest are integers... here it's adding a & b and comparing to c... x should then be set to true or false...
same line in a little more ambiguous form:
x = (c = (a + b))
-tg
-
Mar 5th, 2015, 04:38 PM
#3
Re: Help Determining Assignment and Equality Symbols
I didn't think of assigning Boolean values to a condition. I do this all the time in function like:
Code:
Private Function IsFive(ByVal input As Integer) As Boolean
Return input = 5
End Function
-
Mar 5th, 2015, 05:00 PM
#4
Re: Help Determining Assignment and Equality Symbols
I do stuff like that all the time too... write a function to check a condition ... that's why it's important to either understand the context, or use a different operand token.
Code:
Private Function PlusFiveIsTen(ByVal input As Integer) As Boolean
Return (input + 5) = 10
End Function
That's what trips people up when going from something like VB to C ... these produce some very different results:
Code:
c = (input = 5) ' c is set to true because the assignment (input = 5) was successful even if input was 6 - I think I have that right... it's either true or 5 (see example 3 below)
//vs.
c = (input == 5) 'c is set to true ONLY when input is 5... false otherwise.
Heck even I get tripped up by it form time to time. it's subtle but the distinction is significant... so in your world, what would you expect those two lines to produce?
then there's this little fin nugget:
Code:
c = input = 5 ' c is set to 5.. yeah, that's always fun to explain...
-tg
-
Mar 5th, 2015, 05:01 PM
#5
Re: Help Determining Assignment and Equality Symbols
I guess where I was going was to say use two different operands, removing any ambiguity.
-tg
-
Mar 5th, 2015, 05:49 PM
#6
Re: Help Determining Assignment and Equality Symbols
Ok, I think that I will go with the double equal sign for equality and a single equal sign for assignment then.
-
Mar 6th, 2015, 01:34 AM
#7
Re: Help Determining Assignment and Equality Symbols
I was going to suggest the double equal sign too. Thats just what I am used to with PHP. and TG was right... it does cause problems at some points when you set something in a if/then statement because then it is always true.
-
Mar 6th, 2015, 06:41 AM
#8
Re: Help Determining Assignment and Equality Symbols
A different token (and double equals is as good as any other, probably better given it's familiarity) get's a thumbs up from me. I like a complete lack of ambiguity. I get that forgetting to use a double equals on a comparison can lead to a bug but at least it's an unambiguous bug.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Mar 6th, 2015, 06:45 AM
#9
Re: Help Determining Assignment and Equality Symbols
I was going to suggest the double equal sign too
+1
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Mar 6th, 2015, 07:15 AM
#10
Re: Help Determining Assignment and Equality Symbols
it does cause problems at some points when you set something in a if/then statement
You can do a lot to alleviate that with compiler warnings and errors though. Thinking about it, I don't think I've even introduced a bug that way in C#. I've certainly typed it out a few times but VS always tapped me on the shoulder and told me I was being an idiot.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|