Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3e05baa
Added new folder for parsing test
AsbjoernJC Apr 19, 2023
dcb0d97
Added simple test foundation
AsbjoernJC Apr 19, 2023
9678692
Moved some stuff around in source for better clarity and began the id…
AsbjoernJC Apr 20, 2023
39d9ee0
added dir for a parsermocker which will mock parse trees
AsbjoernJC Apr 21, 2023
a9f8dba
Renamed file and started working on the parsertest
AsbjoernJC Apr 25, 2023
7a60549
Testing layout
AsbjoernJC Apr 25, 2023
438b1fd
made the parsertest
BlakRoenne Apr 26, 2023
3e5bd9f
started the AST visitor
BlakRoenne Apr 26, 2023
a306813
Fixed the BuildAstVisitorTest
BlakRoenne Apr 27, 2023
7b68fe1
Added some test programs that should run without an exception
BlakRoenne Apr 27, 2023
d864f7b
Started on the FuncCallNodeTests
BlakRoenne Apr 28, 2023
ccdcef5
Tested vardcl in typecheckvisitor
AsbjoernJC Apr 28, 2023
a196b55
Made FuncCallNodeTests
BlakRoenne Apr 28, 2023
b88e11a
created tests for visit VarDclNode in typechekvisitor
AsbjoernJC Apr 28, 2023
6187aa3
Merge branch '29-mocking-for-alfa' of https://github.com/KORFITZ1DEV/…
AsbjoernJC Apr 28, 2023
67702f4
minor fixes to FuncCallNodeTest
BlakRoenne Apr 28, 2023
84e0bfd
Merge branch '29-mocking-for-alfa' of https://github.com/KORFITZ1DEV/…
BlakRoenne Apr 28, 2023
ce12185
Added file and dir for codegen
AsbjoernJC May 2, 2023
9f9cbc9
done testing on the codegenvisitor
BlakRoenne May 2, 2023
c1d29c3
ploading symboltable due to restart of rider
BlakRoenne May 3, 2023
a6aaced
Changed symboltable so it now works with multiple depths and changed …
BlakRoenne May 4, 2023
4bb988d
did a lil something with the closescope
BlakRoenne May 4, 2023
23275aa
finished test for mvp1
BlakRoenne May 9, 2023
85f9c10
Added a comment as there may be an issue with the symbol table
BlakRoenne May 9, 2023
2365095
Improved structuring
BlakRoenne May 9, 2023
59fef6e
merged main into 29-mocking-for-alfa
BlakRoenne May 9, 2023
dd206ea
Added a few comments
BlakRoenne May 9, 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
26 changes: 26 additions & 0 deletions ALFA/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net7.0/ALFA.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions ALFA/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ALFA.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/ALFA.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/ALFA.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
31 changes: 11 additions & 20 deletions ALFA/CodeGen-p5.js/Output/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,17 @@ class ParalAnimation extends Animation {
}
}

const x1 = 0
const y1 = 0
const x2 = 100
const y2 = 140
const length = 100
const width = 200
const animDuration = 1000
const delay = 300
const myrect1 = new Rectangle(x1,y1,width,length);

const myrect2 = new Rectangle(x2,y2,length,length);

const anim_0 = new MoveAnimation(myrect1,200,animDuration);
const x = 0
const y = 0
const length = 20
const animDuration = 4000
const delay = 2000
const myRect1 = new Rectangle(0,0,length,length);

const anim_0 = new MoveAnimation(myRect1,200,animDuration);
const anim_1 = new WaitAnimation(delay);
const anim_2 = new MoveAnimation(myrect2,300,animDuration);
const anim_3 = new MoveAnimation(myrect1,-200,animDuration);
const anim_4 = new WaitAnimation(delay);
const anim_5 = new MoveAnimation(myrect2,-200,animDuration);
const seqAnim = new SeqAnimation([anim_0,anim_1,anim_2,anim_3,anim_4,anim_5]);
const anim_2 = new MoveAnimation(myRect1,-200,animDuration);
const seqAnim = new SeqAnimation([anim_0,anim_1,anim_2]);

