Added Parser
- can parse a basic token list to an AST
This commit is contained in:
parent
ea26acce4a
commit
b3fe78fffb
19 changed files with 451 additions and 88 deletions
17
Compiler/Parser/Exceptions/InvalidIdentifierException.cs
Normal file
17
Compiler/Parser/Exceptions/InvalidIdentifierException.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace Compiler.Parser.Exceptions
|
||||
{
|
||||
public class InvalidIdentifierException : Exception
|
||||
{
|
||||
private string value { get; set; }
|
||||
|
||||
public override string Message { get; }
|
||||
|
||||
public InvalidIdentifierException(string value)
|
||||
{
|
||||
this.value = value;
|
||||
this.Message = "Unexpected Identifier " + this.value + ".";
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Compiler/Parser/Exceptions/MissingSemicolonException.cs
Normal file
9
Compiler/Parser/Exceptions/MissingSemicolonException.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace Compiler.Parser.Exceptions
|
||||
{
|
||||
public class MissingSemicolonException : Exception
|
||||
{
|
||||
public override string Message { get; }
|
||||
}
|
||||
}
|
||||
20
Compiler/Parser/Exceptions/UnexpectedTokenException.cs
Normal file
20
Compiler/Parser/Exceptions/UnexpectedTokenException.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using Compiler.Lexer;
|
||||
|
||||
namespace Compiler.Parser.Exceptions
|
||||
{
|
||||
public class UnexpectedTokenException : Exception
|
||||
{
|
||||
public TokenType expected { get; set; }
|
||||
public TokenType got { get; set; }
|
||||
|
||||
public override string Message { get; }
|
||||
|
||||
public UnexpectedTokenException(TokenType expected, TokenType got)
|
||||
{
|
||||
this.expected = expected;
|
||||
this.got = got;
|
||||
this.Message = "Unexpected Token " + got + ". Expected: " + expected;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Compiler/Parser/Exceptions/WrongTypeException.cs
Normal file
21
Compiler/Parser/Exceptions/WrongTypeException.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
|
||||
namespace Compiler.Parser.Exceptions
|
||||
{
|
||||
public class WrongTypeException : Exception
|
||||
{
|
||||
private Type expected;
|
||||
private Type got;
|
||||
|
||||
public override string Message { get; }
|
||||
|
||||
public WrongTypeException(Type expected, Type got)
|
||||
{
|
||||
this.expected = expected;
|
||||
this.got = got;
|
||||
|
||||
this.Message = "Expected type " + expected +
|
||||
"but got type " + got + ".";
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Compiler/Parser/Nodes/ConstantNode.cs
Normal file
14
Compiler/Parser/Nodes/ConstantNode.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace Compiler.Parser.Nodes
|
||||
{
|
||||
public sealed class ConstantNode : Node
|
||||
{
|
||||
public override NodeType NodeType { get; set; }
|
||||
public int value { get; set; }
|
||||
|
||||
public ConstantNode(int value)
|
||||
{
|
||||
this.NodeType = NodeType.ExpressionNode;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Compiler/Parser/Nodes/FunctionNode.cs
Normal file
15
Compiler/Parser/Nodes/FunctionNode.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
|
||||
namespace Compiler.Parser.Nodes
|
||||
{
|
||||
public sealed class FunctionNode : Node
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public override NodeType NodeType { get; set; }
|
||||
|
||||
public FunctionNode()
|
||||
{
|
||||
this.NodeType = NodeType.FunctionNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Compiler/Parser/Nodes/Node.cs
Normal file
15
Compiler/Parser/Nodes/Node.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Compiler.Parser.Nodes
|
||||
{
|
||||
public abstract class Node
|
||||
{
|
||||
public abstract NodeType NodeType { get; set; }
|
||||
public List<Node> Children { get; set; }
|
||||
|
||||
protected Node()
|
||||
{
|
||||
Children = new List<Node>();
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Compiler/Parser/Nodes/NodeType.cs
Normal file
10
Compiler/Parser/Nodes/NodeType.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
namespace Compiler.Parser
|
||||
{
|
||||
public enum NodeType
|
||||
{
|
||||
ProgramNode,
|
||||
FunctionNode,
|
||||
StatementNode,
|
||||
ExpressionNode
|
||||
}
|
||||
}
|
||||
12
Compiler/Parser/Nodes/ProgramNode.cs
Normal file
12
Compiler/Parser/Nodes/ProgramNode.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
namespace Compiler.Parser.Nodes
|
||||
{
|
||||
public sealed class ProgramNode : Node
|
||||
{
|
||||
public override NodeType NodeType { get; set; }
|
||||
|
||||
public ProgramNode()
|
||||
{
|
||||
this.NodeType = NodeType.ProgramNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Compiler/Parser/Nodes/ReturnNode.cs
Normal file
12
Compiler/Parser/Nodes/ReturnNode.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
namespace Compiler.Parser.Nodes
|
||||
{
|
||||
public sealed class ReturnNode : Node
|
||||
{
|
||||
public override NodeType NodeType { get; set; }
|
||||
|
||||
public ReturnNode()
|
||||
{
|
||||
this.NodeType = NodeType.StatementNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Compiler/Parser/Parser.cs
Normal file
130
Compiler/Parser/Parser.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Compiler.Lexer;
|
||||
using Compiler.Parser.Exceptions;
|
||||
using Compiler.Parser.Nodes;
|
||||
|
||||
namespace Compiler.Parser
|
||||
{
|
||||
public class Parser
|
||||
{
|
||||
public Node Parse(ref List<Token> tokenList, NodeType nodeType)
|
||||
{
|
||||
var t = tokenList.FirstOrDefault();
|
||||
|
||||
if (t == null) throw new Exception("Empty Token List.");
|
||||
Node n;
|
||||
switch (nodeType)
|
||||
{
|
||||
case NodeType.ProgramNode:
|
||||
n = new ProgramNode();
|
||||
|
||||
//if this node is a program node, the next one must be a function node
|
||||
Node childNode = Parse(ref tokenList, NodeType.FunctionNode);
|
||||
n.Children.Add(childNode);
|
||||
break;
|
||||
|
||||
case NodeType.FunctionNode:
|
||||
n = new FunctionNode();
|
||||
|
||||
//retrieve signature and remove it
|
||||
List<Token> signature = tokenList.GetRange(0, 5);
|
||||
tokenList.RemoveRange(0, 5);
|
||||
|
||||
//check each element of the signature and raise corresponding errors
|
||||
if (signature[0].TokenType != TokenType.IntToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.IntToken, signature[1].TokenType);
|
||||
}
|
||||
|
||||
if (signature[1].Value != null)
|
||||
{
|
||||
if (signature[1].TokenType == TokenType.IdentifierToken)
|
||||
{
|
||||
((FunctionNode) n).Name = signature[1].Value.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.IdentifierToken, signature[1].TokenType);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidIdentifierException(null);
|
||||
}
|
||||
|
||||
if (signature[2].TokenType != TokenType.OpenParenthesisToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.OpenParenthesisToken, signature[1].TokenType);
|
||||
}
|
||||
|
||||
if (signature[3].TokenType != TokenType.CloseParenthesisToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.CloseParenthesisToken, signature[1].TokenType);
|
||||
}
|
||||
|
||||
if (signature[4].TokenType != TokenType.OpenBraceToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.OpenBraceToken, signature[1].TokenType);
|
||||
}
|
||||
|
||||
//add returned child node to AST
|
||||
n.Children.Add(Parse(ref tokenList, NodeType.StatementNode));
|
||||
|
||||
//remove trailing }
|
||||
tokenList.RemoveAt(0);
|
||||
break;
|
||||
case NodeType.StatementNode:
|
||||
|
||||
//TODO: This Type of return/statement node will probably need fixing later
|
||||
n = new ReturnNode();
|
||||
|
||||
//get return token and remove it
|
||||
List<Token> returnStatement = tokenList.GetRange(0, 1);
|
||||
tokenList.RemoveAt(0);
|
||||
|
||||
if (returnStatement[0].TokenType != TokenType.ReturnToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.ReturnToken, returnStatement[0].TokenType);
|
||||
}
|
||||
else
|
||||
{
|
||||
//add returned child node to AST
|
||||
n.Children.Add(Parse(ref tokenList, NodeType.ExpressionNode));
|
||||
//remove trailing ;
|
||||
tokenList.RemoveAt(0);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case NodeType.ExpressionNode:
|
||||
|
||||
Token constantToken = tokenList[0];
|
||||
//check if TokenType is right
|
||||
if (constantToken.TokenType != TokenType.IntegerLiteralToken)
|
||||
{
|
||||
throw new UnexpectedTokenException(TokenType.IntToken, constantToken.TokenType);
|
||||
}
|
||||
else
|
||||
{
|
||||
//check if value Type is right
|
||||
if (constantToken.Value.GetType() != typeof(int))
|
||||
{
|
||||
throw new WrongTypeException(typeof(int), constantToken.Value.GetType());
|
||||
}
|
||||
|
||||
//return final constant node to end recursion
|
||||
n = new ConstantNode((int) constantToken.Value);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception("Unknown Node Type " + nodeType);
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue