swift

The Good Old Days and Tiny BASIC

This week, we learned that Dr. Dobb's Journal is shutting down after 38 years. Admittedly, I haven't paid much attention to Dr. Dobb's for the past few years, but back when I was a kid who wanted to be a programmer, I anxiously awaited each monthly issue so that I could read every single article multiple times. We didn't have the Internet to give us whatever training we needed whenever we wanted, so magazines like Dr. Dobb's were precious.

While reminiscing about Dr. Dobb's with other grieving Twitter users, somebody brought up the fact that the first issue was titled "Dr. Dobb's Journal of Tiny BASIC Calisthenics & Orthodontia: Running Light Without Overbyte" (which I think is the coolest magazine title I've ever heard). It started as a xerographed newsletter to tell people about Tiny BASIC, a simplified BASIC programming language interpreter that could run in 2 or 3 kilobytes of memory. That was an important feature back when personal computers had only 4 kilobytes of memory.

Having just completed an implementation of a Forth programming language interpreter in Apple's new Swift programming language, I got the idea of implementing a Tiny BASIC interpreter in Swift. It didn't make any sense. I didn't want to write any programs in Tiny BASIC. I didn't think anybody else would want to write any programs in Tiny BASIC. There are more important things I could be doing with my free time.

But, apparently, reimplementing 50-year-old programming languages in Swift is my thing now

I did it. I call it "Finch", and if you want it, you can find it here:

Useless as it may be, it was an interesting exercise. Most of the Tiny BASIC implementations you find are focused on the original goal: getting a full implementation to fit in a few kilobytes. That isn't an important goal anymore, but I liked the idea of implementing a very-simple programming language. I focused on these goals:

  • Use Swift's high-level abstraction features, rather than writing code that looks like C or assembly language.
  • Make it easy for new Swift programmers to understand, so it could be useful as a beginner's example or in a tutorial.
  • Make it easy for people to hack on to add new features.

I think I did alright. It's about a thousand lines of code (not counting blank lines and comments), which is smaller than a C-based Tiny BASIC implementation I found, and I don't think I did anything too complicated. I will have to wait and see if anyone else wants to hack on it.

BASIC was the first programming language I learned. I expressed an interest in programming after attending an IBM open house, and my father brought home a BASIC programming manual. I studied it intently, but there weren't any computers around, so it was a while before I could try out what I had learned. But eventually the Radio Shack at the local mall started selling the TRS-80, and I could walk into that store and write my very first program:

10 PRINT "TRS-80 SUCKS! ";
20 GOTO 10
RUN

With great power comes great responsibility.

SuwaneeForth: A Forth Implementation in Swift

Using a new high-level programming language to implement an old low-level language is a strange thing to do, but I've done just that. SuwaneeForth is an implementation of Forth interpreter, written in Swift. If you are interested in such a thing, you can find it here:

SuwaneeForth is a translation/port of the system described in "A sometimes minimal FORTH compiler and tutorial for Linux/i386 systems" (a.k.a. "JONESFORTH") by Richard W.M. Jones. I'd suggest that all programmers read the source to that, as it is a very readable tutorial for bootstrapping a programming language implementation.

I heartily agree with the first paragraph of jonesforth.S:

FORTH is one of those alien languages which most working programmers regard in the same way as Haskell, LISP, and so on. Something so strange that they'd rather any thoughts of it just go away so they can get on with writing this paying code. But that's wrong and if you care at all about programming then you should at least understand all these languages, even if you will never use them.

Markingbird: A Markdown Processor for Swift

Markingbird is a Markdown processor written in Swift for OS X and iOS. It is a translation/port of the MarkdownSharp processor used by the Stack Overflow website.

(If you have no idea what "Markdown" and "Swift" are, you can just stop reading now.)

F#'s Pipe-Forward Operator in Swift

Note: At WWDC 2015, Apple announced Swift 2, which includes changes and a new feature called "protocol extensions" that render much of the code below either irrelevant or incorrect. This article applies to Swift 1.x.

Apple's new Swift programming language isn't really a functional programming language. However, it does support generic types and functional-programming patterns, so many FP aficionados are implementing their favorite functional idioms and libraries from other programming languages in Swift.

I've been a functional-programming enthusiast for a couple of decades, and I'm playing with this myself. A feature I like in the F# programming language is its pipe-forward operator, |>, which helps to write readable functional code. This article explains what that operator is, why you would want to use it, and how to do it in Swift.

Disclaimer: The code in this article is based upon the versions of the Swift language in Xcode 6.3. Future changes to Swift syntax or its standard library may render all of this incorrect or obsolete.

KJTipCalculator: A Simple iOS Swift App

Screenshot

As an experiment in using Apple's new Swift programming language, I whipped up a simple tip-calculator app for iOS 8.

Yes, the world really needs Yet Another Tip Calculator, and it also really needs Yet Another Swift App Example.

In addition to using Swift, the app also uses an embedded framework, which is a new feature of iOS 8. The framework contains functions and classes for converting between numbers and text, and it might be useful in other apps.

Source is available on GitHub: https://github.com/kristopherjohnson/KJTipCalculator

If you actually want to use this app, you'll have to build it yourself. But if you have iOS 8 on your phone now, you should know how to do that.

Syndicate content