/
types.rkt
119 lines (96 loc) · 5.55 KB
/
types.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
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
#lang typed/racket
(require typed/racket/unsafe)
(require (submod pyramid/types common))
(provide (all-defined-out))
(define-type CValue (U Integer String Char))
(define-type (Maybe A) (U A #f))
(define-type c-typespace (U #f 'struct 'union))
(struct c-declaration () #:transparent)
(struct c-expression () #:transparent)
(struct c-type-fixed ([ signed? : Boolean ] [ bytes : Size ]) #:transparent)
(struct c-type-struct-field ([ name : (U #f Symbol) ] [ type : c-type ]) #:mutable #:transparent)
(struct c-type-struct ([ name : (Maybe Symbol) ] [ fs : c-type-struct-fields ]) #:transparent)
(struct c-type-union ([ name : (Maybe Symbol) ] [ fs : c-type-struct-fields ]) #:transparent)
(struct c-type-alias ([ name : Symbol ] [ typespace : c-typespace]) #:transparent)
(struct c-type-pointer ([ type : c-type ]) #:transparent)
(struct c-type-void () #:transparent)
(struct c-signature ([ ret : c-type ] [ args : c-sigvars ]) #:transparent)
(: t-int c-type)
(define t-int (c-type-fixed #t 32))
(: t-uint c-type)
(define t-uint (c-type-fixed #f 32))
(: t-char c-type)
(define t-char (c-type-fixed #f 1))
(: t-size c-type)
(define t-size t-uint)
(define-type c-type-struct-fields (Listof c-type-struct-field))
(define-type c-type (U c-type-fixed c-type-struct c-type-alias c-signature c-type-pointer c-type-void c-type-union))
(define-predicate c-type? c-type)
(struct c-unit ([ decls : c-declarations ]) #:transparent)
(struct c-decl-var c-declaration ([ name : Symbol ] [ type : c-type ] [ init : (Maybe c-expression) ]) #:mutable #:transparent)
(struct c-decl-type c-declaration ([ name : Symbol ] [ type : c-type ]) #:transparent)
(struct c-decl-func c-declaration ([ name : Symbol ] [ sig : c-signature ] [ body : c-statement ]) #:transparent)
(define-type c-modifier (U 'extern 'static '*))
(struct c-labeled ([ name : Symbol ] [ body : c-statement ]) #:transparent)
(struct c-labeled-case ([ expected : c-expression ] [ body : c-statement ]) #:transparent)
(struct c-labeled-default ([ body : c-statement ]) #:transparent)
(struct c-expression-statement ([ exp : c-expression ]) #:transparent)
(struct c-switch ([ actual : c-expression ] [ body : c-statement ]) #:transparent)
(struct c-if ([ pred : c-expression ] [ consequent : c-statement ] [ alternative : c-statement ]) #:transparent)
(struct c-for ([ init : (Listof c-decl-var) ] [ pred : (Maybe c-expression) ] [ post : (Maybe c-expression) ] [ body : c-statement ]) #:transparent)
(struct c-while ([ pred : c-expression ] [ body : c-statement ]) #:transparent)
(struct c-do-while ([ pred : c-expression ] [ body : c-statement ]) #:transparent)
(struct c-goto ([ target : Symbol ]) #:transparent)
(struct c-block ([ body : c-statements ]) #:transparent)
(struct c-return ([ val : (Maybe c-expression)]) #:transparent)
(struct c-break () #:transparent)
(struct c-continue () #:transparent)
(struct c-sigvar ([ name : Symbol ] [ type : c-type ]) #:transparent)
(define-type c-statement (U c-labeled
c-labeled-case
c-labeled-default
c-expression-statement
c-switch
c-if
c-for
c-while
c-do-while
c-goto
c-block
c-return
c-break
c-continue
c-declaration))
(struct c-const c-expression ([ value : CValue ] [ signed? : Boolean ]) #:transparent)
(struct c-variable c-expression ([ name : Symbol ]) #:transparent)
(struct c-ternary c-expression ([ pred : c-expression ] [ consequent : c-expression ] [ alternative : c-expression ]) #:transparent)
(struct c-binop c-expression ([ op : Symbol ] [ left : c-expression ] [ right : c-expression ]) #:transparent)
(struct c-unop c-expression ([ op : Symbol ] [ exp : c-expression ]) #:transparent)
(struct c-function-call c-expression ([ func : c-expression ] [ args : c-expressions ]) #:transparent)
(struct c-field-access c-expression ([ source : c-expression ] [ name : Symbol ]) #:transparent)
(struct c-cast c-expression ([ type : c-type ] [ exp : c-expression ]) #:transparent)
(struct c-sizeof c-expression ([ value : (U c-type c-expression)]) #:transparent)
(struct c-array-access c-expression ([ array : c-expression ] [ index : c-expression ]) #:transparent)
(struct c-expression-sequence c-expression ([ exps : c-expressions ]) #:transparent)
(struct c-expression-array c-expression ([ exps : c-expressions ]) #:transparent)
(struct c-field-info ([ offset : Integer ] [ type : c-type ]) #:transparent)
(define-type c-value-type (U 'lvalue 'rvalue))
(define-type c-statements (Listof c-statement))
(define-type c-modifiers (Listof c-modifier))
(define-type c-declarations (Listof c-declaration))
(define-type c-expressions (Listof c-expression))
(define-type c-sigvars (Listof c-sigvar))
(define-type c-signatures (Listof c-signature))
(define-type c-labeled-cases (Listof c-labeled-case))
(define-type c-labeled-defaults (Listof c-labeled-default))
(define-type c-decl-vars (Listof c-decl-var))
(define-type FieldTable (HashTable Symbol c-field-info))
(define-type TypeRegistry (HashTable Symbol c-type))
(define-type variable-table (HashTable Symbol c-type))
(: make-field-table (-> FieldTable))
(define (make-field-table) (make-hash))
(: make-variable-table (-> variable-table))
(define (make-variable-table) (make-hash))
(: make-type-registry (-> TypeRegistry))
(define make-type-registry make-hash)
(define-type CQ Sexp)