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

Wrong compilation order of appended files #155

Closed
cardillan opened this issue Oct 4, 2024 · 0 comments
Closed

Wrong compilation order of appended files #155

cardillan opened this issue Oct 4, 2024 · 0 comments
Labels
bug Something isn't working compiler Related to the code generator

Comments

@cardillan
Copy link
Owner

When using the --append command line option, the main source file is compiled first, then the additional (library) files. This can cause problems if the library files contain constants. Example

File lib.mnd

const SOME_CONSTANT = 10;

def foo()
    print(SOME_CONSTANT);
end;

File main.mnd

foo();

Now compiling these files with mindcode cm main.mnd --append lib.mnd, both of these files get parsed and then compiled. This is equivalent to compiling this code:

foo();

const SOME_CONSTANT = 10;

def foo()
    print(SOME_CONSTANT);
end;

Now, foo() is called just once, meaning it gets inlined by the compiler. The compiler will see print(SOME_CONSTANT) and will create SOME_CONSTANT as a global variable. Then comes const SOME_CONSTANT = 10;, which causes an error, because it is not possible to redefine a variable as a constant.

To fix this issue temporarily, the parse trees will be put together in an reverse order (first the appended files, then the main file).

The real solution with modules (#149) will combine constants, variables and functions in parse trees in a well defined order, instead of blindly gluing them together the way it is now.

@cardillan cardillan added bug Something isn't working compiler Related to the code generator labels Oct 7, 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 compiler Related to the code generator
Projects
None yet
Development

No branches or pull requests

1 participant