1use crate::emit::emit;
2use crate::ir::{Expr, Program, Stmt, Type};
3
4pub const SE_MAGIC: &str = "SE/1";
5const SEMANTICS_MARKER: &str = "--- semantics-json ---";
6const PIVOT_MARKER: &str = "--- julia-pivot ---";
7const END_MARKER: &str = "--- end ---";
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct SemanticBindingInfo {
11 pub name: String,
12 pub kind: String,
13 pub ty: String,
14 pub scope: String,
15 pub mutable: bool,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct SemanticOperationInfo {
20 pub op: String,
21 pub result_type: String,
22 pub scope: String,
23 pub source: String,
24 pub semantics: Vec<(String, String)>,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct SemanticFunctionInfo {
29 pub name: String,
30 pub parameters: Vec<(String, String)>,
31 pub return_type: String,
32 pub bindings: Vec<SemanticBindingInfo>,
33 pub operations: Vec<SemanticOperationInfo>,
34 pub effects: Vec<String>,
35 pub control_flow: Vec<String>,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct SemanticProgram {
40 pub source_language: String,
41 pub evaluation: String,
42 pub value_model: String,
43 pub source_index_base: usize,
44 pub index_base: usize,
45 pub functions: Vec<SemanticFunctionInfo>,
46 pub features: Vec<String>,
47 pub effects: Vec<String>,
48 pub warnings: Vec<String>,
49}
50
51fn type_name(t: Option<&Type>) -> String {
52 match t {
53 None | Some(Type::Unit) => "Nothing".into(),
54 Some(Type::Bool) => "Bool".into(),
55 Some(Type::String) => "String".into(),
56 Some(Type::Int) => "Int".into(),
57 Some(Type::Int8) => "Int8".into(),
58 Some(Type::Int16) => "Int16".into(),
59 Some(Type::Int32) => "Int32".into(),
60 Some(Type::Int64) => "Int64".into(),
61 Some(Type::UInt) => "UInt".into(),
62 Some(Type::UInt8) => "UInt8".into(),
63 Some(Type::UInt16) => "UInt16".into(),
64 Some(Type::UInt32) => "UInt32".into(),
65 Some(Type::UInt64) => "UInt64".into(),
66 Some(Type::Float32) => "Float32".into(),
67 Some(Type::Float64) => "Float64".into(),
68 Some(Type::Slice(x)) => format!("Vector{{{}}}", type_name(Some(x))),
69 Some(Type::Map(k, v)) => format!("Dict{{{}, {}}}", type_name(Some(k)), type_name(Some(v))),
70 Some(Type::Tuple(items)) => format!(
71 "Tuple{{{}}}",
72 items
73 .iter()
74 .map(|x| type_name(Some(x)))
75 .collect::<Vec<_>>()
76 .join(", ")
77 ),
78 Some(Type::Named(n)) => n.clone(),
79 }
80}
81
82fn expr_text(e: &Expr) -> String {
83 match e {
84 Expr::Ident(x) | Expr::Int(x) | Expr::Float(x) => x.clone(),
85 Expr::String(x) => format!("{:?}", x),
86 Expr::Bool(v) => v.to_string(),
87 Expr::Nil => "nothing".into(),
88 Expr::Unary { op, value } => format!("{}{}", op, expr_text(value)),
89 Expr::Binary { left, op, right } => {
90 format!("{} {} {}", expr_text(left), op, expr_text(right))
91 }
92 Expr::Call { function, args } => format!(
93 "{}({})",
94 function,
95 args.iter().map(expr_text).collect::<Vec<_>>().join(", ")
96 ),
97 Expr::Index { value, index } => format!("{}[{}]", expr_text(value), expr_text(index)),
98 Expr::Array { items, .. } => format!(
99 "[{}]",
100 items.iter().map(expr_text).collect::<Vec<_>>().join(", ")
101 ),
102 Expr::Map { entries, .. } => format!(
103 "Dict({})",
104 entries
105 .iter()
106 .map(|(k, v)| format!("{} => {}", expr_text(k), expr_text(v)))
107 .collect::<Vec<_>>()
108 .join(", ")
109 ),
110 Expr::MapIndex { value, index, .. } => {
111 format!("{}[{}]", expr_text(value), expr_text(index))
112 }
113 Expr::MapLookupOk { value, index, .. } => {
114 format!("lookup_ok({}, {})", expr_text(value), expr_text(index))
115 }
116 Expr::Tuple(items) => format!(
117 "({})",
118 items.iter().map(expr_text).collect::<Vec<_>>().join(", ")
119 ),
120 Expr::StructLiteral {
121 ty,
122 fields,
123 positional,
124 } => {
125 if !fields.is_empty() {
126 format!(
127 "{}{{{}}}",
128 ty,
129 fields
130 .iter()
131 .map(|(k, v)| format!("{}={}", k, expr_text(v)))
132 .collect::<Vec<_>>()
133 .join(", ")
134 )
135 } else {
136 format!(
137 "{}({})",
138 ty,
139 positional
140 .iter()
141 .map(expr_text)
142 .collect::<Vec<_>>()
143 .join(", ")
144 )
145 }
146 }
147 Expr::Raw(x) => x.clone(),
148 }
149}
150
151fn operation(e: &Expr, scope: &str) -> Option<SemanticOperationInfo> {
152 let (op, result, semantics) = match e {
153 Expr::Binary { op, .. } => {
154 let name = match op.as_str() {
155 "+" => "numeric.add",
156 "-" => "numeric.subtract",
157 "*" => "numeric.multiply",
158 "/" => "numeric.divide",
159 "%" => "numeric.remainder",
160 "==" => "comparison.equal",
161 "!=" => "comparison.not_equal",
162 "<" => "comparison.less",
163 "<=" => "comparison.less_equal",
164 ">" => "comparison.greater",
165 ">=" => "comparison.greater_equal",
166 "&&" => "logical.and",
167 "||" => "logical.or",
168 _ => "operator",
169 };
170 (
171 name,
172 "unknown",
173 vec![("evaluation_order".into(), "left_to_right".into())],
174 )
175 }
176 Expr::Index { .. } | Expr::MapIndex { .. } => (
177 "collection.index",
178 "unknown",
179 vec![("bounds".into(), "source_language".into())],
180 ),
181 Expr::Call { function, .. } if function == "len" || function == "length" => (
182 "collection.length",
183 "Int",
184 vec![("result".into(), "canonical_machine_integer".into())],
185 ),
186 Expr::Call { function, .. } if function == "append" || function == "push!" => (
187 "collection.append",
188 "Nothing",
189 vec![("mutation".into(), "in_place_or_source_equivalent".into())],
190 ),
191 Expr::Call { .. } => (
192 "function.call",
193 "unknown",
194 vec![("evaluation_order".into(), "left_to_right".into())],
195 ),
196 _ => return None,
197 };
198 Some(SemanticOperationInfo {
199 op: op.into(),
200 result_type: result.into(),
201 scope: scope.into(),
202 source: expr_text(e),
203 semantics,
204 })
205}
206
207fn inspect_expr(
208 e: &Expr,
209 scope: &str,
210 ops: &mut Vec<SemanticOperationInfo>,
211 features: &mut Vec<String>,
212) {
213 if let Some(op) = operation(e, scope) {
214 ops.push(op);
215 }
216 match e {
217 Expr::Binary { left, right, .. } => {
218 inspect_expr(left, scope, ops, features);
219 inspect_expr(right, scope, ops, features);
220 }
221 Expr::Unary { value, .. } => inspect_expr(value, scope, ops, features),
222 Expr::Call { args, .. } => {
223 for x in args {
224 inspect_expr(x, scope, ops, features);
225 }
226 }
227 Expr::Index { value, index }
228 | Expr::MapIndex { value, index, .. }
229 | Expr::MapLookupOk { value, index, .. } => {
230 push_unique(features, "indexing");
231 inspect_expr(value, scope, ops, features);
232 inspect_expr(index, scope, ops, features);
233 }
234 Expr::Array { items, .. } | Expr::Tuple(items) => {
235 for x in items {
236 inspect_expr(x, scope, ops, features);
237 }
238 }
239 Expr::Map { entries, .. } => {
240 push_unique(features, "maps");
241 for (k, v) in entries {
242 inspect_expr(k, scope, ops, features);
243 inspect_expr(v, scope, ops, features);
244 }
245 }
246 Expr::StructLiteral {
247 fields, positional, ..
248 } => {
249 push_unique(features, "structs");
250 for (_, v) in fields {
251 inspect_expr(v, scope, ops, features);
252 }
253 for v in positional {
254 inspect_expr(v, scope, ops, features);
255 }
256 }
257 _ => {}
258 }
259}
260
261fn push_unique(v: &mut Vec<String>, value: &str) {
262 if !v.iter().any(|x| x == value) {
263 v.push(value.to_string());
264 }
265}
266
267fn inspect_stmts(
268 stmts: &[Stmt],
269 scope: &str,
270 info: &mut SemanticFunctionInfo,
271 features: &mut Vec<String>,
272) {
273 for s in stmts {
274 match s {
275 Stmt::Let {
276 name,
277 ty,
278 value,
279 mutable,
280 } => {
281 info.bindings.push(SemanticBindingInfo {
282 name: name.clone(),
283 kind: "local".into(),
284 ty: type_name(ty.as_ref()),
285 scope: scope.into(),
286 mutable: *mutable,
287 });
288 inspect_expr(value, scope, &mut info.operations, features);
289 }
290 Stmt::MultiLet {
291 names,
292 value,
293 mutable,
294 } => {
295 for name in names {
296 info.bindings.push(SemanticBindingInfo {
297 name: name.clone(),
298 kind: "local".into(),
299 ty: "unknown".into(),
300 scope: scope.into(),
301 mutable: *mutable,
302 });
303 }
304 inspect_expr(value, scope, &mut info.operations, features);
305 }
306 Stmt::Assign { value, .. } | Stmt::MultiAssign { value, .. } => {
307 push_unique(&mut info.effects, "mutation.binding");
308 inspect_expr(value, scope, &mut info.operations, features);
309 }
310 Stmt::IndexAssign {
311 collection,
312 index,
313 value,
314 ..
315 } => {
316 push_unique(&mut info.effects, "mutation.collection");
317 push_unique(features, "indexing");
318 inspect_expr(collection, scope, &mut info.operations, features);
319 inspect_expr(index, scope, &mut info.operations, features);
320 inspect_expr(value, scope, &mut info.operations, features);
321 }
322 Stmt::Expr(e) => inspect_expr(e, scope, &mut info.operations, features),
323 Stmt::Print { args, .. } => {
324 push_unique(&mut info.effects, "io.stdout");
325 for e in args {
326 inspect_expr(e, scope, &mut info.operations, features);
327 }
328 }
329 Stmt::Return(v) => {
330 if let Some(e) = v {
331 inspect_expr(e, scope, &mut info.operations, features);
332 }
333 }
334 Stmt::If {
335 cond,
336 then_body,
337 else_body,
338 } => {
339 push_unique(features, "branching");
340 info.control_flow.push("if".into());
341 inspect_expr(cond, scope, &mut info.operations, features);
342 inspect_stmts(then_body, scope, info, features);
343 inspect_stmts(else_body, scope, info, features);
344 }
345 Stmt::While { cond, body } => {
346 push_unique(features, "loops");
347 info.control_flow.push("while".into());
348 inspect_expr(cond, scope, &mut info.operations, features);
349 inspect_stmts(body, scope, info, features);
350 }
351 Stmt::ForEach { iter, body, .. } => {
352 push_unique(features, "loops");
353 info.control_flow.push("for".into());
354 inspect_expr(iter, scope, &mut info.operations, features);
355 inspect_stmts(body, scope, info, features);
356 }
357 Stmt::Switch {
358 value,
359 cases,
360 default,
361 } => {
362 push_unique(features, "branching");
363 info.control_flow.push("switch".into());
364 if let Some(v) = value {
365 inspect_expr(v, scope, &mut info.operations, features);
366 }
367 for c in cases {
368 for v in &c.values {
369 inspect_expr(v, scope, &mut info.operations, features);
370 }
371 inspect_stmts(&c.body, scope, info, features);
372 }
373 inspect_stmts(default, scope, info, features);
374 }
375 Stmt::Break | Stmt::Continue => {}
376 }
377 }
378}
379
380pub fn semantic_program(source_language: &str, program: &Program) -> SemanticProgram {
381 let mut features = vec!["functions".into()];
382 if !program.structs.is_empty() {
383 push_unique(&mut features, "structs");
384 }
385 let mut functions = Vec::new();
386 let mut global_effects = Vec::new();
387 for f in &program.functions {
388 let mut info = SemanticFunctionInfo {
389 name: f.name.clone(),
390 parameters: f
391 .params
392 .iter()
393 .map(|p| (p.name.clone(), type_name(Some(&p.ty))))
394 .collect(),
395 return_type: type_name(f.return_type.as_ref()),
396 bindings: f
397 .params
398 .iter()
399 .map(|p| SemanticBindingInfo {
400 name: p.name.clone(),
401 kind: "parameter".into(),
402 ty: type_name(Some(&p.ty)),
403 scope: f.name.clone(),
404 mutable: false,
405 })
406 .collect(),
407 operations: Vec::new(),
408 effects: Vec::new(),
409 control_flow: Vec::new(),
410 };
411 inspect_stmts(&f.body, &f.name, &mut info, &mut features);
412 for e in &info.effects {
413 push_unique(&mut global_effects, e);
414 }
415 functions.push(info);
416 }
417 let source_index_base = if source_language == "julia" || source_language == "r" {
418 1
419 } else {
420 0
421 };
422 SemanticProgram {
423 source_language: source_language.into(),
424 evaluation: if source_language == "r" {
425 "lazy_demand".into()
426 } else {
427 "eager_left_to_right".into()
428 },
429 value_model: match source_language {
430 "rust" => "static_owned_values",
431 "go" => "static_go_values",
432 "c" | "cpp" => "static_native_machine_values",
433 "python" => "dynamic_arbitrary_precision_integer",
434 _ => "mixed_static_dynamic",
435 }
436 .into(),
437 source_index_base,
438 index_base: 1,
439 functions,
440 features,
441 effects: global_effects,
442 warnings: Vec::new(),
443 }
444}
445
446fn esc(s: &str) -> String {
447 let mut out = String::with_capacity(s.len() + 8);
448 for c in s.chars() {
449 match c {
450 '\\' => out.push_str("\\\\"),
451 '"' => out.push_str("\\\""),
452 '\n' => out.push_str("\\n"),
453 '\r' => out.push_str("\\r"),
454 '\t' => out.push_str("\\t"),
455 c if c.is_control() => out.push_str(&format!("\\u{:04x}", c as u32)),
456 c => out.push(c),
457 }
458 }
459 out
460}
461fn js(s: &str) -> String {
462 format!("\"{}\"", esc(s))
463}
464fn jarr(v: &[String]) -> String {
465 format!(
466 "[{}]",
467 v.iter().map(|x| js(x)).collect::<Vec<_>>().join(",")
468 )
469}
470
471pub fn semantic_json(p: &SemanticProgram) -> String {
472 let functions = p.functions.iter().map(|f| {
473 let params = f.parameters.iter().map(|(n,t)| format!("{{\"name\":{},\"type\":{}}}", js(n), js(t))).collect::<Vec<_>>().join(",");
474 let bindings = f.bindings.iter().map(|b| format!("{{\"name\":{},\"kind\":{},\"type\":{},\"scope\":{},\"mutable\":{}}}", js(&b.name), js(&b.kind), js(&b.ty), js(&b.scope), b.mutable)).collect::<Vec<_>>().join(",");
475 let ops = f.operations.iter().map(|o| {
476 let sem = o.semantics.iter().map(|(k,v)| format!("{}:{}", js(k), js(v))).collect::<Vec<_>>().join(",");
477 format!("{{\"operation\":{},\"result_type\":{},\"scope\":{},\"source\":{},\"semantics\":{{{}}}}}", js(&o.op), js(&o.result_type), js(&o.scope), js(&o.source), sem)
478 }).collect::<Vec<_>>().join(",");
479 format!("{{\"name\":{},\"parameters\":[{}],\"return_type\":{},\"bindings\":[{}],\"operations\":[{}],\"effects\":{},\"control_flow\":{}}}", js(&f.name), params, js(&f.return_type), bindings, ops, jarr(&f.effects), jarr(&f.control_flow))
480 }).collect::<Vec<_>>().join(",");
481 format!("{{\"source_language\":{},\"evaluation\":{},\"value_model\":{},\"source_index_base\":{},\"index_base\":{},\"functions\":[{}],\"globals\":[],\"features\":{},\"effects\":{},\"warnings\":{}}}\n",
482 js(&p.source_language), js(&p.evaluation), js(&p.value_model), p.source_index_base, p.index_base, functions, jarr(&p.features), jarr(&p.effects), jarr(&p.warnings))
483}
484
485pub fn semantic_text(p: &SemanticProgram) -> String {
486 let mut out = String::new();
487 out.push_str("SemanticProgram\n");
488 out.push_str(&format!(" source: {}\n evaluation: {}\n value_model: {}\n source_index_base: {}\n canonical_index_base: {}\n features: {}\n", p.source_language, p.evaluation, p.value_model, p.source_index_base, p.index_base, if p.features.is_empty() { "none".into() } else { p.features.join(", ") }));
489 for f in &p.functions {
490 out.push_str(&format!(
491 "\nfunction {}({}) -> {}\n",
492 f.name,
493 f.parameters
494 .iter()
495 .map(|(n, t)| format!("{}::{}", n, t))
496 .collect::<Vec<_>>()
497 .join(", "),
498 f.return_type
499 ));
500 if !f.control_flow.is_empty() {
501 out.push_str(&format!(" control: {}\n", f.control_flow.join(" -> ")));
502 }
503 if !f.effects.is_empty() {
504 out.push_str(&format!(" effects: {}\n", f.effects.join(", ")));
505 }
506 if !f.bindings.is_empty() {
507 out.push_str(" bindings:\n");
508 for b in &f.bindings {
509 out.push_str(&format!(
510 " - {} : {} [{}{}]\n",
511 b.name,
512 b.ty,
513 b.kind,
514 if b.mutable { ", mutable" } else { "" }
515 ));
516 }
517 }
518 if !f.operations.is_empty() {
519 out.push_str(" operations:\n");
520 for op in &f.operations {
521 out.push_str(&format!(
522 " - {} -> {}: {}\n",
523 op.op, op.result_type, op.source
524 ));
525 }
526 }
527 }
528 out
529}
530
531pub fn semantic_exchange(source: &str, program: &Program) -> Result<String, String> {
532 let pivot = emit(program, "julia").map_err(|e| e.to_string())?;
533 let sem = semantic_json(&semantic_program(source, program));
534 Ok(format!("{SE_MAGIC}\nsource: {source}\ncanonical: julia\n{SEMANTICS_MARKER}\n{sem}{PIVOT_MARKER}\n{pivot}{END_MARKER}\n"))
535}
536
537pub fn semantic_exchange_pivot(text: &str) -> Result<String, String> {
538 let s = text.replace("\r\n", "\n");
539 if !s.starts_with("SE/1\n") {
540 return Err("invalid SE input: expected SE/1 header".into());
541 }
542 let marker = format!("{PIVOT_MARKER}\n");
543 let start = s
544 .find(&marker)
545 .ok_or_else(|| "invalid SE input: missing Julia pivot section".to_string())?
546 + marker.len();
547 let end_marker = format!("\n{END_MARKER}");
548 let rel = s[start..]
549 .find(&end_marker)
550 .ok_or_else(|| "invalid SE input: missing end marker".to_string())?;
551 let pivot = &s[start..start + rel];
552 if pivot.trim().is_empty() {
553 return Err("invalid SE input: empty Julia pivot".into());
554 }
555 Ok(format!(
556 "{}{}",
557 pivot,
558 if pivot.ends_with('\n') { "" } else { "\n" }
559 ))
560}