Skip to content

Commit

Permalink
Merge pull request #89 from sezna/alex/wasm-compiler
Browse files Browse the repository at this point in the history
Initial wasm compiler and playground
  • Loading branch information
sezna authored Jul 4, 2024
2 parents 367b5eb + 3fba392 commit 0a2d199
Show file tree
Hide file tree
Showing 21 changed files with 5,088 additions and 442 deletions.
546 changes: 200 additions & 346 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ members = [
"petr-ir",
"petr-vm",
"petr-pkg",
"petr-profiling",
"petr-profiling",
"petr-api",
"petr-playground"
]
18 changes: 5 additions & 13 deletions pete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
petr-parse = { "path" = "../petr-parse" }
petr-utils = { "path" = "../petr-utils", optional = true }
petr-vm = { "path" = "../petr-vm" }
petr-ir = { "path" = "../petr-ir" }
petr-fmt = { path = "../petr-fmt" }
petr-api = { "path" = "../petr-api", features = ["fancy", "default"] }
petr-profiling = { "path" = "../petr-profiling" }
petr-resolve = { "path" = "../petr-resolve" }
petr-typecheck = { "path" = "../petr-typecheck" }
petr-resolve = { "path" = "../petr-resolve", optional = true }
petr-pkg = { "path" = "../petr-pkg" }
petr-profiling = { path = "../petr-profiling" }
miette = { version = "7.2.0", features = ["fancy"] }
thiserror = "1.0"
toml = "0.8"
termcolor = "1.4.1"
termcolor = { version = "1.4" }
petr-pkg = { "path" = "../petr-pkg" }

[features]
debug = ["petr-utils/debug", "petr-resolve/debug"]
default = ["dep:petr-utils", "dep:petr-resolve"]
131 changes: 58 additions & 73 deletions pete/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ use std::{
};

use clap::Parser as ClapParser;
use petr_ir::Lowerer;
use petr_parse::Parser;
use petr_api::*;
use petr_pkg::BuildPlan;
use petr_utils::{Identifier, IndexMap, SourceId, SpannedItem};
use petr_vm::Vm;
use termcolor::{ColorChoice, ColorSpec, StandardStream, WriteColor};

