Chapter 8. Modules

Modules represent groupings of functions or classes. Each Hassium source file is it's own module. Hassium also comes equipped with an array of builtin modules for Crypto, Drawing, IO, Math, Net, Reflection, Text, and Utils.

8.1 Importing a Module from File

You can import Hassium source code from another file as a module. To test this, open a file called Person.has. This file will contain a class we will define.

Person.has:

class Person {
    name { get { return this.name_; } set { ; } }
    age { get { return this.age_; } set { this.age_ = value; } }

    func new (name : string, age : int) {
        this.name_ = name;
        this.age_ = age;
    }

    func sayhello () : null {
        printf ("Hello! My name is {0} and I am {1} years old!\n", this.name_, this.age_);
    }
}

Notice this file just contains the class definition, there is no main function. Now create the source file that will contain main.

Main.has:



Output:

Hello! My name is John and I am 37 years old!
Hello! My name is Victoria and I am 40 years old!

8.1.1 Statement - use from

At the top of the file Main.has before the main declaration, there is the statement use Person from Person. The use statement is used to import builtin or external modules. In this case, the module name given was Person. This causes the Hassium compiler to search for the files Person, Person.has, and Person.dll in the current directory. In addition, this also searches for %HOMEDRIVE%%HOMEPATH%/Person, %HOMEDRIVE%%HOMEPATH%/Person.has, %HOMEDRIVE%%HOMEPATH%/Person.dll, %HOMEDRIVE%%HOMEPATH%/.Hassium/Person, %HOMEDRIVE%%HOMEPATH%/.Hassium/Person.has, %HOMEDRIVE%%HOMEPATH%/.Hassium/Person.dll, and for a builtin module named Person. Since you saved the file Person.has in the same directory as Main.has, the compiler locates the file Person.has, compiles it, then imports the Person class from Person.has into the current module. This gives Main.has access to the class Person.

8.2 Writing C# Modules

In order to adequately understand and benefit from this section, a good, in depth understanding of the C# programming language and a C# IDE is required.

The Hassium interpreter comes with a wide variety of builtin modules for completing a wide variety of tasks, but the reality is that this might not always be enough. To remedy this, the programmer can write their own modules in C#. To demonstrate this, we will write our own module with an example static class and an example OOP class. Since this section is advanced, we will move quickly through this subject.

Open your C# IDE and choose a New Project, for project type choose Class Library or similar. Name this project TestModule. Once the project has been created goto Refrences->Add Reference. Navigate to the Hassium interpreter, Hassium.exe and select this as a reference.

Now add a new C# class to the project called HassiumTestModule.cs.

HassiumTestModule.cs:

using Hassium.Runtime.Objects;
using Hassium.Runtime.Objects.Types;

namespace TestModule
{
    public class HassiumTestModule : InternalModule
    {
        public HassiumTestModule() : base("TestModule")
        {
            AddAttribute("OOPClass", new HassiumOOPClass());
            AddAttribute("StaticClass", new HassiumStaticClass());
        }
    }
}

This is the main module file. Now we will build the OOPClass and StaticClass.

HassiumOOPClass.cs:

using Hassium.Runtime;
using Hassium.Runtime.Objects;
using Hassium.Runtime.Objects.Types;

namespace TestModule
{
    public class HassiumOOPClass : HassiumObject
    {
        public static HassiumTypeDefinition TypeDefinition = new HassiumTypeDefinition("OOPClass");
        public HassiumInt NumberOne { get; set; }
        public HassiumInt NumberTwo { get; set; }
        public HassiumOOPClass()
        {
            AddAttribute(HassiumObject.INVOKE, _new);
        }
        public HassiumOOPClass _new(VirtualMachine vm, params HassiumObject[] args)
        {
            HassiumOOPClass oopClass = new HassiumOOPClass();
            oopClass.AddType(TypeDefinition);
            oopClass.NumberOne = args[0].ToInt(vm);
            oopClass.NumberTwo = args[0].ToInt(vm);
            oopClass.AddAttribute("add", oopClass.add, 0, 1);
            return oopClass;
        }
        public HassiumInt add(VirtualMachine vm, params HassiumObject[] args)
        {
            if (args.Length == 1)
                return new HassiumInt(NumberOne.Int + NumberTwo.Int + args[0].ToInt(vm).Int);
            return new HassiumInt(NumberOne.Int + NumberTwo.Int);
        }
    }
}

HassiumStaticClass.cs:

using System;
using Hassium.Runtime;
using Hassium.Runtime.Objects;
using Hassium.Runtime.Objects.Types;

namespace TestModule
{
    public class HassiumStaticClass: HassiumObject
    {
        public HassiumStaticClass()
        {
            AddAttribute("sayHello", sayHello, 0);
        }
        public HassiumNull sayHello(VirtualMachine vm, params HassiumObject[] args)
        {
            Console.WriteLine("Hello, World! From TestModule.StaticClass.sayHello");

            return HassiumObject.Null;
        }
    }
}

Compile this library using the green "Play" button, or by selecting the "Compile" option. The DLL should output to TestModule\TestModule\bin\Debug\TestModule.dll. Move this DLL into your home directory or the directory your Hassium programs are located.

Now we can import this DLL into our Hassium code and use OOPClass and StaticClass.

use TestModule;

func main () {
    StaticClass.stayHello ();
    OOPClass clazz = new OOPClass (4, 8);
    println (clazz.add ()); # prints 12
    println (clazz.add (2)); # prints 14
}

Output:

Hello, World! From TestModule.StaticClass.sayHello
12
14

8.3 Builtin Modules

The Hassium interpreter comes with several builtin modules, called the standard library (builtin classes and functions for doing advanced tasks). The builtin modules are named Drawing, IO, Math, Net, Reflection, Text, and Util. In Chapter 9 we will go over some of the features of the Math, IO, Net, Text, and Util modules.

Chapter 8 - Exercises

Exercise 8.1

Write a simple C# module that includes methods for writing to the screen in color. Import this module and test it.

Exercise 8.2

Write a module in Hassium that contains methods for writing to the screen in color. Import this module and test it.

results matching ""

    No results matching ""