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
/*

Copyright 2023 - Andrew Johnson

This code and all related intellectual property is available under the terms of
the attached permissive MIT license. This license is intended only to protect
the future development of the project while otherwise allowing people to use
the code and IP as they would like. Please, just be nice.

A: An S-Expression based AST

*/

use regex::Regex;
use std::collections::HashMap;
use crate::*;

pub fn literal(s: &str) -> S {
   s_cons( s_atom("literal"), s_atom(s) )
}

pub fn variable(s: &str) -> S {
   s_cons( s_atom("variable"), s_atom(s) )
}

pub fn typ(s: &str) -> S {
   s_cons( s_atom("type"), s_atom(s) )
}

pub fn lambda(l: S, r: S) -> S {
   s_cons( s_atom("lambda"), s_cons(l,r) )
}

pub fn app(f: S, x: S) -> S {
   s_cons( s_atom("app"), s_cons(f,x) )
}

pub fn regex(r: &str) -> S {
   s_cons( s_atom("regex"), s_atom(r) )
}

pub fn nil() -> S {
   s_nil()
}

pub fn list(s: &[S]) -> S {
   let mut tail = s_nil();
   for x in s.iter().rev() {
      tail = s_cons( x.clone(), tail );
   }
   tail
}

pub fn kv(s: &[(S,S)]) -> S {
   let mut tail = s_nil();
   for (k,v) in s.iter().rev() {
      tail = s_cons( s_cons(k.clone(),v.clone()), tail );
   }
   s_cons( s_atom("kv"), tail )
}

pub fn kv_iter(s: &S) -> Vec<(S,S)> {
   let mut kvs = Vec::new();
   let mut h = s.clone();
   while is_cons(&h) {
      let kv = head(&h);
      if is_cons(&kv) {
         kvs.push(( head(&kv), tail(&kv) ));
      }
      h = tail(&h);
   }
   kvs
}

pub fn kv_merge(l: &S, r: &S) -> S {
   let mut kvs = Vec::new();
   for (k,v) in kv_iter(l) {
      kvs.push(( k, v ));
   }
   for (k,v) in kv_iter(r) {
      kvs.push(( k, v ));
   }
   kv(&kvs)
}

pub fn kv_ctx(s: &S) -> HashMap<String,S> {
   let mut ctx = HashMap::new();
   for (k,v) in kv_iter(s) {
      let k = k.to_string();
      ctx.insert( k, v );
   }
   ctx
}

pub fn kv_lookup(ctx: &S, key: &S, default: &S) -> S {
   for (k,v) in kv_iter(ctx) {
      if k==*key { return v.clone(); }
   }
   default.clone()
}

pub fn kv_s(ctx: &HashMap<String,S>) -> S {
   kv(&ctx.iter().map(|(k,v)| (s_atom(&k),v.clone())).collect::<Vec<(S,S)>>())
}

pub fn destructure(ctx: &mut HashMap<String,S>, pattern: S, value: S) -> bool {
   if pattern==value { return true; }
   if !is_cons(&pattern) { return false; }
   if head(&pattern)==s_atom("variable") {
      let k = tail(&pattern).to_string();
      ctx.insert( k, value );
      return true;
   }
   if !is_cons(&value) { return false; }
   if is_atom(&head(&pattern)) && head(&pattern).to_string()=="lambda" {
      return false;
   }
   if is_atom(&head(&pattern)) && head(&pattern).to_string()=="regex" {
      if !is_atom(&head(&value)) || head(&value).to_string()!="literal" { return false; }
      let value = tail(&value).to_string();
      let re = Regex::new(&tail(&pattern).to_string()).unwrap();
      return if let Some(c) = re.captures(&value) {
         for (ci,cm) in c.iter().enumerate() {
            if let Some(m) = cm.map(|m| m.as_str()) {
               let k = "{".to_string() + &format!("{}",ci) + "}";
               ctx.insert( k, literal(m) );
            }
         }
         true
      } else { false }
   }
   if is_atom(&head(&pattern)) && head(&pattern).to_string()=="kv" &&
      is_atom(&head(&value)) && head(&value).to_string()=="kv" {
      for (lk,lv) in kv_iter(&tail(&pattern)) {
         let mut found = false;
         for (rk,rv) in kv_iter(&tail(&value)) {
            if lk==rk {
               if !destructure(ctx, lv, rv) { return false; }
               found = true;
               break;
            }
         }
         if !found { return false; }
      }
      return true;
   }
   destructure(ctx, head(&pattern), head(&value)) &&
   destructure(ctx, tail(&pattern), tail(&value))
}
fn restructure(ctx: &HashMap<String,S>, value: S) -> S {
   if !is_cons(&value) { return value; }
   if head(&value)==s_atom("variable") {
      let k = tail(&value).to_string();
      return if let Some(v) = ctx.get(&k) { v.clone() }
      else { value };
   }
   value
}
pub fn map(lhs: S, v: S, rhs: S) -> S {
   let mut ctx = HashMap::new();
   if destructure(&mut ctx, lhs, v) {
      restructure(&ctx, rhs)
   } else { s_nil() }
}