Repository: huff-language/huff-rs
Pull request #122 (2022-06-24 15:38:04)

feat(huff_lexer): Syntax sugar for boolean values
## Overview

Adds syntax sugar for boolean values
* Lexes true as the literal `0x01`
* Lexes false as the literal `0x00`


Patch:
diff --git a/huff_codegen/src/lib.rs b/huff_codegen/src/lib.rs
index 3358f99..6cd22b3 100644
--- a/huff_codegen/src/lib.rs
+++ b/huff_codegen/src/lib.rs
@@ -430,7 +430,7 @@ impl Codegen {
     ///
     /// * `out` - Output location to write the serialized json artifact to.
     pub fn export(output: String, art: &Artifact) -> Result<(), CodegenError> {
-        let serialized_artifact = serde_json::to_string(art).unwrap();
+        let serialized_artifact = serde_json::to_string_pretty(art).unwrap();
         // Try to create the parent directory
         let file_path = Path::new(&output);
         if let Some(p) = file_path.parent() {
diff --git a/huff_lexer/src/lib.rs b/huff_lexer/src/lib.rs
index e474da4..f274574 100644
--- a/huff_lexer/src/lib.rs
+++ b/huff_lexer/src/lib.rs
@@ -444,6 +444,15 @@ impl<'a> Iterator for Lexer<'a> {
                     }
 
                     let pot_op = self.dyn_peek(|c| c.is_alphanumeric());
+
+                    // Syntax sugar: true evaluates to 0x01, false evaluates to 0x00
+                    if matches!(pot_op.as_str(), "true" | "false") {
+                        found_kind = Some(TokenKind::Literal(str_to_bytes32(
+                            if pot_op.as_str() == "true" { "1" } else { "0" },
+                        )));
+                        self.dyn_consume(|c| c.is_alphabetic());
+                    }
+
                     // goes over all opcodes
                     for opcode in OPCODES {
                         if self.context != Context::MacroBody {
diff --git a/huff_parser/src/lib.rs b/huff_parser/src/lib.rs
index df831aa..a114e77 100644
--- a/huff_parser/src/lib.rs
+++ b/huff_parser/src/lib.rs
@@ -311,7 +311,7 @@ impl Parser {
         self.match_kind(TokenKind::Ident("x".to_string()))?;
         let tok = self.peek_behind().unwrap().kind;
         let name = match tok {
-            TokenKind::Ident(event_name) => event_name,
+            TokenKind::Ident(const_name) => const_name,
             _ => {
                 tracing::error!(target: "parser", "TOKEN MISMATCH - EXPECTED IDENT, GOT: {}", tok);
                 let new_spans = self.spans.clone();
diff --git a/huffup/huffup b/huffup/huffup
index 8e4347a..6b9614a 100755
--- a/huffup/huffup
+++ b/huffup/huffup
@@ -120,8 +120,8 @@ main() {
 
     # Compute the URL of the release tarball in the Huff repository.
     RELEASE_URL="https://github.com/${HUFFUP_REPO}/releases/download/${HUFFUP_TAG}/"
-    BIN_TARBALL_URL="${RELEASE_URL}huffc${HUFFUP_VERSION}_${PLATFORM}_${ARCHITECTURE}.tar.gz"
-    MAN_TARBALL_URL="${RELEASE_URL}huffc${HUFFUP_VERSION}.tar.gz"
+    BIN_TARBALL_URL="${RELEASE_URL}huff_${HUFFUP_VERSION}_${PLATFORM}_${ARCHITECTURE}.tar.gz"
+    MAN_TARBALL_URL="${RELEASE_URL}huff_man_${HUFFUP_VERSION}.tar.gz"
 
     # Download the binaries tarball and unpack it into the .huff bin directory.
     say "downloading latest huff-rs"
@@ -215,4 +215,4 @@ ensure() {
   if ! "$@"; then err "command failed: $*"; fi
 }
 
-main "$@" || exit 1
\ No newline at end of file
+main "$@" || exit 1


Test patch:
diff --git a/huff_lexer/tests/hex.rs b/huff_lexer/tests/hex.rs
index b40ec99..b94cb11 100644
--- a/huff_lexer/tests/hex.rs
+++ b/huff_lexer/tests/hex.rs
@@ -19,6 +19,30 @@ fn parses_single_hex() {
     assert!(lexer.eof);
 }
 
+#[test]
+fn parses_bool() {
+    let source = "false true";
+    let flattened_source = FullFileSource { source, file: None, spans: vec![] };
+    let mut lexer = Lexer::new(flattened_source);
+
+    // The first token should be lexed as a Literal representing 0x00
+    let tok = lexer.next().unwrap().unwrap();
+    assert_eq!(tok, Token::new(TokenKind::Literal(str_to_bytes32("0")), Span::new(0..5, None)));
+    assert_eq!(lexer.current_span().deref(), &Span::new(0..5, None));
+
+    let _ = lexer.next(); // Whitespace
+
+    // The second token should be lexed as a Literal representing 0x01
+    let tok = lexer.next().unwrap().unwrap();
+    assert_eq!(tok, Token::new(TokenKind::Literal(str_to_bytes32("1")), Span::new(6..10, None)));
+    assert_eq!(lexer.current_span().deref(), &Span::new(6..10, None));
+
+    // We covered the whole source
+    lexer.next();
+    assert_eq!(lexer.current_span().end, source.len());
+    assert!(lexer.eof);
+}
+
 #[test]
 fn parses_odd_len_hex() {
     let source = "0x1";