Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Remove compile command (#121)
Browse files Browse the repository at this point in the history
* First try to locate module

* Replace location of builtin library

* Don't generate builtin lib in build.rs

* Use main and lib as entry files in packet

* Use new structure for packets

* Remove compile command

* Fix compilation

* Fix tests

* Add test
  • Loading branch information
gavrilikhin-d authored Apr 7, 2024
1 parent 23b5dc3 commit 5581434
Show file tree
Hide file tree
Showing 173 changed files with 727 additions and 682 deletions.
22 changes: 0 additions & 22 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use clap::ValueEnum;
use clap_complete::{generate_to, Shell};
use clap_complete_fig::Fig;
use std::io::Error;
use std::path::Path;

include!("src/driver/cli.rs");

Expand All @@ -21,29 +20,8 @@ fn generate_autocompletion() -> Result<(), Error> {
Ok(())
}

/// Precompile the builtin module
fn compile_builtin_module() -> Result<(), Error> {
let ppl = Path::new("target/debug/ppl");
if !ppl.exists() {
return Ok(());
}

let status = std::process::Command::new(ppl)
.args(&["compile", "src/runtime/ppl.ppl"])
.args(&["--emit", "dynamic-library"])
.args(&["--output-dir", "target/debug/deps"])
.arg("--no-core")
.status()?;

assert!(status.success());

Ok(())
}

fn main() -> Result<(), Error> {
generate_autocompletion()?;

compile_builtin_module()?;

Ok(())
}
2 changes: 2 additions & 0 deletions ppl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore files generated by PPL compiler
**/target
1 change: 1 addition & 0 deletions ppl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# PPL standard library
File renamed without changes.
85 changes: 58 additions & 27 deletions src/compilation/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
env::current_dir,
path::{Path, PathBuf},
};

use indexmap::IndexMap;

Expand All @@ -9,7 +12,7 @@ use crate::{
SourceFile,
};
use log::{debug, trace};
use miette::miette;
use miette::{bail, miette};

use super::{Package, PackageData};

Expand Down Expand Up @@ -89,22 +92,15 @@ pub struct Compiler {
}

