Attila's Blog

About writing your own programming language in C, Go and Swift

The ASPL language - part 4

Jul 31, 2018 Comments

Categories: aspl
Tags: aspl

This is the last part of the introduction of ASPL. Let’s see the remaining features we haven’t discussed yet: classes, inheritance and the standard library.

Classes

I think, most software developers in the world today use some object oriented language. It seems obvious that OOP cannot be left out from a new language, that’s why I chose to implement it.

When you’d like to implement object oriented features in your language you have make an important decision.

What kind of object orientation would you like to see in your language? The traditional, class based style that C++ and Java uses? Or you prefer to go Javascript style with prototypes?

Both has its merits. Classes are still more wide-spread with languages like C++, Java and C# (I think). But with Web 2.0 came along Javascript and took over the world, and programmers started to get familiar with prototype based OOP.

All of my life I worked with class based OOP, so I feel it is the right choice for me to implement class-based OOP in ASPL. Also, in my experience most people working with prototype-based languages spend most of their time to implement or simulate a class-based inheritance system, and this tells me that most likely other people also feel more familiar with the more traditional class-based approach.

Classes are containers of custom data and code. Here’s what a class definition looks like in ASPL:

class Greeter {
    func goodMorning(name) {
        print "Good morning " + name + "!";
    }
    
    func goodAfternoon() {
        print "Good afternoon!";
    }
}

You can create new instances of a class, and call methods on them like this:

var greeter = Greeter();

greeter.goodAfternoon(); // Good afternoon
greeter.goodMorning("Joe") // Good morning Joe! 

Next, we need data stored in our classes. Similarly to Javascript, ASPL allows it to assign any properties to class instances:

class Greeter {
    func greet(name) {
        print "Good " + this.timeOfDay + " " + name + "!";
    }
}

var greeter = Greeter();
greeter.timeOfDay = "morning";
greeter.greet("Joe"); // Good morning Joe!

Assigning a field to an instance will create it if it doesn’t exist, and set its new value if it already does. Inside the methods you can access fields via the this keyword.

Classes can have initializers as well. It is done via the init keyword:

class Greeter {
    init(timeOfDay) {
        this.timeOfDay = timeOfDAy;
    }
    
    func greet(name) {
        print "Good " + this.timeOfDay + " " + name + "!";
    }
}

var greeter = Greeter("morning");
greeter.greet("Joe"); // Good morning Joe!

Inheritance

ASPL supports single inheritance. You specify the base class with : in your class definition:

class Employee {
    init(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    func fullName() {
        return firstName + " " + lastName;
    }
    
    func doWork() {
        print this.fullName() + " is doing some work";
    }
}

class Manager: Employee {
    func doWork() {
        super.doWork();
        print ", and also doing some supervising as well";
    }
    
    func supervise() {
        print "Supervising my subordinates";
    }
}

var manager = Manager("John", "Doe");
manager.doWork(); // John Doe is doing some work, and also doing some supervising as well
manager.supervise(); // Supervising my subordinates

Subclasses inherit every method, even the initializers, if there is any. You can also override any method, including the initializers. The same method defined in the superclass can be called via the super keyword.

Standard library

We are now done with all the features of the ASPL language. All that’s left is the standard library, the functionality that is offered by the language and is implemented directly inside the vm.

At the beginning this will be very thin, consists of only one thing, the print statement. Later on, we will introduce some new features to make ASPL a little more useful, things like:

  • String manipulation
  • Reading input from the user
  • File I/O
  • Networking
  • Threading

Well, that is it. We have seen every feature of ASPL, and we are ready to start writing some code. This is exactly what I’m gonna do next time.

Stay tuned. I’ll be back.

Tags: aspl

← The ASPL language - part 3 The interpreter framework →

comments powered by Disqus