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

Juno patterns

parent e21147cf
No related branches found
No related tags found
1 merge request!138Juno patterns
......@@ -1134,6 +1134,16 @@ dependencies = [
"with_builtin_macros",
]
[[package]]
name = "juno_patterns"
version = "0.1.0"
dependencies = [
"async-std",
"hercules_rt",
"juno_build",
"with_builtin_macros",
]
[[package]]
name = "juno_schedule_test"
version = "0.1.0"
......
......@@ -21,6 +21,7 @@ members = [
"hercules_samples/ccp",
"juno_samples/simple3",
"juno_samples/patterns",
"juno_samples/matmul",
"juno_samples/casts_and_intrinsics",
"juno_samples/nested_ccp",
......
......@@ -107,6 +107,7 @@ void "void"
: ":"
, ","
\.\. ".."
\. "."
; ";"
~ "~"
......
......@@ -197,8 +197,10 @@ Pattern -> Result<Pattern, ()>
Ok(Pattern::IntLit { span : span, base : base }) }
| PackageName { Ok(Pattern::Variable { span : $span, name : $1? }) }
| '(' PatternsComma ')' { Ok(Pattern::TuplePattern { span : $span, pats : $2? }) }
| PackageName '{' NamePatterns '}'
{ Ok(Pattern::StructPattern { span : $span, name : $1?, pats : $3? }) }
| PackageName '{' StructPatterns '}'
{ let (pats, ignore_other) = $3?;
let pats = pats.into_iter().collect();
Ok(Pattern::StructPattern { span : $span, name : $1?, pats, ignore_other }) }
| PackageName '(' PatternsComma ')'
{ Ok(Pattern::UnionPattern { span : $span, name : $1?, pats : $3? }) }
;
......@@ -211,13 +213,23 @@ PatternsCommaS -> Result<Vec<Pattern>, ()>
: Pattern { Ok(vec![$1?]) }
| PatternsCommaS ',' Pattern { flatten($1, $3) }
;
NamePatterns -> Result<Vec<(Id, Pattern)>, ()>
: 'ID' ':' Pattern { Ok(vec![(span_of_tok($1)?, $3?)]) }
| NamePatternsS ',' 'ID' ':' Pattern { flatten($1, res_pair(span_of_tok($3), $5)) }
;
NamePatternsS -> Result<Vec<(Id, Pattern)>, ()>
: 'ID' ':' Pattern { Ok(vec![(span_of_tok($1)?, $3?)]) }
| NamePatternsS ',' 'ID' ':' Pattern { flatten($1, res_pair(span_of_tok($3), $5)) }
StructPatterns -> Result<(VecDeque<(Id, Pattern)>, bool), ()>
: { Ok((VecDeque::new(), false)) }
| '..' { Ok((VecDeque::new(), true)) }
| 'ID' { let span = span_of_tok($1)?;
let pattern = Pattern::Variable { span, name: vec![span] };
Ok((VecDeque::from([(span, pattern)]), false)) }
| '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, ()>
......@@ -683,7 +695,8 @@ pub enum Pattern {
IntLit { span : Span, base : IntBase },
Variable { span : Span, name : PackageName },
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> },
}
......
This diff is collapsed.
......@@ -556,15 +556,16 @@ impl TypeSolver {
_ => None,
}
}
*/
fn get_fields(&self, Type { val } : Type) -> Vec<Type> {
match &self.types[val] {
TypeForm::Tuple(fields) => { fields.clone() },
TypeForm::OtherType(t) => self.get_fields(*t),
_ => panic!("Internal function get_fields used on non-tuple"),
}
// Returns the types of the fields of a tuple
pub fn get_fields(&self, Type { val }: Type) -> Option<&Vec<Type>> {
match &self.types[val] {
TypeForm::Tuple { fields, .. } => Some(fields),
TypeForm::OtherType { other, .. } => self.get_fields(*other),
_ => None,
}
*/
}
// 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> {
......@@ -626,17 +627,18 @@ impl TypeSolver {
}
}
/*
pub fn get_field_names(&self, Type { val } : Type) -> Option<Vec<usize>> {
match &self.types[val] {
TypeForm::Struct { name : _, id : _, fields : _, names } => {
Some(names.keys().map(|i| *i).collect::<Vec<_>>())
},
TypeForm::OtherType(t) => self.get_field_names(*t),
_ => None,
}
pub fn get_field_names(&self, Type { val }: Type) -> Option<Vec<usize>> {
match &self.types[val] {
TypeForm::Struct {
name: _,
id: _,
fields: _,
names,
} => 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> {
match &self.types[val] {
......
......@@ -114,7 +114,7 @@ fn very_complex_antideps(x: usize) -> usize {
#[entry]
fn read_chains(input : i32) -> i32 {
let arrs : (i32[2], i32[2]);
let sub = arrs.0;
let (sub, _) = arrs;
sub[1] = input + 7;
arrs.0[1] = input + 3;
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