Skip to main content

riddle/
lib.rs

1use crate::{
2    language::{ClassDef, ConstructorDef, Expr, FunctionDef, PredicateDef, ProblemDef, Statement},
3    lexer::Lexer,
4    parser::Parser,
5};
6use ::core::fmt;
7
8pub mod core;
9pub mod env;
10pub mod language;
11mod lexer;
12mod parser;
13pub mod scope;
14
15#[derive(Debug)]
16pub enum RiddleError {
17    NotAnEnvironment(String),
18    NotAClass(String),
19    NotAPredicate(String),
20    TypeError(String),
21    NotFound(String),
22    InconsistencyError(String),
23    RuntimeError(String),
24}
25
26impl fmt::Display for RiddleError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            RiddleError::NotAnEnvironment(name) => write!(f, "Variable '{}' is not an environment", name),
30            RiddleError::NotAClass(name) => write!(f, "Type '{}' is not a class", name),
31            RiddleError::NotAPredicate(name) => write!(f, "Predicate '{}' not found", name),
32            RiddleError::TypeError(msg) => write!(f, "Type error: {}", msg),
33            RiddleError::NotFound(name) => write!(f, "'{}' not found", name),
34            RiddleError::InconsistencyError(msg) => write!(f, "Inconsistency error: {}", msg),
35            RiddleError::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
36        }
37    }
38}
39
40pub fn parse_problem(input: &str) -> Result<ProblemDef, RiddleError> {
41    Parser::new(Lexer::new(input)).parse_problem()
42}
43
44pub fn parse_class(input: &str) -> Result<ClassDef, RiddleError> {
45    Parser::new(Lexer::new(input)).parse_class()
46}
47
48pub fn parse_constructor(input: &str) -> Result<ConstructorDef, RiddleError> {
49    Parser::new(Lexer::new(input)).parse_constructor()
50}
51
52pub fn parse_function(input: &str) -> Result<FunctionDef, RiddleError> {
53    Parser::new(Lexer::new(input)).parse_function()
54}
55
56pub fn parse_predicate(input: &str) -> Result<PredicateDef, RiddleError> {
57    Parser::new(Lexer::new(input)).parse_predicate()
58}
59
60pub fn parse_statement(input: &str) -> Result<Statement, RiddleError> {
61    Parser::new(Lexer::new(input)).parse_statement()
62}
63
64pub fn parse_expression(input: &str) -> Result<Expr, RiddleError> {
65    Parser::new(Lexer::new(input)).parse_expression()
66}