normalize-surface-syntax 0.3.2

Surface-level syntax translation between languages via a common IR
Documentation
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
//! Convert S-expressions to AST.

use super::SExprError;
use crate::ir::*;
use serde_json::Value;

/// Convert S-expression to a program.
pub fn from_sexpr(sexpr: &Value) -> Result<Program, SExprError> {
    let stmt = value_to_stmt(sexpr)?;
    Ok(Program::new(vec![stmt]))
}

fn value_to_stmt(value: &Value) -> Result<Stmt, SExprError> {
    match value {
        // Literals become expression statements
        Value::Null => Ok(Stmt::expr(Expr::null())),
        Value::Bool(b) => Ok(Stmt::expr(Expr::bool(*b))),
        Value::Number(n) => Ok(Stmt::expr(Expr::number(n.as_f64().unwrap_or(0.0)))),
        Value::String(s) => Ok(Stmt::expr(Expr::string(s.clone()))),

        Value::Array(arr) => {
            if arr.is_empty() {
                return Ok(Stmt::expr(Expr::array(vec![])));
            }

            // First element should be the opcode
            let opcode = arr[0]
                .as_str()
                .ok_or_else(|| SExprError::ExpectedOpcode(format!("{:?}", arr[0])))?;

            let args = &arr[1..];
            parse_opcode(opcode, args)
        }

        Value::Object(_) => Err(SExprError::InvalidArgument(
            "unexpected object in S-expression".into(),
        )),
    }
}

fn value_to_expr(value: &Value) -> Result<Expr, SExprError> {
    match value {
        Value::Null => Ok(Expr::null()),
        Value::Bool(b) => Ok(Expr::bool(*b)),
        Value::Number(n) => Ok(Expr::number(n.as_f64().unwrap_or(0.0))),
        Value::String(s) => Ok(Expr::string(s.clone())),

        Value::Array(arr) => {
            if arr.is_empty() {
                return Ok(Expr::array(vec![]));
            }

            let opcode = arr[0]
                .as_str()
                .ok_or_else(|| SExprError::ExpectedOpcode(format!("{:?}", arr[0])))?;

            let args = &arr[1..];
            parse_opcode_expr(opcode, args)
        }

        Value::Object(_) => Err(SExprError::InvalidArgument(
            "unexpected object in S-expression".into(),
        )),
    }
}

