summaryrefslogtreecommitdiffstats
path: root/src/Atom.vala
blob: bb3a6fd28ea5871e7d992c2fd9f8a3f58589bfd1 (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
namespace Eva {
  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("'", "\\'") + "'";
      }
    }
    
    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 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);
    }
  }
}