impl Compiler {
/// Name of builtin module
pub const BUILTIN_MODULE_NAME: &'static str = "ppl";
/// Directory of builtin module
pub const BUILTIN_MODULE_DIR: &'static str =
concat!(env!("CARGO_MANIFEST_DIR"), "/src/runtime");
/// Path of builtin module
pub const BUILTIN_MODULE_PATH: &'static str =
concat!(env!("CARGO_MANIFEST_DIR"), "/src/runtime/ppl.ppl");
/// Location of PPL package
pub const PPL_PACKAGE: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/ppl");

/// Create new compiler with empty cache
pub fn new() -> Self {
let path = Path::new(Self::BUILTIN_MODULE_DIR);

let path = Path::new(Self::PPL_PACKAGE);
let mut compiler = Compiler::without_builtin().at(path);

compiler.compile_package(Self::BUILTIN_MODULE_NAME).unwrap();
compiler.compile_package("ppl").unwrap();

compiler.import_builtin = true;

Expand Down Expand Up @@ -155,17 +151,13 @@ impl Compiler {
/// Locate module by name
///
/// # Module search order
/// 1. `{root}/{name}.ppl`
/// 2. `{root}/{name}/mod.ppl`
/// 1. `{root}/src/{name}.ppl`
/// 2. `{root}/src/{name}/mod.ppl`
pub fn locate(&mut self, name: &str) -> miette::Result<PathBuf> {
let variants = if name == Self::BUILTIN_MODULE_NAME {
vec![Self::BUILTIN_MODULE_PATH.into()]
} else {
vec![
self.root.join(format!("{name}.ppl")),
self.root.join(name).join("mod.ppl"),
]
};
let variants = vec![
self.root.join("src").join(format!("{name}.ppl")),
self.root.join("src").join(name).join("mod.ppl"),
];

variants
.iter()
Expand All @@ -191,9 +183,9 @@ impl Compiler {
/// Get compiled module from cache or compile it
///
/// # Module search order
/// 1. `{root}/{name}.ppl`
/// 2. `{root}/{name}/mod.ppl`
fn compile(&mut self, name: &str) -> miette::Result<Module> {
/// 1. `{root}/src/{name}.ppl`
/// 2. `{root}/src/{name}/mod.ppl`
pub(crate) fn compile(&mut self, name: &str) -> miette::Result<Module> {
let path = self.locate(name)?;
let canonic_path = std::fs::canonicalize(&path).unwrap();

Expand Down Expand Up @@ -233,6 +225,28 @@ impl Compiler {
Ok(module)
}

fn locate_package(&mut self, package: &str) -> miette::Result<PathBuf> {
if package == "ppl" {
return Ok(Self::PPL_PACKAGE.into());
}

let cwd = current_dir().unwrap();
if cwd.is_dir() && cwd.ends_with(package) {
return Ok(cwd);
}

let dep = cwd.join("dependencies").join(package);
if dep.is_dir() {
return Ok(dep);
}

bail!(
"Package `{package}` not found in {} or {}",
cwd.display(),
dep.display()
)
}

/// Get compiled package from cache or compile it
pub fn compile_package(&mut self, package: &str) -> miette::Result<Package> {
if let Some(index) = self.packages.get_index_of(package) {
Expand All @@ -242,18 +256,35 @@ impl Compiler {
let name = package.to_string();
let index = self.packages.len();
let package = Package::with_index(index);
let old_root = self.root.clone();
let root = self.locate_package(&name)?;
self.root = root.clone();
self.packages.insert(
name.clone(),
PackageData {
root,
name: name.clone(),
modules: Default::default(),
dependencies: Default::default(),
},
);

self.package_stack.push(package);
self.compile(&name)?;
let main = self.root.join("src/main.ppl");
let lib = self.root.join("src/lib.ppl");
if main.exists() {
self.compile("main")?;
} else if lib.exists() {
self.compile("lib")?;
} else {
bail!(
"No {main} or {lib} found in package `{name}`",
main = main.display(),
lib = lib.display()
);
}
self.package_stack.pop();
self.root = old_root;

Ok(package)
}
Expand Down
4 changes: 3 additions & 1 deletion src/compilation/package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashSet, path::PathBuf};

use super::{Compiler, Module};

Expand Down Expand Up @@ -34,6 +34,8 @@ impl Package {
pub struct PackageData {
/// Name of the package
pub name: String,
/// Root directory of the package
pub root: PathBuf,
/// List of modules in the package
pub modules: Vec<Module>,
/// List of dependencies for this package
Expand Down
19 changes: 3 additions & 16 deletions src/driver/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use self::commands::{Build, Compile, New};
use self::commands::{Build, New};
use clap::{Parser, Subcommand};
use derive_more::From;

Expand All @@ -17,8 +17,6 @@ pub enum Command {
New(New),
/// Build package
Build(Build),
/// Compile single ppl file
Compile(Compile),
}

pub mod commands {
Expand All @@ -38,24 +36,13 @@ pub mod commands {

/// Command to build a package
#[derive(Parser, Debug)]
pub struct Build {}

/// Command to compile single ppl file
#[derive(Parser, Debug)]
pub struct Compile {
/// File to compile
#[arg(value_name = "file")]
pub file: PathBuf,
pub struct Build {
/// Directory where compiler output will be placed.
#[arg(long, value_name = "dir", default_value = ".")]
#[arg(long, value_name = "dir", default_value = "target")]
pub output_dir: PathBuf,
/// Output type of compilation
#[arg(long = "emit", value_name = "output type")]
pub output_type: Option<OutputType>,
/// Compile without core library.
/// Used for compiling core library itself.
#[arg(long, default_value = "false")]
pub no_core: bool,
}

pub mod compile {
Expand Down
Loading

0 comments on commit 5581434

Please sign in to comment.