Friday, October 16, 2009

Structures in Yabasic 3

Often, it is desirable to have variables that can contain data of mixed types (real numbers and strings). For example, take a student database (a typical example, I know, but sufficient). The name, gender, and date of birth of each student must be stored, as well as course information for each individual. To accomplish this in Yabasic as it stands, about six array variables are required. (Technically, fewer are necessary if clarity is unimportant.)

In Yabasic 3, structures will be able to contain any type of data: real numbers, strings, and even other structures. This will allow for better organisation of variables and a more consistent approach to various aspects of Yabasic programming.

Take the aforementioned example, for instance. In Yabasic as it currently stands, an implementation of the student database might be as follows:

number_of_students = 30
number_of_courses = 5

dim name$ (number_of_students)
dim gender (number_of_students)
dim dob_year (number_of_students)
dim dob_month (number_of_students)
dim dob_day (number_of_students)
dim course_name$ (number_of_students, number_of_courses)
dim course_teacher$ (number_of_students, number_of_courses)

name$ (1) = "Amy"
gender (1) = female
dob_year (1) = 1989
dob_month (1) = 5
dob_day (1) = 14
course_name$ (1, 1) = "Mathematics"
course_teacher$ (1, 1) = "Pythagoras"
course_name$ (1, 2) = "Physics"
course_teacher$ (1, 2) = "Einstein"
...

Clearly, this has the potential to become extremely confusing, particularly when used in complicated Yabasic programs. In Yabasic 3, on the other hand, the same database program might conceptually be implemented as follows:

number_of_students = 30
number_of_courses = 5

struct students (number_of_students)
name$
gender
struct dob
year
month
day
end struct
struct courses (number_of_courses)
name$
teacher$
end struct
end struct

students (1).name$ = "Amy"
students (1).gender = female
students (1).dob.year = 1989
students (1).dob.month = 5
students (1).dob.day = 14
students (1).courses (1).name$ = "Mathematics"
students (1).courses (1).teacher$ = "Pythagoras"
students (1).courses (2).name$ = "Physics"
students (1).courses (2).teacher$ = "Einstein"
...

Compare this with the previous example. It is more readable and logical, and easier to understand. If you are not convinced, imagine a complex database program making use of many data fields. Without structures, the risk of errors would be unacceptably high.

Structures are not overly difficult to implement, although there may initially be a few hiccups. Instead of making complicated changes to the existing variable-handling code in Yabasic, a special "rearranging" trick could be used. Array indices could be brought to the right-hand side of the full structure name for the purpose of internal representation, allowing structures to make use of existing array and scalar handling routines in Yabasic: for example, students (1).name$ could become students.name$ (1). The struct statement would, therefore, handle all necessary "dimensioning" of arrays, and, if appropriate, make variables local or static. Later, it will probably become possible for users to define types, structure templates. Tentatively, their implementation might be as follows (but I am very open to suggestions):
type person
name$
gender
struct dob
year
month
day
end struct
end type

number_of_people = 30

struct people (number_of_people) as person

people (1).name$ = "John"
people (1).gender = male
people (1).dob.year = 1987
people (1).dob.month = 11
people (1).dob.day = 5
...
I expect to implement structures in time for the Yabasic 3 beta release, but types may come a little later, since I need more feedback on the basic design before I extend it significantly.

3 comments:

  1. Thumbs up. Looks good to me. (I hope it won't slow down the interpreter too much when processing structs with the "array trick"?)

    Will it be possible to assign one struct to another (eg.

    struct people(30) as person
    struct teacher as person
    people(0)= teacher ?)

    Cheers,

    syzygy

    ReplyDelete
  2. Hi,
    will you rewrote yabasic ore add you new methodes?

    The struct and type are good things.

    Ralf Schülke

    ReplyDelete
  3. @Syzygy: Probably, although I'll need to consider the implementation before I can give a definitive answer.

    ReplyDelete

Note: Only a member of this blog may post a comment.