Skip to content

Commit

Permalink
Test infrastructure and CI (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
DasLixou authored May 9, 2024
1 parent 16789a9 commit 7c1296b
Show file tree
Hide file tree
Showing 9 changed files with 1,076 additions and 16 deletions.
32 changes: 32 additions & 0 deletions ohdlc/.github/workflows/compiler_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "Compiler CI"

on:
push:
paths:
- "ohdlc/**"
- ".github/workflows/**"
pull_request:
paths:
- "ohdlc/**"
- ".github/workflows/**"
merge_group:

env:
CARGO_TERM_COLOR: always

jobs:
build_and_test:
name: "[Compiler] Cargo Build & Test"
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- stable
defaults:
run:
working-directory: ohdlc
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose
- run: cargo test --verbose
43 changes: 43 additions & 0 deletions ohdlc/Cargo.lock

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

3 changes: 3 additions & 0 deletions ohdlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ lasso = { version = "0.7.2", features = ["multi-threaded"] }
logos = "0.14.0"
once_cell = "1.19.0"
surotto = { git = "https://github.com/DasLixou/surotto" }

[dev-dependencies]
insta = "1.38.0"
17 changes: 17 additions & 0 deletions ohdlc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![deny(rust_2018_idioms)]

use message::Messages;

// TODO: can we get unused errors back? :3
pub mod ast;
pub mod ir;
pub mod lexer;
pub mod message;
pub mod parser;
pub mod span;
pub mod symbol;

pub static MESSAGES: Messages = Messages::new();

#[derive(Clone)]
pub struct Source<'s>(pub String, pub &'s str);
19 changes: 3 additions & 16 deletions ohdlc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

use ariadne::{Label, Report};
use bumpalo::Bump;
use message::Messages;
use parser::Parser;

use crate::{
use ohdlc::{
ir::{
import_bucket::ImportBucket,
name_lookup::NameLookup,
Expand All @@ -15,21 +13,10 @@ use crate::{
},
},
lexer::Lexer,
parser::Parser,
Source, MESSAGES,
};

mod ast;
mod ir;
mod lexer;
mod message;
mod parser;
mod span;
mod symbol;

static MESSAGES: Messages = Messages::new();

#[derive(Clone)]
pub struct Source<'s>(pub String, pub &'s str);

fn main() -> Result<(), ()> {
let source = Source("work.ohd".to_owned(), include_str!("work.ohd"));

Expand Down
78 changes: 78 additions & 0 deletions ohdlc/tests/basic.ohd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
mod std {
enum Logical {
High,
Low,
}
}
use ::std::Logical;

mod x {
use y;
}

mod y {
use x;
}

record MyRecord {
a: Logical,
b: Logical,
}

enum MyEnum {
A,
B,
C,
D,
E,
}

entity HalfAdder {
in a: Logical,
in b: Logical,
out o: Logical,
out c: Logical,
}

arch RTL for HalfAdder {
o <= a xor b;
c <= a and b;
}

use a::b::Lol;

mod a {
use other::b;
}

mod other {
mod b {
entity Lol { }
}
}

entity FullAdder {
in a: Logical,
in b: Logical,
in ci: Logical,
out o: Logical,
out co: Logical,
}

arch RTL for FullAdder {
HalfAdder(RTL) {
a <= a,
b <= b,
o => signal s,
c => signal cb,
}

HalfAdder(RTL) {
a <= s,
b <= ci,
o => o,
c => signal cc,
}

co <= cb or cc;
}
109 changes: 109 additions & 0 deletions ohdlc/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use ariadne::{Label, Report};
use bumpalo::Bump;
use insta::assert_debug_snapshot;
use ohdlc::{
ir::{
import_bucket::ImportBucket,
name_lookup::NameLookup,
registry::Registry,
stages::{
flatten_lookup::FlattenLookupStage, refine_types::RefineTypesStage, rough::RoughStage,
},
},
lexer::Lexer,
parser::Parser,
Source, MESSAGES,
};

#[test]
fn main() {
let source = Source("work.ohd".to_owned(), include_str!("basic.ohd"));

println!("[STAGE] Lexer");

let lexer = Lexer::new(&source.1);
report_messages(&source);
let lexer = lexer.unwrap();

println!("[STAGE] Parser");

let parser_arena = Bump::new();

let mut parser = Parser::new(&parser_arena, source.clone(), lexer);

let root = parser.parse();
let root = match root {
Ok(tree) => tree,
Err(messages) => {
MESSAGES.extend(messages);
report_messages(&source);
panic!();
}
};

assert_debug_snapshot!(root);

let ir_arena = Bump::new();
let mut registry = Registry::default();
let mut name_lookup = NameLookup::new();
let mut import_bucket = ImportBucket::new();

{
let rough = RoughStage {
arena: &ir_arena,
registry: &mut registry,
name_lookup: &mut name_lookup,
import_bucket: &mut import_bucket,
root: &root,
};
rough.lower();
report_messages(&source);
}

let name_lookup = {
let resolve = FlattenLookupStage {
registry: &registry,
name_lookup,
import_bucket,
resolvables: Vec::new(),
};
let lookup = resolve.lower();
report_messages(&source);
lookup
};
let name_lookup = name_lookup.unwrap();

let refined_types = {
let refine_types = RefineTypesStage {
arena: &ir_arena,
name_lookup: &name_lookup,
module_registry: &registry.modules,
};
let refined_types = refine_types.lower(registry.types);
report_messages(&source);
Registry {
modules: registry.modules,
types: refined_types,
}
};

assert_debug_snapshot!(refined_types);
}

fn report_messages(source: &Source<'_>) {
MESSAGES.drain(|msg| {
let filename = source.0.as_str();

let report =
Report::build(msg.kind, filename, msg.location.0)
.with_message(msg.message)
.with_labels(msg.labels.into_iter().map(|label| {
Label::new((filename, label.span.into())).with_message(label.message)
}))
.finish();

report
.eprint((filename, ariadne::Source::from(source.1)))
.unwrap();
});
}
Loading

0 comments on commit 7c1296b

Please sign in to comment.