commit e2c75cf8f0cb53f2bd00e89389b0cda87a85c5d6
parent 592afa2e375eb3cf8c03e4b41a08011a4700fa1f
Author: Drew DeVault <sir@cmpwn.com>
Date: Sat, 6 Mar 2021 11:14:53 -0500
hare::ast: add unparse
Diffstat:
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/hare/ast/unparse.ha b/hare/ast/unparse.ha
@@ -0,0 +1,21 @@
+use fmt;
+use io;
+use strio;
+
+// Unparses an identifier.
+export fn ident_unparse(out: *io::stream, ident: ident) (size | io::error) = {
+ let n = 0z;
+ for (let i = 0z; i < len(ident); i += 1) {
+ n += fmt::fprintf(out, "{}{}", ident[i],
+ if (i + 1 < len(ident)) "::"
+ else "")?;
+ };
+ return n;
+};
+
+// Unparses an identifier into a string. The caller must free the return value.
+export fn ident_unparse_s(ident: ident) str = {
+ let buf = strio::dynamic();
+ ident_unparse(buf, ident);
+ return strio::finish(buf);
+};