J'ai un projet de créer un compilateur en langage scheme, SVP,, est ce qu'il y a quelqu'un pour m'aider dans plusieurs choses..
J'ai un projet de créer un compilateur en langage scheme, SVP,, est ce qu'il y a quelqu'un pour m'aider dans plusieurs choses..
À vrai dire, je n’ai aucune idée comment on écrit des compilateurs, mais on pourrait, peut-être, discuter quelque chose de concret.
C'est le code que j'ai écrit; mais il y a un problème pour le booléen dans (= ,expr1 ,expr2),
je doit écrire push $1 pour #f et push $9 pour #t mais je ne sais pas comment!
et aussi pour: (println #f) et (println #t)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 ;;; File: codegen.scm (include "match.scm") ;;;============================================================================ ;; Program compiler. (define (compile-program exprs) (list " .text\n" " .globl _main\n" " .globl main\n" "_main:\n" "main:\n" (map compile-expr exprs) " mov $0, %rax\n" " ret\n")) (define jumping 0) (define (compile-expr expr) (if (and (pair? expr) (equal? (car expr) 'println) (pair? (cdr expr)) (null? (cddr expr))) (let ((val (cadr expr))) (list " push $" val "\n" " call print_word_dec\n" " push $10\n" " call putchar\n")) (error "unknown expression" expr))) (define (compile-expr expr) ;; version avec macro "match" (match expr ((println ,val) (list " push $" val "\n" " call print_word_dec\n" " push $10\n" " call putchar\n")))) (define (compile-expr expr) (match expr ((println ,expr1) (append (compile-expr expr1) (list " call print_word_dec\n" " push $10\n" " call putchar\n"))) (,val (list " push $" val "\n")))) (define (compile-expr expr) (match expr ((println #f) ;when (boolean? expr1) (append (list " push $35\n" " call putchar\n" " push $102\n" " call putchar\n" " push $10\n" " call putchar\n"))) ((println #t) ;when (boolean? expr1) (append (list " push $35\n" " call putchar\n" " push $116\n" " call putchar\n" " push $10\n" " call putchar\n"))) ((println ,expr1) (append (compile-expr expr1) (list " call print_word_dec\n" " push $10\n" " call putchar\n"))) ((+ ,expr1 ,expr2) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rax\n" " add %rax,(%rsp)\n"))) ((- ,expr1 ,expr2) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rax\n" " sub %rax,(%rsp)\n"))) ((* ,expr1 ,expr2) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " imul (%rsp), %rbx\n" " mov %rbx,(%rsp)\n"))) ((quotient ,expr1 ,expr2) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " mov (%rsp), %rax\n" " cqto\n" " idiv %rbx\n" " mov %rax,(%rsp)\n"))) ((modulo ,expr1 ,expr2) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " mov %rbx, %rcx\n" " mov (%rsp), %rax\n " " cqto\n" " idiv %rbx\n" " mov %rdx, (%rsp)\n" " test %rax, %rax\n" (string-append " js negatif" (number->string (begin (set! jumping (+ 1 jumping)) jumping)) "\n") (string-append " positif" (number->string jumping) ":\n") (string-append " jmp end" (number->string jumping) "\n") (string-append " negatif" (number->string jumping) ":\n") " test %rdx, %rdx\n" (string-append " jz nul" (number->string jumping) "\n") " add %rcx,(%rsp)\n" (string-append " jmp end" (number->string jumping) "\n") (string-append " nul" (number->string jumping) ":\n") (string-append " jmp end" (number->string jumping) "\n") (string-append " end" (number->string jumping) ":\n")))) ((= ,expr1 ,expr2) (let ((et1 (gensym))) (let ((et2 (gensym))) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " pop %rax\n" " cmp %rbx,%rax\n" " je " et1 "\n" " push $35\n";# " call putchar\n" " push $102\n";#f " call putchar\n" " push $10\n" ; " call putchar\n" " jmp " et2 "\n" " " et1 ":\n" " push $35\n";# " call putchar\n" " push $116\n";#t " call putchar\n" " push $10\n" ; " call putchar\n" " " et2 ":\n"))))) ((< ,expr1 ,expr2) (let ((et1 (gensym))) (let ((et2 (gensym))) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " pop %rax\n" " sub %rbx,%rax\n" " jl " et1 "\n" " push $35\n";# " call putchar\n" " push $102\n";#f " call putchar\n" " push $10\n" ; " call putchar\n" " jmp " et2 "\n" " " et1 ":\n" " push $35\n";# " call putchar\n" " push $116\n";#t " call putchar\n" " push $10\n" ; " call putchar\n" " " et2 ":\n"))))) ((if ,condition ,expr1 ,expr2) (if condition (append (compile-expr expr1) (compile-expr expr2) (list " pop %rax\n")) (append (compile-expr expr1) (compile-expr expr2) (list " pop %rbx\n" " movq %rbx,(%rsp)\n")))) (,val (list " push $" val "\n")))) ;;;===========================================================================
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager