diff options
Diffstat (limited to '')
-rw-r--r-- | matrix-lang/src/ast.rs (renamed from matrix/src/ast.rs) | 100 |
1 files changed, 67 insertions, 33 deletions
diff --git a/matrix/src/ast.rs b/matrix-lang/src/ast.rs index de63630..5720f76 100644 --- a/matrix/src/ast.rs +++ b/matrix-lang/src/ast.rs @@ -1,42 +1,70 @@ -use std::{rc::Rc, ops::{Neg, Not}, fmt::Debug}; -use crate::{lex::{Position, TokenData}, value::{Value, InlineList, InlineMatrix, InlineTable}, Result}; +use std::{ops::{Neg, Not}, fmt::{Debug, Display}}; + +use crate::prelude::*; + +pub type AstName = (Rc<str>, Position); +pub type InlineList = Vec<Expr>; +pub type InlineMatrix = (usize, usize, Vec<Expr>); +pub type InlineTable = Vec<(Expr, Expr)>; #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum UnaryOp { // normal math - Negate, + Negate = 1, // equality - Not, + Not = 2, +} + +impl TryFrom<u8> for UnaryOp { + type Error = Exception; + + fn try_from(value: u8) -> std::prelude::v1::Result<Self, Self::Error> { + if value < 1 || value > 2 { + Err(exception!(BINARY_EXCEPTION, "cannot convert {value} to UnaryOp")) + } else { + unsafe { Ok(std::mem::transmute(value)) } + } + } } #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum BinaryOp { // normal math - Add, - Subtract, - Multiply, - Divide, - Modulo, - Power, + Add = 1, + Subtract = 2, + Multiply = 3, + Divide = 4, + Modulo = 5, + Power = 6, // binary math - BitwiseAnd, - BitwiseOr, - BitwiseXor, - BitwiseShiftLeft, - BitwiseShiftRight, + BitwiseAnd = 7, + BitwiseOr = 8, + BitwiseXor = 9, + BitwiseShiftLeft = 10, + BitwiseShiftRight = 11, // equality - Equals, - NotEquals, - GreaterEquals, - LessEquals, - GreaterThan, - LessThan, + Equals = 12, + NotEquals = 13, + GreaterEquals = 14, + LessEquals = 15, + GreaterThan = 16, + LessThan = 17, // iter - Range, - RangeEq + Range = 18, + RangeEq = 19 } -pub type AstName = (Rc<str>, Position); +impl TryFrom<u8> for BinaryOp { + type Error = Exception; + + fn try_from(value: u8) -> std::prelude::v1::Result<Self, Self::Error> { + if value < 1 || value > 19 { + Err(exception!(BINARY_EXCEPTION, "cannot convert {value} to BinaryOp")) + } else { + unsafe { Ok(std::mem::transmute(value)) } + } + } +} #[derive(Debug, Clone, PartialEq)] pub enum ExprData { @@ -75,6 +103,7 @@ pub enum ExprData { Try(Box<Expr>, AstName, Box<Expr>), Let(AstName, Box<Expr>), + Const(AstName, Box<Expr>), Pipeline(Box<Expr>, Box<Expr>), @@ -83,22 +112,24 @@ pub enum ExprData { Return(Box<Expr>), } -#[derive(Clone, PartialEq)] -pub struct Expr { - pub data: ExprData, - pub pos: Position +impl Display for Expr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{self:?}") + } } impl Debug for Expr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if f.alternate() { - write!(f, "{:#?}", self.data) - } else { - write!(f, "{:?}", self.data) - } + write!(f, "{:?}", self.data) } } +#[derive(Clone, PartialEq)] +pub struct Expr { + pub data: ExprData, + pub pos: Position +} + impl From<(ExprData, Position)> for Expr { fn from(value: (ExprData, Position)) -> Self { Self { data: value.0, pos: value.1 } @@ -377,6 +408,9 @@ pub fn optimize(mut expr: Expr) -> Result<Expr> { E::Let(ident, expr) => { E::Let(ident, Box::new(optimize(*expr)?)) }, + E::Const(ident, expr) => { + E::Const(ident, Box::new(optimize(*expr)?)) + }, E::Assign(lhs, rhs) => { let lhs = Box::new(optimize(*lhs)?); let rhs = Box::new(optimize(*rhs)?); |