File: util.ha

package info (click to toggle)
hare-update 0.26.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,044 kB
  • sloc: makefile: 37; sh: 14
file content (85 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use ascii;
use common;
use common::{ltok, token, location, nonterminal};
use fmt;
use io;
use strings;
use vNEXT::lex;

fn version_tosym(name: const str) str = {
	return strings::replace(name, ".", "_")!;
};

fn pass(out: io::handle, lex: *lex::lexer, tok: token) void = {
	pass_whitespace(out, lex, tok);

	match (tok.1) {
	case let s: str =>
		switch (tok.0) {
		case ltok::NAME =>
			fmt::fprintf(out, `{}`, s)!;
		case ltok::LIT_STR =>
			fmt::fprint(out, '"')!;
			escape_string(out, s);
			fmt::fprint(out, '"')!;
		case => abort();
		};
	case let rn: rune =>
		fmt::fprint(out, "'")!;
		escape_rune(out, rn);
		fmt::fprint(out, "'")!;
	case let u: u64 =>
		fmt::fprint(out, u)!;
	case let f: f64 =>
		fmt::fprint(out, f)!;
	case void =>
		fmt::fprint(out, common::tokstr(tok))!;
	};
};

fn pass_whitespace(out: io::handle, lex: *lex::lexer, tok: token) void = {
	const loc = tok.2;
	for (prevloc.line < loc.line) {
		fmt::fprint(out, "\n")!;
		prevloc.line += 1;
		prevloc.col = 1;
	};
	for (prevloc.col < loc.col) {
		fmt::fprint(out, " ")!;
		prevloc.col += 1;
	};
	prevloc = lex::mkloc(lex);
};

fn escape_string(out: io::handle, s: str) void = {
	const iter = strings::iter(s);
	for (const rn => strings::next(&iter)) {
		escape_rune(out, rn);
	};
};

fn escape_rune(out: io::handle, rn: rune) void = {
	switch (rn) {
	case '\\' =>
		fmt::fprint(out, `\\`)!;
	case '\n' =>
		fmt::fprint(out, `\n`)!;
	case '\r' =>
		fmt::fprint(out, `\r`)!;
	case '\t' =>
		fmt::fprint(out, `\t`)!;
	case '"' =>
		fmt::fprint(out, `\"`)!;
	case =>
		if (ascii::isprint(rn)) {
			fmt::fprint(out, rn)!;
		} else {
			fmt::fprintf(out, `\u{:.4x}`, rn: u32)!;
		};
	};
};

fn push(t: ltok) void = {
	append(stack, t)!;
	sp += 1;
};