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:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -770,7 +770,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lux"
|
name = "lux"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lsp-server",
|
"lsp-server",
|
||||||
"lsp-types",
|
"lsp-types",
|
||||||
|
|||||||
@@ -622,6 +622,7 @@ pub enum BinaryOp {
|
|||||||
Or,
|
Or,
|
||||||
// Other
|
// Other
|
||||||
Pipe, // |>
|
Pipe, // |>
|
||||||
|
Concat, // ++
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BinaryOp {
|
impl fmt::Display for BinaryOp {
|
||||||
@@ -641,6 +642,7 @@ impl fmt::Display for BinaryOp {
|
|||||||
BinaryOp::And => write!(f, "&&"),
|
BinaryOp::And => write!(f, "&&"),
|
||||||
BinaryOp::Or => write!(f, "||"),
|
BinaryOp::Or => write!(f, "||"),
|
||||||
BinaryOp::Pipe => write!(f, "|>"),
|
BinaryOp::Pipe => write!(f, "|>"),
|
||||||
|
BinaryOp::Concat => write!(f, "++"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -730,10 +730,10 @@ impl CBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for string concatenation - use lux_string_concat instead of +
|
// Check for string concatenation - use lux_string_concat instead of +
|
||||||
if matches!(op, BinaryOp::Add) {
|
if matches!(op, BinaryOp::Add | BinaryOp::Concat) {
|
||||||
let left_is_string = self.infer_expr_type(left).as_deref() == Some("LuxString");
|
let left_is_string = self.infer_expr_type(left).as_deref() == Some("LuxString");
|
||||||
let right_is_string = self.infer_expr_type(right).as_deref() == Some("LuxString");
|
let right_is_string = self.infer_expr_type(right).as_deref() == Some("LuxString");
|
||||||
if left_is_string || right_is_string {
|
if left_is_string || right_is_string || matches!(op, BinaryOp::Concat) {
|
||||||
return Ok(format!("lux_string_concat({}, {})", l, r));
|
return Ok(format!("lux_string_concat({}, {})", l, r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2839,8 +2839,18 @@ impl CBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String concatenation for ++ and +
|
||||||
|
if matches!(op, BinaryOp::Add | BinaryOp::Concat) {
|
||||||
|
let left_is_string = self.infer_expr_type(left).as_deref() == Some("LuxString");
|
||||||
|
let right_is_string = self.infer_expr_type(right).as_deref() == Some("LuxString");
|
||||||
|
if left_is_string || right_is_string || matches!(op, BinaryOp::Concat) {
|
||||||
|
return Ok(format!("lux_string_concat({}, {})", l, r));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let op_str = match op {
|
let op_str = match op {
|
||||||
BinaryOp::Add => "+",
|
BinaryOp::Add => "+",
|
||||||
|
BinaryOp::Concat => unreachable!("handled above"),
|
||||||
BinaryOp::Sub => "-",
|
BinaryOp::Sub => "-",
|
||||||
BinaryOp::Mul => "*",
|
BinaryOp::Mul => "*",
|
||||||
BinaryOp::Div => "/",
|
BinaryOp::Div => "/",
|
||||||
|
|||||||
@@ -954,12 +954,17 @@ impl JsBackend {
|
|||||||
let r = self.emit_expr(right)?;
|
let r = self.emit_expr(right)?;
|
||||||
|
|
||||||
// Check for string concatenation
|
// Check for string concatenation
|
||||||
if matches!(op, BinaryOp::Add) {
|
if matches!(op, BinaryOp::Add | BinaryOp::Concat) {
|
||||||
if self.is_string_expr(left) || self.is_string_expr(right) {
|
if self.is_string_expr(left) || self.is_string_expr(right) {
|
||||||
return Ok(format!("({} + {})", l, r));
|
return Ok(format!("({} + {})", l, r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ++ on lists: use .concat()
|
||||||
|
if matches!(op, BinaryOp::Concat) {
|
||||||
|
return Ok(format!("{}.concat({})", l, r));
|
||||||
|
}
|
||||||
|
|
||||||
let op_str = match op {
|
let op_str = match op {
|
||||||
BinaryOp::Add => "+",
|
BinaryOp::Add => "+",
|
||||||
BinaryOp::Sub => "-",
|
BinaryOp::Sub => "-",
|
||||||
@@ -974,6 +979,7 @@ impl JsBackend {
|
|||||||
BinaryOp::Ge => ">=",
|
BinaryOp::Ge => ">=",
|
||||||
BinaryOp::And => "&&",
|
BinaryOp::And => "&&",
|
||||||
BinaryOp::Or => "||",
|
BinaryOp::Or => "||",
|
||||||
|
BinaryOp::Concat => unreachable!("handled above"),
|
||||||
BinaryOp::Pipe => {
|
BinaryOp::Pipe => {
|
||||||
// Pipe operator: x |> f becomes f(x)
|
// Pipe operator: x |> f becomes f(x)
|
||||||
return Ok(format!("{}({})", r, l));
|
return Ok(format!("{}({})", r, l));
|
||||||
@@ -2342,7 +2348,7 @@ impl JsBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::BinaryOp { op, left, right, .. } => {
|
Expr::BinaryOp { op, left, right, .. } => {
|
||||||
matches!(op, BinaryOp::Add)
|
matches!(op, BinaryOp::Add | BinaryOp::Concat)
|
||||||
&& (self.is_string_expr(left) || self.is_string_expr(right))
|
&& (self.is_string_expr(left) || self.is_string_expr(right))
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
|
|||||||
@@ -753,6 +753,7 @@ impl Formatter {
|
|||||||
BinaryOp::Ge => ">=",
|
BinaryOp::Ge => ">=",
|
||||||
BinaryOp::And => "&&",
|
BinaryOp::And => "&&",
|
||||||
BinaryOp::Or => "||",
|
BinaryOp::Or => "||",
|
||||||
|
BinaryOp::Concat => "++",
|
||||||
BinaryOp::Pipe => "|>",
|
BinaryOp::Pipe => "|>",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1115,11 +1115,50 @@ impl Interpreter {
|
|||||||
/// Execute a program
|
/// Execute a program
|
||||||
pub fn run(&mut self, program: &Program) -> Result<Value, RuntimeError> {
|
pub fn run(&mut self, program: &Program) -> Result<Value, RuntimeError> {
|
||||||
let mut last_value = Value::Unit;
|
let mut last_value = Value::Unit;
|
||||||
|
let mut has_main_let = false;
|
||||||
|
|
||||||
for decl in &program.declarations {
|
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)?;
|
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)
|
Ok(last_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1599,6 +1638,18 @@ impl Interpreter {
|
|||||||
span: Some(span),
|
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) {
|
BinaryOp::Sub => match (left, right) {
|
||||||
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
|
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
|
||||||
(Value::Float(a), Value::Float(b)) => Ok(Value::Float(a - b)),
|
(Value::Float(a), Value::Float(b)) => Ok(Value::Float(a - b)),
|
||||||
|
|||||||
11
src/lexer.rs
11
src/lexer.rs
@@ -70,6 +70,7 @@ pub enum TokenKind {
|
|||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
Plus, // +
|
Plus, // +
|
||||||
|
PlusPlus, // ++
|
||||||
Minus, // -
|
Minus, // -
|
||||||
Star, // *
|
Star, // *
|
||||||
Slash, // /
|
Slash, // /
|
||||||
@@ -160,6 +161,7 @@ impl fmt::Display for TokenKind {
|
|||||||
TokenKind::True => write!(f, "true"),
|
TokenKind::True => write!(f, "true"),
|
||||||
TokenKind::False => write!(f, "false"),
|
TokenKind::False => write!(f, "false"),
|
||||||
TokenKind::Plus => write!(f, "+"),
|
TokenKind::Plus => write!(f, "+"),
|
||||||
|
TokenKind::PlusPlus => write!(f, "++"),
|
||||||
TokenKind::Minus => write!(f, "-"),
|
TokenKind::Minus => write!(f, "-"),
|
||||||
TokenKind::Star => write!(f, "*"),
|
TokenKind::Star => write!(f, "*"),
|
||||||
TokenKind::Slash => write!(f, "/"),
|
TokenKind::Slash => write!(f, "/"),
|
||||||
@@ -268,7 +270,14 @@ impl<'a> Lexer<'a> {
|
|||||||
|
|
||||||
let kind = match c {
|
let kind = match c {
|
||||||
// Single-character tokens
|
// Single-character tokens
|
||||||
'+' => TokenKind::Plus,
|
'+' => {
|
||||||
|
if self.peek() == Some('+') {
|
||||||
|
self.advance();
|
||||||
|
TokenKind::PlusPlus
|
||||||
|
} else {
|
||||||
|
TokenKind::Plus
|
||||||
|
}
|
||||||
|
}
|
||||||
'*' => TokenKind::Star,
|
'*' => TokenKind::Star,
|
||||||
'%' => TokenKind::Percent,
|
'%' => TokenKind::Percent,
|
||||||
'(' => TokenKind::LParen,
|
'(' => TokenKind::LParen,
|
||||||
|
|||||||
@@ -1558,6 +1558,7 @@ impl Parser {
|
|||||||
loop {
|
loop {
|
||||||
let op = match self.peek_kind() {
|
let op = match self.peek_kind() {
|
||||||
TokenKind::Plus => BinaryOp::Add,
|
TokenKind::Plus => BinaryOp::Add,
|
||||||
|
TokenKind::PlusPlus => BinaryOp::Concat,
|
||||||
TokenKind::Minus => BinaryOp::Sub,
|
TokenKind::Minus => BinaryOp::Sub,
|
||||||
_ => break,
|
_ => break,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1804,6 +1804,29 @@ impl TypeChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BinaryOp::Concat => {
|
||||||
|
// Concat (++) supports strings and lists
|
||||||
|
if let Err(e) = unify_with_env(&left_type, &right_type, &self.env) {
|
||||||
|
self.errors.push(TypeError {
|
||||||
|
message: format!("Operands of '++' must have same type: {}", e),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
match &left_type {
|
||||||
|
Type::String | Type::List(_) | Type::Var(_) => left_type,
|
||||||
|
_ => {
|
||||||
|
self.errors.push(TypeError {
|
||||||
|
message: format!(
|
||||||
|
"Operator '++' requires String or List operands, got {}",
|
||||||
|
left_type
|
||||||
|
),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
Type::Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
|
BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
|
||||||
// Arithmetic: both operands must be same numeric type
|
// Arithmetic: both operands must be same numeric type
|
||||||
if let Err(e) = unify_with_env(&left_type, &right_type, &self.env) {
|
if let Err(e) = unify_with_env(&left_type, &right_type, &self.env) {
|
||||||
|
|||||||
Reference in New Issue
Block a user