feat: L Language v0.1 编译器完整实现
5 阶段编译流水线: 词法分析 → 语法分析(Pratt) → 语义分析(类型推断) → LLVM IR → .exe 模块: - lexer: 手写状态机, 40 种 Token, // 和 /* */ 注释 - parser: Pratt 表达式解析(9 级优先级) + 递归下降语句/函数 - ast: 14 种节点类型 + 工厂函数 - sema: 作用域链符号表 + 类型推断 + 类型检查 - codegen: AST → LLVM-C API, print_i64/f64/bool 内建 - driver: 命令行 + 流水线串联 + 错误报告 - util: Arena bump allocator (8MB) 测试: 65 单元测试(词法41+语法15+语义9) + 5 集成测试 全部通过 语言特性: i64/f64/bool/void, let不可变变量, if/else, while, 递归函数
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include "error.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
void error_init(ErrorList* list) {
|
||||
list->capacity = 8;
|
||||
list->errors = malloc(list->capacity * sizeof(ErrorInfo));
|
||||
list->count = 0;
|
||||
if (!list->errors) list->capacity = 0;
|
||||
}
|
||||
|
||||
void error_add(ErrorList* list, const char* filename, int line, int col, const char* fmt, ...) {
|
||||
if (!list->errors) return;
|
||||
if (list->count >= list->capacity) {
|
||||
size_t new_cap = list->capacity * 2;
|
||||
ErrorInfo* new_errs = realloc(list->errors, new_cap * sizeof(ErrorInfo));
|
||||
if (!new_errs) return;
|
||||
list->errors = new_errs;
|
||||
list->capacity = new_cap;
|
||||
}
|
||||
char buf[512];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int n = vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
va_end(args);
|
||||
if (n < 0) return;
|
||||
char* msg = strdup(buf);
|
||||
char* fname = strdup(filename);
|
||||
if (!msg || !fname) {
|
||||
free(msg); free(fname);
|
||||
return;
|
||||
}
|
||||
list->errors[list->count++] = (ErrorInfo){
|
||||
.message = msg,
|
||||
.filename = fname,
|
||||
.line = line,
|
||||
.col = col,
|
||||
};
|
||||
}
|
||||
|
||||
void error_print(const ErrorList* list) {
|
||||
for (size_t i = 0; i < list->count; i++) {
|
||||
const ErrorInfo* e = &list->errors[i];
|
||||
fprintf(stderr, "\033[1;31m错误:\033[0m %s:%d:%d: %s\n",
|
||||
e->filename, e->line, e->col, e->message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user