rustfmt fixes
This commit is contained in:
parent
ba7db1e91b
commit
151c6abf45
12 changed files with 103 additions and 51 deletions
|
@ -2,6 +2,7 @@ unstable_features = true
|
|||
|
||||
hard_tabs = true
|
||||
tab_spaces = 4
|
||||
max_width = 92
|
||||
|
||||
trailing_semicolon = false
|
||||
trailing_comma = "Vertical"
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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: T) -> Box<T> {
|
|||
|
||||
impl TokenKind {
|
||||
fn expr_first(self) -> bool {
|
||||
matches!(self, |T::Return| T::Var
|
||||
| T::Global
|
||||
matches!(
|
||||
self,
|
||||
T::Return
|
||||
| T::Var | T::Global
|
||||
| T::Fn | T::Not
|
||||
| T::Backslash
|
||||
| T::Colon
|
||||
| T::Minus
|
||||
| T::Colon | T::Minus
|
||||
| T::Identifier
|
||||
| T::LParen
|
||||
| T::LBrack
|
||||
| T::LBrace
|
||||
| T::Dollar
|
||||
| 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::While | T::For
|
||||
| T::Try | T::Integer
|
||||
| T::Float | T::Imaginary
|
||||
| T::String | T::Symbol
|
||||
| T::True | T::False
|
||||
| T::Nil)
|
||||
| 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),
|
||||
};
|
||||
|
|
|
@ -67,7 +67,10 @@ impl std::fmt::Debug for NativeFunc {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn disasm_recursive(f: &Rc<Function>, w: &mut impl std::io::Write) -> std::io::Result<()> {
|
||||
pub fn disasm_recursive(
|
||||
f: &Rc<Function>,
|
||||
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")?;
|
||||
|
|
|
@ -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<Option<Value>> {
|
||||
fn run_instr(
|
||||
&mut self,
|
||||
frame: &mut CallFrame,
|
||||
instr: Instruction,
|
||||
) -> Result<Option<Value>> {
|
||||
use Instruction as I;
|
||||
|
||||
match instr {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -15,12 +15,18 @@ pub fn throw(_: &mut Vm, args: Vec<Value>) -> Result<Value> {
|
|||
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"
|
||||
|
|
|
@ -115,7 +115,9 @@ impl BufFile {
|
|||
}
|
||||
}
|
||||
|
||||
fn try_into_raw_fd(self) -> std::result::Result<RawFd, IntoInnerError<BufWriter<File>>> {
|
||||
fn try_into_raw_fd(
|
||||
self,
|
||||
) -> std::result::Result<RawFd, IntoInnerError<BufWriter<File>>> {
|
||||
match self {
|
||||
BufFile::Buffered { r, w } => {
|
||||
let _ = w.into_inner()?.into_raw_fd();
|
||||
|
|
|
@ -203,7 +203,11 @@ impl<'p> FmtParser<'p> {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_arg<'a>(args: &'a [Value], n: Option<usize>, faidx: &mut usize) -> Result<&'a Value> {
|
||||
fn get_arg<'a>(
|
||||
args: &'a [Value],
|
||||
n: Option<usize>,
|
||||
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<usize>, ty: FmtType, buf: &mut LString) -> Result<()> {
|
||||
fn format_ratio(
|
||||
n: Rational64,
|
||||
prec: Option<usize>,
|
||||
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<usize>, ty: FmtType, buf: &mut LString) -> Result<()> {
|
||||
fn format_value(
|
||||
v: &Value,
|
||||
prec: Option<usize>,
|
||||
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<usize>, ty: FmtType, buf: &mut LString)
|
|||
res.map_err(|e| exception!(*SYM_FORMAT_ERROR, "{e}"))
|
||||
}
|
||||
|
||||
fn format_string(mut s: &LStr, prec: Option<usize>, ty: FmtType, buf: &mut LString) -> Result<()> {
|
||||
fn format_string(
|
||||
mut s: &LStr,
|
||||
prec: Option<usize>,
|
||||
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,
|
||||
|
|
|
@ -46,8 +46,12 @@ pub fn rand_in(vm: &mut Vm, args: Vec<Value>) -> Result<Value> {
|
|||
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"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -323,8 +323,8 @@ pub fn compile(_: &mut Vm, args: Vec<Value>) -> Result<Value> {
|
|||
"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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue