diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs | 376 |
1 files changed, 188 insertions, 188 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs index 3ba1021..58c0e4b 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Functor.cs | |||
@@ -1,188 +1,188 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2007-2008, Jeff Thompson | 2 | * Copyright (C) 2007-2008, Jeff Thompson |
3 | * | 3 | * |
4 | * All rights reserved. | 4 | * All rights reserved. |
5 | * | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are met: | 7 | * modification, are permitted provided that the following conditions are met: |
8 | * | 8 | * |
9 | * * Redistributions of source code must retain the above copyright | 9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above copyright | 11 | * * Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
14 | * * Neither the name of the copyright holder nor the names of its contributors | 14 | * * Neither the name of the copyright holder nor the names of its contributors |
15 | * may be used to endorse or promote products derived from this software | 15 | * may be used to endorse or promote products derived from this software |
16 | * without specific prior written permission. | 16 | * without specific prior written permission. |
17 | * | 17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ | 29 | */ |
30 | 30 | ||
31 | using System; | 31 | using System; |
32 | using System.Collections.Generic; | 32 | using System.Collections.Generic; |
33 | 33 | ||
34 | namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.YieldProlog | 34 | namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.YieldProlog |
35 | { | 35 | { |
36 | public class Functor : IUnifiable | 36 | public class Functor : IUnifiable |
37 | { | 37 | { |
38 | public readonly Atom _name; | 38 | public readonly Atom _name; |
39 | public readonly object[] _args; | 39 | public readonly object[] _args; |
40 | 40 | ||
41 | public Functor(Atom name, object[] args) | 41 | public Functor(Atom name, object[] args) |
42 | { | 42 | { |
43 | if (args.Length <= 3) | 43 | if (args.Length <= 3) |
44 | { | 44 | { |
45 | if (args.Length == 0) | 45 | if (args.Length == 0) |
46 | throw new Exception("For arity 0 functor, just use name as an Atom"); | 46 | throw new Exception("For arity 0 functor, just use name as an Atom"); |
47 | else if (args.Length == 1) | 47 | else if (args.Length == 1) |
48 | throw new Exception("For arity 1 functor, use Functor1"); | 48 | throw new Exception("For arity 1 functor, use Functor1"); |
49 | else if (args.Length == 2) | 49 | else if (args.Length == 2) |
50 | throw new Exception("For arity 2 functor, use Functor2"); | 50 | throw new Exception("For arity 2 functor, use Functor2"); |
51 | else if (args.Length == 3) | 51 | else if (args.Length == 3) |
52 | throw new Exception("For arity 3 functor, use Functor3"); | 52 | throw new Exception("For arity 3 functor, use Functor3"); |
53 | else | 53 | else |
54 | // (This shouldn't happen, but include it for completeness. | 54 | // (This shouldn't happen, but include it for completeness. |
55 | throw new Exception("Cannot create a Functor of arity " + args.Length); | 55 | throw new Exception("Cannot create a Functor of arity " + args.Length); |
56 | } | 56 | } |
57 | 57 | ||
58 | _name = name; | 58 | _name = name; |
59 | _args = args; | 59 | _args = args; |
60 | } | 60 | } |
61 | 61 | ||
62 | public Functor(string name, object[] args) | 62 | public Functor(string name, object[] args) |
63 | : this(Atom.a(name), args) | 63 | : this(Atom.a(name), args) |
64 | { | 64 | { |
65 | } | 65 | } |
66 | 66 | ||
67 | /// <summary> | 67 | /// <summary> |
68 | /// Return an Atom, Functor1, Functor2, Functor3 or Functor depending on the | 68 | /// Return an Atom, Functor1, Functor2, Functor3 or Functor depending on the |
69 | /// length of args. | 69 | /// length of args. |
70 | /// Note that this is different than the Functor constructor which requires | 70 | /// Note that this is different than the Functor constructor which requires |
71 | /// the length of args to be greater than 3. | 71 | /// the length of args to be greater than 3. |
72 | /// </summary> | 72 | /// </summary> |
73 | /// <param name="name"></param> | 73 | /// <param name="name"></param> |
74 | /// <param name="args"></param> | 74 | /// <param name="args"></param> |
75 | /// <returns></returns> | 75 | /// <returns></returns> |
76 | public static object make(Atom name, object[] args) | 76 | public static object make(Atom name, object[] args) |
77 | { | 77 | { |
78 | if (args.Length <= 0) | 78 | if (args.Length <= 0) |
79 | return name; | 79 | return name; |
80 | else if (args.Length == 1) | 80 | else if (args.Length == 1) |
81 | return new Functor1(name, args[0]); | 81 | return new Functor1(name, args[0]); |
82 | else if (args.Length == 2) | 82 | else if (args.Length == 2) |
83 | return new Functor2(name, args[0], args[1]); | 83 | return new Functor2(name, args[0], args[1]); |
84 | else if (args.Length == 3) | 84 | else if (args.Length == 3) |
85 | return new Functor3(name, args[0], args[1], args[2]); | 85 | return new Functor3(name, args[0], args[1], args[2]); |
86 | else | 86 | else |
87 | return new Functor(name, args); | 87 | return new Functor(name, args); |
88 | } | 88 | } |
89 | 89 | ||
90 | /// <summary> | 90 | /// <summary> |
91 | /// Call the main make, first converting name to an Atom. | 91 | /// Call the main make, first converting name to an Atom. |
92 | /// </summary> | 92 | /// </summary> |
93 | /// <param name="name"></param> | 93 | /// <param name="name"></param> |
94 | /// <param name="args"></param> | 94 | /// <param name="args"></param> |
95 | /// <returns></returns> | 95 | /// <returns></returns> |
96 | public static object make(string name, object[] args) | 96 | public static object make(string name, object[] args) |
97 | { | 97 | { |
98 | return make(Atom.a(name), args); | 98 | return make(Atom.a(name), args); |
99 | } | 99 | } |
100 | 100 | ||
101 | public IEnumerable<bool> unify(object arg) | 101 | public IEnumerable<bool> unify(object arg) |
102 | { | 102 | { |
103 | arg = YP.getValue(arg); | 103 | arg = YP.getValue(arg); |
104 | if (arg is Functor) | 104 | if (arg is Functor) |
105 | { | 105 | { |
106 | Functor argFunctor = (Functor)arg; | 106 | Functor argFunctor = (Functor)arg; |
107 | if (_name.Equals(argFunctor._name)) | 107 | if (_name.Equals(argFunctor._name)) |
108 | return YP.unifyArrays(_args, argFunctor._args); | 108 | return YP.unifyArrays(_args, argFunctor._args); |
109 | else | 109 | else |
110 | return YP.fail(); | 110 | return YP.fail(); |
111 | } | 111 | } |
112 | else if (arg is Variable) | 112 | else if (arg is Variable) |
113 | return ((Variable)arg).unify(this); | 113 | return ((Variable)arg).unify(this); |
114 | else | 114 | else |
115 | return YP.fail(); | 115 | return YP.fail(); |
116 | } | 116 | } |
117 | 117 | ||
118 | public override string ToString() | 118 | public override string ToString() |
119 | { | 119 | { |
120 | string result = _name + "(" + YP.getValue(_args[0]); | 120 | string result = _name + "(" + YP.getValue(_args[0]); |
121 | for (int i = 1; i < _args.Length; ++i) | 121 | for (int i = 1; i < _args.Length; ++i) |
122 | result += ", " + YP.getValue(_args[i]); | 122 | result += ", " + YP.getValue(_args[i]); |
123 | result += ")"; | 123 | result += ")"; |
124 | return result; | 124 | return result; |
125 | } | 125 | } |
126 | 126 | ||
127 | public bool termEqual(object term) | 127 | public bool termEqual(object term) |
128 | { | 128 | { |
129 | term = YP.getValue(term); | 129 | term = YP.getValue(term); |
130 | if (term is Functor) | 130 | if (term is Functor) |
131 | { | 131 | { |
132 | Functor termFunctor = (Functor)term; | 132 | Functor termFunctor = (Functor)term; |
133 | if (_name.Equals(termFunctor._name) && _args.Length == termFunctor._args.Length) | 133 | if (_name.Equals(termFunctor._name) && _args.Length == termFunctor._args.Length) |
134 | { | 134 | { |
135 | for (int i = 0; i < _args.Length; ++i) | 135 | for (int i = 0; i < _args.Length; ++i) |
136 | { | 136 | { |
137 | if (!YP.termEqual(_args[i], termFunctor._args[i])) | 137 | if (!YP.termEqual(_args[i], termFunctor._args[i])) |
138 | return false; | 138 | return false; |
139 | } | 139 | } |
140 | return true; | 140 | return true; |
141 | } | 141 | } |
142 | } | 142 | } |
143 | return false; | 143 | return false; |
144 | } | 144 | } |
145 | 145 | ||
146 | public bool lessThan(Functor functor) | 146 | public bool lessThan(Functor functor) |
147 | { | 147 | { |
148 | // Do the equal check first since it is faster. | 148 | // Do the equal check first since it is faster. |
149 | if (!_name.Equals(functor._name)) | 149 | if (!_name.Equals(functor._name)) |
150 | return _name.lessThan(functor._name); | 150 | return _name.lessThan(functor._name); |
151 | 151 | ||
152 | if (_args.Length != functor._args.Length) | 152 | if (_args.Length != functor._args.Length) |
153 | return _args.Length < functor._args.Length; | 153 | return _args.Length < functor._args.Length; |
154 | 154 | ||
155 | for (int i = 0; i < _args.Length; ++i) | 155 | for (int i = 0; i < _args.Length; ++i) |
156 | { | 156 | { |
157 | if (!YP.termEqual(_args[i], functor._args[i])) | 157 | if (!YP.termEqual(_args[i], functor._args[i])) |
158 | return YP.termLessThan(_args[i], functor._args[i]); | 158 | return YP.termLessThan(_args[i], functor._args[i]); |
159 | } | 159 | } |
160 | 160 | ||
161 | return false; | 161 | return false; |
162 | } | 162 | } |
163 | 163 | ||
164 | public bool ground() | 164 | public bool ground() |
165 | { | 165 | { |
166 | for (int i = 0; i < _args.Length; ++i) | 166 | for (int i = 0; i < _args.Length; ++i) |
167 | { | 167 | { |
168 | if (!YP.ground(_args[i])) | 168 | if (!YP.ground(_args[i])) |
169 | return false; | 169 | return false; |
170 | } | 170 | } |
171 | return true; | 171 | return true; |
172 | } | 172 | } |
173 | 173 | ||
174 | public void addUniqueVariables(List<Variable> variableSet) | 174 | public void addUniqueVariables(List<Variable> variableSet) |
175 | { | 175 | { |
176 | for (int i = 0; i < _args.Length; ++i) | 176 | for (int i = 0; i < _args.Length; ++i) |
177 | YP.addUniqueVariables(_args[i], variableSet); | 177 | YP.addUniqueVariables(_args[i], variableSet); |
178 | } | 178 | } |
179 | 179 | ||
180 | public object makeCopy(Variable.CopyStore copyStore) | 180 | public object makeCopy(Variable.CopyStore copyStore) |
181 | { | 181 | { |
182 | object[] argsCopy = new object[_args.Length]; | 182 | object[] argsCopy = new object[_args.Length]; |
183 | for (int i = 0; i < _args.Length; ++i) | 183 | for (int i = 0; i < _args.Length; ++i) |
184 | argsCopy[i] = YP.makeCopy(_args[i], copyStore); | 184 | argsCopy[i] = YP.makeCopy(_args[i], copyStore); |
185 | return new Functor(_name, argsCopy); | 185 | return new Functor(_name, argsCopy); |
186 | } | 186 | } |
187 | } | 187 | } |
188 | } | 188 | } |