9/10/2011

pseudo objects in alpha-3

Object-oriented programming is an exceptionally bad idea which could only have originated in California.
Edsger Dijkstra

Today, I realized that some of the new features that alpha-3 offers I would be able to create some sort of objects. Note that I did not intentionally add any support for OOP. I like to call this sort of objects pseudo objects and the classes that define them pseudo classes. Why? Because you can't really call them real objects.

Take a look at the following script: (if you are interested at all)

_inc("&Person.tbs");
?test = !new Person(~"Test", 14);
!out(~!test.getPersName(), ~" is ", ~!str(!test.getPersAge()), ~" years old. \n");
!test.setPersAge(15);
!out(~!test.getPersName(), ~" is now ", ~!str(!test.getPersAge()), ~" years old. \n");
_die();
The file Person.tbs contains a pseudo class. In fact, the first one ever created.
This is the file(note that it's highly unlikely that you can understand all of the code because it uses some alpha-3 features like globvar and upvar.
@ This is a Person pseudo class written by Test;
!out(~"Don't run this, it's only made to include!\n");
_die();
include_begin;

@ class_info_begin
Variables:
* [0] Name : string
* [1] Age : number
Subroutines:
* person.getPersName() : string
* person.getPersAge() : number
@ class_info_end;

?Person::objects;

#newPerson(?name, ?age) {
    ?object;
    object[] =~ name;
    object[] = age;
    ?person_list => !globvar(~"Person::objects");
    person_list[] => object;
    !setglobvar(~"Person::objects", >person_list);
    :   !sizeof(>person_list) - 1;
};

#copyPerson(?person) {
    ?person_list => !globvar(~"Person::objects");
    ?old_person => person_list[person];
    ?new_person;
    new_person[] =~ old_person[0];
    new_person[] = old_person[1];
    person_list[] => new_person;
    !setglobvar(~"Person::objects", >person_list);
    :   !sizeof(>person_list) - 1;
};

#Person::getVar(?pers_id, ?var_id, ?type) {
    ?person_list => !globvar(~"Person::objects");
    ?person => person_list[pers_id];
    _if(type == 0) {
        :   person[var_id];
    } _elseif(type == 1) {
        :~  person[var_id];
    } _elseif(type == 2) {
        :|  person[var_id];
    } _elseif(type == 3) {
        :>  person[var_id];
    } _else {
        !out(~"ERROR: Invalid type for Person::getVar");
        :   0;
    }
};

#Person::setVar(?pers_id, ?var_id, ?var_value) {
    ?person_list => !globvar(~"Person::objects");
    ?person => person_list[pers_id];
    person[var_id] = var_value;
    person[var_id] =~ var_value;
    person[var_id] =| [var_value];
    person[var_id] => var_value;
    person_list[pers_id] => person;
    !setglobvar(~"Person::objects", >person_list);
    :   0;
};

#person.getPersName() {
    ?id = !upvar(~!globvar(~"SUB_LAST_SUBJ"));
    :~  !Person::getVar(id, 0, 1);
};

#person.getPersAge() {
    ?id = !upvar(~!globvar(~"SUB_LAST_SUBJ"));
    :  !Person::getVar(id, 1, 0);
};

#person.setPersName(?name) {
    ?id = !upvar(~!globvar(~"SUB_LAST_SUBJ"));
    :~  !Person::setVar(id, 0, ~name);
};

#person.setPersAge(?age) {
    ?id = !upvar(~!globvar(~"SUB_LAST_SUBJ"));
    :  !Person::setVar(id, 1, age);
};
include_end;
As you may or may not see every object is stored as an array which is stored inside an array that contains all of the objects. I use :: in the names of things that should be private. The variable that you get in your script is actually an integer: the index number of the "object" in the array of objects.
What does this mean for TBS?
Not much, the STDL will have a number of files that contain pseudo classes(perhaps a Vector class?) but most of the STDL will remain non-object oriented.

No comments:

Post a Comment