-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
keyuchang
committed
May 30, 2022
1 parent
d608322
commit d460080
Showing
8 changed files
with
157 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
@@ -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{ | ||
|
@@ -70,10 +111,6 @@ func PopContex() { | |
StateSymStack = globalContext[len(globalContext)-1].StackSym | ||
globalContext = globalContext[:len(globalContext)-1] | ||
} | ||
func init() { | ||
ParserInit() | ||
} | ||
func ParserInit() { | ||
StateSymStack = []StateSym{ | ||
{ | ||
|
@@ -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}}` |
Oops, something went wrong.