Skip to content

Commit 90422c7

Browse files
author
Sergiu Marton
committed
Added some comments, removed unnecessary files
1 parent 6748487 commit 90422c7

File tree

9 files changed

+59
-65
lines changed

9 files changed

+59
-65
lines changed

PseudocodeInterpreter/PseudocodeInterpreter.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@
5757
<Compile Include="src\ExtensionMethods.cs" />
5858
<Compile Include="src\Interpreter.cs" />
5959
<Compile Include="src\Objects\BooleanLiteral.cs" />
60-
<Compile Include="src\Objects\IValue.cs" />
6160
<Compile Include="src\Objects\Literal.cs" />
6261
<Compile Include="src\Objects\NumberLiteral.cs" />
6362
<Compile Include="src\Objects\StringLiteral.cs" />
6463
<Compile Include="src\Objects\TypeNames.cs" />
6564
<Compile Include="src\PseudoVisitorImpl.cs" />
6665
<Compile Include="src\Scope.cs" />
67-
<Compile Include="src\SymbolTable.cs" />
6866
</ItemGroup>
6967
<ItemGroup>
7068
<Content Include="src\AntlrFiles\antlr-4.7.1-complete.jar" />

PseudocodeInterpreter/src/Interpreter.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ public static class Interpreter
1111
public static Action<string> Output { get; set; } = Console.Write;
1212
public static Func<string> Input { get; set; } = Console.ReadLine;
1313

14-
public static void ExecuteFile(string[] args)
14+
public static void ExecuteFile(string fileName)
1515
{
16-
string source = File.ReadAllText(args[0]);
16+
string source = File.ReadAllText(fileName);
17+
18+
// Remove all empty lines
1719
source = Regex.Replace(source, @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline).TrimEnd();
1820

1921
ICharStream input = CharStreams.fromstring(source);
@@ -34,28 +36,5 @@ public static void ExecuteFile(string[] args)
3436
Output(e.Message);
3537
}
3638
}
37-
38-
public static void ExecuteString(string text)
39-
{
40-
string toExecute = Regex.Replace(text, @"^\s*$\n|\r", string.Empty, RegexOptions.Multiline).TrimEnd();
41-
42-
ICharStream input = CharStreams.fromstring(toExecute);
43-
44-
PseudoLexer lexer = new PseudoLexer(input);
45-
CommonTokenStream tokens = new CommonTokenStream(lexer);
46-
PseudoParser parser = new PseudoParser(tokens);
47-
var tree = parser.file();
48-
49-
PseudoVisitorImpl calcVisitor = new PseudoVisitorImpl();
50-
51-
try
52-
{
53-
calcVisitor.Visit(tree);
54-
}
55-
catch (Exception e)
56-
{
57-
Output(e.Message);
58-
}
59-
}
6039
}
6140
}

PseudocodeInterpreter/src/Objects/IValue.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

PseudocodeInterpreter/src/Objects/Literal.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33

44
namespace PseudocodeInterpreter.Objects
55
{
6-
public abstract class Literal : IValue
6+
public abstract class Literal
77
{
8-
Literal IValue.GetValue()
9-
{
10-
return this;
11-
}
12-
138
public string Type { get; protected set; }
149

1510
public virtual bool ToBoolean()

PseudocodeInterpreter/src/Objects/StringLiteral.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class StringLiteral : Literal
66

77
public StringLiteral(string value)
88
{
9+
// Handle escaped quotes
910
_value = value.Replace("\\\"", "\"");
1011
Type = TypeNames.StringType;
1112
}

PseudocodeInterpreter/src/Program.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace PseudocodeInterpreter
77
{
88
static class Program
99
{
10+
/// <param name="args">
11+
/// Represents the command line args. Should be &lt;filename&gt;.
12+
/// "diagnostics" is optional but should come after the &lt;filename&gt;
13+
/// </param>
1014
public static void Main(string[] args)
1115
{
1216
if (!File.Exists(args[0]))
@@ -20,17 +24,25 @@ public static void Main(string[] args)
2024
{
2125
Interpreter.Output($"> Se executa {args[0]}{Environment.NewLine}");
2226

27+
// Start to count execution time
2328
Stopwatch stopwatch = Stopwatch.StartNew();
2429

25-
Interpreter.ExecuteFile(args);
30+
Interpreter.ExecuteFile(args[0]);
2631

2732
stopwatch.Stop();
2833

29-
Interpreter.Output($"{Environment.NewLine}> Executia a durat: {stopwatch.Elapsed}{Environment.NewLine}");
34+
Interpreter.Output(
35+
$"{Environment.NewLine}> Executia a durat: {stopwatch.Elapsed}{Environment.NewLine}");
3036
Interpreter.Output("> Apasati tasta ENTER pentru a inchide programul...");
37+
38+
// Ask for ENTER-ending input so the user can see the output of the program before quiting
3139
Interpreter.Input();
3240
}
3341
}
42+
else
43+
{
44+
Interpreter.ExecuteFile(args[0]);
45+
}
3446
}
3547
}
3648
}

PseudocodeInterpreter/src/PseudoVisitorImpl.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

PseudocodeInterpreter/src/Scope.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public void CreateVariable(string name, Literal value)
4545

4646
public Literal GetVar(string name)
4747
{
48+
// Go from the last added scope to the first one
4849
for (int i = _scopes.Count - 1; i >= 0; i--)
4950
{
51+
// Searching for the variable by name
5052
if (_scopes[i].Variables.ContainsKey(name))
5153
{
5254
return _scopes[i].Variables[name];
@@ -58,23 +60,28 @@ public Literal GetVar(string name)
5860

5961
public void SetVar(string name, Literal value)
6062
{
61-
for (int i = _scopes.Count - 1; i >= 0; i--)
63+
// Go from the last added scope to the first one
64+
for (int i = _scopes.Count - 1; i >= 0; i--)
6265
{
63-
if (_scopes[i].Variables.ContainsKey(name))
66+
// Searching for the variable by name
67+
if (_scopes[i].Variables.ContainsKey(name))
6468
{
6569
_scopes[i].Variables[name] = value;
6670
return;
6771
}
6872
}
6973

74+
// The name specified does not define a variable
7075
throw new Exception(ErrorMessages.UndefinedSymbol(name));
7176
}
7277

7378
public bool DoesVariableExist(string name)
7479
{
75-
for (int i = _scopes.Count - 1; i >= 0; i--)
80+
// Go from the last added scope to the first one
81+
for (int i = _scopes.Count - 1; i >= 0; i--)
7682
{
77-
if (_scopes[i].Variables.ContainsKey(name))
83+
// Searching for the variable by name
84+
if (_scopes[i].Variables.ContainsKey(name))
7885
{
7986
return true;
8087
}

PseudocodeInterpreter/src/SymbolTable.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)