function setup() {
createCanvas(600, 600)
Expand All @@ -137,7 +129,6 @@ function setup() {

function draw() {
background(255)
myrect1.render();
myrect2.render();
myRect1.render();
seqAnim.play();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ public class UndeclaredVariableException : Exception
public UndeclaredVariableException(string message)
: base(message){}
}

public class RedeclaredVariableException : Exception
{
public RedeclaredVariableException(string message) : base(message){}
}
24 changes: 24 additions & 0 deletions ALFA/src/Helpers/FormalParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ALFA.Types;
namespace ALFA;

public static class FormalParameters
{
public static readonly Dictionary<string, BuiltIn> FormalParams = new()
{
{"createRect", new BuiltIn(
new List<ALFATypes.TypeEnum>()
{ ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int },
ALFATypes.BuiltInTypeEnum.createRect)},

{"move", new BuiltIn(new List<ALFATypes.TypeEnum>()
{
ALFATypes.TypeEnum.rect, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int
},ALFATypes.BuiltInTypeEnum.move)},

{"wait", new BuiltIn(new List<ALFATypes.TypeEnum>()
{
ALFATypes.TypeEnum.@int
}, ALFATypes.BuiltInTypeEnum.wait)}
};

}
File renamed without changes.
49 changes: 32 additions & 17 deletions ALFA/src/SymbolTable.cs → ALFA/src/Helpers/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ namespace ALFA;
// Fischer Crafting a Compiler page 292 figure 8.7
public class SymbolTable
{
private int _depth = 0;
private Dictionary<string, Symbol> _symbols = new();
private List<Symbol?>_scopeDisplay = new() {null}; //open the first scope (Global scope) init with null
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++;
_scopeDisplay[_depth] = null;
_scopeDisplay.Add(null);
}

public void CloseScope()
{
Symbol? sym = _scopeDisplay[_depth];
while (sym != null)
{
Symbol? prevSymbol = sym.PrevSymbol;
_symbols.Remove(sym.Name);
//_symbols.Remove(sym.Name);
if (prevSymbol != null)
{
_symbols.Add(prevSymbol.Name, prevSymbol);
Expand All @@ -32,35 +31,39 @@ public void CloseScope()
public void EnterSymbol(Symbol symbol)
{
Symbol? oldSymbol = RetrieveSymbol(symbol.Name);
if (oldSymbol != null && oldSymbol.Depth == _depth)
if (oldSymbol != null)
{
throw new Exception($"Symbol {symbol.Name} already declared on line {oldSymbol.LineNumber}:{oldSymbol.ColumnNumber}");
throw new RedeclaredVariableException(
$"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;

if (oldSymbol == null)
{
_symbols.Add(symbol.Name, newSymbol);
}
else
{
_symbols.Remove(oldSymbol.Name);
if (_symbols.ContainsKey(symbol.Name))
{
oldSymbol = _symbols[symbol.Name];
_symbols.Remove(symbol.Name);
}
_symbols.Add(symbol.Name, newSymbol);
}

//As the oldSymbol is set to previous symbol, one with the same name
//This could result in an issue when closing a scope as the oldsymbol
//may be overwritten and thus maybe lost.
newSymbol.PrevSymbol = oldSymbol!;
}

public Symbol? RetrieveSymbol(string name)
public Symbol? RetrieveSymbol(string name)
{
Symbol? sym;
if (!_symbols.TryGetValue(name, out sym)) return null;
while (sym != null)
{
if (sym.Name == name)
if (sym.Name == name && sym.Depth == _depth)
{
return sym;
}
Expand All @@ -72,6 +75,18 @@ public void EnterSymbol(Symbol symbol)

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

sym = sym.PrevSymbol;
}

return isDeclaredLocally;
}
}
22 changes: 2 additions & 20 deletions ALFA/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,9 @@ public static void Main(string path = "../../../srcprograms/mvp.alfa",string pro
ALFAParser parser = new ALFAParser(tokens);
parser.BuildParseTree = true;
IParseTree tree = parser.program();

Dictionary<string, BuiltIn> formalParams = new()
{
{"createRect", new BuiltIn(
new List<ALFATypes.TypeEnum>()
{ ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int },
ALFATypes.BuiltInTypeEnum.createRect)},

{"move", new BuiltIn(new List<ALFATypes.TypeEnum>()
{
ALFATypes.TypeEnum.rect, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int
},ALFATypes.BuiltInTypeEnum.move)},

{"wait", new BuiltIn(new List<ALFATypes.TypeEnum>()
{
ALFATypes.TypeEnum.@int
}, ALFATypes.BuiltInTypeEnum.wait)}
};


SymbolTable symbolTable = new();
BuildASTVisitor visitor = new BuildASTVisitor(symbolTable, formalParams);
BuildASTVisitor visitor = new BuildASTVisitor(symbolTable);
Node ast = visitor.Visit(tree);
TypeCheckVisitor typeCheckVisitor = new TypeCheckVisitor(symbolTable);
typeCheckVisitor.Visit(ast);
Expand Down
11 changes: 5 additions & 6 deletions ALFA/src/Visitors/BuildASTVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ namespace ALFA.Visitors;
public class BuildASTVisitor : ALFABaseVisitor<Node>
{
private SymbolTable _symbolTable;
private Dictionary<string, BuiltIn> _formalParams;

public BuildASTVisitor(SymbolTable symbolTable, Dictionary<string, BuiltIn> formalParams)
public BuildASTVisitor(SymbolTable symbolTable)
{
_symbolTable = symbolTable;
_formalParams = formalParams;
}

public override ProgramNode VisitProgram(ALFAParser.ProgramContext context)
Expand Down Expand Up @@ -41,7 +39,8 @@ public override VarDclNode VisitVarDcl(ALFAParser.VarDclContext context)
{
var parent = (ALFAParser.StatementContext)context.Parent;
string id = context.ID().GetText();
ALFATypes.TypeEnum typeEnum;
//program should throw an exception if one of the children is a ErrorNodeImpl.
ALFATypes.TypeEnum typeEnum;

switch (parent.type().GetText())
{
Expand Down Expand Up @@ -95,7 +94,7 @@ public override FuncCallNode VisitFuncCall(ALFAParser.FuncCallContext context)
ALFATypes.TypeEnum[] formalMoveParamsArray = {ALFATypes.TypeEnum.rect, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalMoveParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.move;
if (context.args().arg()[0].ID() == null) throw new ArgumentTypeException("You are trying to move something that isnt a rect");
if (context.args().arg()[0].ID() == null) throw new ArgumentTypeException("You are trying to move something that isn't a rect");
identifier = context.args().arg()[0].ID().GetText();
break;
case "wait":
Expand Down Expand Up @@ -146,7 +145,7 @@ public override FuncCallNode VisitFuncCall(ALFAParser.FuncCallContext context)
public override BuiltInsNode VisitBuiltIns(ALFAParser.BuiltInsContext context)
{
var type = context.GetText();
BuiltIn builtIn = _formalParams[type];
BuiltIn builtIn = FormalParameters.FormalParams[type];
return new BuiltInsNode(builtIn.Type, builtIn.FormalParams, context.Start.Line, context.Start.Column);
}
}
14 changes: 7 additions & 7 deletions ALFA/src/Visitors/CodeGenVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace ALFA.Visitors;
public class CodeGenVisitor : ASTVisitor<Node>
{

private string _output = string.Empty;
private string _varOutput = string.Empty;
private string _setupOutput = string.Empty;
private string _drawOutput = string.Empty;
public string _output = string.Empty;
public string _varOutput = string.Empty;
public string _setupOutput = string.Empty;
public string _drawOutput = string.Empty;

private int _animationCount = 0;
private SymbolTable _symbolTable;
private string _path;
public int _animationCount = 0;
public SymbolTable _symbolTable;
public string _path;

public CodeGenVisitor(SymbolTable st, string path)
{
Expand Down
6 changes: 3 additions & 3 deletions ALFA/src/Visitors/TypeCheckVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override FuncCallNode Visit(FuncCallNode node)
{ //think this check is useless as the parser should not allow it
if (node.BuiltIns.FormalParams[i] != ALFATypes.TypeEnum.@int)
throw new ArgumentTypeException($"Invalid type expected {node.BuiltIns.FormalParams[i]} but got {ALFATypes.TypeEnum.@int} on line {numNode.Line}:{numNode.Col}");
}
}
i++;
}
return node;
Expand All @@ -60,12 +60,12 @@ public override VarDclNode Visit(VarDclNode node)
if (visitedNode is FuncCallNode)
{
if (node.Type != ALFATypes.TypeEnum.rect)
throw new Exception($"Invalid type {node.Type}, expected type {ALFATypes.TypeEnum.rect} on line {node.Line}:{node.Col}");
throw new TypeException($"Invalid type {node.Type}, expected type {ALFATypes.TypeEnum.rect} on line {node.Line}:{node.Col}");
}
else if (visitedNode is NumNode)
{
if (node.Type != ALFATypes.TypeEnum.@int)
throw new Exception($"Invalid type {node.Type.ToString()}, expected type {ALFATypes.TypeEnum.@int} on line {node.Line}:{node.Col}");
throw new TypeException($"Invalid type {node.Type.ToString()}, expected type {ALFATypes.TypeEnum.@int} on line {node.Line}:{node.Col}");
}

return node;
Expand Down
1 change: 1 addition & 0 deletions AlfaTest/AlfaTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
14 changes: 12 additions & 2 deletions AlfaTest/BuildAST/BuildAstNoException.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
[
{
"Prog": "",
"Comment": "",
"Prog": "int x1 = 0;\nint y1 = 0;\nint x2 = 100;\nint y2 = 120;\n\nint length = 100;\nint width = 200;\nint animDuration = 1000;\nint delay = 300;\nrect myrect1 = createRect(x1, y1, width, length);\nrect myrect2 = createRect(x2, y2, length, length);\n\nmove(myrect1, 200, animDuration);\nwait(delay);\nmove(myrect2, 300, animDuration);\nmove(myrect1, -200, animDuration);\nwait(delay);\nmove(myrect2, -200, animDuration);",
"Comment": "laver 2 firkanter henholdsvis til højre og derefter venter hvorefter den rykker tilbage.",
"ExceptionType": ""
},
{
"Prog": "int x1 = 0;\nint y1 = 0;\nint x2 = 100;\nint y2 = 120;\n\nint length = 100;\nint width = 200;\nint animDuration = 1000;\nint delay = 300;\nrect myrect1 = createRect(x1, y1, width, length);\nrect myrect2 = createRect(x2, y2, length, length);\n\nmove(myrect1, -200, animDuration);\nwait(delay);\nmove(myrect2, -200, animDuration);",
"Comment" : "laver 2 firkanter og rykker dem til venstre",
"ExceptionType": ""
},
{
"Prog": "rect myrect1 = createRect(100, 100, 20, 20);\nrect myrect2 = createRect(300, 300, 20, 20);\n\nmove(myrect1, -200, 500);\nwait(1000);\nmove(myrect2, -200, 500);",
"Comment": "Laver rect med konstanter",
"ExceptionType": ""
}
]
Loading