feat: add Math.sin, Math.cos, Math.atan2 trig functions

Adds trigonometric functions to the Math module across interpreter,
type system, and C backend. JS backend already supported them.
Also adds #include <math.h> to C preamble and handles Math module
calls through both Call and EffectOp paths in C backend.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-18 23:05:12 -05:00
parent b56c5461f1
commit 7c3bfa9301
3 changed files with 179 additions and 8 deletions

View File

@@ -858,6 +858,7 @@ impl CBackend {
self.writeln("#include <stdio.h>");
self.writeln("#include <stdlib.h>");
self.writeln("#include <string.h>");
self.writeln("#include <math.h>");
self.writeln("");
self.writeln("// === Lux Runtime Types ===");
self.writeln("");
@@ -3029,6 +3030,10 @@ impl CBackend {
self.register_rc_var(&temp, "LuxString");
return Ok(temp);
}
// Math module
if module_name.name == "Math" {
return self.emit_math_operation(&field.name, args);
}
// Check for user-defined module function
let key = (module_name.name.clone(), field.name.clone());
if let Some(c_name) = self.module_functions.get(&key).cloned() {
@@ -3392,6 +3397,11 @@ impl CBackend {
}
}
// Math module (treated as effect by parser but handled as direct C calls)
if effect.name == "Math" {
return self.emit_math_operation(&operation.name, args);
}
// Built-in Console effect
if effect.name == "Console" {
if operation.name == "print" {
@@ -3854,12 +3864,34 @@ impl CBackend {
}
}
Expr::Record { fields, .. } => {
let field_strs: Result<Vec<_>, _> = fields.iter().map(|(name, val)| {
let v = self.emit_expr(val)?;
Ok(format!(".{} = {}", name.name, v))
}).collect();
Ok(format!("{{ {} }}", field_strs?.join(", ")))
Expr::Record {
spread, fields, ..
} => {
if let Some(spread_expr) = spread {
// Evaluate spread source, then override fields
let base = self.emit_expr(spread_expr)?;
if fields.is_empty() {
Ok(base)
} else {
// Copy spread into a temp, then override fields
let temp = format!("_spread_{}", self.fresh_name());
self.writeln(&format!("__auto_type {} = {};", temp, base));
for (name, val) in fields {
let v = self.emit_expr(val)?;
self.writeln(&format!("{}.{} = {};", temp, name.name, v));
}
Ok(temp)
}
} else {
let field_strs: Result<Vec<_>, _> = fields
.iter()
.map(|(name, val)| {
let v = self.emit_expr(val)?;
Ok(format!(".{} = {}", name.name, v))
})
.collect();
Ok(format!("{{ {} }}", field_strs?.join(", ")))
}
}
Expr::Field { object, field, .. } => {
@@ -3929,6 +3961,64 @@ impl CBackend {
}
}
/// Emit code for Math module operations (Math.sin, Math.cos, etc.)
fn emit_math_operation(&mut self, op: &str, args: &[Expr]) -> Result<String, CGenError> {
match op {
"abs" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("fabs({})", x))
}
"min" => {
let a = self.emit_expr(&args[0])?;
let b = self.emit_expr(&args[1])?;
Ok(format!("fmin({}, {})", a, b))
}
"max" => {
let a = self.emit_expr(&args[0])?;
let b = self.emit_expr(&args[1])?;
Ok(format!("fmax({}, {})", a, b))
}
"sqrt" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("sqrt({})", x))
}
"pow" => {
let base = self.emit_expr(&args[0])?;
let exp = self.emit_expr(&args[1])?;
Ok(format!("pow({}, {})", base, exp))
}
"floor" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("(int64_t)floor({})", x))
}
"ceil" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("(int64_t)ceil({})", x))
}
"round" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("(int64_t)round({})", x))
}
"sin" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("sin({})", x))
}
"cos" => {
let x = self.emit_expr(&args[0])?;
Ok(format!("cos({})", x))
}
"atan2" => {
let y = self.emit_expr(&args[0])?;
let x = self.emit_expr(&args[1])?;
Ok(format!("atan2({}, {})", y, x))
}
_ => Err(CGenError {
message: format!("Math.{} not supported in C backend", op),
span: None,
}),
}
}
/// Emit code for List module operations (List.map, List.filter, etc.)
fn emit_list_operation(&mut self, op: &str, args: &[Expr]) -> Result<String, CGenError> {
match op {
@@ -5831,7 +5921,10 @@ impl CBackend {
}
self.collect_free_vars(body, &inner_bound, free);
}
Expr::Record { fields, .. } => {
Expr::Record { spread, fields, .. } => {
if let Some(spread_expr) = spread {
self.collect_free_vars(spread_expr, bound, free);
}
for (_, val) in fields {
self.collect_free_vars(val, bound, free);
}