2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2017-12-02 05:37:10 -08:00
|
|
|
use target;
|
|
|
|
|
2019-04-11 15:23:14 -07:00
|
|
|
use crate::platform::{Platform, PlatformInterface};
|
2018-06-19 10:04:03 -07:00
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
lazy_static! {
|
2019-04-11 15:23:14 -07:00
|
|
|
static ref FUNCTIONS: BTreeMap<&'static str, Function> = vec![
|
2018-12-08 14:29:41 -08:00
|
|
|
("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)),
|
|
|
|
(
|
|
|
|
"invocation_directory",
|
|
|
|
Function::Nullary(invocation_directory)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Function {
|
2018-12-08 14:29:41 -08:00
|
|
|
Nullary(fn(&FunctionContext) -> Result<String, String>),
|
|
|
|
Unary(fn(&FunctionContext, &str) -> Result<String, String>),
|
|
|
|
Binary(fn(&FunctionContext, &str, &str) -> Result<String, String>),
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
fn argc(&self) -> usize {
|
|
|
|
use self::Function::*;
|
|
|
|
match *self {
|
|
|
|
Nullary(_) => 0,
|
2018-12-08 14:29:41 -08:00
|
|
|
Unary(_) => 1,
|
|
|
|
Binary(_) => 2,
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub struct FunctionContext<'a> {
|
2018-06-19 10:04:03 -07:00
|
|
|
pub invocation_directory: &'a Result<PathBuf, String>,
|
2019-04-11 15:23:14 -07:00
|
|
|
pub dotenv: &'a BTreeMap<String, String>,
|
2018-03-17 09:17:41 -07:00
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
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) {
|
2018-12-08 14:29:41 -08:00
|
|
|
(&Nullary(_), 0) | (&Unary(_), 1) | (&Binary(_), 2) => Ok(()),
|
|
|
|
_ => Err(
|
|
|
|
token.error(CompilationErrorKind::FunctionArgumentCountMismatch {
|
|
|
|
function: name,
|
|
|
|
found: argc,
|
|
|
|
expected: function.argc(),
|
|
|
|
}),
|
|
|
|
),
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
} else {
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(token.error(CompilationErrorKind::UnknownFunction {
|
|
|
|
function: token.lexeme,
|
|
|
|
}))
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 12:44:45 -08:00
|
|
|
pub fn evaluate_function<'a>(
|
2018-12-08 14:29:41 -08:00
|
|
|
token: &Token<'a>,
|
|
|
|
name: &'a str,
|
|
|
|
context: &FunctionContext,
|
|
|
|
arguments: &[String],
|
2017-12-11 12:44:45 -08:00
|
|
|
) -> RunResult<'a, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
if let Some(function) = FUNCTIONS.get(name) {
|
|
|
|
use self::Function::*;
|
|
|
|
let argc = arguments.len();
|
|
|
|
match (function, argc) {
|
2018-12-08 14:29:41 -08:00
|
|
|
(&Nullary(f), 0) => f(context).map_err(|message| RuntimeError::FunctionCall {
|
|
|
|
token: token.clone(),
|
|
|
|
message,
|
|
|
|
}),
|
|
|
|
(&Unary(f), 1) => f(context, &arguments[0]).map_err(|message| RuntimeError::FunctionCall {
|
|
|
|
token: token.clone(),
|
|
|
|
message,
|
|
|
|
}),
|
|
|
|
(&Binary(f), 2) => {
|
|
|
|
f(context, &arguments[0], &arguments[1]).map_err(|message| RuntimeError::FunctionCall {
|
|
|
|
token: token.clone(),
|
|
|
|
message,
|
2017-12-02 14:59:07 -08:00
|
|
|
})
|
|
|
|
}
|
2018-12-08 14:29:41 -08:00
|
|
|
_ => Err(RuntimeError::Internal {
|
|
|
|
message: format!(
|
|
|
|
"attempted to evaluate function `{}` with {} arguments",
|
|
|
|
name, argc
|
|
|
|
),
|
|
|
|
}),
|
2017-12-02 14:59:07 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(RuntimeError::Internal {
|
2018-12-08 14:29:41 -08:00
|
|
|
message: format!("attempted to evaluate unknown function: `{}`", name),
|
2017-12-02 05:37:10 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub fn arch(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::arch().to_string())
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub fn os(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::os().to_string())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub fn os_family(_context: &FunctionContext) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(target::os_family().to_string())
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|
|
|
|
|
2018-06-19 10:04:03 -07:00
|
|
|
pub fn invocation_directory(context: &FunctionContext) -> Result<String, String> {
|
2018-12-08 14:29:41 -08:00
|
|
|
context.invocation_directory.clone().and_then(|s| {
|
|
|
|
Platform::to_shell_path(&s).map_err(|e| format!("Error getting shell path: {}", e))
|
|
|
|
})
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub fn env_var(context: &FunctionContext, key: &str) -> Result<String, String> {
|
2017-12-02 14:59:07 -08:00
|
|
|
use std::env::VarError::*;
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
match env::var(key) {
|
|
|
|
Err(NotPresent) => Err(format!("environment variable `{}` not present", key)),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:17:41 -07:00
|
|
|
pub fn env_var_or_default(
|
|
|
|
context: &FunctionContext,
|
2018-12-08 14:29:41 -08:00
|
|
|
key: &str,
|
|
|
|
default: &str,
|
2018-03-17 09:17:41 -07:00
|
|
|
) -> Result<String, String> {
|
|
|
|
if let Some(value) = context.dotenv.get(key) {
|
|
|
|
return Ok(value.clone());
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:59:07 -08:00
|
|
|
use std::env::VarError::*;
|
|
|
|
match env::var(key) {
|
|
|
|
Err(NotPresent) => Ok(default.to_string()),
|
2018-12-08 14:29:41 -08:00
|
|
|
Err(NotUnicode(os_string)) => Err(format!(
|
|
|
|
"environment variable `{}` not unicode: {:?}",
|
|
|
|
key, os_string
|
|
|
|
)),
|
2017-12-02 14:59:07 -08:00
|
|
|
Ok(value) => Ok(value),
|
|
|
|
}
|
2017-12-02 05:37:10 -08:00
|
|
|
}
|