2021-02-28 14:10:17 +01:00
|
|
|
/*
|
2021-02-09 00:35:40 +01:00
|
|
|
[Test: all]
|
|
|
|
[skip]
|
2021-02-28 14:10:17 +01:00
|
|
|
*/
|
|
|
|
//[source: mixed]
|
2020-10-26 22:55:20 +01:00
|
|
|
// Example file to test JAPL's syntax
|
|
|
|
|
|
|
|
// Mathematical expressions
|
|
|
|
|
|
|
|
2 + 2;
|
|
|
|
-1 * 6;
|
|
|
|
3 * (9 / 2); // Parentheses for grouping
|
|
|
|
8 % 2; // Modulo division
|
|
|
|
6 ** 9; // Exponentiation
|
|
|
|
~5; // Binary NOT
|
|
|
|
2 ^ 5; // XOR
|
|
|
|
3 & 9; // AND
|
|
|
|
9 | 3; // OR
|
|
|
|
|
|
|
|
|
|
|
|
// Variable definition and assignment
|
|
|
|
|
|
|
|
var name = "bob"; // Dynamically typed
|
|
|
|
name = "joe"; // Can only be assigned if it's defined
|
|
|
|
del name; // Delete a variable
|
|
|
|
var foo; // Unitialized variables are equal to nil
|
|
|
|
|
|
|
|
// Scoping
|
|
|
|
|
|
|
|
var a = "global";
|
|
|
|
var b = "global1";
|
|
|
|
{ // open a new scope
|
|
|
|
var b = "local"; // Shadow the global variable
|
|
|
|
print(a); // This falls back to the global scope
|
|
|
|
print(b);
|
|
|
|
}
|
|
|
|
print(a);
|
|
|
|
print(b); // The outer scope isn't affected
|
|
|
|
|
|
|
|
/*
|
|
|
|
A multiline comment
|
|
|
|
yay!
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Control flow statements
|
|
|
|
|
|
|
|
var n = 0;
|
|
|
|
while (n <= 10) { // While loops
|
|
|
|
if (n <= 5) { // If statements
|
|
|
|
print(n);
|
|
|
|
}
|
|
|
|
n = n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < 10; i = i + 1) { // For loops
|
|
|
|
print(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
|
|
|
|
print(clock()); // Function calls
|
|
|
|
|
|
|
|
fun count(n) { // Function definitions
|
|
|
|
if (n > 1) count(n - 1); // Recursion works
|
|
|
|
print(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
// Closures work too!
|
|
|
|
|
|
|
|
var a = "global";
|
|
|
|
{
|
|
|
|
fun showA() {
|
|
|
|
print(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
showA();
|
|
|
|
var a = "block";
|
|
|
|
showA();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nested functions
|
|
|
|
|
|
|
|
fun makeCounter() {
|
|
|
|
var i = 0;
|
|
|
|
fun count() {
|
|
|
|
i = i + 1;
|
|
|
|
print(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
var counter = makeCounter();
|
|
|
|
counter(); // "1".
|
|
|
|
counter(); // "2".
|
|
|
|
|
|
|
|
|
|
|
|
// Classes
|
|
|
|
|
|
|
|
class Person {
|
|
|
|
|
|
|
|
init(name) { // Class initializer
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
greet() { // Methods don't use the 'fun' keyword!
|
|
|
|
print("Hello, " + this.name); // this refers to the current instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bob = Person("Bob"); // Object creation
|
|
|
|
bob.greet(); // Prints Hello, Bob
|
|
|
|
var greetbob = bob.greet; // Functions and methods are first-class objects! (classes are too)
|
|
|
|
greetbob();
|
|
|
|
|
|
|
|
|
|
|
|
class Male < Person { // Male inherits from person
|
|
|
|
|
|
|
|
init(name) {
|
|
|
|
super.init(name); // Inherits constructor behavior
|
|
|
|
this.sex = "male";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
greet() {
|
|
|
|
super.greet(); // Inherits behavior from superclass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mark = Male("Mark");
|
|
|
|
mark.greet();
|
|
|
|
|
|
|
|
// Strings
|
|
|
|
|
|
|
|
"string slicing!"[0]; // 0-indexed
|
|
|
|
"ranges work too"[0:5];
|
|
|
|
"implicit end"[3:]; // Ends at the end of the string
|
|
|
|
"implicit start"[:5]; // From 0 to 5
|
|
|
|
"hello" + " world"; // Strings are immutable!
|
|
|
|
"hello" * 3; //hellohellohello
|
2021-02-28 14:10:17 +01:00
|
|
|
//[end]
|
|
|
|
//[end]
|