-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer_test.go
96 lines (80 loc) · 1.87 KB
/
printer_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
package quisnix
import (
"bytes"
"fmt"
"github.com/milandamen/quisnix/lexer"
"github.com/milandamen/quisnix/semanalyzer"
"github.com/milandamen/quisnix/parser"
"github.com/milandamen/quisnix/printer"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Printer", func() {
FIt("should print correct LLVM IR", func() {
l := lexer.Lexer{}
p := parser.Parser{}
a := semanalyzer.SemAnalyzer{}
pr := printer.LLVMPrinter{}
program := `
func main() Int {
var a Int;
a = 123;
a = test(a);
return a;
}
func test(asd Int) Int {
return 2 + asd;
}
`
tokens, err := l.Parse(bytes.NewBufferString(program))
Expect(err).To(Succeed())
//Expect(len(tokens)).To(Equal(20))
declarations, fileScope, err := p.Parse(tokens)
Expect(err).To(Succeed())
//Expect(len(declarations)).To(Equal(1))
mainFunc, err := a.Analyze(declarations, fileScope)
Expect(err).To(Succeed())
Expect(mainFunc).ToNot(BeNil())
b := bytes.Buffer{}
Expect(pr.Print(&b, declarations)).To(Succeed())
fmt.Println(b.String())
})
PIt("should print correct LLVM IR", func() {
l := lexer.Lexer{}
p := parser.Parser{}
a := semanalyzer.SemAnalyzer{}
pr := printer.LLVMPrinter{}
program := `
func main() {
var a Int;
a = 123;
a = test(a);
a = test(123);
}
func test(asd Int) Int {
var a Int;
var b Byte;
var cc String;
a = 123 + 4;
b = 'b';
cc = "abc";
a -= 2 + 3 * 4;
a++;
a = a + asd;
return a;
}
`
tokens, err := l.Parse(bytes.NewBufferString(program))
Expect(err).To(Succeed())
Expect(len(tokens)).To(Equal(83))
declarations, fileScope, err := p.Parse(tokens)
Expect(err).To(Succeed())
Expect(len(declarations)).To(Equal(2))
mainFunc, err := a.Analyze(declarations, fileScope)
Expect(err).To(Succeed())
Expect(mainFunc).ToNot(BeNil())
b := bytes.Buffer{}
Expect(pr.Print(&b, declarations)).To(Succeed())
fmt.Println(b.String())
})
})