From e4d40be6344d2d6dcf81e7234fb691bb6e70282b Mon Sep 17 00:00:00 2001 From: fy Date: Sat, 7 Sep 2024 15:48:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=A7=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E9=AA=B0=E9=9D=A2=E6=A6=82=E7=8E=87=E5=81=8F=E7=A7=BB?= =?UTF-8?q?=E9=97=AE=E9=A2=98;=20imp:=20=E8=AE=A1=E7=AE=97=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=8E=B0=E5=9C=A8=E8=83=BD=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E7=9A=84=E4=B8=AD=E9=97=B4=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parser.go | 39 ++++++++++++++++++++++++++++----------- roll.peg | 8 ++++---- roll.peg.go | 8 ++++---- roll_func.go | 27 ++++++++++++++++++++++----- rollvm.go | 4 +++- rollvm_test.go | 6 ++++-- types.go | 16 ++++++++++++++-- 7 files changed, 79 insertions(+), 29 deletions(-) diff --git a/parser.go b/parser.go index 21174aca..f79ff1f0 100644 --- a/parser.go +++ b/parser.go @@ -22,8 +22,9 @@ type ParserData struct { } loopLayer int // 当前loop层数 codeStack []struct { - code []ByteCode - index int + code []ByteCode + index int + textPos int } } @@ -286,8 +287,21 @@ func (e *ParserData) AddInvoke(paramsNum IntType) { e.WriteCode(typeInvoke, paramsNum) } +func fixCodeByOffset(code []ByteCode, offset int) { + for index, i := range code { + switch i.T { + case typeDetailMark: + v := i.Value.(BufferSpan) + v.Begin -= IntType(offset) + v.End -= IntType(offset) + code[index].Value = v + } + } +} + func (p *ParserData) AddStoreComputed(name string, text string) { - code, length := p.CodePop() + code, length, offset := p.CodePop() + fixCodeByOffset(code, offset) val := NewComputedValRaw(&ComputedData{ Expr: text, code: code, @@ -299,7 +313,8 @@ func (p *ParserData) AddStoreComputed(name string, text string) { } func (p *ParserData) AddStoreComputedOnStack(text string) { - code, length := p.CodePop() + code, length, offset := p.CodePop() + fixCodeByOffset(code, offset) val := NewComputedValRaw(&ComputedData{ Expr: text, code: code, @@ -310,7 +325,8 @@ func (p *ParserData) AddStoreComputedOnStack(text string) { } func (p *ParserData) AddStoreFunction(name string, paramsReversed []string, text string) { - code, length := p.CodePop() + code, length, offset := p.CodePop() + fixCodeByOffset(code, offset) // 翻转一次 for i, j := 0, len(paramsReversed)-1; i < j; i, j = i+1, j-1 { @@ -340,16 +356,17 @@ func (p *ParserData) AddAttrSet(objName string, attr string, isRaw bool) { p.WriteCode(typeAttrSet, attr) } -func (p *ParserData) CodePush() { +func (p *ParserData) CodePush(textPos int) { p.codeStack = append(p.codeStack, struct { - code []ByteCode - index int - }{code: p.code, index: p.codeIndex}) + code []ByteCode + index int + textPos int + }{code: p.code, index: p.codeIndex, textPos: textPos}) p.code = make([]ByteCode, 256) p.codeIndex = 0 } -func (p *ParserData) CodePop() ([]ByteCode, int) { +func (p *ParserData) CodePop() ([]ByteCode, int, int) { lastCode, lastIndex := p.code, p.codeIndex last := len(p.codeStack) - 1 @@ -357,5 +374,5 @@ func (p *ParserData) CodePop() ([]ByteCode, int) { p.codeStack = p.codeStack[:last] p.code = info.code p.codeIndex = info.index - return lastCode, lastIndex + return lastCode, lastIndex, info.textPos } diff --git a/roll.peg b/roll.peg index 1434fe38..7ba12cb6 100644 --- a/roll.peg +++ b/roll.peg @@ -120,12 +120,12 @@ stmtIf <- "if" sp1x (exprRoot sp { c.data.AddOp(typeBlockPush); c.data.AddOp(typ func_def_params <- '(' sp ')' sp { c.data.CounterPush() } / '(' sp { c.data.CounterPush(); c.data.CounterAdd(1) } id:identifier sp { c.data.NamePush(id.(string)) } (',' sp id2:identifier sp { c.data.NamePush(id2.(string)) } {c.data.CounterAdd(1)} )* ')' sp -stmtFunc <- "func" sp1x id:identifier sp { c.data.NamePush(id.(string)) } func_def_params '{' sp { c.data.CodePush() } exprText:< stmtRoot? > '}' sp +stmtFunc <- "func" sp1x id:identifier sp { c.data.NamePush(id.(string)) } func_def_params '{' sp { c.data.CodePush(p.pt.offset) } exprText:< stmtRoot? > '}' sp { num := c.data.CounterPop(); arr := []string{}; for i:=IntType(0); i