fn parse_opcode(opcode: &str, args: &[Value]) -> Result<Stmt, SExprError> {
    match opcode {
        // Statement opcodes
        "std.let" => {
            ensure_arity(opcode, args, 2)?;
            let name = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.let name must be string".into()))?;
            let init = value_to_expr(&args[1])?;
            Ok(Stmt::let_decl(name, Some(init)))
        }

        "std.seq" => {
            let stmts: Result<Vec<Stmt>, _> = args.iter().map(value_to_stmt).collect();
            Ok(Stmt::block(stmts?))
        }

        "std.if" => {
            if args.len() < 2 {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 2,
                    got: args.len(),
                });
            }
            let test = value_to_expr(&args[0])?;
            let consequent = value_to_stmt(&args[1])?;
            let alternate = if args.len() > 2 {
                Some(value_to_stmt(&args[2])?)
            } else {
                None
            };
            Ok(Stmt::if_stmt(test, consequent, alternate))
        }

        "std.while" => {
            ensure_arity(opcode, args, 2)?;
            let test = value_to_expr(&args[0])?;
            let body = value_to_stmt(&args[1])?;
            Ok(Stmt::while_loop(test, body))
        }

        "std.for" => {
            ensure_arity(opcode, args, 3)?;
            let var = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.for var must be string".into()))?;
            let iterable = value_to_expr(&args[1])?;
            let body = value_to_stmt(&args[2])?;
            Ok(Stmt::for_in(var, iterable, body))
        }

        "std.return" => {
            let expr = if args.is_empty() {
                None
            } else {
                Some(value_to_expr(&args[0])?)
            };
            Ok(Stmt::return_stmt(expr))
        }

        "std.break" => Ok(Stmt::break_stmt()),
        "std.continue" => Ok(Stmt::continue_stmt()),

        "std.try" => {
            if args.is_empty() {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 1,
                    got: 0,
                });
            }
            let body = value_to_stmt(&args[0])?;
            let mut catch_param = None;
            let mut catch_body = None;
            let mut finally_body = None;

            for arg in &args[1..] {
                if let Value::Array(arr) = arg
                    && let Some(tag) = arr.first().and_then(|v| v.as_str())
                {
                    match tag {
                        "catch" if arr.len() >= 3 => {
                            catch_param = arr[1].as_str().map(String::from);
                            catch_body = Some(value_to_stmt(&arr[2])?);
                        }
                        "finally" if arr.len() >= 2 => {
                            finally_body = Some(value_to_stmt(&arr[1])?);
                        }
                        _ => {}
                    }
                }
            }

            Ok(Stmt::try_catch(body, catch_param, catch_body, finally_body))
        }

        "std.fn" => {
            ensure_arity(opcode, args, 3)?;
            let name = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.fn name must be string".into()))?;
            let params = parse_params(&args[1])?;
            let body = value_to_stmt(&args[2])?;
            Ok(Stmt::function(Function::new(name, params, vec![body])))
        }

        "std.import" => {
            if args.len() < 2 {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 2,
                    got: args.len(),
                });
            }
            let source = args[0]
                .as_str()
                .ok_or_else(|| {
                    SExprError::InvalidArgument("std.import source must be string".into())
                })?
                .to_string();
            let mut names = Vec::new();
            if let Value::Array(name_arr) = &args[1] {
                for item in name_arr {
                    match item {
                        Value::String(s) => {
                            names.push(ImportName::named(s.clone()));
                        }
                        Value::Array(parts) if parts.len() == 2 => {
                            let first = parts[0].as_str().unwrap_or("").to_string();
                            let second = parts[1].as_str().unwrap_or("").to_string();
                            if first == "ns" {
                                names.push(ImportName::namespace(second));
                            } else {
                                names.push(ImportName::aliased(first, second));
                            }
                        }
                        _ => {}
                    }
                }
            }
            Ok(Stmt::import(source, names))
        }

        "std.export" => {
            if args.is_empty() {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 1,
                    got: 0,
                });
            }
            let mut names = Vec::new();
            if let Value::Array(name_arr) = &args[0] {
                for item in name_arr {
                    match item {
                        Value::String(s) => {
                            names.push(ExportName::named(s.clone()));
                        }
                        Value::Array(parts) if parts.len() == 2 => {
                            let first = parts[0].as_str().unwrap_or("").to_string();
                            let second = parts[1].as_str().unwrap_or("").to_string();
                            names.push(ExportName::aliased(first, second));
                        }
                        _ => {}
                    }
                }
            }
            let source = args.get(1).and_then(|v| v.as_str()).map(String::from);
            Ok(Stmt::export(names, source))
        }

        "std.class" => {
            if args.len() < 3 {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 3,
                    got: args.len(),
                });
            }
            let name = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.class name must be string".into()))?
                .to_string();
            let extends = args[1].as_str().map(String::from);
            let mut methods = Vec::new();
            if let Value::Array(method_arr) = &args[2] {
                for item in method_arr {
                    if let Value::Array(parts) = item
                        && parts.len() >= 3
                    {
                        let method_name = parts[0].as_str().unwrap_or("").to_string();
                        let params = parse_params(&parts[1])?;
                        let body_stmts = if let Value::Array(body_arr) = &parts[2] {
                            body_arr
                                .iter()
                                .map(value_to_stmt)
                                .collect::<Result<Vec<_>, _>>()?
                        } else {
                            vec![]
                        };
                        methods.push(Method::new(method_name, params, body_stmts));
                    }
                }
            }
            Ok(Stmt::class(name, extends, methods))
        }

        // Expression opcodes - wrap in Stmt::Expr
        _ => {
            let expr = parse_opcode_expr(opcode, args)?;
            Ok(Stmt::expr(expr))
        }
    }
}

