Hash Tables 20

This commit is contained in:
nazrin 2025-06-07 04:09:48 +00:00
parent 3b234814fa
commit a7b7348f61
20 changed files with 868 additions and 837 deletions

View file

@ -1,50 +1,38 @@
module clox.parser;
import clox.compiler;
import clox.value;
import core.stdc.stdio;
import std.functional : ctEval;
import clox.scanner;
import clox.compiler;
import clox.chunk;
import clox.parserules;
import clox.util;
struct Parser{
Compiler* compiler;
Token current, previous;
bool hadError, panicMode;
void errorAtCurrent(string message){
errorAt(current, message);
}
void error(string message){
errorAt(previous, message);
}
void errorAt(in ref Token token, string message){
import core.stdc.stdio;
if(panicMode)
return;
panicMode = true;
fprintf(stderr, "[line %d] Error", token.line);
if(token.type == Token.Type.EOF){
fprintf(stderr, " at end");
} else if(token.type != Token.Type.Error){
fprintf(stderr, " at '%.*s'", cast(int)token.lexeme.length, token.lexeme.ptr);
}
fprintf(stderr, ": %.*s\n", cast(int)message.length, message.ptr);
hadError = true;
}
auto consume(Token.Type type, string msg){
if(current.type == type){
advance();
return;
}
errorAtCurrent(msg);
void initialise(Compiler* compiler){
this.compiler = compiler;
}
void advance(){
previous = current;
while(true){
current = compiler.scanner.scan();
current = compiler.scanner.scanToken();
if(current.type != Token.Type.Error)
break;
errorAtCurrent(current.lexeme);
errorAtCurrent(current.lexeme.ptr);
}
}
void consume(Token.Type type, in char* message){
if(current.type == type){
advance();
return;
}
errorAtCurrent(message);
}
void expression(){
parsePrecedence(Precedence.Assignment);
@ -63,4 +51,29 @@ struct Parser{
infixRule(compiler);
}
}
void errorAtCurrent(in char* message){
errorAt(current, message);
}
void error(in char* message){
errorAt(previous, message);
}
void errorAt(in ref Token token, in char* message){
if(panicMode)
return;
panicMode = true;
fprintf(stderr, ctEval!("[line %d] " ~ colour!("Error", Colour.Red)).ptr, token.line);
if (token.type == Token.Type.EOF) {
fprintf(stderr, " at end");
} else if (token.type == Token.Type.Error) {
// Nothing.
} else {
fprintf(stderr, " at '%.*s'", cast(int)token.lexeme.length, token.lexeme.ptr);
}
fprintf(stderr, ": %s\n", message);
hadError = true;
}
}