MCPcopy Create free account
hub / github.com/crossoverJie/gscript / EvaluateWithRuntime

Function EvaluateWithRuntime

syntax/parse.go:124–283  ·  view source on GitHub ↗

EvaluateWithRuntime deep recursion loop AST

(node *ASTNode, indent string)

Source from the content-addressed store, hash-verified

122
123// EvaluateWithRuntime deep recursion loop AST
124func EvaluateWithRuntime(node *ASTNode, indent string) interface{} {
125 var result interface{}
126 switch node.GetNodeType() {
127 case Program:
128 for _, astNode := range node.GetChildren() {
129 result = EvaluateWithRuntime(astNode, indent)
130 }
131 case AdditiveType:
132 ch01 := node.GetChildren()[0]
133 val1 := EvaluateWithRuntime(ch01, indent+"\t")
134 ch02 := node.GetChildren()[1]
135 val2 := EvaluateWithRuntime(ch02, indent+"\t")
136 var (
137 v1i int
138 v1f float64
139 v2i int
140 v2f float64
141 )
142 switch val1.(type) {
143 case int:
144 v1i = val1.(int)
145 case float64:
146 v1f = val1.(float64)
147 }
148
149 switch val2.(type) {
150 case int:
151 v2i = val2.(int)
152 case float64:
153 v2f = val2.(float64)
154 }
155
156 if node.GetText() == "+" {
157 if v1i != 0 && v2i != 0 {
158 result = v1i + v2i
159 }
160 if v1i != 0 && v2f != 0 {
161 result = float64(v1i) + v2f
162 }
163 if v1f != 0 && v2i != 0 {
164 result = v1f + float64(v2i)
165 }
166 if v1f != 0 && v2f != 0 {
167 result = v1f + v2f
168 }
169
170 } else {
171 if v1i != 0 && v2i != 0 {
172 result = v1i - v2i
173 }
174 if v1i != 0 && v2f != 0 {
175 result = float64(v1i) - v2f
176 }
177 if v1f != 0 && v2i != 0 {
178 result = v1f - float64(v2i)
179 }
180 if v1f != 0 && v2f != 0 {
181 result = v1f - v2f

Callers 1

ArithmeticOperatorsFunction · 0.85

Calls 4

GetNodeTypeMethod · 0.80
GetChildrenMethod · 0.80
GetTextMethod · 0.80
VarsMethod · 0.80

Tested by

no test coverage detected