corth-lang / Corth

A self-hosted stack based language like Forth
MIT License
7 stars 1 forks source link

Banning the usage of non-included procedures #55

Open HuseyinSimsek7904 opened 4 months ago

HuseyinSimsek7904 commented 4 months ago

Let's imagine a scenario:

File library1.corth:

include "linux_x86/stdio.corth"

proc hello-world -> in
  "Hello, world!\n" puts
end

File library2.corth:

include "library1.corth"

File main.corth:

include "library2.corth"

proc main int int -> int in
  hello-world
end 0 end

In this program, main.corth is trying to call the hello-world procedure which is inside library1.corth. Compiler allows this behavior because library2.corth includes library1.corth. C/C++ compiler also allows this. However this is an unwanted "feature", because in Corth, every source file should include the source files that it needs and only the source files that it needs. So the compiler should to complain, or at least create a warning about this situation.

Utkub24 commented 4 months ago

Wouldn't this cause a problem with recursive include statements? You'd need

#pragma once

+ this seems like it would cause complex source files to have unreasonably large include sections.

For example, in the scenario you presented wouldn't the user also need to include linux_x86/stdio.corth too?

// main.corth
include "library2.corth"
include "linux_x86/stdio.corth"

proc main int int -> int in
  hello-world
end 0 end

since puts is in linux_x86/stdio.corth and hello-world depends on it.