Added simple documentation for the exposed parts.

This commit is contained in:
PaddiM8 2020-06-11 23:09:44 +02:00
parent 0274721878
commit 46a27d4129
2 changed files with 13 additions and 0 deletions

View File

@ -2,13 +2,16 @@ use crate::lexer::TokenKind;
use crate::parser::CalcError;
use crate::parser::Unit;
/// A tree structure of a statement.
#[derive(Debug, Clone, PartialEq)]
pub enum Stmt {
VarDecl(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>),
}
/// A tree structure of an expression.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Binary(Box<Expr>, TokenKind, Box<Expr>),

View File

@ -6,6 +6,12 @@ use crate::{
};
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 {
tokens: Vec<Token>,
pos: usize,
@ -36,12 +42,14 @@ impl Default for Context {
}
}
/// Mathematical unit used in calculations.
#[derive(Debug, Clone, PartialEq)]
pub enum Unit {
Radians,
Degrees,
}
/// Error that occured during parsing or evaluation.
#[derive(Debug, Clone, PartialEq)]
pub enum CalcError {
IncorrectAmountOfArguments(usize, String, usize),
@ -54,6 +62,7 @@ pub enum CalcError {
Unknown,
}
/// Evaluate expressions/declarations and return the answer.
pub fn eval(
context: &mut Context,
input: &str,
@ -66,6 +75,7 @@ pub fn eval(
interpreter.interpret(statements)
}
/// Parse expressions/declarations and return a syntax tree.
pub fn parse(context: &mut Context, input: &str) -> Result<Vec<Stmt>, CalcError> {
context.tokens = Lexer::lex(input);
context.pos = 0;