Page 2 of 2

Re: Default Variable Assignment

Posted: Mon Jun 22, 2020 4:45 pm
by bitfogav
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

Re: Default Variable Assignment

Posted: Tue Jun 23, 2020 9:12 am
by David Barker
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

Re: Default Variable Assignment

Posted: Sat Jun 27, 2020 2:38 pm
by bitfogav
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)

Re: Default Variable Assignment

Posted: Sat Jun 27, 2020 2:48 pm
by David Barker
Variable declarations as well as constants etc still need to be declared *before* the main statement block. This has not changed.

Re: Default Variable Assignment

Posted: Sat Jun 27, 2020 3:41 pm
by Jerry Messina
... 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

Re: Default Variable Assignment

Posted: Sat Jun 27, 2020 3:52 pm
by bitfogav
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:

Re: Default Variable Assignment

Posted: Sun Jun 28, 2020 8:57 am
by octal
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.

Re: Default Variable Assignment

Posted: Sun Jun 28, 2020 10:08 am
by David Barker
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.

Re: Default Variable Assignment

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

Re: Default Variable Assignment

Posted: Sun Jun 28, 2020 11:26 am
by Jerry Messina
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

Re: Default Variable Assignment

Posted: Sun Jun 28, 2020 12:38 pm
by octal
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).

Re: Default Variable Assignment

Posted: Fri Jul 03, 2020 2:42 pm
by Jerry Messina
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)

Re: Default Variable Assignment

Posted: Fri Jul 03, 2020 3:28 pm
by Jerry Messina
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)