diff --git a/rustfmt.toml b/rustfmt.toml index 0a88f15..cb4ca31 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -2,6 +2,7 @@ unstable_features = true hard_tabs = true tab_spaces = 4 +max_width = 92 trailing_semicolon = false trailing_comma = "Vertical" diff --git a/talc-bin/src/main.rs b/talc-bin/src/main.rs index 7647f3f..b76114d 100644 --- a/talc-bin/src/main.rs +++ b/talc-bin/src/main.rs @@ -1,6 +1,8 @@ use clap::{ColorChoice, Parser}; use std::{path::PathBuf, process::ExitCode, rc::Rc}; -use talc_lang::{compiler::compile, parser, symbol::Symbol, value::function::disasm_recursive, Vm}; +use talc_lang::{ + compiler::compile, parser, symbol::Symbol, value::function::disasm_recursive, Vm, +}; mod helper; mod repl; diff --git a/talc-lang/src/chunk.rs b/talc-lang/src/chunk.rs index d32e938..73920b2 100644 --- a/talc-lang/src/chunk.rs +++ b/talc-lang/src/chunk.rs @@ -110,12 +110,12 @@ pub enum Instruction { LoadGlobal(Arg24), // load global by id StoreGlobal(Arg24), // store global by id - CloseOver(Arg24), // load nth local and convert to cell, write back a copy - Closure(Arg24), // load constant function and fill state from stack - LoadUpvalue(Arg24), // load - StoreUpvalue(Arg24), // store a cell from closure state to new local - ContinueUpvalue(Arg24), // - LoadClosedLocal(Arg24), // load through cell in nth local + CloseOver(Arg24), // load nth local and convert to cell, write back a copy + Closure(Arg24), // load constant function and fill state from stack + LoadUpvalue(Arg24), // load + StoreUpvalue(Arg24), // store a cell from closure state to new local + ContinueUpvalue(Arg24), // + LoadClosedLocal(Arg24), // load through cell in nth local StoreClosedLocal(Arg24), // store through cell in nth local Const(Arg24), // push nth constant diff --git a/talc-lang/src/parser/parser.rs b/talc-lang/src/parser/parser.rs index ebef907..c7ce3c6 100644 --- a/talc-lang/src/parser/parser.rs +++ b/talc-lang/src/parser/parser.rs @@ -7,8 +7,8 @@ use crate::{ use super::{ ast::{BinaryOp, CatchBlock, Expr, ExprKind, LValue, UnaryOp}, - parse_float, parse_int_literal, parse_str_escapes, Lexer, ParserError, Span, SpanParserError, - Token, TokenKind, + parse_float, parse_int_literal, parse_str_escapes, Lexer, ParserError, Span, + SpanParserError, Token, TokenKind, }; use num_complex::Complex64; use ExprKind as E; @@ -159,27 +159,24 @@ fn b(t: T) -> Box { impl TokenKind { fn expr_first(self) -> bool { - matches!(self, |T::Return| T::Var - | T::Global - | T::Fn | T::Not - | T::Backslash - | T::Colon - | T::Minus - | T::Identifier - | T::LParen - | T::LBrack - | T::LBrace - | T::Dollar - | T::Do | T::If - | T::While - | T::For | T::Try - | T::Integer - | T::Float - | T::Imaginary - | T::String - | T::Symbol - | T::True | T::False - | T::Nil) + matches!( + self, + T::Return + | T::Var | T::Global + | T::Fn | T::Not + | T::Backslash + | T::Colon | T::Minus + | T::Identifier + | T::LParen | T::LBrack + | T::LBrace | T::Dollar + | T::Do | T::If + | T::While | T::For + | T::Try | T::Integer + | T::Float | T::Imaginary + | T::String | T::Symbol + | T::True | T::False + | T::Nil + ) } } @@ -371,7 +368,8 @@ impl<'s> Parser<'s> { Ok(E::Literal(x.into()).span(tok.span)) } T::Imaginary => { - let x = parse_float(&tok.content[..tok.content.len() - 1]).span_err(tok.span)?; + let x = parse_float(&tok.content[..tok.content.len() - 1]) + .span_err(tok.span)?; Ok(E::Literal(Complex64::new(0.0, x).into()).span(tok.span)) } T::String => { @@ -388,7 +386,8 @@ impl<'s> Parser<'s> { let s = match inner.chars().next() { Some('\'') => Symbol::get(&inner[1..inner.len() - 1]), Some('\"') => Symbol::get( - &parse_str_escapes(&inner[1..inner.len() - 1]).span_err(tok.span)?, + &parse_str_escapes(&inner[1..inner.len() - 1]) + .span_err(tok.span)?, ), _ => Symbol::get(inner), }; diff --git a/talc-lang/src/value/function.rs b/talc-lang/src/value/function.rs index aa5fd54..4b4f164 100644 --- a/talc-lang/src/value/function.rs +++ b/talc-lang/src/value/function.rs @@ -67,7 +67,10 @@ impl std::fmt::Debug for NativeFunc { } } -pub fn disasm_recursive(f: &Rc, w: &mut impl std::io::Write) -> std::io::Result<()> { +pub fn disasm_recursive( + f: &Rc, + w: &mut impl std::io::Write, +) -> std::io::Result<()> { writeln!(w, "{} ({})", Value::Function(f.clone()), f.attrs.arity)?; if !f.chunk.consts.is_empty() { writeln!(w, "constants")?; diff --git a/talc-lang/src/vm.rs b/talc-lang/src/vm.rs index 1575008..66018c9 100644 --- a/talc-lang/src/vm.rs +++ b/talc-lang/src/vm.rs @@ -10,7 +10,9 @@ use crate::{ exception::{throw, Exception, Result}, lstring::LStr, parser::ast::{BinaryOp, UnaryOp}, - symbol::{Symbol, SYM_CALL_STACK_OVERFLOW, SYM_INTERRUPTED, SYM_NAME_ERROR, SYM_TYPE_ERROR}, + symbol::{ + Symbol, SYM_CALL_STACK_OVERFLOW, SYM_INTERRUPTED, SYM_NAME_ERROR, SYM_TYPE_ERROR, + }, value::{ function::{FuncAttrs, Function, NativeFunc}, Value, @@ -198,7 +200,9 @@ impl Vm { while let Some(try_frame) = frame.try_frames.pop() { let table = &frame.func.chunk.try_tables[try_frame.idx]; for catch in &table.catches { - if catch.types.is_none() || catch.types.as_ref().unwrap().contains(&exc.ty) { + if catch.types.is_none() + || catch.types.as_ref().unwrap().contains(&exc.ty) + { frame.ip = catch.addr; frame.locals.truncate(table.local_count); self.stack.truncate(try_frame.stack_len); @@ -241,7 +245,11 @@ impl Vm { Ok(()) } - fn run_instr(&mut self, frame: &mut CallFrame, instr: Instruction) -> Result> { + fn run_instr( + &mut self, + frame: &mut CallFrame, + instr: Instruction, + ) -> Result> { use Instruction as I; match instr { diff --git a/talc-std/src/collection.rs b/talc-std/src/collection.rs index 445030b..db02366 100644 --- a/talc-std/src/collection.rs +++ b/talc-std/src/collection.rs @@ -149,7 +149,8 @@ fn insertion_by(vals: &mut [Value], by: &Value, vm: &mut Vm) -> Result<()> { for i in 0..vals.len() { let mut j = i; while j > 0 { - let ord = call_comparison(vm, by.clone(), vals[j - 1].clone(), vals[j].clone())?; + let ord = + call_comparison(vm, by.clone(), vals[j - 1].clone(), vals[j].clone())?; if ord <= 0 { break } diff --git a/talc-std/src/exception.rs b/talc-std/src/exception.rs index 9fec58e..365801a 100644 --- a/talc-std/src/exception.rs +++ b/talc-std/src/exception.rs @@ -15,12 +15,18 @@ pub fn throw(_: &mut Vm, args: Vec) -> Result { Value::Symbol(ty) => Exception::new(ty), Value::List(l) => match l.borrow().as_slice() { [Value::Symbol(ty)] | [Value::Symbol(ty), Value::Nil] => Exception::new(*ty), - [Value::Symbol(ty), Value::Nil, data] => Exception::new_with_data(*ty, data.clone()), - [Value::Symbol(ty), Value::String(s)] => Exception::new_with_msg(*ty, s.clone()), + [Value::Symbol(ty), Value::Nil, data] => { + Exception::new_with_data(*ty, data.clone()) + } + [Value::Symbol(ty), Value::String(s)] => { + Exception::new_with_msg(*ty, s.clone()) + } [Value::Symbol(ty), Value::String(s), data] => { Exception::new_with_msg_data(*ty, s.clone(), data.clone()) } - [] | [_] | [_, _] | [_, _, _] => throw!(*SYM_TYPE_ERROR, "wrong argument for throw"), + [] | [_] | [_, _] | [_, _, _] => { + throw!(*SYM_TYPE_ERROR, "wrong argument for throw") + } [_, _, _, _, ..] => throw!( *SYM_VALUE_ERROR, "too many elements in list argument for throw" diff --git a/talc-std/src/file.rs b/talc-std/src/file.rs index 0a52c94..0a11ca9 100644 --- a/talc-std/src/file.rs +++ b/talc-std/src/file.rs @@ -115,7 +115,9 @@ impl BufFile { } } - fn try_into_raw_fd(self) -> std::result::Result>> { + fn try_into_raw_fd( + self, + ) -> std::result::Result>> { match self { BufFile::Buffered { r, w } => { let _ = w.into_inner()?.into_raw_fd(); diff --git a/talc-std/src/format.rs b/talc-std/src/format.rs index 91f7436..afc9829 100644 --- a/talc-std/src/format.rs +++ b/talc-std/src/format.rs @@ -203,7 +203,11 @@ impl<'p> FmtParser<'p> { } } -fn get_arg<'a>(args: &'a [Value], n: Option, faidx: &mut usize) -> Result<&'a Value> { +fn get_arg<'a>( + args: &'a [Value], + n: Option, + faidx: &mut usize, +) -> Result<&'a Value> { let i = match n { Some(n) => n, None => { @@ -255,7 +259,9 @@ fn format_float( (None, FmtType::Repr) => write!(buf, "{:#}", Value::Float(f)), (None, FmtType::Exp(true)) => write!(buf, "{:E}", f), (None, FmtType::Exp(false)) => write!(buf, "{:e}", f), - (Some(p), FmtType::Str) | (Some(p), FmtType::Repr) => write!(buf, "{0:.1$}", f, p), + (Some(p), FmtType::Str) | (Some(p), FmtType::Repr) => { + write!(buf, "{0:.1$}", f, p) + } (Some(p), FmtType::Exp(true)) => write!(buf, "{0:.1$E}", f, p), (Some(p), FmtType::Exp(false)) => write!(buf, "{0:.1$e}", f, p), _ => throw!(*SYM_FORMAT_ERROR, "invalid format specifier for {ty_name}"), @@ -301,14 +307,24 @@ fn format_int( res.map_err(|e| exception!(*SYM_FORMAT_ERROR, "{e}")) } -fn format_ratio(n: Rational64, prec: Option, ty: FmtType, buf: &mut LString) -> Result<()> { +fn format_ratio( + n: Rational64, + prec: Option, + ty: FmtType, + buf: &mut LString, +) -> Result<()> { format_int(*n.numer(), prec, ty, buf, "ratio")?; buf.push_char('/'); format_int(*n.denom(), prec, ty, buf, "ratio")?; Ok(()) } -fn format_value(v: &Value, prec: Option, ty: FmtType, buf: &mut LString) -> Result<()> { +fn format_value( + v: &Value, + prec: Option, + ty: FmtType, + buf: &mut LString, +) -> Result<()> { if prec.is_some() { throw!(*SYM_FORMAT_ERROR, "invalid format specifier for value") } @@ -320,7 +336,12 @@ fn format_value(v: &Value, prec: Option, ty: FmtType, buf: &mut LString) res.map_err(|e| exception!(*SYM_FORMAT_ERROR, "{e}")) } -fn format_string(mut s: &LStr, prec: Option, ty: FmtType, buf: &mut LString) -> Result<()> { +fn format_string( + mut s: &LStr, + prec: Option, + ty: FmtType, + buf: &mut LString, +) -> Result<()> { if let Some(prec) = prec { s = &s[..prec] } @@ -341,7 +362,12 @@ fn pad_str(n: usize, c: char, buf: &mut LString) { } } -fn format_arg(args: &[Value], faidx: &mut usize, code: &LStr, res: &mut LString) -> Result<()> { +fn format_arg( + args: &[Value], + faidx: &mut usize, + code: &LStr, + res: &mut LString, +) -> Result<()> { if !code.is_utf8() { throw!( *SYM_FORMAT_ERROR, diff --git a/talc-std/src/random.rs b/talc-std/src/random.rs index e48a3b3..72f1826 100644 --- a/talc-std/src/random.rs +++ b/talc-std/src/random.rs @@ -46,8 +46,12 @@ pub fn rand_in(vm: &mut Vm, args: Vec) -> Result { throw!(*SYM_VALUE_ERROR, "rand_in: empty range") } match r.ty { - RangeType::Open => Ok(Value::Int(rand::thread_rng().gen_range(r.start..r.stop))), - RangeType::Closed => Ok(Value::Int(rand::thread_rng().gen_range(r.start..=r.stop))), + RangeType::Open => { + Ok(Value::Int(rand::thread_rng().gen_range(r.start..r.stop))) + } + RangeType::Closed => { + Ok(Value::Int(rand::thread_rng().gen_range(r.start..=r.stop))) + } RangeType::Endless => throw!(*SYM_VALUE_ERROR, "rand_in: endless range"), } } diff --git a/talc-std/src/value.rs b/talc-std/src/value.rs index ef79336..5102e65 100644 --- a/talc-std/src/value.rs +++ b/talc-std/src/value.rs @@ -323,8 +323,8 @@ pub fn compile(_: &mut Vm, args: Vec) -> Result { "compile: argument must be valid unicode ({e})" ) })?; - let ast = - talc_lang::parser::parse(src).map_err(|e| exception!(symbol!("parse_error"), "{e}"))?; + let ast = talc_lang::parser::parse(src) + .map_err(|e| exception!(symbol!("parse_error"), "{e}"))?; let func = talc_lang::compiler::compile(&ast, None); Ok(func.into()) }