Skip to content

Commit

Permalink
loxcraft run - should read source from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Sep 28, 2024
1 parent 17d6743 commit 43920e6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;
use std::io::{self, Write};
use std::io::{self, Read, Write};

use anyhow::{Context, Result, bail};
use clap::Parser;
Expand Down Expand Up @@ -43,8 +43,17 @@ impl Cmd {
Cmd::Repl => bail!("loxcraft was not compiled with the `repl` feature"),

Cmd::Run { path } => {
let source = fs::read_to_string(path)
.with_context(|| format!("could not read file: {path}"))?;
let source = if path == "-" {
let mut source = String::new();
io::stdin()
.read_to_string(&mut source)
.context("could not read source from stdin")?;
source
} else {
fs::read_to_string(path)
.with_context(|| format!("could not read source from file: {path}"))?
};

let mut vm = VM::default();
let stdout = &mut io::stdout().lock();
if let Err(e) = vm.run(&source, stdout) {
Expand Down

0 comments on commit 43920e6

Please sign in to comment.