šŸ¦‹ "BASIC" to Swift

Your example attempted to alter the Loop Index value inside the loopā€¦ That was what Swift complained about. Loop variables are immutable, unlike in Xojo

FYIā€¦ here is the final translation of my above example

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

which is actually WRONG now I look at itā€¦ the DO UNTIL loop was translated incorrectly

:scream: :man_facepalming:

Okā€¦ what I said yesterday was a lie :slight_smile:
HERE IS ANOTHER UPDATE
This one fixs an issue with DO UNTIL
Add much more enhanced error and warning handling (colors the lines to see them easier, and message should be a bit more verbose)
It also optimizes some simple equations

x=x+10
becomes
x += 10

Please give me as much feedback as you can. But also be aware that just because a specific syntax works in Xojo doesnā€™t mean it will work hereā€¦ .And this translator right now is focused on control structure, not so much builtin math and string functionsā€¦ That being saidā€¦ I think it does a LOT of the work in simple translationsā€¦ And this app will be used to help write the larger Transpiler app by helping me port Xojo code I have already written to Swift.

Marcusā€¦ if you try you example again, it should now ā€œexplainā€ what the issue is

This seems wrong:

for i = 10 downTo 1
if i = 2 then
J = 5
end if
next

becomes

for i in(10...1).reversed() {
   if i == 2 {
      j = 5
   }
}

I think that should be

