Skip to content
Snippets Groups Projects
Commit 84436227 authored by Aaron Councilman's avatar Aaron Councilman
Browse files

Merge branch 'juno-patterns' into 'main'

Juno patterns

See merge request !138
parents e21147cf 8d2facca
No related branches found
No related tags found
1 merge request!138Juno patterns
Pipeline #201284 passed
...@@ -1134,6 +1134,16 @@ dependencies = [ ...@@ -1134,6 +1134,16 @@ dependencies = [
"with_builtin_macros", "with_builtin_macros",
] ]
[[package]]
name = "juno_patterns"
version = "0.1.0"
dependencies = [
"async-std",
"hercules_rt",
"juno_build",
"with_builtin_macros",
]
[[package]] [[package]]
name = "juno_schedule_test" name = "juno_schedule_test"
version = "0.1.0" version = "0.1.0"
......
...@@ -21,6 +21,7 @@ members = [ ...@@ -21,6 +21,7 @@ members = [
"hercules_samples/ccp", "hercules_samples/ccp",
"juno_samples/simple3", "juno_samples/simple3",
"juno_samples/patterns",
"juno_samples/matmul", "juno_samples/matmul",
"juno_samples/casts_and_intrinsics", "juno_samples/casts_and_intrinsics",
"juno_samples/nested_ccp", "juno_samples/nested_ccp",
......
...@@ -107,6 +107,7 @@ void "void" ...@@ -107,6 +107,7 @@ void "void"
: ":" : ":"
, "," , ","
\.\. ".."
\. "." \. "."
; ";" ; ";"
~ "~" ~ "~"
......
...@@ -197,8 +197,10 @@ Pattern -> Result<Pattern, ()> ...@@ -197,8 +197,10 @@ Pattern -> Result<Pattern, ()>
Ok(Pattern::IntLit { span : span, base : base }) } Ok(Pattern::IntLit { span : span, base : base }) }
| PackageName { Ok(Pattern::Variable { span : $span, name : $1? }) } | PackageName { Ok(Pattern::Variable { span : $span, name : $1? }) }
| '(' PatternsComma ')' { Ok(Pattern::TuplePattern { span : $span, pats : $2? }) } | '(' PatternsComma ')' { Ok(Pattern::TuplePattern { span : $span, pats : $2? }) }
| PackageName '{' NamePatterns '}' | PackageName '{' StructPatterns '}'
{ Ok(Pattern::StructPattern { span : $span, name : $1?, pats : $3? }) } { let (pats, ignore_other) = $3?;
let pats = pats.into_iter().collect();
Ok(Pattern::StructPattern { span : $span, name : $1?, pats, ignore_other }) }
| PackageName '(' PatternsComma ')' | PackageName '(' PatternsComma ')'
{ Ok(Pattern::UnionPattern { span : $span, name : $1?, pats : $3? }) } { Ok(Pattern::UnionPattern { span : $span, name : $1?, pats : $3? }) }
; ;
...@@ -211,13 +213,23 @@ PatternsCommaS -> Result<Vec<Pattern>, ()> ...@@ -211,13 +213,23 @@ PatternsCommaS -> Result<Vec<Pattern>, ()>
: Pattern { Ok(vec![$1?]) } : Pattern { Ok(vec![$1?]) }
| PatternsCommaS ',' Pattern { flatten($1, $3) } | PatternsCommaS ',' Pattern { flatten($1, $3) }
; ;
NamePatterns -> Result<Vec<(Id, Pattern)>, ()> StructPatterns -> Result<(VecDeque<(Id, Pattern)>, bool), ()>
: 'ID' ':' Pattern { Ok(vec![(span_of_tok($1)?, $3?)]) } : { Ok((VecDeque::new(), false)) }
| NamePatternsS ',' 'ID' ':' Pattern { flatten($1, res_pair(span_of_tok($3), $5)) } | '..' { Ok((VecDeque::new(), true)) }
; | 'ID' { let span = span_of_tok($1)?;
NamePatternsS -> Result<Vec<(Id, Pattern)>, ()> let pattern = Pattern::Variable { span, name: vec![span] };
: 'ID' ':' Pattern { Ok(vec![(span_of_tok($1)?, $3?)]) } Ok((VecDeque::from([(span, pattern)]), false)) }
| NamePatternsS ',' 'ID' ':' Pattern { flatten($1, res_pair(span_of_tok($3), $5)) } | 'ID' ':' Pattern { let span = span_of_tok($1)?;
Ok((VecDeque::from([(span, $3?)]), false)) }
| 'ID' ',' StructPatterns { let span = span_of_tok($1)?;
let pattern = Pattern::Variable { span, name: vec![span] };
let (mut fields, ignore_rest) = $3?;
fields.push_front((span, pattern));
Ok((fields, ignore_rest)) }
| 'ID' ':' Pattern ',' StructPatterns { let span = span_of_tok($1)?;
let (mut fields, ignore_rest) = $5?;
fields.push_front((span, $3?));
Ok((fields, ignore_rest)) }
; ;
Stmt -> Result<Stmt, ()> Stmt -> Result<Stmt, ()>
...@@ -683,7 +695,8 @@ pub enum Pattern { ...@@ -683,7 +695,8 @@ pub enum Pattern {
IntLit { span : Span, base : IntBase }, IntLit { span : Span, base : IntBase },
Variable { span : Span, name : PackageName }, Variable { span : Span, name : PackageName },
TuplePattern { span : Span, pats : Vec<Pattern> }, TuplePattern { span : Span, pats : Vec<Pattern> },
StructPattern { span : Span, name : PackageName, pats : Vec<(Id, Pattern)> }, // Ignore other indicates the pattern ended with .. and so there may be other fields that were not listed
StructPattern { span : Span, name : PackageName, pats : Vec<(Id, Pattern)>, ignore_other: bool },
UnionPattern { span : Span, name : PackageName, pats : Vec<Pattern> }, UnionPattern { span : Span, name : PackageName, pats : Vec<Pattern> },
} }
......
This diff is collapsed.
...@@ -556,15 +556,16 @@ impl TypeSolver { ...@@ -556,15 +556,16 @@ impl TypeSolver {
_ => None, _ => None,
} }
} }
*/
fn get_fields(&self, Type { val } : Type) -> Vec<Type> { // Returns the types of the fields of a tuple
match &self.types[val] { pub fn get_fields(&self, Type { val }: Type) -> Option<&Vec<Type>> {
TypeForm::Tuple(fields) => { fields.clone() }, match &self.types[val] {
TypeForm::OtherType(t) => self.get_fields(*t), TypeForm::Tuple { fields, .. } => Some(fields),
_ => panic!("Internal function get_fields used on non-tuple"), TypeForm::OtherType { other, .. } => self.get_fields(*other),
} _ => None,
} }
*/ }
// Return the type of the field (in a tuple) at a particular index // Return the type of the field (in a tuple) at a particular index
pub fn get_index(&self, Type { val }: Type, idx: usize) -> Option<Type> { pub fn get_index(&self, Type { val }: Type, idx: usize) -> Option<Type> {
...@@ -626,17 +627,18 @@ impl TypeSolver { ...@@ -626,17 +627,18 @@ impl TypeSolver {
} }
} }
/* pub fn get_field_names(&self, Type { val }: Type) -> Option<Vec<usize>> {
pub fn get_field_names(&self, Type { val } : Type) -> Option<Vec<usize>> { match &self.types[val] {
match &self.types[val] { TypeForm::Struct {
TypeForm::Struct { name : _, id : _, fields : _, names } => { name: _,
Some(names.keys().map(|i| *i).collect::<Vec<_>>()) id: _,
}, fields: _,
TypeForm::OtherType(t) => self.get_field_names(*t), names,
_ => None, } => Some(names.keys().map(|i| *i).collect::<Vec<_>>()),
} TypeForm::OtherType { other, .. } => self.get_field_names(*other),
_ => None,
} }
*/ }
pub fn get_num_dimensions(&self, Type { val }: Type) -> Option<usize> { pub fn get_num_dimensions(&self, Type { val }: Type) -> Option<usize> {
match &self.types[val] { match &self.types[val] {
......
...@@ -114,7 +114,7 @@ fn very_complex_antideps(x: usize) -> usize { ...@@ -114,7 +114,7 @@ fn very_complex_antideps(x: usize) -> usize {
#[entry] #[entry]
fn read_chains(input : i32) -> i32 { fn read_chains(input : i32) -> i32 {
let arrs : (i32[2], i32[2]); let arrs : (i32[2], i32[2]);
let sub = arrs.0; let (sub, _) = arrs;
sub[1] = input + 7; sub[1] = input + 7;
arrs.0[1] = input + 3; arrs.0[1] = input + 3;
let result = sub[1] + arrs.0[1]; let result = sub[1] + arrs.0[1];
......
[package]
name = "juno_patterns"
version = "0.1.0"
authors = ["Aaron Councilman <aaronjc4@illinois.edu>"]
edition = "2021"
[[bin]]
name = "juno_patterns"
path = "src/main.rs"
[build-dependencies]
juno_build = { path = "../../juno_build" }
[dependencies]
juno_build = { path = "../../juno_build" }
hercules_rt = { path = "../../hercules_rt" }
with_builtin_macros = "0.1.0"
async-std = "*"
use juno_build::JunoCompiler;
fn main() {
JunoCompiler::new()
.file_in_src("patterns.jn")
.unwrap()
.build()
.unwrap();
}
#![feature(concat_idents)]
use hercules_rt::{runner};
juno_build::juno!("patterns");
fn main() {
async_std::task::block_on(async {
let mut r = runner!(entry);
let c = r.run(3, 8.0).await;
println!("{}", c);
assert_eq!(c, 14.0);
});
}
#[test]
fn simple3_test() {
main();
}
type Record = struct { a: i32; b: f64; };
#[entry]
fn entry(x: i32, f: f64) -> f64 {
let r = Record { a=x, b=f };
let p = (f, f, x);
let Record { a, .. } = r;
let (_, b, c) = p;
return (a + c) as f64 + b;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment