#! @file formatting.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the lowering of text a program writes while it runs: interpolated #! strings, whose holes are written as the values they hold, and the text of #! literals, decoded from how they are written. import sakura::analyzer; module sakura::compiler; #! An interpolated string: its text and the values of its holes, written in #! order into text a `string` then owns. A view of bytes, and a string, are #! written as the text they hold. #! @param this The emitter. #! @param node The `istring` node. #! @returns The string. pub def emitter::interpolated(&mut this, node: u32): operand { this.build.effect(op::format, span::::blank(), 0, 0); val run := this.c.checker.left_of(node); val count := this.c.checker.run_length(run); # The lines of a block are written without the indentation its closing # delimiter stands at, as a block of plain text is. var indent: u64 = 0; var block := false; if count != 0 && this.c.checker.kind_of(this.c.checker.run_at(run, 0)) == nk::string { val opened := this.c.checker.text_of(this.c.checker.run_at(run, 0)); block = opened.length >= 4 && opened.substr(0, 4) == "$\"\"\""; } if block && this.c.checker.kind_of(this.c.checker.run_at(run, count - 1)) == nk::string { val closed := this.c.checker.text_of(this.c.checker.run_at(run, count - 1)); var line := closed.length; while line > 0 && closed[line - 1] != '\n' { line--; } if line != 0 && closed.length >= 3 { indent = closed.length - 3 - line; } } for i in 0..count { val piece := this.c.checker.run_at(run, i as u32); if this.c.checker.kind_of(piece) == nk::string { var text := string::new(nothing {}); if block { this.block_piece(piece, i == 0, indent, &mut text); } else { this.piece_text(piece, &mut text); } if text.length != 0 { this.build.effect(op::format_text, span::::blank(), this.c.text_datum(text.span), 0); } continue; } val mark := this.cell_mark(); val value := this.value(piece); this.format_value(&value); this.release(mark); } # Text written where a view is wanted is made as a string all the same, which # the statement closes, so the view is only good while it runs. val made := this.c.checker.type_at(node); val text := this.owned_text(made); val wanted := this.c.checker.converted(node); return text if wanted == made; return this.convert(&text, wanted); } #! Writes a value into the text being made: the bytes a view of them or a #! string holds, or the value as its layout says it is written. #! @param this The emitter. #! @param value The value. pub def emitter::format_value(&mut this, value: &operand): void { val kind := this.c.checker.unalias(value.kind); val viewed := this.c.is_reference_type(kind) ? 0 : this.c.viewed(kind); if viewed != 0 { val element := this.c.shape_of(viewed); if element.size == 1 && element.kind != lk::record { var args: u32[2] = [this.build.load(access::i64, value.address, 0), this.build.load(access::i64, value.address, word_size)]; this.build.effect(op::format_bytes, args[0:2], 0, 0); return; } } if !this.c.is_reference_type(kind) && this.c.is_string(kind) { val index := this.c.layout_of(kind); val data := this.c.field_at(index, 1); val count := this.c.field_at(index, 2); var args: u32[2] = [this.build.load(access::i64, value.address, data.offset), this.build.load(access::i64, value.address, count.offset)]; this.build.effect(op::format_bytes, args[0:2], 0, 0); return; } var args := vector::::new(); value.args(&mut args); this.build.effect(op::format_value, args.span, this.c.layout_of(kind), 0); } #! The text of a piece of an interpolated block, decoded: each of its lines #! without the spaces trailing it and followed by a line break, and each line #! after one without the indentation the block's closing delimiter stands at. #! The first piece begins below the line the block opens. #! @param this The emitter. #! @param piece The `string` node of the piece. #! @param first Whether it is the first piece. #! @param indent How far the block's lines are indented. #! @param out Where the bytes are written. def emitter::block_piece(&mut this, piece: u32, first: bool, indent: u64, out: &mut string): void { val written := this.c.checker.text_of(piece); var from := 0u; var to := written.length; if written.length >= 4 && written.substr(0, 4) == "$\"\"\"" { from = 4; } else if written.length >= 1 && written[0] == '}' { from = 1; } if to >= from + 3 && written.substr(to - 3, 3) == "\"\"\"" { to -= 3; } else if to > from && written[to - 1] == '{' { to -= 1; } var at := from; if first { while at < to && written[at] != '\n' { at++; } return if at >= to; at = past_indent(written, at + 1, to, indent); } var start := at; while at < to { if written[at] != '\n' { at++; continue; } var stop := at; while stop > start && (written[stop - 1] == ' ' || written[stop - 1] == '\t') { stop--; } this.c.checker.decode_text(written.substr(start, stop - start), out); out.push('\n'); at = past_indent(written, at + 1, to, indent); start = at; } this.c.checker.decode_text(written.substr(start, to - start), out); } #! Where a line begins past the indentation of a block. #! @param text The text. #! @param at Where the line begins. #! @param end Where the text ends. #! @param indent How far the block's lines are indented. #! @returns Where the line begins past it. def past_indent(text: strview, at: u64, end: u64, indent: u64): u64 { var made := at; var skipped: u64 = 0; while skipped < indent && made < end && (text[made] == ' ' || text[made] == '\t') { made++; skipped++; } return made; } #! The text of a piece of an interpolated string, without what opens and #! closes it, decoded. #! @param this The emitter. #! @param piece The `string` node of the piece. #! @param out Where the bytes are written. def emitter::piece_text(&mut this, piece: u32, out: &mut string): void { val written := this.c.checker.text_of(piece); var from := 0u; var to := written.length; if written.length >= 4 && written.substr(0, 4) == "$\"\"\"" { from = 4; } else if written.length >= 2 && written.substr(0, 2) == "$\"" { from = 2; } else if written.length >= 1 && written[0] == '}' { from = 1; } if to >= from + 3 && written.substr(to - 3, 3) == "\"\"\"" { to -= 3; } else if to > from && (written[to - 1] == '"' || written[to - 1] == '{') { to -= 1; } this.c.checker.decode_text(written.substr(from, to - from), out); } #! Decodes the text a literal is written as: between its quotes, with its #! escapes, or of text spanning lines, each of its lines without the #! indentation the closing quotes stand at, and each followed by a line break. #! @param this The emitter. #! @param written The literal, with its quotes. #! @param out Where the bytes are written. pub def emitter::decode_literal(&mut this, written: strview, out: &mut string): void { if written.length < 6 || written.substr(0, 3) != "\"\"\"" { this.c.checker.decode_text(written.substr(1, written.length < 2 ? 0 : written.length - 2), out); return; } # The text begins below the line the block opens, and the closing quotes' own # line says how far every line of it is indented. val inner := written.substr(3, written.length - 6); var first := 0u; while first < inner.length && inner[first] != '\n' { first++; } var last := inner.length; while last > 0 && inner[last - 1] != '\n' { last--; } return if first >= inner.length || last <= first; val indent := inner.length - last; var line := first + 1; while line < last { var stop := line; while stop < last && inner[stop] != '\n' { stop++; } var at := line; var skipped := 0u; while skipped < indent && at < stop && (inner[at] == ' ' || inner[at] == '\t') { at++; skipped++; } var text := stop; while text > at && (inner[text - 1] == ' ' || inner[text - 1] == '\t') { text--; } this.c.checker.decode_text(inner.substr(at, text - at), out); out.push('\n'); line = stop + 1; } }