Default Variable Assignment
Moderators: David Barker, Jerry Messina
Re: Default Variable Assignment
Yes I thought that was the case, after I sent that post.
Anyway It's no problem David, I'm just glad that we can test these things.
Thank you for fixing the issue, I look forward to the next update!.
Anyway It's no problem David, I'm just glad that we can test these things.
Thank you for fixing the issue, I look forward to the next update!.
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
Re: Default Variable Assignment
There is a new update available which should fix this problem for you. Just run "check for update" in the IDE "About" box. For more information, see:
https://www.sfcompiler.co.uk/wiki/pmwi ... ionHistory
https://www.sfcompiler.co.uk/wiki/pmwi ... ionHistory
Re: Default Variable Assignment
Something that as caught me out a few times over the last few days is the use of this type of variable assignment, now I'm not sure if it's something I expect the compiler to do or if it's just something totally out of the scope of the compiler but this will give me a compiler error "Symbol not expected".. Does the variable assignment have to be directly assigned after the subroutine?
For example the following, this works.
But if the following is used then I get the compiler error:
For example the following, this works.
Code: Select all
Function multiply(a,b As Byte) As Word
result = a * b
End Function
dim val as word = multiply(2,4)
Code: Select all
Include "usart.bas"
Include "convert.bas"
Dim x As Byte = 2
Dim i As Byte = 4
Sub Swap(byref pValueA, pValueB As byte)
Dim Tmp As byte
Tmp = pValueA
pValueA = pValueB
pValueB = Tmp
End Sub
Function multiply(a,b As Byte) As Word
result = a * b
End Function
USART.SetBaudrate(br19200)
Swap(x,i)
Dim value As Word = multiply(2,4)
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
Re: Default Variable Assignment
Variable declarations as well as constants etc still need to be declared *before* the main statement block. This has not changed.
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Default Variable Assignment
... so in your case that would be
You can have initializers setup local variables inside sub/functions too:
The declarations have to be before any executable code, just as before without the initializer
Code: Select all
Dim value As Word = multiply(2,4)
USART.SetBaudrate(br19200)
Swap(x,i)
Code: Select all
sub mysub()
dim x as byte = 1
...
end sub
Re: Default Variable Assignment
Thank you David for clearing that up for me, and thank you Jerry for the examples.. I guess that's what you get when you are switching between two similar syntax languages Swordfish and Visual Basic.. You expect things to work in the same aspects.
Re: Default Variable Assignment
This is one of the things I hate in Swordfish. I would have prefered to see a statement like Main, or ProgStart, .... to explicitely introduce the main program.
Firewing with its "main" sub is a true progress in that sens.
Firewing with its "main" sub is a true progress in that sens.
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
Re: Default Variable Assignment
When I originally designed Swordfish, the aim was to be as "familiar" to some other basics - where you didn't need an explicit procedure "main" entry point - like PROTON or PICBASIC. In hindsight, I agree with you. Having an explicit entry point would have made things a lot easier. Particularly with respect to placing declarations and statements. As you point out, Firewing is much more advanced in this respect (because it was designed with hindsight).
I'm still looking at ways to make Swordfish more Firewing like in this regard (declaration assignments are one part of this). Anything I do must of course be 100% backward compatible with old code.
I'm still looking at ways to make Swordfish more Firewing like in this regard (declaration assignments are one part of this). Anything I do must of course be 100% backward compatible with old code.
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Default Variable Assignment
I'm not sure I really see much of an advantage one way or the other, unless you were going to allow forward references.
Couldn't you always just use a template like this?
What does requiring 'main' get you?
Couldn't you always just use a template like this?
Code: Select all
program myprog
inline sub main()
// main program goes here
end sub
main
end program
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Default Variable Assignment
How about adding static variables?I'm still looking at ways to make Swordfish more Firewing like in this regard
Code: Select all
Sub MySub()
Static index As Byte = 0
index += 1
Console.Write("Index = ", CStr(index),13,10)
End Sub
Re: Default Variable Assignment
Jerry,Jerry Messina wrote: ↑Sun Jun 28, 2020 11:11 amI'm not sure I really see much of an advantage one way or the other, unless you were going to allow forward references.
Couldn't you always just use a template like this?Want does requiring 'main' get you?Code: Select all
program myprog inline sub main() // main program goes here end sub main end program
My own programs are always limited to include self contained modules which expose getters and setters, and my main program file always only contains "option" statements before the includes and only two calls:
first call is InitMCU() which calls a sub responsible for special MCU initializations (ports, clock tunning, ...)
second call is main() ... which is a sub that contains all usual main program code.
I know from experience that when I can "impose" this discipline to myself, you can't ensure other developpers will do it. Having the compiler inforce this rule makes programs clearer and changes the language parser/compiler from having to deal with mixed statements and declarations in the main unit to handling only declarations (everything becomes a decralaration line in ANSI C). No statement or executable code can reside outside a programming unit (sub or function). This makes the language much cleaner (and makes writing automatic code handling easier).
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Default Variable Assignment
It seems that one thing you can't do in 2.2.3.4 is initialize a variable using '@' or 'addressof()'
Code: Select all
const cdata() as byte = (1,2,3,4,5,6,7,8)
dim bdata() as byte = (10,20,30)
// both of these produce 'symbol not expected'
dim caddr as longword = @cdata
dim baddr as word = addressof(bdata)
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Default Variable Assignment
also, you can't use 'bound()'
'sizeof()' works however
EDIT: Thanks to David the fix for bound() and addressof() will be included in the next update (likely 2.2.3.5)
Code: Select all
dim bdata() as byte = (10,20,30)
// 'symbol not expected'
dim bsize as byte = bound(bdata)
EDIT: Thanks to David the fix for bound() and addressof() will be included in the next update (likely 2.2.3.5)