Skip to content

Commit e9e8475

Browse files
committed
invalid change (?)
1 parent d38b7d8 commit e9e8475

File tree

7 files changed

+119
-34
lines changed

7 files changed

+119
-34
lines changed

cmd/src/main.ri

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Commands:
2121
run Create executable and run immediately.
2222
test Create and run a test build.
2323
check Scans, parses, and checks the files without compiling.
24+
fmt Format the Rivet code provided.
2425
version Print version number and exit.
2526
help Print this help and exit.
2627

@@ -41,7 +42,8 @@ func main() {
4142
is_check := cmd == "check";
4243
rivet.Compiler.new(process.args[2..], is_test, is_test or cmd == "run", is_check)!;
4344
},
44-
"new", "init" -> tools.new(process.args[2..], cmd == "init")!,
45+
"new", "init" -> tools.cmd_new(process.args[2..], cmd == "init")!,
46+
"fmt" -> tools.cmd_fmt(process.args[2..])!,
4547
"version" -> {
4648
console.ewriteln(utils.full_version());
4749
process.exit(0);

cmd/src/tools/cmd_fmt.ri

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2023 The Rivet Developers. All rights reserved.
2+
// Use of this source code is governed by an MIT license that can
3+
// be found in the LICENSE file.
4+
5+
import std/flag;
6+
import std/console;
7+
8+
import rivet;
9+
import rivet/ast;
10+
import rivet/fmt;
11+
import rivet/prefs;
12+
import rivet/utils;
13+
import rivet/report;
14+
15+
static fmtDescription := "Formats the given Rivet source files or recursively formats all files in the
16+
directory, then prints their formatted source to stdout.";
17+
18+
public func cmd_fmt(args: []string) -> ! {
19+
mut fp := flag.FlagParser.new(args);
20+
fp.set_application_name("rivet fmt");
21+
fp.set_arguments_description("[FILES|DIRECTORIES]");
22+
fp.set_application_short_description(fmtDescription);
23+
fp.add_usage_example("file.ri");
24+
fp.add_usage_example("directory/");
25+
fp.add_usage_example("-w file.ri");
26+
write_to_file := fp.bool_flag(
27+
"write-to-file", b'w', "Write result to source file(s) instead of to stdout.");
28+
remaining := fp.finalize() catch |err_fp| {
29+
console.ewriteln(fp.usage());
30+
utils.error(err_fp.to_string())
31+
};
32+
if remaining.len == 0 {
33+
console.ewriteln(fp.usage());
34+
utils.error("no input received");
35+
}
36+
mut source_files := @vec(ast.SourceFile);
37+
for input in remaining {
38+
mut prefs_ := prefs.Prefs(
39+
input: input,
40+
hide_all_warns: true
41+
);
42+
prefs_.check_input(input);
43+
prefs_.load_data()!;
44+
report.reportTable.prefs = prefs_;
45+
table_ := ast.Table.new(prefs_);
46+
mut compiler := rivet.Compiler(table: table_, prefs: prefs_);
47+
compiler.load_root_module()!;
48+
for sf in table_.source_files {
49+
source_files.push(sf);
50+
}
51+
}
52+
for sf in source_files {
53+
utils.info("sf: {}", sf.file);
54+
}
55+
}
56+
57+
//func format()

cmd/src/tools/cmd_new.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static availableTemplates := "Available templates:
2929
bin A simple binary project (default).
3030
lib A simple library project.";
3131

32-
public func new(args: []string, is_init: bool) -> ! {
32+
public func cmd_new(args: []string, is_init: bool) -> ! {
3333
mut template := "";
3434
mut fp := flag.FlagParser.new(args);
3535
if is_init {

lib/rivet/src/fmt/mod.ri

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (C) 2023 The Rivet Developers. All rights reserved.
2+
// Use of this source code is governed by an MIT license that can
3+
// be found in the LICENSE file.
4+
5+
import ../ast;
6+
import ../prefs;
7+
8+
public struct Formatter {
9+
prefs: prefs.Prefs;
10+
table: ast.Table;
11+
mut source_files: []ast.SourceFile;
12+
13+
write_to_file: bool;
14+
15+
public func fmt(mut self, source_files: []ast.SourceFile) -> Self {
16+
self.source_files = source_files;
17+
}
18+
}

lib/rivet/src/lib.ri

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct Compiler {
9191
}
9292
}
9393

94-
func load_root_module(mut self) -> ! {
94+
func load_root_module(mut self, recursive: bool := false) -> ! {
9595
files := if Path.is_directory(self.prefs.input) {
9696
mut filtered_files := self.table.filter_files(
9797
Directory.walk(self.prefs.input, ".ri")!
@@ -261,7 +261,7 @@ public struct Compiler {
261261
);
262262
}
263263

264-
func parse_files(mut self, mod_sym: ast.Module, files: []string) {
264+
public func parse_files(mut self, mod_sym: ast.Module, files: []string) {
265265
for sf in parser.Parser(table: self.table, prefs: self.prefs).parse_module(
266266
mod_sym, files
267267
) {

lib/rivet/src/prefs/mod.ri

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct Prefs {
8383
public mut run_output_args: []string;
8484

8585
public func new(args: []string, is_test: bool, run_output: bool, is_check: bool) -> !Self {
86-
prefs := Prefs(is_test: is_test, run_output: run_output, check: is_check);
86+
mut prefs := Prefs(is_test: is_test, run_output: run_output, check: is_check);
8787
mut fp := flag.FlagParser.new(args);
8888
fp.set_application_name(if is_test {
8989
"rivet test"
@@ -273,49 +273,56 @@ public struct Prefs {
273273
utils.error(err.to_string())
274274
};
275275
for arg in remaining_args {
276-
if !prefs.input.is_empty() {
277-
utils.error("the compiler can only receive one module");
278-
} else if !Path.exists(arg) {
279-
utils.error("`{}` does not exist", arg);
280-
} else if !(Path.is_directory(arg) or Path.is_file(arg)) {
281-
utils.error("`{}` is not a directory or file");
276+
if prefs.input.is_empty() {
277+
prefs.check_input(arg);
282278
} else {
283-
prefs.input = arg;
284-
if prefs.mod_name.is_empty() {
285-
prefs.mod_name = if Path.is_file(arg) {
286-
Path.basename(arg).all_before_of(".ri")
287-
} else {
288-
Path.basename(Path.resolve(arg) catch arg)
289-
};
290-
}
279+
utils.error("the compiler can only receive one module");
291280
}
292281
}
293-
294282
if prefs.input.is_empty() {
295283
utils.error("no input received");
296284
}
285+
prefs.load_data()!;
286+
return prefs;
287+
}
288+
289+
public func check_input(mut self, input: string) {
290+
if !Path.exists(input) {
291+
utils.error("`{}` does not exist", input);
292+
} else if !(Path.is_directory(input) or Path.is_file(input)) {
293+
utils.error("`{}` is not a directory or file");
294+
} else {
295+
self.input = input;
296+
if self.mod_name.is_empty() {
297+
self.mod_name = if Path.is_file(input) {
298+
Path.basename(input).all_before_of(".ri")
299+
} else {
300+
Path.basename(Path.resolve(input) catch input)
301+
};
302+
}
303+
}
304+
}
297305

298-
if prefs.is_test {
299-
prefs.mod_output = "_rivet_tests_runner_";
300-
} else if prefs.mod_output.is_empty() {
301-
prefs.mod_output = prefs.mod_name;
306+
public func load_data(mut self) -> ! {
307+
if self.is_test {
308+
self.mod_output = "_rivet_tests_runner_";
309+
} else if self.mod_output.is_empty() {
310+
self.mod_output = self.mod_name;
302311
}
303312

304-
if prefs.target_os == .Windows and !prefs.mod_output.ends_with(".exe") {
305-
prefs.mod_output = prefs.mod_output.concat(".exe");
313+
if self.target_os == .Windows and !self.mod_output.ends_with(".exe") {
314+
self.mod_output = self.mod_output.concat(".exe");
306315
}
307316

308-
if !Path.is_absolute(prefs.mod_output) {
309-
prefs.mod_output = Path.join(process.get_cwd()!, prefs.mod_output)!;
317+
if !Path.is_absolute(self.mod_output) {
318+
self.mod_output = Path.join(process.get_cwd()!, self.mod_output)!;
310319
}
311320

312-
prefs.mod_dir = if Path.is_file(prefs.input) {
313-
Path.dirname(Path.absolute(prefs.input)!) ?? prefs.input
321+
self.mod_dir = if Path.is_file(self.input) {
322+
Path.dirname(Path.absolute(self.input)!) ?? self.input
314323
} else {
315-
prefs.input
324+
self.input
316325
};
317-
prefs.library_path.push(prefs.mod_dir);
318-
319-
return prefs;
326+
self.library_path.push(self.mod_dir);
320327
}
321328
}

lib/rivet/src/resolver/stmts.ri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ../report;
88
extend Resolver {
99
func resolve_stmt(mut self, mut stmt: ast.Stmt) {
1010
match stmt is {
11+
.Comment -> {},
1112
.Empty as empty_pos -> report.error("BUG: empty statement found", empty_pos),
1213
.Expr as mut expr -> self.resolve_expr(expr),
1314
.VarDecl as var_stmt -> {

0 commit comments

Comments
 (0)