├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── examples ├── cmd_lib.rs ├── cmd_lib.sh ├── hello.rs ├── hello.rss └── hello.sh ├── runtime ├── cmd_lib.rs └── cmd_lib.sh └── src ├── bash_backend.rs ├── main.rs ├── parser.rs ├── resolver.rs ├── rust_backend.rs ├── scanner.rs └── util ├── char.rs ├── mod.rs └── scan_iter.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "rust-shell-script" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-shell-script" 3 | version = "0.1.0" 4 | authors = ["rust-shell-script "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-shell-script 2 | ~~Rustlike shell scripting language (WIP)~~ -> use [rust_cmd_lib](https://github.com/rust-shell-script/rust_cmd_lib) instead. 3 | 4 | Resilient & robust shell script, compiling to rust code/bash script 5 | 6 | # Build & Install 7 | ```bash 8 | $ cargo build 9 | ``` 10 | 11 | # Example 12 | 13 | ## Source file 14 | 15 | ```bash 16 | rust-shell-script$:~/rust-shell-script$ cat examples/hello.rss 17 | cmd error_command() { 18 | do_something_failed 19 | } 20 | 21 | fun bad_greeting(name) { 22 | info "Running error_command ..." 23 | error_command 24 | output "hello, ${name}!" 25 | } 26 | 27 | fun good_greeting(name) { 28 | info "Running good_command ..." 29 | output "hello, ${name}!" 30 | } 31 | 32 | cmd main() { 33 | let bad_ans = $(bad_greeting "rust-shell-script") 34 | info "${bad_ans}" 35 | let good_ans = $(good_greeting "rust-shell-script") 36 | info "${good_ans}" 37 | return 0 38 | } 39 | ``` 40 | 41 | ## Compiling to bash 42 | 43 | ### compiling 44 | ```bash 45 | rust-shell-script:~/rust-shell-script$ target/debug/rust-shell-script -t bash examples/hello.rss 46 | Generating bash script to examples/hello.sh ... 47 | ``` 48 | 49 | ### passing shellcheck 50 | ```bash 51 | rust-shell-script:~/rust-shell-script$ shellcheck examples/hello.sh 52 | rust-shell-script:~/rust-shell-script$ echo $? 53 | 0 54 | ``` 55 | 56 | ### generated bash script 57 | ```bash 58 | #!/bin/bash 59 | # 60 | # Generated by rust-shell-script 61 | # 62 | . "${RUNTIME:-.}/cmd_lib.sh" 63 | 64 | error_command() { 65 | do_something_failed 66 | } 67 | 68 | function bad_greeting() { 69 | local name="$1" 70 | 71 | info "Running error_command ..." 72 | error_command 73 | output "hello, ${name}!" 74 | } 75 | 76 | function good_greeting() { 77 | local name="$1" 78 | 79 | info "Running good_command ..." 80 | output "hello, ${name}!" 81 | } 82 | 83 | main() { 84 | local bad_ans 85 | bad_ans=$(_call bad_greeting "rust-shell-script") 86 | info "${bad_ans}" 87 | local good_ans 88 | good_ans=$(_call good_greeting "rust-shell-script") 89 | info "${good_ans}" 90 | return 0 91 | } 92 | 93 | main "$@" 94 | ``` 95 | 96 | ### running script 97 | 98 | All command errors will be captured automatically, even within bash function: 99 | ```bash 100 | rust-shell-script:~/rust-shell-script/examples$ ./hello.sh 101 | Running error_command ... 102 | /bin/bash: line 219: do_something_failed: command not found 103 | FATAL: Running command (bad_ans=$(_call bad_greeting "rust-shell-script")) near script hello.sh:main():1 failed (ret=127) 104 | ``` 105 | 106 | ## Compiling to rust 107 | 108 | ### compiling 109 | ```bash 110 | tao@sophia:~/rust-shell-script$ target/debug/rust-shell-script -t rust examples/hello.rss 111 | Generating rust code to examples/hello.rs ... 112 | ``` 113 | 114 | ### generated rust code 115 | ```rust 116 | // Generated by rust-shell-script 117 | mod cmd_lib; 118 | use crate::cmd_lib::{CmdResult, FunResult}; 119 | 120 | fn error_command() -> CmdResult { 121 | run_cmd!("do_something_failed") 122 | } 123 | 124 | fn bad_greeting(name: &str) -> FunResult { 125 | info!("Running error_command ..."); 126 | error_command()?; 127 | output!("hello, {}!", name) 128 | } 129 | 130 | fn good_greeting(name: &str) -> FunResult { 131 | info!("Running good_command ..."); 132 | output!("hello, {}!", name) 133 | } 134 | 135 | fn main() -> CmdResult { 136 | let bad_ans = bad_greeting("rust-shell-script")?;; 137 | info!("{}", bad_ans); 138 | let good_ans = good_greeting("rust-shell-script")?;; 139 | info!("{}", good_ans); 140 | return Ok(()) 141 | } 142 | ``` 143 | 144 | ### running rust code 145 | ```bash 146 | rust-shell-script:~/rust-shell-script/examples$ rustc hello.rs 147 | rust-shell-script:~/rust-shell-script/examples$ ./hello 148 | Running error_command ... 149 | Running ["do_something_failed"] ... 150 | Error: Os { code: 2, kind: NotFound, message: "No such file or directory" } 151 | ``` 152 | -------------------------------------------------------------------------------- /examples/cmd_lib.rs: -------------------------------------------------------------------------------- 1 | ../runtime/cmd_lib.rs -------------------------------------------------------------------------------- /examples/cmd_lib.sh: -------------------------------------------------------------------------------- 1 | ../runtime/cmd_lib.sh -------------------------------------------------------------------------------- /examples/hello.rs: -------------------------------------------------------------------------------- 1 | // Generated by rust-shell-script 2 | mod cmd_lib; 3 | use crate::cmd_lib::{CmdResult, FunResult}; 4 | 5 | fn error_command() -> CmdResult { 6 | run_cmd!("do_something_failed") 7 | } 8 | 9 | fn bad_greeting(name: &str) -> FunResult { 10 | info!("Running error_command ..."); 11 | error_command()?; 12 | output!("hello, {}!", name) 13 | } 14 | 15 | fn good_greeting(name: &str) -> FunResult { 16 | info!("Running good_command ..."); 17 | output!("hello, {}!", name) 18 | } 19 | 20 | fn main() -> CmdResult { 21 | let bad_ans = bad_greeting("rust-shell-script")?;; 22 | info!("{}", bad_ans); 23 | let good_ans = good_greeting("rust-shell-script")?;; 24 | info!("{}", good_ans); 25 | return Ok(()) 26 | } 27 | 28 | -------------------------------------------------------------------------------- /examples/hello.rss: -------------------------------------------------------------------------------- 1 | cmd error_command() { 2 | do_something_failed 3 | } 4 | 5 | fun bad_greeting(name) { 6 | info "Running error_command ..." 7 | error_command 8 | output "hello, ${name}!" 9 | } 10 | 11 | fun good_greeting(name) { 12 | info "Running good_command ..." 13 | output "hello, ${name}!" 14 | } 15 | 16 | cmd main() { 17 | let bad_ans = $(bad_greeting "rust-shell-script") 18 | info "${bad_ans}" 19 | let good_ans = $(good_greeting "rust-shell-script") 20 | info "${good_ans}" 21 | return 0 22 | } 23 | -------------------------------------------------------------------------------- /examples/hello.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Generated by rust-shell-script 4 | # 5 | . "${RUNTIME:-.}/cmd_lib.sh" 6 | 7 | error_command() { 8 | do_something_failed 9 | } 10 | 11 | function bad_greeting() { 12 | local name="$1" 13 | 14 | info "Running error_command ..." 15 | error_command 16 | output "hello, ${name}!" 17 | } 18 | 19 | function good_greeting() { 20 | local name="$1" 21 | 22 | info "Running good_command ..." 23 | output "hello, ${name}!" 24 | } 25 | 26 | main() { 27 | local bad_ans 28 | bad_ans=$(_call bad_greeting "rust-shell-script") 29 | info "${bad_ans}" 30 | local good_ans 31 | good_ans=$(_call good_greeting "rust-shell-script") 32 | info "${good_ans}" 33 | return 0 34 | } 35 | 36 | main "$@" 37 | -------------------------------------------------------------------------------- /runtime/cmd_lib.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | use std::io::{Read, Error, ErrorKind}; 3 | use std::process::{Command, Stdio, ExitStatus,Child, ChildStdout}; 4 | use std::collections::VecDeque; 5 | 6 | pub type FunResult = Result; 7 | pub type CmdResult = Result<(), std::io::Error>; 8 | pub type PipeResult = Result<(Child, ChildStdout), std::io::Error>; 9 | 10 | #[macro_export] 11 | macro_rules! info { 12 | ($($arg:tt)*) => { 13 | $crate::cmd_lib::info(format!($($arg)*)) 14 | } 15 | } 16 | 17 | #[macro_export] 18 | macro_rules! output { 19 | ($($arg:tt)*) => { 20 | $crate::cmd_lib::output(format!($($arg)*)) 21 | } 22 | } 23 | 24 | #[macro_export] 25 | macro_rules! run_cmd { 26 | ($($arg:tt)*) => { 27 | $crate::cmd_lib::run_cmd(format!($($arg)*)) 28 | } 29 | } 30 | 31 | #[macro_export] 32 | macro_rules! run_fun { 33 | ($($arg:tt)*) => { 34 | $crate::cmd_lib::run_fun(format!($($arg)*)) 35 | } 36 | } 37 | 38 | #[doc(hidden)] 39 | pub fn info(msg: S) 40 | where 41 | S: Into + Display, 42 | { 43 | eprintln!("{}", msg); 44 | } 45 | 46 | #[doc(hidden)] 47 | pub fn output(msg: S) -> FunResult 48 | where 49 | S: Into, 50 | { 51 | Ok(msg.into()) 52 | } 53 | 54 | #[doc(hidden)] 55 | pub fn run_pipe(full_command: &str) -> PipeResult { 56 | let pipe_args = parse_pipes(full_command); 57 | let pipe_argv = parse_argv(&pipe_args); 58 | let n = pipe_argv.len(); 59 | let mut pipe_procs = VecDeque::with_capacity(n); 60 | let mut pipe_outputs = VecDeque::with_capacity(n); 61 | 62 | info!("Running \"{}\" ...", full_command); 63 | for (i, pipe_cmd) in pipe_argv.iter().enumerate() { 64 | let args = parse_args(pipe_cmd); 65 | let argv = parse_argv(&args); 66 | 67 | if i == 0 { 68 | pipe_procs.push_back(Command::new(&argv[0]) 69 | .args(&argv[1..]) 70 | .stdout(Stdio::piped()) 71 | .spawn()?); 72 | } else { 73 | pipe_procs.push_back(Command::new(&argv[0]) 74 | .args(&argv[1..]) 75 | .stdin(pipe_outputs.pop_front().unwrap()) 76 | .stdout(Stdio::piped()) 77 | .spawn()?); 78 | pipe_procs.pop_front().unwrap().wait()?; 79 | } 80 | 81 | pipe_outputs.push_back(pipe_procs.back_mut().unwrap().stdout.take().ok_or_else(|| { 82 | std::io::Error::new(std::io::ErrorKind::BrokenPipe, "Broken pipe") 83 | })?); 84 | } 85 | 86 | Ok((pipe_procs.pop_front().unwrap(), pipe_outputs.pop_front().unwrap())) 87 | } 88 | 89 | #[doc(hidden)] 90 | pub fn run_cmd(full_command: String) -> CmdResult { 91 | let (mut proc, mut output) = run_pipe(&full_command)?; 92 | let status = proc.wait()?; 93 | if !status.success() { 94 | Err(to_io_error(&full_command, status)) 95 | } else { 96 | let mut s = String::new(); 97 | output.read_to_string(&mut s)?; 98 | print!("{}", s); 99 | Ok(()) 100 | } 101 | } 102 | 103 | #[doc(hidden)] 104 | pub fn run_fun(full_command: String) -> FunResult { 105 | let (mut proc, mut output) = run_pipe(&full_command)?; 106 | let status = proc.wait()?; 107 | if !status.success() { 108 | Err(to_io_error(&full_command, status)) 109 | } else { 110 | let mut s = String::new(); 111 | output.read_to_string(&mut s)?; 112 | Ok(s) 113 | } 114 | } 115 | 116 | fn to_io_error(command: &str, status: ExitStatus) -> Error { 117 | if let Some(code) = status.code() { 118 | Error::new(ErrorKind::Other, format!("{} exit with {}", command, code)) 119 | } else { 120 | Error::new(ErrorKind::Other, "Unknown error") 121 | } 122 | } 123 | 124 | fn parse_args(s: &str) -> String { 125 | let mut in_single_quote = false; 126 | let mut in_double_quote = false; 127 | s.chars() 128 | .map(|c| { 129 | if c == '"' && !in_single_quote { 130 | in_double_quote = !in_double_quote; 131 | '\n' 132 | } else if c == '\'' && !in_double_quote { 133 | in_single_quote = !in_single_quote; 134 | '\n' 135 | } else if !in_single_quote && !in_double_quote && char::is_whitespace(c) { 136 | '\n' 137 | } else { 138 | c 139 | } 140 | }) 141 | .collect() 142 | } 143 | 144 | fn parse_pipes(s: &str) -> String { 145 | let mut in_single_quote = false; 146 | let mut in_double_quote = false; 147 | s.chars() 148 | .map(|c| { 149 | if c == '"' && !in_single_quote { 150 | in_double_quote = !in_double_quote; 151 | } else if c == '\'' && !in_double_quote { 152 | in_single_quote = !in_single_quote; 153 | } 154 | 155 | if c == '|' && !in_single_quote && !in_double_quote { 156 | '\n' 157 | } else { 158 | c 159 | } 160 | }) 161 | .collect() 162 | } 163 | 164 | fn parse_argv(s: &str) -> Vec<&str> { 165 | s.split("\n") 166 | .filter(|s| !s.is_empty()) 167 | .collect::>() 168 | } 169 | -------------------------------------------------------------------------------- /runtime/cmd_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Already sourced 4 | if type __setup >/dev/null 2>&1; then 5 | return 6 | fi 7 | 8 | LIB_PATH="${BASH_SOURCE[0]}" 9 | ROOT_DIR=$(cd ${BASH_SOURCE[0]%/*}/../.. && pwd) # The first element is this script 10 | # ${BASH_SOURCE[-1]} not working for old bash 11 | __idx=$((${#BASH_SOURCE[@]} - 1)) 12 | SCRIPT_PATH=${BASH_SOURCE[$__idx]} 13 | SCRIPT_NAME=${SCRIPT_NAME:-$(basename ${BASH_SOURCE[$__idx]})} # The last element is the command script 14 | SCRIPT_ARGS="$*" 15 | PATH=$PATH:/usr/sbin:/sbin 16 | DEBUG_MODE=${DEBUG_MODE:-"0"} 17 | _SHELL=$(ps -p $$ -o args= | awk '{print $1}') 18 | export ROOT_DIR SCRIPT_NAME SCRIPT_ARGS PATH DEBUG_MODE _SHELL 19 | 20 | # not sourced from bash 21 | if [ $SCRIPT_NAME != "base_functions" ]; then 22 | set -e 23 | set -u 24 | set -o pipefail 25 | fi 26 | 27 | __TEMPFILES=('') 28 | __LOOPDEVS=('') 29 | __EXCEPTIONS=('') 30 | __FD_INFO=${__FD_INFO:-2} 31 | __LOG_FILE=${__LOG_FILE:-""} 32 | __CONSOLE_OUTPUT=${__CONSOLE_COLOR:-"yes"} 33 | __CONSOLE_TS=${__CONSOLE_TS:-"no"} 34 | __CONSOLE_COLOR=${__CONSOLE_COLOR:-"yes"} 35 | 36 | [ -f ${ROOT_DIR}/etc/environment ] && . ${ROOT_DIR}/etc/environment 37 | 38 | _call() { 39 | echo ". $LIB_PATH; trap - EXIT; $(declare -pf); $*" | $_SHELL 40 | } 41 | 42 | __umount_all() 43 | { 44 | local tmp_mnt=$1 45 | local ret=0 46 | 47 | if [ -z "$tmp_mnt" ] || [ ! -d "$tmp_mnt" ]; then 48 | return $ret 49 | fi 50 | 51 | # Flush unwritten writes 52 | sync 53 | tmp_mnt=$(cd $tmp_mnt && pwd) 54 | if awk '{print $2}' /proc/mounts | grep -qw ^$tmp_mnt; then 55 | # In case it has sub-mounts 56 | mnt_list=$(awk '{print $2}' /proc/mounts | grep ^$tmp_mnt | sort -ur) 57 | for mnt in $mnt_list; do 58 | umount -d $mnt || ret=$? 59 | done 60 | fi 61 | 62 | return $ret 63 | } 64 | 65 | __cleanup_tmps_loops() 66 | { 67 | for f in "${__TEMPFILES[@]}"; do 68 | [ -n "$f" ] || continue 69 | if [ -f "$f" ]; then 70 | rm -f $f 71 | fi 72 | 73 | if [ -d "$f" ]; then 74 | __umount_all $f 75 | rmdir $f 76 | fi 77 | done 78 | 79 | for dev in "${__LOOPDEVS[@]}"; do 80 | [ -n "$dev" ] || continue 81 | if ls /dev/mapper/"$(basename $dev)"* >/dev/null 2>&1; then 82 | kpartx -d $dev 83 | fi 84 | if losetup -a | grep -q $dev:; then 85 | losetup -d $dev 86 | fi 87 | done 88 | } 89 | 90 | # Clean up temp files, mountpoints, loop devices 91 | __cleanup() 92 | { 93 | set +x 94 | local ret=$1 95 | local func=$2 96 | local lineno=$3 97 | local comm=$4 98 | 99 | trap - EXIT 100 | 101 | for f in "${__EXCEPTIONS[@]}"; do 102 | [ -n "$f" ] || continue 103 | $f 104 | done 105 | 106 | __cleanup_tmps_loops 107 | 108 | if type cleanup >/dev/null 2>&1; then 109 | cleanup $ret 110 | fi 111 | 112 | if [ $ret -ne 0 ]; then 113 | if [ -z "$func" ]; then 114 | location="$lineno" 115 | else 116 | location="$func():$lineno" 117 | fi 118 | if echo $comm | grep -qw -e ^exit -e ^return; then 119 | if echo $comm | grep -qw ^return; then 120 | info "exit ($comm) near script $SCRIPT_NAME:$location" 121 | fi 122 | exit $ret 123 | fi 124 | die "Running command ($comm) near script $SCRIPT_NAME:$location failed" $ret 125 | fi 126 | 127 | return $ret 128 | } 129 | 130 | __interrupt() 131 | { 132 | info "Interrupted by signal $1" 133 | shift 134 | __cleanup "$@" 135 | } 136 | 137 | # Set up trap handler and debugger 138 | __setup() 139 | { 140 | trap 'exit $?' SIGUSR2 141 | trap '__cleanup $? "${FUNCNAME[0]-main}" "$LINENO" "$BASH_COMMAND"' EXIT 142 | trap '__interrupt SIGINT 130 "${FUNCNAME[0]-main}" "$LINENO" "$BASH_COMMAND"' SIGINT 143 | trap '__interrupt SIGTERM 130 "${FUNCNAME[0]-main}" "$LINENO" "$BASH_COMMAND"' SIGTERM 144 | 145 | if [ "$DEBUG_MODE" != "1" ]; then 146 | eval DEBUG_MODE="\${DEBUG_$(basename $SCRIPT_NAME .sh | sed 's/-/_/g' | sed 's/\./_/g')-0}" 147 | fi 148 | if [ "$DEBUG_MODE" = "1" ] || [[ $- =~ x ]]; then 149 | enable_debug 150 | fi 151 | 152 | if [ -n "$__LOG_FILE" ]; then 153 | # Adjust pipe to reflect correct script name 154 | exec 2> >(__add_ts >>$__LOG_FILE) 155 | fi 156 | } 157 | 158 | # Create temp files/directories 159 | # __cleanup() will clean up these files 160 | setup_tmp_file() 161 | { 162 | local var=$1 163 | local file_name=${2:-""} 164 | 165 | [[ -n $file_name ]] || file_name=$(mktemp /tmp/${SCRIPT_NAME}-XXXXXXXX) 166 | __TEMPFILES=("$file_name" "${__TEMPFILES[@]}") 167 | eval $var=$file_name 168 | } 169 | 170 | setup_tmp_mnt() 171 | { 172 | local var=$1 173 | local dir_name=${2:-""} 174 | 175 | [[ -n $dir_name ]] || dir_name=$(mktemp -d /tmp/${SCRIPT_NAME}-XXXXXXXX) 176 | __TEMPFILES=("$dir_name" "${__TEMPFILES[@]}") 177 | eval $var=$dir_name 178 | } 179 | 180 | setup_loop_dev() 181 | { 182 | local var=$1 183 | local image_file=$2 184 | local loop_dev="" 185 | 186 | if losetup -h 2>&1 | grep -q -- '--show'; then 187 | loop_dev=$(losetup -f --show $image_file) 188 | else 189 | loop_dev=$(losetup -f) 190 | losetup $loop_dev $image_file 191 | fi 192 | # Only kpartx works for loop device 193 | kpartx -va $loop_dev >&2 194 | __LOOPDEVS=("$loop_dev" "${__LOOPDEVS[@]}") 195 | eval $var=$loop_dev 196 | } 197 | 198 | set_exception() 199 | { 200 | local func="$*" 201 | 202 | __EXCEPTIONS=("$func" "${__EXCEPTIONS[@]}") 203 | } 204 | 205 | unset_exception() 206 | { 207 | unset __EXCEPTIONS[0] 208 | } 209 | 210 | # Add timestamp and script name before logging 211 | __add_ts() 212 | { 213 | awk -v SCRIPT="${SCRIPT_NAME}" '{ print strftime("%c: ") SCRIPT ": " $0; system("")}' 214 | } 215 | 216 | setup_log_file() 217 | { 218 | set +x 219 | local old_log="$__LOG_FILE" 220 | export __LOG_FILE=$1 221 | local msg=${2:-""} 222 | 223 | if [ $__FD_INFO -eq 2 ]; then 224 | # preserve the original stderr to be used to report informaion 225 | export __FD_INFO=7 226 | eval "exec $__FD_INFO>&2" 227 | fi 228 | 229 | if [ "$__LOG_FILE" != "$old_log" ]; then 230 | if [ -d /dev/fd ]; then 231 | exec 2> >(__add_ts >>$__LOG_FILE) 232 | if [ $__CONSOLE_OUTPUT = "no" ]; then 233 | exec 1> >(__add_ts >>$__LOG_FILE) 234 | fi 235 | else 236 | die "Could not find /dev/fd directory, try to run \"ln -s /proc/self/fd /dev/fd\" to fix it" 237 | fi 238 | fi 239 | 240 | if [ -n "$msg" ]; then 241 | __output_with_log "green" $msg 242 | fi 243 | __output_with_log "green" "Running \"$0 $SCRIPT_ARGS\" with log file: $__LOG_FILE" 244 | } 245 | 246 | # Options: 247 | # "ts|no_ts": output w/? timestamps 248 | # "color|no_color": output w/? colors 249 | # eg. 250 | # setup_console "ts" "no_color" 251 | # 252 | setup_console() 253 | { 254 | local opt="" 255 | for opt in "$@"; do 256 | case $opt in 257 | ts) 258 | export __CONSOLE_TS=yes 259 | ;; 260 | no_ts) 261 | export __CONSOLE_TS=no 262 | ;; 263 | color) 264 | export __CONSOLE_COLOR=yes 265 | ;; 266 | no_color) 267 | export __CONSOLE_COLOR=no 268 | ;; 269 | no_output) 270 | export __CONSOLE_OUTPUT=no 271 | exec 1>/dev/null 272 | exec 2>/dev/null 273 | ;; 274 | esac 275 | done 276 | } 277 | 278 | __support_color() 279 | { 280 | type tput >/dev/null 2>&1 || return 1 281 | [ $__CONSOLE_COLOR = "yes" ] || return 1 282 | [ -t $1 ] && [ -n "$(tput colors)" ] && [ "$(tput colors)" -ge 8 ] 283 | } 284 | 285 | __color() 286 | { 287 | local color=$1 288 | shift 289 | 290 | case $color in 291 | red) 292 | echo "$(tput setaf 1)$(tput bold)$*$(tput sgr0)" 293 | ;; 294 | green) 295 | echo "$(tput setaf 2)$(tput bold)$*$(tput sgr0)" 296 | ;; 297 | yellow) 298 | echo "$(tput setaf 3)$(tput bold)$*$(tput sgr0)" 299 | ;; 300 | *) 301 | echo "$*" 302 | esac 303 | } 304 | 305 | __output() 306 | { 307 | local fd=$1 308 | local color=$2 309 | shift 2 310 | local msg="$*" 311 | 312 | if __support_color $fd; then 313 | __color $color "$msg" >&$fd 314 | else 315 | echo "$msg" >&$fd 316 | fi 317 | } 318 | 319 | # Output to both console and the log file 320 | __output_with_log() 321 | { 322 | set +x 323 | local color=$1 324 | shift 325 | local msg="$*" 326 | 327 | msg_with_ts="$(echo "$msg" | __add_ts)" 328 | if [ $__FD_INFO -ne 2 ]; then 329 | # log file always with timestamps, already set 330 | __output 2 $color "$msg" 331 | fi 332 | 333 | if [ $__CONSOLE_OUTPUT = "yes" ]; then 334 | if [ "$__CONSOLE_TS" = "no" ]; then 335 | __output $__FD_INFO $color "$msg" 336 | else 337 | __output $__FD_INFO $color "$msg_with_ts" 338 | fi 339 | fi 340 | 341 | if [ "$DEBUG_MODE" = "1" ]; then 342 | set -x 343 | fi 344 | } 345 | 346 | __ret_info() 347 | { 348 | local ret=$1 349 | 350 | if [ "$ret" -ne 0 ]; then 351 | echo " (ret=$ret)" 352 | fi 353 | } 354 | 355 | output() 356 | { 357 | echo "$*" 358 | } 359 | 360 | info() 361 | { 362 | set +x 363 | __output_with_log "default" "$*" 364 | } 365 | 366 | warn() 367 | { 368 | set +x 369 | __output_with_log "yellow" "WARNING: $*" 370 | } 371 | 372 | err() 373 | { 374 | local last_ret=$? 375 | local msg="$1" 376 | local ret=${2-0} 377 | 378 | set +x 379 | [ "$ret" -eq 0 ] && ret=$last_ret 380 | __output_with_log "red" "ERROR: $msg$(__ret_info $ret)" 381 | } 382 | 383 | die() 384 | { 385 | local last_ret=$? 386 | local msg="$1" 387 | local ret=${2-0} 388 | 389 | set +x 390 | [ "$ret" -eq 0 ] && ret=$last_ret 391 | __output_with_log "red" "FATAL: $msg$(__ret_info $ret)" 392 | 393 | sync # To flush logs 394 | [ "$ret" -ne 0 ] || ret=1 395 | exit $ret 396 | } 397 | 398 | check_root_permission() 399 | { 400 | if [ "$EUID" -ne 0 ]; then 401 | die "Please run as root" 402 | fi 403 | } 404 | 405 | enable_debug() 406 | { 407 | set +x 408 | export DEBUG_MODE=1 409 | export PS4='+${BASH_SOURCE:-""}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' 410 | set -x 411 | } 412 | 413 | disable_debug() 414 | { 415 | set +x 416 | export DEBUG_MODE=0 417 | } 418 | 419 | __setup 420 | -------------------------------------------------------------------------------- /src/bash_backend.rs: -------------------------------------------------------------------------------- 1 | use crate::parser::Expr; 2 | use crate::parser::Stmt; 3 | use crate::parser::Stmt::{CallCmd, DefCmd, DefFun, DefVar, Return}; 4 | use std::fs; 5 | use std::fs::File; 6 | use std::io::Write; 7 | use std::os::unix::fs::OpenOptionsExt; 8 | 9 | const INDENT: isize = 4; 10 | 11 | macro_rules! output { 12 | ($fs: expr, $indent: expr, $($arg:tt)*) => { 13 | write!($fs, "{}", (0..$indent).map(|_| " ").collect::()).expect("write error"); 14 | writeln!($fs, $($arg)*).expect("write error"); 15 | } 16 | } 17 | 18 | pub fn gen_code(stmts: &Vec, file: &str) { 19 | eprintln!("Generating bash script to {} ...", file); 20 | let mut file = fs::OpenOptions::new() 21 | .create(true) 22 | .write(true) 23 | .truncate(true) 24 | .mode(0o755) 25 | .open(file) 26 | .expect("create file for writing failed"); 27 | 28 | output!(file, 0, "#!/bin/bash"); 29 | output!(file, 0, "#"); 30 | output!(file, 0, "# Generated by rust-shell-script"); 31 | output!(file, 0, "#"); 32 | output!(file, 0, ". \"${{RUNTIME:-.}}/cmd_lib.sh\""); 33 | output!(file, 0, ""); 34 | 35 | for stmt in stmts { 36 | match stmt { 37 | DefFun(fun_name, parameters, body) => { 38 | visit_def_fun(&mut file, &fun_name, ¶meters, &body) 39 | } 40 | DefCmd(cmd_name, parameters, body) => { 41 | visit_def_cmd(&mut file, &cmd_name, ¶meters, &body) 42 | } 43 | _ => eprintln!("Not supported yet!"), 44 | } 45 | } 46 | 47 | output!(file, 0, "main \"$@\""); 48 | } 49 | 50 | fn visit_def_var(file: &mut File, indent: &mut isize, var_name: &str, var_def: &Option) { 51 | if let Some(expr) = var_def { 52 | output!(file, *indent, "local {}", var_name); 53 | output!(file, *indent, "{}={}", var_name, visit_expr(expr)); 54 | } else { 55 | output!(file, *indent, "local {}", var_name); 56 | } 57 | } 58 | 59 | fn visit_def_fun(file: &mut File, fun_name: &str, parameters: &Vec, body: &Vec) { 60 | let mut indent = 0; 61 | output!(file, indent, "function {}() {{", fun_name); 62 | 63 | indent += INDENT; 64 | for (i, p) in parameters.iter().enumerate() { 65 | output!(file, indent, "local {}=\"${}\"", p, i + 1); 66 | } 67 | if parameters.len() > 0 { 68 | output!(file, 0, ""); 69 | } 70 | 71 | for stmt in body { 72 | visit_stmt(file, &mut indent, stmt); 73 | } 74 | 75 | output!(file, 0, "}}"); 76 | output!(file, 0, ""); 77 | } 78 | 79 | fn visit_def_cmd(file: &mut File, fun_name: &str, parameters: &Vec, body: &Vec) { 80 | let mut indent = 0; 81 | output!(file, indent, "{}() {{", fun_name); 82 | 83 | indent += INDENT; 84 | for (i, p) in parameters.iter().enumerate() { 85 | output!(file, indent, "local {}=\"${}\"", p, i + 1); 86 | } 87 | if parameters.len() > 0 { 88 | output!(file, 0, ""); 89 | } 90 | 91 | for stmt in body { 92 | visit_stmt(file, &mut indent, stmt); 93 | } 94 | 95 | output!(file, 0, "}}"); 96 | output!(file, 0, ""); 97 | } 98 | 99 | fn visit_stmt(file: &mut File, indent: &mut isize, stmt: &Stmt) { 100 | match stmt { 101 | CallCmd(cmd, parameters) => visit_call_cmd(file, indent, &cmd, ¶meters), 102 | Return(expr) => visit_return(file, indent, &expr), 103 | DefVar(var_name, var_def) => visit_def_var(file, indent, &var_name, &var_def), 104 | _ => { 105 | let stmt = format!("{:?}", stmt); 106 | output!(file, *indent, "{}", stmt); 107 | } 108 | } 109 | } 110 | 111 | fn visit_call_cmd(file: &mut File, indent: &mut isize, cmd: &str, parameters: &Vec) { 112 | if parameters.len() == 0 { 113 | output!(file, *indent, "{}", cmd); 114 | } else { 115 | output!(file, *indent, "{} {}", cmd, visit_call(parameters)); 116 | } 117 | } 118 | 119 | fn visit_call(parameters: &Vec) -> String { 120 | let mut args = String::new(); 121 | for (i, expr) in parameters.iter().enumerate() { 122 | if i > 0 { 123 | args += " "; 124 | } 125 | args += visit_expr(expr).as_ref(); 126 | } 127 | args 128 | } 129 | 130 | fn visit_return(file: &mut File, indent: &mut isize, expr: &Expr) { 131 | output!(file, *indent, "return {}", visit_expr(expr)); 132 | } 133 | 134 | fn visit_expr(expr: &Expr) -> String { 135 | match expr { 136 | Expr::LitNum(n) => format!("{}", n), 137 | Expr::LitStr(s) => format!("\"{}\"", s), 138 | Expr::Var(v) => format!("\"${{{}}}\"", v.identifier), 139 | Expr::CallFun(f, args) => format!("$(_call {} {})", f, visit_call(args)), 140 | _ => format!("{:?}", expr), 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod util; 2 | mod scanner; 3 | mod parser; 4 | mod resolver; 5 | mod bash_backend; 6 | mod rust_backend; 7 | 8 | use std::env; 9 | use std::process; 10 | use std::fs; 11 | 12 | fn usage(cmd: &str) { 13 | eprintln!("Usage: {} {{-t|--target bash|rust}} ", cmd); 14 | process::exit(1); 15 | } 16 | 17 | fn main() { 18 | let args: Vec = env::args().collect(); 19 | 20 | if args.len() != 4 || 21 | (args[1] != "-t" && args[1] != "--target") || 22 | (args[2] != "bash" && args[2] != "rust") { 23 | usage(&args[0]); 24 | } 25 | let (target, filename) = (&args[2], &args[3]); 26 | compile(filename, target); 27 | } 28 | 29 | fn compile(filename: &str, target: &str) { 30 | let file: Vec = fs::read_to_string(filename) 31 | .expect("error reading file") 32 | .chars() 33 | .collect(); 34 | 35 | let tokens = scanner::scan(file); 36 | let stmts = parser::parse(tokens); 37 | let sym_table = resolver::gen_sym_table(&stmts); 38 | 39 | if target == "bash" { 40 | bash_backend::gen_code(&stmts, 41 | &String::from(filename).replace(".rss", ".sh")); 42 | } else { 43 | rust_backend::gen_code(&stmts, 44 | &sym_table, 45 | &String::from(filename).replace(".rss", ".rs")); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/parser.rs: -------------------------------------------------------------------------------- 1 | use crate::parser::Expr::CallFun; 2 | use std::process; 3 | use crate::scanner::Token; 4 | use crate::util::CmpType; 5 | use crate::util::Newline; 6 | use crate::util::ScanIter; 7 | use crate::parser::Stmt::{CallCmd, DefCmd, DefFun, Return}; 8 | 9 | #[derive(Debug)] 10 | pub enum Stmt { 11 | DefVar(String, Option), 12 | DefFun(String, Vec, Vec), 13 | DefCmd(String, Vec, Vec), 14 | 15 | CallCmd(String, Vec), 16 | Return(Expr), 17 | } 18 | impl CmpType for Stmt{} 19 | 20 | #[derive(Debug)] 21 | pub enum Expr { 22 | LitNum(isize), 23 | LitStr(String), 24 | Var(Var), 25 | CallFun(String, Vec), 26 | } 27 | 28 | #[derive(Clone, Debug)] 29 | pub struct Var { 30 | pub identifier: String, 31 | pub hops: usize, 32 | } 33 | impl Var { 34 | pub fn new(id: String) -> Self { 35 | Self { 36 | identifier: id, 37 | hops: 0, 38 | } 39 | } 40 | } 41 | 42 | macro_rules! parse_error { 43 | ($line: expr, $($arg:tt)*) => { 44 | eprint!("Parse error at line {}: ", $line); 45 | eprintln!($($arg)*); 46 | process::exit(1); 47 | } 48 | } 49 | 50 | impl ScanIter { 51 | fn consume(&mut self, expected: Token) { 52 | if self.match_item(expected.clone()) { 53 | return; 54 | } else { 55 | parse_error!(self.line(), "got {:?}, expect token: {:?}", 56 | self.peek(), expected); 57 | } 58 | } 59 | 60 | fn consume_identifier(&mut self) -> String { 61 | if let Token::Identifier(ref name) = *self.peek() { 62 | let id = name.clone(); 63 | self.advance(); 64 | id 65 | } else { 66 | parse_error!(self.line(), "got {:?}, expect identifier", self.peek()); 67 | } 68 | } 69 | 70 | fn consume_litstr(&mut self) -> String { 71 | if let Token::String(ref name) = *self.peek() { 72 | let id = name.clone(); 73 | self.advance(); 74 | id 75 | } else { 76 | parse_error!(self.line(), "got {:?}, expect literal string", self.peek()); 77 | } 78 | } 79 | } 80 | 81 | pub fn parse(tokens: Vec) -> Vec { 82 | let mut tokens = ScanIter::new(tokens); 83 | let mut stmts = Vec::new(); 84 | 85 | loop { 86 | while !tokens.is_at_end() && tokens.peek().is_newline() { 87 | tokens.advance(); 88 | } 89 | if tokens.is_at_end() { 90 | break; 91 | } 92 | 93 | let stmt = parse_stmt(&mut tokens); 94 | stmts.push(stmt); 95 | } 96 | stmts 97 | } 98 | 99 | fn parse_stmt(tokens: &mut ScanIter) -> Stmt { 100 | if tokens.match_item(Token::Fun) { 101 | return stmt_def_fun(tokens); 102 | } else if tokens.match_item(Token::Cmd) { 103 | return stmt_def_cmd(tokens); 104 | } else if tokens.match_item(Token::Return) { 105 | return stmt_return(tokens); 106 | } 107 | 108 | parse_error!(tokens.line(), "unexpected token: {:?}", tokens.peek()); 109 | } 110 | 111 | fn stmt_def_fun(tokens: &mut ScanIter) -> Stmt { 112 | let id = tokens.consume_identifier(); 113 | tokens.consume(Token::LeftParen); 114 | let (parameters, body) = fun_parameters_and_body(tokens); 115 | DefFun(id, parameters, body) 116 | } 117 | 118 | fn stmt_def_cmd(tokens: &mut ScanIter) -> Stmt { 119 | let id = tokens.consume_identifier(); 120 | tokens.consume(Token::LeftParen); 121 | let (parameters, body) = fun_parameters_and_body(tokens); 122 | DefCmd(id, parameters, body) 123 | } 124 | 125 | fn stmt_return(tokens: &mut ScanIter) -> Stmt { 126 | if let Token::Number(n) = *tokens.peek() { 127 | let ret = n.clone(); 128 | tokens.advance(); 129 | return Return(Expr::LitNum(ret)); 130 | } else if let Token::String(ref s) = *tokens.peek() { 131 | let ret = s.clone(); 132 | tokens.advance(); 133 | return Return(Expr::LitStr(ret)); 134 | } 135 | parse_error!(tokens.line(), "Wrong return value"); 136 | } 137 | 138 | fn fun_parameters_and_body(tokens: &mut ScanIter) -> (Vec, Vec) { 139 | let mut parameters = Vec::new(); 140 | if *tokens.peek() != Token::RightParen { 141 | loop { 142 | let id = tokens.consume_identifier(); 143 | parameters.push(id); 144 | if !tokens.match_item(Token::Comma) { 145 | break; 146 | } 147 | } 148 | } 149 | tokens.consume(Token::RightParen); 150 | tokens.consume(Token::LeftBrace); 151 | let body = block_statement(tokens); 152 | 153 | (parameters, body) 154 | } 155 | 156 | 157 | fn block_statement(tokens: &mut ScanIter) -> Vec { 158 | let mut stmts = Vec::new(); 159 | 160 | while *tokens.peek() != Token::RightBrace && !tokens.is_at_end() { 161 | if tokens.peek().is_newline() { 162 | tokens.advance(); 163 | continue; 164 | } 165 | stmts.push(cmds(tokens)); 166 | } 167 | 168 | tokens.consume(Token::RightBrace); 169 | stmts 170 | } 171 | 172 | fn cmds(tokens: &mut ScanIter) -> Stmt { 173 | if tokens.match_item(Token::Let) { 174 | var_declaration(tokens) 175 | } else if tokens.match_item(Token::Return) { 176 | stmt_return(tokens) 177 | } else { 178 | call_cmd(tokens) 179 | } 180 | } 181 | 182 | fn var_declaration(tokens: &mut ScanIter) -> Stmt { 183 | let name = tokens.consume_identifier(); 184 | 185 | let initializer = if tokens.match_item(Token::Equal) { 186 | Some(expression(tokens)) 187 | } else { 188 | None 189 | }; 190 | 191 | Stmt::DefVar(name, initializer) 192 | } 193 | 194 | fn call_cmd(tokens: &mut ScanIter) -> Stmt { 195 | let id = tokens.consume_identifier(); 196 | let mut args = Vec::new(); 197 | while *tokens.peek() != Token::Newline { 198 | if tokens.check_item(Token::String("".into())) { 199 | args.push(Expr::LitStr(tokens.consume_litstr())); 200 | } else { 201 | tokens.consume(Token::DollarSign); 202 | args.push(Expr::Var(Var::new(tokens.consume_identifier()))); 203 | } 204 | } 205 | 206 | CallCmd(id, args) 207 | } 208 | 209 | fn expression(tokens: &mut ScanIter) -> Expr { 210 | if let Token::Number(n) = *tokens.peek() { 211 | let ret = n.clone(); 212 | tokens.advance(); 213 | return Expr::LitNum(ret); 214 | } else if let Token::String(ref s) = *tokens.peek() { 215 | let ret = s.clone(); 216 | tokens.advance(); 217 | return Expr::LitStr(ret); 218 | } else if tokens.match_item(Token::DollarSign) { 219 | if tokens.match_item(Token::LeftParen) { 220 | let res = call_fun(tokens); 221 | tokens.consume(Token::RightParen); 222 | return res; 223 | } else { 224 | return Expr::LitStr(tokens.consume_identifier()); 225 | } 226 | } 227 | parse_error!(tokens.line(), "Wrong expression"); 228 | } 229 | 230 | fn call_fun(tokens: &mut ScanIter) -> Expr { 231 | let id = tokens.consume_identifier(); 232 | let mut args = Vec::new(); 233 | while *tokens.peek() != Token::Newline { 234 | if tokens.check_item(Token::String("".into())) { 235 | args.push(Expr::LitStr(tokens.consume_litstr())); 236 | } else if tokens.match_item(Token::DollarSign) { 237 | args.push(Expr::Var(Var::new(tokens.consume_identifier()))); 238 | } else { 239 | break; 240 | } 241 | } 242 | 243 | CallFun(id, args) 244 | } 245 | -------------------------------------------------------------------------------- /src/resolver.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashSet; 2 | use crate::parser::Stmt; 3 | use crate::parser::Stmt::{DefCmd, DefFun}; 4 | 5 | pub fn gen_sym_table(stmts: &Vec) -> HashSet<&String> { 6 | let mut sym_table = HashSet::new(); 7 | for stmt in stmts { 8 | match stmt { 9 | DefFun(fun_name, _, _) => { 10 | sym_table.insert(fun_name); 11 | } 12 | DefCmd(cmd_name, _, _) => { 13 | sym_table.insert(cmd_name); 14 | } 15 | _ => (), 16 | } 17 | } 18 | 19 | sym_table 20 | } 21 | -------------------------------------------------------------------------------- /src/rust_backend.rs: -------------------------------------------------------------------------------- 1 | use crate::parser::Expr; 2 | use crate::parser::Stmt; 3 | use crate::parser::Stmt::{CallCmd, DefCmd, DefFun, DefVar, Return}; 4 | use std::fs; 5 | use std::fs::File; 6 | use std::io::Write; 7 | use std::os::unix::fs::OpenOptionsExt; 8 | use std::collections::HashSet; 9 | 10 | const INDENT: isize = 4; 11 | 12 | macro_rules! output { 13 | ($fs: expr, $indent: expr, $($arg:tt)*) => { 14 | write!($fs, "{}", (0..$indent).map(|_| " ").collect::()).expect("write error"); 15 | writeln!($fs, $($arg)*).expect("write error"); 16 | } 17 | } 18 | 19 | pub fn gen_code(stmts: &Vec, sym_table: &HashSet<&String>, file: &str) { 20 | eprintln!("Generating rust code to {} ...", file); 21 | let mut file = fs::OpenOptions::new() 22 | .create(true) 23 | .write(true) 24 | .truncate(true) 25 | .open(file) 26 | .expect("create file for writing failed"); 27 | 28 | output!(file, 0, "// Generated by rust-shell-script"); 29 | output!(file, 0, "mod cmd_lib;"); 30 | output!(file, 0, "use crate::cmd_lib::{{CmdResult, FunResult}};"); 31 | output!(file, 0, ""); 32 | 33 | for stmt in stmts { 34 | match stmt { 35 | DefFun(fun_name, parameters, body) => { 36 | visit_def_fun(sym_table, &mut file, &fun_name, ¶meters, &body) 37 | } 38 | DefCmd(cmd_name, parameters, body) => { 39 | visit_def_cmd(sym_table, &mut file, &cmd_name, ¶meters, &body) 40 | } 41 | _ => eprintln!("Not supported yet!"), 42 | } 43 | } 44 | } 45 | 46 | fn visit_def_var(file: &mut File, indent: &mut isize, var_name: &str, var_def: &Option) { 47 | if let Some(expr) = var_def { 48 | output!(file, *indent, "let {} = {};", var_name, visit_expr(expr)); 49 | } else { 50 | output!(file, *indent, "let {} = String::new();", var_name); 51 | } 52 | } 53 | 54 | fn visit_def_fun(sym_table: &HashSet<&String>, file: &mut File, fun_name: &str, parameters: &Vec, body: &Vec) { 55 | let mut indent = 0; 56 | let mut fun_args = String::new(); 57 | 58 | fun_args = format!("fn {}(", fun_name); 59 | for (i, p) in parameters.iter().enumerate() { 60 | if i != 0 { 61 | fun_args += ", "; 62 | } 63 | fun_args += format!("{}: &str", p).as_ref(); 64 | } 65 | fun_args += ") -> FunResult {"; 66 | output!(file, 0, "{}", fun_args); 67 | 68 | indent += INDENT; 69 | for (i, stmt) in body.iter().enumerate() { 70 | visit_stmt(sym_table, file, &mut indent, stmt, i == body.len()-1); 71 | } 72 | 73 | output!(file, 0, "}}"); 74 | output!(file, 0, ""); 75 | } 76 | 77 | fn visit_def_cmd(sym_table: &HashSet<&String>, file: &mut File, fun_name: &str, parameters: &Vec, body: &Vec) { 78 | let mut indent = 0; 79 | let mut fun_args = String::new(); 80 | 81 | fun_args = format!("fn {}(", fun_name); 82 | for (i, p) in parameters.iter().enumerate() { 83 | if i != 0 { 84 | fun_args += ", "; 85 | } 86 | fun_args += format!("{}: &str", p).as_ref(); 87 | } 88 | fun_args += ") -> CmdResult {"; 89 | output!(file, 0, "{}", fun_args); 90 | 91 | indent += INDENT; 92 | for (i, stmt) in body.iter().enumerate() { 93 | visit_stmt(sym_table, file, &mut indent, stmt, i == body.len()-1); 94 | } 95 | 96 | output!(file, 0, "}}"); 97 | output!(file, 0, ""); 98 | } 99 | 100 | fn visit_stmt(sym_table: &HashSet<&String>, file: &mut File, indent: &mut isize, stmt: &Stmt, is_last: bool) { 101 | match stmt { 102 | CallCmd(cmd, parameters) => visit_call_cmd(sym_table, file, indent, &cmd, ¶meters, is_last), 103 | Return(expr) => visit_return(file, indent, &expr), 104 | DefVar(var_name, var_def) => visit_def_var(file, indent, &var_name, &var_def), 105 | _ => { 106 | let mut stmt = format!("{:?}", stmt); 107 | if !is_last { 108 | stmt += "?"; 109 | } 110 | output!(file, *indent, "{}", stmt); 111 | } 112 | } 113 | } 114 | 115 | fn visit_call_cmd(sym_table: &HashSet<&String>, file: &mut File, indent: &mut isize, cmd: &str, parameters: &Vec, is_last: bool) { 116 | let mut cmd = String::from(cmd); 117 | let mut ending = String::new(); 118 | let mut builtin = false; 119 | 120 | if !is_last { 121 | if cmd == "info" { 122 | ending += ";"; 123 | } else { 124 | ending += "?;"; 125 | } 126 | } 127 | if cmd == "info" || cmd == "output" { 128 | cmd += "!"; 129 | builtin = true; 130 | } 131 | 132 | if builtin || sym_table.contains(&cmd) { 133 | if parameters.len() == 0 { 134 | output!(file, *indent, "{}(){}", cmd, ending); 135 | } else { 136 | output!(file, *indent, "{}({}){}", cmd, visit_call(parameters), ending); 137 | } 138 | } else { 139 | if parameters.len() == 0 { 140 | output!(file, *indent, "run_cmd!(\"{}\"){}", cmd, ending); 141 | } else { 142 | output!(file, *indent, "run_cmd!(\"{} {}\"){}", cmd, visit_call(parameters), ending); 143 | } 144 | } 145 | } 146 | 147 | fn visit_call(parameters: &Vec) -> String { 148 | let mut args = String::new(); 149 | for (i, expr) in parameters.iter().enumerate() { 150 | if i > 0 { 151 | args += " "; 152 | } 153 | args += visit_expr(expr).as_ref(); 154 | } 155 | format_str(&args) 156 | } 157 | 158 | fn visit_return(file: &mut File, indent: &mut isize, expr: &Expr) { 159 | output!(file, *indent, "return {}", visit_expr(expr)); 160 | } 161 | 162 | fn visit_expr(expr: &Expr) -> String { 163 | match expr { 164 | Expr::LitNum(n) => { 165 | if *n == 0 { 166 | format!("Ok(())") 167 | } else { 168 | format!("Err(())") 169 | } 170 | }, 171 | Expr::LitStr(s) => format!("\"{}\"", s), 172 | Expr::Var(v) => format!("\"${{{}}}\"", v.identifier), 173 | Expr::CallFun(f, args) => format!("{}({})?;", f, visit_call(args)), 174 | _ => format!("{:?}", expr), 175 | } 176 | } 177 | 178 | fn format_str(input: &str) -> String { 179 | let mut output = String::new(); 180 | let mut vars = Vec::new(); 181 | let mut input = input.chars().peekable(); 182 | 183 | 184 | while let Some(c) = input.next() { 185 | if c == '$' && input.peek() == Some(&'{') { 186 | 187 | input.next(); 188 | let mut var = String::new(); 189 | while let Some(v) = input.next() { 190 | if v != '}' { 191 | var.push(v); 192 | } else { 193 | break; 194 | } 195 | } 196 | output += "{}"; 197 | vars.push(var); 198 | } else { 199 | output.push(c); 200 | } 201 | } 202 | 203 | for v in vars { 204 | output += ", "; 205 | output += v.as_ref(); 206 | } 207 | 208 | output 209 | } 210 | 211 | #[test] 212 | fn test_format_str() { 213 | assert_eq!(format_str("${a} aa ${b} bb ${cc}"), "{} aa {} bb {}, a, b, cc".to_string()); 214 | } 215 | -------------------------------------------------------------------------------- /src/scanner.rs: -------------------------------------------------------------------------------- 1 | use std::process; 2 | use crate::util::Newline; 3 | use crate::util::CmpType; 4 | use crate::util::ScanIter; 5 | use crate::util::char; 6 | 7 | #[derive(Debug, PartialEq, Clone)] 8 | pub enum Token { 9 | LeftParen, 10 | RightParen, 11 | LeftBrace, 12 | RightBrace, 13 | DollarSign, 14 | Slash, 15 | Comma, 16 | 17 | Bang, 18 | BangEqual, 19 | Equal, 20 | EqualEqual, 21 | 22 | Identifier(String), 23 | String(String), 24 | Number(isize), 25 | 26 | Fun, 27 | Cmd, 28 | Main, 29 | Return, 30 | Let, 31 | If, 32 | While, 33 | For, 34 | 35 | Newline, 36 | Eof, 37 | } 38 | 39 | impl CmpType for Token {} 40 | impl Newline for Token { 41 | fn is_newline(&self) -> bool { 42 | *self == Token::Newline 43 | } 44 | } 45 | 46 | macro_rules! scan_error { 47 | ($line: expr, $($arg:tt)*) => { 48 | eprint!("Scan error at line {}: ", $line); 49 | eprintln!($($arg)*); 50 | process::exit(1); 51 | } 52 | } 53 | 54 | pub fn scan(src: Vec) -> Vec { 55 | let mut src = ScanIter::new(src); 56 | let mut tokens: Vec = Vec::new(); 57 | 58 | loop { 59 | let token = scan_token(&mut src); 60 | tokens.push(token); 61 | if src.is_at_end() { 62 | break; 63 | } 64 | } 65 | tokens 66 | } 67 | 68 | fn scan_token(src: &mut ScanIter) -> Token { 69 | if src.is_at_end() { 70 | return Token::Eof; 71 | } 72 | 73 | let line = src.line(); 74 | let c = src.peek().clone(); 75 | src.advance(); 76 | match c { 77 | '(' => Token::LeftParen, 78 | ')' => Token::RightParen, 79 | '{' => Token::LeftBrace, 80 | '}' => Token::RightBrace, 81 | '$' => Token::DollarSign, 82 | ',' => Token::Comma, 83 | '!' => if src.match_item('=') { 84 | Token::BangEqual 85 | } else { 86 | Token::Bang 87 | }, 88 | '=' => if src.match_item('=') { 89 | Token::EqualEqual 90 | } else { 91 | Token::Equal 92 | }, 93 | '/' => { 94 | if src.match_item('/') { 95 | while *src.peek() != '\n' && !src.is_at_end() { 96 | src.advance(); 97 | } 98 | scan_token(src) 99 | } else { 100 | Token::Slash 101 | } 102 | }, 103 | // skip whitespaces 104 | ' '|'\r'|'\t' => scan_token(src), 105 | '\n' => Token::Newline, 106 | '"' => scan_string(src), 107 | _ => { 108 | if char::is_digit(c) { 109 | scan_number(src) 110 | } else if char::is_alpha_or_underscore(c) { 111 | scan_identifier_or_keyword(src) 112 | } else { 113 | scan_error!(line, "unexpected character '{}'", c); 114 | } 115 | } 116 | } 117 | } 118 | 119 | fn scan_string(src: &mut ScanIter) -> Token { 120 | let mut s = String::new(); 121 | let mut c = *src.peek(); 122 | 123 | while c != '"' && !src.is_at_end() { 124 | if c == '\n' { 125 | scan_error!(src.line(), "do not support multi-line string"); 126 | } 127 | s.push(c); 128 | src.advance(); 129 | c = *src.peek(); 130 | } 131 | 132 | if src.is_at_end() { 133 | scan_error!(src.line(), "unterminated string"); 134 | } 135 | 136 | // Closing ". 137 | src.advance(); 138 | Token::String(s) 139 | } 140 | 141 | fn scan_number(src: &mut ScanIter) -> Token { 142 | let mut s = String::new(); 143 | let mut c = *src.peek(); 144 | 145 | s.push(*src.previous()); 146 | while char::is_digit(c) { 147 | s.push(c); 148 | src.advance(); 149 | c = *src.peek(); 150 | } 151 | Token::Number(s.parse().unwrap()) 152 | } 153 | 154 | fn scan_identifier_or_keyword(src: &mut ScanIter) -> Token { 155 | let mut s = String::new(); 156 | let mut c = *src.peek(); 157 | 158 | s.push(*src.previous()); 159 | while char::is_alpha_numeric_or_underscore(c) { 160 | s.push(c); 161 | src.advance(); 162 | c = *src.peek(); 163 | } 164 | 165 | if c == '!' { 166 | s.push(c); 167 | src.advance(); 168 | return Token::Identifier(s); 169 | } 170 | 171 | match s.as_ref() { 172 | "fun" => Token::Fun, 173 | "cmd" => Token::Cmd, 174 | "let" => Token::Let, 175 | "return" => Token::Return, 176 | _ => Token::Identifier(s), 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/util/char.rs: -------------------------------------------------------------------------------- 1 | use crate::util::scan_iter::Newline; 2 | use crate::util::scan_iter::CmpType; 3 | 4 | impl Newline for char { 5 | fn is_newline(&self) -> bool { 6 | *self == '\n' 7 | } 8 | } 9 | 10 | impl CmpType for char { 11 | fn cmp_type(&self, other: &char) -> bool { 12 | *self == *other 13 | } 14 | } 15 | 16 | pub fn is_digit(c: char) -> bool { 17 | c >= '0' && c <= '9' 18 | } 19 | 20 | pub fn is_alpha_or_underscore(c: char) -> bool { 21 | c == '_' || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 22 | } 23 | 24 | pub fn is_alpha_numeric_or_underscore(c: char) -> bool { 25 | is_alpha_or_underscore(c) || is_digit(c) 26 | } 27 | -------------------------------------------------------------------------------- /src/util/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod char; 2 | pub mod scan_iter; 3 | 4 | pub use scan_iter::CmpType; 5 | pub use scan_iter::Newline; 6 | pub use scan_iter::ScanIter; -------------------------------------------------------------------------------- /src/util/scan_iter.rs: -------------------------------------------------------------------------------- 1 | use std::mem::discriminant; 2 | use std::borrow::Borrow; 3 | 4 | pub trait Newline { 5 | fn is_newline(&self) -> bool; 6 | } 7 | 8 | pub trait CmpType: Sized { 9 | fn cmp_type(&self, other: &Self) -> bool { 10 | discriminant(self) == discriminant(other) 11 | } 12 | } 13 | 14 | pub struct ScanIter { 15 | items: Vec, 16 | line: usize, 17 | pos: usize, 18 | } 19 | 20 | impl ScanIter { 21 | pub fn new(items: Vec) -> Self { 22 | Self { items, pos: 0, line: 1} 23 | } 24 | 25 | pub fn is_at_end(&self) -> bool { 26 | self.pos >= self.items.len() 27 | } 28 | 29 | pub fn line(&self) -> usize { 30 | self.line 31 | } 32 | 33 | pub fn advance(&mut self) { 34 | if !self.is_at_end() { 35 | if self.items[self.pos].is_newline() { 36 | self.line += 1; 37 | } 38 | self.pos += 1; 39 | } 40 | } 41 | 42 | pub fn previous(&mut self) -> &Item { 43 | &self.items[self.pos - 1] 44 | } 45 | 46 | pub fn peek(&self) -> &Item { 47 | &self.items[self.pos] 48 | } 49 | 50 | pub fn check_item>(&mut self, expected: T) -> bool { 51 | if self.items[self.pos].is_newline() { 52 | self.advance(); 53 | } 54 | if self.is_at_end() { return false; } 55 | self.peek().cmp_type(expected.borrow()) 56 | } 57 | 58 | pub fn match_item>(&mut self, expected: T) -> bool { 59 | if self.check_item(expected) { 60 | self.advance(); 61 | true 62 | } else { 63 | false 64 | } 65 | } 66 | 67 | pub fn match_any_item(&mut self, expected: &[Item]) -> bool { 68 | for e in expected { 69 | if self.match_item(e) { return true; } 70 | } 71 | false 72 | } 73 | } 74 | --------------------------------------------------------------------------------