-
Notifications
You must be signed in to change notification settings - Fork 43
/
instance_code.go
53 lines (45 loc) · 1.29 KB
/
instance_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package yakast
import (
yak "github.com/yaklang/yaklang/common/yak/antlr4yak/parser"
"github.com/yaklang/yaklang/common/yak/antlr4yak/yakvm"
"github.com/google/uuid"
)
func (y *YakCompiler) VisitInstanceCode(raw yak.IInstanceCodeContext) interface{} {
if y == nil || raw == nil {
return nil
}
i, _ := raw.(*yak.InstanceCodeContext)
if i == nil {
return nil
}
recoverRange := y.SetRange(i.BaseParserRuleContext)
defer recoverRange()
if op := i.Func(); op != nil {
y.writeString(op.GetText() + " ")
}
//var funcName = fmt.Sprintf("anonymous-%v", uuid.NewV4().String())
var yakFn *yakvm.Function
tableRecover := y.SwitchSymbolTable("instanceCode", uuid.New().String())
defer tableRecover()
recoverFunc := y.SwitchCodes()
y.VisitBlock(i.Block(), true)
y.pushOperator(yakvm.OpReturn)
funcCode := make([]*yakvm.Code, len(y.codes))
copy(funcCode, y.codes)
recoverFunc()
yakFn = yakvm.NewFunction(funcCode, y.currentSymtbl)
if y.sourceCodePointer != nil {
yakFn.SetSourceCode(*y.sourceCodePointer)
}
if yakFn == nil {
y.panicCompilerError(compileError, "cannot create yak function from compiler")
}
// 配置函数
//y.pushScope(yakvm.GetCurrentTableCount())
y.pushValue(&yakvm.Value{
TypeVerbose: "anonymous-function",
Value: yakFn,
})
y.pushCall(0)
return nil
}