mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-22 10:41:22 +02:00
Added simple documentation for the exposed parts.
This commit is contained in:
parent
0274721878
commit
46a27d4129
@ -2,13 +2,16 @@ use crate::lexer::TokenKind;
|
|||||||
use crate::parser::CalcError;
|
use crate::parser::CalcError;
|
||||||
use crate::parser::Unit;
|
use crate::parser::Unit;
|
||||||
|
|
||||||
|
/// A tree structure of a statement.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Stmt {
|
pub enum Stmt {
|
||||||
VarDecl(String, Box<Expr>),
|
VarDecl(String, Box<Expr>),
|
||||||
FnDecl(String, Vec<String>, Box<Expr>),
|
FnDecl(String, Vec<String>, Box<Expr>),
|
||||||
|
/// For simplicity, expressions can be put into statements. This is the form in which expressions are passed to the interpreter.
|
||||||
Expr(Box<Expr>),
|
Expr(Box<Expr>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A tree structure of an expression.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Binary(Box<Expr>, TokenKind, Box<Expr>),
|
Binary(Box<Expr>, TokenKind, Box<Expr>),
|
||||||
|
@ -6,6 +6,12 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use rug::Float;
|
use rug::Float;
|
||||||
|
|
||||||
|
/// Struct containing the current state of the parser. It stores user-defined functions and variables.
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// let parser_context = parser::Context::new();
|
||||||
|
/// assert_eq!(parser_context.eval("5*3").unwrap().unwrap(), 15);
|
||||||
|
/// ```
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
@ -36,12 +42,14 @@ impl Default for Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mathematical unit used in calculations.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Unit {
|
pub enum Unit {
|
||||||
Radians,
|
Radians,
|
||||||
Degrees,
|
Degrees,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Error that occured during parsing or evaluation.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum CalcError {
|
pub enum CalcError {
|
||||||
IncorrectAmountOfArguments(usize, String, usize),
|
IncorrectAmountOfArguments(usize, String, usize),
|
||||||
@ -54,6 +62,7 @@ pub enum CalcError {
|
|||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate expressions/declarations and return the answer.
|
||||||
pub fn eval(
|
pub fn eval(
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
input: &str,
|
input: &str,
|
||||||
@ -66,6 +75,7 @@ pub fn eval(
|
|||||||
interpreter.interpret(statements)
|
interpreter.interpret(statements)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse expressions/declarations and return a syntax tree.
|
||||||
pub fn parse(context: &mut Context, input: &str) -> Result<Vec<Stmt>, CalcError> {
|
pub fn parse(context: &mut Context, input: &str) -> Result<Vec<Stmt>, CalcError> {
|
||||||
context.tokens = Lexer::lex(input);
|
context.tokens = Lexer::lex(input);
|
||||||
context.pos = 0;
|
context.pos = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user