Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
07f1c7b
Added new case in VisitSymbol for exprnode, didnt visit block in if s…
AsbjoernJC Jun 23, 2023
a594de1
Fixed wrong assertion of typeerror
AsbjoernJC Jun 23, 2023
4edb767
Fixed wrong assertion of typeerror
AsbjoernJC Jun 23, 2023
65722e8
Fixed wrong assertion of typeerror
AsbjoernJC Jun 23, 2023
cfc24ef
Added test
AsbjoernJC Jun 23, 2023
9e7d37e
working version of symbol table
AsbjoernJC Jun 23, 2023
d83de77
Improved line and col specification in visitsymbol on error
AsbjoernJC Jun 23, 2023
5e0382a
Added test and fixed issue with and operator in tcv
AsbjoernJC Jun 23, 2023
9ab7c7d
Removed assignment of operator to parent expr node
AsbjoernJC Jun 23, 2023
3ecea69
Removed scopedisplay
AsbjoernJC Jun 23, 2023
5c75b91
Changed test name
AsbjoernJC Jun 24, 2023
75d0425
Changed test name
AsbjoernJC Jun 24, 2023
539d630
Changed param in test
AsbjoernJC Jun 24, 2023
d84957d
Pulled frederiks symbol table in
AsbjoernJC Jun 24, 2023
3e85711
Fixed bug during demo
AsbjoernJC Jun 24, 2023
f1c529c
Imrpoved error message
AsbjoernJC Jun 24, 2023
1c30264
Imrpoved error message
AsbjoernJC Jun 24, 2023
3721aed
Fixed numnode errormessage
AsbjoernJC Jun 24, 2023
78613be
ChangedTestName
AsbjoernJC Jun 24, 2023
acb49d1
Added comments
AsbjoernJC Jun 24, 2023
b15726f
Small changes
AsbjoernJC Jun 24, 2023
95822a0
Binary minus fix
AsbjoernJC Jun 24, 2023
f00860f
added test for unary minus
AsbjoernJC Jun 24, 2023
475ea51
Fixed issue where wait(-100+100); would not throw exception
AsbjoernJC Jun 24, 2023
636d2ce
Cleaned up in the visitsymbol cases
AsbjoernJC Jun 25, 2023
2ef0bf6
Update ALFA.g4
AsbjoernJC Jun 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ALFA/ALFA.g4
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ loopStmt: 'loop' '(' 'int' ID 'from' expr '..' expr ')' block;
paralStmt: 'paral' paralBlock;

expr
: '(' expr ')' #Parens
: '-'?NUM #Num
| ID #Id
| bool #Boolean
//Precedence rules
| '(' expr ')' #Parens
| '!' expr #Not
| '-' expr #UnaryMinus
| expr op=('*' | '/' | '%') expr #MulDiv
Expand All @@ -32,9 +36,6 @@ expr
| expr op=('==' | '!=') expr #Equality
| expr 'and' expr #And
| expr 'or' expr #Or
| ID #Id
| NUM #Num
| bool #Boolean
;

block: '{' stmt* '}';
Expand All @@ -53,7 +54,7 @@ actualParams: (expr (',' expr)*)?;

COMMENT: '#' ~[\r\n]* -> skip;
ID: [a-zA-Z_][a-zA-Z0-9_]*;
NUM: '0'| '-'?[1-9][0-9]*;
NUM: '0'| [1-9][0-9]*;
type: 'int' | 'bool' | 'rect';
bool: 'true' | 'false';
WS: [ \t\r\n]+ -> skip;
WS: [ \t\r\n]+ -> skip;
8 changes: 1 addition & 7 deletions ALFA/src/AST Nodes/AssignStmtNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ public class AssignStmtNode : Node
public string Identifier { get; set; }
public Node Value { get; set; }
public ALFATypes.TypeEnum VarDclParentType { get; set; }

public AssignStmtNode(int line, int col) : base(line, col)
{
}
public AssignStmtNode(string identifier, Node value, int line, int col) : base(line, col)
{
Identifier = identifier;
Value = value;
}
public AssignStmtNode(string identifier, Node value, int line, int col, ALFATypes.TypeEnum varDclParentType) : base(line, col)
{
Identifier = identifier;
Value = value;
VarDclParentType = varDclParentType;
}
}
2 changes: 0 additions & 2 deletions ALFA/src/AST Nodes/BuiltInAnimCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace ALFA.AST_Nodes;

