Skip to content

Commit

Permalink
Add 'append' function and parse [x] as a vector rather than a group
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Apr 20, 2024
1 parent 1ff6813 commit c94c6da
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion kalk/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ fn analyse_expr(context: &mut Context, expr: Expr) -> Result<Expr, KalkError> {
analysed_values.push(analyse_expr(context, value)?);
}

Expr::Vector(analysed_values)
if analysed_values.len() == 1 && matches!(analysed_values[0], Expr::Comprehension(_, _, _)) {
analysed_values.pop().unwrap()
} else {
Expr::Vector(analysed_values)
}
}
Expr::Matrix(rows) => {
let mut analysed_rows = Vec::new();
Expand All @@ -259,6 +263,7 @@ fn analyse_expr(context: &mut Context, expr: Expr) -> Result<Expr, KalkError> {
}
Expr::Comprehension(left, right, vars) => Expr::Comprehension(left, right, vars),
Expr::Equation(left, right, identifier) => Expr::Equation(left, right, identifier),
Expr::Preevaluated(_) => expr,
})
}

Expand Down
3 changes: 2 additions & 1 deletion kalk/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kalk_value::KalkFloat, lexer::TokenKind};
use crate::{kalk_value::{KalkFloat, KalkValue}, lexer::TokenKind};

/// A tree structure of a statement.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -27,6 +27,7 @@ pub enum Expr {
Indexer(Box<Expr>, Vec<Expr>),
Comprehension(Box<Expr>, Vec<Expr>, Vec<RangedVar>),
Equation(Box<Expr>, Box<Expr>, Identifier),
Preevaluated(KalkValue),
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
3 changes: 2 additions & 1 deletion kalk/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub(crate) fn eval_expr(
context, left, conditions, vars,
)?)),
Expr::Equation(left, right, identifier) => eval_equation(context, left, right, identifier),
Expr::Preevaluated(value) => Ok(value.clone()),
}
}

Expand Down Expand Up @@ -548,7 +549,7 @@ pub(crate) fn eval_fn_call_expr(
};
let var_decl = Stmt::VarDecl(
argument_identifier,
Box::new(crate::ast::build_literal_ast(&eval_expr(
Box::new(Expr::Preevaluated(eval_expr(
context,
&expressions[i],
None,
Expand Down
2 changes: 2 additions & 0 deletions kalk/src/inverter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn invert(
Err(KalkError::UnableToInvert(String::from("Comprehension")))
}
Expr::Equation(_, _, _) => Err(KalkError::UnableToInvert(String::from("Equation"))),
Expr::Preevaluated(_) => Err(KalkError::UnableToInvert(String::from("Equation"))),
}
}

Expand Down Expand Up @@ -402,6 +403,7 @@ pub fn contains_var(symbol_table: &SymbolTable, expr: &Expr, var_name: &str) ->
Expr::Indexer(_, _) => false,
Expr::Comprehension(_, _, _) => false,
Expr::Equation(_, _, _) => false,
Expr::Preevaluated(_) => false,
}
}

Expand Down
7 changes: 6 additions & 1 deletion kalk/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ fn parse_group_fn(context: &mut Context) -> Result<Expr, KalkError> {

fn parse_vector(context: &mut Context) -> Result<Expr, KalkError> {
let kind = advance(context).kind;
if match_token(context, TokenKind::ClosedBracket) {
advance(context);

return Ok(Expr::Vector(vec![]));
}

if kind == TokenKind::OpenBracket {
skip_newlines(context);
Expand Down Expand Up @@ -617,7 +622,7 @@ fn parse_vector(context: &mut Context) -> Result<Expr, KalkError> {

if rows.len() == 1 {
let mut values = rows.pop().unwrap();
if values.len() == 1 {
if values.len() == 1 && kind == TokenKind::OpenParenthesis {
Ok(Expr::Group(Box::new(values.pop().unwrap())))
} else {
Ok(Expr::Vector(values))
Expand Down
12 changes: 12 additions & 0 deletions kalk/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ lazy_static! {
};
pub static ref BINARY_FUNCS: HashMap<&'static str, (BinaryFuncInfo, &'static str)> = {
let mut m = HashMap::new();
m.insert("append", (BinaryFuncInfo(append, Other), ""));
m.insert("bitand", (BinaryFuncInfo(bitand, Other), ""));
m.insert("bitor", (BinaryFuncInfo(bitor, Other), ""));
m.insert("bitxor", (BinaryFuncInfo(bitxor, Other), ""));
Expand Down Expand Up @@ -700,6 +701,17 @@ pub mod funcs {
))
}

pub fn append(x: KalkValue, y: KalkValue) -> Result<KalkValue, KalkError> {
if let KalkValue::Vector(items) = x {
let mut new_items = items.clone();
new_items.push(y);

Ok(KalkValue::Vector(new_items))
} else {
Err(KalkError::Expected(String::from("Vector")))
}
}

// ⎛ ⎞
// ⎜ ⎜a⎜ ⎟
// lcm(a, b) = ⎜ ───────── ⎟ × ⎜b⎜
Expand Down

0 comments on commit c94c6da

Please sign in to comment.