@@ -17,6 +17,9 @@ public override object VisitFile(PseudoParser.FileContext context)
1717 return null ;
1818 }
1919
20+ /// <summary>
21+ /// Enter a list/ block of statements, and manages the scope creation for it
22+ /// </summary>
2023 public override object VisitStatList ( PseudoParser . StatListContext context )
2124 {
2225 _scopes . Push ( ) ;
@@ -33,6 +36,7 @@ public override object VisitStatList(PseudoParser.StatListContext context)
3336
3437 public override object VisitIfStat ( PseudoParser . IfStatContext context )
3538 {
39+ // Evaluate the boolean condition
3640 var condRes = ( ( Literal ) Visit ( context . boolOp ( ) ) ) . ToBoolean ( ) ;
3741 if ( condRes )
3842 {
@@ -75,6 +79,7 @@ public override object VisitForStat(PseudoParser.ForStatContext context)
7579 var varType = _scopes . GetVar ( varName ) . Type ;
7680 Visit ( context . varAssign ( ) ) ;
7781
82+ // Loops through the for as long as the incremented variable meets the condition
7883 for (
7984 float i = ( NumberLiteral ) _scopes . GetVar ( varName ) ;
8085 i < ( NumberLiteral ) Visit ( context . expr ( ) ) ;
@@ -89,13 +94,15 @@ public override object VisitForStat(PseudoParser.ForStatContext context)
8994
9095 public override object VisitReadBuiltin ( PseudoParser . ReadBuiltinContext context )
9196 {
97+ // Separate input using spaces into multiple values
9298 var values = Interpreter . Input ( ) ? . Split ( ' ' ) ;
9399
94100 if ( values == null )
95101 {
96102 throw new Exception ( ErrorMessages . NullInput ) ;
97103 }
98104
105+ // Check if the number of values read is bigger than the number of values requested
99106 if ( values . Length != context . ID ( ) . Length )
100107 {
101108 throw new Exception ( ErrorMessages . ArgumentsNumber ) ;
@@ -108,6 +115,8 @@ public override object VisitReadBuiltin(PseudoParser.ReadBuiltinContext context)
108115 {
109116 var variable = _scopes . GetVar ( ids [ i ] ) ;
110117
118+ // Try to fit the value into the type of the variable that requested the value
119+
111120 if ( variable is NumberLiteral numVar )
112121 {
113122 if ( numVar . IsInteger )
@@ -164,6 +173,9 @@ public override object VisitWriteBuiltin(PseudoParser.WriteBuiltinContext contex
164173 public override object VisitVariableDeclaration ( PseudoParser . VariableDeclarationContext context )
165174 {
166175 var varType = context . type ( ) . GetText ( ) ;
176+
177+ // Check for each declaration if it includes initialization or not
178+
167179 foreach ( var optionalAssignContext in context . optionalAssign ( ) )
168180 {
169181 VisitOptionalAssign ( optionalAssignContext , varType ) ;
@@ -179,6 +191,8 @@ public object VisitOptionalAssign(PseudoParser.OptionalAssignContext context, st
179191 var expr = context . expr ( ) ;
180192 if ( expr != null )
181193 {
194+ // Try to fit the initialization value into the variable
195+
182196 var exprResult = Visit ( expr ) as Literal ;
183197 if ( exprResult is NumberLiteral exprNum )
184198 {
@@ -214,7 +228,7 @@ public object VisitOptionalAssign(PseudoParser.OptionalAssignContext context, st
214228 }
215229 }
216230 }
217- else
231+ else // if the variable is not initialized, assign a default value for its type
218232 {
219233 if ( varType == TypeNames . IntegerType )
220234 {
@@ -256,6 +270,8 @@ public override object VisitVarAssign(PseudoParser.VarAssignContext context)
256270 throw new Exception ( ErrorMessages . UndefinedSymbol ( varName ) ) ;
257271 }
258272
273+ // Try to fit the assigned value into the variable
274+
259275 var exprResult = ( Literal ) Visit ( context . expr ( ) ) ;
260276
261277 if ( exprResult is NumberLiteral exprNum )
@@ -295,14 +311,14 @@ public override object VisitVarAssign(PseudoParser.VarAssignContext context)
295311 public override object VisitAndOp ( PseudoParser . AndOpContext context )
296312 {
297313 var leftValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 0 ) ) ) . ToBoolean ( ) ;
298- var rightValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 1 ) ) ) . ToBoolean ( ) ;
314+ var rightValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 1 ) ) ) . ToBoolean ( ) ;
299315 return new BooleanLiteral ( leftValue && rightValue ) ;
300316 }
301317
302318 public override object VisitOrOp ( PseudoParser . OrOpContext context )
303319 {
304- var leftValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 0 ) ) ) . ToBoolean ( ) ;
305- var rightValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 1 ) ) ) . ToBoolean ( ) ;
320+ var leftValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 0 ) ) ) . ToBoolean ( ) ;
321+ var rightValue = ( ( BooleanLiteral ) Visit ( context . boolOp ( 1 ) ) ) . ToBoolean ( ) ;
306322 return new BooleanLiteral ( leftValue || rightValue ) ;
307323 }
308324
@@ -327,29 +343,29 @@ public override object VisitAreEqual(PseudoParser.AreEqualContext context)
327343
328344 public override object VisitGreaterThan ( PseudoParser . GreaterThanContext context )
329345 {
330- var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
331- var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
346+ var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
347+ var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
332348 return new BooleanLiteral ( leftValue . Value > rightValue . Value ) ;
333349 }
334350
335351 public override object VisitGreaterOrEqual ( PseudoParser . GreaterOrEqualContext context )
336352 {
337- var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
338- var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
353+ var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
354+ var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
339355 return new BooleanLiteral ( leftValue . Value >= rightValue . Value ) ;
340356 }
341357
342358 public override object VisitLessThan ( PseudoParser . LessThanContext context )
343359 {
344- var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
345- var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
360+ var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
361+ var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
346362 return new BooleanLiteral ( leftValue . Value < rightValue . Value ) ;
347363 }
348364
349365 public override object VisitLessOrEqual ( PseudoParser . LessOrEqualContext context )
350366 {
351- var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
352- var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
367+ var leftValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 0 ) ) ;
368+ var rightValue = ( NumberLiteral ) Visit ( context . plusOrMinus ( 1 ) ) ;
353369 return new BooleanLiteral ( leftValue . Value <= rightValue . Value ) ;
354370 }
355371
0 commit comments