mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
env vars
This commit is contained in:
parent
d92e661253
commit
d2577acccd
2 changed files with 99 additions and 23 deletions
112
src/eval.rs
112
src/eval.rs
|
@ -18,9 +18,30 @@ pub enum Value {
|
||||||
Int { val: i64, span: Span },
|
Int { val: i64, span: Span },
|
||||||
Float { val: f64, span: Span },
|
Float { val: f64, span: Span },
|
||||||
String { val: String, span: Span },
|
String { val: String, span: Span },
|
||||||
List(Vec<Value>),
|
List { val: Vec<Value>, span: Span },
|
||||||
Block(BlockId),
|
Block { val: BlockId, span: Span },
|
||||||
Unknown,
|
Nothing { span: Span },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
pub fn as_string(&self) -> Result<String, ShellError> {
|
||||||
|
match self {
|
||||||
|
Value::String { val, .. } => Ok(val.to_string()),
|
||||||
|
_ => Err(ShellError::Mismatch("string".into(), self.span())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn span(&self) -> Span {
|
||||||
|
match self {
|
||||||
|
Value::Bool { span, .. } => *span,
|
||||||
|
Value::Int { span, .. } => *span,
|
||||||
|
Value::Float { span, .. } => *span,
|
||||||
|
Value::String { span, .. } => *span,
|
||||||
|
Value::List { span, .. } => *span,
|
||||||
|
Value::Block { span, .. } => *span,
|
||||||
|
Value::Nothing { span, .. } => *span,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Value {
|
impl PartialEq for Value {
|
||||||
|
@ -30,8 +51,8 @@ impl PartialEq for Value {
|
||||||
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
|
||||||
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
|
||||||
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
||||||
(Value::List(l1), Value::List(l2)) => l1 == l2,
|
(Value::List { val: l1, .. }, Value::List { val: l2, .. }) => l1 == l2,
|
||||||
(Value::Block(b1), Value::Block(b2)) => b1 == b2,
|
(Value::Block { val: b1, .. }, Value::Block { val: b2, .. }) => b1 == b2,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,9 +71,9 @@ impl Display for Value {
|
||||||
write!(f, "{}", val)
|
write!(f, "{}", val)
|
||||||
}
|
}
|
||||||
Value::String { val, .. } => write!(f, "{}", val),
|
Value::String { val, .. } => write!(f, "{}", val),
|
||||||
Value::List(..) => write!(f, "<list>"),
|
Value::List { .. } => write!(f, "<list>"),
|
||||||
Value::Block(..) => write!(f, "<block>"),
|
Value::Block { .. } => write!(f, "<block>"),
|
||||||
Value::Unknown => write!(f, "<unknown>"),
|
Value::Nothing { .. } => write!(f, ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +102,7 @@ impl Value {
|
||||||
span: Span::unknown(),
|
span: Span::unknown(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
_ => Ok(Value::Unknown),
|
_ => Err(ShellError::Mismatch("addition".into(), self.span())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +113,7 @@ pub struct State<'a> {
|
||||||
|
|
||||||
pub struct StackFrame {
|
pub struct StackFrame {
|
||||||
pub vars: HashMap<VarId, Value>,
|
pub vars: HashMap<VarId, Value>,
|
||||||
|
pub env_vars: HashMap<String, String>,
|
||||||
pub parent: Option<Stack>,
|
pub parent: Option<Stack>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +130,7 @@ impl Stack {
|
||||||
pub fn new() -> Stack {
|
pub fn new() -> Stack {
|
||||||
Stack(Rc::new(RefCell::new(StackFrame {
|
Stack(Rc::new(RefCell::new(StackFrame {
|
||||||
vars: HashMap::new(),
|
vars: HashMap::new(),
|
||||||
|
env_vars: HashMap::new(),
|
||||||
parent: None,
|
parent: None,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
@ -130,17 +153,28 @@ impl Stack {
|
||||||
this.vars.insert(var_id, value);
|
this.vars.insert(var_id, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_env_var(&self, var: String, value: String) {
|
||||||
|
let mut this = self.0.borrow_mut();
|
||||||
|
this.env_vars.insert(var, value);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn enter_scope(self) -> Stack {
|
pub fn enter_scope(self) -> Stack {
|
||||||
Stack(Rc::new(RefCell::new(StackFrame {
|
Stack(Rc::new(RefCell::new(StackFrame {
|
||||||
vars: HashMap::new(),
|
vars: HashMap::new(),
|
||||||
|
env_vars: HashMap::new(),
|
||||||
parent: Some(self),
|
parent: Some(self),
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_stack(&self) {
|
pub fn print_stack(&self) {
|
||||||
println!("===frame===");
|
println!("===frame===");
|
||||||
|
println!("vars:");
|
||||||
for (var, val) in &self.0.borrow().vars {
|
for (var, val) in &self.0.borrow().vars {
|
||||||
println!("{}: {:?}", var, val);
|
println!(" {}: {:?}", var, val);
|
||||||
|
}
|
||||||
|
println!("env vars:");
|
||||||
|
for (var, val) in &self.0.borrow().env_vars {
|
||||||
|
println!(" {}: {:?}", var, val);
|
||||||
}
|
}
|
||||||
if let Some(parent) = &self.0.borrow().parent {
|
if let Some(parent) = &self.0.borrow().parent {
|
||||||
parent.print_stack()
|
parent.print_stack()
|
||||||
|
@ -194,7 +228,27 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellErr
|
||||||
//println!("Adding: {:?} to {}", rhs, var_id);
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||||
|
|
||||||
stack.add_var(var_id, rhs);
|
stack.add_var(var_id, rhs);
|
||||||
Ok(Value::Unknown)
|
Ok(Value::Nothing {
|
||||||
|
span: call.positional[0].span,
|
||||||
|
})
|
||||||
|
} else if decl.signature.name == "let-env" {
|
||||||
|
let env_var = call.positional[0]
|
||||||
|
.as_string()
|
||||||
|
.expect("internal error: missing variable");
|
||||||
|
|
||||||
|
let keyword_expr = call.positional[1]
|
||||||
|
.as_keyword()
|
||||||
|
.expect("internal error: missing keyword");
|
||||||
|
|
||||||
|
let rhs = eval_expression(state, stack.clone(), keyword_expr)?;
|
||||||
|
let rhs = rhs.as_string()?;
|
||||||
|
|
||||||
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||||
|
|
||||||
|
stack.add_env_var(env_var, rhs);
|
||||||
|
Ok(Value::Nothing {
|
||||||
|
span: call.positional[0].span,
|
||||||
|
})
|
||||||
} else if decl.signature.name == "if" {
|
} else if decl.signature.name == "if" {
|
||||||
let cond = &call.positional[0];
|
let cond = &call.positional[0];
|
||||||
let then_block = call.positional[1]
|
let then_block = call.positional[1]
|
||||||
|
@ -204,7 +258,7 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellErr
|
||||||
|
|
||||||
let result = eval_expression(state, stack.clone(), cond)?;
|
let result = eval_expression(state, stack.clone(), cond)?;
|
||||||
match result {
|
match result {
|
||||||
Value::Bool { val, .. } => {
|
Value::Bool { val, span } => {
|
||||||
if val {
|
if val {
|
||||||
let block = state.parser_state.get_block(then_block);
|
let block = state.parser_state.get_block(then_block);
|
||||||
let stack = stack.enter_scope();
|
let stack = stack.enter_scope();
|
||||||
|
@ -222,7 +276,7 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellErr
|
||||||
eval_expression(state, stack, else_case)
|
eval_expression(state, stack, else_case)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Unknown)
|
Ok(Value::Nothing { span })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::Mismatch("bool".into(), Span::unknown())),
|
_ => Err(ShellError::Mismatch("bool".into(), Span::unknown())),
|
||||||
|
@ -250,7 +304,9 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellErr
|
||||||
eval_block(state, stack, block)?;
|
eval_block(state, stack, block)?;
|
||||||
let end_time = Instant::now();
|
let end_time = Instant::now();
|
||||||
println!("{} ms", (end_time - start_time).as_millis());
|
println!("{} ms", (end_time - start_time).as_millis());
|
||||||
Ok(Value::Unknown)
|
Ok(Value::Nothing {
|
||||||
|
span: call.positional[0].span,
|
||||||
|
})
|
||||||
} else if decl.signature.name == "for" {
|
} else if decl.signature.name == "for" {
|
||||||
let var_id = call.positional[0]
|
let var_id = call.positional[0]
|
||||||
.as_var()
|
.as_var()
|
||||||
|
@ -284,9 +340,11 @@ fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellErr
|
||||||
*val += 1
|
*val += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Value::Unknown)
|
Ok(Value::Nothing {
|
||||||
|
span: call.positional[0].span,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Unknown)
|
Err(ShellError::Unsupported(call.head))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,7 +371,7 @@ pub fn eval_expression(
|
||||||
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
||||||
Expr::Call(call) => eval_call(state, stack, call),
|
Expr::Call(call) => eval_call(state, stack, call),
|
||||||
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
||||||
Expr::Operator(_) => Ok(Value::Unknown),
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
||||||
Expr::BinaryOp(lhs, op, rhs) => {
|
Expr::BinaryOp(lhs, op, rhs) => {
|
||||||
let lhs = eval_expression(state, stack.clone(), lhs)?;
|
let lhs = eval_expression(state, stack.clone(), lhs)?;
|
||||||
let op = eval_operator(state, stack.clone(), op)?;
|
let op = eval_operator(state, stack.clone(), op)?;
|
||||||
|
@ -321,7 +379,7 @@ pub fn eval_expression(
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Operator::Plus => lhs.add(&rhs),
|
Operator::Plus => lhs.add(&rhs),
|
||||||
_ => Ok(Value::Unknown),
|
_ => Ok(Value::Nothing { span: expr.span }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,13 +389,19 @@ pub fn eval_expression(
|
||||||
let stack = stack.enter_scope();
|
let stack = stack.enter_scope();
|
||||||
eval_block(state, stack, block)
|
eval_block(state, stack, block)
|
||||||
}
|
}
|
||||||
Expr::Block(block_id) => Ok(Value::Block(*block_id)),
|
Expr::Block(block_id) => Ok(Value::Block {
|
||||||
|
val: *block_id,
|
||||||
|
span: expr.span,
|
||||||
|
}),
|
||||||
Expr::List(x) => {
|
Expr::List(x) => {
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
for expr in x {
|
for expr in x {
|
||||||
output.push(eval_expression(state, stack.clone(), expr)?);
|
output.push(eval_expression(state, stack.clone(), expr)?);
|
||||||
}
|
}
|
||||||
Ok(Value::List(output))
|
Ok(Value::List {
|
||||||
|
val: output,
|
||||||
|
span: expr.span,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Expr::Table(_, _) => Err(ShellError::Unsupported(expr.span)),
|
Expr::Table(_, _) => Err(ShellError::Unsupported(expr.span)),
|
||||||
Expr::Keyword(_, _, expr) => eval_expression(state, stack, expr),
|
Expr::Keyword(_, _, expr) => eval_expression(state, stack, expr),
|
||||||
|
@ -345,13 +409,15 @@ pub fn eval_expression(
|
||||||
val: s.clone(),
|
val: s.clone(),
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
}),
|
}),
|
||||||
Expr::Signature(_) => Ok(Value::Unknown),
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
||||||
Expr::Garbage => Ok(Value::Unknown),
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eval_block(state: &State, stack: Stack, block: &Block) -> Result<Value, ShellError> {
|
pub fn eval_block(state: &State, stack: Stack, block: &Block) -> Result<Value, ShellError> {
|
||||||
let mut last = Ok(Value::Unknown);
|
let mut last = Ok(Value::Nothing {
|
||||||
|
span: Span { start: 0, end: 0 },
|
||||||
|
});
|
||||||
|
|
||||||
for stmt in &block.stmts {
|
for stmt in &block.stmts {
|
||||||
if let Statement::Expression(expression) = stmt {
|
if let Statement::Expression(expression) = stmt {
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -33,6 +33,15 @@ fn main() -> std::io::Result<()> {
|
||||||
);
|
);
|
||||||
working_set.add_decl(sig.into());
|
working_set.add_decl(sig.into());
|
||||||
|
|
||||||
|
let sig = Signature::build("let-env")
|
||||||
|
.required("var_name", SyntaxShape::String, "variable name")
|
||||||
|
.required(
|
||||||
|
"initial_value",
|
||||||
|
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::String)),
|
||||||
|
"equals sign followed by value",
|
||||||
|
);
|
||||||
|
working_set.add_decl(sig.into());
|
||||||
|
|
||||||
let sig = Signature::build("alias")
|
let sig = Signature::build("alias")
|
||||||
.required("var_name", SyntaxShape::Variable, "variable name")
|
.required("var_name", SyntaxShape::Variable, "variable name")
|
||||||
.required(
|
.required(
|
||||||
|
@ -179,6 +188,7 @@ fn main() -> std::io::Result<()> {
|
||||||
continue;
|
continue;
|
||||||
} else if s.trim() == "stack" {
|
} else if s.trim() == "stack" {
|
||||||
stack.print_stack();
|
stack.print_stack();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
// println!("input: '{}'", s);
|
// println!("input: '{}'", s);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue