use std::collections::{HashMap, HashSet};
use crate::{
backends::Backend,
compiler::diagnostics::CompileError,
ir::{Class, Command, Expr, ExprKind, Field, Function, Method, Place, Program, Stmt, Type},
};
#[derive(Debug, Default, Clone, Copy)]
pub struct Bash;
pub type BashBackend = Bash;
impl Backend for Bash {
fn emit(&self, program: &Program) -> Result<String, CompileError> {
validate_program(program)?;
let metadata = Metadata::new(program);
let mut output =
String::from("#!/usr/bin/env bash\n# Generated by tsh. Requires Bash 3.2 or newer.\n");
if !program.functions.is_empty() || !program.classes.is_empty() {
output.push_str("__tsh_backend_result=''\n");
}
if !program.classes.is_empty() {
output.push_str("__tsh_backend_next_object_id=0\n");
}
for name in &metadata.global_order {
if metadata.guarded_globals.contains(name) {
output.push_str(&format!("{}=0\n", ready_flag(name)));
}
}
if !metadata.guarded_globals.is_empty() {
output.push('\n');
}
for class in &program.classes {
for field in &class.fields {
output.push_str(&format!(
"declare -a {}\n",
field_array(&class.name, &field.name)
));
}
}
if !program.classes.is_empty() {
output.push('\n');
}
let mut next_temp = 0usize;
for function in &program.functions {
output.push_str(&emit_function(function, None, &metadata, &mut next_temp)?);
output.push('\n');
}
for class in &program.classes {
output.push_str(&emit_constructor(class, &metadata, &mut next_temp)?);
output.push('\n');
for method in &class.methods {
output.push_str(&emit_method(class, method, &metadata, &mut next_temp)?);
output.push('\n');
}
}
let mut top = Writer::new(&metadata, &mut next_temp, ReturnMode::TopLevel);
top.emit_statements(&program.body)?;
output.push_str(&top.code);
Ok(output)
}
}
#[derive(Debug, Clone)]
struct Value {
word: String,
arithmetic: Option<String>,
name: Option<String>,
ty: Type,
}
impl Value {
fn new(word: impl Into<String>, arithmetic: Option<String>, ty: Type) -> Self {
Self {
word: word.into(),
arithmetic,
name: None,
ty,
}
}
fn named(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
fn arithmetic(&self) -> Result<&str, CompileError> {
self.arithmetic.as_deref().ok_or_else(|| {
CompileError::plain("Bash backend expected a numeric or boolean IR value")
})
}
}
#[derive(Debug, Clone)]
struct Metadata<'a> {
classes: HashMap<&'a str, &'a Class>,
methods: HashMap<(&'a str, &'a str), bool>,
global_order: Vec<&'a str>,
guarded_globals: HashSet<&'a str>,
}
impl<'a> Metadata<'a> {
fn new(program: &'a Program) -> Self {
let mut classes = HashMap::new();
let mut methods = HashMap::new();
let mut global_names = HashSet::new();
let mut global_order = Vec::new();
for class in &program.classes {
classes.insert(class.name.as_str(), class);
for method in &class.methods {
methods.insert(
(class.name.as_str(), method.function.name.as_str()),
method.is_static,
);
}
}
for statement in &program.body {
if let Stmt::Let { name, .. } = statement
&& global_names.insert(name.as_str())
{
global_order.push(name.as_str());
}
}
let mut referenced_globals = HashSet::new();
for function in &program.functions {
collect_statement_variables(&function.body, &mut referenced_globals);
}
for class in &program.classes {
for field in &class.fields {
if let Some(initial) = &field.initial {
collect_expression_variables(initial, &mut referenced_globals);
}
}
collect_statement_variables(&class.constructor.body, &mut referenced_globals);
for method in &class.methods {
collect_statement_variables(&method.function.body, &mut referenced_globals);
}
}
let guarded_globals: HashSet<&str> = global_names
.iter()
.copied()
.filter(|name| referenced_globals.contains(*name))
.collect();
global_order.retain(|name| guarded_globals.contains(name));
Self {
classes,
methods,
global_order,
guarded_globals,
}
}
fn class(&self, name: &str) -> Result<&'a Class, CompileError> {
self.classes
.get(name)
.copied()
.ok_or_else(|| CompileError::plain(format!("unknown class `{name}` in Bash IR")))
}
fn field(&self, class: &str, field: &str) -> Result<&'a Field, CompileError> {
self.class(class)?
.fields
.iter()
.find(|candidate| candidate.name == field)
.ok_or_else(|| {
CompileError::plain(format!("unknown field `{class}.{field}` in Bash IR"))
})
}
fn is_static(&self, class: &str, method: &str) -> Result<bool, CompileError> {
self.methods.get(&(class, method)).copied().ok_or_else(|| {
CompileError::plain(format!("unknown method `{class}.{method}` in Bash IR"))
})
}
}
fn collect_statement_variables(statements: &[Stmt], names: &mut HashSet<String>) {
for statement in statements {
match statement {
Stmt::Let { value, .. } | Stmt::Expr(value) | Stmt::Return(Some(value)) => {
collect_expression_variables(value, names);
}
Stmt::Assign { place, value } => {
match place {
Place::Variable(name) => {
names.insert(name.clone());
}
Place::Field { object, .. } => collect_expression_variables(object, names),
}
collect_expression_variables(value, names);
}
Stmt::If {
condition,
then_body,
else_body,
} => {
collect_expression_variables(condition, names);
collect_statement_variables(then_body, names);
collect_statement_variables(else_body, names);
}
Stmt::While { condition, body } => {
collect_expression_variables(condition, names);
collect_statement_variables(body, names);
}
Stmt::For {
init,
condition,
update,
body,
} => {
collect_statement_variables(init, names);
if let Some(condition) = condition {
collect_expression_variables(condition, names);
}
collect_statement_variables(update, names);
collect_statement_variables(body, names);
}
Stmt::Command { args, .. } => {
for argument in args {
collect_expression_variables(argument, names);
}
}
Stmt::Block(body) => collect_statement_variables(body, names),
Stmt::Return(None) | Stmt::Break | Stmt::Continue | Stmt::Raw(_) | Stmt::Comment(_) => {
}
}
}
}
fn collect_expression_variables(expression: &Expr, names: &mut HashSet<String>) {
match &expression.kind {
ExprKind::Variable(name) => {
names.insert(name.clone());
}
ExprKind::Template(parts) => {
for part in parts {
collect_expression_variables(part, names);
}
}
ExprKind::Binary { left, right, .. } => {
collect_expression_variables(left, names);
collect_expression_variables(right, names);
}
ExprKind::Unary { value, .. } | ExprKind::Env(value) => {
collect_expression_variables(value, names);
}
ExprKind::Call { args, .. } | ExprKind::New { args, .. } => {
for argument in args {
collect_expression_variables(argument, names);
}
}
ExprKind::Field { object, .. } => collect_expression_variables(object, names),
ExprKind::Method { object, args, .. } => {
if let Some(object) = object {
collect_expression_variables(object, names);
}
for argument in args {
collect_expression_variables(argument, names);
}
}
ExprKind::String(_) | ExprKind::Number(_) | ExprKind::Boolean(_) => {}
}
}
#[derive(Debug, Clone)]
enum ReturnMode {
TopLevel,
Function(Type),
Constructor,
}
#[derive(Debug, Clone)]
enum LoopMode {
While,
For(Vec<Stmt>),
}
struct Writer<'a, 'n> {
code: String,
indent: usize,
metadata: &'a Metadata<'a>,
next_temp: &'n mut usize,
temps: Vec<String>,
loops: Vec<LoopMode>,
return_mode: ReturnMode,
}
impl<'a, 'n> Writer<'a, 'n> {
fn new(metadata: &'a Metadata<'a>, next_temp: &'n mut usize, return_mode: ReturnMode) -> Self {
Self {
code: String::new(),
indent: 0,
metadata,
next_temp,
temps: Vec::new(),
loops: Vec::new(),
return_mode,
}
}
fn line(&mut self, line: &str) {
for _ in 0..self.indent {
self.code.push_str(" ");
}
self.code.push_str(line);
self.code.push('\n');
}
fn fresh_temp(&mut self) -> String {
let name = format!("__tsh_backend_tmp_{}", *self.next_temp);
*self.next_temp += 1;
self.temps.push(name.clone());
name
}
fn emit_statements(&mut self, statements: &[Stmt]) -> Result<(), CompileError> {
for statement in statements {
self.emit_statement(statement)?;
}
Ok(())
}
fn emit_statement(&mut self, statement: &Stmt) -> Result<(), CompileError> {
match statement {
Stmt::Let { name, value } => {
let value = self.emit_expr(value)?;
self.line(&format!("{name}={}", value.word));
if matches!(&self.return_mode, ReturnMode::TopLevel)
&& self.metadata.guarded_globals.contains(name.as_str())
{
self.line(&format!("{}=1", ready_flag(name)));
}
}
Stmt::Assign { place, value } => self.emit_assignment(place, value)?,
Stmt::Expr(expression) => {
let _ = self.emit_expr(expression)?;
}
Stmt::If {
condition,
then_body,
else_body,
} => {
let condition = self.emit_expr(condition)?;
self.line(&format!("if (( {} != 0 )); then", condition.arithmetic()?));
self.indent += 1;
self.emit_control_body(then_body)?;
self.indent -= 1;
if else_body.is_empty() {
self.line("fi");
} else {
self.line("else");
self.indent += 1;
self.emit_control_body(else_body)?;
self.indent -= 1;
self.line("fi");
}
}
Stmt::While { condition, body } => {
self.line("while :; do");
self.indent += 1;
let condition = self.emit_expr(condition)?;
self.line(&format!(
"if (( {} == 0 )); then break; fi",
condition.arithmetic()?
));
self.loops.push(LoopMode::While);
self.emit_control_body(body)?;
self.loops.pop();
self.indent -= 1;
self.line("done");
}
Stmt::For {
init,
condition,
update,
body,
} => {
self.emit_statements(init)?;
self.line("while :; do");
self.indent += 1;
if let Some(condition) = condition {
let condition = self.emit_expr(condition)?;
self.line(&format!(
"if (( {} == 0 )); then break; fi",
condition.arithmetic()?
));
}
self.loops.push(LoopMode::For(update.clone()));
self.emit_control_body(body)?;
self.loops.pop();
self.emit_statements(update)?;
self.indent -= 1;
self.line("done");
}
Stmt::Return(value) => self.emit_return(value.as_ref())?,
Stmt::Break => {
if self.loops.is_empty() {
return Err(CompileError::plain(
"`break` is not inside a loop in Bash IR",
));
}
self.line("break");
}
Stmt::Continue => {
let mode = self.loops.last().cloned().ok_or_else(|| {
CompileError::plain("`continue` is not inside a loop in Bash IR")
})?;
if let LoopMode::For(update) = mode {
self.emit_statements(&update)?;
}
self.line("continue");
}
Stmt::Command {
kind,
args,
recursive,
} => self.emit_command(*kind, args, *recursive)?,
Stmt::Raw(raw) => {
self.code.push_str(raw);
if !raw.ends_with('\n') {
self.code.push('\n');
}
}
Stmt::Comment(comment) => self.emit_comment(comment),
Stmt::Block(body) => self.emit_statements(body)?,
}
Ok(())
}
fn emit_comment(&mut self, comment: &str) {
let lines = crate::backends::shell_comment_lines(comment);
if lines.is_empty() {
self.line("#");
return;
}
for text in lines {
self.line(&format!("# {text}"));
}
}
fn emit_control_body(&mut self, statements: &[Stmt]) -> Result<(), CompileError> {
let start = self.code.len();
self.emit_statements(statements)?;
let body = &self.code[start..];
let has_command = body.lines().any(|line| {
let line = line.trim();
!line.is_empty() && !line.starts_with('#')
});
if !has_command {
self.line(":");
}
Ok(())
}
fn emit_global_ready_check(&mut self, name: &str) {
let flag = ready_flag(name);
self.line(&format!("if (( {flag} == 0 )); then"));
self.indent += 1;
self.line(&format!(
"printf '%s\\n' {} >&2",
bash_quote(&format!(
"tsh: global `{name}` accessed before initialization"
))
));
self.line("exit 1");
self.indent -= 1;
self.line("fi");
}
fn emit_assignment(&mut self, place: &Place, expression: &Expr) -> Result<(), CompileError> {
match place {
Place::Variable(name) => {
if self.metadata.guarded_globals.contains(name.as_str())
&& !matches!(&self.return_mode, ReturnMode::TopLevel)
{
self.emit_global_ready_check(name);
}
let value = self.emit_expr(expression)?;
self.line(&format!("{name}={}", value.word));
}
Place::Field {
object,
class,
field,
} => {
self.metadata.field(class, field)?;
let object_value = self.emit_expr(object)?;
let object = self.capture_if_variable(object, object_value);
let value = self.emit_expr(expression)?;
self.line(&format!(
"{}[${}]={}",
field_array(class, field),
object.arithmetic()?,
value.word
));
}
}
Ok(())
}
fn emit_return(&mut self, expression: Option<&Expr>) -> Result<(), CompileError> {
match self.return_mode.clone() {
ReturnMode::TopLevel => Err(CompileError::plain(
"`return` is not valid at top level in Bash IR",
)),
ReturnMode::Constructor => {
if let Some(expression) = expression {
if !matches!(expression.ty, Type::Void) {
return Err(CompileError::plain(
"a constructor cannot return a value in Bash IR",
));
}
let _ = self.emit_expr(expression)?;
}
self.line("__tsh_backend_result=\"${__tsh_this}\"");
self.line("return 0");
Ok(())
}
ReturnMode::Function(return_type) => {
if let Some(expression) = expression {
let value = self.emit_expr(expression)?;
self.line(&format!("__tsh_backend_result={}", value.word));
} else if matches!(return_type, Type::Void) {
self.line("__tsh_backend_result=''");
} else {
return Err(CompileError::plain(
"non-void function returned without a value in Bash IR",
));
}
self.line("return 0");
Ok(())
}
}
}
fn emit_expr(&mut self, expression: &Expr) -> Result<Value, CompileError> {
match &expression.kind {
ExprKind::String(value) => {
Ok(Value::new(bash_quote(value), None, expression.ty.clone()))
}
ExprKind::Number(value) => Ok(Value::new(
value.to_string(),
Some(value.to_string()),
expression.ty.clone(),
)),
ExprKind::Boolean(value) => {
let bit = if *value { "1" } else { "0" };
Ok(Value::new(bit, Some(bit.to_owned()), expression.ty.clone()))
}
ExprKind::Variable(name) => {
if self.metadata.guarded_globals.contains(name.as_str())
&& !matches!(&self.return_mode, ReturnMode::TopLevel)
{
self.emit_global_ready_check(name);
}
let arithmetic = is_arithmetic_type(&expression.ty).then(|| name.clone());
Ok(
Value::new(shell_expansion(name), arithmetic, expression.ty.clone())
.named(name),
)
}
ExprKind::Template(parts) => self.emit_template(parts, expression.ty.clone()),
ExprKind::Binary { op, left, right } => {
self.emit_binary(op, left, right, expression.ty.clone())
}
ExprKind::Unary { op, value } => self.emit_unary(op, value, expression.ty.clone()),
ExprKind::Call { function, args } => {
self.emit_call(function, args, expression.ty.clone())
}
ExprKind::New { class, args } => self.emit_new(class, args),
ExprKind::Field {
object,
class,
field,
} => {
self.metadata.field(class, field)?;
let object_value = self.emit_expr(object)?;
let object = self.capture_if_variable(object, object_value);
let temp = self.fresh_temp();
self.line(&format!(
"{temp}=\"${{{}[${}]}}\"",
field_array(class, field),
object.arithmetic()?
));
let arithmetic = is_arithmetic_type(&expression.ty).then(|| temp.clone());
Ok(
Value::new(shell_expansion(&temp), arithmetic, expression.ty.clone())
.named(temp),
)
}
ExprKind::Method {
object,
class,
method,
args,
} => self.emit_method_call(
object.as_deref(),
class,
method,
args,
expression.ty.clone(),
),
ExprKind::Env(key) => self.emit_env(key, expression.ty.clone()),
}
}
fn emit_template(&mut self, parts: &[Expr], result_type: Type) -> Result<Value, CompileError> {
let result = self.fresh_temp();
self.line(&format!("{result}=''"));
for part in parts {
if matches!(part.ty, Type::Void) {
return Err(CompileError::plain(
"cannot interpolate a void expression in Bash IR",
));
}
let mut value = self.emit_expr(part)?;
if matches!(value.ty, Type::Boolean) {
value = self.boolean_as_text(value)?;
}
self.line(&format!("{result}=\"${{{result}}}\"{}", value.word));
}
Ok(Value::new(shell_expansion(&result), None, result_type))
}
fn emit_binary(
&mut self,
op: &str,
left: &Expr,
right: &Expr,
result_type: Type,
) -> Result<Value, CompileError> {
if op == "&&" || op == "||" {
return self.emit_logical(op, left, right, result_type);
}
let left_value = self.emit_expr(left)?;
let mut left = self.capture_if_variable(left, left_value);
if op == "+" && matches!(result_type, Type::String) && matches!(left.ty, Type::Boolean) {
left = self.boolean_as_text(left)?;
}
let right_value = self.emit_expr(right)?;
let mut right = self.capture_if_variable(right, right_value);
if op == "+" && matches!(result_type, Type::String) && matches!(right.ty, Type::Boolean) {
right = self.boolean_as_text(right)?;
}
let result = self.fresh_temp();
if op == "+" && matches!(result_type, Type::String) {
self.line(&format!("{result}={}{}", left.word, right.word));
return Ok(Value::new(shell_expansion(&result), None, result_type));
}
if is_comparison(op) {
let condition = if is_numeric_type(&left.ty) && is_numeric_type(&right.ty) {
format!(
"(( {} {} {} ))",
left.arithmetic()?,
normalize_comparison(op)?,
right.arithmetic()?
)
} else {
let a = &left.word;
let b = &right.word;
match normalize_comparison(op)? {
"==" => format!("[[ {a} == {b} ]]"),
"!=" => format!("[[ {a} != {b} ]]"),
"<" => format!("[[ {a} < {b} ]]"),
">" => format!("[[ {a} > {b} ]]"),
"<=" => format!("[[ {a} < {b} || {a} == {b} ]]"),
">=" => format!("[[ {a} > {b} || {a} == {b} ]]"),
_ => unreachable!(),
}
};
self.emit_boolean_assignment(&result, &condition);
return Ok(Value::new(
shell_expansion(&result),
Some(result.clone()),
result_type,
));
}
let arithmetic_op = match op {
"+" | "-" | "*" | "/" | "%" => op,
_ => {
return Err(CompileError::plain(format!(
"unsupported Bash binary operator `{op}`"
)));
}
};
self.line(&format!(
"{result}=$(( {} {arithmetic_op} {} ))",
left.arithmetic()?,
right.arithmetic()?
));
let arithmetic = is_arithmetic_type(&result_type).then(|| result.clone());
Ok(Value::new(
shell_expansion(&result),
arithmetic,
result_type,
))
}
fn emit_logical(
&mut self,
op: &str,
left: &Expr,
right: &Expr,
result_type: Type,
) -> Result<Value, CompileError> {
if !matches!(result_type, Type::Boolean) {
return Err(CompileError::plain(
"logical Bash IR expression must have boolean type",
));
}
let left = self.emit_expr(left)?;
let result = self.fresh_temp();
self.line(&format!("{result}=0"));
match op {
"&&" => {
self.line(&format!("if (( {} != 0 )); then", left.arithmetic()?));
self.indent += 1;
let right = self.emit_expr(right)?;
self.line(&format!("{result}={}", right.word));
self.indent -= 1;
self.line("fi");
}
"||" => {
self.line(&format!("if (( {} == 0 )); then", left.arithmetic()?));
self.indent += 1;
let right = self.emit_expr(right)?;
self.line(&format!("{result}={}", right.word));
self.indent -= 1;
self.line("else");
self.indent += 1;
self.line(&format!("{result}=1"));
self.indent -= 1;
self.line("fi");
}
_ => unreachable!(),
}
Ok(Value::new(
shell_expansion(&result),
Some(result.clone()),
result_type,
))
}
fn emit_unary(
&mut self,
op: &str,
expression: &Expr,
result_type: Type,
) -> Result<Value, CompileError> {
let value = self.emit_expr(expression)?;
let value = self.capture_if_variable(expression, value);
let result = self.fresh_temp();
match op {
"!" => {
self.line(&format!("if (( {} == 0 )); then", value.arithmetic()?));
self.indent += 1;
self.line(&format!("{result}=1"));
self.indent -= 1;
self.line("else");
self.indent += 1;
self.line(&format!("{result}=0"));
self.indent -= 1;
self.line("fi");
Ok(Value::new(
shell_expansion(&result),
Some(result),
result_type,
))
}
"-" => {
self.line(&format!("{result}=$(( -{} ))", value.arithmetic()?));
let arithmetic = is_arithmetic_type(&result_type).then(|| result.clone());
Ok(Value::new(
shell_expansion(&result),
arithmetic,
result_type,
))
}
"+" => {
self.line(&format!("{result}=$(( {} ))", value.arithmetic()?));
Ok(Value::new(
shell_expansion(&result),
Some(result),
result_type,
))
}
_ => Err(CompileError::plain(format!(
"unsupported Bash unary operator `{op}`"
))),
}
}
fn emit_call(
&mut self,
function: &str,
args: &[Expr],
result_type: Type,
) -> Result<Value, CompileError> {
let args = self.capture_arguments(args)?;
let call = command_words(&format!("__tsh_fn_{function}"), &args);
self.line(&call);
let result = self.capture_result(result_type);
Ok(result)
}
fn emit_new(&mut self, class: &str, args: &[Expr]) -> Result<Value, CompileError> {
self.metadata.class(class)?;
let args = self.capture_arguments(args)?;
let call = command_words(&format!("__tsh_class_{class}"), &args);
self.line(&call);
Ok(self.capture_result(Type::Class(class.to_owned())))
}
fn emit_method_call(
&mut self,
object: Option<&Expr>,
class: &str,
method: &str,
args: &[Expr],
result_type: Type,
) -> Result<Value, CompileError> {
let is_static = self.metadata.is_static(class, method)?;
let object = match (object, is_static) {
(Some(object), false) => {
let object_value = self.emit_expr(object)?;
Some(self.capture_if_variable(object, object_value))
}
(None, false) => {
return Err(CompileError::plain(format!(
"instance method `{class}.{method}` has no receiver in Bash IR"
)));
}
(Some(_), true) => {
return Err(CompileError::plain(format!(
"static method `{class}.{method}` unexpectedly has a receiver in Bash IR"
)));
}
(None, true) => None,
};
let args = self.capture_arguments(args)?;
let mut call = format!("__tsh_classMethod_{class}_{method}");
if let Some(object) = object {
call.push(' ');
call.push_str(&object.word);
}
for argument in &args {
call.push(' ');
call.push_str(&argument.word);
}
self.line(&call);
Ok(self.capture_result(result_type))
}
fn emit_env(&mut self, expression: &Expr, result_type: Type) -> Result<Value, CompileError> {
let key = self.emit_expr(expression)?;
let key = self.capture(key);
let result = self.fresh_temp();
self.line(&format!("{result}=''"));
let key_name = key.name.as_deref().ok_or_else(|| {
CompileError::plain("Bash environment key was not captured into a variable")
})?;
self.line(&format!(
"if [[ {} =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then",
key.word
));
self.indent += 1;
self.line(&format!("{result}=\"${{!{key_name}}}\""));
self.indent -= 1;
self.line("fi");
Ok(Value::new(shell_expansion(&result), None, result_type).named(result))
}
fn emit_command(
&mut self,
kind: Command,
args: &[Expr],
recursive: bool,
) -> Result<(), CompileError> {
match kind {
Command::Exit => {
if args.len() > 1 {
return Err(CompileError::plain(
"`exit` accepts at most one argument in Bash IR",
));
}
if let Some(value) = args.first() {
let value = self.emit_expr(value)?;
let value = self.capture(value);
self.line(&format!("exit {}", value.word));
} else {
self.line("exit 0");
}
}
Command::Echo => {
let values = self.capture_arguments(args)?;
let mut format = String::new();
let mut words = Vec::new();
for (index, value) in values.iter().enumerate() {
if index > 0 {
format.push(' ');
}
format.push_str("%s");
let value = if matches!(value.ty, Type::Boolean) {
self.boolean_as_text(value.clone())?
} else {
value.clone()
};
words.push(value.word);
}
let command = if words.is_empty() {
format!("printf {}", bash_quote("\\n"))
} else {
format.push_str("\\n");
let mut command = format!("printf {}", bash_quote(&format));
for word in words {
command.push(' ');
command.push_str(&word);
}
command
};
self.emit_checked_command(&command);
}
Command::Mkdir
| Command::Rm
| Command::Cp
| Command::Mv
| Command::Cd
| Command::Run => {
if kind == Command::Run && args.is_empty() {
return Err(CompileError::plain("`run` needs a command in Bash IR"));
}
let values = self.capture_arguments(args)?;
let mut command = match kind {
Command::Mkdir => if recursive { "mkdir -p --" } else { "mkdir --" }.to_owned(),
Command::Rm => if recursive { "rm -r --" } else { "rm --" }.to_owned(),
Command::Cp => "cp --".to_owned(),
Command::Mv => "mv --".to_owned(),
Command::Cd => "cd --".to_owned(),
Command::Run => String::new(),
_ => unreachable!(),
};
for value in values {
let value = if matches!(value.ty, Type::Boolean) {
self.boolean_as_text(value)?
} else {
value
};
if !command.is_empty() {
command.push(' ');
}
command.push_str(&value.word);
}
self.emit_checked_command(&command);
}
Command::Chmod => {
if args.len() != 2 {
return Err(CompileError::plain(
"`chmod` expects path and mode arguments in Bash IR",
));
}
let path_value = self.emit_expr(&args[0])?;
let path = self.capture_if_variable(&args[0], path_value);
let mode_value = self.emit_expr(&args[1])?;
let mode = self.capture_if_variable(&args[1], mode_value);
let octal = self.fresh_temp();
if let ExprKind::Number(decimal) = &args[1].kind {
if *decimal < 0 {
return Err(CompileError::plain(
"chmod mode cannot be negative in Bash IR",
));
}
self.line(&format!("{octal}={}", bash_quote(&format!("{decimal:o}"))));
} else if matches!(mode.ty, Type::String) {
self.line(&format!("{octal}={}", mode.word));
} else {
self.line(&format!(
"printf -v {octal} '%o' \"${{{}}}\"",
mode.arithmetic()?
));
}
let recursive_flag = if recursive { " -R" } else { "" };
self.emit_checked_command(&format!(
"chmod{recursive_flag} -- \"${{{octal}}}\" {}",
path.word
));
}
}
Ok(())
}
fn emit_checked_command(&mut self, command: &str) {
self.line(&format!("{command} || exit $?"));
}
fn emit_boolean_assignment(&mut self, destination: &str, condition: &str) {
self.line(&format!("if {condition}; then"));
self.indent += 1;
self.line(&format!("{destination}=1"));
self.indent -= 1;
self.line("else");
self.indent += 1;
self.line(&format!("{destination}=0"));
self.indent -= 1;
self.line("fi");
}
fn capture_arguments(&mut self, args: &[Expr]) -> Result<Vec<Value>, CompileError> {
let mut values = Vec::with_capacity(args.len());
for argument in args {
let value = self.emit_expr(argument)?;
values.push(self.capture_if_variable(argument, value));
}
Ok(values)
}
fn capture_if_variable(&mut self, expression: &Expr, value: Value) -> Value {
if matches!(expression.kind, ExprKind::Variable(_)) {
self.capture(value)
} else {
value
}
}
fn capture(&mut self, value: Value) -> Value {
let temp = self.fresh_temp();
self.line(&format!("{temp}={}", value.word));
let arithmetic = is_arithmetic_type(&value.ty).then(|| temp.clone());
Value::new(shell_expansion(&temp), arithmetic, value.ty).named(temp)
}
fn capture_result(&mut self, ty: Type) -> Value {
let temp = self.fresh_temp();
self.line(&format!("{temp}=\"${{__tsh_backend_result}}\""));
let arithmetic = is_arithmetic_type(&ty).then(|| temp.clone());
Value::new(shell_expansion(&temp), arithmetic, ty).named(temp)
}
fn boolean_as_text(&mut self, value: Value) -> Result<Value, CompileError> {
if !matches!(value.ty, Type::Boolean) {
return Ok(value);
}
if value.name.is_none() {
if value.arithmetic.as_deref() == Some("1") {
return Ok(Value::new(bash_quote("true"), None, Type::String));
}
if value.arithmetic.as_deref() == Some("0") {
return Ok(Value::new(bash_quote("false"), None, Type::String));
}
}
let source = if value.arithmetic.is_some() {
value
} else {
self.capture(value)
};
let result = self.fresh_temp();
self.line(&format!("if (( {} != 0 )); then", source.arithmetic()?));
self.indent += 1;
self.line(&format!("{result}='true'"));
self.indent -= 1;
self.line("else");
self.indent += 1;
self.line(&format!("{result}='false'"));
self.indent -= 1;
self.line("fi");
Ok(Value::new(shell_expansion(&result), None, Type::String))
}
}
fn emit_function(
function: &Function,
method: Option<(&Class, bool)>,
metadata: &Metadata<'_>,
next_temp: &mut usize,
) -> Result<String, CompileError> {
let mut writer = Writer::new(
metadata,
next_temp,
ReturnMode::Function(function.return_type.clone()),
);
writer.indent = 1;
writer.emit_statements(&function.body)?;
writer.line(&format!(
"__tsh_backend_result={}",
default_value(&function.return_type)
));
let mut locals = Vec::new();
if let Some((_, true)) = method {
locals.push("__tsh_this".to_owned());
}
locals.extend(function.params.iter().map(|param| param.name.clone()));
collect_let_names(&function.body, &mut locals);
locals.extend(writer.temps.iter().cloned());
deduplicate(&mut locals);
let mut output = String::new();
let symbol = match method {
Some((class, _)) => format!("__tsh_classMethod_{}_{}", class.name, function.name),
None => format!("__tsh_fn_{}", function.name),
};
output.push_str(&format!("{symbol}() {{\n"));
for chunk in locals.chunks(20) {
output.push_str(" local");
for name in chunk {
output.push(' ');
output.push_str(name);
}
output.push('\n');
}
let mut index = 1usize;
if let Some((_, true)) = method {
output.push_str(" __tsh_this=\"$1\"\n");
index += 1;
}
for param in &function.params {
output.push_str(&format!(" {}=\"${index}\"\n", param.name));
index += 1;
}
output.push_str(&writer.code);
output.push_str("}\n");
Ok(output)
}
fn emit_constructor(
class: &Class,
metadata: &Metadata<'_>,
next_temp: &mut usize,
) -> Result<String, CompileError> {
let mut writer = Writer::new(metadata, next_temp, ReturnMode::Constructor);
writer.indent = 1;
writer.line("__tsh_backend_next_object_id=$(( __tsh_backend_next_object_id + 1 ))");
writer.line("__tsh_this=\"${__tsh_backend_next_object_id}\"");
for field in &class.fields {
let array = field_array(&class.name, &field.name);
if let Some(initial) = &field.initial {
let value = writer.emit_expr(initial)?;
writer.line(&format!("{array}[$__tsh_this]={}", value.word));
} else {
writer.line(&format!(
"{array}[$__tsh_this]={}",
default_value(&field.ty)
));
}
}
writer.emit_statements(&class.constructor.body)?;
writer.line("__tsh_backend_result=\"${__tsh_this}\"");
let mut locals = vec!["__tsh_this".to_owned()];
locals.extend(
class
.constructor
.params
.iter()
.map(|param| param.name.clone()),
);
collect_let_names(&class.constructor.body, &mut locals);
locals.extend(writer.temps.iter().cloned());
deduplicate(&mut locals);
let mut output = format!("__tsh_class_{}() {{\n", class.name);
for chunk in locals.chunks(20) {
output.push_str(" local");
for name in chunk {
output.push(' ');
output.push_str(name);
}
output.push('\n');
}
for (index, param) in class.constructor.params.iter().enumerate() {
output.push_str(&format!(" {}=\"${}\"\n", param.name, index + 1));
}
output.push_str(&writer.code);
output.push_str("}\n");
Ok(output)
}
fn emit_method(
class: &Class,
method: &Method,
metadata: &Metadata<'_>,
next_temp: &mut usize,
) -> Result<String, CompileError> {
emit_function(
&method.function,
Some((class, !method.is_static)),
metadata,
next_temp,
)
}
fn collect_let_names(statements: &[Stmt], locals: &mut Vec<String>) {
for statement in statements {
match statement {
Stmt::Let { name, .. } => locals.push(name.clone()),
Stmt::If {
then_body,
else_body,
..
} => {
collect_let_names(then_body, locals);
collect_let_names(else_body, locals);
}
Stmt::While { body, .. } | Stmt::Block(body) => collect_let_names(body, locals),
Stmt::For {
init, update, body, ..
} => {
collect_let_names(init, locals);
collect_let_names(update, locals);
collect_let_names(body, locals);
}
_ => {}
}
}
}
fn deduplicate(values: &mut Vec<String>) {
let mut seen = HashSet::new();
values.retain(|value| seen.insert(value.clone()));
}
fn command_words(command: &str, args: &[Value]) -> String {
let mut output = command.to_owned();
for arg in args {
output.push(' ');
output.push_str(&arg.word);
}
output
}
fn shell_expansion(name: &str) -> String {
format!("\"${{{name}}}\"")
}
fn bash_quote(value: &str) -> String {
if value.is_empty() {
return "''".to_owned();
}
format!("'{}'", value.replace('\'', "'\\''"))
}
fn field_array(class: &str, field: &str) -> String {
format!(
"__tsh_backend_field_{}_{}",
encode_identifier_component(class),
encode_identifier_component(field)
)
}
fn ready_flag(name: &str) -> String {
format!("__tsh_backend_ready_{name}")
}
fn encode_identifier_component(value: &str) -> String {
value
.as_bytes()
.iter()
.map(|byte| format!("{byte:02x}"))
.collect()
}
fn validate_program(program: &Program) -> Result<(), CompileError> {
for function in &program.functions {
validate_function(function)?;
}
for class in &program.classes {
validate_identifier(&class.name)?;
for field in &class.fields {
if let Some(initial) = &field.initial {
validate_expr(initial)?;
}
}
validate_function(&class.constructor)?;
for method in &class.methods {
validate_function(&method.function)?;
}
}
validate_statements(&program.body)
}
fn validate_function(function: &Function) -> Result<(), CompileError> {
validate_identifier(&function.name)?;
for param in &function.params {
validate_identifier(¶m.name)?;
}
validate_statements(&function.body)
}
fn validate_statements(statements: &[Stmt]) -> Result<(), CompileError> {
for statement in statements {
match statement {
Stmt::Let { name, value } => {
validate_identifier(name)?;
validate_expr(value)?;
}
Stmt::Assign { place, value } => {
match place {
Place::Variable(name) => validate_identifier(name)?,
Place::Field { object, class, .. } => {
validate_identifier(class)?;
validate_expr(object)?;
}
}
validate_expr(value)?;
}
Stmt::Expr(expression) | Stmt::Return(Some(expression)) => validate_expr(expression)?,
Stmt::If {
condition,
then_body,
else_body,
} => {
validate_expr(condition)?;
validate_statements(then_body)?;
validate_statements(else_body)?;
}
Stmt::While { condition, body } => {
validate_expr(condition)?;
validate_statements(body)?;
}
Stmt::For {
init,
condition,
update,
body,
} => {
validate_statements(init)?;
if let Some(condition) = condition {
validate_expr(condition)?;
}
validate_statements(update)?;
validate_statements(body)?;
}
Stmt::Command { args, .. } => {
for argument in args {
validate_expr(argument)?;
}
}
Stmt::Block(body) => validate_statements(body)?,
Stmt::Return(None) | Stmt::Break | Stmt::Continue | Stmt::Raw(_) | Stmt::Comment(_) => {
}
}
}
Ok(())
}
fn validate_expr(expression: &Expr) -> Result<(), CompileError> {
match &expression.kind {
ExprKind::String(_) | ExprKind::Number(_) | ExprKind::Boolean(_) => {}
ExprKind::Variable(name) => validate_identifier(name)?,
ExprKind::Template(parts) => {
for part in parts {
validate_expr(part)?;
}
}
ExprKind::Binary { left, right, .. } => {
validate_expr(left)?;
validate_expr(right)?;
}
ExprKind::Unary { value, .. } | ExprKind::Env(value) => validate_expr(value)?,
ExprKind::Call { function, args } => {
validate_identifier(function)?;
for argument in args {
validate_expr(argument)?;
}
}
ExprKind::New { class, args } => {
validate_identifier(class)?;
for argument in args {
validate_expr(argument)?;
}
}
ExprKind::Field { object, class, .. } => {
validate_identifier(class)?;
validate_expr(object)?;
}
ExprKind::Method {
object,
class,
method,
args,
} => {
validate_identifier(class)?;
validate_identifier(method)?;
if let Some(object) = object {
validate_expr(object)?;
}
for argument in args {
validate_expr(argument)?;
}
}
}
Ok(())
}
fn validate_identifier(name: &str) -> Result<(), CompileError> {
let mut bytes = name.bytes();
let valid_start = bytes
.next()
.is_some_and(|byte| byte == b'_' || byte.is_ascii_alphabetic());
if valid_start && bytes.all(|byte| byte == b'_' || byte.is_ascii_alphanumeric()) {
Ok(())
} else {
Err(CompileError::plain(format!(
"unsafe shell identifier `{name}` in Bash IR"
)))
}
}
fn is_numeric_type(ty: &Type) -> bool {
matches!(ty, Type::Number | Type::Boolean | Type::Class(_))
}
fn is_arithmetic_type(ty: &Type) -> bool {
is_numeric_type(ty)
}
fn is_comparison(op: &str) -> bool {
matches!(op, "==" | "===" | "!=" | "!==" | "<" | "<=" | ">" | ">=")
}
fn normalize_comparison(op: &str) -> Result<&'static str, CompileError> {
match op {
"==" | "===" => Ok("=="),
"!=" | "!==" => Ok("!="),
"<" => Ok("<"),
"<=" => Ok("<="),
">" => Ok(">"),
">=" => Ok(">="),
_ => Err(CompileError::plain(format!(
"unsupported Bash comparison operator `{op}`"
))),
}
}
fn default_value(ty: &Type) -> &'static str {
match ty {
Type::Number | Type::Boolean | Type::Class(_) => "0",
Type::String | Type::Void => "''",
}
}
#[cfg(test)]
mod tests {
use std::process::{Command as ProcessCommand, Output};
use super::*;
fn expr(kind: ExprKind, ty: Type) -> Expr {
Expr { kind, ty }
}
fn number(value: i64) -> Expr {
expr(ExprKind::Number(value), Type::Number)
}
fn boolean(value: bool) -> Expr {
expr(ExprKind::Boolean(value), Type::Boolean)
}
fn string(value: &str) -> Expr {
expr(ExprKind::String(value.to_owned()), Type::String)
}
fn variable(name: &str, ty: Type) -> Expr {
expr(ExprKind::Variable(name.to_owned()), ty)
}
fn command(kind: Command, args: Vec<Expr>, recursive: bool) -> Stmt {
Stmt::Command {
kind,
args,
recursive,
}
}
fn run_bash(script: &str, cwd: Option<&std::path::Path>) -> Output {
let mut command = ProcessCommand::new("bash");
command.args(["--noprofile", "--norc", "-c", script]);
if let Some(cwd) = cwd {
command.current_dir(cwd);
}
command
.output()
.expect("Bash is required to run backend tests")
}
fn factorial_program() -> Program {
let n = variable("n", Type::Number);
let base_condition = expr(
ExprKind::Binary {
op: "<=".to_owned(),
left: Box::new(n.clone()),
right: Box::new(number(1)),
},
Type::Boolean,
);
let recursive_argument = expr(
ExprKind::Binary {
op: "-".to_owned(),
left: Box::new(n.clone()),
right: Box::new(number(1)),
},
Type::Number,
);
let recursive_call = expr(
ExprKind::Call {
function: "fact".to_owned(),
args: vec![recursive_argument],
},
Type::Number,
);
let recursive_result = expr(
ExprKind::Binary {
op: "*".to_owned(),
left: Box::new(n),
right: Box::new(recursive_call),
},
Type::Number,
);
Program {
functions: vec![Function {
name: "fact".to_owned(),
params: vec![crate::ir::Param {
name: "n".to_owned(),
ty: Type::Number,
}],
return_type: Type::Number,
body: vec![Stmt::If {
condition: base_condition,
then_body: vec![Stmt::Return(Some(number(1)))],
else_body: vec![Stmt::Return(Some(recursive_result))],
}],
}],
body: vec![command(
Command::Echo,
vec![expr(
ExprKind::Call {
function: "fact".to_owned(),
args: vec![number(6)],
},
Type::Number,
)],
false,
)],
..Program::default()
}
}
#[test]
fn recursive_calls_keep_each_frames_temporaries_and_result() {
let script = Bash.emit(&factorial_program()).expect("emit factorial");
assert!(script.contains("__tsh_fn_fact"));
let output = run_bash(&script, None);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout), "720\n");
}
#[test]
fn global_initialization_guards_allow_ready_globals_and_reject_tdz_calls() {
let read_first = Function {
name: "read_first".to_owned(),
params: vec![],
return_type: Type::Number,
body: vec![Stmt::Return(Some(variable("first", Type::Number)))],
};
let safe = Program {
functions: vec![read_first.clone()],
body: vec![
Stmt::Let {
name: "first".to_owned(),
value: number(41),
},
Stmt::Let {
name: "answer".to_owned(),
value: expr(
ExprKind::Call {
function: "read_first".to_owned(),
args: vec![],
},
Type::Number,
),
},
command(Command::Echo, vec![variable("answer", Type::Number)], false),
],
..Program::default()
};
let safe_script = Bash.emit(&safe).expect("emit initialized global call");
let safe_output = run_bash(&safe_script, None);
assert!(
safe_output.status.success(),
"{}",
String::from_utf8_lossy(&safe_output.stderr)
);
assert_eq!(String::from_utf8_lossy(&safe_output.stdout), "41\n");
let early = Program {
functions: vec![Function {
name: "read_later".to_owned(),
params: vec![],
return_type: Type::Number,
body: vec![Stmt::Return(Some(variable("later", Type::Number)))],
}],
body: vec![
Stmt::Let {
name: "early".to_owned(),
value: expr(
ExprKind::Call {
function: "read_later".to_owned(),
args: vec![],
},
Type::Number,
),
},
Stmt::Let {
name: "later".to_owned(),
value: number(9),
},
],
..Program::default()
};
let early_script = Bash.emit(&early).expect("emit guarded global call");
let early_output = run_bash(&early_script, None);
assert_eq!(early_output.status.code(), Some(1));
assert!(
String::from_utf8_lossy(&early_output.stderr)
.contains("global `later` accessed before initialization")
);
}
#[test]
fn instances_have_separate_mutable_fields_and_safe_field_symbols() {
let this = || variable("__tsh_this", Type::Class("Counter".to_owned()));
let this_count = || {
expr(
ExprKind::Field {
object: Box::new(this()),
class: "Counter".to_owned(),
field: "x-y;$(touch BAD)".to_owned(),
},
Type::Number,
)
};
let increment = Function {
name: "increment".to_owned(),
params: vec![],
return_type: Type::Number,
body: vec![
Stmt::Assign {
place: Place::Field {
object: this(),
class: "Counter".to_owned(),
field: "x-y;$(touch BAD)".to_owned(),
},
value: expr(
ExprKind::Binary {
op: "+".to_owned(),
left: Box::new(this_count()),
right: Box::new(number(1)),
},
Type::Number,
),
},
Stmt::Return(Some(this_count())),
],
};
let class = Class {
name: "Counter".to_owned(),
fields: vec![Field {
name: "x-y;$(touch BAD)".to_owned(),
ty: Type::Number,
initial: Some(number(4)),
}],
constructor: Function {
name: "Counter".to_owned(),
params: vec![],
return_type: Type::Void,
body: vec![],
},
methods: vec![Method {
function: increment,
is_static: false,
}],
};
let instance = |name: &str| variable(name, Type::Class("Counter".to_owned()));
let field = |name: &str| {
expr(
ExprKind::Field {
object: Box::new(instance(name)),
class: "Counter".to_owned(),
field: "x-y;$(touch BAD)".to_owned(),
},
Type::Number,
)
};
let call_increment = |name: &str| {
expr(
ExprKind::Method {
object: Some(Box::new(instance(name))),
class: "Counter".to_owned(),
method: "increment".to_owned(),
args: vec![],
},
Type::Number,
)
};
let program = Program {
classes: vec![class],
body: vec![
Stmt::Let {
name: "first".to_owned(),
value: expr(
ExprKind::New {
class: "Counter".to_owned(),
args: vec![],
},
Type::Class("Counter".to_owned()),
),
},
Stmt::Let {
name: "second".to_owned(),
value: expr(
ExprKind::New {
class: "Counter".to_owned(),
args: vec![],
},
Type::Class("Counter".to_owned()),
),
},
Stmt::Expr(call_increment("first")),
command(Command::Echo, vec![field("second"), field("first")], false),
],
..Program::default()
};
let script = Bash.emit(&program).expect("emit class");
assert!(!script.contains("x-y;$(touch BAD)"));
assert!(!script.contains("touch BAD"));
let output = run_bash(&script, None);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(String::from_utf8_lossy(&output.stdout), "4 5\n");
}
#[test]
fn comments_only_and_empty_branches_stay_valid_bash() {
let program = Program {
body: vec![Stmt::If {
condition: boolean(true),
then_body: vec![],
else_body: vec![Stmt::Comment("// comment only".to_owned())],
}],
..Program::default()
};
let script = Bash.emit(&program).expect("emit empty branch");
assert!(script.contains("then\n :\nelse\n # comment only\n :\n"));
let output = run_bash(&script, None);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn booleans_print_as_language_values_and_strings_stay_literal() {
let temp = tempfile::tempdir().expect("temporary directory");
let hostile = "single' quote\n$(touch PWNED) * $HOME `touch PWNED`";
let template = expr(
ExprKind::Template(vec![
string("text="),
string(hostile),
string("|"),
boolean(false),
]),
Type::String,
);
let variable_template = expr(
ExprKind::Template(vec![string("flag="), variable("enabled", Type::Boolean)]),
Type::String,
);
let program = Program {
body: vec![
Stmt::Let {
name: "enabled".to_owned(),
value: boolean(true),
},
command(Command::Echo, vec![boolean(true), template], false),
command(Command::Echo, vec![variable_template], false),
],
..Program::default()
};
let script = Bash.emit(&program).expect("emit interpolation");
let output = run_bash(&script, Some(temp.path()));
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
String::from_utf8_lossy(&output.stdout),
format!("true text={hostile}|false\nflag=true\n")
);
assert!(!temp.path().join("PWNED").exists());
}
#[cfg(unix)]
#[test]
fn filesystem_commands_quote_leading_dash_operands_and_preserve_status() {
use std::os::unix::fs::PermissionsExt;
let temp = tempfile::tempdir().expect("temporary directory");
let path = temp.path().to_string_lossy().into_owned();
let program = Program {
body: vec![
command(Command::Cd, vec![string(&path)], false),
command(Command::Mkdir, vec![string("-p/nested")], true),
command(
Command::Run,
vec![string("touch"), string("--"), string("-f")],
false,
),
command(Command::Mv, vec![string("-f"), string("-g")], false),
command(Command::Cp, vec![string("-g"), string("-h")], false),
command(Command::Chmod, vec![string("-h"), number(493)], false),
command(Command::Rm, vec![string("-g")], false),
command(Command::Rm, vec![string("-p")], true),
],
..Program::default()
};
let script = Bash.emit(&program).expect("emit filesystem commands");
assert!(script.contains("mkdir -p --"));
assert!(script.contains("mv --"));
assert!(script.contains("cp --"));
assert!(script.contains("chmod --"));
let output = run_bash(&script, None);
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
std::fs::metadata(temp.path().join("-h"))
.expect("chmod target")
.permissions()
.mode()
& 0o777,
0o755
);
assert!(!temp.path().join("-p").exists());
assert!(!temp.path().join("-g").exists());
}
#[test]
fn failed_commands_exit_with_the_command_status() {
let program = Program {
body: vec![command(
Command::Run,
vec![string("bash"), string("-c"), string("exit 23")],
false,
)],
..Program::default()
};
let script = Bash.emit(&program).expect("emit failing command");
let output = run_bash(&script, None);
assert_eq!(output.status.code(), Some(23));
}
}