From ce8dccda687184988846d33bf1e7922750e3363e Mon Sep 17 00:00:00 2001 From: Vlamonster Date: Fri, 8 Dec 2023 16:26:52 +0100 Subject: [PATCH] Cleanup 2: Electric Boogaloo. --- README.md | 19 +++++---- compiler/src/debug/display.rs | 2 +- compiler/src/debug/mod.rs | 2 +- compiler/src/debug/time.rs | 4 +- compiler/src/lib.rs | 22 ++++++---- compiler/src/main.rs | 21 ++++------ compiler/src/passes/assign/assign.rs | 2 +- .../src/passes/assign/color_interference.rs | 2 +- .../src/passes/assign/include_liveness.rs | 2 +- compiler/src/passes/assign/mod.rs | 2 +- compiler/src/passes/atomize/atomize.rs | 2 +- compiler/src/passes/atomize/mod.rs | 2 +- compiler/src/passes/conclude/conclude.rs | 2 +- compiler/src/passes/conclude/mod.rs | 2 +- compiler/src/passes/eliminate/eliminate.rs | 2 +- .../src/passes/eliminate/eliminate_params.rs | 2 +- .../src/passes/eliminate/eliminate_seq.rs | 2 +- .../src/passes/eliminate/eliminate_tail.rs | 2 +- compiler/src/passes/eliminate/mod.rs | 2 +- compiler/src/passes/emit/mod.rs | 2 +- compiler/src/passes/explicate/explicate.rs | 2 +- .../src/passes/explicate/explicate_assign.rs | 2 +- .../src/passes/explicate/explicate_pred.rs | 2 +- .../src/passes/explicate/explicate_tail.rs | 2 +- compiler/src/passes/explicate/mod.rs | 2 +- compiler/src/passes/parse/mod.rs | 2 +- compiler/src/passes/patch/mod.rs | 2 +- compiler/src/passes/patch/patch.rs | 2 +- compiler/src/passes/reveal/mod.rs | 2 +- compiler/src/passes/reveal/reveal.rs | 2 +- compiler/src/passes/select/mod.rs | 2 +- compiler/src/passes/select/select.rs | 2 +- compiler/src/passes/validate/check_sized.rs | 2 +- .../src/passes/validate/constrain/apply.rs | 10 ++--- .../src/passes/validate/constrain/assign.rs | 7 ++-- .../src/passes/validate/constrain/continue.rs | 8 ++-- compiler/src/passes/validate/constrain/def.rs | 2 +- compiler/src/passes/validate/constrain/let.rs | 2 +- compiler/src/passes/validate/constrain/mod.rs | 2 +- .../src/passes/validate/constrain/struct.rs | 21 ++++------ .../validate/constrain/uncover_globals.rs | 2 +- compiler/src/passes/validate/constrain/var.rs | 2 +- compiler/src/passes/validate/mod.rs | 2 +- compiler/src/passes/validate/partial_type.rs | 2 +- compiler/src/passes/validate/resolve.rs | 2 +- compiler/src/passes/validate/tests.rs | 10 +++-- compiler/src/passes/validate/uniquify/def.rs | 2 +- compiler/src/passes/validate/uniquify/expr.rs | 2 +- compiler/src/passes/validate/uniquify/fn.rs | 2 +- compiler/src/passes/validate/uniquify/mod.rs | 2 +- compiler/src/passes/validate/uniquify/type.rs | 2 +- .../src/passes/validate/uniquify/typedef.rs | 2 +- compiler/src/utils/expect.rs | 3 -- compiler/src/utils/mod.rs | 5 +-- compiler/src/utils/test_runtime.rs | 27 ------------ .../src/utils/{gen_sym.rs => unique_sym.rs} | 0 compiler/tests/integration.rs | 41 ++++++------------- 57 files changed, 116 insertions(+), 168 deletions(-) delete mode 100644 compiler/src/utils/expect.rs delete mode 100644 compiler/src/utils/test_runtime.rs rename compiler/src/utils/{gen_sym.rs => unique_sym.rs} (100%) diff --git a/README.md b/README.md index 7de4457..38f0e67 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ Arguments: Options: -o, --output Specifies the path to an output file. If None, it uses the input filename. If that's also None, it defaults to "output" - -d, --display [possible values: parse, validate, reveal, atomize, explicate, select] - -r, --run + -r, --run Optionally runs and deletes the compiled executable. Only supported on Unix systems + -d, --display Specifies the pass to display. Supported passes are defined by the `Pass` enum [possible values: parse, validate, reveal, atomize, explicate, select] + -t, --time Print timing debug information -h, --help Print help -V, --version Print version ``` @@ -45,10 +46,10 @@ cargo run -- input.sp -o output * Let * If * Functions - * Return + * Return * Loop - * Break - * Continue + * Break + * Continue * While * Sequences * Structs @@ -67,8 +68,8 @@ cargo run -- input.sp -o output * [ ] Add documentation where necessary. * [x] Improve error handling for parsing pass. * [x] Improve error handling for type checking pass. - * [ ] Make errors prettier. -* [ ] Improve algorithm for colouring the interference graph. + * [ ] Make errors prettier. +* [x] Improve algorithm for colouring the interference graph. * [x] Add read and write functionality to the bencher to update locally. * [x] Lots, and lots, of refactoring! * [x] Write test input in comments. @@ -78,8 +79,8 @@ cargo run -- input.sp -o output * [x] Type inference. * [x] Implement comments in code. * [ ] Algebraic Data Types. - * [x] Structs. - * [ ] Enums. + * [x] Structs. + * [ ] Enums. * [ ] First-class functions. * [ ] Constants. diff --git a/compiler/src/debug/display.rs b/compiler/src/debug/display.rs index 131e95b..dc687a9 100644 --- a/compiler/src/debug/display.rs +++ b/compiler/src/debug/display.rs @@ -2,7 +2,7 @@ use crate::debug::{Pass, DEBUG_ARGS}; use std::fmt::Display; pub fn display(value: &T, pass: Pass) { - if let Some(display) = &DEBUG_ARGS.get().unwrap().display { + if let Some(display) = &DEBUG_ARGS.get().and_then(|args| args.display) { if display == &pass { print!("{value}") } diff --git a/compiler/src/debug/mod.rs b/compiler/src/debug/mod.rs index f772995..5cd3334 100644 --- a/compiler/src/debug/mod.rs +++ b/compiler/src/debug/mod.rs @@ -27,7 +27,7 @@ impl DebugArgs { pub static DEBUG_ARGS: OnceCell = OnceCell::new(); #[cfg(feature = "debug")] -#[derive(ValueEnum, Clone, Debug, PartialEq)] +#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)] pub enum Pass { Parse, Validate, diff --git a/compiler/src/debug/time.rs b/compiler/src/debug/time.rs index 185fa05..56f5d6b 100644 --- a/compiler/src/debug/time.rs +++ b/compiler/src/debug/time.rs @@ -27,7 +27,7 @@ static TIME: Lazy> = Lazy::new(|| Mutex::new(Time::new())); /// Initializes the global time tracking instance. /// This function should be called before using the `time` function. pub fn time_init() { - if DEBUG_ARGS.get().unwrap().time { + if let Some(true) = DEBUG_ARGS.get().map(|args| args.time) { println!("{:>12} {:>12} {:>12}", "pass", "since prev", "since init"); Lazy::force(&TIME); } @@ -35,7 +35,7 @@ pub fn time_init() { /// Tracks and prints the time elapsed since the last call to `time` or `time_init`. pub fn time(pass: &str) { - if DEBUG_ARGS.get().unwrap().time { + if let Some(true) = DEBUG_ARGS.get().map(|args| args.time) { let now = Instant::now(); let mut time = TIME.lock().unwrap(); let since_init = now.duration_since(time.init); diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index f73b876..dc43aa6 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -10,15 +10,7 @@ use std::path::Path; pub fn compile(program: &str, filename: &str, output: &Path) -> miette::Result<()> { time_init!(); - // todo: make a module system so we don't have to append the std-library to the source. - let program = String::leak(format!( - "{program}{}{}{}{}{}", - include_str!("../std/exit.sp"), - include_str!("../std/read.sp"), - include_str!("../std/print.sp"), - include_str!("../std/alloc.sp"), - include_str!("../std/math.sp"), - )); + let program = apped_std(program); let add_source = |error| Report::with_source_code(error, NamedSource::new(filename, (*program).to_string())); @@ -42,3 +34,15 @@ pub fn compile(program: &str, filename: &str, output: &Path) -> miette::Result<( Ok(()) } + +// todo: make a module system so we don't have to append the std-library to the source. +fn apped_std(program: &str) -> &str { + String::leak(format!( + "{program}{}{}{}{}{}", + include_str!("../std/exit.sp"), + include_str!("../std/read.sp"), + include_str!("../std/print.sp"), + include_str!("../std/alloc.sp"), + include_str!("../std/math.sp"), + )) +} diff --git a/compiler/src/main.rs b/compiler/src/main.rs index 5ed5e59..e2dd4e7 100644 --- a/compiler/src/main.rs +++ b/compiler/src/main.rs @@ -2,12 +2,13 @@ mod run; use clap::Parser; -use compiler::passes::parse::parse::PrettyParseError; -use compiler::passes::validate::error::TypeError; -use compiler::{compile, debug}; +use compiler::compile; +#[cfg(feature = "debug")] +use compiler::debug::{DebugArgs, Pass}; +use compiler::passes::{parse::parse::PrettyParseError, validate::error::TypeError}; use miette::{Diagnostic, IntoDiagnostic}; use std::fs; -use std::io::Read; +use std::io::{read_to_string, stdin}; use std::path::Path; use thiserror::Error; @@ -40,7 +41,7 @@ struct Args { /// Specifies the pass to display. Supported passes are defined by the `Pass` enum. #[cfg(feature = "debug")] #[arg(value_enum, short, long, value_name = "PASS")] - display: Option, + display: Option, /// Print timing debug information. #[cfg(feature = "debug")] @@ -48,20 +49,14 @@ struct Args { time: bool, } -fn read_from_stdin() -> Result { - let mut program = String::new(); - std::io::stdin().read_to_string(&mut program)?; - Ok(program) -} - fn main() -> miette::Result<()> { let args = Args::parse(); #[cfg(feature = "debug")] - debug::DebugArgs::set(args.time, args.display)?; + DebugArgs::set(args.time, args.display)?; let (program, filename) = match args.input.as_ref() { - None => (read_from_stdin().into_diagnostic()?, "stdin"), + None => (read_to_string(stdin()).into_diagnostic()?, "stdin"), Some(file) => (fs::read_to_string(file).into_diagnostic()?, file.as_str()), }; diff --git a/compiler/src/passes/assign/assign.rs b/compiler/src/passes/assign/assign.rs index 60412dc..06dc680 100644 --- a/compiler/src/passes/assign/assign.rs +++ b/compiler/src/passes/assign/assign.rs @@ -1,7 +1,7 @@ use crate::passes::assign::{Arg, FunAssigned, X86Assigned}; use crate::passes::select::{Block, Instr, InstrSelected, VarArg, X86Selected}; use crate::time; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use functor_derive::Functor; use std::collections::HashMap; diff --git a/compiler/src/passes/assign/color_interference.rs b/compiler/src/passes/assign/color_interference.rs index 6dc0fb3..aa68b65 100644 --- a/compiler/src/passes/assign/color_interference.rs +++ b/compiler/src/passes/assign/color_interference.rs @@ -1,6 +1,6 @@ use crate::passes::assign::{Arg, InterferenceGraph, LArg}; use crate::passes::select::{Reg, CALLEE_SAVED_NO_STACK}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use binary_heap_plus::BinaryHeap; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; diff --git a/compiler/src/passes/assign/include_liveness.rs b/compiler/src/passes/assign/include_liveness.rs index f86b235..41a6dc0 100644 --- a/compiler/src/passes/assign/include_liveness.rs +++ b/compiler/src/passes/assign/include_liveness.rs @@ -1,4 +1,4 @@ -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use crate::passes::assign::{LArg, LBlock, LFun, LX86VarProgram}; use crate::passes::select::{ diff --git a/compiler/src/passes/assign/mod.rs b/compiler/src/passes/assign/mod.rs index c5ba6ca..c0996ae 100644 --- a/compiler/src/passes/assign/mod.rs +++ b/compiler/src/passes/assign/mod.rs @@ -6,7 +6,7 @@ mod include_liveness; use crate::passes::select::{ Block, FunSelected, Imm, Instr, InstrSelected, Reg, VarArg, X86Selected, }; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use functor_derive::Functor; use petgraph::graphmap::GraphMap; diff --git a/compiler/src/passes/atomize/atomize.rs b/compiler/src/passes/atomize/atomize.rs index 106601d..0b0ef79 100644 --- a/compiler/src/passes/atomize/atomize.rs +++ b/compiler/src/passes/atomize/atomize.rs @@ -2,8 +2,8 @@ use crate::passes::atomize::{AExpr, Atom, DefAtomized, PrgAtomized}; use crate::passes::parse::types::Type; use crate::passes::parse::{Meta, Typed}; use crate::passes::reveal::{DefRevealed, PrgRevealed, RExpr}; -use crate::utils::gen_sym::{gen_sym, UniqueSym}; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::{gen_sym, UniqueSym}; use crate::{display, time}; impl<'p> PrgRevealed<'p> { diff --git a/compiler/src/passes/atomize/mod.rs b/compiler/src/passes/atomize/mod.rs index fa28dc3..7fffb01 100644 --- a/compiler/src/passes/atomize/mod.rs +++ b/compiler/src/passes/atomize/mod.rs @@ -5,7 +5,7 @@ use crate::passes::parse::types::Type; use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp}; use crate::passes::select::InstrSelected; use crate::passes::validate::Int; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use itertools::Itertools; use std::collections::HashMap; diff --git a/compiler/src/passes/conclude/conclude.rs b/compiler/src/passes/conclude/conclude.rs index ea29560..ab28556 100644 --- a/compiler/src/passes/conclude/conclude.rs +++ b/compiler/src/passes/conclude/conclude.rs @@ -2,7 +2,7 @@ use crate::passes::assign::{Arg, InstrAssigned}; use crate::passes::conclude::X86Concluded; use crate::passes::patch::X86Patched; use crate::passes::select::{Block, Imm, Instr}; -use crate::utils::gen_sym::gen_sym; +use crate::utils::unique_sym::gen_sym; use crate::*; use std::collections::HashMap; diff --git a/compiler/src/passes/conclude/mod.rs b/compiler/src/passes/conclude/mod.rs index 40afaef..2d06bbf 100644 --- a/compiler/src/passes/conclude/mod.rs +++ b/compiler/src/passes/conclude/mod.rs @@ -2,7 +2,7 @@ pub mod conclude; use crate::passes::assign::Arg; use crate::passes::select::Block; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub struct X86Concluded<'p> { diff --git a/compiler/src/passes/eliminate/eliminate.rs b/compiler/src/passes/eliminate/eliminate.rs index 7a45fcb..df0e598 100644 --- a/compiler/src/passes/eliminate/eliminate.rs +++ b/compiler/src/passes/eliminate/eliminate.rs @@ -3,7 +3,7 @@ use crate::passes::eliminate::eliminate_tail::eliminate_tail; use crate::passes::eliminate::{FunEliminated, PrgEliminated}; use crate::passes::explicate::PrgExplicated; use crate::time; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; // (Old variable name, field name) -> New variable name diff --git a/compiler/src/passes/eliminate/eliminate_params.rs b/compiler/src/passes/eliminate/eliminate_params.rs index 35f00b2..c33e121 100644 --- a/compiler/src/passes/eliminate/eliminate_params.rs +++ b/compiler/src/passes/eliminate/eliminate_params.rs @@ -1,7 +1,7 @@ use crate::passes::eliminate::eliminate::Ctx; use crate::passes::parse::types::Type; use crate::passes::parse::{Param, TypeDef}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub fn eliminate_params<'p>( diff --git a/compiler/src/passes/eliminate/eliminate_seq.rs b/compiler/src/passes/eliminate/eliminate_seq.rs index f4cbac2..f91b6d0 100644 --- a/compiler/src/passes/eliminate/eliminate_seq.rs +++ b/compiler/src/passes/eliminate/eliminate_seq.rs @@ -6,7 +6,7 @@ use crate::passes::eliminate::{ExprEliminated, TailEliminated}; use crate::passes::explicate::ExprExplicated; use crate::passes::parse::types::Type; use crate::passes::parse::{Meta, TypeDef, Typed}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub fn eliminate_seq<'p>( diff --git a/compiler/src/passes/eliminate/eliminate_tail.rs b/compiler/src/passes/eliminate/eliminate_tail.rs index a2e9998..b219828 100644 --- a/compiler/src/passes/eliminate/eliminate_tail.rs +++ b/compiler/src/passes/eliminate/eliminate_tail.rs @@ -6,7 +6,7 @@ use crate::passes::eliminate::eliminate_seq::eliminate_seq; use crate::passes::eliminate::TailEliminated; use crate::passes::explicate::TailExplicated; use crate::passes::parse::TypeDef; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use functor_derive::Functor; use std::collections::HashMap; diff --git a/compiler/src/passes/eliminate/mod.rs b/compiler/src/passes/eliminate/mod.rs index 2a1aab0..6594e0e 100644 --- a/compiler/src/passes/eliminate/mod.rs +++ b/compiler/src/passes/eliminate/mod.rs @@ -8,7 +8,7 @@ use crate::passes::atomize::Atom; use crate::passes::parse::types::Type; use crate::passes::parse::{BinaryOp, Meta, Param, TypeDef, UnaryOp}; use crate::passes::select::InstrSelected; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub struct PrgEliminated<'p> { diff --git a/compiler/src/passes/emit/mod.rs b/compiler/src/passes/emit/mod.rs index 1cb2189..8eefdd5 100644 --- a/compiler/src/passes/emit/mod.rs +++ b/compiler/src/passes/emit/mod.rs @@ -16,7 +16,7 @@ use crate::passes::emit::push_pop::{encode_push_pop, POPQ_INFO, PUSHQ_INFO}; use crate::passes::emit::special::encode_setcc; use crate::passes::emit::unary::{encode_unary_instr, CALLQ_INDIRECT_INFO, NEGQ_INFO}; use crate::passes::select::{Block, Cnd, Instr, Reg}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use crate::{imm32, time}; use std::collections::HashMap; diff --git a/compiler/src/passes/explicate/explicate.rs b/compiler/src/passes/explicate/explicate.rs index 5a47181..cdfa0cd 100644 --- a/compiler/src/passes/explicate/explicate.rs +++ b/compiler/src/passes/explicate/explicate.rs @@ -1,7 +1,7 @@ use crate::passes::atomize::{DefAtomized, PrgAtomized}; use crate::passes::explicate::explicate_tail::explicate_tail; use crate::passes::explicate::{FunExplicated, PrgExplicated, TailExplicated}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use crate::{display, time}; use std::collections::HashMap; diff --git a/compiler/src/passes/explicate/explicate_assign.rs b/compiler/src/passes/explicate/explicate_assign.rs index 30ce4f0..f362593 100644 --- a/compiler/src/passes/explicate/explicate_assign.rs +++ b/compiler/src/passes/explicate/explicate_assign.rs @@ -3,7 +3,7 @@ use crate::passes::explicate::explicate::Env; use crate::passes::explicate::{explicate_pred, ExprExplicated, TailExplicated}; use crate::passes::parse::{Lit, Meta, Typed}; -use crate::utils::gen_sym::{gen_sym, UniqueSym}; +use crate::utils::unique_sym::{gen_sym, UniqueSym}; pub fn explicate_assign<'p>( sym: UniqueSym<'p>, diff --git a/compiler/src/passes/explicate/explicate_pred.rs b/compiler/src/passes/explicate/explicate_pred.rs index 15bea9f..f4f5c9b 100644 --- a/compiler/src/passes/explicate/explicate_pred.rs +++ b/compiler/src/passes/explicate/explicate_pred.rs @@ -4,7 +4,7 @@ use crate::passes::explicate::explicate_assign::explicate_assign; use crate::passes::explicate::{ExprExplicated, TailExplicated}; use crate::passes::parse::types::Type; use crate::passes::parse::{BinaryOp, Lit, Meta, UnaryOp}; -use crate::utils::gen_sym::gen_sym; +use crate::utils::unique_sym::gen_sym; pub fn explicate_pred<'p>( cnd: AExpr<'p>, diff --git a/compiler/src/passes/explicate/explicate_tail.rs b/compiler/src/passes/explicate/explicate_tail.rs index 75b8dee..529c92e 100644 --- a/compiler/src/passes/explicate/explicate_tail.rs +++ b/compiler/src/passes/explicate/explicate_tail.rs @@ -4,7 +4,7 @@ use crate::passes::explicate::explicate_assign::explicate_assign; use crate::passes::explicate::TailExplicated; use crate::passes::parse::{Meta, Typed}; -use crate::utils::gen_sym::gen_sym; +use crate::utils::unique_sym::gen_sym; pub fn explicate_tail<'p>(expr: Typed<'p, AExpr<'p>>, env: &mut Env<'_, 'p>) -> TailExplicated<'p> { let tmp = gen_sym("return"); diff --git a/compiler/src/passes/explicate/mod.rs b/compiler/src/passes/explicate/mod.rs index 76c3213..a3fcf75 100644 --- a/compiler/src/passes/explicate/mod.rs +++ b/compiler/src/passes/explicate/mod.rs @@ -8,7 +8,7 @@ use crate::passes::atomize::Atom; use crate::passes::parse::types::Type; use crate::passes::parse::{BinaryOp, Param, TypeDef, Typed, UnaryOp}; use crate::passes::select::InstrSelected; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use itertools::Itertools; use std::collections::HashMap; diff --git a/compiler/src/passes/parse/mod.rs b/compiler/src/passes/parse/mod.rs index 4af7236..c78f43a 100644 --- a/compiler/src/passes/parse/mod.rs +++ b/compiler/src/passes/parse/mod.rs @@ -12,7 +12,7 @@ pub mod types; use crate::passes::select::{Instr, VarArg}; use crate::passes::validate::MetaConstrained; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use functor_derive::Functor; use itertools::Itertools; diff --git a/compiler/src/passes/patch/mod.rs b/compiler/src/passes/patch/mod.rs index 205dff3..c94eca1 100644 --- a/compiler/src/passes/patch/mod.rs +++ b/compiler/src/passes/patch/mod.rs @@ -2,7 +2,7 @@ pub mod patch; use crate::passes::assign::FunAssigned; use crate::passes::select::X86Selected; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use functor_derive::Functor; use std::collections::HashMap; diff --git a/compiler/src/passes/patch/patch.rs b/compiler/src/passes/patch/patch.rs index b1ac171..6c43dbb 100644 --- a/compiler/src/passes/patch/patch.rs +++ b/compiler/src/passes/patch/patch.rs @@ -1,7 +1,7 @@ use crate::passes::assign::{Arg, FunAssigned, X86Assigned}; use crate::passes::patch::X86Patched; use crate::passes::select::{Block, Instr}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use crate::{addq, movq, popq, pushq, reg, subq, time}; use functor_derive::Functor; diff --git a/compiler/src/passes/reveal/mod.rs b/compiler/src/passes/reveal/mod.rs index 58de654..2baf2a0 100644 --- a/compiler/src/passes/reveal/mod.rs +++ b/compiler/src/passes/reveal/mod.rs @@ -4,7 +4,7 @@ pub mod reveal; use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp}; use crate::passes::select::InstrSelected; use crate::passes::validate::Int; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use itertools::Itertools; use std::collections::HashMap; diff --git a/compiler/src/passes/reveal/reveal.rs b/compiler/src/passes/reveal/reveal.rs index 1a431e1..b785e01 100644 --- a/compiler/src/passes/reveal/reveal.rs +++ b/compiler/src/passes/reveal/reveal.rs @@ -1,8 +1,8 @@ use crate::passes::parse::{Meta, Typed}; use crate::passes::reveal::{DefRevealed, PrgRevealed, RExpr}; use crate::passes::validate::{DefValidated, ExprValidated, PrgValidated}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; use crate::{display, time}; impl<'p> PrgValidated<'p> { diff --git a/compiler/src/passes/select/mod.rs b/compiler/src/passes/select/mod.rs index d419679..bd06d64 100644 --- a/compiler/src/passes/select/mod.rs +++ b/compiler/src/passes/select/mod.rs @@ -2,7 +2,7 @@ mod display; pub mod macros; pub mod select; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use functor_derive::Functor; use itertools::Itertools; diff --git a/compiler/src/passes/select/select.rs b/compiler/src/passes/select/select.rs index 0dad46d..3be78fe 100644 --- a/compiler/src/passes/select/select.rs +++ b/compiler/src/passes/select/select.rs @@ -7,7 +7,7 @@ use crate::passes::select::{ CALLER_SAVED, }; use crate::passes::validate::Int; -use crate::utils::gen_sym::{gen_sym, UniqueSym}; +use crate::utils::unique_sym::{gen_sym, UniqueSym}; use crate::*; use std::collections::HashMap; diff --git a/compiler/src/passes/validate/check_sized.rs b/compiler/src/passes/validate/check_sized.rs index c61adb8..bcbde1c 100644 --- a/compiler/src/passes/validate/check_sized.rs +++ b/compiler/src/passes/validate/check_sized.rs @@ -2,7 +2,7 @@ use crate::passes::parse::types::Type; use crate::passes::parse::{Def, TypeDef}; use crate::passes::validate::error::TypeError; use crate::passes::validate::PrgConstrained; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use petgraph::algo::toposort; use petgraph::prelude::GraphMap; use petgraph::Directed; diff --git a/compiler/src/passes/validate/constrain/apply.rs b/compiler/src/passes/validate/constrain/apply.rs index 7ad4584..98e7f2e 100644 --- a/compiler/src/passes/validate/constrain/apply.rs +++ b/compiler/src/passes/validate/constrain/apply.rs @@ -4,7 +4,6 @@ use crate::passes::validate::constrain::uncover_globals::Env; use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{ExprConstrained, ExprUniquified, MetaConstrained}; -use crate::utils::expect::expect; pub fn constrain_apply<'p>( env: &mut Env<'_, 'p>, @@ -26,14 +25,13 @@ pub fn constrain_apply<'p>( }); }; - expect( - params.len() == args.len(), - TypeError::ArgCountMismatch { + if params.len() != args.len() { + return Err(TypeError::ArgCountMismatch { got: args.len(), expected: params.len(), span, // todo: maybe highlight only the args and params? - }, - )?; + }); + } for (arg, param_type) in args.iter().zip(params.iter()) { env.uf diff --git a/compiler/src/passes/validate/constrain/assign.rs b/compiler/src/passes/validate/constrain/assign.rs index 7741ba0..e7c41c3 100644 --- a/compiler/src/passes/validate/constrain/assign.rs +++ b/compiler/src/passes/validate/constrain/assign.rs @@ -5,8 +5,7 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::error::TypeError::MismatchedAssignBinding; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{ExprConstrained, ExprUniquified, MetaConstrained}; -use crate::utils::expect::expect; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; pub fn constrain_assign<'p>( env: &mut Env<'_, 'p>, @@ -20,7 +19,9 @@ pub fn constrain_assign<'p>( return Err(TypeError::SymbolShouldBeVariable { span: sym.meta }); }; - expect(mutable, TypeError::ModifyImmutable { span: sym.meta })?; + if !mutable { + return Err(TypeError::ModifyImmutable { span: sym.meta }); + } env.uf .expect_equal(typ, bnd.meta.index, |sym_typ, bnd_type| { diff --git a/compiler/src/passes/validate/constrain/continue.rs b/compiler/src/passes/validate/constrain/continue.rs index 21aa189..31545a3 100644 --- a/compiler/src/passes/validate/constrain/continue.rs +++ b/compiler/src/passes/validate/constrain/continue.rs @@ -3,16 +3,14 @@ use crate::passes::validate::constrain::uncover_globals::Env; use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{ExprConstrained, MetaConstrained}; -use crate::utils::expect::expect; pub fn constrain_continue<'p>( env: &mut Env<'_, 'p>, span: Span, ) -> Result>, TypeError> { - expect( - env.loop_type.is_some(), - TypeError::ContinueOutsideLoop { span }, - )?; + if env.loop_type.is_none() { + return Err(TypeError::ContinueOutsideLoop { span }); + } Ok(Constrained { meta: MetaConstrained { diff --git a/compiler/src/passes/validate/constrain/def.rs b/compiler/src/passes/validate/constrain/def.rs index 5ba6035..9971fa8 100644 --- a/compiler/src/passes/validate/constrain/def.rs +++ b/compiler/src/passes/validate/constrain/def.rs @@ -4,8 +4,8 @@ use crate::passes::validate::constrain::uncover_globals::{Env, EnvEntry}; use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{DefConstrained, DefUniquified}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::UnionFind; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub fn constrain_def<'p>( diff --git a/compiler/src/passes/validate/constrain/let.rs b/compiler/src/passes/validate/constrain/let.rs index da1f2ab..8345f25 100644 --- a/compiler/src/passes/validate/constrain/let.rs +++ b/compiler/src/passes/validate/constrain/let.rs @@ -4,7 +4,7 @@ use crate::passes::validate::constrain::expr; use crate::passes::validate::constrain::uncover_globals::{Env, EnvEntry}; use crate::passes::validate::error::TypeError; use crate::passes::validate::{ExprConstrained, ExprUniquified, MetaConstrained}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; pub fn constrain_let<'p>( env: &mut Env<'_, 'p>, diff --git a/compiler/src/passes/validate/constrain/mod.rs b/compiler/src/passes/validate/constrain/mod.rs index 21c7f8a..e7b2578 100644 --- a/compiler/src/passes/validate/constrain/mod.rs +++ b/compiler/src/passes/validate/constrain/mod.rs @@ -5,8 +5,8 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::uniquify::PrgUniquified; use crate::passes::validate::{partial_type, PrgConstrained}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::{UnionFind, UnionIndex}; +use crate::utils::unique_sym::UniqueSym; use uncover_globals::uncover_globals; mod access_field; diff --git a/compiler/src/passes/validate/constrain/struct.rs b/compiler/src/passes/validate/constrain/struct.rs index fd9c099..5b7ca15 100644 --- a/compiler/src/passes/validate/constrain/struct.rs +++ b/compiler/src/passes/validate/constrain/struct.rs @@ -4,8 +4,7 @@ use crate::passes::validate::constrain::uncover_globals::{Env, EnvEntry}; use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{ExprConstrained, ExprUniquified, MetaConstrained}; -use crate::utils::expect::expect; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; use std::collections::{HashMap, HashSet}; pub fn constrain_struct<'p>( @@ -36,13 +35,12 @@ pub fn constrain_struct<'p>( .map(|(field_sym, field_bnd)| { let field_bnd = expr::constrain_expr(field_bnd, env)?; - expect( - seen_fields.insert(field_sym.inner), - TypeError::ConstructDuplicateField { + if !seen_fields.insert(field_sym.inner) { + return Err(TypeError::ConstructDuplicateField { sym: field_sym.to_string(), span: field_sym.meta, - }, - )?; + }); + } let Some((def_span, def_typ)) = def_fields.get(field_sym.inner) else { return Err(TypeError::UnknownStructField { @@ -68,14 +66,13 @@ pub fn constrain_struct<'p>( // Verify that all fields from the struct definition are present. for (def_sym, (def_span, _)) in def_fields { - expect( - seen_fields.contains(def_sym), - TypeError::ConstructMissingField { + if !seen_fields.contains(def_sym) { + return Err(TypeError::ConstructMissingField { sym: def_sym.to_string(), struct_span: sym.meta, def_span, - }, - )?; + }); + } } let index = env.uf.add(PartialType::Var { sym: sym.inner }); diff --git a/compiler/src/passes/validate/constrain/uncover_globals.rs b/compiler/src/passes/validate/constrain/uncover_globals.rs index 13cd4cc..9ef20e7 100644 --- a/compiler/src/passes/validate/constrain/uncover_globals.rs +++ b/compiler/src/passes/validate/constrain/uncover_globals.rs @@ -4,8 +4,8 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::uniquify::PrgUniquified; use crate::passes::validate::DefUniquified; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::{UnionFind, UnionIndex}; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; diff --git a/compiler/src/passes/validate/constrain/var.rs b/compiler/src/passes/validate/constrain/var.rs index eb1dba1..8405692 100644 --- a/compiler/src/passes/validate/constrain/var.rs +++ b/compiler/src/passes/validate/constrain/var.rs @@ -2,7 +2,7 @@ use crate::passes::parse::{Constrained, Span, Spanned}; use crate::passes::validate::constrain::uncover_globals::{Env, EnvEntry}; use crate::passes::validate::error::TypeError; use crate::passes::validate::{ExprConstrained, MetaConstrained}; -use crate::utils::gen_sym::UniqueSym; +use crate::utils::unique_sym::UniqueSym; pub fn constrain_var<'p>( env: &Env<'_, 'p>, diff --git a/compiler/src/passes/validate/mod.rs b/compiler/src/passes/validate/mod.rs index e560b5d..f57a10e 100644 --- a/compiler/src/passes/validate/mod.rs +++ b/compiler/src/passes/validate/mod.rs @@ -11,8 +11,8 @@ pub mod validate; use crate::passes::parse::types::Type; use crate::passes::parse::{Constrained, Def, Expr, Lit, Span, Spanned, Typed}; use crate::passes::select::{Instr, VarArg}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::{UnionFind, UnionIndex}; +use crate::utils::unique_sym::UniqueSym; use derive_more::Display; use itertools::Itertools; use partial_type::PartialType; diff --git a/compiler/src/passes/validate/partial_type.rs b/compiler/src/passes/validate/partial_type.rs index b4dd3a8..6ef9b8f 100644 --- a/compiler/src/passes/validate/partial_type.rs +++ b/compiler/src/passes/validate/partial_type.rs @@ -1,6 +1,6 @@ use crate::passes::parse::types::IntType; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::{UnionFind, UnionIndex}; +use crate::utils::unique_sym::UniqueSym; use itertools::Itertools; #[derive(Debug, Clone)] diff --git a/compiler/src/passes/validate/resolve.rs b/compiler/src/passes/validate/resolve.rs index a10576f..2de857d 100644 --- a/compiler/src/passes/validate/resolve.rs +++ b/compiler/src/passes/validate/resolve.rs @@ -6,8 +6,8 @@ use crate::passes::validate::partial_type::PartialType; use crate::passes::validate::{ DefConstrained, DefValidated, ExprConstrained, ExprValidated, Int, PrgConstrained, PrgValidated, }; -use crate::utils::gen_sym::UniqueSym; use crate::utils::union_find::{UnionFind, UnionIndex}; +use crate::utils::unique_sym::UniqueSym; use crate::*; use functor_derive::Functor; use std::num::ParseIntError; diff --git a/compiler/src/passes/validate/tests.rs b/compiler/src/passes/validate/tests.rs index dfd6853..83a9fb3 100644 --- a/compiler/src/passes/validate/tests.rs +++ b/compiler/src/passes/validate/tests.rs @@ -1,3 +1,4 @@ +use crate::apped_std; use crate::passes::parse::parse::parse; use crate::utils::split_test::split_test; use derive_name::VariantName; @@ -5,12 +6,15 @@ use miette::{NamedSource, Report}; use test_each_file::test_each_file; fn validate([test]: [&str; 1], good: bool) { - let (_input, _expected_output, _expected_return, expected_error) = split_test(test); + let (_, _, _, expected_error) = split_test(test); + + // Sanity check. Successes should not have expected errors. assert_eq!(good, expected_error.is_none()); - let result = parse(test).unwrap().validate(); + // todo: make a module system so we don't have to append the std-library to the source. + let test = apped_std(test); - match (result, expected_error) { + match (parse(test).unwrap().validate(), expected_error) { (Ok(_), None) => {} (Ok(_), Some(expected_error)) => { panic!("Expected validation to fail with: {expected_error}.") diff --git a/compiler/src/passes/validate/uniquify/def.rs b/compiler/src/passes/validate/uniquify/def.rs index 3be29dc..53c1f3b 100644 --- a/compiler/src/passes/validate/uniquify/def.rs +++ b/compiler/src/passes/validate/uniquify/def.rs @@ -3,8 +3,8 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::uniquify::r#fn::uniquify_fn; use crate::passes::validate::uniquify::typedef::uniquify_typedef; use crate::passes::validate::DefUniquified; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; pub fn uniquify_def<'p>( def: DefParsed<'p>, diff --git a/compiler/src/passes/validate/uniquify/expr.rs b/compiler/src/passes/validate/uniquify/expr.rs index ae510ae..8a32505 100644 --- a/compiler/src/passes/validate/uniquify/expr.rs +++ b/compiler/src/passes/validate/uniquify/expr.rs @@ -4,8 +4,8 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::uniquify::r#type::uniquify_type; use crate::passes::validate::uniquify::{gen_spanned_sym, try_get}; use crate::passes::validate::{uniquify, ExprUniquified, InstrUniquified}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; use crate::*; pub fn uniquify_expr<'p>( diff --git a/compiler/src/passes/validate/uniquify/fn.rs b/compiler/src/passes/validate/uniquify/fn.rs index 5262ec4..d6f5e23 100644 --- a/compiler/src/passes/validate/uniquify/fn.rs +++ b/compiler/src/passes/validate/uniquify/fn.rs @@ -5,8 +5,8 @@ use crate::passes::validate::uniquify::expr::uniquify_expr; use crate::passes::validate::uniquify::gen_spanned_sym; use crate::passes::validate::uniquify::r#type::uniquify_type; use crate::passes::validate::{uniquify, DefUniquified}; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; use std::collections::HashMap; pub fn uniquify_fn<'p>( diff --git a/compiler/src/passes/validate/uniquify/mod.rs b/compiler/src/passes/validate/uniquify/mod.rs index 5732b25..9d88d39 100644 --- a/compiler/src/passes/validate/uniquify/mod.rs +++ b/compiler/src/passes/validate/uniquify/mod.rs @@ -2,8 +2,8 @@ use crate::passes::parse::{Meta, PrgParsed, Spanned}; use crate::passes::validate::error::TypeError; use crate::passes::validate::error::TypeError::{NoMain, UndeclaredVar}; use crate::passes::validate::DefUniquified; -use crate::utils::gen_sym::{gen_sym, UniqueSym}; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::{gen_sym, UniqueSym}; mod def; mod expr; diff --git a/compiler/src/passes/validate/uniquify/type.rs b/compiler/src/passes/validate/uniquify/type.rs index c693799..a92f7c5 100644 --- a/compiler/src/passes/validate/uniquify/type.rs +++ b/compiler/src/passes/validate/uniquify/type.rs @@ -2,8 +2,8 @@ use crate::passes::parse::types::Type; use crate::passes::parse::Spanned; use crate::passes::validate::error::TypeError; use crate::passes::validate::uniquify::try_get; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; pub fn uniquify_type<'p>( typ: Type>, diff --git a/compiler/src/passes/validate/uniquify/typedef.rs b/compiler/src/passes/validate/uniquify/typedef.rs index ed72763..2997bb7 100644 --- a/compiler/src/passes/validate/uniquify/typedef.rs +++ b/compiler/src/passes/validate/uniquify/typedef.rs @@ -3,8 +3,8 @@ use crate::passes::validate::error::TypeError; use crate::passes::validate::uniquify::r#type::uniquify_type; use crate::passes::validate::uniquify::try_get; use crate::passes::validate::DefUniquified; -use crate::utils::gen_sym::UniqueSym; use crate::utils::push_map::PushMap; +use crate::utils::unique_sym::UniqueSym; pub fn uniquify_typedef<'p>( scope: &mut PushMap<&'p str, UniqueSym<'p>>, diff --git a/compiler/src/utils/expect.rs b/compiler/src/utils/expect.rs deleted file mode 100644 index 75fa6d7..0000000 --- a/compiler/src/utils/expect.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub fn expect(b: bool, err: T) -> Result<(), T> { - b.then_some(()).ok_or(err) -} diff --git a/compiler/src/utils/mod.rs b/compiler/src/utils/mod.rs index 6c66e28..8a02900 100644 --- a/compiler/src/utils/mod.rs +++ b/compiler/src/utils/mod.rs @@ -1,8 +1,5 @@ #![allow(unused)] -pub mod expect; -pub mod gen_sym; pub mod push_map; pub mod split_test; -#[cfg(test)] -pub mod test_runtime; pub mod union_find; +pub mod unique_sym; diff --git a/compiler/src/utils/test_runtime.rs b/compiler/src/utils/test_runtime.rs deleted file mode 100644 index b66833b..0000000 --- a/compiler/src/utils/test_runtime.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::passes::select::{FunSelected, X86Selected}; -use crate::utils::gen_sym::gen_sym; -use crate::*; -use std::collections::HashMap; - -/// Adds runtime for testing. -pub fn add_runtime(program: &mut X86Selected) { - let runtime = gen_sym("runtime"); - let entry = gen_sym("entry"); - let exit = gen_sym("exit"); - - let entry_block = block!(callq_direct!(program.entry, 0), jmp!(exit)); - let exit_block = block!( - movq!(reg!(RAX), reg!(RDI)), - movq!(imm32!(0x3C), reg!(RAX)), - syscall!(2) - ); - - let runtime_fn = FunSelected { - blocks: HashMap::from([(entry, entry_block), (exit, exit_block)]), - entry, - exit, - }; - - program.fns.insert(runtime, runtime_fn); - program.entry = runtime; -} diff --git a/compiler/src/utils/gen_sym.rs b/compiler/src/utils/unique_sym.rs similarity index 100% rename from compiler/src/utils/gen_sym.rs rename to compiler/src/utils/unique_sym.rs diff --git a/compiler/tests/integration.rs b/compiler/tests/integration.rs index 09a3910..316d9a9 100644 --- a/compiler/tests/integration.rs +++ b/compiler/tests/integration.rs @@ -1,46 +1,29 @@ #![cfg(unix)] -use compiler::passes::parse::parse::parse; +use compiler::compile; use compiler::utils::split_test::{split_test, str_to_int}; -use std::fs::OpenOptions; +use miette::IntoDiagnostic; use std::io::{BufRead, Write}; -use std::os::unix::prelude::OpenOptionsExt; use std::process::{Command, Stdio}; use tempfile::TempDir; use test_each_file::test_each_file; fn integration([test]: [&str; 1]) { - let tempdir = TempDir::with_prefix("rust-compiler-construction-integration").unwrap(); - let (input, expected_output, expected_return, _) = split_test(test); - let expected_return: i64 = expected_return.into(); - let input_path = tempdir.path().join("output"); - let mut output = OpenOptions::new() - .write(true) - .create(true) - .mode(0o777) - .open(input_path) - .unwrap(); + let tempdir = TempDir::with_prefix("rust-compiler-construction-integration").unwrap(); - parse(test) - .unwrap() - .validate() - .unwrap() - .reveal() - .atomize() - .explicate() - .eliminate() - .select() - .assign() - .patch() - .conclude() - .emit() - .write(&mut output); + compile(test, "", &tempdir.path().join("output")).unwrap(); - drop(output); + Command::new("chmod") + .current_dir(&tempdir) + .arg("+x") + .arg("./output") + .output() + .into_diagnostic() + .unwrap(); - // Wait for file to be readable + // Wait for file to be readable. let mut program; loop { let sub_res = Command::new("./output")