mod error {
pub mod error {
use thiserror::Error;
#[derive(Error, Debug)]
pub enum PeteError {
Expand Down Expand Up @@ -117,7 +114,7 @@ fn main() -> Result<(), error::PeteError> {
timings.end("load files");

timings.start("format");
petr_fmt::format_sources(files, manifest.formatter.into())?;
format_sources(files, manifest.formatter.into())?;
timings.end("format");

if time {
Expand All @@ -139,73 +136,7 @@ fn main() -> Result<(), error::PeteError> {
Ok(())
}

#[allow(clippy::type_complexity)]
fn load_project_and_dependencies(path: &Path) -> Result<(petr_pkg::Lockfile, Vec<(PathBuf, String)>, BuildPlan), crate::error::PeteError> {
let manifest = petr_pkg::manifest::find_manifest(Some(path.to_path_buf())).expect("Failed to find manifest");
let dependencies = manifest.dependencies;
let mut stdout = StandardStream::stdout(ColorChoice::Always);

if !dependencies.is_empty() {
stdout.set_color(ColorSpec::new().set_bold(true))?;
/*
todo!(
"instead of saying fetching, pay attention to if it already exists
and print if it does or doesn't. also, check if checksum agrees with lockfile
and use rev etc on github dep to determine thet key"
);
*/
println!(
"Fetching {} {} for package {}",
dependencies.len(),
if dependencies.len() == 1 { "dependency" } else { "dependencies" },
manifest.name
);

stdout.set_color(ColorSpec::new().set_bold(false))?;
}
let (lockfile, build_plan) = petr_pkg::load_dependencies(dependencies)?;

let files = load_files(path);
Ok((lockfile, files, build_plan))
}

fn load_files(path: &Path) -> Vec<(PathBuf, String)> {
let mut buf = Vec::new();

fn read_petr_files(
dir: &PathBuf,
buf: &mut Vec<(PathBuf, String)>,
) {
let entries = fs::read_dir(dir).expect("Failed to read directory");
for entry in entries {
let entry = entry.expect("Failed to read directory entry");
let path = entry.path();
if path.is_dir() {
read_petr_files(&path, buf);
} else if path.extension().and_then(|s| s.to_str()) == Some("pt") {
let source = fs::read_to_string(&path).expect("Failed to read file");
buf.push((path, source));
}
}
}

read_petr_files(&path.join("src"), &mut buf);
buf
}

fn render_errors<T>(
errs: Vec<SpannedItem<T>>,
sources: &IndexMap<SourceId, (&'static str, &'static str)>,
) where
T: miette::Diagnostic + Send + Sync + 'static,
{
for err in errs {
let rendered = petr_utils::render_error(sources, err);
eprintln!("{:?}", rendered);
}
}

fn compile(
pub fn compile(
path: PathBuf,
timings: &mut petr_profiling::Timings,
) -> Result<Lowerer, crate::error::PeteError> {
Expand Down Expand Up @@ -295,3 +226,57 @@ fn compile(

Ok(lowerer)
}

#[allow(clippy::type_complexity)]
pub fn load_project_and_dependencies(path: &Path) -> Result<(petr_pkg::Lockfile, Vec<(PathBuf, String)>, BuildPlan), crate::error::PeteError> {
let manifest = petr_pkg::manifest::find_manifest(Some(path.to_path_buf())).expect("Failed to find manifest");
let dependencies = manifest.dependencies;
let mut stdout = StandardStream::stdout(ColorChoice::Always);

if !dependencies.is_empty() {
stdout.set_color(ColorSpec::new().set_bold(true))?;
/*
todo!(
"instead of saying fetching, pay attention to if it already exists
and print if it does or doesn't. also, check if checksum agrees with lockfile
and use rev etc on github dep to determine thet key"
);
*/
println!(
"Fetching {} {} for package {}",
dependencies.len(),
if dependencies.len() == 1 { "dependency" } else { "dependencies" },
manifest.name
);

stdout.set_color(ColorSpec::new().set_bold(false))?;
}
let (lockfile, build_plan) = petr_pkg::load_dependencies(dependencies)?;

let files = load_files(path);
Ok((lockfile, files, build_plan))
}

pub fn load_files(path: &Path) -> Vec<(PathBuf, String)> {
let mut buf = Vec::new();

fn read_petr_files(
dir: &PathBuf,
buf: &mut Vec<(PathBuf, String)>,
) {
let entries = fs::read_dir(dir).expect("Failed to read directory");
for entry in entries {
let entry = entry.expect("Failed to read directory entry");
let path = entry.path();
if path.is_dir() {
read_petr_files(&path, buf);
} else if path.extension().and_then(|s| s.to_str()) == Some("pt") {
let source = fs::read_to_string(&path).expect("Failed to read file");
buf.push((path, source));
}
}
}

read_petr_files(&path.join("src"), &mut buf);
buf
}
27 changes: 27 additions & 0 deletions petr-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "petr-api"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
petr-parse = { "path" = "../petr-parse" }
petr-utils = { "path" = "../petr-utils", optional = true }
petr-vm = { "path" = "../petr-vm" }
petr-ir = { "path" = "../petr-ir" }
petr-fmt = { path = "../petr-fmt" }
petr-typecheck = { "path" = "../petr-typecheck" }
petr-resolve = { "path" = "../petr-resolve", optional = true }
petr-pkg = { "path" = "../petr-pkg", optional = true }
termcolor = { version = "1.4", optional = true }
petr-profiling = { path = "../petr-profiling" }
miette = { version = "5.10", optional = true }
thiserror = "1.0"
toml = "0.8"

[features]
debug = ["petr-utils/debug", "petr-resolve/debug"]
no_std = ["dep:petr-utils", "dep:petr-resolve", "dep:miette"]
fancy = ["miette/fancy"]
default = ["dep:petr-utils", "dep:petr-resolve", "dep:miette", "termcolor", "petr-pkg"]
Loading

0 comments on commit 0a2d199

Please sign in to comment.