View previous topic :: View next topic |
Author |
Message |
sriesch
Joined: 18 Feb 2005 Posts: 5
|
Posted: Fri Feb 18, 2005 7:38 pm Post subject: complexity of expressions allowed within "if" stat |
|
|
Does anybody know what types of expressions are/aren't allowed within an "if" statement? The manual suggests that any expression that evaluates to true or false would work, but something like this seems to fail. Not sure if I'm doing something wrong or if I just shouldn't be trying anything other than a basic comparison:
If Tempstr = "a"+ "b" Then
compiler gives me errors ..."3 parameters expected"... and ..."IF THEN expected"...
It works fine if I drop the +"b". I also tried adding () for kicks around the string concatenation, but that didn't change anything. |
|
Back to top |
|
|
ATIU
Joined: 15 Oct 2004 Posts: 534
|
Posted: Sat Feb 19, 2005 5:28 am Post subject: |
|
|
As far as I can remember, bascom does not like more than one equation in one line, maybe to keep the compiler simple and least expensive. You may want to dimension a temporary string and store the a + b before testing the argument. |
|
Back to top |
|
|
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Sat Feb 19, 2005 6:06 am Post subject: |
|
|
no "math" allowed on either side....
if a=b then ... is ok,
if a+b>c or if a<b+c won't work.
I made this mistake in my very first BASCOM program too until I remembered RTFM......
Similar problem with equations:
a=(a+b)/(c+d) doesn't work either. I assume it would complicate the parser quite a bit..... However, it would be really nice if it would work as it would make more complex math much easier to write and it would be less likely to loose the overview. Would bring it from basic BASIC to BASIC.
Just tried to write a routine to calculate S-parameters from a voltage and current input (both amplitude and phase) and it almost turned into a disaster until I finally switched to a C-compiler which allows real equations.
Juergen |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Sat Feb 19, 2005 3:08 pm Post subject: |
|
|
Writing an equation as
a=(a+b)/(c+d)
Instead of
a=a+b
e=c+d
a=a/e
Is in no way functionally better, it just looks better. It won't produce smaller code or use less memory. I show using variable e which you would need in the first example too but it's hidden from you by the compiler.
If you found another compiler that allows for nested calculations, try it both ways and see what the end code looks like.
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Sun Feb 20, 2005 12:24 am Post subject: |
|
|
Eddy,
I didn't complain about code length as I am aware that it will be the same!
However, if you do some more then basic/simple equations (in my case it would have been a 200 character line!) it's simply not very convenient if you need a half dozen of interim variables (to be declared too) and to cut the equation into about 65 lines of equations. You just have too much potential for mistakes compared to just "copy and paste" from the math software.
To me that's the only real drawback of BASCOM compared to some other compilers I know/use. It's fine for control functions where the equations are pretty short.
Juergen |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Sun Feb 20, 2005 1:51 am Post subject: |
|
|
I agree that it's simpler to program when you input the equations in the manner that you're used to (human readible form), but I can also understand the decision to not add that much more complexity into the parser/compiler to handle complex equations. I do a lot of programming but I couldn't begin to understand what it would take to build a complex equation parser.
Maybe someone could create a function like
a=solve("(a+b)/(c+d)")
But given the limitations of microcontrollers, there has to be a limit on nesting...
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
sigmacom
Joined: 08 Nov 2004 Posts: 16
|
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Fri Feb 25, 2005 12:20 am Post subject: |
|
|
Well, that's not quite a valid comparison since the AVR has a native command for multiplication, but I understand your point. The hard part is defining a "simple" equation. How many calculations on a single line would be allowed? How deep can you nest? Would the compiler be able to efficiently generate the code needed to handle the multiple calculations. Optimal code is much more important on an MCU.
This is really a wishlist item since it could be done, but it's difficult to say how much work it would be to implement it. I wouldn't mind being able to use complex equations as you describe, but first I'd want full support of new chips, additional LCD chip sets and such.
Of course, it's Mark's decision on what takes priority; personally I wouldn't put this up too high on the list. It sounds like you would, which is fine. We all have opinions and hopefully they're not all the same
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net
Last edited by eddy on Fri Feb 25, 2005 6:02 am; edited 1 time in total |
|
Back to top |
|
|
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Fri Feb 25, 2005 2:46 am Post subject: |
|
|
Eddy,
as I wrote above: for simple control functions it's fine as it is but if you do anthing which contains a certain amount of real match it's a pain.
In my opinion even a stand alone program converting a real equation into the spagetti required for a lengthier formula (copy and paste) would be a big help.
On the other side: even the first versions of BASIC were able to parse equations on CPU's which had a fraction of the compute power of todays PC's. They were actually less powerfull then even the Tiny's.......
Sure, new chips have to be supported and some may want other LCD's implemented (I just use the ones which work already = my lowest priority) but there's more one can do with the AVR's then just reacting on some input HiLow's.......
Juergen |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Fri Feb 25, 2005 6:01 am Post subject: |
|
|
It's hard to compare any PC with an MCU. My first XT PC had 640k of RAM, my Mega32 as 2k. That's a pretty big difference...
You could probably write a VB app that takes any length equation and breaks it down into a list of primary calculations that you could paste into Bascom. Leverage the power of your PC to make efficient compact code for the MCU - that sounds like a decent approach to this issue...
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Fri Feb 25, 2005 9:17 am Post subject: |
|
|
eddy wrote: | You could probably write a VB app that takes any length equation and breaks it down into a list of primary calculations that you could paste into Bascom. Leverage the power of your PC to make efficient compact code for the MCU - that sounds like a decent approach to this issue... |
Using your solution, the generated source code will be still readable and easy to understand?
If not, how do you debug the generated code?
Luciano |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Fri Feb 25, 2005 3:25 pm Post subject: |
|
|
You could take an equation like this:
a=(b+c)/d-e
and turn it into a string of Bascom commands like this
a=(b+c)
a=a/d
a=a-e
So it's still readible but would allow you to cut and paste the equation and have it converted to Bascom compatible code. Of course, the VB app would have to be written properly to ensure the bascom code was correct.
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Sat Feb 26, 2005 1:38 am Post subject: |
|
|
Eddy,
and what's the difference? BASCOM runs with the power of a PC and could contain the same routine executed before it actually goes into compiling the code........ The resulting code for the controller would/could be exactly the same....
Juergen |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Sat Feb 26, 2005 1:56 am Post subject: |
|
|
No difference, but it's something you could do for yourself if you feel it's important enough.
It's not a trivial task to create a complex equation parser. Give it a try and let us know how it goes.
Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
|