Zulip Chat Archive
Stream: Is there code for X?
Topic: Make the syntax for updating variables common.
Asei Inoue (Apr 02 2024 at 13:38):
This is a part of my code in mdgen:
/-- new notaion to represent `x := x ++ e`. -/
syntax ident "++=" term : doElem
macro_rules
| `(doElem| $x:ident ++= $e:term) => `(doElem| ($x) := ($x) ++ ($e))
/-- A chunk of grouped code for conversion to markdown. -/
structure Block where
content : String
toCodeBlock : Bool
deriving Repr
private def buildBlocks (lines : List String) : List Block := Id.run do
let mut toCodeBlock := true
let mut blocks : List Block := []
let mut content := ""
for line in lines do
if line.endsWith "--#" then
continue
if line.startsWith "/-" && ! line.startsWith "/--" then
blocks ++= [{content := content.trim, toCodeBlock := toCodeBlock}]
toCodeBlock := ! toCodeBlock
content := line ++ "\n"
if line.endsWith "-/" then
blocks ++= [{content := content.trim, toCodeBlock := toCodeBlock}]
toCodeBlock := ! toCodeBlock
content := ""
else if line.endsWith "-/" && ! toCodeBlock then
content ++= line
blocks ++= [{content := content.trim, toCodeBlock := toCodeBlock}]
toCodeBlock := ! toCodeBlock
content := ""
else
content ++= line ++ "\n"
if content != "" then
blocks ++= [{content := content.trim, toCodeBlock := toCodeBlock}]
return blocks
The following codes appear repeatedly in the above code:
blocks ++= [{content := content.trim, toCodeBlock := toCodeBlock}]
toCodeBlock := ! toCodeBlock
Could this be made common? Obviously this can be done using macros, but I want to make it common as a function rather than a macro.
Thank you.
Last updated: May 02 2025 at 03:31 UTC