This time I am going to dig a little deeper into ASPL, and provide some details about its syntax and the reasoning behind it.
I chose it simply because everyone knows it, so everyone will feel right at home when they start using the language.
Dynamic type system
It means that any variable can store values of any type. And the same variable can also store values of any type when reassigned. Type checks are happening at runtime. Dynamic typing makes it easy to use a language, gives us a lot simpler language, and makes it way easier to implement the type system. I believe it’s the perfect fit for writing your first language.
Manual memory management is tedious and prone to errors. Virtually every new language tries to avoid it, and so does ASPL. It’s much easier to write correct code without it and the development time speeds up as well.
Reference counting is easier to implement but has some serious drawbacks, and nowadays less and less languages are using them. Amongst the few that are still go with reference counting is Objective-C and its newer counterpart, Swift for example.
Tracing on the other hand is much more complicated but has some advantages over the reference counting, and for that reason I chose to implement it in ASPL.
I grew up using mostly imperative languages, this is the style that I am most familiar with. Although I like functional programming, especially some awesome features in Erlang, I still feel that it is best for me to choose the imperative programming style to implement in ASPL.
But, enough of the theory! Let’s see how the language itself will look like.
There are 2 types of comments in ASPL.
// This is a comment
And block comments:
/* This is also a comment */
The difference is that block comments can wrap multiple lines.
ASPL has a real boolean data type built-in. It has two possible values, and also a literal for both.
true; // This is true false; // This is false
To keep things simple ASPL only supports one literal notation for integers.
125; // This is an integer
For the sake of simplicity there is no hexadecimal, octal, or binary notation.
Well, I figured if we have integers we should have floating point numbers as well.
165.34; // This is a double
Similarly to the others, strings also have only one literal. Strings are enclosed in double quotes.
"hello world"; // This is a string ""; // This is an empty string
In ASPL we don’t care about various character encodings either.
Having a dynamically typed imperative language usually means you are not forced to initialize every variable you create. And if you don’t, the language needs a way to represent variables with no value. In some languages it is called null, and called nil in others. ASPL uses nil. There is also a built-in literal for this.
nil; // I represent "no value"
Operators and expressions
An expression is a combination of constants / variables / functions and operators that will return a value after evaluation.
There are six arithmetic operators supported in ASPL.
Four of these are infix operators, meaning that the operator needs two operands, and the operator is in the middle, between its operands. They are also called binary operators. They are: addition, subtraction, multiplication and division.
x + y; x - y; x * y; x / y;
There are three prefix operators in ASPL: negation, increment and decrement. They can be applied to integers and doubles. Prefix operators have only one operand, and the operator is before its operand.
-x; ++x; --x;
There are two postfix operators in ASPL: increment and decrement. They can be applied to integers and doubles. Postfix operators have only one operand, and the operator is after its operand.
The ternary operator
The ternary operator is the only one in ASPL that has more than two operands. It is well-known in C and C++, and its syntax is the same in ASPL as well.
x < 2 ? true : false;
ASPL has 4 comparison operators similarly to most other languages. They are: less than, less than or equal, greater than, greater than or equal. They can be applied to integers and doubles.
x < y; x <= y; x > y; x >= y;
ASPL has two equality operators: equal and not equal. They can be applied to values of any kind. Values of different kinds are never equal.
23 == 23; // true "apples" == "oranges"; // false 12 != "12"; // true
ASPL has three logical operators.
Logical not (!) is a prefix operator. It negates the value of its operand, which must be a logical expression.
!true; // false !false; // true
The other two are logical or (||), and logical and (&&).
The logical and operator returns true if both of its operands are true, this also means it operates on two logical expressions.
true && false; // false true && true; // true
The logical or operator returns true if at least one of its operands (which are logical expressions) are true.
true || false; // true false || false; // false
Important to mention that both of these operators are short circuit. This means that the and operator immediately returns false if its left operand is false, and it will not evaluate its right operand in this case. The or operator returns true immediately if its left operand is true, and skip evaluating its right operand.
Showing all of the important features of the language takes a lot of time (and typing ☺), so I’m gonna finish here today.
Next time I will continue with statements, blocks, variables, control flow, functions and closures.
Stay tuned. I’ll be back.