-
Notifications
You must be signed in to change notification settings - Fork 24
/
visitor_test.go
189 lines (182 loc) · 4.31 KB
/
visitor_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gscript
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/crossoverJie/gscript/parser"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
func TestGScriptVisitor_Visit_Lexer(t *testing.T) {
expression := "(2+3) * 2"
input := antlr.NewInputStream(expression)
lexer := parser.NewGScriptLexer(input)
for {
t := lexer.NextToken()
if t.GetTokenType() == antlr.TokenEOF {
break
}
fmt.Printf("%s (%q) %d\n",
lexer.SymbolicNames[t.GetTokenType()], t.GetText(), t.GetColumn())
}
}
func TestGScriptVisitor_Visit(t *testing.T) {
expression := "(2+3) * 2;"
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, "=", result)
assert.Equal(t, result, 10)
}
func TestGScriptVisitor_Visit2(t *testing.T) {
expression := "5%2;"
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, "=", result)
assert.Equal(t, result, 1)
}
func TestMod(t *testing.T) {
val, err := strconv.ParseInt("2", 32, 10)
fmt.Println(val, err)
fmt.Println(5 % 2)
}
func TestGScriptVisitor_VisitIfElse5(t *testing.T) {
expression := `
if(3==(1+2)){
return 1+2*3;
}`
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, " result:", result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), 7)
}
func TestGScriptVisitor_VisitIfElse6(t *testing.T) {
expression := `
if(3<(1+2)){
return 1+2*3;
} else {
return 2;
}`
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, " result:", result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), 2)
}
func TestGScriptVisitor_VisitIfElse7(t *testing.T) {
expression := `
if(3<(1+2)){
return 1+2*3;
} else {
return "123";
}`
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, " result:", result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), "123")
}
func TestGScriptVisitor_VisitIfElse8(t *testing.T) {
expression := `
if(3!=(1+2)){
return 1+3;
} else {
return false;
}`
var result = NewCompiler().Compiler(expression)
fmt.Println(expression, " result:", result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), false)
}
func TestArithmeticOperators(t *testing.T) {
expression := `
if(4==(2+2)){
return 1+3;
} else {
return false;
}`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), 4)
}
func TestArithmeticOperators2(t *testing.T) {
expression := `(10+20)*20;`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
assert.Equal(t, result, 600)
}
func TestArithmeticOperators3(t *testing.T) {
expression := `(1+1.1)-2;`
var result = NewCompiler().Compiler(expression)
fmt.Println(result.(float64))
expression = `(10+10)*10+10.1;`
result = NewCompiler().Compiler(expression)
fmt.Println(result)
}
func TestArithmeticOperators4(t *testing.T) {
expression := `
if ( (10 +10 ) == 20 ) {
return true ;
} else {
return 20 ;
}
`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), true)
}
func TestArithmeticOperators5(t *testing.T) {
expression := `
if ( (10 +10 ) == 20 ) {
return 10++ ;
} else {
return 20 ;
}
`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), 11)
}
func TestArithmeticOperators6(t *testing.T) {
expression := `
if ( (10 +10 ) == 20 ) {
return !(1+1==2) ;
} else {
return 20 ;
}
`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
object := result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), false)
expression = `
if ( (10 +10 ) == 20 ) {
return !(1+1!=2) ;
} else {
return 20 ;
}
`
result = NewCompiler().Compiler(expression)
fmt.Println(result)
object = result.(*ReturnObject)
assert.Equal(t, object.GetReturnObject(), true)
}
func TestArithmeticOperators7(t *testing.T) {
expression := `
if ( (10 +10 ) == 20 ) {
return !10 ;
} else {
return 20 ;
}
`
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
}
func TestDeclare(t *testing.T) {
expression := `int a=100;`
var result = NewCompiler().Compiler(expression)
fmt.Println(result)
}