stage1 优化重构代码

This commit is contained in:
zzy
2026-07-12 10:01:57 +08:00
parent f1b4225c92
commit 118c153280
6 changed files with 316 additions and 655 deletions

View File

@@ -3,7 +3,6 @@
#include "spl_comp.h"
#include "spl_lex_util.h"
#include <stdlib.h>
#include <string.h>
/* ============================================================
* Return statement: ret expr;
@@ -41,9 +40,7 @@ static void parse_var_decl(spl_comp_t *ctx, int is_const) {
spl_tok_t *name_tok = advance(ctx);
char vname[256];
usize nlen = name_tok->len < 255 ? name_tok->len : 255;
memcpy(vname, name_tok->lexeme, nlen);
vname[nlen] = '\0';
spl_tok_copy_name(name_tok, vname, sizeof(vname));
spl_type_info_t *var_type = NULL;
int has_init = 0;
@@ -141,6 +138,7 @@ void spl_parse_block(spl_comp_t *ctx) {
/* Forward declaration for block expr */
static int is_assign_op(spl_tok_type_t t);
static int lookahead_is_assign(spl_comp_t *ctx);
/* ============================================================
* Block expression: { stmts; [trailing_expr] }
@@ -174,24 +172,7 @@ spl_expr_result_t spl_parse_block_expr(spl_comp_t *ctx) {
result = (spl_expr_result_t){0};
} else {
/* Expression — look ahead for assignment */
int is_assign = 0;
{
usize look = ctx->tok_idx;
while (look < vec_size(ctx->toks)) {
spl_tok_type_t lt = vec_at(ctx->toks, look).type;
if (lt == TOK_SEMICOLON || lt == TOK_ENDLINE || lt == TOK_R_BRACE ||
lt == TOK_EOF)
break;
if (lt == TOK_L_PAREN)
break;
if (is_assign_op(lt)) {
is_assign = 1;
break;
}
look++;
}
}
int is_assign = lookahead_is_assign(ctx);
int saved_addr = ctx->addr_of_mode;
if (is_assign)
ctx->addr_of_mode = 1;
@@ -286,11 +267,11 @@ static void loop_exit(spl_comp_t *ctx, spl_loop_save_t *save) {
static void parse_while_stmt(spl_comp_t *ctx) {
advance(ctx); /* while */
skip_nl(ctx);
spl_expr_result_t cond = spl_parse_expr(ctx, PREC_MIN);
(void)cond;
spl_loop_save_t save;
loop_enter(ctx, &save);
spl_expr_result_t cond = spl_parse_expr(ctx, PREC_MIN);
(void)cond;
spl_val_t bz_addr = spl_emit_bz(ctx);
skip_nl(ctx);
spl_parse_block(ctx);
@@ -338,9 +319,7 @@ static void parse_for_stmt(spl_comp_t *ctx) {
spl_tok_t *ivar = advance(ctx);
char iname[256];
usize inl = ivar->len < 255 ? ivar->len : 255;
memcpy(iname, ivar->lexeme, inl);
iname[inl] = '\0';
spl_tok_copy_name(ivar, iname, sizeof(iname));
spl_push_scope(ctx);
int ioffset = spl_declare_var(ctx, iname, spl_type_basic(SPL_USIZE), 0);
@@ -410,18 +389,14 @@ static void parse_for_stmt(spl_comp_t *ctx) {
/* Parse val variable name */
spl_tok_t *vtok = advance(ctx);
char vname[256];
usize vnl = vtok->len < 255 ? vtok->len : 255;
memcpy(vname, vtok->lexeme, vnl);
vname[vnl] = '\0';
spl_tok_copy_name(vtok, vname, sizeof(vname));
/* Parse optional idx variable name */
char iname[256] = {0};
if (peek(ctx)->type == TOK_COMMA) {
advance(ctx);
spl_tok_t *itok = advance(ctx);
usize inl = itok->len < 255 ? itok->len : 255;
memcpy(iname, itok->lexeme, inl);
iname[inl] = '\0';
spl_tok_copy_name(itok, iname, sizeof(iname));
}
/* Stack: [slice_addr] or whatever the slice expression left */
@@ -602,9 +577,7 @@ static void parse_match_enum_bindings(spl_comp_t *ctx, spl_enum_variant_t *varia
for (;;) {
spl_tok_t *btok = advance(ctx);
char bname[256];
usize bnl = btok->len < 255 ? btok->len : 255;
memcpy(bname, btok->lexeme, bnl);
bname[bnl] = '\0';
spl_tok_copy_name(btok, bname, sizeof(bname));
spl_type_info_t *btype = spl_type_basic(SPL_I32);
usize field_byte_off = 4; /* skip tag */
@@ -628,9 +601,7 @@ static void parse_match_enum_bindings(spl_comp_t *ctx, spl_enum_variant_t *varia
/* Single-value binding */
spl_tok_t *btok = advance(ctx);
char bname[256];
usize bnl = btok->len < 255 ? btok->len : 255;
memcpy(bname, btok->lexeme, bnl);
bname[bnl] = '\0';
spl_tok_copy_name(btok, bname, sizeof(bname));
spl_type_info_t *btype = variant->data_type;
int boffset = spl_declare_var(ctx, bname, btype, 0);
@@ -816,9 +787,7 @@ static void parse_extern_decl(spl_comp_t *ctx) {
skip_nl(ctx);
spl_tok_t *fname_tok = advance(ctx);
char fn_name[256];
usize fnl = fname_tok->len < 255 ? fname_tok->len : 255;
memcpy(fn_name, fname_tok->lexeme, fnl);
fn_name[fnl] = '\0';
spl_tok_copy_name(fname_tok, fn_name, sizeof(fn_name));
skip_nl(ctx);
expect(ctx, TOK_L_PAREN);
@@ -880,6 +849,25 @@ static int is_assign_op(spl_tok_type_t t) {
t == TOK_ASSIGN_R_SH;
}
/* Scan ahead to check if the expression at current position is an assignment.
* Returns 1 if an assignment operator is found before a statement terminator
* or function-call paren. */
static int lookahead_is_assign(spl_comp_t *ctx) {
usize look = ctx->tok_idx;
while (look < vec_size(ctx->toks)) {
spl_tok_type_t t = vec_at(ctx->toks, look).type;
if (t == TOK_SEMICOLON || t == TOK_ENDLINE || t == TOK_L_BRACE || t == TOK_R_BRACE ||
t == TOK_EOF)
break;
if (t == TOK_L_PAREN)
break;
if (is_assign_op(t))
return 1;
look++;
}
return 0;
}
static void parse_expr_stmt(spl_comp_t *ctx) {
skip_nl(ctx);
if (peek(ctx)->type == KW_RET) {
@@ -889,24 +877,8 @@ static void parse_expr_stmt(spl_comp_t *ctx) {
usize prev = ctx->tok_idx;
/* Look ahead for assignment operator (scan past expression tokens) */
int is_assign = 0;
{
usize look = ctx->tok_idx;
while (look < vec_size(ctx->toks)) {
spl_tok_type_t t = vec_at(ctx->toks, look).type;
if (t == TOK_SEMICOLON || t == TOK_ENDLINE || t == TOK_L_BRACE || t == TOK_R_BRACE ||
t == TOK_EOF)
break;
if (t == TOK_L_PAREN)
break; /* function call, not assignment */
if (is_assign_op(t)) {
is_assign = 1;
break;
}
look++;
}
}
/* Look ahead for assignment operator */
int is_assign = lookahead_is_assign(ctx);
if (is_assign)
ctx->addr_of_mode = 1;