Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Contents of main block are incorrectly indented #648

Open
hdwalters opened this issue Dec 26, 2024 · 0 comments
Open

[BUG] Contents of main block are incorrectly indented #648

hdwalters opened this issue Dec 26, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@hdwalters
Copy link
Contributor

hdwalters commented Dec 26, 2024

Describe the bug
Because Amber uses the same code to translate the main block as it uses for defined functions, the code in the main block is incorrectly indented.

To Reproduce
Create this Amber script:

hwalters@Ghostwheel ~/git/amber-lang (main) 
$ cat main.ab 
fun foo() {
    echo "Function block should be indented"
}

main(args) {
    echo "Main block should not be indented"
    foo()
}

Build without running any postprocessors:

hwalters@Ghostwheel ~/git/amber-lang (main) 
$ amber build main.ab - --no-proc '*'
#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.4.0-alpha
# date: 2024-12-26 16:14:56
# #!/usr/bin/env amber
foo__0_v0() {
    echo "Function block should be indented"
}
declare -r args=("$0" "$@")
    echo "Main block should not be indented"
    foo__0_v0 ;
    __AF_foo0_v0__9_5="$__AF_foo0_v0";
    echo "$__AF_foo0_v0__9_5" > /dev/null 2>&1

Expected behavior
The contents of the main block should not be indented:

hwalters@Ghostwheel ~/git/amber-lang (main) 
$ amber build main.ab - --no-proc '*'
#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.4.0-alpha
# date: 2024-12-26 16:14:56
# #!/usr/bin/env amber
foo__0_v0() {
    echo "Function block should be indented"
}
declare -r args=("$0" "$@")
echo "Main block should not be indented"
foo__0_v0 ;
__AF_foo0_v0__9_5="$__AF_foo0_v0";
echo "$__AF_foo0_v0__9_5" > /dev/null 2>&1

Additional context
Currently, we generate Bash output in one of two ways: we either append individual lines directly to meta.stmt_queue, or build a Vec<String> with the lines, join on \n, and append the result to meta.stmt_queue. I would eventually like to add a new mechanism to format and indent lines in any scope as part of the 0.5.0 roadmap; but as a short-term workaround, I would like to decrease and increase the indentation level around the block translate call in src/modules/main.rs:

impl TranslateModule for Main {
    fn translate(&self, meta: &mut TranslateMetadata) -> String {
        ...
        meta.decrease_indent();
        let result = format!("{args}\n{}", self.block.translate(meta));
        meta.increase_indent();
        result
        ...
    }
}

Note, this will not cause an unsigned integer overflow, because the indentation level is stored as an i64. But just in case, we can cap the value at zero when doing the actual indentation:

impl TranslateMetadata {
    pub fn gen_indent(&self) -> String {
        INDENT_SPACES.repeat(cmp::max(self.indent, 0) as usize)
    }
}
@hdwalters hdwalters added the bug Something isn't working label Dec 26, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Dec 26, 2024
@hdwalters hdwalters self-assigned this Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant