-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexprtype.py
71 lines (57 loc) · 1.84 KB
/
exprtype.py
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
# exprtype.py
'''
Expr Type System
================
This file defines classes representing types. There is a general
class used to represent all types. Each type is then a singleton
instance of the type class.
class ExprType(object):
pass
int_type = ExprType("int",...)
float_type = ExprType("float",...)
string_type = ExprType("string", ...)
The contents of the type class is entirely up to you. However, you
will minimally need to encode some information about:
a. What operators are supported (+, -, *, etc.).
b. Default values
c. ????
d. Profit!
Once you have defined the built-in types, you will need to
make sure they get registered with any symbol tables or
code that checks for type names in 'exprcheck.py'.
'''
class ExprType(object):
'''
Class that represents a type in the Expr language. Types
are declared as singleton instances of this type.
'''
def __init__(self, name, bin_ops=set(), un_ops=set()):
'''
You must implement yourself and figure out what to store.
'''
self.name = name
self.bin_ops = bin_ops
self.un_ops = un_ops
# Create specific instances of types. You will need to add
# appropriate arguments depending on your definition of ExprType
int_type = ExprType("int",
set(('PLUS', 'MINUS', 'TIMES', 'DIVIDE',
'LE', 'LT', 'EQ', 'NE', 'GT', 'GE')),
set(('PLUS', 'MINUS')),
)
float_type = ExprType("float",
set(('PLUS', 'MINUS', 'TIMES', 'DIVIDE',
'LE', 'LT', 'EQ', 'NE', 'GT', 'GE')),
set(('PLUS', 'MINUS')),
)
string_type = ExprType("string",
set(('PLUS',)),
set(),
)
boolean_type = ExprType("bool",
set(('LAND', 'LOR', 'EQ', 'NE')),
set(('LNOT',))
)
# In your type checking code, you will need to reference the
# above type objects. Think of how you will want to access
# them.