Added configuration and some fixes

This commit is contained in:
Felipe g 2022-09-20 13:14:27 -03:00
parent 6706b4b1df
commit e6d79950d9
10 changed files with 156 additions and 41 deletions

2
CHANGELOG.md Normal file
View File

@ -0,0 +1,2 @@
# Kind2 0.2.76
The main.rs and language.rs files have been broken into several parts.

65
Cargo.lock generated
View File

@ -119,6 +119,22 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "ctor"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdffe87e1d521a10f9696f833fe502293ea446d7f256c06128293a4119bdf4cb"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "either"
version = "1.8.0"
@ -435,7 +451,9 @@ dependencies = [
"clap",
"highlight_error",
"hvm",
"pretty_assertions",
"rand",
"walkdir",
]
[[package]]
@ -568,6 +586,15 @@ version = "6.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
[[package]]
name = "output_vt100"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66"
dependencies = [
"winapi",
]
[[package]]
name = "percent-encoding"
version = "2.2.0"
@ -598,6 +625,18 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "pretty_assertions"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755"
dependencies = [
"ctor",
"diff",
"output_vt100",
"yansi",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -748,6 +787,15 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "schannel"
version = "0.1.20"
@ -1008,6 +1056,17 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "want"
version = "0.3.0"
@ -1182,3 +1241,9 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View File

@ -15,3 +15,11 @@ hvm = "0.1.81"
highlight_error = "0.1.1"
clap = { version = "3.1.8", features = ["derive"] }
rand = "0.8.5"
walkdir = "2"
[[test]]
name = "kind2-tests"
path = "tests/mod.rs"
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -232,7 +232,7 @@ impl Localized for Term {
func.set_origin_file(file);
argm.set_origin_file(file);
}
Mat { orig, expr, cses, moti } => {
Mat { orig, expr, cses, moti, .. } => {
*orig = orig.set_file(file);
expr.set_origin_file(file);
for cse in cses {

View File

@ -1,6 +1,6 @@
use crate::book::name::Ident;
use crate::book::term::{Term, Operator};
use crate::book::{Entry, Rule}
use crate::book::{Entry, Rule};
use crate::codegen::kdl::Book;
use std::collections::HashMap;

View File

@ -1,6 +1,7 @@
mod loader;
pub mod loader;
pub mod config;
use crate::driver::loader::{load, file};
use crate::driver::loader::{load, File};
use crate::checker::to_checker_book;
use crate::parser::new_type;
use crate::book::new_type::Derived;
@ -8,11 +9,21 @@ use crate::book::name::Ident;
use crate::book::Book;
use crate::codegen;
use crate::driver::config::Config;
const CHECKER_HVM: &str = include_str!("checker.hvm");
pub struct RunResult {
output: String,
rewrites: u64,
pub output: String,
pub rewrites: u64,
}
pub fn highlight(should: bool, text: &str) -> String {
if should {
format!("\x1b[4m\x1b{}\x1b[4m\x1b", text)
} else {
text.to_string()
}
}
pub fn readback_string(rt: &hvm::Runtime, host: u64) -> String {
@ -95,28 +106,29 @@ pub fn run_with_hvm(code: &str, main: &str, read_string: bool) -> Result<RunResu
})
}
pub fn cmd_to_hvm(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_to_hvm(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
let result = codegen::hvm::to_hvm_book(&loaded.book);
print!("{}", result);
Ok(())
}
pub fn cmd_show(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_show(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
println!("{}", loaded.book);
Ok(())
}
pub fn cmd_gen_checker(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_gen_checker(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
let gen_path = format!("{}.hvm", path.replace(".kind2", ".check"));
println!("Generated '{}'.", gen_path);
std::fs::write(gen_path, gen_checker(&loaded.book)).ok();
Ok(())
}
pub fn cmd_derive(path: &str) -> Result<(), String> {
pub fn cmd_derive(config: Config, path: &str) -> Result<(), String> {
let color = config.color_output;
let newcode = match std::fs::read_to_string(&path) {
Err(_) => {
return Err(format!("File not found: '{}'.", path));
@ -125,28 +137,28 @@ pub fn cmd_derive(path: &str) -> Result<(), String> {
};
let newtype = match new_type::read_newtype(&newcode) {
Err(err) => {
return Err(format!("\x1b[1m[{}]\x1b[0m\n{}", path, err));
return Err(format!("[{}]\n{}", highlight(color, path), err));
}
Ok(book) => book,
};
fn save_derived(path: &str, derived: &Derived) {
fn save_derived(color: bool, path: &str, derived: &Derived) {
let dir = std::path::Path::new(&derived.path.0);
let txt = format!("// Automatically derived from {}\n{}", path, derived.entr);
println!("\x1b[4m\x1b[1mDerived '{}':\x1b[0m", derived.path);
println!("[1mDerived '{}':", highlight(color, &derived.path.0));
println!("{}\n", txt);
std::fs::create_dir_all(dir.parent().unwrap()).unwrap();
std::fs::write(dir, txt).ok();
}
save_derived(path, &new_type::derive_type(&newtype));
save_derived(color, path, &new_type::derive_type(&newtype));
for i in 0..newtype.ctrs.len() {
save_derived(path, &new_type::derive_ctr(&newtype, i));
save_derived(color, path, &new_type::derive_ctr(&newtype, i));
}
save_derived(path, &new_type::derive_match(&newtype));
save_derived(color, path, &new_type::derive_match(&newtype));
Ok(())
}
pub fn cmd_check_all(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_check_all(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
let result = run_with_hvm(&gen_checker(&loaded.book), "Kind.API.check_all", true)?;
print!("{}", inject_highlights(&loaded.file, &result.output));
println!("Rewrites: {}", result.rewrites);
@ -154,8 +166,8 @@ pub fn cmd_check_all(path: &str) -> Result<(), String> {
}
// Evaluates Main on Kind2
pub fn cmd_eval_main(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_eval_main(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
if loaded.book.entrs.contains_key(&Ident("Main".to_string())) {
let result = run_with_hvm(&gen_checker(&loaded.book), "Kind.API.eval_main", true)?;
print!("{}", result.output);
@ -166,8 +178,8 @@ pub fn cmd_eval_main(path: &str) -> Result<(), String> {
}
}
pub fn cmd_run_main(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_run_main(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
if loaded.book.entrs.contains_key(&Ident("Main".to_string())) {
let result = codegen::hvm::to_hvm_book(&loaded.book);
let result = run_with_hvm(&result, "Main", false)?;
@ -179,8 +191,8 @@ pub fn cmd_run_main(path: &str) -> Result<(), String> {
}
}
pub fn cmd_to_kdl(path: &str) -> Result<(), String> {
let loaded = load(path)?;
pub fn cmd_to_kdl(config: Config, path: &str) -> Result<(), String> {
let loaded = load(config, path)?;
let comp_book = codegen::kdl::compile_book(&loaded.book)?;
let kdl_names = codegen::kdl::get_kdl_names(&comp_book)?;
let result = codegen::kdl::to_kdl_book(&loaded.book, &kdl_names, &comp_book)?;

7
src/driver/config.rs Normal file
View File

@ -0,0 +1,7 @@
// The configuration needed to customize
// the compiler experience
#[derive(Clone, Copy)]
pub struct Config {
pub no_high_line: bool,
pub color_output: bool,
}

View File

@ -5,6 +5,8 @@ use crate::book::Book;
use crate::lowering::adjust::AdjustErrorKind;
use crate::parser::read_book;
use super::config::Config;
pub struct File {
pub path: String,
pub code: String,
@ -74,7 +76,7 @@ pub fn load_entry(name: &str, load: &mut Load) -> Result<(), String> {
Ok(())
}
pub fn load(name: &str) -> Result<Load, String> {
pub fn load(config: Config, name: &str) -> Result<Load, String> {
let mut load = Load::new_empty();
if !std::path::Path::new(name).is_file() {
@ -88,11 +90,12 @@ pub fn load(name: &str) -> Result<Load, String> {
load.book = book;
}
Err(err) => {
let high_line = if let Span::Localized(SpanData { file, start, end }) = err.orig {
highlight_error::highlight_error(start.0 as usize, end.0 as usize, &load.file[file.0 as usize].code)
} else {
"".to_string()
};
let high_line =
match err.orig {
Span::Localized(SpanData { file, start, end }) if !config.no_high_line =>
highlight_error::highlight_error(start.0 as usize, end.0 as usize, &load.file[file.0 as usize].code),
_ => "".to_string()
};
return match err.kind {
AdjustErrorKind::IncorrectArity => Err(format!("Incorrect arity.\n{}", high_line)),
AdjustErrorKind::UnboundVariable { name } => Err(format!("Unbound variable '{}'.\n{}", name, high_line)),

6
src/lib.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod book;
pub mod checker;
pub mod driver;
pub mod lowering;
pub mod parser;
pub mod codegen;

View File

@ -6,6 +6,7 @@ pub mod parser;
pub mod codegen;
use crate::driver::*;
use crate::driver::config::Config;
use clap::{Parser, Subcommand};
@ -15,6 +16,12 @@ use clap::{Parser, Subcommand};
pub struct Cli {
#[clap(subcommand)]
pub command: Command,
#[clap(short, long, value_parser, default_value_t = false)]
pub no_high_line: bool,
#[clap(short, long, value_parser, default_value_t = true)]
pub color_output: bool
}
#[derive(Subcommand)]
@ -55,15 +62,20 @@ pub enum Command {
fn run_cli() -> Result<(), String> {
let cli_matches = Cli::parse();
let config = Config {
no_high_line: cli_matches.no_high_line,
color_output: cli_matches.color_output
};
match cli_matches.command {
Command::Eval { file: path } => cmd_eval_main(&path),
Command::Run { file: path } => cmd_run_main(&path),
Command::Check { file: path } => cmd_check_all(&path),
Command::Derive { file: path } => cmd_derive(&path),
Command::GenChecker { file: path } => cmd_gen_checker(&path),
Command::Show { file: path } => cmd_show(&path),
Command::ToKDL { file: path } => cmd_to_kdl(&path),
Command::ToHVM { file: path } => cmd_to_hvm(&path),
Command::Eval { file: path } => cmd_eval_main(config, &path),
Command::Run { file: path } => cmd_run_main(config, &path),
Command::Check { file: path } => cmd_check_all(config, &path),
Command::Derive { file: path } => cmd_derive(config, &path),
Command::GenChecker { file: path } => cmd_gen_checker(config, &path),
Command::Show { file: path } => cmd_show(config, &path),
Command::ToKDL { file: path } => cmd_to_kdl(config, &path),
Command::ToHVM { file: path } => cmd_to_hvm(config, &path),
}
}