Enum Ast

Source
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)

Tuple Fields

§0: BTreeMap<i64, Ast>

Map of the block

§

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))),
};

Fields

§op: String

Operator

§left: Box<Ast>

Left expression

§right: Box<Ast>

Right expression

§

Set

Set expression (id = expr)

Fields

§id: Box<Ast>

Identifier to set value to

§expr: Box<Ast>

Expression of value to set

§

FunctionDefinition

Define function

Fields

§id: String

Function name

§params: Vec<(String, String)>

Function parameter (Params name, Params type)

§body: Box<Ast>

Code of the function (Ast::Block)

§

FunctionCall

Function call

Fields

§id: String

Name of function to call

§args: Vec<Ast>

Argument given

§

Array(Vec<Ast>)

Array (is a Vector in Array clothing)

Tuple Fields

§0: Vec<Ast>

Vector to set

§

ArrayCall

Accessing array field

Fields

§id: String

Ast::Identifier of the array name

§k: Box<Ast>

Where to access (wrapped in Box)

§

Bool(bool)

Boolean data type

Tuple Fields

§0: bool

Bool to create

§

If

If structure

Fields

§condition: Box<Ast>

Condition for the block to be run

§block: Box<Ast>

The code block that will run if condition evaluated to true

Implementations§

Source§

impl Ast

Source

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§

Source§

impl Clone for Ast

Source§

fn clone(&self) -> Ast

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Ast

Source§

fn eq(&self, other: &Ast) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Ast

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.