- 实现 as 类型转换运算符、修复 &&/|| 短路求值 bug。 - 在 splc1.spl 中实现 Vec/Map/Emit 基础库(~50 个方法)。 - 修复实例方法调用中 self 参数被错误丢弃的 bug。
207 lines
5.3 KiB
C
207 lines
5.3 KiB
C
/* spl_comp.h — SPL compiler: type system, parser, codegen */
|
|
#ifndef __SPL_COMP_H__
|
|
#define __SPL_COMP_H__
|
|
|
|
#include "../stage0/spl_ir.h"
|
|
#include "spl_lexer.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "spl_emit.h"
|
|
#include "spl_type.h"
|
|
|
|
/* Forward declarations */
|
|
typedef struct spl_comp spl_comp_t;
|
|
|
|
/* ============================================================
|
|
* Scope / symbol table
|
|
* ============================================================ */
|
|
|
|
typedef struct spl_var_info {
|
|
char *name;
|
|
int type_idx;
|
|
int offset;
|
|
int is_const;
|
|
int depth;
|
|
} spl_var_info_t;
|
|
typedef VEC(spl_var_info_t) spl_var_vec_t;
|
|
|
|
typedef struct spl_scope {
|
|
spl_var_vec_t vars;
|
|
int depth;
|
|
} spl_scope_t;
|
|
typedef VEC(spl_scope_t) spl_scope_vec_t;
|
|
|
|
typedef struct spl_func_info {
|
|
char *name;
|
|
int ret_type_idx;
|
|
int *param_type_indices;
|
|
char **param_names;
|
|
int nparams;
|
|
int func_idx;
|
|
int is_extern;
|
|
int is_pub;
|
|
} spl_func_info_t;
|
|
typedef VEC(spl_func_info_t) spl_func_info_vec_t;
|
|
|
|
/* ============================================================
|
|
* Compiler context
|
|
* ============================================================ */
|
|
|
|
#define COMP_ERROR_MAX 256
|
|
#define DEFER_MAX 64
|
|
|
|
typedef struct {
|
|
usize body_start;
|
|
usize jmp_exit;
|
|
int depth;
|
|
int count_at_decl;
|
|
} spl_defer_entry_t;
|
|
|
|
typedef struct spl_comp {
|
|
/* Lexer output */
|
|
spl_tok_vec_t toks;
|
|
usize tok_idx;
|
|
const char *fname;
|
|
const char *source;
|
|
|
|
/* Program output */
|
|
spl_prog_t prog;
|
|
|
|
/* IR emission + frame allocator */
|
|
spl_emit_t emit;
|
|
|
|
/* Error state */
|
|
char error_msg[COMP_ERROR_MAX];
|
|
int has_error;
|
|
|
|
/* Scopes */
|
|
spl_scope_vec_t scopes;
|
|
int scope_depth;
|
|
|
|
/* Function table */
|
|
spl_func_info_vec_t funcs;
|
|
|
|
/* Type context — first-class type arena + namespace */
|
|
spl_type_ctx_t tctx;
|
|
|
|
/* Current function context */
|
|
int current_func_idx;
|
|
int current_ret_type_idx;
|
|
|
|
/* Loop context for break/continue */
|
|
int in_loop;
|
|
usize *break_patches;
|
|
usize break_patch_count;
|
|
usize break_patch_cap;
|
|
usize continue_target;
|
|
|
|
/* Defer stack */
|
|
spl_defer_entry_t defer_stack[DEFER_MAX];
|
|
int defer_count;
|
|
|
|
/* Global data index for string literals */
|
|
int next_gdata_idx;
|
|
|
|
/* Const values */
|
|
MAP(const char *, spl_val_t) const_values;
|
|
|
|
int addr_of_mode;
|
|
} spl_comp_t;
|
|
|
|
/* Initialize/destroy compiler context */
|
|
void spl_comp_init(spl_comp_t *ctx);
|
|
void spl_comp_drop(spl_comp_t *ctx);
|
|
|
|
/* Main compilation entry: source → .sir */
|
|
int spl_compile(spl_comp_t *ctx, const char *source, const char *fname);
|
|
|
|
/* Reset for a new compilation */
|
|
void spl_comp_reset(spl_comp_t *ctx);
|
|
|
|
/* Error reporting */
|
|
void spl_comp_error(spl_comp_t *ctx, const char *fmt, ...);
|
|
|
|
/* ============================================================
|
|
* Parser functions (spl_parser.c)
|
|
* ============================================================ */
|
|
|
|
void spl_parse_prog(spl_comp_t *ctx);
|
|
void parse_type_decl(spl_comp_t *ctx);
|
|
|
|
/* ============================================================
|
|
* Expression functions (spl_expr.c)
|
|
* ============================================================ */
|
|
|
|
enum {
|
|
PREC_MIN = 0,
|
|
PREC_ASSIGN = 1,
|
|
PREC_LOGOR = 2,
|
|
PREC_LOGAND = 3,
|
|
PREC_OR = 4,
|
|
PREC_XOR = 5,
|
|
PREC_AND = 6,
|
|
PREC_CMPEQ = 7,
|
|
PREC_CMP = 8,
|
|
PREC_SHIFT = 9,
|
|
PREC_ADD = 10,
|
|
PREC_MUL = 11,
|
|
PREC_PREFIX = 12,
|
|
PREC_POSTFIX = 13
|
|
};
|
|
|
|
typedef struct {
|
|
int type_idx;
|
|
int is_lvalue;
|
|
} spl_expr_result_t;
|
|
|
|
spl_expr_result_t spl_parse_expr(spl_comp_t *ctx, int min_prec);
|
|
spl_expr_result_t spl_parse_struct_literal(spl_comp_t *ctx, int type_idx);
|
|
|
|
/* Match arm pattern comparison */
|
|
int spl_emit_match_enum_cmp(spl_comp_t *ctx, int enum_type_idx, int val_offset, int by_value);
|
|
void spl_emit_match_value_cmp(spl_comp_t *ctx, int val_offset);
|
|
|
|
/* ============================================================
|
|
* Statement functions (spl_stmt.c)
|
|
* ============================================================ */
|
|
|
|
void spl_parse_stmt(spl_comp_t *ctx);
|
|
void spl_parse_block(spl_comp_t *ctx);
|
|
spl_expr_result_t spl_parse_block_expr(spl_comp_t *ctx);
|
|
|
|
/* ============================================================
|
|
* Remaining helpers in spl_comp.c
|
|
* ============================================================ */
|
|
|
|
/* Variable management */
|
|
int spl_declare_var(spl_comp_t *ctx, const char *name, int type_idx, int is_const);
|
|
spl_var_info_t *spl_lookup_var(spl_comp_t *ctx, const char *name);
|
|
int spl_get_var_offset(spl_comp_t *ctx, const char *name);
|
|
|
|
/* Function management */
|
|
int spl_declare_func(spl_comp_t *ctx, const char *name, int ret_type_idx, int nparams,
|
|
int is_extern, int is_pub);
|
|
int spl_lookup_func(spl_comp_t *ctx, const char *name);
|
|
int spl_ensure_native(spl_comp_t *ctx, const char *name);
|
|
|
|
/* String/data management */
|
|
int spl_add_string(spl_comp_t *ctx, const char *str);
|
|
int spl_add_global_data(spl_comp_t *ctx, void *data, usize size);
|
|
|
|
/* Scope management */
|
|
void spl_push_scope(spl_comp_t *ctx);
|
|
void spl_pop_scope(spl_comp_t *ctx);
|
|
|
|
/* Defer */
|
|
void spl_emit_defer(spl_comp_t *ctx);
|
|
void spl_emit_defer_epilogue(spl_comp_t *ctx, int depth);
|
|
|
|
/* Register runtime natives (stub) */
|
|
void spl_comp_register(spl_prog_t *prog);
|
|
|
|
#endif /* __SPL_COMP_H__ */
|