Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Sep 27, 2023
1 parent 0fa01c2 commit 609c0a0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 89 deletions.
12 changes: 2 additions & 10 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ pub fn compile_from_compileform(
p0: CompileForm,
symbol_table: &mut HashMap<String, String>,
) -> Result<SExp, CompileErr> {
let mut wrapper = CompileContextWrapper::new(
allocator, runner, symbol_table
);
let mut wrapper = CompileContextWrapper::new(allocator, runner, symbol_table);
let p1 = if opts.frontend_opt() {
// Front end optimization
fe_opt(&mut wrapper.context, opts.clone(), p0)?
Expand Down Expand Up @@ -217,13 +215,7 @@ pub fn compile_pre_forms(
// Resolve includes, convert program source to lexemes
let p0 = frontend(opts.clone(), pre_forms)?;

compile_from_compileform(
allocator,
runner,
opts,
p0,
symbol_table
)
compile_from_compileform(allocator, runner, opts, p0, symbol_table)
}

pub fn compile_file(
Expand Down
5 changes: 1 addition & 4 deletions src/compiler/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,7 @@ fn make_let_bindings(
opts: Rc<dyn CompilerOpts>,
body: Rc<SExp>,
) -> Result<Vec<Rc<Binding>>, CompileErr> {
let err = Err(CompileErr(
body.loc(),
format!("Bad binding tail {body:?}")
));
let err = Err(CompileErr(body.loc(), format!("Bad binding tail {body:?}")));
let do_atomize = if opts.dialect().strict {
|a: &SExp| -> SExp { a.atomize() }
} else {
Expand Down
99 changes: 26 additions & 73 deletions src/compiler/preprocessor/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn match_quoted_string(body: Rc<SExp>) -> Result<Option<(Srcloc, Vec<u8>)>, Comp
let is_string = match body.borrow() {
SExp::QuotedString(_, b'x', _) => None,
SExp::QuotedString(al, _, an) => Some((al.clone(), an.clone())),
_ => None
_ => None,
};

if let Some((loc, s)) = is_string {
Expand Down Expand Up @@ -60,7 +60,7 @@ fn match_number(body: Rc<SExp>) -> Result<Option<MatchedNumber>, CompileErr> {
SExp::Nil(il) => {
return Ok(Some(MatchedNumber::MatchedInt(il.clone(), bi_zero())));
}
_ => { }
_ => {}
}

Err(CompileErr(body.loc(), "number required".to_string()))
Expand Down Expand Up @@ -94,11 +94,7 @@ pub trait ExtensionFunction {
}
fn required_args(&self) -> Option<usize>;
#[allow(clippy::too_many_arguments)]
fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr>;
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr>;
}

struct StringQ {}
Expand All @@ -114,11 +110,7 @@ impl ExtensionFunction for StringQ {
Some(1)
}

fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let res = match match_quoted_string(args[0].clone()) {
Ok(Some(_)) => SExp::Integer(loc.clone(), bi_one()),
_ => SExp::Nil(loc.clone()),
Expand All @@ -141,11 +133,7 @@ impl ExtensionFunction for NumberQ {
Some(1)
}

fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let res = match match_number(args[0].clone()) {
Ok(Some(_)) => SExp::Integer(loc.clone(), bi_one()),
_ => SExp::Nil(loc.clone()),
Expand All @@ -168,11 +156,7 @@ impl ExtensionFunction for SymbolQ {
Some(1)
}

fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let res = match match_atom(args[0].clone()) {
Ok(Some(_)) => SExp::Integer(loc.clone(), bi_one()),
_ => SExp::Nil(loc.clone()),
Expand All @@ -195,15 +179,9 @@ impl ExtensionFunction for SymbolToString {
Some(1)
}

fn try_eval(
&self,
_loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, _loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
if let Some((loc, value)) = match_atom(args[0].clone())? {
Ok(Rc::new(SExp::QuotedString(
loc, b'\"', value,
)))
Ok(Rc::new(SExp::QuotedString(loc, b'\"', value)))
} else {
Err(CompileErr(args[0].loc(), "Not a symbol".to_string()))
}
Expand All @@ -223,11 +201,7 @@ impl ExtensionFunction for StringToSymbol {
Some(1)
}

fn try_eval(
&self,
_loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, _loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
if let Some((loc, value)) = match_quoted_string(args[0].clone())? {
Ok(Rc::new(SExp::Atom(loc, value)))
} else {
Expand All @@ -249,11 +223,7 @@ impl ExtensionFunction for StringAppend {
None
}

fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let mut out_vec = Vec::new();
let mut out_loc = None;
for a in args.iter() {
Expand Down Expand Up @@ -287,11 +257,7 @@ impl ExtensionFunction for NumberToString {
Some(1)
}

fn try_eval(
&self,
_loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, _loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let match_res = match_number(args[0].clone())?;
let (use_loc, int_val) = match &match_res {
Some(MatchedNumber::MatchedInt(l, i)) => (l.clone(), i.clone()),
Expand Down Expand Up @@ -321,11 +287,7 @@ impl ExtensionFunction for StringToNumber {
Some(1)
}

fn try_eval(
&self,
loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
if let Some((loc, value)) = match_quoted_string(args[0].clone())? {
if let Ok(cvt_bi) = decode_string(&value).parse::<Number>() {
Ok(Rc::new(SExp::Integer(loc, cvt_bi)))
Expand Down Expand Up @@ -354,18 +316,17 @@ impl ExtensionFunction for StringLength {
Some(1)
}

fn try_eval(
&self,
_loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, _loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
if let Some((loc, value)) = match_quoted_string(args[0].clone())? {
if let Some(len_bi) = value.len().to_bigint() {
return Ok(Rc::new(SExp::Integer(loc, len_bi)));
}
}

Err(CompileErr(args[0].loc(), "Error getting string length".to_string()))
Err(CompileErr(
args[0].loc(),
"Error getting string length".to_string(),
))
}
}

Expand All @@ -382,11 +343,7 @@ impl ExtensionFunction for Substring {
Some(3)
}

fn try_eval(
&self,
_loc: &Srcloc,
args: &[Rc<SExp>],
) -> Result<Rc<SExp>, CompileErr> {
fn try_eval(&self, _loc: &Srcloc, args: &[Rc<SExp>]) -> Result<Rc<SExp>, CompileErr> {
let start_element = usize_value(args[1].clone())?;
let end_element = usize_value(args[2].clone())?;

Expand All @@ -404,11 +361,7 @@ impl ExtensionFunction for Substring {
.skip(start_element)
.copied()
.collect();
Ok(Rc::new(SExp::QuotedString(
l.clone(),
*ch,
result_value,
)))
Ok(Rc::new(SExp::QuotedString(l.clone(), *ch, result_value)))
}
_ => Err(CompileErr(args[0].loc(), "Not a string".to_string())),
}
Expand Down Expand Up @@ -468,15 +421,15 @@ impl PrimOverride for PreprocessorExtension {
) -> Result<Option<Rc<SExp>>, RunFailure> {
eprintln!("running {head} {tail}");
if let SExp::Atom(hl, head_atom) = head.borrow() {
let have_args: Vec<Rc<SExp>> =
if let Some(args_list) = tail.proper_list() {
args_list.into_iter().map(Rc::new).collect()
} else {
return Ok(None);
};
let have_args: Vec<Rc<SExp>> = if let Some(args_list) = tail.proper_list() {
args_list.into_iter().map(Rc::new).collect()
} else {
return Ok(None);
};

if let Some(extension) = self.extfuns.get(head_atom) {
let res = extension.try_eval(&hl, &have_args)
let res = extension
.try_eval(&hl, &have_args)
.map_err(compile_to_run_err)?;

eprintln!("res = {res}");
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/preprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn make_defmac_name(name: &[u8]) -> Vec<u8> {
}

fn nilize(v: Rc<SExp>) -> Rc<SExp> {
if let SExp::Cons(l,a,b) = v.borrow() {
if let SExp::Cons(l, a, b) = v.borrow() {
let a_conv = nilize(a.clone());
let b_conv = nilize(b.clone());
if Rc::as_ptr(&a_conv) == Rc::as_ptr(&a) && Rc::as_ptr(&b_conv) == Rc::as_ptr(&b) {
Expand Down Expand Up @@ -321,7 +321,9 @@ impl Preprocessor {
args.clone(),
Some(extension),
None,
).map(nilize).map_err(|e| CompileErr::from(e))?;
)
.map(nilize)
.map_err(|e| CompileErr::from(e))?;

eprintln!("macro {} {args} => {res}", decode_string(&name));
return Ok(Some(res));
Expand Down

0 comments on commit 609c0a0

Please sign in to comment.