-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…280) `env_var(key)` looks up the value of the environment variable with name `key`, aborting execution if it is not found. `env_var_or_default(key, default)` looks up the value of the environment variable with name `key`, returning `default` if it is not found.
- Loading branch information
Showing
13 changed files
with
240 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,104 @@ | ||
use common::*; | ||
use target; | ||
|
||
pub fn resolve_function<'a>(token: &Token<'a>) -> CompilationResult<'a, ()> { | ||
if !&["arch", "os", "os_family"].contains(&token.lexeme) { | ||
Err(token.error(CompilationErrorKind::UnknownFunction{function: token.lexeme})) | ||
lazy_static! { | ||
static ref FUNCTIONS: Map<&'static str, Function> = vec![ | ||
("arch", Function::Nullary(arch )), | ||
("os", Function::Nullary(os )), | ||
("os_family", Function::Nullary(os_family )), | ||
("env_var", Function::Unary (env_var )), | ||
("env_var_or_default", Function::Binary (env_var_or_default)), | ||
].into_iter().collect(); | ||
} | ||
|
||
enum Function { | ||
Nullary(fn( ) -> Result<String, String>), | ||
Unary (fn(&str ) -> Result<String, String>), | ||
Binary (fn(&str, &str) -> Result<String, String>), | ||
} | ||
|
||
impl Function { | ||
fn argc(&self) -> usize { | ||
use self::Function::*; | ||
match *self { | ||
Nullary(_) => 0, | ||
Unary(_) => 1, | ||
Binary(_) => 2, | ||
} | ||
} | ||
} | ||
|
||
pub fn resolve_function<'a>(token: &Token<'a>, argc: usize) -> CompilationResult<'a, ()> { | ||
let name = token.lexeme; | ||
if let Some(function) = FUNCTIONS.get(&name) { | ||
use self::Function::*; | ||
match (function, argc) { | ||
(&Nullary(_), 0) => Ok(()), | ||
(&Unary(_), 1) => Ok(()), | ||
(&Binary(_), 2) => Ok(()), | ||
_ => { | ||
Err(token.error(CompilationErrorKind::FunctionArgumentCountMismatch{ | ||
function: name, found: argc, expected: function.argc(), | ||
})) | ||
} | ||
} | ||
} else { | ||
Ok(()) | ||
Err(token.error(CompilationErrorKind::UnknownFunction{function: token.lexeme})) | ||
} | ||
} | ||
|
||
pub fn evaluate_function<'a>(name: &'a str) -> RunResult<'a, String> { | ||
match name { | ||
"arch" => Ok(arch().to_string()), | ||
"os" => Ok(os().to_string()), | ||
"os_family" => Ok(os_family().to_string()), | ||
_ => Err(RuntimeError::Internal { | ||
pub fn evaluate_function<'a>(token: &Token<'a>, name: &'a str, arguments: &[String]) -> RunResult<'a, String> { | ||
if let Some(function) = FUNCTIONS.get(name) { | ||
use self::Function::*; | ||
let argc = arguments.len(); | ||
match (function, argc) { | ||
(&Nullary(f), 0) => f() | ||
.map_err(|message| RuntimeError::FunctionCall{token: token.clone(), message}), | ||
(&Unary(f), 1) => f(&arguments[0]) | ||
.map_err(|message| RuntimeError::FunctionCall{token: token.clone(), message}), | ||
(&Binary(f), 2) => f(&arguments[0], &arguments[1]) | ||
.map_err(|message| RuntimeError::FunctionCall{token: token.clone(), message}), | ||
_ => { | ||
Err(RuntimeError::Internal { | ||
message: format!("attempted to evaluate function `{}` with {} arguments", name, argc) | ||
}) | ||
} | ||
} | ||
} else { | ||
Err(RuntimeError::Internal { | ||
message: format!("attempted to evaluate unknown function: `{}`", name) | ||
}) | ||
} | ||
} | ||
|
||
pub fn arch() -> &'static str { | ||
target::arch() | ||
pub fn arch() -> Result<String, String> { | ||
Ok(target::arch().to_string()) | ||
} | ||
|
||
pub fn os() -> Result<String, String> { | ||
Ok(target::os().to_string()) | ||
} | ||
|
||
pub fn os() -> &'static str { | ||
target::os() | ||
pub fn os_family() -> Result<String, String> { | ||
Ok(target::os_family().to_string()) | ||
} | ||
|
||
pub fn os_family() -> &'static str { | ||
target::os_family() | ||
pub fn env_var<'a>(key: &str) -> Result<String, String> { | ||
use std::env::VarError::*; | ||
match env::var(key) { | ||
Err(NotPresent) => Err(format!("environment variable `{}` not present", key)), | ||
Err(NotUnicode(os_string)) => | ||
Err(format!("environment variable `{}` not unicode: {:?}", key, os_string)), | ||
Ok(value) => Ok(value), | ||
} | ||
} | ||
|
||
pub fn env_var_or_default<'a>(key: &str, default: &str) -> Result<String, String> { | ||
use std::env::VarError::*; | ||
match env::var(key) { | ||
Err(NotPresent) => Ok(default.to_string()), | ||
Err(NotUnicode(os_string)) => | ||
Err(format!("environment variable `{}` not unicode: {:?}", key, os_string)), | ||
Ok(value) => Ok(value), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.