How to make an Aardvark module in Python
h
hg0428 (193)

If you do not know Aardvark, go here: https://repl.it/talk/challenge/Aardvark-The-language-that-covers-all-of-your-needs/51790

Sorry, but python is currently the only language supported for making Aardvark modules.
In Aardvark, a simple module can change a lot about the language.
For it to be an Aardvark module, you have to make sure Aardvark is installed, and that you are in the Aardvark folder. To start we need to make a new python file, give it any title you want. Now we can start making our module. Another thing your file will need is from Aardvark import *, this will give you access to all the Aardvark developer tools. For it to be an includable Aardvark module you also have to add #Aardvark.library to your python file, this allows Aardvark to #include it. Now we can start making our module, lets start by making a simple function that takes 2 numbers and adds them together:

from Aardvark import *
#Aardvark.library


@Aardvark.function("Add2nums")
def myAdd2nums_function(name, num1, num2):
    return num1 + num2

@Aardvark.function() tells the program that myAdd2nums_function is an Aardvark function and it has the name "Add2nums". In def myAdd2nums_function(name, num1, num2):, you see that name parameter, this argument is always supplied automatically by Aardvark, it's value is always the same as its name, in this case: "Add2nums". The other two parameters are the ones that the Aardvark user will enter when using that function. return num1 + num2, this line takes the two arguments that the user entered, adds them together, and returns them so that the user can store it in a variable or something. You have finally created your first Aardvark module and function using python! You can go off and start making all your functions now, but I recommend staying until the end of this tutorial. Now, let's do something a little harder, what if you want to make your own Aardvark data type? Well, let's learn how to do that too.

from Aardvark import *
#Aardvark.library
import re

@Aardvark.type("MynewType")
def mynewtype(what, line_num): 
    what = str(what)
    ismynewtype = re.fullmatch("MynewType\((.*?)\)", what)
    if ismynewtype:
        return True, f"It is my new type: {mynewtype.groups()[0]}"
    elif what.startswith("It is my new type: "):
        return True, what
    else:
        return False, what

Wow, that was a lot of code, do you understand it all already, probably not.
Let's break it down for you, first, we import Aardvark and make it an includable module, next we define mynewtype() as an Aardvark type using @Aardvark.type(), and we name our type: "MynewType".
Why does our function take two arguments? Because those arguments are always supplied to Aardvark data types, no more or less are ever given.
Now we turn the first argument, what, to a string using what=str(what), this makes sure it is a string so our module doesn't give any errors.
Next, we check to see if what matches with the regular expression "MynewType\((.*?)\)" and if so we return True and the value of it which is "It is my new type" plus whatever what is the parentheses, but why do we return True? Because in Aardvark when one of the datatypes returns True, that means it is that datatype. So if ismynewtype is True then we return True to tell Aardvark that this is the correct datatype. Also, if what.startswith("It is my new type: ") then we know it is this type, and it already went through this, so we return True and it how it is.
If both of those ifs are false then we return false and it how it is. Now let's test our datatype with this Aardvark code, replace <filesname> to the name of your python file with your datatype.

#include <filename>
output(MynewType(hello)) / should output "It is my new type: hello" \

So now you can create a function and a basic data type. Let's add more to our data type and learn how to make attributes and methods. Methods a pretty straight forward. Let's add this code to the file:

@Aardvark.method("MynewType", "amethod")
def myfirstmethod(name, calledon):
    return calledon+" amethod has been called on this."

That, when called on a MynewType object, will return the MynewType object plus " amethod has been called on this.". The first parameter is name, it works just like it does in functions. The second parameter is calledon, this is also automatically supplied by Aardvark and it is the object that the method is being calledon.
You can add more parameters and it will work very similar to functions except it is a method and has the calledon.
Now to start on making attributes. Attributes require a calculator function, a function to calculate to attribute's value Lets add the code below to our module:

def mylength_attribue(myclssobj):
    return len(myclassobj)

Aardvark.addattribute("MynewType", "length", mylength_attribue)

Let's take a look at what it does. We define the mylength_attribue function, its just a normal function. Then we use Aardvark.addattribute() to add that function as the calculator function for the length attribute of MynewType.
I hope you enjoyed this tutorial and hope to see a lot more modules added to APE soon! 👍😁

If you want to publish your module to APE, then contact our team at [email protected], or ask on our discord in the modules section.

You are viewing a single comment. View All
CodingCactus (4192)

can't use Aardvark to make Aardvark modules??? hrmmmm

hg0428 (193)

you can, this is just the tutorial for making it in python.
All Aardvark files work as Aardvark modules.
@CodingCactus