-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_error_propogation.go
152 lines (129 loc) · 4.17 KB
/
plugin_error_propogation.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package kplugins
import (
"errors"
"fmt"
"github.com/ShindouMihou/korin/pkg/klabels"
"strconv"
"strings"
)
type ErrorPropogationPlugin struct {
Plugin
}
func NewErrorPropogationPlugin() ErrorPropogationPlugin {
return ErrorPropogationPlugin{}
}
func (p ErrorPropogationPlugin) Name() string {
return "KorinErrorPropogation"
}
func (p ErrorPropogationPlugin) Group() string {
return "pw.mihou"
}
func (p ErrorPropogationPlugin) Version() string {
return "1.0.0"
}
func (p ErrorPropogationPlugin) Context(file string) *any {
return nil
}
func (p ErrorPropogationPlugin) FreeContext(file string) {
}
func (p ErrorPropogationPlugin) Process(line string, index int, headers *Headers, stack []klabels.Analysis, context *any) (string, error) {
analysis := stack[index]
varDeclaration := ReadHelper.Get(klabels.VariableKind, analysis.Labels)
shouldPropogate, parameters := ReadHelper.Parameters("+k:float", analysis.Labels)
if shouldPropogate {
if varDeclaration == nil {
return NoChanges, fmt.Errorf("cannot float error in a non-variable-declaration line (Line %d)", analysis.Line+1)
}
variables := (*varDeclaration).Data.([]klabels.VariableDeclaration)
var errorVar *klabels.VariableDeclaration
if len(parameters) > 0 {
pos, err := strconv.Atoi(parameters[0])
if err != nil {
return NoChanges, errors.Join(
fmt.Errorf("expected `int` from k:float's first parameter (position of error variable) "+
"(Line %d)", analysis.Line+1),
err,
)
}
if len(variables) < pos {
return NoChanges, fmt.Errorf("no variable in the position of %d in k:float (Line %d)", pos, analysis.Line+1)
}
errorVar = &variables[pos]
} else {
for _, variable := range variables {
variable := variable
if variable.Name == "_" || variable.Name == "err" {
errorVar = &variable
break
}
}
}
if errorVar == nil {
return NoChanges, fmt.Errorf("no error variable provided in k:float (Line %d): "+
"use _ or `err` for the error name, or provide the position as an argument", analysis.Line+1)
}
var function *klabels.Label
for index := analysis.Line; function == nil && index > 0; index-- {
fn := ReadHelper.Get(klabels.FunctionKind, stack[index].Labels)
if fn != nil {
function = fn
break
}
}
if function == nil {
return NoChanges, fmt.Errorf("k:float variable is declared in a non-function scope. (Line %d)", analysis.Line+1)
}
results := function.Data.(klabels.FunctionDeclaration).Result
var resultValues []string
for _, result := range results {
result := result
if result == "string" {
resultValues = append(resultValues, "\"\"")
} else if result == "bool" {
resultValues = append(resultValues, "false")
} else if result == "rune" {
resultValues = append(resultValues, "''")
} else if strings.Contains(result, "float") || result == "byte" || strings.Contains(result, "int") {
resultValues = append(resultValues, "0")
} else if result == "error" {
name := (*errorVar).Name
if name == "_" {
name = "err"
}
resultValues = append(resultValues, name)
} else {
resultValues = append(resultValues, "nil")
}
}
var declarationNames []string
var declarationValues []string
var errorVariableName string
for _, variable := range variables {
variable := variable
if variable == *errorVar {
if variable.Name == "_" {
errorVariableName = "err"
declarationNames = append(declarationNames, "err")
} else {
errorVariableName = variable.Name
}
} else {
declarationNames = append(declarationNames, variable.Name)
}
if variable.Value != nil {
declarationValues = append(declarationValues, *variable.Value)
}
}
tab := SyntaxHelper.TabSizeFrom(line)
line := tab
line += SyntaxHelper.VariableDeclaration(errorVar.Reassignment, declarationNames, declarationValues, "")
line += SyntaxHelper.NewLine()
line += tab + SyntaxHelper.If(errorVariableName+" != nil") + " " + SyntaxHelper.OpenBracket()
line += SyntaxHelper.NewLine()
line += tab + SyntaxHelper.Tab() + SyntaxHelper.Return(resultValues)
line += SyntaxHelper.NewLine()
line += tab + SyntaxHelper.CloseBracket()
return line, nil
}
return NoChanges, nil
}