Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
89 changes: 0 additions & 89 deletions ALFA/CodeGen-p5.js/Output/sketch.js

This file was deleted.

17 changes: 3 additions & 14 deletions ALFA/bin/Debug/net7.0/CodeGen-p5.js/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,11 @@ async function moveParal(animations) {


async function main() {
let var_myRect=new Rect(300,300,100,100)
let var_i=0
let var_Rect1=new Rect(100,100,100,100)

for (let var_j=0
; var_j <= var_i; var_j++){
var_i=var_j + 1
await var_Rect1.move(200,0,4000);

await var_myRect.move(0,40,200);

await wait(100);
if (var_j > 5){
var_i=-1
}
}

await var_myRect.move(100,0,400);
await wait(300);
}

function setup() {
Expand Down
8 changes: 3 additions & 5 deletions ALFA/src/AST Nodes/BuiltInAnimCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace ALFA.AST_Nodes;

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

public BuiltInAnimCallNode(ALFATypes.BuiltInAnimEnum type, List<Node> arguments, int line, int col) : base(line, col)
public BuiltInAnimCallNode(ALFATypes.BuiltInAnimEnum type, List<Node> arguments, int line, int col) : base(line, col, arguments)
{
Type = type;
Arguments = arguments;
}
}
8 changes: 3 additions & 5 deletions ALFA/src/AST Nodes/BuiltInParalAnimCallNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace ALFA.AST_Nodes;

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

public BuiltInParalAnimCallNode(ALFATypes.BuiltInParalAnimEnum type, List<Node> arguments, int line, int col) : base(line, col)
public BuiltInParalAnimCallNode(ALFATypes.BuiltInParalAnimEnum type, List<Node> arguments, int line, int col) : base(line, col, arguments)
{
Type = type;
Arguments = arguments;
}
}
12 changes: 12 additions & 0 deletions ALFA/src/Helpers/AnimCallNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ALFA.AST_Nodes;
using ALFA.Types;

public abstract class AnimCallNode<T> : Node
{
public List<Node> Arguments { get; set; }
public abstract T Type { get; set; }
public AnimCallNode(int line, int col, List<Node> arguments) : base(line, col)
{
Arguments = arguments;
}
}
131 changes: 50 additions & 81 deletions ALFA/src/Visitors/TypeCheckVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class TypeCheckVisitor : ASTVisitor<Node>
{
private SymbolTable _symbolTable;

//TODO move typecheck vardcl here
public TypeCheckVisitor(SymbolTable symbolTable)
{
_symbolTable = symbolTable;
Expand All @@ -24,29 +23,16 @@ public override ProgramNode Visit(ProgramNode node)
return node;
}

private void typeCheckIdNodeVarDcl(IdNode idNode, ALFATypes.TypeEnum type)
private void TypeCheckIdNodeVarDcl(IdNode idNode, ALFATypes.TypeEnum type)
{
Symbol symbol = _symbolTable.RetrieveSymbol(idNode.Identifier);
if (symbol.Type != type)
if (symbol != null && symbol.Type != type)
{
throw new TypeException($"You are assigning something of type {symbol.Type} on line {symbol.LineNumber} column {symbol.ColumnNumber} to a variable of type {type.ToString()}");
}
}


public override Node Visit(VarDclNode node)
{
Visit(node.AssignStmt);

if (node.AssignStmt.Value is IdNode idNode)
{
typeCheckIdNodeVarDcl(idNode, node.Type);
}

return node;
}

public override BuiltInAnimCallNode Visit(BuiltInAnimCallNode node)
private void TypeCheckArgsInAnimCallNode<T>(AnimCallNode<T> node)
{
List<ALFATypes.TypeEnum> nodeFormalParameters = FormalParameters.FormalParams[node.Type.ToString()];

Expand All @@ -55,83 +41,70 @@ public override BuiltInAnimCallNode Visit(BuiltInAnimCallNode node)
throw new InvalidNumberOfArgumentsException(
$"Invalid number of arguments to {node.Type.ToString()}, expected {nodeFormalParameters.Count} but got {node.Arguments.Count} arguments");
}

int i = 0;
foreach (var actualParam in node.Arguments)
{
if (actualParam is IdNode idNode)
{
TypeCheckArgNode<T>(actualParam, i++, node);
}
}

private void TypeCheckArgNode<T>(Node actualParam, int i, AnimCallNode<T> node)
{
List<ALFATypes.TypeEnum> nodeFormalParameters = FormalParameters.FormalParams[node.Type.ToString()];
switch (actualParam)
{
case IdNode idNode:
Symbol? idSymbol = _symbolTable.RetrieveSymbol(idNode.Identifier);
if (idSymbol != null)
{
if (idSymbol.Type != FormalParameters.FormalParams[node.Type.ToString()][i])
if (idSymbol.Type != FormalParameters.FormalParams[node.Type.ToString()!][i])
throw new ArgumentTypeException($"Invalid type, expected {nodeFormalParameters[i]} but got {idSymbol.Type} on line {idNode.Line}:{idNode.Col}");

if (i == FormalParameters.FormalParams[node.Type.ToString()].Count() - 1)
{
if(idSymbol.Value is AssignStmtNode assStmt && assStmt.Value is NumNode numNode && numNode.Value <= 0)
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {idSymbol.LineNumber} column {idSymbol.ColumnNumber}");
else if (idSymbol.Value is NumNode numNode1 && numNode1.Value <= 0)
{
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {idSymbol.LineNumber} column {idSymbol.ColumnNumber}");
}
}
TypeCheckArgNode<T>(idSymbol.Value, i, node);
}
}
else if (actualParam is NumNode numNode)
{
if (nodeFormalParameters[i] != ALFATypes.TypeEnum.@int)
throw new ArgumentTypeException($"Invalid type expected {nodeFormalParameters[i]} but got {ALFATypes.TypeEnum.@int} on line {numNode.Line}:{numNode.Col}");
if(i == FormalParameters.FormalParams[node.Type.ToString()].Count() - 1 && numNode.Value <= 0)
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {numNode.Line} column {numNode.Col}");

}
else if (actualParam is ExprNode exprNode)
{
break;

case NumNode numNode when nodeFormalParameters[i] != ALFATypes.TypeEnum.@int:
throw new ArgumentTypeException($"Invalid type expected {nodeFormalParameters[i]} but got {ALFATypes.TypeEnum.@int} on line {numNode.Line}:{numNode.Col}");

case NumNode numNode when i == FormalParameters.FormalParams[node.Type.ToString()!].Count() - 1 && numNode.Value <= 0:
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {numNode.Line} column {numNode.Col}");

case BoolNode boolNode when nodeFormalParameters[i] != ALFATypes.TypeEnum.@bool:
throw new ArgumentTypeException($"Invalid type expected {nodeFormalParameters[i]} but got {ALFATypes.TypeEnum.@bool} on line {boolNode.Line}:{boolNode.Col}");

case ExprNode exprNode:
Visit(exprNode);
if (i == FormalParameters.FormalParams[node.Type.ToString()].Count() - 1 && exprNode.Value is NumNode exprNumNode && exprNumNode.Value <= 0)
if (i == FormalParameters.FormalParams[node.Type.ToString()!].Count() - 1 && exprNode.Value is NumNode exprNumNode && exprNumNode.Value <= 0)
{
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {exprNumNode.Line} column {exprNumNode.Col}");
}
}
i++;
break;
}
return node;
}


public override Node Visit(BuiltInParalAnimCallNode node)
public override Node Visit(VarDclNode node)
{
List<ALFATypes.TypeEnum> nodeFormalParameters = FormalParameters.FormalParams[node.Type.ToString()];
Visit(node.AssignStmt);

if (node.Arguments.Count != nodeFormalParameters.Count)
if (node.AssignStmt.Value is IdNode idNode)
{
throw new InvalidNumberOfArgumentsException(
$"Invalid number of arguments to {node.Type.ToString()}, expected {nodeFormalParameters.Count} but got {node.Arguments.Count} arguments");
TypeCheckIdNodeVarDcl(idNode, node.Type);
}

return node;
}

int i = 0;
foreach (var actualParam in node.Arguments)
{
if (actualParam is IdNode idNode)
{
Visit(actualParam); //Not needed now but it is more extensible
Symbol? idSymbol = _symbolTable.RetrieveSymbol(idNode.Identifier);
if (idSymbol != null)
{
if (idSymbol.Type != FormalParameters.FormalParams[node.Type.ToString()][i])
throw new ArgumentTypeException($"Invalid type, expected {nodeFormalParameters[i]} but got {idSymbol.Type} on line {idNode.Line}:{idNode.Col}");
}
}
else if (actualParam is ExprNode exprNode)
{
Visit(exprNode);
if (i == FormalParameters.FormalParams[node.Type.ToString()].Count() - 1 && exprNode.Value is NumNode exprNumNode && exprNumNode.Value <= 0)
{
throw new NonPositiveAnimationDurationException($"The duration of an animation must be greater than 0 on line {exprNumNode.Line} column {exprNumNode.Col}");
}
}
i++;
}
public override BuiltInAnimCallNode Visit(BuiltInAnimCallNode node)
{
TypeCheckArgsInAnimCallNode<ALFATypes.BuiltInAnimEnum>(node);
return node;
}

public override Node Visit(BuiltInParalAnimCallNode node)
{
TypeCheckArgsInAnimCallNode<ALFATypes.BuiltInParalAnimEnum>(node);
return node;
}

Expand All @@ -157,6 +130,8 @@ public override BuiltInCreateShapeCallNode Visit(BuiltInCreateShapeCallNode call
if (idSymbol.Type != FormalParameters.FormalParams[callNode.Type.ToString()][i])
throw new ArgumentTypeException($"Invalid type, expected {nodeFormalParameters[i]} but got {idSymbol.Type} on line {idNode.Line}:{idNode.Col}");
}

TypeCheckIdNodeVarDcl(idNode, FormalParameters.FormalParams[callNode.Type.ToString()][i]);
}
i++;
}
Expand All @@ -166,10 +141,6 @@ public override BuiltInCreateShapeCallNode Visit(BuiltInCreateShapeCallNode call
public override AssignStmtNode Visit(AssignStmtNode assNode)
{
Symbol? idSymbol = _symbolTable.RetrieveSymbol(assNode.Identifier);
if (idSymbol == null && assNode.Value == null)
{
throw new UndeclaredVariableException( $"An undeclared variable {assNode.Identifier} is attempted to be assigned on line: {assNode.Line} column: {assNode.Col}");
}
bool visitedChild = false;

if (assNode.Value is ExprNode exprValue)
Expand All @@ -193,9 +164,7 @@ public override AssignStmtNode Visit(AssignStmtNode assNode)

if (assNode.Value is IdNode idChildNode)
{
Symbol? idSymbolChild = _symbolTable.RetrieveSymbol(idChildNode.Identifier);
if (idSymbolChild?.Type != assNode.VarDclParentType || (idSymbol != null && idSymbolChild?.Type != idSymbol.Type))
throw new TypeException($"Invalid type {idSymbolChild?.Type} on line: " + assNode.Line + ": " + "column: " + assNode.Col);
TypeCheckIdNodeVarDcl(idChildNode, assNode.VarDclParentType);
}

if (!visitedChild)
Expand Down