diff --git a/.vscode/settings.json b/.vscode/settings.json index ecdb9bef5ca40f3bb804a31df9e42049435b4aac..ddaae1da70cdc610364d7d2ebd2b85b38dcc65ce 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,10 @@ { "cSpell.ignoreWords": [ - "llvm" - ] + "intrinsics", + "lalrpop", + "lalrpop mod", + "llvm", + "mod" + ], + "rust-analyzer.inlayHints.enable": true } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 73757b3a240c2ef6a9ade76b8f484505e310b911..a4ab3bcfe4528280efb90451737b0ae4d1ba8d60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,14 @@ build = "build.rs" # LALRPOP preprocessing [build-dependencies.lalrpop] version = "0.19.0" +features = ["lexer"] [dependencies] lalrpop-util = "0.19.0" regex = "1.3.9" -lalrpop = {version = "0.19.0", features = ["lexer"] } +# lalrpop = {version = "0.19.0", features = ["lexer"] } + +[[bin]] +name = "minimal" +path = "src/minimal/main.rs" diff --git a/README.md b/README.md index 9317dc3f417b5d7db2cf376a0485edd72275e6dc..ac70683659149ad2133480c735a3dcb38878b2b3 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ You will each be scheduled 30 minutes to present Your home exam to us, based on Implement for higher grades - Basic code generation. -- Pass `noalias` where possible allowing for better optimization (assuming your borrowchecker prevents aliasing). +- Pass `noalias` where possible allowing for better optimization (assuming your borrow checker prevents aliasing). - Other attributes, intrinsics, etc. that enables further LLVM optimizations. - Bindings to external code, (this could allow for ) diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop new file mode 100644 index 0000000000000000000000000000000000000000..31d2921010dad1d4b3c5afb4ccfd1976b40c2f2e --- /dev/null +++ b/src/grammar.lalrpop @@ -0,0 +1,7 @@ +use std::str::FromStr; + +grammar; + +pub Id: String = { + r"([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(), +}; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..a319e9138f38ddf0983b19743eb6e3f4f6379f93 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,15 @@ +use lalrpop_util::lalrpop_mod; + +lalrpop_mod!(pub grammar); + +use grammar::*; + +fn main() { + println!("hello world"); +} + +#[test] +fn hello() { + println!("Hello in test") +} + diff --git a/src/minimal/main.rs b/src/minimal/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..1f5731be37427916bffece74868abcda7e82c282 --- /dev/null +++ b/src/minimal/main.rs @@ -0,0 +1,36 @@ +use lalrpop_util::lalrpop_mod; + +lalrpop_mod!(pub parser, "/minimal/parser.rs"); + +use parser::*; + +fn main() {} + +#[test] +fn parse_id() { + println!( + "{:?}", + IdParser::new().parse( + " + + //a1_a // + + /* + a1_a + */ + a1_a + " + ) + ); +} + +#[test] +fn parse_num() { + println!("{:?}", NumParser::new().parse("123")); +} + +#[test] +fn parse_num_or_id() { + println!("{:?}", NumOrIdParser::new().parse("123")); + println!("{:?}", NumOrIdParser::new().parse("a1_a")); +} diff --git a/src/minimal/parser.lalrpop b/src/minimal/parser.lalrpop new file mode 100644 index 0000000000000000000000000000000000000000..1372d50e8c0b180caf2510af2e518a262801843b --- /dev/null +++ b/src/minimal/parser.lalrpop @@ -0,0 +1,55 @@ +use std::str::FromStr; + +grammar; + +match { + // The default whitespace skipping is disabled an `ignore pattern` is specified + r"\s*" => { }, + // Skip `// comments` + r"//[^\n\r]*" => { }, + // Skip `/* comments */` + r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { }, + _ +} + +pub NumOrId = { + Num => <>.to_string(), + Id, +} + +pub Num: usize = { + r"[0-9]+" => usize::from_str(<>).unwrap(), +}; + +pub Id: String = { + r"([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(), +}; + +// lets try to break down the multi-line comment +// +// should allow patterns like +// /* +// ... +// */ +// and +// /* /* +// */ +// */ +// but reject patterns like +// /* /* +// */ +// and +// /* */ */ +// +// Rather tricky right... +// +// /\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/ +// the outer "/\* ... \*/" requires start and end "/*" ... "*/" +// so let's look closer at "([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*" +// the trailing "*", just means any number of +// "[^\*]*\*+[^\*/])*([^\*]*\*+|[^\*]" +// +// "[^\*]*\*+[^\*/])*" +// "[^\*]*"" any number or Not("*"), e.g. 0123 +// "\*" one or many "*", e.g. *, or **** +// .... ehhh I gave up, regexp is haaaaaaarrrrrrrd, another day perhaps \ No newline at end of file