s2s2lua 0.1.1

Transpile Stranded II .s2s scripts to Lua for OpenStranded
Documentation
// s2s2lua – transpile Stranded II .s2s scripts to Lua for OpenStranded
// Copyright (C) 2025  s2s2lua contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::*;

impl LuaGenerator {
    // ── Expression to Lua ─────────────────────────────────

    pub(super) fn expr_to_lua(&self, expr: &Expr) -> String {
        match expr {
            Expr::String(s) => {
                let escaped = s
                    .replace('\\', "\\\\")
                    .replace('"', "\\\"")
                    .replace('\n', "\\n")
                    .replace('\r', "\\r");
                format!("\"{}\"", escaped)
            }
            Expr::Number(n) => n.clone(),
            Expr::Variable(var) => var.clone(),
            Expr::Condition { left, op, right } => {
                format!(
                    "({} {} {})",
                    self.expr_to_lua(left),
                    self.condition_op_to_lua(op),
                    self.expr_to_lua(right)
                )
            }
            Expr::Logical { left, op, right } => match op {
                LogicalOp::And => format!(
                    "({} and {})",
                    self.expr_to_lua(left),
                    self.expr_to_lua(right)
                ),
                LogicalOp::Or => format!(
                    "({} or {})",
                    self.expr_to_lua(left),
                    self.expr_to_lua(right)
                ),
                LogicalOp::Xor => format!(
                    "({} ~= false) ~= ({} ~= false)",
                    self.expr_to_lua(left),
                    self.expr_to_lua(right)
                ),
            },
            Expr::CommandCall { name, args } => {
                if self.inside_loop && name == "loop_id" && args.is_empty() {
                    return "id".to_string();
                }
                let args_str: Vec<String> = args.iter().map(|a| self.expr_to_lua(a)).collect();
                format!(
                    "{}{}({})",
                    self.options.command_prefix,
                    self.escape_command_name(name),
                    args_str.join(", ")
                )
            }
            Expr::Negate(inner) => {
                format!("(-{})", self.expr_to_lua(inner))
            }
            Expr::Arithmetic { left, op, right } => {
                let op_str = match op {
                    ArithOp::Add => " + ",
                    ArithOp::Sub => " - ",
                    ArithOp::Mul => " * ",
                    ArithOp::Div => " / ",
                };
                format!(
                    "({}{}{})",
                    self.expr_to_lua(left),
                    op_str,
                    self.expr_to_lua(right)
                )
            }
        }
    }

    fn condition_op_to_lua<'a>(&self, op: &'a str) -> &'a str {
        match op {
            "==" => "==",
            "!=" => "~=",
            "<" => "<",
            ">" => ">",
            "<=" => "<=",
            ">=" => ">=",
            _ => op,
        }
    }
}