A simple interpreted programming language built upon Python.
Acorn is an interpreted language which does not depend upon a compiler. Its only dependence is having Python3 (working up to 3.6) installed on the machine of intended coding. Acorn is very similar to Javascript. A more comprehensive tutorial will be written in the future.
To start coding in Acorn on MacOS, clone or download the Acorn directory onto your desktop. Inside the Acorn folder, run the setup
executable by doing
bash setup.sh
Once that is set up, to test if Acorn was sucessfully installed, create a new file on your desktop named Hello.acorn
, so we can create a Hello World program. Inside the file, have the line:
print("Hello, World from Acorn!");
In Terminal, locate the Hello.acorn
file and run:
cocoa Hello.acorn
If sucessful, your Hello World program should output:
Hello, World from Acorn!
Strings
- String concatenation
- Fix issue in 1.1.1 were strings may become truncated
- Integers are represented as 64-bits and can be expanded to 1024-bit
- Cocoa can recognize hexidecimal numbers
- Fix issue in 1.1.1 were expressions may fail during order of operations
- Modulo operator
- Logical And & Or
- Bit And & Or
- Bit left & right shift
- Bit invert
- Fix issue in 1.1.1 were banged and negated expression/value fail to work
- Operation order of precdence from lowest to highest: ||, &&, {<,<=,==,>=,>,<<,>>}, {+,-}, {*,/,%,~>}, |, &, {-, !, ~}, (expression)
- Functions may take more than one argument now
- Recursive functions can be called within the return statement now
- Functions return Null on void return type functions
- Else if statement are now available in Cocoa
- Cocoa offers full-featured for loops with index variable, condition, and counter, delegated to the responsibility of the coder
- For loops can be nested now
- Basic for loops in 1.1.1 have been demoted to foreach loop
- Cocoa offers while loops
-
Cocoa offers basic casting of variables to other primative types
- Acorn is no longer a global-scope language. Lexical scope is now implement in Cocoa.
- Boolean are represented as 1s and 0s and no longer true or false respectively.
- Integer overflow through any arithmetic will result in INT_MAX 64-bit unsigned to be returned
- Rewritten tokenizer
- Rewritten parser
- Strict token parsing, prevent dangling lexem
- Safer parsing by no longer exposing parser to raw non-foundation values
- Parser no longer accesses Cocoa's stack or heap. An environment stack has replaced it for the implementation of lexical scoping
- Previoius recursive implementation of Acorn have been rewritten in Cocoa to be loops, reducing the weight on Python
- Cocoa takes advantage of improved dictionaries in Python 3.6 as for its implementation of lexical scoping
- Cocoa improves the speed of certain tasks. Recursive functions are
19%
more efficent in Cocoa. Nested for loops were implemented in Acorn 1.1.1 under its environment and tested again Cocoa. Cocoa showed a7%
speed improvement.
All Acorn files must have the extention of .acorn
All statements in Acorn must be finished with a semi-colon (including if statements and functions).
Printing
Printing to the console is simple.
print("Hello, World from Acorn!");
As of Acorn 1.1, Acorn disallows multiple types in call-by-name operations e.g.
print("Give me a high "+5);
is disallowed. Values in a call-by-name operation must be casted first. Homogenous-typed values forming an expression are allowed such as
print(2.14159+1.0);
Cocoa infers the type of printed values and no longer assumes integers to be floats when printed.
Conditionals
If statements
if(0){
print("This should not be true.");
}else if(42){
print("The answer to everything?");
}else{
print("Some sort of number that will pass in a multi-dimensional universe...");
};
The clause binded to the first if
statement will execute if the condition evaulates to true. The condition will first be casted as a boolean. Expression/values that evaluate to true are: booleans that are true, non-zero numbers, non-empty string, logical conditions evaluating to true, and bitwise conditions evaluating to true
Variables
Declaring variable example:
var x = 0;
variables can be bounded to values or homogenous-typed expressions. Constants have been deprecated from Cocoa
Assigning variables
var pi = 3.15; pi = 3.14159;
Arrays
var fib = [0,1,1,2,3,5,8];
var nthFib = fib[n];
Arrays in Cocoa may handle non-homogenous type expressions. An array's size is fix upon declaration. An array may be indexed as given in the example above; array start at index 0. Arrays have reference semantics, meaning modifications to the array that was given as an argument within a function will affect the original.
FunctionsFunction declaration cannot be anonymous; they must be bound to a variable name. All functions are required to return at the end of the function body. Functions may take zero to multiple arugments. Functions return Null
on void return functions. Function example:
var f = function(x){
return 1+x;
};
Calling function f:
f(1);
Function are first-class data type, which allows Acorn to have higher-order functions.
This example function squares the values in a array through mapping
var map = function(f, l, length){
for(var i = 0; i<length; i=i+1){
l[i] = f(l[i]);
};
};
var square = function(x){
return x*x;
};
var image = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
map(square, image, 10);
By mapping, values in the image
array are squared, so elements in the image array are now.
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Input
Acorn accepts input from the stdin() function.
var n = stdin();
stdin()
automatically infers the type of input.
For Loop For loops in Cocoa require the following syntax. It must be given an initial value, followed by a condition, followed by an incrementer. Upon each call to the body, the incrermenter will be re-evalutated, and the condition will be check to determine if the body of the loop will execute again.
For loops:
for(var i = 0; i<10; i=i+1){
...
};
Casting
Expressions when called with the cast operator ~>
will attempt to cast to the desired type. Upon sucess, the expression will be the new type. Upon failure, a run time error will be thrown
Cocoa supports casting to types: (Int)
, (Float)
, (Bool)
, (String)
.
Example casting an integer to a string.
var str = "I have "+3~>(String)+" apples."
Will evaulate to the string being, I have 3 apples
.
For Each
For loops in Acorn 1.1.1 has be demoted to foreach
loops. Foreach loops should be taken advantage for speed when simple iterations are needed.
foreach( i = 0 < 10 ){
...
};
A foreach loop initiates a local variable with the given name and accepts a given range composed of integers. Foreach loops may only handle < and <= conditions.
Bug Fixes
- 1.1 Fix string comparison giving false negatives during conditional checks.
- 1.1.1 Fix strings being handled incorrectly when interpreted, which led to negatives during conditional checks.
- 1.1.1 Fix foreach loop bodies which did not handle more than one statement.
- 1.1.1 Fix binary logicial operators which gave incorrect results due to the evaluation phase of interpretation.