From 8e440823da701dc562facd2391f78131b9cfc21f Mon Sep 17 00:00:00 2001 From: Sascha Hanse Date: Sun, 23 May 2021 00:54:42 +0200 Subject: [PATCH 1/3] feat: use `#` for comments and `;` as delimiter for single line expressions --- doc/src/ch02-lang/ch02-labels.md | 16 ++++---- .../ch02-lang/ch03-macros/ch01-builtins.md | 10 ++--- etk-asm/src/parse/asm.pest | 4 +- etk-asm/src/parse/mod.rs | 37 +++++++++++++++---- etk-asm/tests/asm/simple-constructor/ctor.etk | 30 +++++++-------- etk-asm/tests/asm/simple-constructor/main.etk | 6 +-- 6 files changed, 63 insertions(+), 40 deletions(-) diff --git a/doc/src/ch02-lang/ch02-labels.md b/doc/src/ch02-lang/ch02-labels.md index 67a4292f..1de13681 100644 --- a/doc/src/ch02-lang/ch02-labels.md +++ b/doc/src/ch02-lang/ch02-labels.md @@ -5,16 +5,16 @@ Manually counting out jump destination addresses would be a monumentally pointle ```rust # extern crate etk_asm; # let src = r#" -label0: ; <- This is a label called "label0", - ; and it has the value 0, since it is - ; before any instructions in scope. +label0: # <- This is a label called "label0", + # and it has the value 0, since it is + # before any instructions in scope. jumpdest - push1 label0 ; <- Here we push the value of "label0", - ; which is zero, onto the stack. + push1 label0 # <- Here we push the value of "label0", + # which is zero, onto the stack. - jump ; Now we jump to zero, which is a - ; `jumpdest` instruction, looping forever. + jump # Now we jump to zero, which is a + # `jumpdest` instruction, looping forever. # "#; # let mut ingest = etk_asm::ingest::Ingest::new(Vec::new()); # ingest.ingest(file!(), src).unwrap(); @@ -37,7 +37,7 @@ That's not all! You can also use labels to calculate lengths: # let src = r#" push1 start push1 end -sub ; <- Will leave a 3 on the stack. +sub # <- Will leave a 3 on the stack. stop start: diff --git a/doc/src/ch02-lang/ch03-macros/ch01-builtins.md b/doc/src/ch02-lang/ch03-macros/ch01-builtins.md index 401a8573..cd14dfd7 100644 --- a/doc/src/ch02-lang/ch03-macros/ch01-builtins.md +++ b/doc/src/ch02-lang/ch03-macros/ch01-builtins.md @@ -47,8 +47,8 @@ The path is resolved relative to the current file. #### Source: `main.etk` ```ignore -some_label: ; <- Not visible in `other.etk`. - push1 some_label ; <- Pushes a zero on the stack. +some_label: # <- Not visible in `other.etk`. + push1 some_label # <- Pushes a zero on the stack. %include("other.etk") ``` @@ -56,8 +56,8 @@ some_label: ; <- Not visible in `other.etk`. #### Source: `other.etk` ```ignore -different_label: ; <- Not visible in `main.etk`. - push1 different_label ; <- ALSO pushes a zero on the stack. +different_label: # <- Not visible in `main.etk`. + push1 different_label # <- ALSO pushes a zero on the stack. ``` #### After Expansion @@ -109,7 +109,7 @@ For example: ```rust # extern crate etk_asm; # let src = r#" -push4 selector("_burn(address,bytes32,uint256)") ; <- expands to 0x63936327 +push4 selector("_burn(address,bytes32,uint256)") # <- expands to 0x63936327 # "#; # let mut output = Vec::new(); # let mut ingest = etk_asm::ingest::Ingest::new(&mut output); diff --git a/etk-asm/src/parse/asm.pest b/etk-asm/src/parse/asm.pest index 035712e4..d814eb3b 100644 --- a/etk-asm/src/parse/asm.pest +++ b/etk-asm/src/parse/asm.pest @@ -1,4 +1,4 @@ -program = _{ SOI ~ "\n"* ~ (stmt ~ "\n"+)* ~ stmt? ~ EOI } +program = _{ SOI ~ NEWLINE* ~ (stmt ~ (NEWLINE+|";"))* ~ stmt? ~ EOI } stmt = _{ expr } @@ -57,4 +57,4 @@ include_hex = !{ "include_hex" ~ arguments } push_macro = !{ "push" ~ arguments } WHITESPACE = _{ " " | "\t" } -COMMENT = _{ ";" ~ (!"\n" ~ ANY)* } +COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* } diff --git a/etk-asm/src/parse/mod.rs b/etk-asm/src/parse/mod.rs index a0902f14..a8f1cafe 100644 --- a/etk-asm/src/parse/mod.rs +++ b/etk-asm/src/parse/mod.rs @@ -190,10 +190,33 @@ mod tests { assert_matches!(parse_asm(asm), Ok(e) if e == expected); } + #[test] + fn parse_single_line() { + let asm = r#" + push1 0b0; push1 0b1 + "#; + let expected = nodes![Op::Push1(Imm::from([0])), Op::Push1(Imm::from([1]))]; + assert_matches!(parse_asm(asm), Ok(e) if e == expected); + } + + #[test] + fn parse_mixed_lines() { + let asm = r#" + push1 0b0; push1 0b1 + push1 0b1 + "#; + let expected = nodes![ + Op::Push1(Imm::from([0])), + Op::Push1(Imm::from([1])), + Op::Push1(Imm::from([1])) + ]; + assert_matches!(parse_asm(asm), Ok(e) if e == expected); + } + #[test] fn parse_push_binary() { let asm = r#" - ; simple cases + # simple cases push1 0b0 push1 0b1 "#; @@ -204,7 +227,7 @@ mod tests { #[test] fn parse_push_octal() { let asm = r#" - ; simple cases + # simple cases push1 0o0 push1 0o7 push2 0o400 @@ -220,17 +243,17 @@ mod tests { #[test] fn parse_push_decimal() { let asm = r#" - ; simple cases + # simple cases push1 0 push1 1 - ; left-pad values too small + # left-pad values too small push2 42 - ; barely enough for 2 bytes + # barely enough for 2 bytes push2 256 - ; just enough for 4 bytes + # just enough for 4 bytes push4 4294967295 "#; let expected = nodes![ @@ -249,7 +272,7 @@ mod tests { #[test] fn parse_push_hex() { let asm = r#" - push1 0x01 ; comment + push1 0x01 # comment push1 0x42 push2 0x0102 push4 0x01020304 diff --git a/etk-asm/tests/asm/simple-constructor/ctor.etk b/etk-asm/tests/asm/simple-constructor/ctor.etk index 99887c89..7a06b587 100644 --- a/etk-asm/tests/asm/simple-constructor/ctor.etk +++ b/etk-asm/tests/asm/simple-constructor/ctor.etk @@ -1,19 +1,19 @@ -; -- Instructions -- -- Current stack layout -- +# -- Instructions -- -- Current stack layout -- -pc ; [0] -pc ; [1, 0] -push1 start ; [start, 1, 0] -add ; [start+1, 0] -dup1 ; [start+1, start+1, 0] -codesize ; [codesize, start+1, start+1, 0] -sub ; [length, start+1, 0] -swap2 ; [0, start+1, length] -swap1 ; [start+1, 0, length] -dup3 ; [length, start+1, 0, length] -swap1 ; [start+1, length, 0, length] -dup3 ; [0, start+1, length, 0, length] -codecopy ; [0, length] -return ; [] +pc # [0] +pc # [1, 0] +push1 start # [start, 1, 0] +add # [start+1, 0] +dup1 # [start+1, start+1, 0] +codesize # [codesize, start+1, start+1, 0] +sub # [length, start+1, 0] +swap2 # [0, start+1, length] +swap1 # [start+1, 0, length] +dup3 # [length, start+1, 0, length] +swap1 # [start+1, length, 0, length] +dup3 # [0, start+1, length, 0, length] +codecopy # [0, length] +return # [] start: %include("main.etk") diff --git a/etk-asm/tests/asm/simple-constructor/main.etk b/etk-asm/tests/asm/simple-constructor/main.etk index f105617f..12244212 100644 --- a/etk-asm/tests/asm/simple-constructor/main.etk +++ b/etk-asm/tests/asm/simple-constructor/main.etk @@ -1,6 +1,6 @@ -;;; -; Hello World -;;; +### +# Hello World +### push1 32 push1 31 From 7f90946f7646b2147e0da0955ef2c5989c0a1d84 Mon Sep 17 00:00:00 2001 From: Sascha Hanse Date: Sun, 23 May 2021 03:20:01 +0200 Subject: [PATCH 2/3] use new comment --- doc/src/ch02-lang/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/ch02-lang/README.md b/doc/src/ch02-lang/README.md index e3bcb8e9..3c0350cb 100644 --- a/doc/src/ch02-lang/README.md +++ b/doc/src/ch02-lang/README.md @@ -26,7 +26,7 @@ loop: jumpi pop -stop ; This halts execution +stop # This halts execution # "#; # let mut ingest = etk_asm::ingest::Ingest::new(Vec::new()); # ingest.ingest(file!(), src).unwrap(); From 02b4b5cd4671ef6b2a4b33ec31e6919dcd5fde0f Mon Sep 17 00:00:00 2001 From: Sascha Hanse Date: Sun, 23 May 2021 16:38:02 +0200 Subject: [PATCH 3/3] escape asm comments so mdbook/rustdoc dont strip them --- doc/src/ch02-lang/ch02-labels.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/src/ch02-lang/ch02-labels.md b/doc/src/ch02-lang/ch02-labels.md index 1de13681..342d6395 100644 --- a/doc/src/ch02-lang/ch02-labels.md +++ b/doc/src/ch02-lang/ch02-labels.md @@ -6,15 +6,15 @@ Manually counting out jump destination addresses would be a monumentally pointle # extern crate etk_asm; # let src = r#" label0: # <- This is a label called "label0", - # and it has the value 0, since it is - # before any instructions in scope. + ## and it has the value 0, since it is + ## before any instructions in scope. jumpdest push1 label0 # <- Here we push the value of "label0", - # which is zero, onto the stack. + ## which is zero, onto the stack. jump # Now we jump to zero, which is a - # `jumpdest` instruction, looping forever. + ## `jumpdest` instruction, looping forever. # "#; # let mut ingest = etk_asm::ingest::Ingest::new(Vec::new()); # ingest.ingest(file!(), src).unwrap();