summaryrefslogtreecommitdiff
path: root/src/parser.zig
blob: df0f068bd121ea8c2b46483429ec9743c90dca38 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const std = @import("std");
const tok = @import("tokenize.zig");
const Iterator = tok.Iterator;
const Token = tok.Token;

const ParsingError = error{
    InvalidExpression,
    ExpectedExit,
    ExpectedSemicolon,
    ExpectedEqual,
    ExpectedIdentifier,
    InvalidStatement,
};

pub const NodeExpr = union(enum) {
    intLit: NodeIntlit,
    ident: NodeIdent,
};

pub const NodeStmt = union(enum) {
    exit: NodeExit,
    value: NodeValue,
    assign: NodeAssign,
};

pub const NodeAssign = struct {
    ident: Token,
    value: NodeExpr,
};

pub const NodeValue = struct {
    ident: Token,
    value: NodeExpr,
    isConst: bool,
};

pub const NodeExit = struct {
    expr: NodeExpr,
};

pub const NodeIntlit = struct {
    intlit: Token,
};

pub const NodeIdent = struct {
    ident: Token,
};

pub const Parser = struct {
    tokens: Iterator(Token),
    allocator: std.mem.Allocator,
    nodes: std.ArrayList(NodeStmt),

    pub fn init(allocator: std.mem.Allocator, tokens: []Token) Parser {
        return .{
            .allocator = allocator,
            .tokens = Iterator(Token).init(tokens),
            .nodes = std.ArrayList(NodeStmt).init(allocator),
        };
    }

    pub fn deinit(self: *Parser) void {
        self.nodes.deinit();
    }

    fn parseExpr(self: *Parser) !NodeExpr {
        return switch (self.tokens.peek().?) {
            .intLit => NodeExpr{
                .intLit = NodeIntlit{
                    .intlit = (try self.tokens.consume(.intLit)).?,
                },
            },
            .ident => NodeExpr{
                .ident = NodeIdent{
                    .ident = (try self.tokens.consume(.ident)).?,
                },
            },
            else => ParsingError.InvalidExpression,
        };
    }

    fn parseStmt(self: *Parser) !NodeStmt {
        return switch (self.tokens.peek().?) {
            .exit => NodeStmt{ .exit = try self.parseExit() },
            .constant => NodeStmt{ .value = try self.parseValue(true) },
            .variable => NodeStmt{ .value = try self.parseValue(false) },
            .ident => NodeStmt{ .assign = try self.parseAssign() },
            else => ParsingError.InvalidStatement,
        };
    }

    fn parseAssign(self: *Parser) !NodeAssign {
        const ident = try self.tokens.consume(.ident);
        var isMutable = false;
        var exists = false;
        for (self.nodes.items) |item| {
            switch (item) {
                .value => |v| {
                    if (std.mem.eql(u8, v.ident.ident, ident.?.ident)) {
                        isMutable = !v.isConst;
                        exists = true;
                    }
                },
                else => {},
            }
        }
        if (!exists) return error.UnknownIdentifier;
        if (!isMutable) return error.ImmutableValue;
        _ = try self.tokens.consume(.equal);
        const expr = try self.parseExpr();
        _ = try self.tokens.consume(.semiCol);
        return NodeAssign{
            .ident = ident.?,
            .value = expr,
        };
    }

    fn parseExit(self: *Parser) !NodeExit {
        _ = try self.tokens.consume(.exit);
        const expr = try self.parseExpr();
        _ = try self.tokens.consume(.semiCol);
        return NodeExit{
            .expr = expr,
        };
    }

    fn parseValue(self: *Parser, isConst: bool) !NodeValue {
        self.tokens.skip();
        const ident = (try self.tokens.consume(.ident)).?;
        _ = try self.tokens.consume(.equal);
        const expr = try self.parseExpr();
        _ = try self.tokens.consume(.semiCol);
        return NodeValue{
            .ident = ident,
            .value = expr,
            .isConst = isConst,
        };
    }

    pub fn parse(self: *Parser) ![]const NodeStmt {
        while (self.tokens.peek()) |_|
            try self.nodes.append(try self.parseStmt());

        return self.nodes.items;
    }
};

test "Parser" {
    const expect = std.testing.expect;
    const src = "exit 120;";
    var tokenizer = tok.Tokenizer.init(std.testing.allocator, src);
    defer tokenizer.deinit();
    const toks = try tokenizer.tokenize();
    var parser = Parser.init(std.testing.allocator, toks);
    defer parser.deinit();
    const parseTree = try parser.parse();
    const exp: []const NodeStmt = &[_]NodeStmt{NodeStmt{
        .exit = NodeExit{
            .expr = NodeExpr{
                .intLit = NodeIntlit{
                    .intlit = Token{
                        .intLit = 120,
                    },
                },
            },
        },
    }};
    for (parseTree, exp) |stmt, expStmt|
        try expect(std.meta.eql(stmt, expStmt));
}