Rtfl Programming Language by Michael Termer Licensed under the GNU Public License v3 (http://www.gnu.org/licenses/gpl-3.0.html) Table of Contents 1. General Syntax 2. Variables 3. Functions 4. Built-in Functions 1. General Syntax Rtfl's syntax is somewhat like that of JavaScript. Comments can be expressed by beginning a line with //. Calling functions is a simple as typing function_name(). Operations such as while loops can be achieved by typing while some_expression_that_returns_a_boolean { // Code } The same can be done for if statements, just replace "while" with "if". Expressions can be literals like true, false, null, undefined, void, "some string", 1, 1.0. There are 5 types of variables in Rtfl, those being boolean, string, number, array, and null. Boolean values can be expressed using either true or false, or a reference to a variable that contains a boolean. String values can be expressed by some text enclosed in quotes, or a reference to a variable. Quotes can be escaped using \, and \ can be escaped using the same character. Numbers can be expressed either in integers such as 1 and -1, or decimals such as 1.0 and -1.0, or of course a reference to a variable. Null values can be expressed with null, undefined, and void, as well as a reference to a variable. 2. Logic Logical expressions are expressions that can perform a logical operation and express a boolean. They can use the following operators: =, <, >, |, and &. These act like standard logical operators. Logic expressions must start with "[" and end with "]". Example: [false = false] This would express a value of true, since false equals false. The equals operator can be swapped out for any other logical operator. To add a "not" to the expression, simply put an "!" before the first bracket to have the expression express the opposite of its normal result. 3. Variables Rtfl can store any value inside of a variable. Like in JavaScript, variables are dynamically typed. To declare a variable, simply type def var_name = "some value or expression" To set a variable after it's been defined, simply use the statement above, without the "def". To clear a variable, simply use "undef" undef var_name Doing such will clear the variable definition. To reference a variable, all you have to do is type the name of it. For exaple: def a_boolean = true if a_boolean { } Variables can also be locally scoped, which means they can only be accessed from the current code block, and blocks inside of it (including any load(), eval(), or async() calls inside the block). To declare a variable in a local scope, simply type local var_name = "value" Other than scoping, these variables behave identically to normally scoped variables. 4. Functions In Rtfl, you can define functions to do certain actions. Functions can receive and return values, just like in JavaScript. An example function declaration: func some_function { return "hello world" } This function, when called, will return a string with the value "hell world". Note, returning a value is optional. To get a parameter, simply reference the variable arg*. So referencing arg1 will allow you to get the value of the first argument. To get the amount of parameters passed, reference arglen, which is a number. Just like with variables, functions can be cleared by typing unfunc some_function 5. Error handling Normally, if an error is encountered, the interpreter will print the error in the terminal, and continue executing. However, it is possible to intercept the error and get it as a string using Rtfl's error handling. To handle errors for a code block, follow the below code snippet: error err { // some code that will cause an error } Notice three parts of this: error, err, and the code in the curly brackets. "error" simply indicates that this is an error handling block. "err" can be anything, but for the sake of convenience, it's marked as "err" here. This is simply the name of the variable to create containing the error message if an error occurs in the code block. The last part is the code, which will be executed normally. After the code in the error block has been executed, a variable with the name specified will be initialized, either containing the string "ok" if the code ran without errors, or will contain the error string generated by the code block. Here's a snipped to show how to print an error if one occurs in an error block: error err { // some code that may or may not cause an error } if not(equals(err, "ok")) { // this executes if err does not equal "ok" println("Error!") println(err) } 6. Built-in Functions Rtfl comes with a set of built-in functions. Below are their usage and descriptions. print() - prints the specified string to the console println() - same as print, except adds a newline inc() - increments the specified variable by 1 dec() - decrements the specified variable by 1 equals(, ) - returns whether the two parameters are equal more_than(, ) - returns whether the first parameter is more than the second one less_than(, ) - returns whether the first parameter is less than the second one concat(, ) - returns both provided strings joined together var() - returns the value of the specified variable by name to_string() - returns the string representation of the provided parameter eval() - runs the provided code load() - runs the specified file add(, ) - adds the second number to the first sub(, ) - subtracts the second number from the first mul(, ) - multiplies the first number by the second div(, ) - divides the first number by the second read_file() - reads the specified file and returns its contents as a string write_file(, ) - writes the specified content (2nd param) to the specified file (1st param) file_exists() - returns whether the specified file exists not() - returns the opposite of the provided boolean gc() - runs the Java garbage collector open_terminal() - opens the terminal reader close_terminal() - closes the terminal reader read_terminal() - reads a line from the terminal and returns it terminal_open() - returns whether the terminal reader is open exit() - terminates the program array - returns a new array array_add(, ) - adds the provided value to the specified array array_remove(, ) - removes an item in the specified array at the provided index array_get(, ) - gets an item in the specified array at the provided index array_length() - returns the length of the specified array and(, ) - returns whether both parameters are true or(, ) - returns whether either of the parameters are true split(, ) - splits the first string into an array, splitting at the every instance of the second string starts_with(, ) - returns whether the first string starts with the second string ends_with(, ) - returns whether the first string ends with the second string substring(, , ) - returns a sub-string of the provided string using the two provided co- ordinates char_at(, ) - returns the character at the specified coordinate string_length() - returns the length of the specified string array_set(, , ) - sets the value of a specific index on the provided array type() - returns the name of the type of the provided value in string form to_number() - converts the specified string to a number string_replace(, , ) - replaces the string specified in the second argument with the string specified in the third, inside of the string specified in the first argument library() - loads the specified jar rtfl library sleep() - sleeps for the specified amount of milliseconds async() - executes the specified Rtfl code asynchronously read_http(, [optional]) - returns the HTTP response of the URL specified in the first parameter, optionally with the method specified in the second parameter. delete_file() - deletes the specified file. list_files() - lists all the files and directories in the specified directory. create_directory() - creates a directory with the specified path. exec() - runs the provided system command 7. Known Bugs No more than two function calls can be nested into eachother. For instance, if you were to type println(add(1,1)) it would work, but if you were to type println(add(1,sub(2,1))) the interpreter would encounter an error. 8. Making Jar Rtfl Libraries To make a library that can be loaded with library(), you must create a jar with a least one class named "Lib" that implements the net.termer.rtfl.Library interface. When the library is loaded with library(), the "initialize" method will be called in the library.