šŸ¦‹ "BASIC" to Swift

Okā€¦ yet another update :slight_smile:

  • fixed issues with DIM that had default values where not an array
  • added support for IF(condtion,true,false) structure
  • added support for NAMED color translation
  • added support for RealBasic.Rect/Point/Size to corresponding NSxxx

Note : not all BASIC method/functions will be directly translated to Swift, in the end many will be supported via custom Swift frameworks.

I am hoping some of you are beating this up. and that you find it helpful in learning more about Swift as an alternative system

Is that a Blue Morpho for the icon ? love the symbolism in that

EDIT : FWIW I pasted in a pretty lengthy (for me) method (80 lines) and it translated it all without crashing. Dont have a real way to test this code in Swift as its just one chunk out of a much larger whole.

Still nice to see progress on this :slight_smile:

just a pretty butterflyā€¦ thought was a good symbol for ā€œtransformationā€

Just wondered as the Blue Morpho is a very pretty utterfly
And isnt actually blue. Its a very cool optical effect that makes them appear that way.

Donā€™t actually know what kind it might be based onā€¦ Found it on Iconfinder.com and it caught my fancy

Just reminded me of these

Here is another updateā€¦ Its actually interesting what tidbits Iā€™m learning about Swift while writing thisā€¦

This version should now be able to translate the 3 variations of a BASIC DO/LOOP into the corresponding Swift version

essentially

// DO                            | while true {
// LOOP                          | }
//                               |
// DO UNTIL (condition)          | while !(condition} {
// LOOP                          | }
//                               |
// DO                            | repeat {
// LOOP UNTIL (condition)        |} while !(condition}

This version does NOT yet deal properly with EXIT or CONTINUE.

Also the keyword ā€œBREAKā€ is excluded from the BASIC syntax as it has a totally different meaning in Swift (and this app wonā€™t be directly supporting break points anyhow)

Example
BASIC

sub test
dim x as integer = 3
dim y as double = 2.5
Do
for i=1 to 10
	for j=1 to 10
		while j>1
			do
 				do until x=14
					exit while
				loop
			loop until y<3
		wend
	next j
next i
LOOP
Do Until x > 100 // test
  x = x * 2
Loop
Do
  x = x * 2
Loop Until x > 100
end sub

Swift Translation

func test() {
	var x : Int = 3
	var y : Double = 2.5
	while true {
		for i in(1...10) {
			for j in(1...10) {
				while j > 1 {
					repeat {
						while !(x == 14) {
							exit while
						}
					}while !(y < 3)
				}
			}
		}
	}
	while !(x > 100) {
// test
		x = x * 2
	}
	repeat {
		x = x * 2
	}while !(x > 100)
}

and Xcode properly compiles except for the (EXIT WHILE as mentioned above)

1 Like

Is there no rough equivalent in Swift to insert an unconditional debugger breakpoint ?

not sure if there would be a ā€œcodeā€ activated break (since the keyword ā€˜breakā€™ is what Xojo calls Exit). Plus since my app is a translatorā€¦ there is no ā€œrun timeā€ mode, at least not one equivalent to Xojo

Ah ok - wasnt sure if swift had anything comparable to break in the sense of ā€œstop execution hereā€

So one has to add breakpoints in the XCode IDE and there is no ā€œkeywordā€ for it?

as far as I know yes ā€¦ that is true
Iā€™m not entirely surprised since Obj-C doesnt have one either
BUT ā€¦ Xcode DOES stop at the breakpoints you set :stuck_out_tongue:

But I want to do everything in code!!!

:stuck_out_tongue:

text files !

I did some research, and there is no direct equivalent to the ā€œBREAKā€ command. Seems in Xcode (Swift or ObjC) you manually place breaks by clicking the text editor gutter, same as you can do in Xojo

Here is another updateā€¦
This one should properly handle ā€œEXIT xxxā€ for inner and outer loops (Swift requires labels on outer loop exits). It also detects situations where missing Next, End etc and marks the BASIC line with a :beetle: ā€¦

The next update will require you have Swift installed, as it will take any translated code and run it thru the Swift compiler to test for other errors. This will take a bit more work, as it will need to take a Swift error and relate it back to the BASIC line number.

1 Like

Wow, that was easier than I thought it would beā€¦ So here it isā€¦
Unless I find (or one of you finds) something way out of wack, this will probably be the last tweek (famous last words I know)

This version requires you have Swift installed (it checks when it starts up).
Once you give it some BASIC code, and it passes the minor structure checks, it then passes it (via Shell) to Swift. And if shows any errors returned (right now, Iā€™m letting warnings slide). NOTE : this part of the process writes the Swift code to your DESKTOP

And my thanks to Hector (HMARROQUINC), he sent some test code that pointed out a few errors that my test code neglected.

1 Like

for i as integer = 0 to 10
i = i+1
next

results in

:beetle: error- cannot assign to value

Same for

dim i as integer
for i = 0 to 10
i = i+1
next

Probably donā€™t have Swift installed yet on this Mac ā€¦ nope, Swift is on here as well ā€¦

thisi s because Swift is ā€œdifferentā€
First Loop variables are not "Dim"d or Varā€™dā€¦ Swift by default is the same as if Xojo said
For I As Integer=0 to 10
and
i=i+1 in this case is an attempt to assign a ā€œconstantā€ā€¦

Remember, this is for a BASIC dialetic, NOT ā€œXojoā€ per seā€™

but all that being saidā€¦ It does give me something to think about

test.swift:3:5: warning: immutable value 'i' was never used; consider replacing with '_' or removing it
for i in(0...10) {
    ^
    _

test.swift:4:4: error: cannot assign to value: 'i' is a 'let' constant
   i = i + 1
   ^

that is the exact Swift log

They are further up in your example so Iā€™m not sure what is different: