summaryrefslogtreecommitdiffstats
path: root/src/Term.vala
blob: 46c03b3a4b6be88b704631d628ead3351e4f6497 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
namespace Eva {
  public interface Term : Object {
    public abstract string to_string();
    public abstract bool equals(Term o);
    
    public abstract void encode(Erl.Buffer buffer);
    
    public static Term decode(void *buffer) {
      int index = 0;
      int version;
      
      Erl.decode_version(buffer, ref index, out version);
      
      return do_decode(buffer, ref index);
    }
    
    private static Term do_decode(void *buffer, ref int index) {
      int type, size;
      assert(Erl.get_type(buffer, ref index, out type, out size) == 0);
      
      switch(type) {
      case Erl.TermType.SMALL_INTEGER:
        char value;
        assert(Erl.decode_char(buffer, ref index, out value) == 0);
        return new Int((uchar)value);
        
      case Erl.TermType.INTEGER:
        long value;
        assert(Erl.decode_long(buffer, ref index, out value) == 0);
        return new Int(value);
        
      case Erl.TermType.FLOAT:
        double value;
        assert(Erl.decode_double(buffer, ref index, out value) == 0);
        return new Double(value);
        
      case Erl.TermType.ATOM:
        char[] value = new char[size+1];
        assert(Erl.decode_atom(buffer, ref index, value) == 0);
        return new Atom(array_to_string(value, size));
        
      case Erl.TermType.SMALL_TUPLE:
      case Erl.TermType.LARGE_TUPLE:
        assert(Erl.decode_tuple_header(buffer, ref index, out size) == 0);
        Tuple term = new Tuple();
        
        for(int i = 0; i < size; ++i) {
          term.value.add(do_decode(buffer, ref index));
        }
        
        return term;
        
      case Erl.TermType.NIL:
      case Erl.TermType.LIST:
        List term = new List();
        
        assert(Erl.decode_list_header(buffer, ref index, out size) == 0);
        if(size != 0) {
          for(int i = 0; i < size; ++i) {
            term.list.add(do_decode(buffer, ref index));
          }
        
          term.tail = do_decode(buffer, ref index);
        }
        
        return term;
        
      case Erl.TermType.STRING:
        char[] value = new char[size+1];
        assert(Erl.decode_string(buffer, ref index, value) == 0);
        return new String(array_to_string(value, size));
        
      case Erl.TermType.BINARY:
        char[] value = new char[size];
        long l;
        assert(Erl.decode_binary(buffer, ref index, value, out l) == 0);
        return new Binary(value);
        
      case Erl.TermType.PID:
        Erl.Pid value;
        assert(Erl.decode_pid(buffer, ref index, out value) == 0);
        return new Pid(value);
        
      case Erl.TermType.PORT:
        Erl.Port value;
        assert(Erl.decode_port(buffer, ref index, out value) == 0);
        return new Port(value);
        
      case Erl.TermType.REFERENCE:
      case Erl.TermType.NEW_REFERENCE:
        Erl.Ref value;
        assert(Erl.decode_ref(buffer, ref index, out value) == 0);
        return new Ref(value);
        
      default:
        assert_not_reached();
      }
    }
  }
  
  public class Int : Object, Term {
    public long value {get; construct;}
    
    public Int(long v) {
      Object(value: v);
    }
    
    public string to_string() {
      return value.to_string();
    }
    
    public bool equals(Term o) {
      if(o is Int) {
        return value == (o as Int).value;
      }
      else if(o is UInt) {
        return value == (o as UInt).value;
      }
      else {
        return false;
      }
    }
    
    public void encode(Erl.Buffer buffer) {
      buffer.encode_long(value);
    }
  }
  
  public class UInt : Object, Term {
    public ulong value {get; construct;}
    
    public UInt(ulong v) {
      Object(value: v);
    }
    
    public string to_string() {
      return value.to_string();
    }
    
