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
Okā¦ what I said yesterday was a lie
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
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 is āassignsā ā¦ .or at least I cantāt find it
Here we go once again
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
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
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
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 )
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)
}
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"
}
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ā¦
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