Skip to content

Commit c9fbcf8

Browse files
committed
Add a command to evaluate a single expression, using a fake module to do so (for includes).
When we can include things in-scope in the future, this can be changed to an anonymous expression.
1 parent 1d122d5 commit c9fbcf8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::process::ExitCode;
33
use crate::cli::logging::dump_failure;
44

55
pub mod run;
6+
pub mod expression;
67
pub mod check;
78
pub mod transpile;
89
pub mod logging;
@@ -14,6 +15,7 @@ pub fn make_command() -> Command {
1415
.arg_required_else_help(true)
1516
.allow_external_subcommands(true)
1617
.subcommand(run::make_command())
18+
.subcommand(expression::make_command())
1719
.subcommand(check::make_command())
1820
.subcommand(transpile::make_command())
1921
}
@@ -23,6 +25,7 @@ pub fn run_command() -> ExitCode {
2325

2426
let result = match matches.subcommand() {
2527
Some(("run", sub_matches)) => run::run(sub_matches),
28+
Some(("expression", sub_matches)) => expression::run(sub_matches),
2629
Some(("check", sub_matches)) => check::run(sub_matches),
2730
Some(("transpile", sub_matches)) => transpile::run(sub_matches),
2831
_ => panic!("Unsupported action."),

src/cli/expression.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::path::PathBuf;
2+
use std::process::ExitCode;
3+
4+
use clap::{arg, ArgMatches, Command};
5+
6+
use crate::error::RResult;
7+
use crate::{interpreter, parser};
8+
use crate::interpreter::runtime::Runtime;
9+
use crate::program::functions::FunctionInterface;
10+
use crate::program::module::{module_name, ModuleName};
11+
use crate::program::types::TypeProto;
12+
13+
pub fn make_command() -> Command {
14+
Command::new("expression")
15+
.about("Run an expression using the interpreter.")
16+
.arg_required_else_help(true)
17+
.arg(arg!(<EXPRESSION> "expression to run").value_parser(clap::value_parser!(String)))
18+
}
19+
20+
pub fn run(args: &ArgMatches) -> RResult<ExitCode> {
21+
let expression = args.get_one::<String>("EXPRESSION").unwrap();
22+
let full_expression = format!("use!(module!(\"common\")); def main! :: write_line({});", expression);
23+
24+
let mut runtime = Runtime::new()?;
25+
runtime.add_common_repository();
26+
27+
let module = runtime.load_text_as_module(&full_expression, vec!["dynamic".to_string()])?;
28+
29+
interpreter::run::main(&module, &mut runtime)?;
30+
31+
Ok(ExitCode::SUCCESS)
32+
}

0 commit comments

Comments
 (0)