Skip to content

Commit

Permalink
*: add Push Context and Pop Contex
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed Mar 29, 2022
1 parent 8522f1d commit 18bace1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Builder/GoGenCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

parser "github.com/acekingke/yaccgo/Parser"
rules "github.com/acekingke/yaccgo/Rules"
utils "github.com/acekingke/yaccgo/Utils"
)

type GoBuilder struct {
Expand Down Expand Up @@ -50,6 +51,13 @@ func (b *GoBuilder) buildUionAndCode() {
str := `
var StateSymStack = []StateSym{}
var StackPointer = 0
type Context struct {
StackSym []StateSym
Stackpos int
}
var globalContext = []Context{}
type ValType struct {
%s
}
Expand Down Expand Up @@ -113,7 +121,17 @@ func (s *StateSym) Action(a int) int {
func init() {
ParserInit()
}
func PushContex() {
globalContext = append(globalContext, Context{
StackSym: StateSymStack,
Stackpos: StackPointer,
})
}
func PopContex() {
StackPointer = globalContext[len(globalContext)-1].Stackpos
StateSymStack = globalContext[len(globalContext)-1].StackSym
globalContext = globalContext[:len(globalContext)-1]
}
func ParserInit() {
StateSymStack = []StateSym{
{
Expand Down Expand Up @@ -251,7 +269,7 @@ func GoGenFromString(input string, file string) error {
b := NewGoBuilder(w)
b.buildConstPart()
b.buildUionAndCode()
if b.vnode.NeedPacked {
if b.vnode.NeedPacked && utils.PackFlags {
b.buildAnalyPackTable()
b.buildPackStateFunc()
} else {
Expand Down
14 changes: 12 additions & 2 deletions Builder/GoGenPackTableCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ func (s *StateSym) Action(a int) int {
return StatePackAction[StatePackOffset[s.Yystate]+a]
}
}
func PushContex() {
globalContext = append(globalContext, Context{
StackSym: StateSymStack,
Stackpos: StackPointer,
})
}
func PopContex() {
StackPointer = globalContext[len(globalContext)-1].Stackpos
StateSymStack = globalContext[len(globalContext)-1].StackSym
globalContext = globalContext[:len(globalContext)-1]
}
func init() {
ParserInit()
}
Expand Down Expand Up @@ -84,7 +94,7 @@ func Parser(input string) *ValType {
s := &StateSymStack[StackPointer-1]
a := s.Action(lookAhead)
if a == ERROR_ACTION {
panic("Grammer error")
panic("Grammar parse error")
} else if a == ACCEPT_ACTION {
return &s.ValType
} else {
Expand Down
1 change: 1 addition & 0 deletions Utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Use of this source code is governed by MIT license that can be found in the LICE
package utils

var DebugFlags = false
var PackFlags = true

0 comments on commit 18bace1

Please sign in to comment.