1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
-- Convenience constructors of OpenMath elements
-- These are then used in base.m2.
-- OK OMA (apply)
-- OMATP (attribute pair)
-- OMATTR (attributes)
-- OMB (Binary data)
-- OK OMBIND (Binding)
-- OK OMBVAR (Binding variables)
-- OK OME (Error)
-- OK OMF (Float)
-- OMFOREIGN (Foreign XML)
-- OK OMI (Integers)
-- OK OMOBJ ("Object": the root node)
-- OK OMR (References)
-- OK OMSTR (String)
-- OK OMS (Symbol)
-- OK OMV (Variable)
--- Constructing OpenMath via XMLnode's ---
OMI = method()
OMI String := x -> new XMLnode from {symbol tag => "OMI", children => {x} }
OMI ZZ := x -> OMI(toString x)
OMSTR = method()
OMSTR String := x -> new XMLnode from {symbol tag => "OMSTR", children => {x} }
OMS = method()
OMS (String, String) := (x,y) -> new XMLnode from {symbol tag => "OMS", "cd" => x, "name" => y}
OMA = method()
OMA (XMLnode, List) := (s, l) -> (
if #l > 0 and class(l#0) =!= XMLnode then l = apply(l, toOpenMath);
new XMLnode from {symbol tag => "OMA", children => prepend(s, l) }
)
OMA (String, String, List) := (a,b,l) -> OMA(OMS(a,b), l)
OME = method()
OME (XMLnode, List) := (s, l) -> (
if #l > 0 and class(l#0) =!= XMLnode then l = apply(l, toOpenMath);
new XMLnode from {symbol tag => "OME", children => prepend(s, l) }
)
OME (String) := s -> OME(OMS("scscp1", "error_system_specific"), {OMSTR(s)});
OMV = method()
OMV (String) := x -> new XMLnode from {symbol tag => "OMV", "name" => x}
OMR = method()
OMR (String) := x -> new XMLnode from {symbol tag => "OMR", "href" => x}
OMOBJ = method()
OMOBJ XMLnode := x -> if x.tag === "OMOBJ" then x else new XMLnode from {symbol tag => "OMOBJ", children => {x}}
deOMOBJ = method()
deOMOBJ XMLnode := x -> if x.tag === "OMOBJ" then (x.children)#0 else x
OMF = method()
OMF String := x -> new XMLnode from {symbol tag => "OMF", "dec" => x}
OMBVAR = method()
OMBVAR List := x-> new XMLnode from {symbol tag => "OMBVAR", symbol children => apply(x, toOpenMath) };
OMBIND = method()
OMBIND (XMLnode, List, List) := (hd, vars, exprs) -> (
vvars := OMBVAR(vars);
new XMLnode from {
symbol tag => "OMBIND",
symbol children => prepend(hd, prepend(vvars, exprs))
}
)
OMBIND (String, String, List, List) := (cd, name, vars, exprs) -> OMBIND(OMS(cd, name), vars, exprs);
OMBIND (String, String, List, XMLnode) := (cd, name, vars, expr) -> OMBIND(OMS(cd, name), vars, {expr});
OMBIND (XMLnode, List, XMLnode) := (hd, vars, expr) -> OMBIND(hd, vars, {expr});
OMATTR = method()
OMATTR (XMLnode, HashTable) := (child, attrs) -> (
--construct the key,value,key,value, ... that will appear inside the OMATP
attrsflat := {};
for k in values(attrs) do (
attrsflat = join(attrsflat, {k#0, k#1})
);
--clean out the attributes from the child
clearOMAttr(child);
--construct the OMATTR
new XMLnode from {
symbol tag => "OMATTR",
symbol children => {
new XMLnode from {
symbol tag => "OMATP",
symbol children => attrsflat
},
child
}
}
)
--- And some helper functions ---
isOMAOf = (x, cd, nm) -> (
if not class(x) === XMLnode then return false;
if x.tag =!= "OMA" then return false;
if #(x.children) === 0 then return false;
if ((x.children)#0).tag =!= "OMS" then return false;
if ((x.children)#0)#"cd" =!= cd then return false;
if ((x.children)#0)#"name" =!= nm then return false;
return true
)
|