aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs436
1 files changed, 218 insertions, 218 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs
index 9b4ca6a..7387ef4 100644
--- a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs
+++ b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/YieldProlog/Atom.cs
@@ -1,218 +1,218 @@
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
31using System; 31using System;
32using System.Collections.Generic; 32using System.Collections.Generic;
33using System.Text; 33using System.Text;
34 34
35namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.YieldProlog 35namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.YieldProlog
36{ 36{
37 public class Atom : IUnifiable 37 public class Atom : IUnifiable
38 { 38 {
39 private static Dictionary<string, Atom> _atomStore = new Dictionary<string, Atom>(); 39 private static Dictionary<string, Atom> _atomStore = new Dictionary<string, Atom>();
40 public readonly string _name; 40 public readonly string _name;
41 public readonly Atom _module; 41 public readonly Atom _module;
42 42
43 /// <summary> 43 /// <summary>
44 /// You should not call this constructor, but use Atom.a instead. 44 /// You should not call this constructor, but use Atom.a instead.
45 /// </summary> 45 /// </summary>
46 /// <param name="name"></param> 46 /// <param name="name"></param>
47 /// <param name="module"></param> 47 /// <param name="module"></param>
48 private Atom(string name, Atom module) 48 private Atom(string name, Atom module)
49 { 49 {
50 _name = name; 50 _name = name;
51 _module = module; 51 _module = module;
52 } 52 }
53 53
54 /// <summary> 54 /// <summary>
55 /// Return the unique Atom object for name where module is null. You should use this to create 55 /// Return the unique Atom object for name where module is null. You should use this to create
56 /// an Atom instead of calling the Atom constructor. 56 /// an Atom instead of calling the Atom constructor.
57 /// </summary> 57 /// </summary>
58 /// <param name="name"></param> 58 /// <param name="name"></param>
59 /// <returns></returns> 59 /// <returns></returns>
60 public static Atom a(string name) 60 public static Atom a(string name)
61 { 61 {
62 Atom atom; 62 Atom atom;
63 if (!_atomStore.TryGetValue(name, out atom)) 63 if (!_atomStore.TryGetValue(name, out atom))
64 { 64 {
65 atom = new Atom(name, null); 65 atom = new Atom(name, null);
66 _atomStore[name] = atom; 66 _atomStore[name] = atom;
67 } 67 }
68 return atom; 68 return atom;
69 } 69 }
70 70
71 /// <summary> 71 /// <summary>
72 /// Return an Atom object with the name and module. If module is null or Atom.NIL, 72 /// Return an Atom object with the name and module. If module is null or Atom.NIL,
73 /// this behaves like Atom.a(name) and returns the unique object where the module is null. 73 /// this behaves like Atom.a(name) and returns the unique object where the module is null.
74 /// If module is not null or Atom.NIL, this may or may not be the same object as another Atom 74 /// If module is not null or Atom.NIL, this may or may not be the same object as another Atom
75 /// with the same name and module. 75 /// with the same name and module.
76 /// </summary> 76 /// </summary>
77 /// <param name="name"></param> 77 /// <param name="name"></param>
78 /// <param name="module"></param> 78 /// <param name="module"></param>
79 /// <returns></returns> 79 /// <returns></returns>
80 public static Atom a(string name, Atom module) 80 public static Atom a(string name, Atom module)
81 { 81 {
82 if (module == null || module == Atom.NIL) 82 if (module == null || module == Atom.NIL)
83 return a(name); 83 return a(name);
84 return new Atom(name, module); 84 return new Atom(name, module);
85 } 85 }
86 86
87 /// <summary> 87 /// <summary>
88 /// If Obj is an Atom unify its _module with Module. If the Atom's _module is null, use Atom.NIL. 88 /// If Obj is an Atom unify its _module with Module. If the Atom's _module is null, use Atom.NIL.
89 /// </summary> 89 /// </summary>
90 /// <param name="Atom"></param> 90 /// <param name="Atom"></param>
91 /// <param name="Module"></param> 91 /// <param name="Module"></param>
92 /// <returns></returns> 92 /// <returns></returns>
93 public static IEnumerable<bool> module(object Obj, object Module) 93 public static IEnumerable<bool> module(object Obj, object Module)
94 { 94 {
95 Obj = YP.getValue(Obj); 95 Obj = YP.getValue(Obj);
96 if (Obj is Atom) 96 if (Obj is Atom)
97 { 97 {
98 if (((Atom)Obj)._module == null) 98 if (((Atom)Obj)._module == null)
99 return YP.unify(Module, Atom.NIL); 99 return YP.unify(Module, Atom.NIL);
100 else 100 else
101 return YP.unify(Module, ((Atom)Obj)._module); 101 return YP.unify(Module, ((Atom)Obj)._module);
102 } 102 }
103 return YP.fail(); 103 return YP.fail();
104 } 104 }
105 105
106 public static readonly Atom NIL = Atom.a("[]"); 106 public static readonly Atom NIL = Atom.a("[]");
107 public static readonly Atom DOT = Atom.a("."); 107 public static readonly Atom DOT = Atom.a(".");
108 public static readonly Atom F = Atom.a("f"); 108 public static readonly Atom F = Atom.a("f");
109 public static readonly Atom SLASH = Atom.a("/"); 109 public static readonly Atom SLASH = Atom.a("/");
110 public static readonly Atom HAT = Atom.a("^"); 110 public static readonly Atom HAT = Atom.a("^");
111 public static readonly Atom RULE = Atom.a(":-"); 111 public static readonly Atom RULE = Atom.a(":-");
112 112
113 public IEnumerable<bool> unify(object arg) 113 public IEnumerable<bool> unify(object arg)
114 { 114 {
115 arg = YP.getValue(arg); 115 arg = YP.getValue(arg);
116 if (arg is Atom) 116 if (arg is Atom)
117 return Equals(arg) ? YP.succeed() : YP.fail(); 117 return Equals(arg) ? YP.succeed() : YP.fail();
118 else if (arg is Variable) 118 else if (arg is Variable)
119 return ((Variable)arg).unify(this); 119 return ((Variable)arg).unify(this);
120 else 120 else
121 return YP.fail(); 121 return YP.fail();
122 } 122 }
123 123
124 public void addUniqueVariables(List<Variable> variableSet) 124 public void addUniqueVariables(List<Variable> variableSet)
125 { 125 {
126 // Atom does not contain variables. 126 // Atom does not contain variables.
127 } 127 }
128 128
129 public object makeCopy(Variable.CopyStore copyStore) 129 public object makeCopy(Variable.CopyStore copyStore)
130 { 130 {
131 // Atom does not contain variables that need to be copied. 131 // Atom does not contain variables that need to be copied.
132 return this; 132 return this;
133 } 133 }
134 134
135 public bool termEqual(object term) 135 public bool termEqual(object term)
136 { 136 {
137 return Equals(YP.getValue(term)); 137 return Equals(YP.getValue(term));
138 } 138 }
139 139
140 public bool ground() 140 public bool ground()
141 { 141 {
142 // Atom is always ground. 142 // Atom is always ground.
143 return true; 143 return true;
144 } 144 }
145 145
146 public override bool Equals(object obj) 146 public override bool Equals(object obj)
147 { 147 {
148 if (obj is Atom) 148 if (obj is Atom)
149 { 149 {
150 if (_module == null && ((Atom)obj)._module == null) 150 if (_module == null && ((Atom)obj)._module == null)
151 // When _declaringClass is null, we always use an identical object from _atomStore. 151 // When _declaringClass is null, we always use an identical object from _atomStore.
152 return this == obj; 152 return this == obj;
153 // Otherwise, ignore _declaringClass and do a normal string compare on the _name. 153 // Otherwise, ignore _declaringClass and do a normal string compare on the _name.
154 return _name == ((Atom)obj)._name; 154 return _name == ((Atom)obj)._name;
155 } 155 }
156 return false; 156 return false;
157 } 157 }
158 158
159 public override string ToString() 159 public override string ToString()
160 { 160 {
161 return _name; 161 return _name;
162 } 162 }
163 163
164 public override int GetHashCode() 164 public override int GetHashCode()
165 { 165 {
166 // Debug: need to check _declaringClass. 166 // Debug: need to check _declaringClass.
167 return _name.GetHashCode(); 167 return _name.GetHashCode();
168 } 168 }
169 169
170 public string toQuotedString() 170 public string toQuotedString()
171 { 171 {
172 if (_name.Length == 0) 172 if (_name.Length == 0)
173 return "''"; 173 return "''";
174 else if (this == Atom.NIL) 174 else if (this == Atom.NIL)
175 return "[]"; 175 return "[]";
176 176
177 StringBuilder result = new StringBuilder(_name.Length); 177 StringBuilder result = new StringBuilder(_name.Length);
178 bool useQuotes = false; 178 bool useQuotes = false;
179 foreach (char c in _name) 179 foreach (char c in _name)
180 { 180 {
181 int cInt = (int)c; 181 int cInt = (int)c;
182 if (c == '\'') 182 if (c == '\'')
183 { 183 {
184 result.Append("''"); 184 result.Append("''");
185 useQuotes = true; 185 useQuotes = true;
186 } 186 }
187 else if (c == '_' || cInt >= (int)'a' && cInt <= (int)'z' || 187 else if (c == '_' || cInt >= (int)'a' && cInt <= (int)'z' ||
188 cInt >= (int)'A' && cInt <= (int)'Z' || cInt >= (int)'0' && cInt <= (int)'9') 188 cInt >= (int)'A' && cInt <= (int)'Z' || cInt >= (int)'0' && cInt <= (int)'9')
189 result.Append(c); 189 result.Append(c);
190 else 190 else
191 { 191 {
192 // Debug: Need to handle non-printable chars. 192 // Debug: Need to handle non-printable chars.
193 result.Append(c); 193 result.Append(c);
194 useQuotes = true; 194 useQuotes = true;
195 } 195 }
196 } 196 }
197 197
198 if (!useQuotes && (int)_name[0] >= (int)'a' && (int)_name[0] <= (int)'z') 198 if (!useQuotes && (int)_name[0] >= (int)'a' && (int)_name[0] <= (int)'z')
199 return result.ToString(); 199 return result.ToString();
200 else 200 else
201 { 201 {
202 // Surround in single quotes. 202 // Surround in single quotes.
203 result.Append('\''); 203 result.Append('\'');
204 return "'" + result; 204 return "'" + result;
205 } 205 }
206 } 206 }
207 207
208 /// <summary> 208 /// <summary>
209 /// Return true if _name is lexicographically less than atom._name. 209 /// Return true if _name is lexicographically less than atom._name.
210 /// </summary> 210 /// </summary>
211 /// <param name="atom"></param> 211 /// <param name="atom"></param>
212 /// <returns></returns> 212 /// <returns></returns>
213 public bool lessThan(Atom atom) 213 public bool lessThan(Atom atom)
214 { 214 {
215 return _name.CompareTo(atom._name) < 0; 215 return _name.CompareTo(atom._name) < 0;
216 } 216 }
217 } 217 }
218} 218}