-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-ast.rkt
82 lines (57 loc) · 2.96 KB
/
source-ast.rkt
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
#lang typed/racket/base
(require "core-ast.rkt" "unique.rkt")
(provide (all-defined-out))
(define-type expression
(U binder
lvalue
sequence
assignment
comparison
if-then-else
Constant
negation
function-call
math
equality
create-record
create-array
while-loop
for-loop
break))
(define-type lvalue (U identifier field-ref array-ref))
(define-type declaration
(U type-declaration
function-declaration
untyped-variable-declaration
variable-declaration))
(define-struct: identifier ((symbol : (U Symbol unique))) #:transparent)
(define-struct: field-ref ((base : lvalue) (field : Symbol) (type : (Option type-reference))) #:transparent)
(define-struct: array-ref ((base : lvalue) (index : expression) (type : (Option type-reference))) #:transparent)
(define-struct: binder ((declarations : (Listof declaration)) (body : expression)) #:transparent)
(define-struct: sequence ((exprs : (Listof expression))) #:transparent)
(define-struct: assignment ((value : lvalue) (expr : expression)) #:transparent)
(define-struct: if-then-else
((cond : expression)
(true : expression)
(false : (U #f expression))
(type : (U #f 'nil 'unit type-reference))) #:transparent)
(define-struct: function-call ((fun : expression) (args : (Listof expression)) (type : (Option type-reference))) #:transparent)
(define-struct: negation ((expr : expression)) #:transparent)
(define-struct: comparison ((operator : (U '< '> '<= '>=)) (left : expression) (right : expression) (type : (Option type-reference))))
(define-struct: equality ((operator : (U '= '<> )) (left : expression) (right : expression) (type : (Option type-reference))) #:transparent)
(define-struct: math ((operator : (U '+ '* '/ '- '& '\| )) (left : expression) (right : expression)) #:transparent)
(define-struct: create-record ((type : type-reference) (fields : (Listof (Pair Symbol expression)))) #:transparent)
(define-struct: create-array ((type : type-reference) (size : expression) (value : expression)) #:transparent)
(define-struct: while-loop ((guard : expression) (body : expression)) #:transparent)
(define-struct: for-loop ((id : (U Symbol unique)) (init : expression) (final : expression) (body : expression)) #:transparent)
(define-struct: break () #:transparent)
(define-struct: type-declaration ((name : (U Symbol unique)) (type : (U type-reference compound-type))) #:transparent)
(define-struct: function-declaration
((name : (U Symbol unique))
(args : (Listof (Pair (U Symbol unique) type-reference)))
(return-type : (Option type-reference))
(body : expression)) #:transparent)
(define-struct: variable-declaration ((name : (U Symbol unique)) (type : type-reference) (value : expression)) #:transparent)
(define-struct: untyped-variable-declaration ((name : (U Symbol unique)) (value : expression)) #:transparent)
(define-predicate expression? expression)
(define-predicate declaration? declaration)