Default Variable Assignment

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

bitfogav
Registered User
Registered User
Posts: 155
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Mon Jun 22, 2020 4:45 pm

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!. :D

User avatar
David Barker
Swordfish Developer
Posts: 1183
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Tue Jun 23, 2020 9:12 am

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

bitfogav
Registered User
Registered User
Posts: 155
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Sat Jun 27, 2020 2:38 pm

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.

Code: Select all

Function multiply(a,b As Byte) As Word
   result = a * b
End Function

dim val as word = multiply(2,4)
But if the following is used then I get the compiler error:

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)

User avatar
David Barker
Swordfish Developer
Posts: 1183
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Sat Jun 27, 2020 2:48 pm

Variable declarations as well as constants etc still need to be declared *before* the main statement block. This has not changed.

Jerry Messina
Swordfish Developer
Posts: 1184
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Sat Jun 27, 2020 3:41 pm

... so in your case that would be

Code: Select all

Dim value As Word = multiply(2,4)

USART.SetBaudrate(br19200)
Swap(x,i)
You can have initializers setup local variables inside sub/functions too:

Code: Select all

sub mysub()
	dim x as byte = 1
	...
end sub
The declarations have to be before any executable code, just as before without the initializer

bitfogav
Registered User
Registered User
Posts: 155
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Default Variable Assignment

Post by bitfogav » Sat Jun 27, 2020 3:52 pm

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. :lol:

User avatar
octal
Registered User
Registered User
Posts: 583
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: Default Variable Assignment

Post by octal » Sun Jun 28, 2020 8:57 am

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.

User avatar
David Barker
Swordfish Developer
Posts: 1183
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Default Variable Assignment

Post by David Barker » Sun Jun 28, 2020 10:08 am

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.

Jerry Messina
Swordfish Developer
Posts: 1184
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Sun Jun 28, 2020 11:11 am

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?

Code: Select all

program myprog

inline sub main()
	// main program goes here
end sub

main
end program
What does requiring 'main' get you?

Jerry Messina
Swordfish Developer
Posts: 1184
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Sun Jun 28, 2020 11:26 am

I'm still looking at ways to make Swordfish more Firewing like in this regard
How about adding static variables?

Code: Select all

	Sub MySub()
	  Static index As Byte = 0
	  index += 1
	  Console.Write("Index = ", CStr(index),13,10)
	End Sub

User avatar
octal
Registered User
Registered User
Posts: 583
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Re: Default Variable Assignment

Post by octal » Sun Jun 28, 2020 12:38 pm

Jerry Messina wrote:
Sun Jun 28, 2020 11:11 am
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?

Code: Select all

program myprog

inline sub main()
	// main program goes here
end sub

main
end program
Want does requiring 'main' get you?
Jerry,
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).

Jerry Messina
Swordfish Developer
Posts: 1184
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Fri Jul 03, 2020 2:42 pm

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)

Jerry Messina
Swordfish Developer
Posts: 1184
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Default Variable Assignment

Post by Jerry Messina » Fri Jul 03, 2020 3:28 pm

also, you can't use 'bound()'

Code: Select all

dim bdata() as byte = (10,20,30)

// 'symbol not expected'
dim bsize as byte = bound(bdata)
'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)

Post Reply