for i in(1...10).reversed() {

see https://stackoverflow.com/questions/24508592/how-to-iterate-for-loop-in-reverse-order-in-swift/42467712#42467712

Yupā€¦ you are in fact correctā€¦ I will add that to my ā€œto doā€ listā€¦ .Thanks!

EDIT : wow that was easy to fix :slight_smile:
turns out SWIFT has a cool way to swap two variables

(a,b)=(b,a)

now is that cool or what?

Iā€™ll post a new version tommorow, in case anyone else tenders more feedback

Eyyyy! Donā€™t look so surprised - even an old and blind cockerel finds the occasional seed ā€¦

LOLā€¦ no worriesā€¦ if I didnā€™t want anomlies found, I wouldnā€™t have asked :smiley:

After starting to use this app in some real world situations (1st attempt was pasting 300+ lines right out of Xojo, with a dozen function)ā€¦ I realized there would be situations were Swift would emit errors (for now) but I didnā€™ want it to abort the translationā€¦ Especially where there were unresolved variables that might purposely be defined in another section of the program. (My first pass emitted 186 errors of which 150 were unresolved values).

So Iā€™m adding to the program to allow the option to ignore either ALL errors, or just the unresolved variable ones.

Along the way I hope to be able to add more functions to fix even more translation anomolies.

One that already has been fixed is where Xojo allows a function with a paremeter to be called WITHOUT parendsā€¦

myFunction myArgument

but my dialetic requires

myFunction(myArgument)

Another unsupported feature of Swift :frowning: is ā€œassignsā€ ā€¦ .or at least I cantā€™t find it

function myFunction(assigns x as integer)

stay tuned.

Here we go once again :slight_smile:
This update add () where it thinks it is required (hopefully I covered all the bases)
And it ā€œremovesā€ any occurance of the ASSIGNS keyword. This may cause SWIFT to emit other errors, but I think those errors are more accurate in pointing out where a change needs to be made.

It now also handles two more specific transformations

swap a,b // BASIC
becomes
(a,b)=(b,a) // SWIFT

Redim a(-1) // BASIC
becomes
a.removeAll() // SWIFT

There is now the option to

  • IGNORE ALL ERRORS , this will emit the Swift Code as best it could be translated.
  • IGNORE ā€œUNRESOLVEDā€ - this will emit only errors not related to unresolved variables.

For those not awareā€¦ .One thing Swift does that is really different from most BASIC dialetics (Xojo included)ā€¦ Normally any variable named as part of a Method signature is mutable within the Method. This is not true for Swift. so you need to do something like :

Sub foo(bar as Integer)
dim tempBar as Integer=bar
tempBar=tempBar+1
End Sub

The same is true for variables that define a loop Index

upcoming solution to immutable variables in method signature.

this is valid in Xojo (and most other BASIC), but not in Swift

Sub foo(bar as Integer)
bar=bar+3
End Sub

next release will be fixing that where possible, the above would come out as

func foo (_ _bar : Int ) {
var bar = _bar // patch
bar += 3
}

if will rename the variable that is immutable by prepending a ā€œ_ā€ and then create a new local mutable variable and assign the incoming value. This update will be posted soon

Iā€™ve been head in the sand for a little while
What does it do with

Sub foo(byref bar as Integer)
bar=bar+3
End Sub
func foo (_   bar : inout Int) {
   bar += 3
}

what is DOESNā€™T do (at least not yet)
in BASIC you would call that

foo( myvariable)

but in Swift it needs to be called

foo( &myvariable) 

And my app doesnā€™t attempt to find the calls and insert the required ā€œ&ā€
but now that I think about it, I should be able to do that fix in a similar manner to how I just did this one

Swift does a very C like call

yeah I think the need to mark BOTH ends is rather extremeā€¦
The Swift error message for the signature error references the variable so I know which one to apply a patch forā€¦ but the other end where the & is required just calls out the datatypeā€¦ making it more difficult

well that sucks doesnt it ?

It is odd that, in Xojoā€™s case, the called method declares whether the param is by reference or not unlike most languages where the caller determines if they want to pass a reference or not

Most BASIC that have ā€œbyRefā€ work the same as Xojo does (VB6 and predecessors at least).

Iā€™ve fixed the translator to at least deal with adding the ā€œ&ā€ when there is only a single variable in the signature (cuz it canā€™t pick the wrong one that way :slight_smile: )

So

sub x(byref z as integer)
end sub
//
//
//
sub test
dim a as integer
x(a)
end sub

becomes

func x (_  z : inout Int) {
}
 //
 //
 //
func test() {
   var a : Int = 0
   x(&a)
}

:butterfly: www.rdsisemore.com/Morpho.app.zip :butterfly:

Latest updateā€¦ and NEW APP NAME (I removed all the old links)

This update includes

  • Support for ā€˜byrefā€™ as described in above posts
  • Support for Computed Variables (Set/Get)
  • Support for basic Enums. BASIC only supports Integer, this supports all types

BASIC

Public Property align as Integer
Get
  return zAlign
End Get

Set
  zAlign=value
end Set
End Property

Public Enum myENUM
	One
	two  = 17
	three
End Enum

Public Enum myENUM2 as string
	One   = "One"
	two   = "TWO"
	three = "Three"
End Enum

SWIFT

public var align : Int {
   get {
      return(zAlign)
   }
   
   set {
      zAlign = newValue
   }
}

public enum myENUM : Int {
   case One
   case two = 17
   case three
}

public enum myENUM2 : String {
   case One = "One"
   case two = "TWO"
   case three = "Three"
}

Todays update (use link above) adds

  • Structure
  • Hex constants (&H)
  • Oct constants (&O)
  • Binary constants (&B)
  • Color Constants (&C)

Color Constants are converted to a NSColor object
The Structure here is not the same as Xojo in that it is not used for ā€œdeclaresā€, and therefore is not byte aligned. If you specifiy a length on a String (x as string * 10), the length parameter is ignored

BASIC


Structure Untitled1
  fieldName As String * 20
  iType as integer
  dType as double 
  bType as Boolean
End Structure


sub test
let x as color   = &cFFfe32
let h as integer = &H123456
let o as integer = &o237
let b as integer = &b10010
print("\(x)\(h)\(o)\(b)")
end sub

Swift Translation

struct Untitled1 {
   var fieldName : String // * 20
   var iType : Int
   var dType : Double
   var bType : Bool
}


func test() {
   let x : NSColor = NSColor(red:1.0,green:0.996078431372549,blue:0.19607843137254902, alpha:1.0)
   let h : Int = 0x123456
   let o : Int = 0o237
   let b : Int = 0b10010
   print("\(x)\(h)\(o)\(b)")
}

I know that a few of you have downloaded thisā€¦ but am I wasting bandwidth posting these updates? I was hoping for more active feed backā€¦

Hey Dave;

Could you explain (again if you already have) why youā€™re making a transpiler? I get the new UI designer, and I whole-heartedly support that idea, but I donā€™t understand why the transpiler is needed.

The end result is planned to be a macOS replacement for Xojo. So it needs to be able to not only create the UI (for macOS/iOS and possilbly ATV), but also support all the code, logic and events behind it. The idea is that code would be written in a version of BASIC (obviously not Xojo) and then translated to Swift and compiled by Xcode.

The app in this topic is just the beginnings of the translation engine. I plan on using it to help me translate the Xojo code I had already written for the proof of concept version of this new appā€¦ and when I get to that part of the project this translator will be expanded and incorporated into the final product