From 8139b9ed1109868d84cdc39cad495db4a628afa1 Mon Sep 17 00:00:00 2001 From: Champii Date: Mon, 4 Oct 2021 02:26:20 -0700 Subject: [PATCH] v0.1.7 (#51) * Version bump * Generated files for new version * Add complete TypeSignature parsing (#30) * Generated files for new version * Fix branch name that is invalid cargo version * Fix readme build status * Unified version name in readme and cargo.toml * Merged TypeSignature with FuncType * Deactivated out of bounds error for literal index * Removed some clones * Clippy free * Add forgotten file * Generated files for new version * Readme * Generated files for new version * Remove old artefacts * Fix fn arg polymorphism * Add some typecheck failure tests * Add multiline struct const * Add struct field type failure * Generated files for new version Co-authored-by: Florian Greiner * Generated files for new version * Merge * Merge * Mostly cleanup (#40) * Cleanup of type from implementation * Separated struct type into its own module * Separated Types into their own module * Better FuncType implementation * Clippy * Factorized some code in ty::Type * More sensible module and macro exports * Separated resolver from ast * Cleanup, import and code concision * Exchanged concat_idents for paste macro in trait generation * Generated files for new version Co-authored-by: Florian Greiner * Generated files for new version * Added LLVM12 requirement in README * Generated files for new version * Clang instead of llc (#45) * Removed llc to keep only clang as a build dependency * Generated files for new version Co-authored-by: Florian Greiner * Generated files for new version * Create LICENSE * Optimisation pass (#48) * Add (too many) LLVM pass optimizations * Generated files for new version * Interpreter (#49) * Add basic interpreter and fix reassign resolve * Add some better colors for everything * Fix cargo warnings * Color fix and REPL section in README * Readme * First work on repl toplevel decl * Generated files for new version * Top level decl * Colors * Fix cargo * Generated files for new version * Cleanup Co-authored-by: Florian Greiner * Generated files for new version Co-authored-by: Florian Greiner --- .github/templates/Cargo.toml | 2 +- .github/templates/README.md | 47 +- .github/version | 2 +- Cargo.lock | 179 ++++- Cargo.toml | 4 +- LICENSE | 674 ++++++++++++++++++ README.md | 55 +- src/bin/main.rs | 60 +- src/lib/ast_lowering/return_placement.rs | 4 +- src/lib/codegen/codegen_context.rs | 76 +- src/lib/codegen/interpreter.rs | 212 ++++++ src/lib/codegen/mod.rs | 64 +- src/lib/diagnostics/diagnostic.rs | 60 +- src/lib/helpers/config.rs | 18 +- src/lib/hir/has_hir_id.rs | 8 +- src/lib/hir/hir_printer.rs | 2 +- src/lib/hir/tree.rs | 17 +- src/lib/infer/constraint.rs | 2 +- src/lib/infer/state.rs | 46 +- src/lib/parser/parser_impl.rs | 8 +- src/lib/parser/parsing_context.rs | 54 +- src/lib/parser/source_file.rs | 38 +- src/lib/parser/token.rs | 10 + src/lib/resolver/resolve_ctx.rs | 24 +- src/lib/rock.rs | 70 +- src/lib/testcases/basic/reassign_self/main.rk | 4 + .../testcases/basic/reassign_self/main.rk.out | 1 + .../basic/reassign_self/main.rk.stdout | 0 src/lib/tests.rs | 4 + 29 files changed, 1554 insertions(+), 191 deletions(-) create mode 100644 LICENSE create mode 100644 src/lib/codegen/interpreter.rs create mode 100644 src/lib/testcases/basic/reassign_self/main.rk create mode 100644 src/lib/testcases/basic/reassign_self/main.rk.out create mode 100644 src/lib/testcases/basic/reassign_self/main.rk.stdout diff --git a/.github/templates/Cargo.toml b/.github/templates/Cargo.toml index 73b2e981..554ec945 100644 --- a/.github/templates/Cargo.toml +++ b/.github/templates/Cargo.toml @@ -11,7 +11,6 @@ regex = "1" env_logger = "0.5.12" log = "0.4" bitflags = "1.2.1" -concat-idents = "1.1.2" # inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm11-0"] } inkwell = { version = "0.1.0-beta.3", features = ["llvm12-0"] } either = "1.5" @@ -20,6 +19,7 @@ serde_derive = '*' bincode = "*" colored = "2.0.0" paste = "1.0.5" +rustyline = "9.0.0" [lib] name = "rock" diff --git a/.github/templates/README.md b/.github/templates/README.md index c0fd0e8b..15cfa52f 100644 --- a/.github/templates/README.md +++ b/.github/templates/README.md @@ -13,6 +13,8 @@ It's highly inspired from Livescript, and will borrow (pun intended) some featur - [With cargo from Git]( #with-cargo-from-git ) - [From sources]( #from-sources ) - [Quickstart]( #quickstart ) + - [Basic setup]( #basic-setup ) + - [REPL]( #repl ) - [Showcases]( #showcases ) - [Development notes]( #development-notes ) @@ -24,6 +26,7 @@ It's highly inspired from Livescript, and will borrow (pun intended) some featur - Typeclass (Traits) - Parametric Polymorphism by default - Compile to LLVM IR +- REPL (ALPHA) ## Install @@ -33,6 +36,8 @@ How to install and run the compiler: ### Using released binary +You will need `clang` somewhere in your $PATH + Linux x86_64 only [Rock {version}](https://github.com/Champii/Rock/releases/download/{version}/rock) (Tested on arch, btw) @@ -43,14 +48,18 @@ chmod +x rock ./rock -V ``` -### With cargo from git +### From source + +You will need `llvm-12.0.1` and `clang-12.0.1` somewhere in your $PATH + +#### With cargo from git ``` sh cargo install --git https://github.com/Champii/Rock rock -V ``` -### From sources +#### Manual clone and build from git ``` sh git clone https://github.com/Champii/Rock.git @@ -60,6 +69,8 @@ cargo run -- -V ## Quickstart +### Basic setup + Lets create a new project folder to compute some factorials ``` sh @@ -97,6 +108,38 @@ Should output 24 ``` +Take a look at `rock --help` for a quick tour of its flags and arguments + +### REPL + +You can start a REPL session with + +``` sh +rock -r +# OR +rock --repl +``` + +``` sh +Rock: {version} +---- + +Type ':?' for help + +> add a b = a + b +> let x = 30 +30 +> let y = 12 +12 +> add x, y +42 +> :t add +add: (Int64 -> Int64 -> Int64) +> _ +``` + +Only supports basic expressions for now. + ## Showcases ### Polymophic function diff --git a/.github/version b/.github/version index 82942c39..fad30c54 100644 --- a/.github/version +++ b/.github/version @@ -1 +1 @@ -v0.1.6 +v0.1.7 diff --git a/Cargo.lock b/Cargo.lock index ca6d0268..989048b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + [[package]] name = "bincode" version = "1.3.3" @@ -73,6 +79,17 @@ dependencies = [ "vec_map", ] +[[package]] +name = "clipboard-win" +version = "4.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4ea1881992efc993e4dc50a324cdbd03216e41bdc8385720ff47efc9bd2ca8" +dependencies = [ + "error-code", + "str-buf", + "winapi", +] + [[package]] name = "colored" version = "2.0.0" @@ -85,13 +102,24 @@ dependencies = [ ] [[package]] -name = "concat-idents" -version = "1.1.3" +name = "dirs-next" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b6f90860248d75014b7b103db8fee4f291c07bfb41306cdf77a0a5ab7a10d2f" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" dependencies = [ - "quote", - "syn", + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", ] [[package]] @@ -100,6 +128,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + [[package]] name = "env_logger" version = "0.5.13" @@ -113,6 +147,38 @@ dependencies = [ "termcolor", ] +[[package]] +name = "error-code" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5115567ac25674e0043e472be13d14e537f37ea8aa4bdc4aef0c89add1db1ff" +dependencies = [ + "libc", + "str-buf", +] + +[[package]] +name = "fd-lock" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8806dd91a06a7a403a8e596f9bfbfb34e469efbc363fc9c9713e79e26472e36" +dependencies = [ + "cfg-if", + "libc", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -215,6 +281,37 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +[[package]] +name = "memoffset" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +dependencies = [ + "autocfg", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "nix" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1e25ee6b412c2a1e3fcb6a4499a5c1bfe7f43e014bdce9a6b6666e5aa2d187" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "once_cell" version = "1.8.0" @@ -285,6 +382,16 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + [[package]] name = "redox_syscall" version = "0.2.10" @@ -294,6 +401,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom", + "redox_syscall", +] + [[package]] name = "regex" version = "1.5.4" @@ -313,13 +430,12 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "rock" -version = "0.1.6" +version = "0.1.7-interpreter" dependencies = [ "bincode", "bitflags", "clap", "colored", - "concat-idents", "either", "env_logger", "inkwell", @@ -327,10 +443,35 @@ dependencies = [ "log", "paste", "regex", + "rustyline", "serde", "serde_derive", ] +[[package]] +name = "rustyline" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790487c3881a63489ae77126f57048b42d62d3b2bafbf37453ea19eedb6340d6" +dependencies = [ + "bitflags", + "cfg-if", + "clipboard-win", + "dirs-next", + "fd-lock", + "libc", + "log", + "memchr", + "nix", + "radix_trie", + "scopeguard", + "smallvec", + "unicode-segmentation", + "unicode-width", + "utf8parse", + "winapi", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -378,6 +519,12 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" +[[package]] +name = "str-buf" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a" + [[package]] name = "strsim" version = "0.8.0" @@ -419,6 +566,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +[[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + [[package]] name = "unicode-width" version = "0.1.9" @@ -431,12 +584,24 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "utf8parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" + [[package]] name = "vec_map" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 097dbf94..c505a426 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rock" -version = "0.1.6" +version = "0.1.7-develop" authors = ["champii "] edition = "2018" @@ -11,7 +11,6 @@ regex = "1" env_logger = "0.5.12" log = "0.4" bitflags = "1.2.1" -concat-idents = "1.1.2" # inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm11-0"] } inkwell = { version = "0.1.0-beta.3", features = ["llvm12-0"] } either = "1.5" @@ -20,6 +19,7 @@ serde_derive = '*' bincode = "*" colored = "2.0.0" paste = "1.0.5" +rustyline = "9.0.0" [lib] name = "rock" diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..f288702d --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index 09fc0ee5..a3ff6c8c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Rock v0.1.6 +# Rock v0.1.7-develop -[![Rust](https://github.com/Champii/Rock/actions/workflows/rust.yml/badge.svg?branch=master)](https://github.com/Champii/Rock/actions/workflows/rust.yml) +[![Rust](https://github.com/Champii/Rock/actions/workflows/rust.yml/badge.svg?branch=develop)](https://github.com/Champii/Rock/actions/workflows/rust.yml) Little toy language made with Rust and LLVM. Aim to follow the Rust model with enforced safeness with a borrow checker and native performances thanks to LLVM. @@ -13,6 +13,8 @@ It's highly inspired from Livescript, and will borrow (pun intended) some featur - [With cargo from Git]( #with-cargo-from-git ) - [From sources]( #from-sources ) - [Quickstart]( #quickstart ) + - [Basic setup]( #basic-setup ) + - [REPL]( #repl ) - [Showcases]( #showcases ) - [Development notes]( #development-notes ) @@ -24,6 +26,7 @@ It's highly inspired from Livescript, and will borrow (pun intended) some featur - Typeclass (Traits) - Parametric Polymorphism by default - Compile to LLVM IR +- REPL (ALPHA) ## Install @@ -33,24 +36,30 @@ How to install and run the compiler: ### Using released binary +You will need `clang` somewhere in your $PATH + Linux x86_64 only -[Rock v0.1.6](https://github.com/Champii/Rock/releases/download/v0.1.6/rock) (Tested on arch, btw) +[Rock v0.1.7-develop](https://github.com/Champii/Rock/releases/download/v0.1.7-develop/rock) (Tested on arch, btw) ``` sh -wget https://github.com/Champii/Rock/releases/download/v0.1.6/rock +wget https://github.com/Champii/Rock/releases/download/v0.1.7-develop/rock chmod +x rock ./rock -V ``` -### With cargo from git +### From source + +You will need `llvm-12.0.1` and `clang-12.0.1` somewhere in your $PATH + +#### With cargo from git ``` sh cargo install --git https://github.com/Champii/Rock rock -V ``` -### From sources +#### Manual clone and build from git ``` sh git clone https://github.com/Champii/Rock.git @@ -60,6 +69,8 @@ cargo run -- -V ## Quickstart +### Basic setup + Lets create a new project folder to compute some factorials ``` sh @@ -97,6 +108,38 @@ Should output 24 ``` +Take a look at `rock --help` for a quick tour of its flags and arguments + +### REPL + +You can start a REPL session with + +``` sh +rock -r +# OR +rock --repl +``` + +``` sh +Rock: v0.1.7-develop +---- + +Type ':?' for help + +> add a b = a + b +> let x = 30 +30 +> let y = 12 +12 +> add x, y +42 +> :t add +add: (Int64 -> Int64 -> Int64) +> _ +``` + +Only supports basic expressions for now. + ## Showcases ### Polymophic function diff --git a/src/bin/main.rs b/src/bin/main.rs index 1b111a5d..b6d5b808 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -19,7 +19,7 @@ fn build(config: &Config) -> bool { fs::create_dir_all(config.build_folder.clone()).unwrap(); - if let Err(diagnostic) = rock::parse_file(entry_file.to_string(), config) { + if let Err(diagnostic) = rock::compile_file(entry_file.to_string(), config) { if let DiagnosticKind::NoError = diagnostic.get_kind() { } else { println!("Error: {}", diagnostic.get_kind()); @@ -28,44 +28,20 @@ fn build(config: &Config) -> bool { return false; } - let llc_cmd = Command::new("llc") - .args(&[ - "--relocation-model=pic", - config.build_folder.join("out.ir").to_str().unwrap(), - ]) - .output() - .expect("failed to compile to ir"); - - match llc_cmd.status.code() { - Some(code) => { - if code != 0 { - println!( - "BUG: Cannot compile to ir: \n{}", - String::from_utf8(llc_cmd.stderr).unwrap() - ); - - return false; - } - } - None => println!( - "\nError running: \n{}", - String::from_utf8(llc_cmd.stderr).unwrap() - ), - } let clang_cmd = Command::new("clang") .args(&[ + config.build_folder.join("out.bc").to_str().unwrap(), "-o", config.build_folder.join("a.out").to_str().unwrap(), - config.build_folder.join("out.ir.s").to_str().unwrap(), ]) .output() - .expect("failed to compile to binary"); + .expect("failed to compile to ir"); match clang_cmd.status.code() { Some(code) => { if code != 0 { println!( - "BUG: Cannot compile to binary: {}", + "BUG: Cannot compile: \n{}", String::from_utf8(clang_cmd.stderr).unwrap() ); @@ -77,6 +53,7 @@ fn build(config: &Config) -> bool { String::from_utf8(clang_cmd.stderr).unwrap() ), } + true } @@ -112,41 +89,62 @@ fn main() { Arg::with_name("verbose") .takes_value(false) .short("v") + .long("verbose") .help("Verbose level"), ) .arg( Arg::with_name("tokens") .short("t") + .long("tokens") .takes_value(false) .help("Show tokens"), ) .arg( Arg::with_name("ast") .short("a") + .long("ast") .takes_value(false) .help("Show ast"), ) .arg( Arg::with_name("hir") .short("h") + .long("hir") .takes_value(false) .help("Show hir"), ) + .arg( + Arg::with_name("no-optimize") + .short("N") + .long("no-optimize") + .takes_value(false) + .help("Disable LLVM optimization passes"), + ) + .arg( + Arg::with_name("repl") + .short("r") + .long("repl") + .takes_value(false) + .help("Run a REPL interpreter (ALPHA)"), + ) .arg( Arg::with_name("ir") .short("i") + .long("ir") .takes_value(false) .help("Show the generated IR"), ) .arg( Arg::with_name("state") .short("s") + .long("state") .takes_value(false) - .help("Show the InferContext state before solve"), + .help("Show the InferContext state before solve (DEPRECATED)"), ) .arg( Arg::with_name("output-folder") .short("o") + .long("output-folder") .takes_value(true) .default_value("./build") .help("Choose a different output folder"), @@ -162,6 +160,8 @@ fn main() { show_hir: matches.is_present("hir"), show_ir: matches.is_present("ir"), show_state: matches.is_present("state"), + repl: matches.is_present("repl"), + no_optimize: matches.is_present("no-optimize"), build_folder: PathBuf::from(matches.value_of("output-folder").unwrap()), ..Default::default() }; @@ -172,6 +172,8 @@ fn main() { build(&config); } else if let Some(_matches) = matches.subcommand_matches("run") { run(config); + } else if config.repl { + run(config) } else { println!("{}", matches.usage()); } diff --git a/src/lib/ast_lowering/return_placement.rs b/src/lib/ast_lowering/return_placement.rs index 63f8ce45..20507d26 100644 --- a/src/lib/ast_lowering/return_placement.rs +++ b/src/lib/ast_lowering/return_placement.rs @@ -27,8 +27,8 @@ impl<'a> ReturnInserter<'a> { StatementKind::If(ref mut i) => { self.visit_if(i); } - StatementKind::Assign(ref mut _a) => { - unimplemented!("Assign as return value"); + StatementKind::Assign(ref mut a) => { + a.value.kind = ExpressionKind::Return(Box::new(a.value.clone())); } } } diff --git a/src/lib/codegen/codegen_context.rs b/src/lib/codegen/codegen_context.rs index e358c9c0..b8a10ad1 100644 --- a/src/lib/codegen/codegen_context.rs +++ b/src/lib/codegen/codegen_context.rs @@ -5,16 +5,17 @@ use inkwell::{ builder::Builder, context::Context, module::Module, + passes::{PassManager, PassManagerBuilder}, + targets::{InitializationConfig, Target}, types::{BasicType, BasicTypeEnum}, values::{AnyValue, AnyValueEnum, BasicValue, BasicValueEnum, CallableValue, FunctionValue}, AddressSpace, FloatPredicate, IntPredicate, + OptimizationLevel::Aggressive, }; use crate::{ - diagnostics::Diagnostic, helpers::scopes::Scopes, hir::*, - parser::ParsingCtx, ty::{PrimitiveType, Type}, }; @@ -24,11 +25,10 @@ pub struct CodegenContext<'a> { pub module: Module<'a>, pub scopes: Scopes>, pub cur_func: Option>, - pub parsing_ctx: ParsingCtx, } impl<'a> CodegenContext<'a> { - pub fn new(context: &'a Context, parsing_ctx: ParsingCtx, hir: &'a Root) -> Self { + pub fn new(context: &'a Context, hir: &'a Root) -> Self { let module = context.create_module("mod"); Self { @@ -37,10 +37,61 @@ impl<'a> CodegenContext<'a> { hir, scopes: Scopes::new(), cur_func: None, - parsing_ctx, } } + pub fn optimize(&mut self) { + let config = InitializationConfig::default(); + + Target::initialize_native(&config).unwrap(); + + let pass_manager_builder = PassManagerBuilder::create(); + + pass_manager_builder.set_optimization_level(Aggressive); + + let pass_manager = PassManager::create(()); + + pass_manager.add_demote_memory_to_register_pass(); + pass_manager.add_promote_memory_to_register_pass(); + pass_manager.add_argument_promotion_pass(); + pass_manager.add_always_inliner_pass(); + pass_manager.add_gvn_pass(); + pass_manager.add_new_gvn_pass(); + pass_manager.add_function_attrs_pass(); + pass_manager.add_prune_eh_pass(); + pass_manager.add_loop_vectorize_pass(); + pass_manager.add_cfg_simplification_pass(); + pass_manager.add_constant_merge_pass(); + pass_manager.add_scalarizer_pass(); + pass_manager.add_merged_load_store_motion_pass(); + pass_manager.add_ind_var_simplify_pass(); + pass_manager.add_instruction_combining_pass(); + pass_manager.add_licm_pass(); + pass_manager.add_loop_deletion_pass(); + pass_manager.add_loop_unswitch_pass(); + pass_manager.add_memcpy_optimize_pass(); + pass_manager.add_partially_inline_lib_calls_pass(); + pass_manager.add_lower_switch_pass(); + pass_manager.add_reassociate_pass(); + pass_manager.add_simplify_lib_calls_pass(); + pass_manager.add_tail_call_elimination_pass(); + pass_manager.add_aggressive_inst_combiner_pass(); + pass_manager.add_instruction_simplify_pass(); + pass_manager.add_function_inlining_pass(); + pass_manager.add_global_optimizer_pass(); + pass_manager.add_dead_arg_elimination_pass(); + pass_manager.add_strip_symbol_pass(); + pass_manager.add_strip_dead_prototypes_pass(); + pass_manager.add_internalize_pass(true); + pass_manager.add_aggressive_dce_pass(); + pass_manager.add_sccp_pass(); + pass_manager.add_verifier_pass(); + + pass_manager.run_on(&self.module); + + pass_manager_builder.populate_module_pass_manager(&pass_manager); + } + pub fn lower_type(&mut self, t: &Type, builder: &'a Builder) -> Result, ()> { Ok(match t { Type::Primitive(PrimitiveType::Int8) => self.context.i8_type().into(), @@ -628,21 +679,6 @@ impl<'a> CodegenContext<'a> { let val = match self.scopes.get(reso) { None => { - let span = self - .hir - .hir_map - .get_node_id(&id.hir_id) - .map(|node_id| self.hir.spans.get(&node_id).unwrap().clone()) - .unwrap(); - - self.parsing_ctx - .diagnostics - .push_error(Diagnostic::new_codegen_error( - span, - id.hir_id.clone(), - "Cannot resolve identifier", - )); - return Err(()); } Some(val) => val, diff --git a/src/lib/codegen/interpreter.rs b/src/lib/codegen/interpreter.rs new file mode 100644 index 00000000..2a34ca38 --- /dev/null +++ b/src/lib/codegen/interpreter.rs @@ -0,0 +1,212 @@ +use colored::*; +use rustyline::{error::ReadlineError, Editor}; + +use inkwell::{ + builder::Builder, + context::Context, + targets::{InitializationConfig, Target}, + OptimizationLevel, +}; + +use crate::{ + parser::{ParsingCtx, SourceFile}, + ty::Type, + Config, +}; + +use super::codegen_context::CodegenContext; + +pub fn interpret<'a, 'ctx>( + _codegen_ctx: &'a mut CodegenContext<'ctx>, + _builder: &'ctx Builder, + rock_config: &Config, +) { + println!( + "{}{} {}{}\n{}\n\n{}\n", + "Rock".green(), + ":".bright_black(), + "v".bright_cyan(), + env!("CARGO_PKG_VERSION").cyan(), + "----".bright_black(), + "Type ':?' for help".bright_black() + ); + + let config = InitializationConfig::default(); + + Target::initialize_native(&config).unwrap(); + + let mut commands = vec![]; + let mut toplevels = vec![]; + + let mut rl = Editor::<()>::new(); + + if rl.load_history("history.txt").is_err() {} + + loop { + let readline = rl.readline(&"> ".yellow().to_string()); + + match readline { + Ok(line) => { + rl.add_history_entry(line.as_str()); + + if line == "exit" || line == "quit" { + break; + } + + process_line(line, &mut commands, &mut toplevels, rock_config); + } + Err(ReadlineError::Interrupted) => { + break; + } + Err(ReadlineError::Eof) => { + break; + } + Err(err) => { + println!("Error: {:?}", err); + break; + } + } + } + + rl.save_history("history.txt").unwrap(); +} + +fn process_line( + mut line: String, + commands: &mut Vec, + top_levels: &mut Vec, + config: &Config, +) { + if line.is_empty() { + return; + } + + let mut get_type = false; + + if line.starts_with(":?") { + print_help(); + + return; + } + + if line.starts_with(":t ") { + get_type = true; + + line.replace_range(0..3, ""); + } + + if line.starts_with("print ") { + line.replace_range(0..6, ""); + } + + let mut is_top_level = false; + + // FIXME: dirty hack to know if this is a function + let line_parts = line.split("=").collect::>(); + if line_parts.len() > 1 + && !line_parts[0].starts_with("let ") + && line_parts[0].split(" ").count() > 1 + { + is_top_level = true; + } else { + if line.starts_with("use ") || line.starts_with("mod ") { + is_top_level = true; + } + } + + if is_top_level { + top_levels.push(line.clone()); + } else { + if !get_type { + commands.push(" ".to_owned() + &line); + } + } + + let mut parsing_ctx = ParsingCtx::new(&config); + + let src = + SourceFile::from_expr(top_levels.join("\n"), commands.join("\n"), !is_top_level).unwrap(); + + parsing_ctx.add_file(&src); + + let hir = match crate::parse_str(&mut parsing_ctx, config) { + Ok(hir) => hir, + Err(_e) => { + if is_top_level { + top_levels.pop(); + } else { + if !get_type { + commands.pop(); + } + } + + return; + } + }; + + if get_type { + if is_top_level { + top_levels.pop(); + } else { + // commands.pop(); + } + } + + if get_type { + let t = hir + .get_function_by_name(line.split(" ").nth(0).unwrap()) + .map(|f| format!("{:?}", Type::from(f.signature.clone()))) + .or_else(|| Some("UNKNOWN".red().to_string())) + .unwrap(); + + println!("{}: {}", line, t); + + return; + } + + let context = Context::create(); + let builder = context.create_builder(); + + let mut codegen_ctx = CodegenContext::new(&context, &hir); + + if codegen_ctx.lower_hir(&hir, &builder).is_err() {} + + match codegen_ctx.module.verify() { + Ok(_) => (), + Err(e) => { + codegen_ctx.module.print_to_stderr(); + + println!("Error: Bug in the generated IR:\n\n{}", e.to_string()); + + return; + } + } + + if !config.no_optimize { + codegen_ctx.optimize(); + } + + if config.show_ir { + codegen_ctx.module.print_to_stderr(); + } + + let engine = codegen_ctx + .module + .create_jit_execution_engine(OptimizationLevel::None) + .unwrap(); + + unsafe { + engine.run_function_as_main(engine.get_function_value("main").unwrap(), &[]); + } +} + +pub fn print_help() { + println!( + "\n{}\n\n {} : {}\n {} : {}\n", + "Help:".bright_green(), + ":t".bright_yellow(), + "Print the type of an expression".bright_black(), + ":?".bright_yellow(), + "Print This help".bright_black(), + ); +} diff --git a/src/lib/codegen/mod.rs b/src/lib/codegen/mod.rs index 3a5cc3f0..360d2674 100644 --- a/src/lib/codegen/mod.rs +++ b/src/lib/codegen/mod.rs @@ -1,42 +1,78 @@ mod codegen_context; +mod interpreter; use codegen_context::*; use inkwell::context::Context; -use crate::{diagnostics::Diagnostic, hir::Root, parser::ParsingCtx, Config}; +use crate::{diagnostics::Diagnostic, hir::Root, Config}; -pub fn generate( - config: &Config, - parsing_ctx: ParsingCtx, - hir: Root, -) -> Result { +pub fn generate(config: &Config, hir: Root) -> Result<(), Diagnostic> { let context = Context::create(); let builder = context.create_builder(); - let mut codegen_ctx = CodegenContext::new(&context, parsing_ctx, &hir); - if codegen_ctx.lower_hir(&hir, &builder).is_err() { - codegen_ctx.parsing_ctx.return_if_error()?; - } + let mut codegen_ctx = CodegenContext::new(&context, &hir); - if config.show_ir { - codegen_ctx.module.print_to_stderr(); + if codegen_ctx.lower_hir(&hir, &builder).is_err() { + // FIXME: have a movable `Diagnostics` + // codegen_ctx.parsing_ctx.return_if_error()?; } match codegen_ctx.module.verify() { Ok(_) => (), Err(e) => { + codegen_ctx.module.print_to_stderr(); + println!("Error: Bug in the generated IR:\n\n{}", e.to_string()); return Err(Diagnostic::new_empty()); } } + if !config.no_optimize { + codegen_ctx.optimize(); + } + + if config.show_ir { + codegen_ctx.module.print_to_stderr(); + } + if !codegen_ctx .module - .write_bitcode_to_path(&config.build_folder.join("out.ir")) + .write_bitcode_to_path(&config.build_folder.join("out.bc")) { panic!("CANNOT IR WRITE TO PATH"); } - Ok(codegen_ctx.parsing_ctx) + Ok(()) +} + +pub fn interpret(hir: Root, config: &Config) -> Result<(), Diagnostic> { + let context = Context::create(); + let builder = context.create_builder(); + + let mut codegen_ctx = CodegenContext::new(&context, &hir); + + if codegen_ctx.lower_hir(&hir, &builder).is_err() { + // FIXME: have a movable `Diagnostics` + // codegen_ctx.parsing_ctx.return_if_error()?; + } + + match codegen_ctx.module.verify() { + Ok(_) => (), + Err(e) => { + codegen_ctx.module.print_to_stderr(); + + println!("Error: Bug in the generated IR:\n\n{}", e.to_string()); + + return Err(Diagnostic::new_empty()); + } + } + + interpreter::interpret(&mut codegen_ctx, &builder, config); + + // if config.show_ir { + // codegen_ctx.module.print_to_stderr(); + // } + + Ok(()) } diff --git a/src/lib/diagnostics/diagnostic.rs b/src/lib/diagnostics/diagnostic.rs index 2d33952c..3bcb97e6 100644 --- a/src/lib/diagnostics/diagnostic.rs +++ b/src/lib/diagnostics/diagnostic.rs @@ -46,8 +46,8 @@ impl Diagnostic { Self::new(span, DiagnosticKind::UnusedFunction) } - pub fn new_module_not_found(span: Span) -> Self { - Self::new(span, DiagnosticKind::ModuleNotFound) + pub fn new_module_not_found(span: Span, path: String) -> Self { + Self::new(span, DiagnosticKind::ModuleNotFound(path)) } pub fn new_unresolved_type(span: Span, t: Type) -> Self { @@ -85,6 +85,7 @@ impl Diagnostic { pub fn new_no_main() -> Self { Self::new(Span::new_placeholder(), DiagnosticKind::NoMain) } + pub fn new_type_conflict(span: Span, t1: Type, t2: Type, in1: Type, in2: Type) -> Self { Self::new(span, DiagnosticKind::TypeConflict(t1, t2, in1, in2)) } @@ -143,19 +144,50 @@ impl Diagnostic { DiagnosticType::Warning => x.yellow(), }; - println!( - "[{}]: {}\n{}\n{:>4} {}\n{:>4} {} {}\n{:>4} {} {}", + let color_bright = |x: String| match diag_type { + DiagnosticType::Error => x.bright_red(), + DiagnosticType::Warning => x.bright_yellow(), + }; + + let diag_type_str = format!( + "{}{}{} {}{}", + "[".bright_black(), diag_type_str, + "]".bright_black(), color(self.kind.to_string()).bold(), - line_ind.bright_blue(), + ":".bright_black(), + ); + + let line_span_start = line_start; + let mut line_span_stop = line_start + (self.span.end - self.span.start) + 1; + + let line_colored = lines[line - 1].iter().cloned().collect::(); + if line_span_stop > line_colored.len() { + line_span_stop = line_colored.len() - 1; + } + + let first_part = &line_colored[..line_span_start]; + let colored_part = color(line_colored[line_span_start..=line_span_stop].to_string()); + let last_part = if line_span_stop + 1 >= line_colored.len() { + String::new() + } else { + line_colored[line_span_stop + 1..].to_owned() + }; + + let line_colored = format!("{}{}{}", first_part, colored_part, last_part,); + + println!( + "{}\n{}\n{:>4} {}\n{:>4} {} {}\n{:>4} {} {}", + diag_type_str, + line_ind.bright_black(), "", - "|".cyan(), - color(line.to_string()), - "|".cyan(), - lines[line - 1].iter().cloned().collect::(), + "|".bright_black(), + color_bright(line.to_string()), + "|".bright_black(), + line_colored, "", - "|".cyan(), - color(arrow), + "|".bright_black(), + color_bright(arrow), ); } @@ -170,7 +202,7 @@ pub enum DiagnosticKind { UnexpectedToken, SyntaxError(String), UnknownIdentifier, - ModuleNotFound, + ModuleNotFound(String), NotAFunction, UnusedParameter, UnresolvedTraitCall { @@ -194,7 +226,7 @@ impl Display for DiagnosticKind { Self::UnexpectedToken => "UnexpectedToken".to_string(), Self::SyntaxError(msg) => format!("SyntaxError: {}", msg), Self::UnknownIdentifier => "UnknownIdentifier".to_string(), - Self::ModuleNotFound => "ModuleNotFound".to_string(), + Self::ModuleNotFound(path) => format!("Module not found: {}", path), Self::DuplicatedOperator => "DuplicatedOperator".to_string(), Self::TypeConflict(t1, t2, _in1, _in2) => { format!("Type conflict: Expected {:?} but got {:?} ", t1, t2) @@ -215,7 +247,7 @@ impl Display for DiagnosticKind { given_sig, existing_impls .iter() - .map(|sig| format!("Found impl: {:?}", sig)) + .map(|sig| format!(" Found impl: {:?}", sig)) .collect::>() .join("\n") ) diff --git a/src/lib/helpers/config.rs b/src/lib/helpers/config.rs index fb06e7ff..86f3ef6b 100644 --- a/src/lib/helpers/config.rs +++ b/src/lib/helpers/config.rs @@ -12,22 +12,6 @@ impl Default for PackageType { } } -// TBD -// #[derive(Debug, Default, Serialize, Deserialize)] -// pub struct PackageMetaData<'a> { -// pub hir: hir::Root<'a>, -// } - -// impl<'a> PackageMetaData<'a> { -// pub fn load(path: &Path) -> bincode::Result { -// bincode::deserialize_from(BufReader::new(File::open(path).unwrap())) -// } - -// pub fn store(&self, path: &Path) -> bincode::Result<()> { -// bincode::serialize_into(BufWriter::new(File::create(path).unwrap()), self) -// } -// } - #[derive(Debug, Clone, Default)] pub struct ProjectConfig { pub name: String, @@ -47,4 +31,6 @@ pub struct Config { pub show_state: bool, pub verbose: bool, pub build_folder: PathBuf, + pub no_optimize: bool, + pub repl: bool, } diff --git a/src/lib/hir/has_hir_id.rs b/src/lib/hir/has_hir_id.rs index d8774cec..023fc3d6 100644 --- a/src/lib/hir/has_hir_id.rs +++ b/src/lib/hir/has_hir_id.rs @@ -51,14 +51,20 @@ macro_rules! impl_indirect_get_hir_id_trait { impl_indirect_get_hir_id_trait!( TopLevel + Statement Assign AssignLeftSide ArgumentDecl IdentifierPath FnBody Body - Statement Expression Array Else ); + +impl HasHirId for Vec { + fn get_hir_id(&self) -> HirId { + self.last().unwrap().get_hir_id() + } +} diff --git a/src/lib/hir/hir_printer.rs b/src/lib/hir/hir_printer.rs index 2395b5a0..2ae6abce 100644 --- a/src/lib/hir/hir_printer.rs +++ b/src/lib/hir/hir_printer.rs @@ -47,7 +47,7 @@ impl<'a> HirPrinter<'a> { .map_or_else(|| String::from("None"), |t| format!("{:?}", t)); println!( - "{:>13}{:-<60} {}", + "{:<10}{:-<60} {}", t.get_hir_id(), self.make_indent_str(t.class_name_self().magenta()), ty diff --git a/src/lib/hir/tree.rs b/src/lib/hir/tree.rs index 7ad6117f..38cadbfb 100644 --- a/src/lib/hir/tree.rs +++ b/src/lib/hir/tree.rs @@ -10,7 +10,7 @@ use crate::{ ty::{FuncType, StructType, Type}, }; -use super::{arena::Arena, HasHirId}; +use super::{arena::Arena, hir_printer, HasHirId}; #[derive(Debug, Default)] pub struct Root { @@ -126,6 +126,10 @@ impl Root { .filter_map(|(node_id, span)| Some((self.hir_map.get_hir_id(*node_id)?, span.clone()))) .collect() } + + pub fn print(&self) { + hir_printer::print(self); + } } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -427,6 +431,11 @@ impl Expression { kind: Box::new(ExpressionKind::Dot(dot)), } } + pub fn new_return(ret: Expression) -> Self { + Self { + kind: Box::new(ExpressionKind::Return(ret)), + } + } pub fn get_terminal_hir_id(&self) -> HirId { match &*self.kind { @@ -528,6 +537,12 @@ impl Literal { panic!("Not a number"); } } + pub fn new_int64(i: i64) -> Self { + Self { + hir_id: HirId(0), + kind: LiteralKind::Number(i), + } + } } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/lib/infer/constraint.rs b/src/lib/infer/constraint.rs index e2fc21e4..286e3db8 100644 --- a/src/lib/infer/constraint.rs +++ b/src/lib/infer/constraint.rs @@ -405,8 +405,8 @@ impl<'a, 'ar> Visitor<'a> for ConstraintContext<'ar> { } fn visit_assign(&mut self, assign: &'a Assign) { - self.visit_assign_left_side(&assign.name); self.visit_expression(&assign.value); + self.visit_assign_left_side(&assign.name); self.envs .set_type_eq(&assign.name.get_hir_id(), &assign.value.get_hir_id()); diff --git a/src/lib/infer/state.rs b/src/lib/infer/state.rs index d25e3647..f7ad34e4 100644 --- a/src/lib/infer/state.rs +++ b/src/lib/infer/state.rs @@ -62,27 +62,30 @@ impl Envs { self.current_fn.clone() } - pub fn set_type(&mut self, dest: &HirId, src: &Type) { + fn set_type_alone(&mut self, dest: &HirId, src: &Type) -> Option { if let Type::ForAll(_) = src { warn!("set_type requires `src: &Type` to be solved"); - return; + return None; } - let previous = self - .get_current_env_mut() + self.get_current_env_mut() .unwrap() - .insert(dest.clone(), src.clone()); + .insert(dest.clone(), src.clone()) + } + + pub fn set_type(&mut self, dest: &HirId, src: &Type) { + let previous = self.set_type_alone(dest, src); match (src, previous.clone()) { (Type::Func(src_f), Some(Type::Func(prev_f))) if !src_f.eq(&prev_f) => { if prev_f.is_solved() && src_f.is_solved() { self.diagnostics.push_error(Diagnostic::new_type_conflict( self.spans.get(dest).unwrap().clone(), - src.clone(), previous.clone().unwrap(), src.clone(), previous.unwrap(), + src.clone(), )); } } @@ -90,10 +93,10 @@ impl Envs { if previous.is_solved() && src.is_solved() { self.diagnostics.push_error(Diagnostic::new_type_conflict( self.spans.get(dest).unwrap().clone(), - src.clone(), previous.clone(), src.clone(), previous, + src.clone(), )); } } @@ -102,7 +105,34 @@ impl Envs { } pub fn set_type_eq(&mut self, dest: &HirId, src: &HirId) { - self.set_type(dest, &self.get_type(src).unwrap().clone()) + let src_t = self.get_type(src).unwrap().clone(); + let previous = self.set_type_alone(dest, &src_t); + + match (src_t.clone(), previous.clone()) { + (Type::Func(src_f), Some(Type::Func(prev_f))) if !src_f.eq(&prev_f) => { + if prev_f.is_solved() && src_f.is_solved() { + self.diagnostics.push_error(Diagnostic::new_type_conflict( + self.spans.get(src).unwrap().clone(), + previous.clone().unwrap(), + src_t.clone(), + previous.unwrap(), + src_t.clone(), + )); + } + } + (src_t, Some(previous)) if !src_t.eq(&previous) => { + if previous.is_solved() && src_t.is_solved() { + self.diagnostics.push_error(Diagnostic::new_type_conflict( + self.spans.get(src).unwrap().clone(), + previous.clone(), + src_t.clone(), + previous, + src_t.clone(), + )); + } + } + _ => (), + } } pub fn get_type(&self, hir_id: &HirId) -> Option<&Type> { diff --git a/src/lib/parser/parser_impl.rs b/src/lib/parser/parser_impl.rs index 94ea955e..945c3ae2 100644 --- a/src/lib/parser/parser_impl.rs +++ b/src/lib/parser/parser_impl.rs @@ -131,7 +131,7 @@ impl<'a> Parser<'a> { pub fn cur_tok(&self) -> Token { match self.tokens.get(self.cur_tok_id as usize) { Some(a) => a.clone(), - _ => unreachable!(), + _ => Token::eof(), } } @@ -164,11 +164,7 @@ impl<'a> Parser<'a> { pub fn seek(&self, distance: usize) -> Token { match self.tokens.get(self.cur_tok_id as usize + distance) { Some(a) => a.clone(), - _ => Token { - t: TokenType::Eof, - span: Span::new_placeholder(), - txt: "".to_string(), - }, + _ => Token::eof(), } } diff --git a/src/lib/parser/parsing_context.rs b/src/lib/parser/parsing_context.rs index 90c103b5..9f04d0a5 100644 --- a/src/lib/parser/parsing_context.rs +++ b/src/lib/parser/parsing_context.rs @@ -51,15 +51,35 @@ impl ParsingCtx { self.print_diagnostics(); if !self.diagnostics.list.is_empty() { + let diag_type_str = format!( + "{}{}{}", + "[".bright_black(), + "Success".green(), + "]".bright_black(), + ); + println!( - "[{}] Compilation {} with {} {}", - "Warning".yellow(), - "successful".green(), + "{} {} {} {} {} {}", + diag_type_str, + "Compilation".bright_black(), + "successful".bright_green(), + "with".bright_black(), self.diagnostics.list.len().to_string().yellow(), - "warnings".yellow(), + "warnings".bright_yellow(), ); } else if self.config.verbose { - println!("[{}] Compilation successful", "Success".green(),); + let diag_type_str = format!( + "{}{}{}", + "[".bright_black(), + "Success".green(), + "]".bright_black(), + ); + + println!( + "{} {}", + diag_type_str, + "Compilation successful".bright_black(), + ); } } @@ -79,14 +99,24 @@ impl ParsingCtx { ) }); - println!( - "[{}] Compilation {} with {} {} and {} {}", + let diag_type_str = format!( + "{}{}{}", + "[".bright_black(), "Error".red(), - "stopped".red(), + "]".bright_black(), + ); + + println!( + "{} {} {} {} {} {} {} {} {}", + diag_type_str, + "Compilation".bright_black(), + "stopped".bright_red(), + "with".bright_black(), errors.len().to_string().red(), - "errors".red(), + "errors".bright_red(), + "and".bright_black(), warnings.len().to_string().yellow(), - "warnings".yellow(), + "warnings".bright_yellow(), ); return Err(Diagnostic::new_empty()); @@ -102,9 +132,9 @@ impl ParsingCtx { pub fn resolve_and_add_file(&mut self, name: String) -> Result { let current_file = self.get_current_file(); - let new_file = current_file.resolve_new(name).map_err(|_| { + let new_file = current_file.resolve_new(name.clone()).map_err(|m| { // Placeholder span, to be overriden by calling mod (TopLevel::parse()) - Diagnostic::new_module_not_found(Span::new(current_file.file_path.clone(), 0, 0)) + Diagnostic::new_module_not_found(Span::new(current_file.file_path.clone(), 0, 0), m) })?; if self.config.verbose { diff --git a/src/lib/parser/source_file.rs b/src/lib/parser/source_file.rs index 04c105b7..bd2a649a 100644 --- a/src/lib/parser/source_file.rs +++ b/src/lib/parser/source_file.rs @@ -31,7 +31,41 @@ impl SourceFile { }) } - pub fn resolve_new(&self, name: String) -> Result { + pub fn from_expr( + top_levels: String, + mut expr: String, + do_print: bool, + ) -> Result { + let print_str = if do_print { "print " } else { "" }; + + if expr.is_empty() { + expr = " 0".to_string(); + } + + let top_levels = r##"mod lib +use lib::prelude::* +"## + .to_owned() + + &top_levels + + &r##" + +main = + "## + &print_str.to_string() + + &r##"custom() + 0 +custom = +"## + .to_owned() + + &expr; + + Ok(SourceFile { + file_path: PathBuf::from("./src/main.rk"), + mod_path: PathBuf::from("root"), + content: top_levels, + }) + } + + pub fn resolve_new(&self, name: String) -> Result { let mut file_path = self.file_path.parent().unwrap().join(Path::new(&name)); file_path.set_extension("rk"); @@ -40,7 +74,7 @@ impl SourceFile { let content = match fs::read_to_string(file_path.to_str().unwrap().to_string()) { Ok(content) => content, - Err(_) => return Err(()), + Err(_) => return Err(mod_path.as_path().to_str().unwrap().to_string()), }; Ok(Self { diff --git a/src/lib/parser/token.rs b/src/lib/parser/token.rs index c7804ba4..d8529281 100644 --- a/src/lib/parser/token.rs +++ b/src/lib/parser/token.rs @@ -73,3 +73,13 @@ pub struct Token { } pub type TokenId = usize; + +impl Token { + pub fn eof() -> Self { + Token { + t: TokenType::Eof, + span: Span::new_placeholder(), + txt: "".to_string(), + } + } +} diff --git a/src/lib/resolver/resolve_ctx.rs b/src/lib/resolver/resolve_ctx.rs index c65f6bbf..77059c0a 100644 --- a/src/lib/resolver/resolve_ctx.rs +++ b/src/lib/resolver/resolve_ctx.rs @@ -91,13 +91,15 @@ impl<'a> Visitor<'a> for ResolveCtx<'a> { } fn visit_assign(&mut self, assign: &'a Assign) { + self.visit_expression(&assign.value); + match &assign.name { AssignLeftSide::Identifier(id) => { if !assign.is_let { - let previous_assign_node_id = self.get(id.name.clone()).unwrap(); - - self.resolutions - .insert(id.identity.node_id, previous_assign_node_id.node_id); + if let Some(previous_assign_node_id) = self.get(id.name.clone()) { + self.resolutions + .insert(id.identity.node_id, previous_assign_node_id.node_id); + } self.visit_identifier(id) } @@ -107,8 +109,6 @@ impl<'a> Visitor<'a> for ResolveCtx<'a> { AssignLeftSide::Indice(expr) => self.visit_expression(expr), AssignLeftSide::Dot(expr) => self.visit_expression(expr), } - - self.visit_expression(&assign.value); } fn visit_top_level(&mut self, top: &'a TopLevel) { @@ -200,6 +200,12 @@ impl<'a> Visitor<'a> for ResolveCtx<'a> { .diagnostics .push_error(Diagnostic::new_module_not_found( ident.identity.span.clone(), + mod_path + .path + .iter() + .map(|p| p.name.clone()) + .collect::>() + .join("/"), )), }; } @@ -241,6 +247,12 @@ impl<'a> Visitor<'a> for ResolveCtx<'a> { .diagnostics .push_error(Diagnostic::new_module_not_found( ident.identity.span.clone(), + mod_path + .path + .iter() + .map(|p| p.name.clone()) + .collect::>() + .join("/"), )), }; } diff --git a/src/lib/rock.rs b/src/lib/rock.rs index 4527a541..68d2cdee 100644 --- a/src/lib/rock.rs +++ b/src/lib/rock.rs @@ -28,30 +28,45 @@ mod resolver; mod tests; mod ty; +use codegen::interpret; use diagnostics::Diagnostic; pub use helpers::config::Config; use parser::{ParsingCtx, SourceFile}; -pub fn parse_file(in_name: String, config: &Config) -> Result<(), Diagnostic> { +pub fn compile_file(in_name: String, config: &Config) -> Result<(), Diagnostic> { let mut source_file = SourceFile::from_file(in_name)?; source_file.mod_path = PathBuf::from("root"); - parse_str(&source_file, config) + compile_str(&source_file, config) } -pub fn parse_str(input: &SourceFile, config: &Config) -> Result<(), Diagnostic> { +pub fn compile_str(input: &SourceFile, config: &Config) -> Result<(), Diagnostic> { let mut parsing_ctx = ParsingCtx::new(config); parsing_ctx.add_file(input); + let hir = parse_str(&mut parsing_ctx, config)?; + + if config.repl { + interpret(hir, config) + } else { + generate_ir(hir, config)?; + + parsing_ctx.print_success_diagnostics(); + + Ok(()) + } +} + +pub fn parse_str(parsing_ctx: &mut ParsingCtx, config: &Config) -> Result { // Text to Ast debug!(" -> Parsing"); - let mut ast = parser::parse_root(&mut parsing_ctx)?; + let mut ast = parser::parse_root(parsing_ctx)?; // Name resolving debug!(" -> Resolving"); - resolver::resolve(&mut ast, &mut parsing_ctx)?; + resolver::resolve(&mut ast, parsing_ctx)?; // Lowering to HIR debug!(" -> Lowering to HIR"); @@ -59,17 +74,18 @@ pub fn parse_str(input: &SourceFile, config: &Config) -> Result<(), Diagnostic> // Infer Hir debug!(" -> Infer HIR"); - let new_hir = infer::infer(&mut hir, &mut parsing_ctx, config)?; + let new_hir = infer::infer(&mut hir, parsing_ctx, config)?; + + Ok(new_hir) +} +pub fn generate_ir(hir: hir::Root, config: &Config) -> Result<(), Diagnostic> { // Generate code debug!(" -> Lower to LLVM IR"); - let parsing_ctx = codegen::generate(config, parsing_ctx, new_hir)?; - - parsing_ctx.print_success_diagnostics(); + codegen::generate(config, hir)?; Ok(()) } - mod test { use super::*; use crate::{parser::SourceFile, Config}; @@ -86,49 +102,24 @@ mod test { content: input, }; - if let Err(_e) = parse_str(&file, &config) { + if let Err(_e) = compile_str(&file, &config) { return false; } - let llc_cmd = Command::new("llc") - .args(&[ - "--relocation-model=pic", - config.build_folder.join("out.ir").to_str().unwrap(), - ]) - .output() - .expect("failed to execute IR -> ASM"); - - match llc_cmd.status.code() { - Some(code) => { - if code != 0 { - println!( - "BUG: Cannot compile to ir: \n{}", - String::from_utf8(llc_cmd.stderr).unwrap() - ); - - return false; - } - } - None => println!( - "\nError running: \n{}", - String::from_utf8(llc_cmd.stderr).unwrap() - ), - } - let clang_cmd = Command::new("clang") .args(&[ + config.build_folder.join("out.bc").to_str().unwrap(), "-o", config.build_folder.join("a.out").to_str().unwrap(), - config.build_folder.join("out.ir.s").to_str().unwrap(), ]) .output() - .expect("failed to execute ASM -> BINARY"); + .expect("failed to compile to ir"); match clang_cmd.status.code() { Some(code) => { if code != 0 { println!( - "BUG: Cannot compile to binary: {}", + "BUG: Cannot compile: \n{}", String::from_utf8(clang_cmd.stderr).unwrap() ); @@ -140,6 +131,7 @@ mod test { String::from_utf8(clang_cmd.stderr).unwrap() ), } + true } diff --git a/src/lib/testcases/basic/reassign_self/main.rk b/src/lib/testcases/basic/reassign_self/main.rk new file mode 100644 index 00000000..25fb1dbf --- /dev/null +++ b/src/lib/testcases/basic/reassign_self/main.rk @@ -0,0 +1,4 @@ +main = + let a = 21 + a = ~IAdd a a + a diff --git a/src/lib/testcases/basic/reassign_self/main.rk.out b/src/lib/testcases/basic/reassign_self/main.rk.out new file mode 100644 index 00000000..f70d7bba --- /dev/null +++ b/src/lib/testcases/basic/reassign_self/main.rk.out @@ -0,0 +1 @@ +42 \ No newline at end of file diff --git a/src/lib/testcases/basic/reassign_self/main.rk.stdout b/src/lib/testcases/basic/reassign_self/main.rk.stdout new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/tests.rs b/src/lib/tests.rs index c679ba61..31e211a8 100644 --- a/src/lib/tests.rs +++ b/src/lib/tests.rs @@ -82,6 +82,10 @@ fn testcases_basic_monomorph_in_trait_main() { run("testcases/basic/monomorph_in_trait/main.rk", include_str!("testcases/basic/monomorph_in_trait/main.rk"), include_str!("testcases/basic/monomorph_in_trait/main.rk.out"), include_str!("testcases/basic/monomorph_in_trait/main.rk.stdout")); } #[test] +fn testcases_basic_reassign_self_main() { + run("testcases/basic/reassign_self/main.rk", include_str!("testcases/basic/reassign_self/main.rk"), include_str!("testcases/basic/reassign_self/main.rk.out"), include_str!("testcases/basic/reassign_self/main.rk.stdout")); +} +#[test] fn testcases_basic_trait_use_before_decl_main() { run("testcases/basic/trait_use_before_decl/main.rk", include_str!("testcases/basic/trait_use_before_decl/main.rk"), include_str!("testcases/basic/trait_use_before_decl/main.rk.out"), include_str!("testcases/basic/trait_use_before_decl/main.rk.stdout")); }