feat: add ++ concat operator and auto-invoke main
BUG-004: Add ++ operator for string and list concatenation across all backends (interpreter, C, JS) with type checking and formatting support. BUG-001: Auto-invoke top-level `let main = fn () => ...` when main is a zero-parameter function, instead of just printing the function value. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1115,11 +1115,50 @@ impl Interpreter {
|
||||
/// Execute a program
|
||||
pub fn run(&mut self, program: &Program) -> Result<Value, RuntimeError> {
|
||||
let mut last_value = Value::Unit;
|
||||
let mut has_main_let = false;
|
||||
|
||||
for decl in &program.declarations {
|
||||
// Track if there's a top-level `let main = ...`
|
||||
if let Declaration::Let(let_decl) = decl {
|
||||
if let_decl.name.name == "main" {
|
||||
has_main_let = true;
|
||||
}
|
||||
}
|
||||
last_value = self.eval_declaration(decl)?;
|
||||
}
|
||||
|
||||
// Auto-invoke main if it was defined as a let binding with a function value
|
||||
if has_main_let {
|
||||
if let Some(main_val) = self.global_env.get("main") {
|
||||
if let Value::Function(ref closure) = main_val {
|
||||
if closure.params.is_empty() {
|
||||
let span = Span { start: 0, end: 0 };
|
||||
let mut result = self.eval_call(main_val.clone(), vec![], span)?;
|
||||
// Trampoline loop
|
||||
loop {
|
||||
match result {
|
||||
EvalResult::Value(v) => {
|
||||
last_value = v;
|
||||
break;
|
||||
}
|
||||
EvalResult::Effect(req) => {
|
||||
last_value = self.handle_effect(req)?;
|
||||
break;
|
||||
}
|
||||
EvalResult::TailCall { func, args, span } => {
|
||||
result = self.eval_call(func, args, span)?;
|
||||
}
|
||||
EvalResult::Resume(v) => {
|
||||
last_value = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(last_value)
|
||||
}
|
||||
|
||||
@@ -1599,6 +1638,18 @@ impl Interpreter {
|
||||
span: Some(span),
|
||||
}),
|
||||
},
|
||||
BinaryOp::Concat => match (left, right) {
|
||||
(Value::String(a), Value::String(b)) => Ok(Value::String(a + &b)),
|
||||
(Value::List(a), Value::List(b)) => {
|
||||
let mut result = a;
|
||||
result.extend(b);
|
||||
Ok(Value::List(result))
|
||||
}
|
||||
(l, r) => Err(RuntimeError {
|
||||
message: format!("Cannot concatenate {} and {}", l.type_name(), r.type_name()),
|
||||
span: Some(span),
|
||||
}),
|
||||
},
|
||||
BinaryOp::Sub => match (left, right) {
|
||||
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
|
||||
(Value::Float(a), Value::Float(b)) => Ok(Value::Float(a - b)),
|
||||
|
||||
Reference in New Issue
Block a user