eonlib/
lib.rs

1use std::fmt::Display;
2
3use lalrpop_util::{lalrpop_mod, lexer::Token, ParseError};
4use mlua::prelude::*;
5
6mod ast;
7mod lua;
8mod luagen;
9mod preload;
10
11lalrpop_mod!(pub parser);
12
13pub fn run(input: &str) -> EonResult<()> {
14    let lua = Lua::new();
15    preload::register_eon_functions(&lua)?;
16
17    let eon_program = parser::ProgramParser::new().parse(input)?;
18    let lua_program: lua::Program = eon_program.into();
19    let lua_source = lua_program.to_string();
20
21    println!("{}", lua_source);
22    lua.load(&lua_source).exec()?;
23
24    Ok(())
25}
26
27pub type EonResult<T> = Result<T, Error>;
28
29#[derive(Debug, Clone)]
30pub enum Error {
31    LuaError(LuaError),
32    ParseError(String),
33    OtherError(String),
34}
35
36impl Display for Error {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Error::LuaError(e) => e.fmt(f),
40            Error::ParseError(e) => e.fmt(f),
41            Error::OtherError(e) => e.fmt(f),
42        }
43    }
44}
45
46impl From<LuaError> for Error {
47    fn from(e: LuaError) -> Self {
48        Error::LuaError(e)
49    }
50}
51
52impl<'token> From<ParseError<usize, Token<'token>, &'static str>> for Error {
53    fn from(e: ParseError<usize, Token<'token>, &'static str>) -> Self {
54        Error::ParseError(e.to_string())
55    }
56}
57
58impl From<&str> for Error {
59    fn from(e: &str) -> Self {
60        Error::OtherError(e.to_string())
61    }
62}