summaryrefslogtreecommitdiffstats
path: root/src/List.vala
blob: e215db4909abeebb8ce4b0bf8a5726c49458db51 (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace Eva {
  public class List : Object, Term {
    private Term? _tail;
    
    public Gee.List<Term?> list {get; construct;}
    public Term? tail {
      get {
        return _tail;
      }
      set {
        if(value is List) {
          List l = value as List;
          
          list.add_all(l.list);
          _tail = l._tail;
        }
        else {
          _tail = value;
        }
      }
    }
    
    public List() {
      Gee.List<Term?> list0 = new Gee.LinkedList<Term?>();
      Object(list: list0);
      tail = null;
    }
    
    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 List) {
        List l = o as List;
        
        if(list.size != l.list.size)
          return false;
        if((tail == null) != (l.tail == null))
          return false;
        if(tail != null && !tail.do_match(l.tail, vars, aliases))
          return false;
        
        Gee.Iterator<Term> lt = l.list.iterator();
        lt.first();
        foreach(Term t in list) {
          if(!t.do_match(lt.get(), vars, aliases))
            return false;
          
          lt.next();
        }
        
        return true;
      }
      else if(o is String) {
        if(tail != null)
          return false;
        
        string s = "";
        
        foreach(Term t in list) {
          unichar c;
          
          if(t is Int) {
            Int i = t as Int;
            if(i.value < 0 || i.value > 255)
              return false;
            
            c = (unichar)i.value;
          }
          else if(t is UInt) {
            UInt i = t as UInt;
            if(i.value > 255)
              return false;
            
            c = (unichar)i.value;
          }
          else {
            return false;
          }
          
          string buf = string.nfill(6, 0);
          c.to_utf8(buf);
          
          s += buf;
        }
        
        return (s == (o as String).value);
      }
      else {
        return false;
      }
    }
    
    public string to_string() {
      string ret = "[";
      bool first = true;
      
      foreach(Term term in list) {
        if(first) {
          first = false;
          ret += term.to_string();
        }
        else {
          ret += "," + term.to_string();
        }
      }
      
      if(tail != null)
        ret += "|" + tail.to_string();
      
      return ret + "]";
    }
    
    public void encode(Erl.Buffer buffer) {
      if(!list.is_empty) {
        buffer.encode_list_header(list.size);
      
        foreach(Term term in list) {
          term.encode(buffer);
        }
      }
      
      if(tail == null)
        buffer.encode_empty_list();
      else
        tail.encode(buffer);
    }
  }
}