    public bool equals(Term o) {
      if(o is Int) {
        return value == (o as Int).value;
      }
      else if(o is UInt) {
        return value == (o as UInt).value;
      }
      else {
        return false;
      }
    }

    
    public void encode(Erl.Buffer buffer) {
      buffer.encode_ulong(value);
    }
  }
  
  public class Double : Object, Term {
    public double value {get; construct;}
    
    public Double(double v) {
      Object(value: v);
    }
    
    public string to_string() {
      return value.to_string();
    }
    
    public bool equals(Term o) {
      if(o is Double) {
        return value == (o as Double).value;
      }
      else {
        return false;
      }
    }
    
    public void encode(Erl.Buffer buffer) {
      buffer.encode_double(value);
    }
  }
  
  public class Atom : Object, Term {
    public string value {get; construct;}
    
    public Atom(string v) {
      assert(v.validate());
      Object(value: v);
    }
    
    public string to_string() {
      if(value.length == 0)
        return "''";
      
      bool special = false;
      unichar c = value.get_char();
      
      if(!c.islower())
        special = true;
      
      for(unowned string rest = value.next_char(); rest.length != 0 && !special; rest = rest.next_char()) {
        c = rest.get_char();
        
        if(!(c == '_' || c.isalnum()))
          special = true;
      }
      
      if(!special) {
        return value;
      }
      else {
        return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'";
      }
    }
    
    public bool equals(Term o) {
      if(o is Atom) {
        return value == (o as Atom).value;
      }
      else {
        return false;
      }
    }
    
    public void encode(Erl.Buffer buffer) {
      char[]? array = string_to_array(value);
      assert(array != null);
      buffer.encode_atom(array);
    }
  }
  
  public class Tuple : Object, Term {
    public Gee.List<Term?> value {get; construct;}
    
    public Tuple() {
      Gee.List<Term?> list = new Gee.ArrayList<Term?>();
      Object(value: list);
    }
    
    public string to_string() {
      string ret = "{";
      bool first = true;
      
      foreach(Term term in value) {
        if(first) {
          first = false;
          ret += term.to_string();
        }
        else {
          ret += "," + term.to_string();
        }
      }
      
      return ret + "}";
    }
    
    public bool equals(Term o) {
      if(!(o is Tuple))
        return false;
      
      Tuple t = o as Tuple;
      
      if(t.value.size != value.size)
        return false;
      
      for(int i = 0; i < value.size; ++i) {
        if(!value[i].equals(t.value[i]))
          return false;
      }
      
      return true;
    }
    
