pub enum Ast {
Block(BTreeMap<i64, Ast>),
String(String),
Int(Decimal),
Identifier(String),
BinaryOp {
op: String,
left: Box<Ast>,
right: Box<Ast>,
},
Set {
id: Box<Ast>,
expr: Box<Ast>,
},
FunctionDefinition {
id: String,
params: Vec<(String, String)>,
body: Box<Ast>,
},
FunctionCall {
id: String,
args: Vec<Ast>,
},
Array(Vec<Ast>),
ArrayCall {
id: String,
k: Box<Ast>,
},
Bool(bool),
If {
condition: Box<Ast>,
block: Box<Ast>,
},
}
Expand description
Abstract syntax tree type Should have tree structure but I’m stupid so not now
Variants§
Block(BTreeMap<i64, Ast>)
Code block (This is also used on the top domain / parent tree)
String(String)
String data type
Int(Decimal)
Decimal data type (a bit of misleading name)
Identifier(String)
All identifiers (a-Z). Used in function name, variable name, etc.
BinaryOp
Binary operation (Can be nested)
§Example
let a: Ast = Ast::BinaryOp {
op: "+".to_string(),
left: Box::new(Ast::Int(dec!(0.1))),
right: Box::new(Ast::Int(dec!(0.2))),
};
Set
Set expression (id = expr)
FunctionDefinition
Define function
Fields
§
body: Box<Ast>
Code of the function (Ast::Block
)
FunctionCall
Function call
Array(Vec<Ast>)
Array (is a Vector in Array clothing)
ArrayCall
Accessing array field
Bool(bool)
Boolean data type
If
If structure
Implementations§
Source§impl Ast
impl Ast
Sourcepub fn parse_code(block: &str) -> Result<Ast>
pub fn parse_code(block: &str) -> Result<Ast>
Parse code from string
Return Err
if can’t parse, else Return Result
<Ast
>
§Example
§Parsing identifier
assert_eq!(Ast::parse_code("x").unwrap(), Ast::Block(BTreeMap::from([(1, Ast::Identifier("x".to_string()))])));
§Parsing set
assert_eq!(Ast::parse_code("x = 0;").unwrap(), Ast::Block(BTreeMap::from([(1, Ast::Set {id: Box::new(Ast::Identifier("x".to_string())), expr: Box::new(Ast::Int(dec!(0)))})])));
assert_eq!(Ast::parse_code("x").unwrap(), Ast::Block(BTreeMap::from([(1, Ast::Identifier("x".to_string()))])));
Trait Implementations§
impl Eq for Ast
impl StructuralPartialEq for Ast
Auto Trait Implementations§
impl Freeze for Ast
impl RefUnwindSafe for Ast
impl Send for Ast
impl Sync for Ast
impl Unpin for Ast
impl UnwindSafe for Ast
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more