japl/examples/example.jpl

52 lines
950 B
Plaintext

// 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
// 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
// Control flow statements
var n = 0;
while (n <= 10) { // While loops
if (n <= 5) { // If statements
print n;
}
n = n + 1;
}
// Functions
print clock(); // Function calls
fun count(n) { // Function definitions
if (n > 1) count(n - 1);
print n;
}
count(3);