structures
- Posted by Chris Bensler <bensler at telus.net> Aug 13, 2001
- 462 views
Hi all, After commenting on the type catsing, it gave me and idea. I developed a structure sytem. It seems to work pretty well. You can't have dynamic structures though. I think it could be worked out, but it would make the type casting quite a bite messier though. You would probably have to create a type for each element of the structure. It also handles binary enumerations for flag values. here is the code.. please let me know if you find any flaws in it. Chris <CODE> sequence the_struct the_struct={} integer enum_count enum_count=0 integer enum_mode enum_mode=1 global type structure(sequence s) if length(s)<1 then return 0 end if the_struct=call_func(s[1],{s[2..length(s)]}) return length(the_struct)!=0 end type global function new_structure(integer struct_type) if structure({struct_type}) then end if return {struct_type}&the_struct end function global function enum(integer mode) -- mode =-1 : numeric reset -- mode =-2 : binary reset -- mode =2 : binary enumeration -- mode =1 : numeric enumeration -- connot mix numeric and binary enumerations -- mode =0 : reset to 1 -- Should NOT be used for structures.. -- Structures use the first element for internal info -- enumerations start at two, not 1 if enum_count=0 or (enum_mode>0 and mode!=enum_mode) then mode*=-1 end if enum_mode=mode if mode=-1 then enum_count=2 -- numeric reset elsif mode=-2 then enum_count=1 -- binary reset elsif mode=2 then enum_count*=2 -- binary enumeration else enum_count+=1 -- numeric enumeration end if return enum_count end function -- EXAMPLE STARTS HERE function data_struct_type(sequence s) -- this call is nesseccary to create a skeleton structure if length(s)=0 then s={0,{0,0},0} end if if length(s)!=3 or not atom(s[1]) or not sequence(s[2]) or length(s[2])!=2 or not atom(s[2][1]) or not atom(s[2][2]) or not atom(s[3]) then return {} -- return an empty sequence on failure end if return s -- return the structure on success end function constant data_struct=routine_id("data_struct_type"), -- ^ this is stored into the first element of the structure -- structure enumerations start at '2' NOT '1' one=enum(1), two=enum(1), three=enum(1) structure data data=new_structure(data_struct) ? data ? data[one] -- element 2 ? data[two] -- 3 ? data[three] -- 4 while get_key()=-1 do end while <END OF CODE>