Skip to content

Commit

Permalink
Experimental REPL
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
DvvCz committed Mar 25, 2024
1 parent 48b9733 commit 56bb60c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cpkg"
description = "A dead simple C package manager."
version = "0.4.0"
version = "0.5.0"
edition = "2021"

authors = ["David Cruz <[email protected]>"]
Expand Down
33 changes: 29 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
pub struct CompilerFlags {
/// Whether to invoke the compiler in "repl" mode, aka silence everything that isn't an error
repl: bool,
}

impl CompilerFlags {
pub const REPL: Self = Self { repl: true };
}

impl Default for CompilerFlags {
fn default() -> Self {
Self {
repl: false
}
}
}

pub trait Compiler {
fn compile(&self, file: &std::path::Path, deps: &[std::path::PathBuf], to: &std::path::Path) -> anyhow::Result<()>;
fn compile(&self, file: &std::path::Path, deps: &[std::path::PathBuf], to: &std::path::Path, flags: &CompilerFlags) -> anyhow::Result<()>;
}

pub struct Gcc {
path: std::path::PathBuf
}

impl Compiler for Gcc {
fn compile(&self, file: &std::path::Path, _deps: &[std::path::PathBuf], to: &std::path::Path) -> anyhow::Result<()> {
let e = std::process::Command::new(&self.path)
fn compile(&self, file: &std::path::Path, _deps: &[std::path::PathBuf], to: &std::path::Path, flags: &CompilerFlags) -> anyhow::Result<()> {
let mut cmd = std::process::Command::new(&self.path);

let mut cmd = cmd
.arg(file)
.arg("-o")
.arg(to)
.arg(to);

if flags.repl {
cmd = cmd.arg("-w");
}

let e = cmd
.spawn()?
.wait()?;

Expand Down
51 changes: 47 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use clap::{Parser, Subcommand};
use indoc::indoc;
use colored::Colorize;

use crate::compiler::CompilerFlags;

mod compiler;
mod docgen;

Expand Down Expand Up @@ -146,7 +148,7 @@ fn main() -> anyhow::Result<()> {
let path = entry.path();
let out = out.join(&path.file_stem().unwrap());

backend.compile(&path, &[], &out)?;
backend.compile(&path, &[], &out, &Default::default())?;
compiled_tests.push(out);
}
}
Expand Down Expand Up @@ -185,7 +187,7 @@ fn main() -> anyhow::Result<()> {

let out = target.join("out");
let backend = compiler::try_locate()?;
backend.compile(main, &[], &out)?;
backend.compile(main, &[], &out, &Default::default())?;

println!("Successfully built program in {}s", now.elapsed().as_secs_f32());
},
Expand All @@ -209,7 +211,7 @@ fn main() -> anyhow::Result<()> {
let out = target.join("out");

let b = compiler::try_locate()?;
b.compile(main, &[], &out)?;
b.compile(main, &[], &out, &Default::default())?;

std::process::Command::new(out)
.spawn()?;
Expand Down Expand Up @@ -262,7 +264,48 @@ fn main() -> anyhow::Result<()> {
},

Some(Commands::Repl) => {
anyhow::bail!("Repl is not implemented");
use std::io::{Write, BufRead};

println!("{}", "Please note that the repl is very basic and experimental.\nYour code will run entirely each line.".yellow());

let backend = compiler::try_locate()?;

let temp = std::env::temp_dir();
let temp_repl = temp.join("cpkg_repl.c");
let temp_bin = temp.join("cpkg_repl");

let mut stdout = std::io::stdout().lock();
let mut stdin = std::io::stdin().lock();

let mut buffer = String::new();

loop {
stdout.write(b"> ")?;
stdout.flush()?;

let mut temp = String::new();
stdin.read_line(&mut temp)?;

let total = [buffer.clone(), temp].join("");

std::fs::write(&temp_repl, indoc::formatdoc!(r#"
int main() {{
{total}
return 0;
}}
"#))?;

match backend.compile(&temp_repl, &[], &temp_bin, &CompilerFlags::REPL) {
Ok(_) => {
buffer = total;

std::process::Command::new(&temp_bin)
.spawn()?
.wait()?;
},
Err(_) => ()
}
}
},

Some(Commands::Upgrade) => {
Expand Down

0 comments on commit 56bb60c

Please sign in to comment.