fn parse_opcode_expr(opcode: &str, args: &[Value]) -> Result<Expr, SExprError> {
    match opcode {
        // Variable access
        "std.var" => {
            ensure_arity(opcode, args, 1)?;
            let name = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.var name must be string".into()))?;
            Ok(Expr::ident(name))
        }

        // Variable assignment
        "std.set" => {
            ensure_arity(opcode, args, 2)?;
            let name = args[0]
                .as_str()
                .ok_or_else(|| SExprError::InvalidArgument("std.set name must be string".into()))?;
            let value = value_to_expr(&args[1])?;
            Ok(Expr::assign(Expr::ident(name), value))
        }

        // Arithmetic
        "math.add" => binary_op(args, BinaryOp::Add),
        "math.sub" => binary_op(args, BinaryOp::Sub),
        "math.mul" => binary_op(args, BinaryOp::Mul),
        "math.div" => binary_op(args, BinaryOp::Div),
        "math.mod" => binary_op(args, BinaryOp::Mod),
        "math.neg" => unary_op(args, UnaryOp::Neg),

        // Comparison
        "bool.eq" => binary_op(args, BinaryOp::Eq),
        "bool.neq" => binary_op(args, BinaryOp::Ne),
        "bool.lt" => binary_op(args, BinaryOp::Lt),
        "bool.lte" => binary_op(args, BinaryOp::Le),
        "bool.gt" => binary_op(args, BinaryOp::Gt),
        "bool.gte" => binary_op(args, BinaryOp::Ge),

        // Logical
        "bool.and" => binary_op(args, BinaryOp::And),
        "bool.or" => binary_op(args, BinaryOp::Or),
        "bool.not" => unary_op(args, UnaryOp::Not),

        // String
        "str.concat" => binary_op(args, BinaryOp::Concat),

        // Object/list operations
        "obj.get" => {
            ensure_arity(opcode, args, 2)?;
            let obj = value_to_expr(&args[0])?;
            let prop = value_to_expr(&args[1])?;
            Ok(Expr::Member {
                object: Box::new(obj),
                property: Box::new(prop),
                computed: true,
                span: None,
            })
        }

        "obj.set" => {
            ensure_arity(opcode, args, 3)?;
            let obj = value_to_expr(&args[0])?;
            let prop = value_to_expr(&args[1])?;
            let value = value_to_expr(&args[2])?;
            let target = Expr::Member {
                object: Box::new(obj),
                property: Box::new(prop),
                computed: true,
                span: None,
            };
            Ok(Expr::assign(target, value))
        }

        "obj.new" => {
            let pairs: Result<Vec<(String, Expr)>, _> = args
                .iter()
                .map(|pair| {
                    let arr = pair.as_array().ok_or_else(|| {
                        SExprError::InvalidArgument("obj.new pair must be array".into())
                    })?;
                    if arr.len() != 2 {
                        return Err(SExprError::InvalidArgument(
                            "obj.new pair must have 2 elements".into(),
                        ));
                    }
                    let key = arr[0].as_str().ok_or_else(|| {
                        SExprError::InvalidArgument("obj.new key must be string".into())
                    })?;
                    let val = value_to_expr(&arr[1])?;
                    Ok((key.to_string(), val))
                })
                .collect();
            Ok(Expr::object(pairs?))
        }

        "list.new" => {
            let items: Result<Vec<Expr>, _> = args.iter().map(value_to_expr).collect();
            Ok(Expr::array(items?))
        }

        "list.get" => {
            ensure_arity(opcode, args, 2)?;
            let arr = value_to_expr(&args[0])?;
            let idx = value_to_expr(&args[1])?;
            Ok(Expr::index(arr, idx))
        }

        // Lambda
        "std.lambda" => {
            ensure_arity(opcode, args, 2)?;
            let params = parse_params(&args[0])?;
            let body = value_to_stmt(&args[1])?;
            Ok(Expr::Function(Box::new(Function::anonymous(
                params,
                vec![body],
            ))))
        }

        // std.this
        "std.this" => Ok(Expr::ident("this")),

        // std.apply - dynamic function call
        "std.apply" => {
            if args.is_empty() {
                return Err(SExprError::WrongArity {
                    opcode: opcode.into(),
                    expected: 1,
                    got: 0,
                });
            }
            let callee = value_to_expr(&args[0])?;
            let call_args: Result<Vec<Expr>, _> = args[1..].iter().map(value_to_expr).collect();
            Ok(Expr::call(callee, call_args?))
        }

        // Generic function call (namespace.function)
        _ => {
            // Treat as a function call: opcode(args...)
            let parts: Vec<&str> = opcode.split('.').collect();
            let callee = if parts.len() == 1 {
                Expr::ident(parts[0])
            } else {
                // Build member expression chain: a.b.c
                let mut expr = Expr::ident(parts[0]);
                for part in &parts[1..] {
                    expr = Expr::member(expr, *part);
                }
                expr
            };
            let call_args: Result<Vec<Expr>, _> = args.iter().map(value_to_expr).collect();
            Ok(Expr::call(callee, call_args?))
        }
    }
}

