Custom Language Tutorial Part 1 (Leaning CCL - A Custom Language)
I recently made a program that coverts a text file of custom code into python code to execute (which is the repl currently displaying)
This series of tutorials will be on how to use the language.
To test out some code, enter it into the "program.ccl" file
The idea to post this on the learn board so others can use this program was suggested by rediar
Part 2 is out here
Share Page is here
CCL - A language by Geocube101
This tutorial will cover the basic variable types of the language, how to comment, and the different commands.
[Note]: This information can also be found in the tutorial folder of the repl
Basic Variable Types
There are a few main variables in CCL that are required. They are the basic variables found in other languages like java and C++ (but don't have as many types)
- boolean: The boolean is a variable that indicates either "true" or "false"
- char: A single character (must be declared using single quotes)
- integer: A whole number
- float: A decimal number
- string: A sequence of characters (must be declared using double quotes)
- array: A list of items (arrays are zero-indexed)
- hash: A list of items but with custom keys
- object: This can be any value listed above
- pointer: Is always equal to the variable it's pointing to.
Arrays in CCL can contain values of multiple types
Single line comments affect only a single line and are indicated by two slashes and can occure anywhere in the line.
Multi line comments can affect more than one line and are started with "/*" and ended with "*/". If the comment is not ended, a
SyntaxError will be thrown.
Creating variables in CCL starts with the "create" command.
This is how basic variables are created. If the type and value don't match, you'll normally get a
Some extra values are allowed, mainly the "unset" value, which indicates that a variable's value is unknown, and "void" which applies a null value to the variable.
This creates a string, with the value "Hello World" and assigns it to a variable called "greeting".
You cannot create a variable with the same name as an already existing variable, otherwise you will get a
Getting and Printing variables
Getting variables will make use of the "get" command.
In this case, the type of variable is indicating the type of the value, not the target. If the type and value type do not match, the compiler will throw a
So the command for getting the string created in the previous section would be:
We still don't know where to put the variable though.
There are some built-in constant variables that indicate various targets. "console" is the representation to std::out, which will print something to the console. If we wanted to print that variable, then we would do...
...which would print "Hello World" to the console
The get command can also modify variables. If the target of the get command is a variable of the same type, the target variable will be modified to the value of the variable you entered.
Now if you saw it, in the previous example is the keyword "new", which tells the get command that you wish to call upon a non-exisant variable. Instead of accepting a variable name as the value, it will instead accept a value. If you wanted to pring "Hello World" without having to create a string, then you could use "new" to remove haveing to create a new variable.
Get is aslo used for error throwing, but that will come later
Setting and modifying variables
Modifying variables requries the "set" command, which sets the value of a variable to something else.
In this case, the value and target types must match the type specified in the command. Unlike the get command, "console" is not a valid value.
So before going on to some other commands, I'll go over how to get user input from the console.
This actually requries the use of the "get" command mentioned in the previous section.
In order to get user input, just do...
This tells the compiler that the console shall supply a value to the variable. The value entered into the console MUST match the variable type or you will get a
TypeError. This means that if you have...
...if someone enteres "Hi" into the program, it will fail.
It's probably best if you accept it as a string and then test if it can be an intger.
(The console prompt looks like this "@: ", but will be changable once the NBT system is implemented)
Now I'm not entirely sure why you'd want to destroy a variable (besides maybe freeing up program memory), but it's possible anyway, using the "destroy" command.
This command can be dangerous as if you don't supply a target (which is optional), it will delete all variables of the specified type.
Once a variable is destroyed, it cannot be accessed again unless the code is re-run.
The final command is the "import" command
This allows you to import a file, module, or parts from a file or module
You can import individual functions, classes or the whole file.
- file: The "file" type imports an entire CCL file and compiles it.
- module: The "module" type imports an entire python file (must have ".mod" extension).
- function: This type imports a single function from a CCL file ".ccl".
- class: This type imports a single class from a CCL file ".ccl".
Importing Individual Functions and Classes
To import individual functions, you can use:
To import individual classes, you can use"