diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/check.rs | 39 | ||||
-rw-r--r-- | src/commands/eval.rs | 6 | ||||
-rw-r--r-- | src/commands/mod.rs | 2 |
3 files changed, 45 insertions, 2 deletions
diff --git a/src/commands/check.rs b/src/commands/check.rs new file mode 100644 index 000000000000..40de288a282c --- /dev/null +++ b/src/commands/check.rs @@ -0,0 +1,39 @@ +use clap::Clap; +use std::path::PathBuf; + +use crate::ast::Type; +use crate::{parser, tc, Result}; + +/// Typecheck a file or expression +#[derive(Clap)] +pub struct Check { + /// File to check + path: Option<PathBuf>, + + /// Expression to check + #[clap(long, short = 'e')] + expr: Option<String>, +} + +fn run_expr(expr: String) -> Result<Type> { + let (_, parsed) = parser::expr(&expr)?; + let hir_expr = tc::typecheck_expr(parsed)?; + Ok(hir_expr.type_().clone()) +} + +fn run_path(path: PathBuf) -> Result<Type> { + todo!() +} + +impl Check { + pub fn run(self) -> Result<()> { + let type_ = match (self.path, self.expr) { + (None, None) => Err("Must specify either a file or expression to check".into()), + (Some(_), Some(_)) => Err("Cannot specify both a file and expression to check".into()), + (None, Some(expr)) => run_expr(expr), + (Some(path), None) => run_path(path), + }?; + println!("type: {}", type_); + Ok(()) + } +} diff --git a/src/commands/eval.rs b/src/commands/eval.rs index 112bee64625b..61a712c08a8e 100644 --- a/src/commands/eval.rs +++ b/src/commands/eval.rs @@ -3,6 +3,7 @@ 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 @@ -19,10 +20,11 @@ pub struct Eval { 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>(&parsed)?.into() + codegen::jit_eval::<i64>(&hir)?.into() } else { - interpreter::eval(&parsed)? + interpreter::eval(&hir)? }; println!("{}", result); Ok(()) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9c0038dabfb1..fd0a822708c2 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,7 @@ +pub mod check; pub mod compile; pub mod eval; +pub use check::Check; pub use compile::Compile; pub use eval::Eval; |