fn binary_op(args: &[Value], op: BinaryOp) -> Result<Expr, SExprError> {
    if args.len() != 2 {
        return Err(SExprError::WrongArity {
            opcode: format!("{:?}", op),
            expected: 2,
            got: args.len(),
        });
    }
    let left = value_to_expr(&args[0])?;
    let right = value_to_expr(&args[1])?;
    Ok(Expr::binary(left, op, right))
}

fn unary_op(args: &[Value], op: UnaryOp) -> Result<Expr, SExprError> {
    if args.len() != 1 {
        return Err(SExprError::WrongArity {
            opcode: format!("{:?}", op),
            expected: 1,
            got: args.len(),
        });
    }
    Ok(Expr::unary(op, value_to_expr(&args[0])?))
}

fn parse_params(value: &Value) -> Result<Vec<Param>, SExprError> {
    let arr = value
        .as_array()
        .ok_or_else(|| SExprError::InvalidArgument("params must be array".into()))?;
    arr.iter()
        .map(|v| {
            v.as_str()
                .map(Param::new)
                .ok_or_else(|| SExprError::InvalidArgument("param must be string".into()))
        })
        .collect()
}

fn ensure_arity(opcode: &str, args: &[Value], expected: usize) -> Result<(), SExprError> {
    if args.len() != expected {
        Err(SExprError::WrongArity {
            opcode: opcode.into(),
            expected,
            got: args.len(),
        })
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_simple_let() -> Result<(), SExprError> {
        let sexpr = json!(["std.let", "x", 42]);
        let program = from_sexpr(&sexpr)?;
        assert_eq!(program.body.len(), 1);
        match &program.body[0] {
            Stmt::Let { name, init, .. } => {
                assert_eq!(name, "x");
                assert!(init.is_some());
            }
            _ => panic!("expected Let"),
        }
        Ok(())
    }

    #[test]
    fn test_binary_expr() -> Result<(), SExprError> {
        let sexpr = json!(["math.add", 1, 2]);
        let program = from_sexpr(&sexpr)?;
        match &program.body[0] {
            Stmt::Expr(Expr::Binary { op, .. }) => {
                assert_eq!(*op, BinaryOp::Add);
            }
            _ => panic!("expected Binary"),
        }
        Ok(())
    }

    #[test]
    fn test_function_call() -> Result<(), SExprError> {
        let sexpr = json!(["console.log", "hello"]);
        let program = from_sexpr(&sexpr)?;
        match &program.body[0] {
            Stmt::Expr(Expr::Call { callee, args, .. }) => {
                assert_eq!(args.len(), 1);
                // Callee should be console.log member expression
                match callee.as_ref() {
                    Expr::Member { .. } => {}
                    _ => panic!("expected Member expression"),
                }
            }
            _ => panic!("expected Call"),
        }
        Ok(())
    }
}