Skip to content

Commit

Permalink
Improve AST serialization grammar for For nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
roastduck committed Jan 15, 2024
1 parent 051ab9e commit da83797
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion grammar/ast_lexer.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Comment: '/*' .*? '*/' -> skip;
IF: 'if';
ELSE: 'else';
FOR: 'for';
IN: 'in';
FROM: 'from';
UNTIL: 'until';
STEP: 'step';
LENGTH: 'length';
ASSERT_TOKEN: 'assert';
ASSUME: 'assume';
FUNC: 'func';
Expand Down
2 changes: 1 addition & 1 deletion grammar/ast_parser.g
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ forProperty returns [Ref<ForProperty> property]

for returns [Stmt node]
: forProperty
FOR var IN begin=expr ':' end=expr ':' step=expr ':' len=expr
FOR var FROM begin=expr UNTIL end=expr STEP step=expr LENGTH len=expr
LBRACE stmts RBRACE
{
$node = makeFor($var.name, $begin.node, $end.node, $step.node, $len.node,
Expand Down
3 changes: 2 additions & 1 deletion include/serialize/print_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class PrintVisitor : public CodeGen<CodeGenStream> {
hexFloat_ = false, parenDespitePriority_ = false,
printSourceLocation_ = false;
const std::unordered_set<std::string> keywords = {
"if", "else", "for", "in", "assert", "assume", "func", "true", "false",
"if", "else", "for", "from", "until", "step",
"length", "assert", "assume", "func", "true", "false",
};

/**
Expand Down
10 changes: 5 additions & 5 deletions src/serialize/print_ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,14 @@ void PrintVisitor::visit(const For &op) {
os() << "@!prefer_libs" << std::endl;
}
makeIndent();
os() << prettyKeyword("for ") << prettyIterName(op->iter_)
<< prettyKeyword(" in ");
os() << prettyKeyword("for") << " " << prettyIterName(op->iter_) << " "
<< prettyKeyword("from") << " ";
recur(op->begin_);
os() << " : ";
os() << " " << prettyKeyword("until") << " ";
recur(op->end_);
os() << " : ";
os() << " " << prettyKeyword("step") << " ";
recur(op->step_);
os() << " : ";
os() << " " << prettyKeyword("length") << " ";
recur(op->len_);
os() << " ";
beginBlock();
Expand Down

0 comments on commit da83797

Please sign in to comment.