Skip to content

Commit ab9fd29

Browse files
ZJUGuoShuaitatetian
authored andcommitted
feat: add \REPEAT+\UNTIL{<cond>}
1 parent bdaa72f commit ab9fd29

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/Parser.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
* <block> :== ( <control> | <function> | <statement> |
2424
* <comment> )[0..n]
2525
*
26-
* <control> :== <if> | <for> | <while>
26+
* <control> :== <if> | <for> | <while> | <repeat>
2727
* <if> :== \IF{<cond>} + <block>
2828
* + ( \ELIF{<cond>} <block> )[0..n]
2929
* + ( \ELSE <block> )[0..1]
3030
* + \ENDIF
3131
* <for> :== \FOR{<cond>} + <block> + \ENDFOR
3232
* <while> :== \WHILE{<cond>} + <block> + \ENDWHILE
33+
* <repeat> :== \REPEAT + <block> + \UNTIL{<cond>}
3334
*
3435
* <function> :== \FUNCTION{<name>}{<params>} <block> \ENDFUNCTION
3536
* (same for <procedure>)
@@ -251,6 +252,7 @@ Parser.prototype._parseControl = function() {
251252
var controlNode;
252253
if ((controlNode = this._parseIf())) return controlNode;
253254
if ((controlNode = this._parseLoop())) return controlNode;
255+
if ((controlNode = this._parseRepeat())) return controlNode;
254256
};
255257

256258
Parser.prototype._parseFunction = function() {
@@ -331,6 +333,26 @@ Parser.prototype._parseLoop = function() {
331333
return loopNode;
332334
};
333335

336+
Parser.prototype._parseRepeat = function() {
337+
if (!this._lexer.accept('func', ['REPEAT'])) return null;
338+
339+
var repeatName = this._lexer.get().text.toLowerCase();
340+
var repeatNode = new ParseNode('repeat', repeatName);
341+
342+
// <block>
343+
repeatNode.addChild(this._parseBlock());
344+
345+
// \UNTIL
346+
this._lexer.expect('func', 'until');
347+
348+
// {<cond>}
349+
this._lexer.expect('open');
350+
repeatNode.addChild(this._parseCond());
351+
this._lexer.expect('close');
352+
353+
return repeatNode;
354+
};
355+
334356
var INPUTS_OUTPUTS_COMMANDS = ['ensure', 'require'];
335357
var STATEMENT_COMMANDS = ['state', 'print', 'return'];
336358
Parser.prototype._parseCommand = function(acceptCommands) {

src/Renderer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,32 @@ Renderer.prototype._buildTree = function(node) {
736736
this._typeKeyword(endLoopName);
737737
}
738738
break;
739+
case 'repeat':
740+
// \REPEAT
741+
// ==>
742+
// <p class="ps-line">
743+
// <span class="ps-keyword">repeat</span>
744+
// </p>
745+
this._newLine();
746+
this._typeKeyword('repeat');
747+
748+
// block
749+
var repeatBlock = node.children[0];
750+
this._buildCommentsFromBlock(repeatBlock);
751+
this._buildTree(repeatBlock);
752+
753+
if (!this._options.noEnd) {
754+
// \UNTIL{<cond>}
755+
// ==>
756+
// <p class="ps-line">
757+
// <span class="ps-keyword">until</span>
758+
// </p>
759+
this._newLine();
760+
this._typeKeyword('until ');
761+
var repeatCond = node.children[1];
762+
this._buildTree(repeatCond);
763+
}
764+
break;
739765
// ------------------- Lines -------------------
740766
case 'command':
741767
// commands: \STATE, \ENSURE, \PRINT, \RETURN, etc.

static/samples.html.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
\STATE $i \gets i + 1$
8585
\ENDWHILE
8686
\ENDPROCEDURE
87+
\PROCEDURE{Test-Repeat}{$n$}
88+
\STATE $i \gets 0$
89+
\REPEAT
90+
\PRINT $i$
91+
\STATE $i \gets i + 1$
92+
\UNTIL{$i>n$}
93+
\ENDPROCEDURE
8794
\end{algorithmic}
8895
\end{algorithm}
8996
\begin{algorithm}

0 commit comments

Comments
 (0)