hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit ec3d6a2edcff1fcb493dfedf3f55a82982c3bb0a
parent bcdecc67522dc572e9bde2305a4f47d791c12591
Author: Sebastian <sebastian@sebsite.pw>
Date:   Tue,  1 Mar 2022 17:15:53 -0500

unparse: backslash-escaping in string/rune literals

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/parse/+test/expr.ha | 3+++
Mhare/unparse/expr.ha | 13++++++++++---
2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha @@ -147,6 +147,9 @@ 1337u32; 1337i64; 1337u64; + "backslashes\\and \"double quotes\""; + '\''; + '\\'; }; `); }; diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha @@ -9,6 +9,7 @@ use hare::ast; use hare::ast::{binarithm_op}; use hare::lex::{ltok}; use hare::lex; +use strings; // Unparses an [[ast::expr]]. export fn expr( @@ -471,11 +472,17 @@ fn constant( yield v; case let b: bool => return fmt::fprint(out, b); - // TODO: Escape these: case let s: str => - return fmt::fprintf(out, "\"{}\"", s); + const s = strings::multireplace(s, + (`\`, `\\`), (`"`, `\"`)); + defer free(s); + return fmt::fprintf(out, `"{}"`, s); case let r: rune => - return fmt::fprintf(out, "'{}'", r); + if (r == '\'' || r == '\\') { + return fmt::fprintf(out, `'\{}'`, r); + } else { + return fmt::fprintf(out, "'{}'", r); + }; }); case let ac: ast::array_constant => let z = fmt::fprint(out, "[")?;