Learn to Code via Tutorials on Repl.it!

← Back to all posts
Custom Language Tutorial Part 3 (Leaning CCL - A Custom Language)
Geocube101 (591)

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 4 is out here

Share Page is here

CCL - A language by Geocube101

This tutorial will cover how to create classes, class instances, and functions.

[Note]: This information can also be found in the tutorial folder of the repl


CCL Functions


 

Functions in CCL are a bit more complex than other variables.

Creating Functions

The method to create a function is:

create function : "name"'params' >> {
}

The name is the name of the function and can conflict with python keywords.
Function parameters directly follow the name and look like a hash.
An example of a function parameter hash

{string: path}

An example of a function is:

create function : pow{float: base, float: exp} >> {
	get new float : base ** exp >> return
}

The parameters are

  • base: which is a float
  • exp: which is also a float

Function parameters are not required, in which case just don't put the function parameter hash

create function : greet >> {
	get new string : "Hello" >> console
}

 

You can also (as of Beta 1.2) declare optional function values for your function.
By adding a right-biased pass token "<<" and then supplying the value, the parameter will be optional.

create function : input{string: prompt << unset} >> {
	~prompt setmessage prompt
	create string : unset >> out
	get string : console >> out
	~prompt enable
	get string : out >> return
}

The above function creates a function called input with one optional parameter "prompt"

 


 

Returning Values from Functions

To return variables from functions, we go once more to the "get" command.

get 'new' "type" : "value/name" >> return

 


 

CCL Classes


 

Probably the most complex thing is the classes. Their creation is not different from the functions, but creating instances is a litte different.

 

Creating Classes

Creating classes is not that different from creating functions, except there is no parameter hash.

create class : "class_name" >> {

}

 

Now to add parameters to a function, you need the "initialize" function, which is the same as "__init__" for python users.

initialize(*args)
{

}

"*args" are just a list of parameters supplied to the function. In this case, "instance", which is the CCL equivilent of "this" and "self" in other languages, is not nesseccary as it it automatically supplied.
Arguments must specify both the variable type and the variable name

The "instance" variable counts as an "object"

 

Creating methods in CCL is exactly the same as creating functions. Just define the function inside of the class code and that's it. When defining methods, you must add "instance" to the parameter hash if you want access to the instance.

An example class

create class : Matrix >> {
	
	//Initializer
	initialize(integer: length, integer: width)
	{
		create integer : length >> instance.l
		create integer : width >> instance.w
		create array : [] >> instance.matrix
	}

	//Bound method with access to instance
	create function : output{object: instance} >> {
		get array : instance.matrix >> console
	}

	//Bound method without access to instance
	create function : createMatrixFromArray{string: matrix} >> {
		get array : instance.matrix >> console
	}
}

 


 

Creating Class Instances

Class instances can be created by specifying the class and a parameter array (if one is requried).

create "class_name" : "argument_array" >> "name"

Example: using the FileInterface class

create FileInterface : ["test.txt"] >> f

This creates a new "FileInterface" object and supplies the argument "test.txt" as the file name and stores the variable to "f".
If your class has more than one required parameter, then simply add the value to supply to the argument array:

create Matrix : [10, 10] >> matrix