aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: ac52f8a90ba736c20a0e14c0deb05e4a9343e56f (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const std = @import("std");
pub const Grammar = @import("grammar.zig");
pub const gss = @import("gss.zig");
pub const recognizer = @import("recognizer.zig");
pub const argument = @import("argument.zig");
pub const Generator = @import("generator.zig").Generator;
pub const Scheduler = @import("scheduler.zig").Scheduler;

const Args = argument.Args;
const RecognizeArgs = argument.RecognizeArgs;
const GenerateArgs = argument.GenerateArgs;

fn write_result(
	writer: anytype,
	is_tty: bool,
	name: []const u8,
	index: usize,
	input: []const u8,
	accepted: bool,
) !void {
	if (is_tty) {
		try writer.print("{s}[{}] {s}\x1b[0m: \"\x1b[3m{s}\x1b[0m\"\n", .{
			name,
			index,
			if (accepted) "\x1b[32maccept"
			else "\x1b[31mreject",
			input,
		});
	} else {
		try writer.print("{s}[{}] {s}: \"{s}\"\n", .{
			name,
			index,
			if (accepted) "accept"
			else "reject",
			input,
		});
	}
}

fn recognize(args: *RecognizeArgs, allocator: std.mem.Allocator) !void {
	const stdout = std.io.getStdOut();
	var bufwriter = std.io.bufferedWriter(stdout.writer());
	const writer = bufwriter.writer();

	var bufreader = std.io.bufferedReader(args.input.file.reader());
	var reader = bufreader.reader();
	var index: usize = 0;
	const stderr = std.io.getStdErr();

	if (args.input.file.isTty()) {
		try stderr.writeAll("> ");
	}

	var read_arena = std.heap.ArenaAllocator.init(allocator);
	defer read_arena.deinit();

	while (try reader.readUntilDelimiterOrEofAlloc(
			read_arena.allocator(),
			'\n',
			std.math.maxInt(usize)
	)) |buffer| {

		const trimmed = std.mem.trim(u8, buffer, &std.ascii.whitespace);

		try write_result(
			writer,
			stdout.isTty(),
			args.input.name,
			index,
			trimmed,
			try recognizer.check(
				&args.grammar,
				trimmed,
				allocator
		));

		index += 1;

		if (args.input.file.isTty()) {
			try stderr.writeAll("> ");
		}
	}

	try bufwriter.flush();
}

const RandomGenerator = Generator(struct {
	const Self = @This();

	pub fn next(_: *Self, n: usize) usize {
		return std.crypto.random.uintLessThan(usize, n);
	}
});

pub fn generate_word(
	args: *GenerateArgs,
	generator: *RandomGenerator,
	allocator: std.mem.Allocator
) []const u8 {
	while (true) {
		const text = generator.sentential_from_grammar(
			&args.grammar,
			10000,
			allocator
		) catch continue;
		if (text.len >= args.min_length) {
			return text;
		}

		allocator.free(text);
	}
}

fn generate(args: *GenerateArgs, allocator: std.mem.Allocator) !void {
	var writer = args.output.file.writer();

	var scheduler: Scheduler(generate_word) = undefined;
	try scheduler.init(allocator);
	var generator = RandomGenerator {};

	for (0..args.count) |_| {
		try scheduler.push_task(.{args, &generator, allocator});
	}
	
	const results = try scheduler.deinit();
	defer allocator.free(results);

	for (results) |result| {
		try writer.print("{s}\n", .{result});
		allocator.free(result);
	}
}

pub fn main() !void {
	var gpa = std.heap.GeneralPurposeAllocator(.{}){};
	const allocator = gpa.allocator();
	defer {
		if (gpa.deinit() == .leak) {
			@panic("memory leak detected");
		}
	}

	var arguments = Args.parse(allocator);
	defer arguments.deinit(allocator);

	try switch(arguments) {
		.recognize => |*args| recognize(args, allocator),
		.generate => |*args| generate(args, allocator),
	};
}

test {
	std.testing.refAllDecls(@This());
}