1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::fmt::Display;

use lalrpop_util::{lalrpop_mod, lexer::Token, ParseError};
use mlua::prelude::*;

mod ast;
mod lua;
mod luagen;
mod preload;

lalrpop_mod!(pub parser);

pub fn run(input: &str) -> EonResult<()> {
    let lua = Lua::new();
    preload::register_eon_functions(&lua)?;

    let eon_program = parser::ProgramParser::new().parse(input)?;
    let lua_program: lua::Program = eon_program.into();
    let lua_source = lua_program.to_string();

    println!("{}", lua_source);
    lua.load(&lua_source).exec()?;

    Ok(())
}

pub type EonResult<T> = Result<T, Error>;

#[derive(Debug, Clone)]
pub enum Error {
    LuaError(LuaError),
    ParseError(String),
    OtherError(String),
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::LuaError(e) => e.fmt(f),
            Error::ParseError(e) => e.fmt(f),
            Error::OtherError(e) => e.fmt(f),
        }
    }
}

impl From<LuaError> for Error {
    fn from(e: LuaError) -> Self {
        Error::LuaError(e)
    }
}

impl<'token> From<ParseError<usize, Token<'token>, &'static str>> for Error {
    fn from(e: ParseError<usize, Token<'token>, &'static str>) -> Self {
        Error::ParseError(e.to_string())
    }
}

impl From<&str> for Error {
    fn from(e: &str) -> Self {
        Error::OtherError(e.to_string())
    }
}