# 🦋 "BASIC" to Swift

**URL:** <https://ifnotnil.com/t/basic-to-swift/613>\
**Category:** Swift\
**Tags:** swift, xojo\
**Created:** [11 June 2020 22:44 UTC](https://ifnotnil.com/t/basic-to-swift/613 "2020-06-11T22:44:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [11 June 2020 22:44 UTC](https://ifnotnil.com/t/basic-to-swift/613/1 "2020-06-11T22:44:51Z")

</div>

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

 ![x](https://ifnotnil.com/uploads/default/original/1X/77772ad381bfc7f9d02e4a2b5f83b497e345947a.jpeg)

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

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [11 June 2020 22:51 UTC](https://ifnotnil.com/t/basic-to-swift/613/2 "2020-06-11T22:51:08Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [11 June 2020 23:14 UTC](https://ifnotnil.com/t/basic-to-swift/613/3 "2020-06-11T23:14:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [11 June 2020 23:26 UTC](https://ifnotnil.com/t/basic-to-swift/613/4 "2020-06-11T23:26:36Z")

</div>

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 😛

---

<div class="post-metadata">

**Author:** ![HMARROQUINC](https://ifnotnil.com/user_avatar/ifnotnil.com/hmarroquinc/32/256_2.png) [@HMARROQUINC](https://ifnotnil.com/u/HMARROQUINC)\
**Post date:** [12 June 2020 02:32 UTC](https://ifnotnil.com/t/basic-to-swift/613/5 "2020-06-12T02:32:01Z")

</div>

I know absolutely nothing about Swift, no semicolons?

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [12 June 2020 03:02 UTC](https://ifnotnil.com/t/basic-to-swift/613/6 "2020-06-12T03:02:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [12 June 2020 03:04 UTC](https://ifnotnil.com/t/basic-to-swift/613/7 "2020-06-12T03:04:37Z")

</div>

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

[https://docs.swift.org/swift-book/](https://docs.swift.org/swift-book/)

---

<div class="post-metadata">

**Author:** ![HMARROQUINC](https://ifnotnil.com/user_avatar/ifnotnil.com/hmarroquinc/32/256_2.png) [@HMARROQUINC](https://ifnotnil.com/u/HMARROQUINC)\
**Post date:** [12 June 2020 03:56 UTC](https://ifnotnil.com/t/basic-to-swift/613/8 "2020-06-12T03:56:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [12 June 2020 05:52 UTC](https://ifnotnil.com/t/basic-to-swift/613/9 "2020-06-12T05:52:52Z")

</div>

> [@HMARROQUINC](#):
>
> I know absolutely nothing about Swift, no semicolons?

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

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

```

> [@HMARROQUINC](#):
>
> Interesting link. Swift doesn’t look as terryfing as Objective-C

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]

---

<div class="post-metadata">

**Author:** ![stam](https://ifnotnil.com/user_avatar/ifnotnil.com/stam/32/453_2.png) [@stam](https://ifnotnil.com/u/stam)\
**Post date:** [12 June 2020 12:52 UTC](https://ifnotnil.com/t/basic-to-swift/613/10 "2020-06-12T12:52:42Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![BillG](https://ifnotnil.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@BillG](https://ifnotnil.com/u/BillG)\
**Post date:** [12 June 2020 14:45 UTC](https://ifnotnil.com/t/basic-to-swift/613/11 "2020-06-12T14:45:18Z")

</div>

> [@HMARROQUINC](#):
>
> Swift doesn’t look as terryfing as Objective-C

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.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 June 2020 15:15 UTC](https://ifnotnil.com/t/basic-to-swift/613/12 "2020-06-12T15:15:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [12 June 2020 15:22 UTC](https://ifnotnil.com/t/basic-to-swift/613/13 "2020-06-12T15:22:08Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [12 June 2020 15:27 UTC](https://ifnotnil.com/t/basic-to-swift/613/14 "2020-06-12T15:27:31Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![HMARROQUINC](https://ifnotnil.com/user_avatar/ifnotnil.com/hmarroquinc/32/256_2.png) [@HMARROQUINC](https://ifnotnil.com/u/HMARROQUINC)\
**Post date:** [12 June 2020 15:29 UTC](https://ifnotnil.com/t/basic-to-swift/613/15 "2020-06-12T15:29:24Z")

</div>

With pleasure

---

<div class="post-metadata">

**Author:** ![HMARROQUINC](https://ifnotnil.com/user_avatar/ifnotnil.com/hmarroquinc/32/256_2.png) [@HMARROQUINC](https://ifnotnil.com/u/HMARROQUINC)\
**Post date:** [12 June 2020 15:35 UTC](https://ifnotnil.com/t/basic-to-swift/613/16 "2020-06-12T15:35:04Z")

</div>

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 😛

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 June 2020 16:06 UTC](https://ifnotnil.com/t/basic-to-swift/613/17 "2020-06-12T16:06:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [12 June 2020 23:53 UTC](https://ifnotnil.com/t/basic-to-swift/613/18 "2020-06-12T23:53:25Z")

</div>

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

```auto
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

```swift
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)]]])

```

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [13 June 2020 18:04 UTC](https://ifnotnil.com/t/basic-to-swift/613/19 "2020-06-13T18:04:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [13 June 2020 18:06 UTC](https://ifnotnil.com/t/basic-to-swift/613/20 "2020-06-13T18:06:05Z")

</div>

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

[Next page](https://ifnotnil.com/t/basic-to-swift/613.md?page=2)
