Skip to content

Commit

Permalink
nixd/Syntax: the operator range for "Op" AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 22, 2023
1 parent 21da6b5 commit eb7bc7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions nixd/include/nixd/Syntax/Nodes.inc
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,17 @@ NODE(Select, {
NODE(OpHasAttr, {
Node *Operand;
Node *Path;
RangeIdx OpRange;
COMMON_METHOD
})
NODE(OpNot, {
Node *Body;
RangeIdx OpRange;
COMMON_METHOD
})
NODE(OpNegate, {
Node *Body;
RangeIdx OpRange;
COMMON_METHOD
})

Expand All @@ -182,6 +185,7 @@ NODE(LegacyLet, {
NODE(NAME, { \
Node *LHS; \
Node *RHS; \
RangeIdx OpRange; \
COMMON_METHOD \
})
#include "BinaryOps.inc"
Expand Down
33 changes: 18 additions & 15 deletions nixd/lib/Syntax/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,35 @@ expr_op
: '!' expr_op %prec NOT {
auto N = decorateNode(new OpNot, *yylocp, *Data);
N->Body = $2;
N->OpRange = mkRange(@1, *Data);
$$ = N;
}
| '-' expr_op %prec NEGATE {
auto N = decorateNode(new OpNegate, *yylocp, *Data);
N->Body = $2;
N->OpRange = mkRange(@1, *Data);
$$ = N;
}
| expr_op EQ expr_op { $$ = mkBinOp<OpEq>($1, $3, *yylocp, *Data); }
| expr_op NEQ expr_op { $$ = mkBinOp<OpNEq>($1, $3, *yylocp, *Data); }
| expr_op '<' expr_op { $$ = mkBinOp<OpLe>($1, $3, *yylocp, *Data); }
| expr_op LEQ expr_op { $$ = mkBinOp<OpLe>($1, $3, *yylocp, *Data); }
| expr_op '>' expr_op { $$ = mkBinOp<OpGe>($1, $3, *yylocp, *Data); }
| expr_op GEQ expr_op { $$ = mkBinOp<OpGeq>($1, $3, *yylocp, *Data); }
| expr_op AND expr_op { $$ = mkBinOp<OpAnd>($1, $3, *yylocp, *Data); }
| expr_op OR expr_op { $$ = mkBinOp<OpOr>($1, $3, *yylocp, *Data); }
| expr_op IMPL expr_op { $$ = mkBinOp<OpImpl>($1, $3, *yylocp, *Data); }
| expr_op UPDATE expr_op { $$ = mkBinOp<OpUpdate>($1, $3, *yylocp, *Data); }
| expr_op '+' expr_op { $$ = mkBinOp<OpAdd>($1, $3, *yylocp, *Data); }
| expr_op '-' expr_op { $$ = mkBinOp<OpSub>($1, $3, *yylocp, *Data); }
| expr_op '*' expr_op { $$ = mkBinOp<OpMul>($1, $3, *yylocp, *Data); }
| expr_op '/' expr_op { $$ = mkBinOp<OpDiv>($1, $3, *yylocp, *Data); }
| expr_op CONCAT expr_op { $$ = mkBinOp<OpConcatLists>($1, $3, *yylocp, *Data); }
| expr_op EQ expr_op { $$ = mkBinOp<OpEq>($1, $3, @2, *yylocp, *Data); }
| expr_op NEQ expr_op { $$ = mkBinOp<OpNEq>($1, $3, @2, *yylocp, *Data); }
| expr_op '<' expr_op { $$ = mkBinOp<OpLe>($1, $3, @2, *yylocp, *Data); }
| expr_op LEQ expr_op { $$ = mkBinOp<OpLe>($1, $3, @2, *yylocp, *Data); }
| expr_op '>' expr_op { $$ = mkBinOp<OpGe>($1, $3, @2, *yylocp, *Data); }
| expr_op GEQ expr_op { $$ = mkBinOp<OpGeq>($1, $3, @2, *yylocp, *Data); }
| expr_op AND expr_op { $$ = mkBinOp<OpAnd>($1, $3, @2, *yylocp, *Data); }
| expr_op OR expr_op { $$ = mkBinOp<OpOr>($1, $3, @2, *yylocp, *Data); }
| expr_op IMPL expr_op { $$ = mkBinOp<OpImpl>($1, $3, @2, *yylocp, *Data); }
| expr_op UPDATE expr_op { $$ = mkBinOp<OpUpdate>($1, $3, @2, *yylocp, *Data); }
| expr_op '+' expr_op { $$ = mkBinOp<OpAdd>($1, $3, @2, *yylocp, *Data); }
| expr_op '-' expr_op { $$ = mkBinOp<OpSub>($1, $3, @2, *yylocp, *Data); }
| expr_op '*' expr_op { $$ = mkBinOp<OpMul>($1, $3, @2, *yylocp, *Data); }
| expr_op '/' expr_op { $$ = mkBinOp<OpDiv>($1, $3, @2, *yylocp, *Data); }
| expr_op CONCAT expr_op { $$ = mkBinOp<OpConcatLists>($1, $3, @2, *yylocp, *Data); }
| expr_op '?' attrpath {
auto N = decorateNode(new OpHasAttr, *yylocp, *Data);
N->Operand = $1;
N->Path = $3;
N->OpRange = mkRange(@2, *Data);
$$ = N;
}
| expr_app
Expand Down
3 changes: 2 additions & 1 deletion nixd/lib/Syntax/Parser/Prologue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ T *decorateNode(T *Node, YYLTYPE YL, nixd::syntax::ParseData &Data) {

template <class T>
T *mkBinOp(nixd::syntax::Node *LHS, nixd::syntax::Node *RHS, YYLTYPE YL,
nixd::syntax::ParseData &Data) {
YYLTYPE OpRange, nixd::syntax::ParseData &Data) {
auto N = decorateNode<T>(new T, YL, Data);
N->LHS = LHS;
N->RHS = RHS;
N->OpRange = mkRange(OpRange, Data);
return N;
}

0 comments on commit eb7bc7e

Please sign in to comment.