Skip to content

Commit

Permalink
AST Validation - Function Declarators/Types (#80)
Browse files Browse the repository at this point in the history
- Add `AstNodeKind::ParamDecl`
- Finish initial code for `validate_declarations`
- Make type printing a little nicer
- Make `Symbol` not an `enum`
- Add `id` field to `Ast` and use it to write `rebuild_indices` function
- "Parallelized" `validate_declarations`
  • Loading branch information
A1Liu authored May 9, 2023
1 parent 2a29195 commit 096fab3
Show file tree
Hide file tree
Showing 17 changed files with 1,050 additions and 415 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[alias]
wasm = "build --target wasm32-unknown-unknown"
run-test = "run --bin run-test"
run-test-release = "run --release --bin run-test"

[build]
target-dir = ".cargo/target"
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ soa_derive = { git = "https://github.com/lumol-org/soa-derive" }
compiler = { path = "../compiler" }
codespan-reporting = "0.11.1"
clap = { version = "4.2.4", features = ["derive"] }
rayon = "1.7.0"

[dev-dependencies]
ntest = "0.9.0"
Expand Down
44 changes: 44 additions & 0 deletions compiler-tests/pass/func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
int main(int argc, char** argv) {}

int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
int asdf(int a) {}
45 changes: 34 additions & 11 deletions compiler-tests/src/bin/run-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate compiler;
use clap::Parser;
use codespan_reporting::term::termcolor::*;
use codespan_reporting::term::*;
use compiler::{parse_test_case, single_file_db, StageOutput};
use compiler::{api::display_tree, parse_test_case, single_file_db, StageOutput};

#[derive(clap::ValueEnum, Clone, Copy, PartialEq)]
enum Stage {
Expand All @@ -15,13 +15,18 @@ enum Stage {
Validate,
}

const STAGES: &[Stage] = &[Stage::Lex, Stage::Macro, Stage::Parse, Stage::Validate];

/// Run
#[derive(Parser)]
#[clap(author = "Albert Liu", about = "Test runner for TCI.")]
struct Cli {
#[clap(help = "a path to a test case")]
test_case: std::path::PathBuf,

#[clap(long, help = "parallelism for the compiler")]
parallel: Option<u8>,

#[clap(long, help = "print a nested version of the AST")]
print_ast: bool,

Expand All @@ -38,6 +43,9 @@ Examples:
#[arg(value_enum)]
ignore: Vec<Stage>,

#[clap(long, help = "run the compiler, but ignore all stage outputs")]
ignore_all: bool,

#[clap(long, help = "the only stage that should run")]
#[arg(value_enum)]
only: Option<Stage>,
Expand All @@ -54,12 +62,21 @@ Examples:
}

fn main() {
let main_begin = std::time::Instant::now();

// Rust backtraces are useful and it seems dumb to disable them by default, especially in debug mode.
#[cfg(debug_assertions)]
std::env::set_var("RUST_BACKTRACE", "1");

let mut args = Cli::parse();

if let Some(threads) = args.parallel {
rayon::ThreadPoolBuilder::new()
.num_threads(threads as usize)
.build_global()
.unwrap();
}

let test_case =
std::fs::read_to_string(&args.test_case).expect("file should exist and be a valid string");

Expand All @@ -68,19 +85,21 @@ fn main() {

let (source, expected) = parse_test_case(&test_case);

let begin = std::time::Instant::now();

let (db, file_id) = single_file_db(source.to_string());

let mut result = compiler::run_compiler_for_testing(&db, file_id);

if let Some(only) = args.only {
for stage in [Stage::Lex, Stage::Macro, Stage::Parse, Stage::Validate] {
if stage == only {
continue;
}
let elapsed = begin.elapsed();

args.ignore.push(stage);
}
}
args.ignore = if args.ignore_all {
STAGES.iter().map(|s| *s).collect()
} else if let Some(only) = args.only {
STAGES.iter().map(|s| *s).filter(|s| *s != only).collect()
} else {
args.ignore
};

for stage in args.ignore {
match stage {
Expand All @@ -104,9 +123,9 @@ fn main() {
}

if let (StageOutput::Ok(ast), true) = (&result.ast_validation, args.print_ast) {
eprintln!("{}", compiler::ast::display_tree(ast));
eprintln!("{}", display_tree(ast, Some(&result.ty_db)));
} else if let (StageOutput::Ok(ast), true) = (&result.parsed_ast, args.print_ast) {
eprintln!("{}", compiler::ast::display_tree(ast));
eprintln!("{}", display_tree(ast, None));
}

assert_eq!(result, expected);
Expand All @@ -120,4 +139,8 @@ fn main() {
} else {
print!("{}", text);
}

eprintln!("");
eprintln!("Compiler Time: {:.3?}", elapsed);
eprintln!("Total Time: {:.3?}", main_begin.elapsed());
}
Loading

0 comments on commit 096fab3

Please sign in to comment.