-
Notifications
You must be signed in to change notification settings - Fork 278
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
Linking Extension #197
Linking Extension #197
Conversation
Awesome; thanks for looking into this! Seems like a super fun extension to explore. I like that this can work as a language extension in the sense that it can be "compiled away" by the static linker. Abstract syntax nits:
Text syntax nits:
Conceptual comments:
Unimportant thoughts:
Anyway, that's just a bunch of disorganized thoughts! Others are free to weigh in as well. 😃 |
Sure, I wasn't sure whether importing should be abstracted over all possible things to import as just globally "named" things but this probably makes more sense.
Yeah, this is a lot easier. I've made it
Works for me
As you can see, I was making the first pass with only
Would it then be sufficient to just chop off the extension?
Then when creating the documentation for this extension, say that "a fully compliant implementation of this extension is only required to look for a JSON version of the module". I would like to leave the door open for
I was aware of this when I started(if your referring to how the initial file is passed by filename instead of as a stream). This was done to be consistent for nesting imports, as in when I import a file containing imports, it has a starting location of it's file location which a stream doesn't have.
Like this?
Then have
Where I've been concerned that there would be issues if two directories contain a file named
like how in rust you might write use benchmarks::bitwise_ops::{AND, OR as LIB_OR, loop_subroutine}; except with |
Yes, something more or less like this is what I was imagining! Some food for thought on top of the above:
|
I believe all of the outstanding WIP parts have been resolved. There is now some documentation which describes the extension. There are some tests, which may not be all encompassing but I think hit the common cases of importing normally, having nested imports, having recursive imports(in this case, just importing yourself), and having two programs import the same program (inspired by diamond inheritance). Something to note, c794b92 sneaks in a bug fix for source positions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me!! I may have some suggestions about how to make the docs even clearer, but we could do those in a separate PR. If this is good to go in your opinion, let's merge it!
Great, besides any changes you may have I believe this is good to go. It defines the language extension that I was looking to add and then supports it by adding it to the Rust parsing for Bril programs plus a static linker
Sure, whatever would be easiest for you. |
Great! Thanks again for getting this creative idea started! |
Hmm, looks like the Clippy action failed… any chance you could take a look to see why this didn't run on the PR to catch this ahead of time? https://github.com/sampsyo/bril/runs/6514638293?check_suite_focus=true |
The issue comes from a new version of Clippy being released today alongside the new stable Rust release, 1.61. It contains improvements on lints that were tripped when the workflow was rerun on push. |
Ah, that would explain it! Darn you, Rust developers, for always improving Clippy! 😃 |
This wip pr is an attempt at an extension to allow linking/importing Bril programs. My understanding is that this has been previously proposed in sampsyo/cs6120#42 / https://www.cs.cornell.edu/courses/cs6120/2019fa/blog/making-function-calls-work/ but there hasn't been an attempt to make it somewhat official(except accidentally in #7). Inspired by this work, I am proposing a modified version of the
import
syntax/loadbril
idea.Motivation:
This extension is motivated in two parts. First, as the number of Bril programs grow, it becomes more useful to reuse code from previous programs, especially programs that implement utility functions like were seen in #192. Second, programs built from multiple Bril files could be an interesting use case for the source position extension #161, #167 in reporting error messages #191. Currently, source positions assume a single file of origin which is not necessarily true for an arbitrary front-end compiler.
Goals:
loadbril
be run-able bybrili
/brilirs
/other Bril-based tools without modification.Syntax:
Text form:
Grammar:
Example:
JSON form
Semantics
Path
Programmers can provide an absolute file path or relative file path from the current file to the Bril program that they want to include. The file must contain an extension of either
*.bril
or*.json
to specify how the program should be parsed. Ideally this would be any valid file path agnostic of platform but I haven't looked that into parsing paths so it's justr"([A-Za-z]|_|\.|[0-9]|\\|/|-)*", // path
at the moment.Names
From a file, names from that file can be "imported". Currently this is just valid function names but could be extended to support any extension with module level named things(like structs #138). To avoid clashes with other imported names or local names during usage, the programmer can provide an optional alias. Aliases are the way of getting around function name uniqueness. Attempts to use the same alias name as another imported name, imported alias, or local name will return an error. This allows the programmer to use write code like:
Mangling
After all imported paths have been resolved into programs and all names/aliases collected, the contained/used names in each program are mangled to ensure that names declared in a program(function declarations) are unique. The exact mangling is left undefined except that it produces valid names that
brili
/brilirs
would expect but in practice I'm just adding a bunch of underscores to the absolute file name. The only names left unmangled are those explicitly expected by an interpreter which currently is only the top levelmain
. Imported programs have theirmain
functions mangled, so they can be used if they are aliased.For example:
loadbril
loadbril
takes a filename as input and writes the final program in json form tostdout
. Thus, one canloadbril add.bril | brili
or add any arbitrary program in between in a composable fashion.loadbril
does not take the file itself instdin
like other tools such asbril2txt
do because we need to know the file location of the input file. This tool uses theAbstractProgram
representation so it should be as flexible asbril2txt
are in terms of new extensions(besides any additional mangling that an extension like structs might need). Note, all functions are imported from a program, not just the ones that are used. The elimination of this dead code is left to other tools.WIP
I'm seeking feedback on this extension especially because I've both deviated from the original proposal and that some parts were left unresolved as in sampsyo/cs6120#42. It might also be that this isn't the right way to go about linking for an IR. I think this approach works well in keeping everything modular but one could also add this as part of the interpreter or go about this another way.
Documentation, testing, and addressing the clippy lints are still to be done. I just wanted to hack something together to see what it would look like.