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 {
    pub(super) fn emit_command(&mut self, name: &str, args: &[Expr], suppress: bool) -> S2sResult<()> {
        if name.is_empty() && args.len() == 1 {
            return Ok(());
        }

        self.emit_indent();

        let args_str: Vec<String> = args.iter().map(|a| self.expr_to_lua(a)).collect();
        let call = format!(
            "{}{}({})",
            self.options.command_prefix,
            self.escape_command_name(name),
            args_str.join(", ")
        );

        if suppress {
            self.emit(&format!(
                "xpcall(function() {} end, function() end)\n",
                call
            ));
        } else {
            self.emit(&format!("{}\n", call));
        }

        Ok(())
    }

    pub(super) fn emit_assign(&mut self, var: &str, value: &Expr) -> S2sResult<()> {
        self.emit_indent();
        let val_str = self.expr_to_lua(value);
        self.emit(&format!("local {} = {}\n", var, val_str));
        Ok(())
    }

    pub(super) fn emit_compound_assign(&mut self, var: &str, op: &AssignOp, value: &Expr) -> S2sResult<()> {
        self.emit_indent();
        let val_str = self.expr_to_lua(value);
        let lua_op = match op {
            AssignOp::Add => "+",
            AssignOp::Sub => "-",
        };
        self.emit(&format!("{} = {} {} {}\n", var, var, lua_op, val_str));
        Ok(())
    }

    pub(super) fn emit_inc_dec(&mut self, var: &str, inc: bool) -> S2sResult<()> {
        self.emit_indent();
        if inc {
            self.emit(&format!("{} = {} + 1\n", var, var));
        } else {
            self.emit(&format!("{} = {} - 1\n", var, var));
        }
        Ok(())
    }

    pub(super) fn emit_exit(&mut self) -> S2sResult<()> {
        self.emit_indent();
        if self.inside_loop {
            self.emit("return true\n");
        } else {
            self.emit("return\n");
        }
        Ok(())
    }

    pub(super) fn emit_skip(&mut self) -> S2sResult<()> {
        self.emit_indent();
        self.emit(&format!("{}skip()\n", self.options.command_prefix));
        Ok(())
    }

    pub(super) fn emit_skipevent(&mut self) -> S2sResult<()> {
        self.emit_indent();
        self.emit(&format!("{}skipevent()\n", self.options.command_prefix));
        Ok(())
    }

    pub(super) fn emit_comment(&mut self, text: &str) -> S2sResult<()> {
        let trimmed = text.trim();
        if let Some(after_slashes) = trimmed.strip_prefix("//") {
            self.emit_indent();
            self.emit(&format!("--{}\n", after_slashes));
        } else if let Some(inner) = trimmed
            .strip_prefix("/*")
            .and_then(|s| s.strip_suffix("*/"))
        {
            let inner = inner.trim();
            self.emit_indent();
            if inner.contains('\n') {
                self.emit("--[[\n");
                for line in inner.lines() {
                    self.emit_indent();
                    self.emit(line.trim());
                    self.emit("\n");
                }
                self.emit_indent();
                self.emit("]]\n");
            } else {
                self.emit(&format!("--[[ {} ]]\n", inner));
            }
        }
        Ok(())
    }
}