about summary refs log tree commit diff
path: root/src/commands
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2021-03-07T20·29-0500
committerGriffin Smith <root@gws.fyi>2021-03-07T20·29-0500
commit80f8ede0bbc9799d5199707e1e1ad8e80e4ca7ac (patch)
treecbb418b042583714fe09f946f1b9a03d1d98857f /src/commands
Initial commit
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/compile.rs28
-rw-r--r--src/commands/eval.rs30
-rw-r--r--src/commands/mod.rs5
3 files changed, 63 insertions, 0 deletions
diff --git a/src/commands/compile.rs b/src/commands/compile.rs
new file mode 100644
index 000000000000..e16b8c87a659
--- /dev/null
+++ b/src/commands/compile.rs
@@ -0,0 +1,28 @@
+use std::path::PathBuf;
+
+use clap::Clap;
+
+use crate::common::Result;
+use crate::compiler::{self, CompilerOptions};
+
+#[derive(Clap)]
+pub struct Compile {
+    file: PathBuf,
+
+    #[clap(short = 'o')]
+    out_file: PathBuf,
+
+    #[clap(flatten)]
+    options: CompilerOptions,
+}
+
+impl Compile {
+    pub fn run(self) -> Result<()> {
+        eprintln!(
+            ">>> {} -> {}",
+            &self.file.to_string_lossy(),
+            self.out_file.to_string_lossy()
+        );
+        compiler::compile_file(&self.file, &self.out_file, &self.options)
+    }
+}
diff --git a/src/commands/eval.rs b/src/commands/eval.rs
new file mode 100644
index 000000000000..112bee64625b
--- /dev/null
+++ b/src/commands/eval.rs
@@ -0,0 +1,30 @@
+use clap::Clap;
+
+use crate::codegen;
+use crate::interpreter;
+use crate::parser;
+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 result = if self.jit {
+            codegen::jit_eval::<i64>(&parsed)?.into()
+        } else {
+            interpreter::eval(&parsed)?
+        };
+        println!("{}", result);
+        Ok(())
+    }
+}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
new file mode 100644
index 000000000000..9c0038dabfb1
--- /dev/null
+++ b/src/commands/mod.rs
@@ -0,0 +1,5 @@
+pub mod compile;
+pub mod eval;
+
+pub use compile::Compile;
+pub use eval::Eval;