mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 07:11:29 +02:00
Back to working state
This commit is contained in:
44
crates/nu-protocol/src/ast/block.rs
Normal file
44
crates/nu-protocol/src/ast/block.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
use super::Statement;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Block {
|
||||
pub stmts: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn len(&self) -> usize {
|
||||
self.stmts.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.stmts.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Block {
|
||||
type Output = Statement;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
&self.stmts[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl IndexMut<usize> for Block {
|
||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||
&mut self.stmts[index]
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Block {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn new() -> Self {
|
||||
Self { stmts: vec![] }
|
||||
}
|
||||
}
|
28
crates/nu-protocol/src/ast/call.rs
Normal file
28
crates/nu-protocol/src/ast/call.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use super::Expression;
|
||||
use crate::{DeclId, Span};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Call {
|
||||
/// identifier of the declaration to call
|
||||
pub decl_id: DeclId,
|
||||
pub head: Span,
|
||||
pub positional: Vec<Expression>,
|
||||
pub named: Vec<(String, Option<Expression>)>,
|
||||
}
|
||||
|
||||
impl Default for Call {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Call {
|
||||
pub fn new() -> Call {
|
||||
Self {
|
||||
decl_id: 0,
|
||||
head: Span::unknown(),
|
||||
positional: vec![],
|
||||
named: vec![],
|
||||
}
|
||||
}
|
||||
}
|
22
crates/nu-protocol/src/ast/expr.rs
Normal file
22
crates/nu-protocol/src/ast/expr.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use super::{Call, Expression, Operator};
|
||||
use crate::{BlockId, Signature, Span, VarId};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expr {
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Var(VarId),
|
||||
Call(Box<Call>),
|
||||
ExternalCall(Vec<u8>, Vec<Vec<u8>>),
|
||||
Operator(Operator),
|
||||
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
|
||||
Subexpression(BlockId),
|
||||
Block(BlockId),
|
||||
List(Vec<Expression>),
|
||||
Table(Vec<Expression>, Vec<Vec<Expression>>),
|
||||
Keyword(Vec<u8>, Span, Box<Expression>),
|
||||
String(String), // FIXME: improve this in the future?
|
||||
Signature(Box<Signature>),
|
||||
Garbage,
|
||||
}
|
86
crates/nu-protocol/src/ast/expression.rs
Normal file
86
crates/nu-protocol/src/ast/expression.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use super::{Expr, Operator};
|
||||
use crate::{BlockId, Signature, Span, Type, VarId};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Expression {
|
||||
pub expr: Expr,
|
||||
pub span: Span,
|
||||
pub ty: Type,
|
||||
}
|
||||
impl Expression {
|
||||
pub fn garbage(span: Span) -> Expression {
|
||||
Expression {
|
||||
expr: Expr::Garbage,
|
||||
span,
|
||||
ty: Type::Unknown,
|
||||
}
|
||||
}
|
||||
pub fn precedence(&self) -> usize {
|
||||
match &self.expr {
|
||||
Expr::Operator(operator) => {
|
||||
// Higher precedence binds tighter
|
||||
|
||||
match operator {
|
||||
Operator::Pow => 100,
|
||||
Operator::Multiply | Operator::Divide | Operator::Modulo => 95,
|
||||
Operator::Plus | Operator::Minus => 90,
|
||||
Operator::NotContains
|
||||
| Operator::Contains
|
||||
| Operator::LessThan
|
||||
| Operator::LessThanOrEqual
|
||||
| Operator::GreaterThan
|
||||
| Operator::GreaterThanOrEqual
|
||||
| Operator::Equal
|
||||
| Operator::NotEqual
|
||||
| Operator::In
|
||||
| Operator::NotIn => 80,
|
||||
Operator::And => 50,
|
||||
Operator::Or => 40, // TODO: should we have And and Or be different precedence?
|
||||
}
|
||||
}
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_block(&self) -> Option<BlockId> {
|
||||
match self.expr {
|
||||
Expr::Block(block_id) => Some(block_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_signature(&self) -> Option<Box<Signature>> {
|
||||
match &self.expr {
|
||||
Expr::Signature(sig) => Some(sig.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_list(&self) -> Option<Vec<Expression>> {
|
||||
match &self.expr {
|
||||
Expr::List(list) => Some(list.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_keyword(&self) -> Option<&Expression> {
|
||||
match &self.expr {
|
||||
Expr::Keyword(_, _, expr) => Some(expr),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_var(&self) -> Option<VarId> {
|
||||
match self.expr {
|
||||
Expr::Var(var_id) => Some(var_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_string(&self) -> Option<String> {
|
||||
match &self.expr {
|
||||
Expr::String(string) => Some(string.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
15
crates/nu-protocol/src/ast/mod.rs
Normal file
15
crates/nu-protocol/src/ast/mod.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
mod block;
|
||||
mod call;
|
||||
mod expr;
|
||||
mod expression;
|
||||
mod operator;
|
||||
mod pipeline;
|
||||
mod statement;
|
||||
|
||||
pub use block::*;
|
||||
pub use call::*;
|
||||
pub use expr::*;
|
||||
pub use expression::*;
|
||||
pub use operator::*;
|
||||
pub use pipeline::*;
|
||||
pub use statement::*;
|
48
crates/nu-protocol/src/ast/operator.rs
Normal file
48
crates/nu-protocol/src/ast/operator.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Operator {
|
||||
Equal,
|
||||
NotEqual,
|
||||
LessThan,
|
||||
GreaterThan,
|
||||
LessThanOrEqual,
|
||||
GreaterThanOrEqual,
|
||||
Contains,
|
||||
NotContains,
|
||||
Plus,
|
||||
Minus,
|
||||
Multiply,
|
||||
Divide,
|
||||
In,
|
||||
NotIn,
|
||||
Modulo,
|
||||
And,
|
||||
Or,
|
||||
Pow,
|
||||
}
|
||||
|
||||
impl Display for Operator {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Operator::Equal => write!(f, "=="),
|
||||
Operator::NotEqual => write!(f, "!="),
|
||||
Operator::LessThan => write!(f, "<"),
|
||||
Operator::GreaterThan => write!(f, ">"),
|
||||
Operator::Contains => write!(f, "=~"),
|
||||
Operator::NotContains => write!(f, "!~"),
|
||||
Operator::Plus => write!(f, "+"),
|
||||
Operator::Minus => write!(f, "-"),
|
||||
Operator::Multiply => write!(f, "*"),
|
||||
Operator::Divide => write!(f, "/"),
|
||||
Operator::In => write!(f, "in"),
|
||||
Operator::NotIn => write!(f, "not-in"),
|
||||
Operator::Modulo => write!(f, "mod"),
|
||||
Operator::And => write!(f, "&&"),
|
||||
Operator::Or => write!(f, "||"),
|
||||
Operator::Pow => write!(f, "**"),
|
||||
Operator::LessThanOrEqual => write!(f, "<="),
|
||||
Operator::GreaterThanOrEqual => write!(f, ">="),
|
||||
}
|
||||
}
|
||||
}
|
20
crates/nu-protocol/src/ast/pipeline.rs
Normal file
20
crates/nu-protocol/src/ast/pipeline.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::ast::Expression;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Pipeline {
|
||||
pub expressions: Vec<Expression>,
|
||||
}
|
||||
|
||||
impl Default for Pipeline {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
expressions: vec![],
|
||||
}
|
||||
}
|
||||
}
|
9
crates/nu-protocol/src/ast/statement.rs
Normal file
9
crates/nu-protocol/src/ast/statement.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use super::{Expression, Pipeline};
|
||||
use crate::DeclId;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Statement {
|
||||
Declaration(DeclId),
|
||||
Pipeline(Pipeline),
|
||||
Expression(Expression),
|
||||
}
|
Reference in New Issue
Block a user