refactor: 重构前端代码并添加日志功能
- 重命名和重构了多个文件,包括 lexer、parser 和 AST 相关代码 - 添加了日志功能,使用 LOG_* 宏替代原有的 error 和 warn 函数 - 优化了错误处理和内存分配方式 - 调整了代码结构,提高了模块化和可读性
This commit is contained in:
64
lib/rt/std/rt_api_def.h
Normal file
64
lib/rt/std/rt_api_def.h
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef __SMCC_RT_API_DEF_H__
|
||||
#define __SMCC_RT_API_DEF_H__
|
||||
|
||||
#include "rt_type.h"
|
||||
|
||||
#ifndef __RT_SIZE_TYPE__
|
||||
#define __RT_SIZE_TYPE__
|
||||
typedef usz_t rt_size_t;
|
||||
#endif
|
||||
typedef void* (*rt_malloc)(rt_size_t size);
|
||||
typedef void (*rt_free)(void* ptr);
|
||||
typedef void (*rt_exit)(int code);
|
||||
|
||||
#ifndef __RT_FILE_TYPE__
|
||||
#define __RT_FILE_TYPE__
|
||||
typedef void* rt_file_t;
|
||||
#endif
|
||||
extern rt_file_t rt_stdin;
|
||||
extern rt_file_t rt_stdout;
|
||||
extern rt_file_t rt_stderr;
|
||||
typedef rt_file_t (*rt_fopen_t)(const char* file_name, const char* mode);
|
||||
typedef int (*rt_fflush_t)(rt_file_t*file);
|
||||
typedef int (*rt_fclose_t)(rt_file_t file);
|
||||
typedef int (*rt_freads_t)(void * dst_buf, rt_size_t dst_size, rt_size_t elem_size, rt_size_t count, rt_file_t file);
|
||||
typedef int (*rt_fwrite_t)(const void * buf, rt_size_t size, rt_size_t count, rt_file_t file);
|
||||
|
||||
typedef int (*rt_fprintf_t)(void * file, const char *format, ...);
|
||||
typedef int (*rt_snprintf_t)(char * stream, rt_size_t n, const char * format, ...);
|
||||
typedef void* (*rt_realloc_t)(void *memory, rt_size_t new_size);
|
||||
|
||||
typedef struct smcc_rt {
|
||||
rt_malloc _malloc;
|
||||
rt_free _free;
|
||||
rt_exit exit;
|
||||
|
||||
rt_fopen_t fopen;
|
||||
rt_fflush_t fflush;
|
||||
rt_fclose_t fclose;
|
||||
rt_freads_t freads;
|
||||
rt_fwrite_t fwrite;
|
||||
|
||||
// Optional useful runtime
|
||||
rt_fprintf_t fprintf;
|
||||
rt_snprintf_t snprintf;
|
||||
rt_realloc_t _realloc;
|
||||
} smcc_rt_t;
|
||||
|
||||
extern const smcc_rt_t rt;
|
||||
|
||||
// #ifndef NULL
|
||||
// #ifdef __cplusplus
|
||||
// #ifndef _WIN64
|
||||
// #define NULL 0
|
||||
// #else
|
||||
// #define NULL 0LL
|
||||
// #endif /* W64 */
|
||||
// #else
|
||||
// #define NULL ((void *)0)
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
#define NULL ((void *)0)
|
||||
|
||||
#endif
|
32
lib/rt/std/rt_std.c
Normal file
32
lib/rt/std/rt_std.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "rt_api_def.h"
|
||||
|
||||
const smcc_rt_t rt = {
|
||||
._malloc = (rt_malloc)malloc,
|
||||
._free = (rt_free)free,
|
||||
.exit = (rt_exit)exit,
|
||||
|
||||
.fopen = (rt_fopen_t)fopen,
|
||||
.fflush = (rt_fflush_t)fflush,
|
||||
.fclose = (rt_fclose_t)fclose,
|
||||
.freads = (rt_freads_t)fread_s,
|
||||
.fwrite = (rt_fwrite_t)fwrite,
|
||||
|
||||
._realloc = (rt_realloc_t)realloc,
|
||||
.fprintf = (rt_fprintf_t)fprintf,
|
||||
.snprintf = (rt_snprintf_t)snprintf,
|
||||
};
|
||||
|
||||
rt_file_t rt_stdin;
|
||||
rt_file_t rt_stdout;
|
||||
rt_file_t rt_stderr;
|
||||
|
||||
void init_rt_std() {
|
||||
rt_stdin = stdin;
|
||||
rt_stdout = stdout;
|
||||
rt_stderr = stderr;
|
||||
}
|
6
lib/rt/std/rt_std.h
Normal file
6
lib/rt/std/rt_std.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef __SMCC_RT_STD_H__
|
||||
#define __SMCC_RT_STD_H__
|
||||
|
||||
void init_rt_std();
|
||||
|
||||
#endif
|
28
lib/rt/std/rt_type.h
Normal file
28
lib/rt/std/rt_type.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef __SMCC_RT_TYPE_H__
|
||||
#define __SMCC_RT_TYPE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int8_t i8_t;
|
||||
typedef int16_t i16_t;
|
||||
typedef int32_t i32_t;
|
||||
typedef int64_t i64_t;
|
||||
|
||||
typedef uint8_t u8_t;
|
||||
typedef uint16_t u16_t;
|
||||
typedef uint32_t u32_t;
|
||||
typedef uint64_t u64_t;
|
||||
|
||||
typedef float f32_t;
|
||||
typedef double f64_t;
|
||||
|
||||
typedef intptr_t iptr_t;
|
||||
typedef uintptr_t uptr_t;
|
||||
|
||||
typedef size_t usz_t;
|
||||
typedef ssize_t isz_t;
|
||||
|
||||
// typedef u32_t uw_t;
|
||||
// typedef i32_t iw_t;
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user