Translate BASIC to Swift

the first could be missing a closing "
what error messages? {I don’t have XCode 6 yet)

The first line, after I add the missing quote, says:

'(count $T0, repeatedValue: $T1)' not convertible to 'String'

The second line should read:

var xarray:String[]=["A","B","C"]

And that fixes the third line.

Thanks… 2nd line I can fix… although the book infers that the [] are not required as the value indicates it is an array…

Gotta love that error message…

Also, the functions use “Return” to return a value, but Swift is case-sensitive (!) so it must be “return”.

The last two functions in your sample code are declared twice.

It’s an Apple proprietary language (lock in intentions everywhere).
Can’t happen until (probably never) they decide to open it as a public standard.

Other case problem: For/In must be for/in.

It doesn’t like this line at all, but I can’t tell why:

xint=(xint && 4)

“Could not find an overload for ‘__conversion’ that accepts the supplied arguments”.

The translation of one for loop is just wrong. The original code is:

For index=5 To 30 Step 8

The translation is:

For index=5; index<=30; ++index {

That should be “for”, but let’s ignore that for a moment. The “step” is ignored. It should be index+=8, no?

it now translates like this

var myarray:String=(count: 3, repeatedValue:String())
for index in 1...5 {
}
for index=5; index<=30; index+=8 {
}

do not understand why xint=(xint && 4) is “wrong”

new version available later today

(Integer) = (Integer) (OperatorLogicalAND) (Integer) // Error ← Swift seems to need strict boolean expressions
(Integer) = (Integer) (OperatorBitwiseAND) (Integer) // OK! Results a number

xint=1
xint = (xint & 4) // xint=0 , bitwise and

xint=6
xint = (xint & 4) // xint=4

xbol=false
xint=6
xbol=(xint & 4 > 0) // true

xbol = xbol && (4==1) // false

ok… & | are bitwise and && || are logical
now to see how to “guess” which one using only the context of a single line of code :slight_smile:

Hmm, good luck with that.

you need at least a 1 character look ahead to see if you have & or && (or &= if that’s also legal in swift)
that’s assuming you’re tokenizing the lines to eventually get semantic meaning

The problem is not in going from Swift to BASIC, but the other way when the BASIC dialect overloads AND for both a boolean comparison and bitwise operation. You have to know the type of assignment (boolean or numeric) to know which to use. I don’t see how you’d do that from a single line.

I just tested, and &= and the other variations are legit in Swift.

I am making an assumption (so it won’t be correct 100% of the time)

but if either of the arguments before or after the token AND|OR are numeric, the app will choose the Bitwise operator, otherwise it will choose the logical one. Like I said not 100% but I think more than 50% :slight_smile:

So what might it do with:

b = x = 1 and 2 = y

[quote=98323:@Kem Tekinay]So what might it do with:

b = x = 1 and 2 = y [/quote]
Does Swift support all the standard same expression evaluation operations that C does ?
If so yer boned :slight_smile:

The fun of C style expressions
I forget the operator precedence rules for C BUT if this evaluates as
b = (((x = 1) and 2) = y)
then it should fail

if this evaluates as
b = (x = 1) and (2 = y)
x=1 is an assignment that has a residual value (non-zero) so its true AND X now holds 1
2 = y should fail since you cannot assign y to a literal

Just to be clear, I was writing BASIC code, not Swift code.

Fair enough
That simplifies things a bit

[quote=98323:@Kem Tekinay]So what might it do with:

b = x = 1 & 2 = y [/quote]
which would be “wrong” for the intent

like I said… seems it won’t be 100% (although my BASIC dialect will allow you to use &&||&| as well as AND/OR)

as Norman said… “yer boned” :slight_smile:

No, unfortunately not working, the zip File is a 4kb File

although translating from xojo to swift that line should be parseable

b = x = 1 and 2 = y
should translate as if written
b = (x = 1) and (2 = y)
or
b := (x==1) && (2==y)
and you might need the brackets (depending on whether operator precedence in the two languages is the same - not sure if it is)