about summary refs log tree commit diff
path: root/src/commands/eval.rs
blob: 61a712c08a8ef25ab6419c5cfe1e998a45c2016d (plain) (blame)
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
use clap::Clap;

use crate::codegen;
use crate::interpreter;
use crate::parser;
use crate::tc;
use crate::Result;

/// Evaluate an expression and print its result
#[derive(Clap)]
pub struct Eval {
    /// JIT-compile with LLVM instead of interpreting
    #[clap(long)]
    jit: bool,

    /// Expression to evaluate
    expr: String,
}

impl Eval {
    pub fn run(self) -> Result<()> {
        let (_, parsed) = parser::expr(&self.expr)?;
        let hir = tc::typecheck_expr(parsed)?;
        let result = if self.jit {
            codegen::jit_eval::<i64>(&hir)?.into()
        } else {
            interpreter::eval(&hir)?
        };
        println!("{}", result);
        Ok(())
    }
}