    public void encode(Erl.Buffer buffer) {
      buffer.encode_tuple_header(value.size);
      
      foreach(Term term in value) {
        term.encode(buffer);
      }
    }
  }
  
  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;
    }
    
    public bool equals(Term o) {
      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.equals(l.tail))
          return false;
        
        Gee.Iterator<Term> lt = l.list.iterator();
        lt.first();
        foreach(Term t in list) {
          if(!t.equals(lt.get()))
            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);
    }
  }
  
  public class String : Object, Term {
    public string value {get; construct;}
    
    public String(string v) {
      Object(value: v);
    }
    
    public string to_string() {
      return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
    }
    
    public bool equals(Term o) {
      if(o is String) {
        return (value == (o as String).value);
      }
      else if(o is List) {
        return o.equals(this);
      }
      else {
        return false;
      }
    }
    
    public void encode(Erl.Buffer buffer) {
      char[]? array = string_to_array(value);
      if(array != null) {
        buffer.encode_string(array);
      }
      else {
        string_to_list(value).encode(buffer);
      }
    }
  }
  
  public class Binary : Object, Term {
    public void* value {get; private set;}
    public long len {get; private set;}
    
    public Binary(char[] v) {
      value = Memory.dup(v, (uint)(sizeof(char)*v.length));
      len = v.length;
    }
    
    public string to_string() {
      return "#Bin";
    }
    
    public bool equals(Term o) {
      if(o is Binary) {
        Binary b = o as Binary;
        
        if(b.len != len)
          return false;
        
        return (Memory.cmp(value, b.value, len) == 0);
      }
      else {
        return false;
      }
    }

    
    public void encode(Erl.Buffer buffer) {
      buffer.encode_binary((char*)value, len);
    }
  }
  
  public class Pid : Object, Term {
    public string node {get; construct;}
    public uint num {get; construct;}
    public uint serial {get; construct;}
    public uint creation {get; construct;}
    
    public Pid(Erl.Pid pid) {
      this.create(array_to_string(pid.node, Erl.MAXATOMLEN), pid.num, pid.serial, pid.creation);
    }
    
    private Pid.create(string node0, uint num0, uint serial0, uint creation0) {
      Object(node: node0, num: num0, serial: serial0, creation: creation0);
    }
    
    public string to_string() {
      return "<" + node + "." + num.to_string() + "." + serial.to_string() + ">";
    }
    
    public bool equals(Term o) {
      if(!(o is Pid))
        return false;
      
      Pid p = o as Pid;
      
      return (node == p.node && num == p.num && serial == p.serial && creation == p.creation);
    }
    
    public void encode(Erl.Buffer buffer) {
      Erl.Pid pid = Erl.Pid();
      char[]? nodedata = string_to_array(node);
      assert(nodedata != null);
      
      Memory.copy(pid.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length));
      pid.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0;
      
      pid.num = num;
      pid.serial = serial;
      pid.creation = creation;
      
      buffer.encode_pid(pid);
    }
  }

  public class Port : Object, Term {
    public string node {get; construct;}
    public uint id {get; construct;}
    public uint creation {get; construct;}
    
    public Port(Erl.Port port) {
      this.create(array_to_string(port.node, Erl.MAXATOMLEN), port.id, port.creation);
    }
    
    private Port.create(string node0, uint id0, uint creation0) {
      Object(node: node0, id: id0, creation: creation0);
    }
    
    public string to_string() {
      return "#Port";
    }
    
    public bool equals(Term o) {
      if(!(o is Port))
        return false;
      
      Port p = o as Port;
      
      return (node == p.node && id == p.id && creation == p.creation);
    }
    
    public void encode(Erl.Buffer buffer) {
      Erl.Port port = Erl.Port();
      char[]? nodedata = string_to_array(node);
      assert(nodedata != null);
      
      Memory.copy(port.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length));
      port.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0;
      
      port.id = id;
      port.creation = creation;
      
      buffer.encode_port(port);
    }
  }
  
  public class Ref : Object, Term {
    public string node {get; construct;}
    public int len {get; construct;}
    public uint[] n {get; private set;}
    public uint creation {get; construct;}
    
    public Ref(Erl.Ref reference) {
      this.create(array_to_string(reference.node, Erl.MAXATOMLEN), reference.len, reference.n, reference.creation);
    }
    
    private Ref.create(string node0, int len0, uint* n0, uint creation0) {
      Object(node: node0, len: len0, creation: creation0);
      
      n = new uint[len];
      for(int i = 0; i < len; ++i) {
        n[i] = n0[i];
      }
    }
    
    public string to_string() {
      return "#Ref";
    }
    
    public bool equals(Term o) {
      if(!(o is Ref))
        return false;
      
      Ref r = o as Ref;
      
      if(node != r.node || len != r.len || creation != r.creation) {
        return false;
      }
      
      for(int i = 0; i < len; ++i) {
        if(n[i] != r.n[i])
          return false;
      }
      
      return true;
    }
    
    public void encode(Erl.Buffer buffer) {
      Erl.Ref reference = Erl.Ref();
      char[]? nodedata = string_to_array(node);
      assert(nodedata != null);
      
      Memory.copy(reference.node, nodedata, int.min(Erl.MAXATOMLEN, nodedata.length));
      reference.node[int.min(Erl.MAXATOMLEN, nodedata.length)] = 0;
      
      reference.len = len;
      
      for(int i = 0; i < len; ++i) {
        reference.n[i] = n[i];
      }
      
      reference.creation = creation;
      
      buffer.encode_ref(reference);
    }
  }
}