summaryrefslogtreecommitdiffstats
path: root/src/Tuple.vala
blob: 097973fa79091e20c18f6187e3a98dffd3bc2c36 (plain)
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
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);
      }
    }
  }
}