@@ -276,6 +276,7 @@ public class Parser {
276276 | procedure_call
277277 | if_else_statement
278278 | assignment_statement
279+ | repeat_until
279280 | empty
280281 */
281282 private func statement( ) -> AST {
@@ -284,6 +285,8 @@ public class Parser {
284285 return compoundStatement ( )
285286 case . if:
286287 return ifElseStatement ( )
288+ case . repeat :
289+ return repeatUntilLoop ( )
287290 case . id:
288291 if nextToken == . parenthesis( . left) {
289292 return functionCall ( )
@@ -295,6 +298,27 @@ public class Parser {
295298 }
296299 }
297300
301+ /**
302+ Rule:
303+
304+ repeat_until : REPEAT statement UNTIL condition
305+ */
306+ private func repeatUntilLoop( ) -> RepeatUntil {
307+ eat ( . repeat )
308+
309+ var statements : [ AST ] = [ ]
310+
311+ while currentToken != . until {
312+ statements. append ( statement ( ) )
313+ if currentToken == . semi {
314+ eat ( . semi)
315+ }
316+ }
317+ eat ( . until)
318+ let condition = self . condition ( )
319+ return RepeatUntil ( statement: statements. count == 1 ? statements [ 0 ] : Compound ( children: statements) , condition: condition)
320+ }
321+
298322 /**
299323 Rule:
300324
@@ -346,9 +370,12 @@ public class Parser {
346370 /**
347371 Rule:
348372 condition: expr (= | < | >) expr
373+ | LPAREN expr (= | < | >) expr RPAREN
349374 */
350375 private func condition( ) -> Condition {
351- eat ( . parenthesis( . left) )
376+ if currentToken == . parenthesis( . left) {
377+ eat ( . parenthesis( . left) )
378+ }
352379 let left = expr ( )
353380 var type : ConditionType = . equals
354381 switch currentToken {
@@ -365,7 +392,9 @@ public class Parser {
365392 fatalError ( " Invalid condition type \( type) " )
366393 }
367394 let right = expr ( )
368- eat ( . parenthesis( . right) )
395+ if currentToken == . parenthesis( . right) {
396+ eat ( . parenthesis( . right) )
397+ }
369398 return Condition ( type: type, leftSide: left, rightSide: right)
370399 }
371400
@@ -483,10 +512,10 @@ public class Parser {
483512 let result = expr ( )
484513 eat ( . parenthesis( . right) )
485514 return result
486- case . constant( . string( let value) ) :
515+ case let . constant( . string( value) ) :
487516 eat ( . constant( . string( value) ) )
488517 return value
489- case . constant( . boolean( let value) ) :
518+ case let . constant( . boolean( value) ) :
490519 eat ( . constant( . boolean( value) ) )
491520 return value
492521 default :
0 commit comments