Skip to content

Commit

Permalink
Fix lint issues described by cargo clippy
Browse files Browse the repository at this point in the history
Also ran Rust formatter
  • Loading branch information
elimirks committed Aug 15, 2022
1 parent 2b45520 commit 809c241
Show file tree
Hide file tree
Showing 9 changed files with 933 additions and 1,007 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ OPTIONS:

### Examples
See the `examples` or `test` directory! The official [B reference manual](https://www.bell-labs.com/usr/dmr/www/bref.html) and [B tutorial](https://www.bell-labs.com/usr/dmr/www/btut.html) explain all the supported functionality.

Use this commands to run compiler tests:
```
cargo run -- -r run_tests.b
```
### Standard Library
There are some standard library functions defined in `assets`. They aren't bundled with the release for now, so if you want to use them you'll have to download them.

Expand Down
8 changes: 5 additions & 3 deletions assets/memory.b
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* Increase the heap size in increments of 32KiB */
#define _HEAP_INCREMENT 077777;

_heapBegin 0;
_heapEnd 0;

Expand All @@ -9,7 +12,7 @@ malloc(count) {
extrn _heapBegin, _heapEnd;
if (_heapBegin == 0) {
_heapBegin = syscall(12, 0);
_heapEnd = syscall(12, _heapBegin + 077777);
_heapEnd = syscall(12, _heapBegin + _HEAP_INCREMENT);
/* Slot header: (size << 1) + occupiedBit */
/* size=0 and occupied=0 indicate the end of the chunk list */
*_heapBegin = 0;
Expand All @@ -33,8 +36,7 @@ malloc(count) {
ptrEnd = ptr + byteCount + 8;

if (ptrEnd >= _heapEnd) {
/* Increase the heap size in increments of 32KiB */
_heapEnd = syscall(12, (ptrEnd + 077777) & ~077777);
_heapEnd = syscall(12, (ptrEnd + _HEAP_INCREMENT) & ~_HEAP_INCREMENT);
}
/* Set a new null terminator */
*ptrEnd = 0;
Expand Down
3 changes: 2 additions & 1 deletion run_tests.b
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ main() {
it =+ reclen;
}
}
printf("Great success!*n");
}

print_divider(len) {
while (len-- > 0) {
putchar('=');
}
putchar('*n');
}
}
59 changes: 26 additions & 33 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct Pos {
impl Pos {
pub fn new(offset: usize, file_id: usize) -> Pos {
Pos {
offset: offset,
file_id: file_id,
offset,
file_id,
}
}
}
Expand All @@ -23,16 +23,14 @@ impl CompErr {
pub fn err<T>(pos: &Pos, message: String) -> Result<T, CompErr> {
Err(CompErr {
pos: Some(pos.clone()),
message: message,
message,
})
}

pub fn from_io_res<T>(
io_res: Result<T, std::io::Error>
) -> Result<T, CompErr> {
pub fn from_io_res<T>(io_res: Result<T, std::io::Error>) -> Result<T, CompErr> {
match io_res {
Ok(result) => Ok(result),
Err(err) => Err(CompErr {
Err(err) => Err(CompErr {
pos: None,
message: err.to_string(),
}),
Expand All @@ -54,10 +52,10 @@ pub struct RootStatements {
impl RootStatements {
pub fn new() -> RootStatements {
RootStatements {
functions: vec!(),
variables: vec!(),
imports: vec!(),
defines: vec!(),
functions: vec![],
variables: vec![],
imports: vec![],
defines: vec![],
}
}
}
Expand Down Expand Up @@ -147,26 +145,26 @@ pub enum Expr {
Reference(Pos, String),
Dereference(Pos, Box<Expr>),
}

impl GetPos for Expr {
fn pos(&self) -> Pos {
match self {
Expr::Id(pos, _) => pos.clone(),
Expr::Str(pos, _) => pos.clone(),
Expr::Call(pos, _, _) => pos.clone(),
Expr::Int(pos, _) => pos.clone(),
Expr::Assignment(pos, _, _) => pos.clone(),
Expr::Id(pos, _) => pos.clone(),
Expr::Str(pos, _) => pos.clone(),
Expr::Call(pos, _, _) => pos.clone(),
Expr::Int(pos, _) => pos.clone(),
Expr::Assignment(pos, _, _) => pos.clone(),
Expr::DerefAssignment(pos, _, _) => pos.clone(),
Expr::UnaryOperator(pos, _, _) => pos.clone(),
Expr::BinOperator(pos, _, _, _) => pos.clone(),
Expr::Reference(pos, _) => pos.clone(),
Expr::Dereference(pos, _) => pos.clone(),
Expr::Cond(pos, _, _, _) => pos.clone(),
Expr::UnaryOperator(pos, _, _) => pos.clone(),
Expr::BinOperator(pos, _, _, _) => pos.clone(),
Expr::Reference(pos, _) => pos.clone(),
Expr::Dereference(pos, _) => pos.clone(),
Expr::Cond(pos, _, _, _) => pos.clone(),
}
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BinOp {
Assign(Option<Box<BinOp>>), // FIXME: This shouldn't need to be heap allocated
Add,
Expand All @@ -193,7 +191,7 @@ impl BinOp {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum UnaryOp {
PreIncrement,
PreDecrement,
Expand All @@ -206,14 +204,9 @@ pub enum UnaryOp {
impl BinOp {
#[allow(dead_code)]
pub fn is_comparison(&self) -> bool {
match self {
BinOp::Eq => true,
BinOp::Ne => true,
BinOp::Le => true,
BinOp::Ge => true,
BinOp::Lt => true,
BinOp::Gt => true,
_ => false,
}
matches!(
self,
BinOp::Eq | BinOp::Ne | BinOp::Le | BinOp::Ge | BinOp::Lt | BinOp::Gt
)
}
}
Loading

0 comments on commit 809c241

Please sign in to comment.