Skip to content

Instantly share code, notes, and snippets.

@lantos1618
Created February 25, 2024 14:04
Show Gist options
  • Save lantos1618/89ecfca3f593990775acc799d51588ed to your computer and use it in GitHub Desktop.
Save lantos1618/89ecfca3f593990775acc799d51588ed to your computer and use it in GitHub Desktop.
custom programing language
HtmlTag: enum = {
div = "Div",
p = "P",
h1 = "H1",
h2 = "H2",
}
HtmlNode = {
tag: HtmlTag,
attr: Map<String, String>,
children: Vec<HtmlNode>,
render: () String {
let mut result = String::new();
result.push_str("<");
result.push_str(self.tag);
for (key, value) in self.attr {
result.push_str(" ");
result.push_str(key);
result.push_str("=\"");
result.push_str(value);
result.push_str("\"");
}
result.push_str(">");
for child in self.children {
result.push_str(child.render());
}
result.push_str("</");
result.push_str(self.tag);
result.push_str(">");
result
}
}
Div: (attr: Map<String, String>, children: Vec<HtmlNode>) -> HtmlNode = {
tag: HtmlTag::div,
attr: attr,
children: children,
}
genHtml: comp Fn = () {
loop (HtmlTag, (tag) {
`tag`(attr: Map<String, String>, children: Vec<HtmlNode>) -> HtmlNode {
tag: tag,
attr: attr,
children: children,
}
})
}
genHtml()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment