-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Initial error testing and cleanup (#54)
* Add automated compile failure testing of files in bad-programs * Add compile failure tests for conflicting types, modifying immutable variables, and unbound identifiers * Cleaned up the resolver logging * Updated miette
- Loading branch information
Showing
24 changed files
with
271 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
# Not e.g. crates/codegen/allocator.wat | ||
*.wat | ||
*.wasm | ||
*.claw | ||
|
||
# The compiled publish script | ||
publish |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,43 @@ | ||
use claw_codegen::generate; | ||
use claw_common::{make_source, OkPretty}; | ||
use claw_parser::{parse, tokenize}; | ||
use claw_resolver::{resolve, wit::ResolvedWit}; | ||
use claw_codegen::{generate, GenerationError}; | ||
use claw_common::make_source; | ||
use claw_parser::{parse, tokenize, LexerError, ParserError}; | ||
use claw_resolver::{resolve, wit::ResolvedWit, ResolverError}; | ||
use wit_parser::Resolve; | ||
|
||
pub fn compile(source_name: String, source_code: &str, wit: Resolve) -> Option<Vec<u8>> { | ||
use miette::Diagnostic; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, Diagnostic)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
#[diagnostic(transparent)] | ||
Lexer(#[from] LexerError), | ||
|
||
#[error(transparent)] | ||
#[diagnostic(transparent)] | ||
Parser(#[from] ParserError), | ||
|
||
#[error(transparent)] | ||
#[diagnostic(transparent)] | ||
Resolver(#[from] ResolverError), | ||
|
||
#[error(transparent)] | ||
#[diagnostic(transparent)] | ||
Generator(#[from] GenerationError), | ||
} | ||
|
||
pub fn compile(source_name: String, source_code: &str, wit: Resolve) -> Result<Vec<u8>, Error> { | ||
let src = make_source(source_name.as_str(), source_code); | ||
|
||
let tokens = tokenize(src.clone(), source_code).ok_pretty()?; | ||
let tokens = tokenize(src.clone(), source_code)?; | ||
|
||
let ast = parse(src.clone(), tokens).ok_pretty()?; | ||
let ast = parse(src.clone(), tokens)?; | ||
|
||
let wit = ResolvedWit::new(wit); | ||
|
||
let resolved = resolve(src, ast, wit).ok_pretty()?; | ||
let resolved = resolve(src, ast, wit)?; | ||
|
||
let output = generate(&resolved)?; | ||
|
||
generate(&resolved).ok_pretty() | ||
Ok(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
func foo() { | ||
let a: u32 = 1; | ||
let b: u64 = 2; | ||
let c = a + b; | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/lib/tests/bad-programs/adding-conflicting-types.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
x Conflicting types inferred for expression type1 != type0 | ||
,-[adding-conflicting-types.claw:4:17] | ||
3 | let b: u64 = 2; | ||
4 | let c = a + b; | ||
: | | ||
: `-- This bit | ||
5 | } | ||
`---- |
7 changes: 7 additions & 0 deletions
7
crates/lib/tests/bad-programs/global-without-annotation.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
x Global variables must have explicit types annotated starting with ':' | ||
,-[global-without-annotation.claw:1:7] | ||
1 | let a = 0; | ||
: | | ||
: `-- Found Assign | ||
2 | | ||
`---- |
7 changes: 7 additions & 0 deletions
7
crates/lib/tests/bad-programs/global-without-initialization.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
x Global variables must be initialized starting with '=' | ||
,-[global-without-initialization.claw:1:11] | ||
1 | let a: u32; | ||
: | | ||
: `-- Found Semicolon | ||
2 | | ||
`---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
x Unable to tokenize input | ||
,-[invalid-token.claw:1:10] | ||
1 | func foo($a: u32) -> u32 { | ||
: | | ||
: `-- Here | ||
2 | return $a; | ||
`---- |
5 changes: 5 additions & 0 deletions
5
crates/lib/tests/bad-programs/modifying-immutable-global.claw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let a: u32 = 1; | ||
|
||
func foo() { | ||
a = 2; | ||
} |
12 changes: 12 additions & 0 deletions
12
crates/lib/tests/bad-programs/modifying-immutable-global.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
x Assigned to immutable variable "a" | ||
,-[modifying-immutable-global.claw:1:5] | ||
1 | let a: u32 = 1; | ||
: | | ||
: `-- Defined here | ||
2 | | ||
3 | func foo() { | ||
4 | a = 2; | ||
: | | ||
: `-- Assigned here | ||
5 | } | ||
`---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
func foo() { | ||
let a: u32 = 1; | ||
a = 2; | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/lib/tests/bad-programs/modifying-immutable-local.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
x Assigned to immutable variable "a" | ||
,-[modifying-immutable-local.claw:2:9] | ||
1 | func foo() { | ||
2 | let a: u32 = 1; | ||
: | | ||
: `-- Defined here | ||
3 | a = 2; | ||
: | | ||
: `-- Assigned here | ||
4 | } | ||
`---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
func foo(a: u32) { | ||
let b: u64 = a; | ||
} |
8 changes: 8 additions & 0 deletions
8
crates/lib/tests/bad-programs/param-local-type-mismatch.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
x Conflicting types inferred for expression type0 != type1 | ||
,-[param-local-type-mismatch.claw:2:18] | ||
1 | func foo(a: u32) { | ||
2 | let b: u64 = a; | ||
: | | ||
: `-- This bit | ||
3 | } | ||
`---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
func foo() { | ||
let a = b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
x Failed to resolve name "b" | ||
,-[using-unbound-name.claw:2:13] | ||
1 | func foo() { | ||
2 | let a = b; | ||
: | | ||
: `-- Name referenced here | ||
3 | } | ||
`---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use compile_claw::compile; | ||
use miette::{GraphicalReportHandler, GraphicalTheme}; | ||
|
||
use std::fs; | ||
|
||
use wit_parser::Resolve; | ||
|
||
#[test] | ||
fn test_bad_programs() { | ||
for f in fs::read_dir("./tests/bad-programs").unwrap() { | ||
let f = f.unwrap(); | ||
let source_name = f.file_name().into_string().unwrap(); | ||
|
||
if source_name.ends_with(".error.txt") { | ||
continue; // skip error files | ||
} | ||
|
||
assert!(source_name.ends_with(".claw")); | ||
|
||
let source_code = fs::read_to_string(f.path()).unwrap(); | ||
|
||
let mut error_file_path = f.path(); | ||
error_file_path.set_extension("error.txt"); | ||
let error_file_contents = fs::read_to_string(error_file_path).unwrap(); | ||
|
||
let wit = Resolve::new(); | ||
|
||
let result = compile(source_name.clone(), &source_code, wit); | ||
match result { | ||
Ok(_) => { | ||
eprintln!( | ||
"File '{}' compiled without error when the following error was expected:", | ||
source_name | ||
); | ||
eprintln!("{}", error_file_contents); | ||
panic!() | ||
} | ||
Err(error) => { | ||
let mut error_string = String::new(); | ||
GraphicalReportHandler::new_themed(GraphicalTheme::none()) | ||
.render_report(&mut error_string, &error) | ||
.unwrap(); | ||
assert_eq!(error_string, error_file_contents); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.