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
1 change: 1 addition & 0 deletions ALFA/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using ALFA;
using ALFA.AST_Nodes;
using ALFA.Visitors;

MyParseMethod();

Expand Down
1 change: 0 additions & 1 deletion ALFA/src/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ public Symbol(string name, int value, ALFATypes.TypeEnum type, int line, int col
ColumnNumber = column;
Type = type;
}

}
10 changes: 5 additions & 5 deletions ALFA/src/Types/ALFATypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ public class ALFATypes
{
public enum TypeEnum
{
Int,
Square
@int,
square
}

public enum BuiltInTypeEnum
{
CreateSquare,
Move,
Wait
createSquare,
move,
wait
}

}
3 changes: 1 addition & 2 deletions ALFA/src/Visitors/ASTPrintVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ALFA.AST_Nodes;
using Antlr4.Runtime.Tree;

namespace ALFA;
namespace ALFA.Visitors;

public class ASTPrintVisitor : ASTVisitor<Node>
{
Expand Down
47 changes: 23 additions & 24 deletions ALFA/src/Visitors/BuildASTVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ALFA.AST_Nodes;
using ALFA.Types;

namespace ALFA;
namespace ALFA.Visitors;

public class BuildASTVisitor : ALFABaseVisitor<Node>
{
Expand All @@ -27,9 +27,7 @@ public override ProgramNode VisitProgram(ALFAParser.ProgramContext context)
public override StatementNode VisitStatement(ALFAParser.StatementContext context)
{
if (context.varDcl() != null)
{
return VisitVarDcl(context.varDcl());
}

return VisitFuncCall(context.funcCall());
}
Expand All @@ -39,29 +37,28 @@ public override VarDclNode VisitVarDcl(ALFAParser.VarDclContext context)
var parent = (ALFAParser.StatementContext)context.Parent;
string id = context.ID().GetText();
ALFATypes.TypeEnum typeEnum;

switch (parent.type().GetText())
{
case "int":
typeEnum = ALFATypes.TypeEnum.Int;
typeEnum = ALFATypes.TypeEnum.@int;
break;
case "square":
typeEnum = ALFATypes.TypeEnum.Square;
typeEnum = ALFATypes.TypeEnum.square;
break;
default:
throw new Exception("Invalid type on line " + context.Start.Line + ":" + context.Start.Column);
}


if (context.funcCall() != null)
{
var funcCall = (FuncCallNode)Visit(context.funcCall());
_symbolTable.EnterSymbol(new Symbol(id, 0, ALFATypes.TypeEnum.Square, context.Start.Line, context.Start.Column));
_symbolTable.EnterSymbol(new Symbol(id, 0, ALFATypes.TypeEnum.square, context.Start.Line, context.Start.Column));
return new VarDclNode(typeEnum, id, funcCall, context.Start.Line, 0);
}

NumNode num = new NumNode(int.Parse(context.NUM().GetText()), context.Start.Line, context.Start.Column);
_symbolTable.EnterSymbol(new Symbol(id, num.Value, ALFATypes.TypeEnum.Int, context.Start.Line, context.Start.Column));
_symbolTable.EnterSymbol(new Symbol(id, num.Value, ALFATypes.TypeEnum.@int, context.Start.Line, context.Start.Column));
return new VarDclNode(typeEnum,id, num, context.Start.Line, 0);
}

Expand All @@ -74,18 +71,18 @@ public override FuncCallNode VisitFuncCall(ALFAParser.FuncCallContext context)
switch (type)
{
case "createSquare":
ALFATypes.TypeEnum[] formalCSParamsArray = {ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int};
ALFATypes.TypeEnum[] formalCSParamsArray = {ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalCSParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.CreateSquare;
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.createSquare;
break;
case "move":
ALFATypes.TypeEnum[] formalMovParamsArray = {ALFATypes.TypeEnum.Square, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int};
formalParams.AddRange(formalMovParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.Move;
ALFATypes.TypeEnum[] formalMoveParamsArray = {ALFATypes.TypeEnum.square, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalMoveParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.move;
break;
case "wait":
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.Wait;
ALFATypes.TypeEnum[] formalWaitParamsArray = {ALFATypes.TypeEnum.Int};
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.wait;
ALFATypes.TypeEnum[] formalWaitParamsArray = {ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalWaitParamsArray);
break;
default:
Expand Down Expand Up @@ -120,19 +117,19 @@ public override BuiltInsNode VisitBuiltIns(ALFAParser.BuiltInsContext context)
switch (type)
{
case "createSquare":
ALFATypes.TypeEnum[] formalCSParamsArray = {ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int};
ALFATypes.TypeEnum[] formalCSParamsArray = {ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalCSParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.CreateSquare;
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.createSquare;
break;
case "move":
ALFATypes.TypeEnum[] formalMovParamsArray = {ALFATypes.TypeEnum.Square, ALFATypes.TypeEnum.Int, ALFATypes.TypeEnum.Int};
ALFATypes.TypeEnum[] formalMovParamsArray = {ALFATypes.TypeEnum.square, ALFATypes.TypeEnum.@int, ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalMovParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.Move;
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.move;
break;
case "wait":
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.Wait;
ALFATypes.TypeEnum[] formalWaitParamsArray = {ALFATypes.TypeEnum.Int};
formalParams.AddRange(formalWaitParamsArray);
builtInTypeEnum = ALFATypes.BuiltInTypeEnum.wait;
ALFATypes.TypeEnum[] formalwaitParamsArray = {ALFATypes.TypeEnum.@int};
formalParams.AddRange(formalwaitParamsArray);
break;
default:
throw new Exception("Invalid built-in function");
Expand All @@ -149,7 +146,9 @@ public override ArgNode VisitArg(ALFAParser.ArgContext context)
if (id != null)
{
Symbol? sym = _symbolTable.RetrieveSymbol(id.GetText());
if (sym == null) throw new Exception($"Variable {id.GetText()} not declared at line {id.Symbol.Line}:{id.Symbol.Column}");
if (sym == null)
throw new Exception($"Variable {id.GetText()} not declared at line {id.Symbol.Line}:{id.Symbol.Column}");

IdNode idNode = new IdNode(id.GetText(), context.Start.Line, context.Start.Column);
return new ArgNode(idNode, context.Start.Line, context.Start.Column);
}
Expand Down
28 changes: 10 additions & 18 deletions ALFA/src/Visitors/TypeCheckVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ALFA.AST_Nodes;
using ALFA.Types;

namespace ALFA;
namespace ALFA.Visitors;

public class TypeCheckVisitor : ASTVisitor<Node>
{
Expand All @@ -20,13 +20,13 @@ public override Node Visit(ProgramNode node)
}
return node;
}

public override Node Visit(StatementNode node)
{
Visit((dynamic)node);
return node;
}

public override Node Visit(FuncCallNode node)
{
if (node.Arguments.Count != node.BuiltIns.FormalParams.Count)
Expand All @@ -43,17 +43,13 @@ public override Node Visit(FuncCallNode node)
if (idSymbol != null)
{
if (idSymbol.Type != node.BuiltIns.FormalParams[i])
{
throw new Exception($"Invalid type, expected {node.BuiltIns.FormalParams[i]} but got {idSymbol.Type} on line {idNode.Line}:{idNode.Col}");
}
}
}
else if (actualParam.Value is NumNode numNode)
{
if (node.BuiltIns.FormalParams[i] != ALFATypes.TypeEnum.Int)
{
throw new Exception($"Invalid type expected {node.BuiltIns.FormalParams[i]} but got int on line {numNode.Line}:{numNode.Col}");
}
if (node.BuiltIns.FormalParams[i] != ALFATypes.TypeEnum.@int)
throw new Exception($"Invalid type expected {node.BuiltIns.FormalParams[i]} but got {ALFATypes.TypeEnum.@int} on line {numNode.Line}:{numNode.Col}");
}
i++;
}
Expand All @@ -66,22 +62,18 @@ public override Node Visit(VarDclNode node)

if (visitedNode is FuncCallNode)
{
if (node.Type != ALFATypes.TypeEnum.Square)
{
throw new Exception($"Invalid type {node.Type.ToString()}, expected type square on line {node.Line}:{node.Col}");
}
if (node.Type != ALFATypes.TypeEnum.square)
throw new Exception($"Invalid type {node.Type}, expected type {ALFATypes.TypeEnum.square} 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 int on line {node.Line}:{node.Col}");
}
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}");
}

return node;
}

public override Node Visit(BuiltInsNode node) => node;
public override Node Visit(ArgNode node) => node;
public override Node Visit(IdNode node) => node;
Expand Down
9 changes: 7 additions & 2 deletions ALFA/src/antlr/ALFA.interp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ null
'move'
'wait'
','
'int'
'square'
null
null
null
Expand All @@ -22,8 +24,10 @@ null
null
null
null
null
null
ID
INT
NUM
WS

rule names:
Expand All @@ -34,7 +38,8 @@ funcCall
builtIns
args
arg
type


atn:
[4, 1, 11, 51, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 1, 0, 4, 0, 16, 8, 0, 11, 0, 12, 0, 17, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 28, 8, 1, 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, 36, 37, 5, 2, 0, 0, 37, 39, 5, 12, 0, 0, 38, 32, 1, 0, 0, 0, 38, 35, 1, 0, 0, 0, 39, 5, 1, 0, 0, 0, 40, 41, 3, 8, 4, 0, 41, 42, 5, 3, 0, 0, 42, 43, 3, 10, 5, 0, 43, 44, 5, 4, 0, 0, 44, 7, 1, 0, 0, 0, 45, 46, 7, 0, 0, 0, 46, 9, 1, 0, 0, 0, 47, 52, 3, 12, 6, 0, 48, 49, 5, 8, 0, 0, 49, 51, 3, 12, 6, 0, 50, 48, 1, 0, 0, 0, 51, 54, 1, 0, 0, 0, 52, 50, 1, 0, 0, 0, 52, 53, 1, 0, 0, 0, 53, 11, 1, 0, 0, 0, 54, 52, 1, 0, 0, 0, 55, 56, 7, 1, 0, 0, 56, 13, 1, 0, 0, 0, 57, 58, 7, 2, 0, 0, 58, 15, 1, 0, 0, 0, 4, 19, 30, 38, 52]
10 changes: 7 additions & 3 deletions ALFA/src/antlr/ALFA.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ T__4=5
T__5=6
T__6=7
T__7=8
ID=9
INT=10
WS=11
T__8=9
T__9=10
ID=11
NUM=12
WS=13
';'=1
'='=2
'('=3
Expand All @@ -17,3 +19,5 @@ WS=11
'move'=6
'wait'=7
','=8
'int'=9
'square'=10
45 changes: 0 additions & 45 deletions ALFA/src/antlr/ALFA/ALFA.interp

This file was deleted.

23 changes: 0 additions & 23 deletions ALFA/src/antlr/ALFA/ALFA.tokens

This file was deleted.

Loading