adam-mcdaniel / oakc

A portable programming language with a compact intermediate representation
Apache License 2.0
729 stars 21 forks source link

Implemented 'no_std' and 'std' flags #25

Closed adam-mcdaniel closed 4 years ago

adam-mcdaniel commented 4 years ago

This PR implements the no_std and std flags, which explicitly dismiss the standard library and explicitly require the standard library, respectively. By default, the standard library is not required, just like C. When there are conflicting requirements (when the std and no_std flags are both present) a compiler error is emitted.

Additionally, this PR changes the structure of the target directory in the crate, and slightly changes the Target trait itself. The Target trait previously had prelude and postlude methods; these methods have been renamed to core_prelude and core_postlude. These methods ONLY emit code implementing the 14 core VM instructions. Thats it.

A new Target method, the std method, returns the code implementing standard library foreign functions, such as prn, prc, prs, and getch. It is likely that as the standard library grows, these will adopt different names.

When the standard library is required, the std.ok and core.ok files are pasted at the end of the user's code. If the standard library is not required, then just core.ok is pasted at the end of the user's code.

adam-mcdaniel commented 4 years ago

The only thing left to decide is whether or not the standard library should be included by default. What do you think, @kevinramharak? Right now, this PR does not include it by default. See the examples for how it works.

kevinramharak commented 4 years ago

I would say it depends on the target audience. Some languages include the stdlib by default because of convenience and because it is usually expected by novice programmers. I like the idea of having both flags and stdlib being optional. It gives more control on what the resulting code is going to be. The convenience part is easily fixed with good error reporting. For example if print is a stdlib function and a program calls it without stdlib being included the error reporter can be smart enough to ask if they meant to include the stdlib.

adam-mcdaniel commented 4 years ago

Hmmm. Adding error messages for std functions would be a pretty difficult task, depending on the level of obscurity of the function. Maybe we could add have you included the standard library? error messages for common functions like putstrln, but not for EVERY function like fmod or sqrt. I feel like that's an acceptable compromise: new users get the error messages for the functions that they will surely first be exposed to, and more experienced users automatically know why something like std::DateTime is not defined, for example.

kevinramharak commented 4 years ago

I think its fairly easy or am i missing something? If there is a spec for stdlib all those function names should be known right, it might not be the most elegant way but if the called function does not exist and if its name is present in a static array of strings representing those names then a helpful hint can be shown. It doesn't have to be implemented like this and theres no hurry. I just think having good error reporting is a good compiler feature.

adam-mcdaniel commented 4 years ago

It would be easy in the sense that it would be simple, but not in the sense that it would be a small amount of code. It would involve checking against an array of identifiers constituting all the STD symbols, like you said. It definitely could be done, but I think that a comprehensive standard library spec should be written before we consider adding error reporting about the standard library this way.

kevinramharak commented 4 years ago

Oh definitly. I was just trying to provide an alternative to the con of having the stdlib not included by default