-
Notifications
You must be signed in to change notification settings - Fork 0
/
cool-basic.hs
91 lines (80 loc) · 2.54 KB
/
cool-basic.hs
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
-- Solution to Stanford Compilers Course.
-- (c) Copyright 2012 Michael Starzinger. All Rights Reserved.
module CoolBasic (
definedClasses,
inheritableClasses,
nameSelf,
nameInit,
nameMain,
nameAbort,
nameTypeName,
nameCopy,
nameOutputString,
nameOutputInt,
nameInputString,
nameInputInt,
nameLength,
nameConcat,
nameSubstr,
typeSELF,
typeObject,
typeMain,
typeIO,
typeInt,
typeBool,
typeString,
) where
import CoolAST
import CoolLexer
-- List of basic classes that are defined by the runtime.
definedClasses :: a -> [Clazz a]
definedClasses a = [ base a, baseIO a, baseInt a, baseBool a, baseString a ]
-- List of basic inheritable classes present in the runtime.
inheritableClasses :: a -> [Clazz a]
inheritableClasses a = [ base a, baseIO a ]
-- Basic variable names.
nameSelf = "self"
-- Basic method names.
nameInit = ".init"
nameMain = "main"
nameAbort = "abort"
nameTypeName = "type_name"
nameCopy = "copy"
nameOutputString = "out_string"
nameOutputInt = "out_int"
nameInputString = "in_string"
nameInputInt = "in_int"
nameLength = "length"
nameConcat = "concat"
nameSubstr = "substr"
-- Basic type constants.
typeSELF = Type "SELF_TYPE"
typeObject = Type "Object"
typeMain = Type "Main"
typeIO = Type "IO"
typeInt = Type "Int"
typeBool = Type "Bool"
typeString = Type "String"
base a = Clazz typeObject Nothing [ m1,m2,m3 ]
where
m1 = Right $ Method nameAbort [] typeObject (IdentifierExpr a nameSelf)
m2 = Right $ Method nameTypeName [] typeString (ConstantExpr a (String ""))
m3 = Right $ Method nameCopy [] typeSELF (IdentifierExpr a nameSelf)
baseIO a = Clazz typeIO Nothing [ m1,m2,m3,m4 ]
where
m1 = Right $ Method nameOutputString [("x",typeString)] typeSELF (IdentifierExpr a nameSelf)
m2 = Right $ Method nameOutputInt [("x",typeInt)] typeSELF (IdentifierExpr a nameSelf)
m3 = Right $ Method nameInputString [] typeString (ConstantExpr a (String ""))
m4 = Right $ Method nameInputInt [] typeInt (ConstantExpr a (Integer "0" 0))
baseInt _ = Clazz typeInt Nothing [ f1 ]
where
f1 = Left $ Attribute "value" typeInt Nothing
baseBool _ = Clazz typeBool Nothing [ f1 ]
where
f1 = Left $ Attribute "value" typeBool Nothing
baseString a = Clazz typeString Nothing [ f1,m1,m2,m3 ]
where
f1 = Left $ Attribute "value" typeString Nothing
m1 = Right $ Method nameLength [] typeInt (ConstantExpr a (Integer "0" 0))
m2 = Right $ Method nameConcat [("s",typeString)] typeString (IdentifierExpr a nameSelf)
m3 = Right $ Method nameSubstr [("i",typeInt),("l",typeInt)] typeString (IdentifierExpr a nameSelf)