Skip to content

Commit

Permalink
add simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Apr 14, 2024
1 parent 27035f1 commit fb9b600
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/emitter/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ use crate::{
/// Arguments for the expression emitter.
#[derive(Debug)]
pub struct EmitExprArgs<'a> {
scratch: &'a mut ScratchTable,
diagnostics: &'a mut Vec<Report>,
emitter: &'a mut TealEmitter<'a>,
concrete_vars: IndexMap<usize, Vec<Chunk>>,
pub scratch: &'a mut ScratchTable,
pub diagnostics: &'a mut Vec<Report>,
pub emitter: &'a mut TealEmitter<'a>,
pub concrete_vars: IndexMap<usize, Vec<Chunk>>,
}

/// Emit expression returning the len of the type in bytes.
Expand Down
6 changes: 3 additions & 3 deletions crates/emitter/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct FuncInfo {
}

/// Represents a constant literal in teal bytecode.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Constant {
Uint(u64),
Bytes(Vec<u8>),
Expand All @@ -33,7 +33,7 @@ impl Display for Constant {
}

/// Represents a chunk of code of teal AVM bytecode.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Chunk {
pub op: Instruction,
pub constants: Vec<Constant>,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Chunk {
}

/// Represents AVM teal opcode from https://developer.algorand.org/docs/get-details/dapps/avm/teal/opcodes/v10/
#[derive(Debug, Clone, Display)]
#[derive(Debug, Clone, Display, PartialEq)]
pub enum Instruction {
#[display(fmt = "")]
Empty,
Expand Down
3 changes: 3 additions & 0 deletions crates/emitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod instruction;
mod scratch_table;
mod teal;

#[cfg(test)]
mod tests;

impl<'a> Runner<ContractDefinition, TealArtifacts> for TealEmitter<'a> {
fn run(source: &ContractDefinition) -> Result<TealArtifacts, CompilationError>
where
Expand Down
87 changes: 87 additions & 0 deletions crates/emitter/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use folidity_semantics::{
ast::{
BinaryExpression,
Expression,
TypeVariant,
UnaryExpression,
},
ContractDefinition,
Span,
};
use indexmap::IndexMap;
use num_bigint::BigUint;
use num_traits::FromPrimitive;

use crate::{
expression::{
emit_expression,
EmitExprArgs,
},
instruction::{
Chunk,
Constant,
Instruction,
},
scratch_table::ScratchTable,
teal::TealEmitter,
};

#[test]
fn simple_exprs() {
let definition = ContractDefinition::default();
let mut emitter = TealEmitter::new(&definition);

let mut args = EmitExprArgs {
scratch: &mut ScratchTable::default(),
diagnostics: &mut vec![],
emitter: &mut emitter,
concrete_vars: IndexMap::default(),
};

let loc = Span { start: 0, end: 0 };
let e1 = Expression::UInt(UnaryExpression {
loc: loc.clone(),
element: BigUint::from_i64(100).unwrap(),
ty: TypeVariant::Int,
});
let e2 = Expression::UInt(UnaryExpression {
loc: loc.clone(),
element: BigUint::from_i64(2).unwrap(),
ty: TypeVariant::Int,
});
let mul = Expression::Multiply(BinaryExpression {
loc: loc.clone(),
left: Box::new(e1),
right: Box::new(e2),
ty: TypeVariant::Int,
});

let mut chunks = vec![];
let res = emit_expression(&mul, &mut chunks, &mut args);
assert!(res.is_ok());

let expected = vec![
Chunk {
op: Instruction::Empty,
constants: vec![],
},
Chunk {
op: Instruction::PushBytes,
constants: vec![Constant::Bytes(vec![100])],
},
Chunk {
op: Instruction::PushBytes,
constants: vec![Constant::Bytes(vec![2])],
},
Chunk {
op: Instruction::BMul,
constants: vec![],
},
Chunk {
op: Instruction::Empty,
constants: vec![],
},
];

assert_eq!(chunks, expected)
}

0 comments on commit fb9b600

Please sign in to comment.