summaryrefslogtreecommitdiff
path: root/compiler/Parser/Parser.cs
blob: bd46036201feaae15324195c3480b0a997f47295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using AtLangCompiler.Methods;
using System.Reflection;

namespace AtLangCompiler;

internal class Parser
{
    private readonly Lexer lexer;
    private readonly IReadOnlyDictionary<TokenType, IStatementParser> statementParsers;

    internal Token current;
    internal string varName = string.Empty;

    public Parser(Lexer lexer)
    {
        this.lexer = lexer;
        this.statementParsers = Assembly.GetExecutingAssembly()
            .GetTypes()
            .Where(t => t.CustomAttributes.Any(e => e.AttributeType == typeof(ParserForAttribute)))
            .SelectMany(t => t.GetCustomAttributes<ParserForAttribute>().Select(attr => new
            {
                attr.TokenType,
                ParserType = t
            }))
            .ToDictionary(
                entry => entry.TokenType,
                entry => (IStatementParser)Activator.CreateInstance(entry.ParserType)!
            );

        this.current = this.lexer.NextToken();
    }

    internal void Eat(TokenType type)
    {
        if (current.Type == type)
        {
            current = lexer.NextToken();
        }
        else
        {
            throw new Exception($"Expected {type}, got {current.Type}");
        }
    }

    internal IList<ASTNode> ParseProgram()
    {
        IList<ASTNode> stmts = new List<ASTNode>();
        while (current.Type != TokenType.EOF)
        {
            ASTNode? stmt = ParseStatement();
            if (stmt != null) stmts.Add(stmt);
        }
        return stmts;
    }

    internal ASTNode? ParseStatement()
    {
        if (current.Type == TokenType.AT)
        {
            Eat(TokenType.AT);
            if (current.Type == TokenType.IDENT)
            {
                varName = current.Text;
                Eat(TokenType.IDENT);
                Eat(TokenType.EQUAL);

                if (statementParsers.TryGetValue(current.Type, out var statementParserIdent))
                {
                    return statementParserIdent.ParseStatement(this);
                }

                throw new Exception("Invalid assignment");
            }

            if (statementParsers.TryGetValue(current.Type, out var statementParserAt))
            {
                return statementParserAt.ParseStatement(this);
            }

            throw new Exception("Unexpected after '@'");
        }

        if (statementParsers.TryGetValue(current.Type, out var statementParser))
        {
            return statementParser.ParseStatement(this);
        }

        if (current.Type == TokenType.EOF)
        {
            return null;
        }
        else
        {
            throw new Exception($"Unexpected token {current.Type} in statement");
        }
    }

    internal ASTNode ParseExpression()
    {
        ASTNode left = ParseSimpleExpr();
        while (current.Type == TokenType.PLUS)
        {
            string op = current.Text;
            Eat(TokenType.PLUS);
            ASTNode right = ParseSimpleExpr();
            left = new BinaryExpression(left, op, right);
        }
        return left;
    }

    private ASTNode ParseSimpleExpr()
    {
        if (current.Type == TokenType.AT)
        {
            Eat(TokenType.AT);
            string name = current.Text;
            Eat(TokenType.IDENT);
            return new VarReference(name);
        }
        else if (current.Type == TokenType.STRING)
        {
            string val = current.Text;
            Eat(TokenType.STRING);
            return new StringLiteral(val);
        }
        else
        {
            throw new Exception($"Unexpected token {current.Type} in expression");
        }
    }
}