summaryrefslogtreecommitdiffstats
path: root/src/Tuple.vala
diff options
context:
space:
mode:
Diffstat (limited to 'src/Tuple.vala')
-rw-r--r--src/Tuple.vala67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/Tuple.vala b/src/Tuple.vala
new file mode 100644
index 0000000..097973f
--- /dev/null
+++ b/src/Tuple.vala
@@ -0,0 +1,67 @@
+namespace Eva {
+ public class Tuple : Object, Term {
+ private Term[] elements {get; set;}
+
+ public Tuple(Term[] terms) {
+ elements = terms;
+ }
+
+ public int size {
+ get {
+ return elements.length;
+ }
+ }
+
+ public new unowned Term get(int i) {
+ assert(i >= 0 && i < size);
+
+ return elements[i];
+ }
+
+ public string to_string() {
+ string ret = "{";
+ bool first = true;
+
+ foreach(Term term in elements) {
+ if(first) {
+ first = false;
+ ret += term.to_string();
+ }
+ else {
+ ret += "," + term.to_string();
+ }
+ }
+
+ return ret + "}";
+ }
+
+ protected bool do_match(Term o, Gee.Map<string, Term> vars, Gee.Map<string, string> aliases) {
+ if(o is Var) {
+ return o.do_match(this, vars, aliases);
+ }
+
+ if(!(o is Tuple))
+ return false;
+
+ Tuple t = o as Tuple;
+
+ if(t.size != size)
+ return false;
+
+ for(int i = 0; i < size; ++i) {
+ if(!this[i].do_match(t[i], vars, aliases))
+ return false;
+ }
+
+ return true;
+ }
+
+ public void encode(Erl.Buffer buffer) {
+ buffer.encode_tuple_header(size);
+
+ foreach(Term term in elements) {
+ term.encode(buffer);
+ }
+ }
+ }
+}