Skip to content

Commit

Permalink
*: fix the template bug
Browse files Browse the repository at this point in the history
  • Loading branch information
keyuchang committed May 30, 2022
1 parent d608322 commit d460080
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 515 deletions.
142 changes: 101 additions & 41 deletions Builder/GoGenPackTableCode.go → Builder/GoCodeTemplate.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,85 @@
/*
Copyright (c) 2021 Ke Yuchang([email protected]). All rights reserved.
Use of this source code is governed by MIT license that can be found in the LICENSE file.
*/
package builder

import "fmt"
var goCodeTemplateStr string = `
/*Generator Code , do not modify*/
// Code header part
{{.CodeHeader}}
import "strings"
// const part
{{.ConstPart}}
{{ if .NeedPacked }}
// Terminal Size
const NTERMINALS = {{.NTerminals}}
{{end}}
// make AnalyTable
func (b *GoBuilder) buildAnalyPackTable() {
AnalyTable := `
var StatePackAction = []int {
%s
}
var StatePackOffset = []int {
%s
}
var StackPackCheck = []int {
%s
}
`
saction := ""
soffset := ""
scheck := ""
for _, val := range b.vnode.ActionTable {
saction += fmt.Sprintf("%d,\t", val)
var IsTrace bool = false
var StateSymStack = []StateSym{}
var StackPointer = 0
type Context struct {
StackSym []StateSym
Stackpos int
}
var globalContext = []Context{}
type ValType struct {
// Union part
{{.UnionPart}}
}
type StateSym struct {
Yystate int // state
//sym val
YySymIndex int
//other
ValType
}
{{ if .NeedPacked }}
// It is NeedPacked
{{.PackAnalyTable}}
func (s *StateSym) Action(a int) int {
if StatePackOffset[s.Yystate]+a < 0 {
return ERROR_ACTION
}
for _, val := range b.vnode.OffsetTable {
soffset += fmt.Sprintf("%d,\t", val)
if StatePackOffset[s.Yystate]+a >= len(StackPackCheck) ||
StackPackCheck[StatePackOffset[s.Yystate]+a] != s.Yystate {
if a > NTERMINALS {
return StackPackGotoDef[a - NTERMINALS - 1]
}else {
return StackPackActDef[s.Yystate]
}
}else{
return StatePackAction[StatePackOffset[s.Yystate]+a]
}
for _, val := range b.vnode.CheckTable {
scheck += fmt.Sprintf("%d,\t", val)
}
{{else}}
// It is not packed
var StateActionArray = [][]int{
{{.AnalyTable}}
}
func (s *StateSym) Action(a int) int {
return StateActionArray[s.Yystate][a]
}
{{ end }}
func TraceShift(s *StateSym) {
if IsTrace {
fmt.Printf("Shift %s, push state %d\n", TraceTranslate(s.YySymIndex), s.Yystate)
}
}
// Reduce function
func ReduceFunc(reduceIndex int) *StateSym {
dollarDolar := &StateSym{}
topIndex := StackPointer - 1
switch reduceIndex {
{{.ReduceFunc}}
}
b.AnalyTable = fmt.Sprintf(AnalyTable, saction, soffset, scheck)
return dollarDolar
}
func (b *GoBuilder) buildPackStateFunc() {
b.StateFunc = `
// Push StateSym
func PushStateSym(state *StateSym) {
TraceShift(state)
Expand All @@ -52,12 +96,9 @@ func PopStateSym(num int) {
StackPointer -= num
}
func (s *StateSym) Action(a int) int {
if StatePackOffset[s.Yystate]+a < 0|| StatePackOffset[s.Yystate]+a >= len(StackPackCheck) || StackPackCheck[StatePackOffset[s.Yystate]+a] != s.Yystate {
return 0
}else{
return StatePackAction[StatePackOffset[s.Yystate]+a]
}
func init() {
ParserInit()
}
func PushContex() {
globalContext = append(globalContext, Context{
Expand All @@ -70,10 +111,6 @@ func PopContex() {
StateSymStack = globalContext[len(globalContext)-1].StackSym
globalContext = globalContext[:len(globalContext)-1]
}
func init() {
ParserInit()
}
func ParserInit() {
StateSymStack = []StateSym{
{
Expand Down Expand Up @@ -129,5 +166,28 @@ func fetchLookAhead(input string, val *ValType, pos *int) int {
token := GetToken(input, val, pos)
return translate(token)
}
`
func translate(c int) int {
var conv int = 0
switch c {
{{.Translate}}
}
return conv
}
// Trace function for translate
func TraceTranslate(c int) string {
var conv string = ""
switch c {
{{.TranslateTrace}}
}
return conv
}
// Trace function for reduce
func TraceReduce(reduceIndex, s int, look string) {
if IsTrace {
switch reduceIndex {
{{.ReduceTrace}}
}
}
}
// Code Last part
{{.CodeLast}}`
Loading

0 comments on commit d460080

Please sign in to comment.