Skip to content

Commit

Permalink
Merge pull request #9 from maticnetwork/v1-assembler
Browse files Browse the repository at this point in the history
New assembler implementation
  • Loading branch information
bobbinth committed Nov 18, 2021
2 parents 9864409 + 7ed951b commit 2d4f55a
Show file tree
Hide file tree
Showing 15 changed files with 1,504 additions and 14 deletions.
86 changes: 78 additions & 8 deletions assembly/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ impl AssemblyError {
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------

pub fn unexpected_eof(step: usize) -> AssemblyError {
AssemblyError {
message: "unexpected EOF".to_string(),
step,
op: String::from(""),
}
}

pub fn empty_program() -> AssemblyError {
AssemblyError {
message: String::from("a program must contain at least one instruction"),
Expand Down Expand Up @@ -45,14 +53,6 @@ impl AssemblyError {
}
}

pub fn dangling_instructions(step: usize) -> AssemblyError {
AssemblyError {
message: "dangling instructions after program end".to_string(),
step,
op: String::from("end"),
}
}

pub fn invalid_op(op: &[&str], step: usize) -> AssemblyError {
AssemblyError {
message: format!("instruction {} is invalid", op.join(".")),
Expand Down Expand Up @@ -166,6 +166,76 @@ impl AssemblyError {
}
}

// SCRIPT
// --------------------------------------------------------------------------------------------

pub fn missing_begin(step: usize) -> AssemblyError {
AssemblyError {
message: "missing script body".to_string(),
step,
op: "begin".to_string(),
}
}

pub fn unmatched_begin(step: usize) -> AssemblyError {
AssemblyError {
message: "begin without matching end".to_string(),
step,
op: "begin".to_string(),
}
}

pub fn dangling_ops_after_script(op: &[&str], step: usize) -> AssemblyError {
AssemblyError {
message: "dangling instructions after script end".to_string(),
step,
op: op.join("."),
}
}

// PROCEDURES
// --------------------------------------------------------------------------------------------

pub fn duplicate_proc_label(step: usize, label: &str) -> AssemblyError {
AssemblyError {
message: format!("duplicate procedure label: {}", label),
step,
op: format!("proc.{}", label),
}
}

pub fn invalid_proc_label(label: &str, op: &[&str], step: usize) -> AssemblyError {
AssemblyError {
message: format!("invalid procedure label: {}", label),
step,
op: op.join("."),
}
}

pub fn unmatched_proc(step: usize, label: &str) -> AssemblyError {
AssemblyError {
message: "proc without matching end".to_string(),
step,
op: format!("proc.{}", label),
}
}

pub fn undefined_proc(step: usize, label: &str) -> AssemblyError {
AssemblyError {
message: format!("undefined procedure: {}", label),
step,
op: format!("exec.{}", label),
}
}

pub fn dangling_ops_after_proc(op: &[&str], step: usize) -> AssemblyError {
AssemblyError {
message: "dangling instructions after procedure end".to_string(),
step,
op: op.join("."),
}
}

// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
pub fn message(&self) -> &String {
Expand Down
4 changes: 3 additions & 1 deletion assembly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use parsers::*;
mod errors;
pub use errors::AssemblyError;

pub mod v1;

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -46,7 +48,7 @@ pub fn compile(source: &str) -> Result<Program, AssemblyError> {

// make sure there is nothing left after the last token
if i < tokens.len() - 1 {
return Err(AssemblyError::dangling_instructions(i));
return Err(AssemblyError::dangling_ops_after_script(&[], i));
}

// build and return the program
Expand Down
Loading

0 comments on commit 2d4f55a

Please sign in to comment.