Skip to content

Commit 485c731

Browse files
committed
Use macro for echo
1 parent 8b61878 commit 485c731

File tree

355 files changed

+1008
-422
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

355 files changed

+1008
-422
lines changed

compiler-core/src/erlang.rs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ fn module_name_atom(module: &str) -> Document<'static> {
4141
struct Env<'a> {
4242
module: &'a str,
4343
function: &'a str,
44-
src_path: &'a Utf8Path,
45-
project_root: &'a Utf8Path,
4644
line_numbers: &'a LineNumbers,
4745
needs_function_docs: bool,
4846
echo_used: bool,
@@ -51,21 +49,13 @@ struct Env<'a> {
5149
}
5250

5351
impl<'env> Env<'env> {
54-
pub fn new(
55-
module: &'env str,
56-
src_path: &'env Utf8Path,
57-
project_root: &'env Utf8Path,
58-
function: &'env str,
59-
line_numbers: &'env LineNumbers,
60-
) -> Self {
52+
pub fn new(module: &'env str, function: &'env str, line_numbers: &'env LineNumbers) -> Self {
6153
let vars: im::HashMap<_, _> = std::iter::once(("_".into(), 0)).collect();
6254
Self {
6355
current_scope_vars: vars.clone(),
6456
erl_function_scope_vars: vars,
6557
needs_function_docs: false,
6658
echo_used: false,
67-
src_path,
68-
project_root,
6959
line_numbers,
7060
function,
7161
module,
@@ -224,7 +214,13 @@ fn module_document<'a>(
224214
};
225215

226216
let src_path_full = &module.type_info.src_path;
227-
let src_path_relative = src_path_full.strip_prefix(root).unwrap_or(src_path_full);
217+
let src_path_relative = EcoString::from(
218+
src_path_full
219+
.strip_prefix(root)
220+
.unwrap_or(src_path_full)
221+
.as_str(),
222+
)
223+
.replace("\\", "\\\\");
228224

229225
let mut needs_function_docs = false;
230226
let mut echo_used = false;
@@ -235,8 +231,7 @@ fn module_document<'a>(
235231
&module.name,
236232
module.type_info.is_internal,
237233
line_numbers,
238-
src_path_relative,
239-
root,
234+
src_path_relative.clone(),
240235
) {
241236
needs_function_docs = needs_function_docs || env.needs_function_docs;
242237
echo_used = echo_used || env.echo_used;
@@ -273,7 +268,11 @@ fn module_document<'a>(
273268
let module = docvec![
274269
header,
275270
"-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).",
276-
lines(2),
271+
line(),
272+
"-define(FILEPATH, \"",
273+
src_path_relative,
274+
"\").",
275+
line(),
277276
exports,
278277
documentation_directive,
279278
module_doc,
@@ -412,23 +411,17 @@ fn module_statement<'a>(
412411
module: &'a str,
413412
is_internal_module: bool,
414413
line_numbers: &'a LineNumbers,
415-
src_path: &'a Utf8Path,
416-
project_root: &'a Utf8Path,
414+
src_path: EcoString,
417415
) -> Option<(Document<'a>, Env<'a>)> {
418416
match statement {
419417
Definition::TypeAlias(TypeAlias { .. })
420418
| Definition::CustomType(CustomType { .. })
421419
| Definition::Import(Import { .. })
422420
| Definition::ModuleConstant(ModuleConstant { .. }) => None,
423421

424-
Definition::Function(function) => module_function(
425-
function,
426-
module,
427-
is_internal_module,
428-
line_numbers,
429-
src_path,
430-
project_root,
431-
),
422+
Definition::Function(function) => {
423+
module_function(function, module, is_internal_module, line_numbers, src_path)
424+
}
432425
}
433426
}
434427

@@ -437,8 +430,7 @@ fn module_function<'a>(
437430
module: &'a str,
438431
is_internal_module: bool,
439432
line_numbers: &'a LineNumbers,
440-
src_path: &'a Utf8Path,
441-
project_root: &'a Utf8Path,
433+
src_path: EcoString,
442434
) -> Option<(Document<'a>, Env<'a>)> {
443435
// Private external functions don't need to render anything, the underlying
444436
// Erlang implementation is used directly at the call site.
@@ -459,7 +451,7 @@ fn module_function<'a>(
459451
let function_name = escape_erlang_existing_name(function_name);
460452
let file_attribute = file_attribute(src_path, function, line_numbers);
461453

462-
let mut env = Env::new(module, src_path, project_root, function_name, line_numbers);
454+
let mut env = Env::new(module, function_name, line_numbers);
463455
let var_usages = collect_type_var_usages(
464456
HashMap::new(),
465457
std::iter::once(&function.return_type).chain(function.arguments.iter().map(|a| &a.type_)),
@@ -529,12 +521,11 @@ fn module_function<'a>(
529521
}
530522

531523
fn file_attribute<'a>(
532-
path: &'a Utf8Path,
524+
path: EcoString,
533525
function: &'a TypedFunction,
534526
line_numbers: &'a LineNumbers,
535527
) -> Document<'a> {
536528
let line = line_numbers.line_number(function.location.start);
537-
let path = EcoString::from(path.as_str()).replace("\\", "\\\\");
538529
docvec!["-file(\"", path, "\", ", line, ")."]
539530
}
540531

@@ -2011,22 +2002,8 @@ fn panic<'a>(location: SrcSpan, message: Option<&'a TypedExpr>, env: &mut Env<'a
20112002

20122003
fn echo<'a>(body: Document<'a>, location: &SrcSpan, env: &mut Env<'a>) -> Document<'a> {
20132004
env.echo_used = true;
2014-
2015-
let relative_path = env
2016-
.src_path
2017-
.strip_prefix(env.project_root)
2018-
.unwrap_or(env.src_path)
2019-
.as_str();
2020-
2021-
let relative_path_doc = EcoString::from(relative_path)
2022-
.replace("\\", "\\\\")
2023-
.to_doc();
2024-
2025-
let relative_path_doc = docvec!["\"", relative_path_doc, "\""];
2026-
20272005
"echo".to_doc().append(wrap_args(vec![
20282006
body,
2029-
relative_path_doc,
20302007
env.line_numbers.line_number(location.start).to_doc(),
20312008
]))
20322009
}

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__allowed_string_escapes.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 531
34
expression: "pub fn a() { \"\\n\" \"\\r\" \"\\t\" \"\\\\\" \"\\\"\" \"\\\\^\" }"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68
pub fn a() { "\n" "\r" "\t" "\\" "\"" "\\^" }
79

810
----- COMPILED ERLANG
911
-module(my@mod).
1012
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
11-
13+
-define(FILEPATH, "project/test/my/mod.gleam").
1214
-export([a/0]).
1315

1416
-file("project/test/my/mod.gleam", 1).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__binop_parens.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 425
34
expression: "\npub fn main() {\n let a = 2 * {3 + 1} / 2\n let b = 5 + 3 / 3 * 2 - 6 * 4\n b\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -14,7 +16,7 @@ pub fn main() {
1416
----- COMPILED ERLANG
1517
-module(my@mod).
1618
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17-
19+
-define(FILEPATH, "project/test/my/mod.gleam").
1820
-export([main/0]).
1921

2022
-file("project/test/my/mod.gleam", 2).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__bit_pattern_shadowing.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 988
34
expression: "\npub fn main() {\n let code = <<\"hello world\":utf8>>\n let pre = 1\n case code {\n <<pre:bytes-size(pre), _:bytes>> -> pre\n _ -> panic\n }\n} "
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -16,7 +18,7 @@ pub fn main() {
1618
----- COMPILED ERLANG
1719
-module(my@mod).
1820
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
19-
21+
-define(FILEPATH, "project/test/my/mod.gleam").
2022
-export([main/0]).
2123

2224
-file("project/test/my/mod.gleam", 2).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__block_assignment.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 469
34
expression: "\npub fn main() {\n let x = {\n 1\n 2\n }\n x\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -16,7 +18,7 @@ pub fn main() {
1618
----- COMPILED ERLANG
1719
-module(my@mod).
1820
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
19-
21+
-define(FILEPATH, "project/test/my/mod.gleam").
2022
-export([main/0]).
2123

2224
-file("project/test/my/mod.gleam", 2).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__constant_named_module_info.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 787
34
expression: "\npub const module_info = 1\n\npub fn main() {\n module_info\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -14,7 +16,7 @@ pub fn main() {
1416
----- COMPILED ERLANG
1517
-module(my@mod).
1618
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17-
19+
-define(FILEPATH, "project/test/my/mod.gleam").
1820
-export([main/0]).
1921

2022
-file("project/test/my/mod.gleam", 4).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__constant_named_module_info_imported.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 801
34
expression: "\nimport some_module\n\npub fn main() {\n some_module.module_info\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -14,7 +16,7 @@ pub fn main() {
1416
----- COMPILED ERLANG
1517
-module(my@mod).
1618
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17-
19+
-define(FILEPATH, "project/test/my/mod.gleam").
1820
-export([main/0]).
1921

2022
-file("project/test/my/mod.gleam", 4).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__constant_named_module_info_imported_qualified.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 822
34
expression: "\nimport some_module.{module_info}\n\npub fn main() {\n module_info\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -14,7 +16,7 @@ pub fn main() {
1416
----- COMPILED ERLANG
1517
-module(my@mod).
1618
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17-
19+
-define(FILEPATH, "project/test/my/mod.gleam").
1820
-export([main/0]).
1921

2022
-file("project/test/my/mod.gleam", 4).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__constant_named_module_info_with_function_inside.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 843
34
expression: "\npub fn function() {\n 1\n}\n\npub const module_info = function\n\npub fn main() {\n module_info()\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -18,7 +20,7 @@ pub fn main() {
1820
----- COMPILED ERLANG
1921
-module(my@mod).
2022
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
21-
23+
-define(FILEPATH, "project/test/my/mod.gleam").
2224
-export([function/0, main/0]).
2325

2426
-file("project/test/my/mod.gleam", 2).

compiler-core/src/erlang/snapshots/gleam_core__erlang__tests__constant_named_module_info_with_function_inside_imported.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
source: compiler-core/src/erlang/tests.rs
3+
assertion_line: 861
34
expression: "\nimport some_module\n\npub fn main() {\n some_module.module_info()\n}\n"
5+
snapshot_kind: text
46
---
57
----- SOURCE CODE
68

@@ -14,7 +16,7 @@ pub fn main() {
1416
----- COMPILED ERLANG
1517
-module(my@mod).
1618
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
17-
19+
-define(FILEPATH, "project/test/my/mod.gleam").
1820
-export([main/0]).
1921

2022
-file("project/test/my/mod.gleam", 4).

0 commit comments

Comments
 (0)