1. advanced type manager

So, I've got some structures I need to use for type checking, and I wrote a little library for it. Is there anything like this already available in Euphoria core?

This was developed using Phix, with an attempt to remain compatible with Euphoria. The example shows the utility of Phix's "string" type vs Euphoria's plain sequence (even using Euphoria's string() type). Whereas Euphoria doesn't distinguish between "Hello" and {1972,12,18}, Phix does.

Any improvements and suggestions are welcome!

I also created a GitHub Repo for it! Woo hoo!

-- a type template manager 
 
namespace typer 
 
public enum YES, NO 
 
sequence types = {{},{}} 
 
function float(object o) 
    return atom(o) and not integer(o) 
end function 
 
function seq_of_atoms(object a) 
    if sequence(a) then 
        for t=1 to length(a) do 
            if sequence(a[t]) then 
                return false 
            end if 
        end for 
    else 
        return false 
    end if 
    return true 
end function 
 
public procedure add_template(string tname, object tmp) 
integer i = find(tname,types[1]) 
    if i > 0 then 
        types[2][i] = tmp 
    else 
        types[1] = append(types[1],tname) 
        types[2] = append(types[2],tmp) 
    end if 
end procedure 
 
integer Verbose = 0 
public procedure verbose(integer i) 
    Verbose = i 
end procedure 
 
function hasSameStructure(object a, object b) 
    if Verbose = YES then 
        ?{a,b} 
    end if 
    if integer(a) and integer(b) then 
        return true 
    elsif atom(a) and atom(b) then 
        if not integer(a) and not integer(b) then 
            return true 
        end if 
    elsif sequence(a) and sequence(b) then 
--      if string(a) and string(b) then -- Phix catches strings better than Euphoria 
        if seq_of_atoms(a) and seq_of_atoms(b) then 
            return true 
        else 
            if length(a) != length(b) then 
                return false 
            end if 
            for t=1 to length(a) do 
                if not hasSameStructure(a[t],b[t]) then 
                    return false 
                end if 
            end for 
            return true 
        end if 
    end if 
    return false 
end function 
 
public function is(string tname, object var) 
integer i = find(tname,types[1]) 
    if i > 0 then 
        object this = types[2][i] 
        return hasSameStructure(var,this) 
    end if 
    return false 
end function 
include typer.e 
 
type record(object a) 
    return is("record",a) 
end type 
 
type float(object a) 
    return is("float",a) 
end type 
 
procedure test() 
    verbose(YES)  -- just to watch the testing 
     
    add_template("integer",1) 
    add_template("float",1.1) 
     
    ?is("integer",1) 
    ?is("integer",1.1) 
    ?is("float",1) 
    ?is("float",1.1) 
 
    add_template("record",{"","",""}) 
 
    ?is("record",1) 
    ?is("record",1.1) 
    ?is("record",{}) 
    ?is("record",{"",""}) 
    ?is("record",{1,2,3}) 
    ?is("record",{"","",""}) 
    ?is("record",{"a","b","c"}) 
    ?is("record",{"","",{{},{}}}) 
    ?is("record",{{{},{}},"",""}) 
    ?is("record",{"",{{},{}},""}) 
    ?is("record",{"Bobby","Jones","Ranger"}) 
 
    -- this last test reveals what happens when you don't have a proper string() type 
    ?is("record",{"Bobby","Jones",{1972,12,25}}) 
     
    record a 
    a = {"Bobby","Jones","Ranger"} 
     
    float b 
    b = 0.2 
 
    -- now we'll crash 
    a = "seven" 
     
end procedure 
 
test() 
maybe_any_key() 
 
new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu