-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.c
172 lines (138 loc) · 3.64 KB
/
compiler.c
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
171
172
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "chunk.h"
#include "common.h"
#include "compiler.h"
#include "scanner.h"
#include "value.h"
typedef struct {
Token current;
Token previous;
bool hadError;
bool panicMode;
} Parser;
typedef enum {
PREC_NONE,
PREC_ASSIGNMENT, // =
PREC_OR, // or
PREC_AND, // and
PREC_EQUALITY, // == !=
PREC_COMPARISON, // < > <= >=
PREC_TERM, // + -
PREC_FACTOR, // * /
PREC_UNARY, // ! -
PREC_CALL, // . ()
PREC_PRIMARY
} Precedence;
Parser parser;
Chunk* compilingChunk;
static Chunk* currentChunk(){
return compilingChunk;
}
static void errorAt(Token* token, const char* message){
if (parser.panicMode) return;
parser.panicMode = true;
fprintf(stderr, "[line %d] Error",token->line);
if (token->type == TOKEN_EOF){
fprintf(stderr, " at end");
}
else if (token->type == TOKEN_ERROR){
}
else {
fprintf(stderr, "at '%.*s'",token->length, token->start);
}
fprintf(stderr, ": %s\n",message);
parser.hadError = true;
}
static void error(const char* message){
errorAt(&parser.previous, message);
}
static void errorAtCurrent(const char* message){
errorAt(&parser.current, message);
}
static void advance() {
parser.previous = parser.current;
for (;;){
parser.current = scanToken();
if (parser.current.type != TOKEN_ERROR) break;
errorAtCurrent(parser.current.start);
}
}
static void consume(TokenType type, const char* message) {
if (parser.current.type == type) {
advance();
return;
}
errorAtCurrent(message);
}
static void emitByte(uint8_t byte){
writeChunk(currentChunk(), byte, parser.previous.line);
}
static void emitBytes(uint8_t byte1, uint8_t byte2){
emitByte(byte1);
emitByte(byte2);
}
static void emitReturn(){
emitByte(OP_RETURN);
}
static uint8_t makeConstant(Value value){
int constant = addConstant(currentChunk(), value);
if (constant > UINT8_MAX){
error("Too many constants in one Chunk");
return 0;
}
return (uint8_t)constant;
}
static void emitConstant(Value value){
emitBytes(OP_CONSTANT, makeConstant(value));
}
static void endCompiler(){
emitReturn();
}
static void parsePrecedence(Precedence precedence){
}
static void binary(){
TokenType operatorType = parser.previous.type;
ParseRule* rule = getRule(operatorType);
parsePrecedence((Precedence)(rule->precedence+1));
switch (operatorType){
case TOKEN_PLUS: emitByte(OP_ADD); break;
case TOKEN_MINUS: emitByte(OP_SUBTRACT); break;
case TOKEN_STAR: emitByte(OP_MULTIPLY); break;
case TOKEN_SLASH: emitByte(OP_DIVIDE); break;
default: return;
}
}
static void expression(){
parsePrecedence(PREC_ASSIGNMENT);
// the connecting piece
}
static void grouping(){
expression();
consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression");
}
static void number(){
double value = strtod(parser.previous.start,NULL);
emitConstant(value);
}
static void unary(){
TokenType operatorType = parser.previous.type;
parsePrecedence(PREC_UNARY);
switch (operatorType) {
case TOKEN_MINUS: emitByte(OP_NEGATE); break;
default: return;
}
}
bool compile(const char *source, Chunk* chunk){
initScanner(source);
compilingChunk = chunk;
parser.hadError = false;
parser.panicMode = false;
advance();
expression();
consume(TOKEN_EOF, "Expect end of expression.");
endCompiler();
return !parser.hadError;
}