Forráskód Böngészése

zkas: Minor cleanups

x 7 hónapja
szülő
commit
469d4354d8
3 módosított fájl, 29 hozzáadás és 41 törlés
  1. 24 36
      src/zkas/compiler.rs
  2. 3 3
      src/zkas/lexer.rs
  3. 2 2
      src/zkas/types.rs

+ 24 - 36
src/zkas/compiler.rs

@@ -120,30 +120,30 @@ impl Compiler {
             for arg in &i.rhs {
                 match arg {
                     Arg::Var(arg) => {
-                        if let Some(found) = Compiler::lookup_heap(&tmp_heap, &arg.name) {
-                            bincode.push(HeapType::Var as u8);
-                            bincode.extend_from_slice(&serialize(&VarInt(found as u64)));
-                            continue
-                        }
-
-                        return Err(self.error.abort(
-                            &format!("Failed finding a heap reference for `{}`", arg.name),
-                            arg.line,
-                            arg.column,
-                        ))
+                        let heap_idx =
+                            Compiler::lookup_heap(&tmp_heap, &arg.name).ok_or_else(|| {
+                                self.error.abort(
+                                    &format!("Failed finding a heap reference for `{}`", arg.name),
+                                    arg.line,
+                                    arg.column,
+                                )
+                            })?;
+
+                        bincode.push(HeapType::Var as u8);
+                        bincode.extend_from_slice(&serialize(&VarInt(heap_idx as u64)));
                     }
                     Arg::Lit(lit) => {
-                        if let Some(found) = Compiler::lookup_literal(&self.literals, &lit.name) {
-                            bincode.push(HeapType::Lit as u8);
-                            bincode.extend_from_slice(&serialize(&VarInt(found as u64)));
-                            continue
-                        }
-
-                        return Err(self.error.abort(
-                            &format!("Failed finding literal `{}`", lit.name),
-                            lit.line,
-                            lit.column,
-                        ))
+                        let lit_idx = Compiler::lookup_literal(&self.literals, &lit.name)
+                            .ok_or_else(|| {
+                                self.error.abort(
+                                    &format!("Failed finding literal `{}`", lit.name),
+                                    lit.line,
+                                    lit.column,
+                                )
+                            })?;
+
+                        bincode.push(HeapType::Lit as u8);
+                        bincode.extend_from_slice(&serialize(&VarInt(lit_idx as u64)));
                     }
                     _ => unreachable!(),
                 };
@@ -161,22 +161,10 @@ impl Compiler {
     }
 
     fn lookup_heap(heap: &[&str], name: &str) -> Option<usize> {
-        for (idx, n) in heap.iter().enumerate() {
-            if n == &name {
-                return Some(idx)
-            }
-        }
-
-        None
+        heap.iter().position(|&n| n == name)
     }
 
     fn lookup_literal(literals: &[Literal], name: &str) -> Option<usize> {
-        for (idx, n) in literals.iter().enumerate() {
-            if n.name == name {
-                return Some(idx)
-            }
-        }
-
-        None
+        literals.iter().position(|n| n.name == name)
     }
 }

+ 3 - 3
src/zkas/lexer.rs

@@ -94,21 +94,21 @@ impl<'a> Lexer<'a> {
             () => {
                 tokens.push(Token::new(&buf, TokenType::Symbol, lineno, column - buf.len()));
                 in_symbol = false;
-                buf = String::new();
+                buf.clear();
             };
         }
         macro_rules! new_string {
             () => {
                 tokens.push(Token::new(&buf, TokenType::String, lineno, column - buf.len()));
                 in_string = false;
-                buf = String::new();
+                buf.clear();
             };
         }
         macro_rules! new_number {
             () => {
                 tokens.push(Token::new(&buf, TokenType::Number, lineno, column - buf.len()));
                 in_number = false;
-                buf = String::new();
+                buf.clear();
             };
         }
 

+ 2 - 2
src/zkas/types.rs

@@ -34,7 +34,7 @@ impl HeapType {
     }
 }
 
-/// Varable types supported by the zkas VM
+/// Variable types supported by the zkas VM
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 #[repr(u8)]
 pub enum VarType {
@@ -105,7 +105,7 @@ impl VarType {
         }
     }
 
-    pub fn name(&self) -> &str {
+    pub fn name(&self) -> &'static str {
         match self {
             Self::Dummy => "Dummy",
             Self::EcPoint => "EcPoint",