Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pollen supports array access #140

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pollen/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub enum Expr {
op: UOp,
expr: Box<Expr>
},
ArrayAccess {
expr: Box<Expr>,
idx: Box<Expr>
},
Record {
typ: Typ,
fields: Vec<RecordField>
Expand Down
21 changes: 17 additions & 4 deletions pollen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ lazy_static! {

// Precedence is defined lowest to highest
PrattParser::new()
.op(Op::postfix(Rule::array_access))
.op(Op::infix(Rule::or, Left))
.op(Op::infix(Rule::and, Left))
.op(Op::infix(Rule::eq, Left) | Op::infix(Rule::neq, Left))
Expand Down Expand Up @@ -625,10 +626,22 @@ fn parse_expr(expression: Pairs<Rule>) -> Expr {
expr: Box::new(exp),
}
})
.map_postfix(|lhs, op| {
match op.as_rule() {
rule => unreachable!("{:?} not recognized as a postfix", rule),
}
.map_postfix(|exp, op| {
let idx_expr = match op.as_rule() {
Rule::array_access => {
let mut inner = op.into_inner();
let Some(expr) = inner.next() else {
unreachable!("array access requires an expression")
};
parse_expr(expr.into_inner())
}
rule => unreachable!("{:?} not recognized as a postfix", rule)
};

Expr::ArrayAccess {
expr: Box::new(exp),
idx: Box::new(idx_expr)
}
})
.map_infix(|lhs, op, rhs| {
enum OpType {
Expand Down
6 changes: 5 additions & 1 deletion pollen/src/pollen.pest
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ func_call = { identifier ~ call_args }
term = _{ literal | obj_initialize | func_call | identifier
| "(" ~ expr ~ ")" | "[" ~ expr ~ "]" }

expr = { prefix* ~ term ~ (binop ~ prefix* ~ term ) * }
array_access = { "[" ~ expr ~ "]" }
postfix = _{ array_access }
term_ext = _ { prefix* ~ term ~ postfix* }

expr = { term_ext ~ (binop ~ term_ext) * }


/* ----- Statements ---- */
Expand Down
101 changes: 101 additions & 0 deletions pollen/tests/parse-out/paths.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Prog {
imports: [],
func_defs: [
FuncDef {
name: Id(
"emit_paths",
),
args: [],
ret_typ: None,
stmts: [
ParsetDecl {
id: Id(
"out_paths",
),
typ: Tuple(
Step,
Step,
),
graph_id: None,
},
For {
id: Id(
"path",
),
iterator: Var(
Id(
"Paths",
),
),
body: Block {
stmts: [
EmitTo {
expr: Tuple {
lhs: ArrayAccess {
expr: FieldAccess {
object: Var(
Id(
"path",
),
),
field: Var(
Id(
"steps",
),
),
},
idx: Integer(
0,
),
},
rhs: ArrayAccess {
expr: FieldAccess {
object: Var(
Id(
"path",
),
),
field: Var(
Id(
"steps",
),
),
},
idx: BinOpExpr {
lhs: MethodCall {
object: FieldAccess {
object: Var(
Id(
"path",
),
),
field: Var(
Id(
"steps",
),
),
},
method: Id(
"size",
),
args: [],
},
op: Sub,
rhs: Integer(
1,
),
},
},
},
set_id: Id(
"out_paths",
),
},
],
},
},
],
ret: None,
},
],
}
4 changes: 2 additions & 2 deletions pollen/tests/paths.pollen
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def paths() {
def emit_paths() {
// Since Pollen doesn't explicitly have path identifiers,
// we print the start and end steps of each path
// (akin to `odgi paths --idx = graph.og --list-paths --list-paths-start-end`).
Expand All @@ -9,7 +9,7 @@ def paths() {
parset out_paths[(Step*Step)];

for path in Paths {
emit ( path[0] /* path.steps[0], */
emit ( path.steps[0],
path.steps[path.steps.size() - 1])
to out_paths;
}
Expand Down
Loading