public class BuiltInAnimCallNode : AnimCallNode<ALFATypes.BuiltInAnimEnum>
{
public override ALFATypes.BuiltInAnimEnum Type { get; set; }

public BuiltInAnimCallNode(ALFATypes.BuiltInAnimEnum type, List<Node> arguments, int line, int col) : base(line, col, arguments)
{
Type = type;
Expand Down
4 changes: 0 additions & 4 deletions ALFA/src/AST Nodes/BuiltInCreateShapeCallNode.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using ALFA.Types;

namespace ALFA.AST_Nodes;

public class BuiltInCreateShapeCallNode : AnimCallNode<ALFATypes.CreateShapeEnum>
{
public override ALFATypes.CreateShapeEnum Type { get; set; } //createRect
public List<Node> Arguments { get; set; } // can consist of NumNode or IdNode


public BuiltInCreateShapeCallNode(ALFATypes.CreateShapeEnum type, List<Node> arguments, int line, int col) : base(line, col, arguments)
{
Expand Down
2 changes: 0 additions & 2 deletions ALFA/src/AST Nodes/BuiltInParalAnimCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace ALFA.AST_Nodes;

public class BuiltInParalAnimCallNode : AnimCallNode<ALFATypes.BuiltInParalAnimEnum>
{
public override ALFATypes.BuiltInParalAnimEnum Type { get; set; }

public BuiltInParalAnimCallNode(ALFATypes.BuiltInParalAnimEnum type, List<Node> arguments, int line, int col) : base(line, col, arguments)
{
Type = type;
Expand Down
2 changes: 1 addition & 1 deletion ALFA/src/AST Nodes/ExprNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ExprNode(string op, Node left, Node right, int line, int col) : base(line
Left = left;
Right = right;
}
public ExprNode(string op, Node left, int line, int col) : base(line, col)
public ExprNode(string op, Node left, int line, int col) : base(line, col) // unary
{
Operator = op;
Left = left;
Expand Down
2 changes: 1 addition & 1 deletion ALFA/src/AST Nodes/IdNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class IdNode : Node
{
public string Identifier { get; set; }
public Node? LocalValue { get; set; }

public IdNode(string identifier, int line, int col) : base(line, col)
{
Identifier = identifier;
Expand Down
8 changes: 6 additions & 2 deletions ALFA/src/Helpers/AnimCallNode.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace ALFA.AST_Nodes;
using System.Diagnostics.CodeAnalysis;

namespace ALFA.AST_Nodes;
using ALFA.Types;

[ExcludeFromCodeCoverage]
public abstract class AnimCallNode<T> : Node
{
public List<Node> Arguments { get; set; }
public abstract T Type { get; set; }
public T Type { get; set; }

public AnimCallNode(int line, int col, List<Node> arguments) : base(line, col)
{
Arguments = arguments;
Expand Down
83 changes: 20 additions & 63 deletions ALFA/src/Helpers/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,58 @@ public class SymbolTable
{
public int _depth = 0;
public Dictionary<string, Symbol> _symbols = new();
public List<Symbol?>_scopeDisplay = new() {null}; //open the first scope (Global scope) init with null

public void OpenScope() => _depth++;

public void OpenScope()
{
_depth++;
if (_scopeDisplay.Count != _depth + 1)
_scopeDisplay.Add(null);
}
public void CloseScope()
{
Symbol? sym = _scopeDisplay[_depth];
bool removedSymbol = false;
Symbol? origSym = _scopeDisplay[_depth];
while (sym != null)
foreach (var symbol in _symbols.Values.Where(s => s.Depth == _depth))
{
Symbol? prevSymbol = sym.PrevSymbol;
// If a variable is declared under the same name in an outer scope (when sym.PrevSymbol != null)
// it should be added to the dictionary symbols dictionary.
if (prevSymbol != null)
if (symbol.PrevSymbol != null)
{
_symbols.Remove(sym.Name);
removedSymbol = true;
_symbols.Add(prevSymbol.Name, prevSymbol);
_symbols[symbol.Name] = symbol.PrevSymbol;
continue;
}

sym = prevSymbol;
_symbols.Remove(symbol.Name);
}

if (!removedSymbol && origSym != null) _symbols.Remove(origSym.Name);

_depth--;
}

public void EnterSymbol(Symbol symbol)
{
Symbol? oldSymbol = RetrieveSymbol(symbol.Name);
if (oldSymbol != null && oldSymbol.Depth == _depth) //
{
Symbol? oldSymbol = RetrieveSymbol(symbol.Name); // check if symbol already exists
if (oldSymbol != null && oldSymbol.Depth == _depth) // if symbol exists in current scope
throw new VariableAlreadyDeclaredException(
$"Symbol {symbol.Name} already declared on line {oldSymbol.LineNumber}:{oldSymbol.ColumnNumber}");
}

Symbol newSymbol = new(symbol.Name, symbol.Value, symbol.Type, symbol.LineNumber, symbol.ColumnNumber);
newSymbol.Depth = _depth;
_scopeDisplay[_depth] = newSymbol;
// no need to construct a new symbol, but still need to update depth
symbol.Depth = _depth;

//If there is a variable declared with the same name on a lower depth
if (oldSymbol == null || oldSymbol.Depth < _depth)
// if shadowing
if (oldSymbol != null && oldSymbol.Depth < _depth)
{
if (_symbols.ContainsKey(symbol.Name))
{
oldSymbol = _symbols[symbol.Name];
_symbols.Remove(symbol.Name);
}
_symbols.Add(symbol.Name, newSymbol);
symbol.PrevSymbol = oldSymbol;
_symbols[oldSymbol.Name] = symbol;
return;
}

//Sets the oldSymbol with the same name as the newSymbol to be the PrevSymbol
//because newSymbol is in the nearest scope with the name.
newSymbol.PrevSymbol = oldSymbol!;
_symbols.Add(symbol.Name, symbol);
}

public Symbol? RetrieveSymbol(string name)
public Symbol? RetrieveSymbol(string name)
{
Symbol? sym;
sym = _symbols.TryGetValue(name, out sym) ? sym : null;

while (sym != null)
{
if (sym.Name == name && sym.Depth <= _depth)
{
if (sym.Name == name && sym.Depth <= _depth)
return sym;
}

sym = sym.PrevSymbol;
}

return sym;
}

public bool DeclaredLocally(string name)
{
bool isDeclaredLocally = false;
Symbol? sym = _scopeDisplay[_depth];
while (sym != null)
{
if (sym.Name == name)
{
isDeclaredLocally = true;
}

sym = sym.PrevSymbol;
}

return isDeclaredLocally;
}
}
}
8 changes: 3 additions & 5 deletions ALFA/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ALFA
[ExcludeFromCodeCoverage]
public static class Prog
{

public static void Main(string[] args)
{
string input = String.Empty;
Expand All @@ -24,8 +24,6 @@ public static void Main(string[] args)
if (args.Length == 0)
throw new Exception("Missing input arguments. Please provide a .alfa file, example: alfa ./path/to/file.alfa");



if (args.Contains("--test")) // test mode
{
input = args[0];
Expand Down Expand Up @@ -84,7 +82,7 @@ public static void Main(string[] args)
throw new SyntacticException($"Something is syntactically incorrect: {syntacticErrorNode.GetText()} on line {syntacticErrorNode.Payload?.ToString()?.Split(",")[3].Split(":")[0]} column {syntacticErrorNode.Payload.ToString().Split(",")[3].Split(":")[1]}");
}

SymbolTable symbolTable = new();
SymbolTable symbolTable = new SymbolTable();
BuildASTVisitor visitor = new BuildASTVisitor(symbolTable);
Node ast = visitor.Visit(tree);
TypeCheckVisitor typeCheckVisitor = new TypeCheckVisitor(symbolTable);
Expand All @@ -99,7 +97,7 @@ public static void Main(string[] args)

private static ErrorNodeImpl? findErrorNode(IParseTree context)
{

for (int i = 0; i < context.ChildCount; i++)
{
var child = context.GetChild(i);
Expand Down
Loading