Lines
93.84 %
Functions
100 %
Branches
use super::super::context::CompileContext;
use super::super::emit::FunctionEmitter;
use super::super::expr::{
compile_expr, compile_for_stack, compile_for_stack_as, describe_operand, eval_value, fold_call,
fold_value, format_expr, string_designator,
};
use super::NativeSpec;
use super::comparison::bool_result;
use super::nullable::require_non_null;
use crate::ast::{Expr, WasmType};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
pub(super) const NATIVES: &[NativeSpec] = &[
NativeSpec {
name: "UPCASE-STRING",
eval: upcase_string,
stack: Some(compile_upcase_string_to_stack),
effect: Some(compile_upcase_string),
},
name: "STRING=",
eval: string_eq,
stack: Some(compile_string_eq_to_stack),
effect: None,
];
fn check_upcase_arity(args: &[Expr]) -> Result<()> {
if args.len() != 1 {
return Err(Error::Arity {
name: "UPCASE-STRING".to_string(),
expected: 1,
actual: args.len(),
});
}
Ok(())
pub(super) fn upcase_string(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
check_upcase_arity(args)?;
let arg = eval_value(symbols, &args[0])?;
match arg {
Expr::String(s) => Ok(Expr::String(s.to_uppercase())),
other => Err(Error::Compile(format!(
"UPCASE-STRING: expected string, got {}",
format_expr(&other)
))),
pub(super) fn compile_upcase_string(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
if let Some(folded) = fold_call(symbols, "UPCASE-STRING", args)? {
return compile_expr(ctx, emit, symbols, &folded);
let rejection = upcase_rejection(ctx, emit, symbols, &args[0])?;
emit.reject_if_live(rejection)
/// A value-position `UPCASE-STRING` has no lowering (ISS-083); a call that is
/// ill-typed says so first.
fn compile_upcase_string_to_stack(
) -> Result<WasmType> {
if fold_call(symbols, "UPCASE-STRING", args)?.is_some() {
return Err(super::stackless_error("UPCASE-STRING"));
emit.reject_if_live(rejection)?;
Ok(WasmType::StringRef)
/// Why a non-folding `UPCASE-STRING` cannot compile, after its operand did.
fn upcase_rejection(
operand: &Expr,
) -> Result<Error> {
Ok(Error::Compile(
match compile_for_stack(ctx, emit, symbols, operand)? {
// Upcasing a runtime string needs a host function (not built).
WasmType::StringRef => "UPCASE-STRING of a runtime string".to_string(),
ty => format!(
"UPCASE-STRING expects a string, got {}",
describe_operand(symbols, operand, ty)
),
))
pub(super) fn string_eq(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() != 2 {
name: "STRING=".to_string(),
expected: 2,
let a = eval_value(symbols, &args[0])?;
let b = eval_value(symbols, &args[1])?;
if let (Expr::String(sa), Expr::String(sb)) = (&a, &b) {
return Ok(bool_result(sa == sb));
Ok(Expr::WasmRuntime(WasmType::Bool))
pub(super) fn compile_string_eq_to_stack(
let a = fold_value(symbols, &args[0])?;
let b = fold_value(symbols, &args[1])?;
if let (Some(Expr::String(sa)), Some(Expr::String(sb))) = (&a, &b) {
emit.i32_const(i32::from(sa == sb));
return Ok(WasmType::Bool);
let first = compile_string_arg_to_local(ctx, emit, symbols, &args[0], a.as_ref())?;
let second = compile_string_arg_to_local(ctx, emit, symbols, &args[1], b.as_ref())?;
let (local_a, local_b) = (first.local, second.local);
for arg in [first, second].iter().filter(|arg| arg.checked) {
emit.local_get(arg.local);
require_non_null(ctx, emit, WasmType::StringRef, ctx.ids.nil_string)?;
emit.local_set(arg.local);
emit.local_get(local_a);
emit.local_get(local_b);
emit.call(ctx.ids.string_eq);
Ok(WasmType::Bool)
/// A compiled `STRING=` argument: its scratch `StringRef` local and whether it
/// still needs a later null check.
struct StringArg {
local: u32,
checked: bool,
/// Compiles one `STRING=` argument into a scratch `StringRef` local. A folded
/// string literal never needs the null check (it's always freshly built,
/// non-null). Never checks here itself: the caller runs the checks only
/// after every argument has been evaluated.
fn compile_string_arg_to_local(
arg: &Expr,
folded: Option<&Expr>,
) -> Result<StringArg> {
if let Some(Expr::String(s)) = folded {
let data_idx = ctx.add_data(s.as_bytes())?;
emit.i32_const(0);
emit.i32_const(s.len() as i32);
emit.array_new_data(ctx.ids.ty_i8_array, data_idx);
let local = ctx.alloc_local(WasmType::StringRef)?;
emit.local_set(local);
return Ok(StringArg {
local,
checked: false,
// A literal `nil` argument (or anything that folds to it) lowers
// type-directed straight to a nullable `StringRef`, rather than
// folding to `Bool` and failing this as a COMPILE-time type mismatch —
// it fails at RUNTIME instead, as a catchable `TYPE-ERROR`, exactly
// like a runtime-nil string does (ADR-0033 Phase 5).
if matches!(eval_value(&mut symbols.clone(), arg), Ok(Expr::Nil)) {
compile_for_stack_as(ctx, emit, symbols, arg, WasmType::StringRef)?;
checked: true,
let ty = compile_for_stack(ctx, emit, symbols, arg)?;
let ty = string_designator(ctx, emit, ty)?;
if ty != WasmType::StringRef {
emit.reject_if_live(Error::Compile(format!(
"STRING= arguments must be strings, got {ty}"
)))?;
Ok(StringArg {
})
#[cfg(test)]
#[path = "string_tests.rs"]
mod tests;