修正了一些错误

This commit is contained in:
2026-07-26 20:33:17 +08:00
parent 909792bc8f
commit 03d0bba534
40 changed files with 6939 additions and 827 deletions

View File

@@ -5,6 +5,8 @@ import llvmlite
import memhub
import stdio
import string
import viperlib
import lib.core.VLogger as VLogger
import lib.core.Handles.HandlesBase as HandlesBase
import lib.core.Handles.HandlesTranslator as HT
import lib.core.Handles.HandlesVar as HandlesVar
@@ -58,7 +60,7 @@ class AugAssignHandle(HandlesBase.Mixin):
field_ptr: llvmlite.Value | t.CPtr = HandlesExpr.get_attribute_ptr(
builder, pool, mod, target, self.Trans)
if field_ptr is None:
stdio.printf("[AUGASGN] attribute ptr is None\n")
VLogger.error("attribute ptr is None", "AUGASGN")
return 0
# 2. 确定字段类型并加载当前值
@@ -68,21 +70,24 @@ class AugAssignHandle(HandlesBase.Mixin):
cur_val: llvmlite.Value | t.CPtr = llvmlite.build_load(
builder, target_ty, field_ptr)
if cur_val is None:
stdio.printf("[AUGASGN] cannot load attribute\n")
VLogger.error("cannot load attribute", "AUGASGN")
return 0
# 3. 翻译 RHS 值
rhs_val: llvmlite.Value | t.CPtr = HandlesExpr.translate_value(
builder, pool, mod, aug.value, None, 0, self.Trans)
if rhs_val is None:
stdio.printf("[AUGASGN] rhs is None\n")
VLogger.error("rhs is None", "AUGASGN")
return 0
# 4. 应用二元运算
result: llvmlite.Value | t.CPtr = _apply_aug_op(
pool, builder, aug.op, cur_val, rhs_val)
if result is None:
stdio.printf("[AUGASGN] binop failed for attr op=%d\n", aug.op)
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
if fb is not None:
viperlib.snprintf(fb, 1024, "binop failed for attr op=%d", aug.op)
VLogger.error(fb, "AUGASGN")
return 0
# 5. 类型对齐并存储
@@ -92,8 +97,56 @@ class AugAssignHandle(HandlesBase.Mixin):
llvmlite.build_store(builder, result, field_ptr)
return 0
# Subscript 目标: self.state[i] += x / arr[i] += x
# 流程: get_subscript_ptr → load → binop → store
if tk == ast.ASTKind.Subscript:
pool: memhub.MemBuddy | t.CPtr = self.Trans.Pool
builder: llvmlite.IRBuilder | t.CPtr = self.Trans._cur_builder
mod: llvmlite.LLVMModule | t.CPtr = self.Trans.Module
# 1. 获取元素指针
elem_ptr: llvmlite.Value | t.CPtr = HandlesExpr.get_subscript_ptr(
builder, pool, mod, target, self.Trans)
if elem_ptr is None:
VLogger.error("subscript ptr is None", "AUGASGN")
return 0
# 2. 确定元素类型并加载当前值
target_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
if elem_ptr.Ty is not None and elem_ptr.Ty.Pointee is not None:
target_ty = elem_ptr.Ty.Pointee
cur_val: llvmlite.Value | t.CPtr = llvmlite.build_load(
builder, target_ty, elem_ptr)
if cur_val is None:
VLogger.error("cannot load subscript element", "AUGASGN")
return 0
# 3. 翻译 RHS 值
rhs_val: llvmlite.Value | t.CPtr = HandlesExpr.translate_value(
builder, pool, mod, aug.value, None, 0, self.Trans)
if rhs_val is None:
VLogger.error("rhs is None", "AUGASGN")
return 0
# 4. 应用二元运算
result: llvmlite.Value | t.CPtr = _apply_aug_op(
pool, builder, aug.op, cur_val, rhs_val)
if result is None:
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
if fb is not None:
viperlib.snprintf(fb, 1024, "binop failed for sub op=%d", aug.op)
VLogger.error(fb, "AUGASGN")
return 0
# 5. 类型对齐并存储
result = HandlesExpr.coerce_to_type(builder, result, target_ty)
if result is None:
return 0
llvmlite.build_store(builder, result, elem_ptr)
return 0
if tk != ast.ASTKind.Name:
stdio.printf("[AUGASGN] only Name/Attribute target supported\n")
VLogger.error("only Name/Attribute/Subscript target supported", "AUGASGN")
return 0
nm: ast.Name | t.CPtr = (ast.Name | t.CPtr)(target)
@@ -139,14 +192,17 @@ class AugAssignHandle(HandlesBase.Mixin):
cur_val = llvmlite.build_load(builder, target_ty, target_alloca)
if cur_val is None:
stdio.printf("[AUGASGN] cannot load target %s\n", nm.id)
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
if fb is not None:
viperlib.snprintf(fb, 1024, "cannot load target %s", nm.id)
VLogger.error(fb, "AUGASGN")
return 0
# 3. 翻译 RHS 值
rhs_val: llvmlite.Value | t.CPtr = HandlesExpr.translate_value(
builder, pool, mod, aug.value, None, 0, self.Trans)
if rhs_val is None:
stdio.printf("[AUGASGN] rhs is None\n")
VLogger.error("rhs is None", "AUGASGN")
return 0
# 4. 类型对齐 + 应用二元运算
@@ -154,7 +210,10 @@ class AugAssignHandle(HandlesBase.Mixin):
result: llvmlite.Value | t.CPtr = _apply_aug_op(
pool, builder, aug.op, cur_val, rhs_val)
if result is None:
stdio.printf("[AUGASGN] binop failed for op=%d\n", aug.op)
fb: t.CChar | t.CPtr = VLogger.fmt_buf()
if fb is not None:
viperlib.snprintf(fb, 1024, "binop failed for op=%d", aug.op)
VLogger.error(fb, "AUGASGN")
return 0
# 5. 存储结果
@@ -226,6 +285,8 @@ def _apply_aug_op(pool: memhub.MemBuddy | t.CPtr,
return llvmlite.build_mul(builder, lhs, rhs)
elif op == ast.OpKind.Div:
return llvmlite.build_sdiv(builder, lhs, rhs)
elif op == ast.OpKind.FloorDiv:
return llvmlite.build_sdiv(builder, lhs, rhs)
elif op == ast.OpKind.Mod:
return llvmlite.build_srem(builder, lhs, rhs)
elif op == ast.OpKind.BitAnd: