🦋 "BASIC" to Swift

For my current project which will be a developement system that will write a Swift/Xcode project complete with GUI… I was going to need code to translate a dialetic of BASIC into Swift for Xcode to compile.

Well last night I realized that I had spent days copying code from my Xojo proof of concept and then manually changing case and syntax so it would compile in Swift.

Doh! Since the end project needed to be able to translate, and I needed said translation to help write the program that would translate (catch-22?)… I wrote this today

It is not 100% accurate (yet)… but I figure it will save me hundreds of hours of drudge work over the next few weeks, and I’ll tweek this as I use it.

If anyone is interested…let me know and I’ll compile this and post it…

And if you play with it, and find situations that are translated incorrectly, let me know

FYI… this too is written in Swift

1 Like

Compile a version and add it to the post - that lets anyone give it a spin and then provide feedback

Looks interesting already

And since you’re moving from a language like Basic that limits what you need to support in Swift a lot I’d think ?

This is a work in progress… I’ll be fine tuning it as I find situations during actual use.
Right now it UPPERCASES things it isn’t sure about… I’ll be adding a dynamic dictionary that will be prepopulated with some keywords, and add variiable and function names in order to preserve case (case will be what ever the DIM or SUB/FUNCTION defines it as). Xojo doesn’t care… but Swift is very strict in that regard.

To use… either type (or Paste) code in the left pane
Hit [Translate]
copy code from Right Pane.

There is NO file system behind this as the intent if to copy/paste code from my Xojo project, translate it, then copy/paste into my Swift project

It is a brute force translator … as in it examines a scope of one line… with no regard to items that came before or after… meaning the BASIC code is assumed to be in good format.

I just happen to know this guy who wrote a lot of code for parsing all this sam crap … just recently …

Oh look theres the open project that does just that :stuck_out_tongue:

I know absolutely nothing about Swift, no semicolons?

You can use semicolons if you want, but you don’t need to.

Btw I found the following quite nice for getting into Swift:

https://docs.swift.org/swift-book/

Interesting link. Swift doesn’t look as terryfing as Objective-C

Semi-colons are optional… except if you want to put multi statements on a single line

x+=1; y+=2; print(x+y)

No… its not… it is MUCH more friendly than ObjC (at least in my opinion)

And this tool could be used as a way to learn some of the syntax… Not comprehesive by a long shot… but covers the “basics” [pun intended]

i did look at Swift at some point previously – looking at this i am reminded why i liked the language so much.
I do think Swift has done a much better job at being approachable for beginners that XOJO and the recent changes have done…

Sometimes I feel like I’m the only person who really prefers Obj-C, although I probably haven’t really given Swift a fair chance.

Youre not :slight_smile:
But I wrote C for a decade or more so Obj-C wasnt a big leap

I never did “C” , and tried to teach myself ObjC years and years ago… And when Swift was first announced… I was leary… now I really like it :smiley:

I would REALLY appreciate if some of you could take that download. Run some valid BASIC scenarios thru it and see if it creates valid results.

Not expecting it to translate database, or custom class stuff… just standard syntax for now… IF/THEN, FOR/NEXT, WHILE/WEND and anything else I forgot :smiley:

With pleasure

A long time ago I was able to write a bit of C so I thought: “I’ll learn this IOS stuff”, purchased a book, opened it and got a headache at around page 3 :stuck_out_tongue:

1 Like

Wrote way too much C way back when - some if it about the time ANSI even started to standardize it

Ok here is an updated version.
This one uses a dictionary to make sure all variable names, and function names stay a consistent case (based on where first define)

It also should properly handle Arrays (single dimension only). Unlike Xojo, Swift uses square brackets to denote an array

So in BASIC you’d have this

Dim x as integer = 42
dim y(14) as string ="Fred"
Dim z() as double
dim q(-1) as Boolean
dim aa(3) as Integer
dim bb(5) as Double = 99
dim ss(32) as String 
//
x=sin(y(aa(bb((3*7)))))

in Swift you’d have this

var x : Int = 42
var y =[String](repeating : "Fred",count : 14)
var z =[Double]()
var q =[Bool]()
var aa =[Int](repeating : 0,count : 3)
var bb =[Double](repeating : 99,count : 5)
var ss =[String](repeating : "",count : 32)
//
x = sin(y[aa[bb[(3 * 7)]]])
1 Like

Probably nice application, but it needs 10;14 (I think) to run: it was rejected on El Capitan (both a bas icon and an alert window that confirm the bad icon [no run icon]).

Fantastic idea.

Yes it is compiled against the 10.14 framework…

Let me try to compile it as low as I can, and I’ll let you know