Skip to content

Commit

Permalink
♻️ Delegate u256 -> opcode conversion to compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Philogy committed Oct 19, 2024
1 parent 5a94de1 commit c31ff57
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn analyze_macro<'ast: 'src, 'src, E: FnMut(AnalysisError<'ast, 'src>)>(
// Validate instruction against the current scope.
let analyze_instruction =
|instruction: &'ast Instruction, label_stack: &mut LabelStack<'src, ()>| match instruction {
Instruction::Op(_) => None,
Instruction::Op(_) | Instruction::VariablePush(_) => None,
Instruction::LabelReference(label) => {
if !label_stack.contains(label.ident()) {
Some(AnalysisError::ReferenceNotFound {
Expand Down
1 change: 1 addition & 0 deletions crates/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub enum MacroStatement<'src> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Instruction<'src> {
Op(Spanned<Opcode>),
VariablePush(Spanned<U256>),
LabelReference(Spanned<&'src str>),
MacroArgReference(Spanned<&'src str>),
ConstantReference(Spanned<&'src str>),
Expand Down
1 change: 1 addition & 0 deletions crates/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod util;

pub use ast::*;
pub use parser::parse;
pub use util::u256_as_push;
2 changes: 1 addition & 1 deletion crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn macro_statement<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, ast::
}

fn instruction<'tokens, 'src: 'tokens>() -> impl Parser<'tokens, 'src, ast::Instruction<'src>> {
let push_auto = word().map(|(value, span)| (ast::Instruction::Op((u256_as_push(value), span))));
let push_auto = word().map(|(value, span)| (ast::Instruction::VariablePush((value, span))));

let push = select! {
Ident("push1") => 1,
Expand Down
2 changes: 1 addition & 1 deletion crates/ast/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) fn u256_as_push_data<const N: usize>(value: U256) -> Result<[u8; N],
Ok(output)
}

pub(crate) fn u256_as_push(value: U256) -> Opcode {
pub fn u256_as_push(value: U256) -> Opcode {
match value.byte_len() {
0..=1 => u256_as_push_data::<1>(value).map(Opcode::PUSH1).unwrap(),
2 => u256_as_push_data::<2>(value).map(Opcode::PUSH2).unwrap(),
Expand Down

0 comments on commit c31ff57

Please sign in to comment.