1
- import { UnannType } from " ../ast/types/classes" ;
2
- import { DECLARED_BUT_NOT_YET_ASSIGNED , GLOBAL_FRAME , OBJECT_CLASS } from " ./constants" ;
3
- import * as errors from " ./errors" ;
4
- import * as struct from " ./structCreator" ;
1
+ import { UnannType } from ' ../ast/types/classes'
2
+ import { DECLARED_BUT_NOT_YET_ASSIGNED , GLOBAL_FRAME , OBJECT_CLASS } from ' ./constants'
3
+ import * as errors from ' ./errors'
4
+ import * as struct from ' ./structCreator'
5
5
import {
6
6
Class ,
7
7
Closure ,
@@ -11,181 +11,181 @@ import {
11
11
StashItem ,
12
12
Value ,
13
13
VarValue ,
14
- Variable ,
15
- } from " ./types" ;
16
- import { Stack } from " ./utils" ;
14
+ Variable
15
+ } from ' ./types'
16
+ import { Stack } from ' ./utils'
17
17
18
18
/**
19
19
* Components of CSE Machine.
20
20
*/
21
- export class Control extends Stack < ControlItem > { } ;
22
- export class Stash extends Stack < StashItem > { } ;
21
+ export class Control extends Stack < ControlItem > { }
22
+ export class Stash extends Stack < StashItem > { }
23
23
24
24
export class Environment {
25
- private _global : EnvNode ;
26
- private _current : EnvNode ;
27
- private _objects : Object [ ] = [ ] ;
25
+ private _global : EnvNode
26
+ private _current : EnvNode
27
+ private _objects : Object [ ] = [ ]
28
28
29
29
constructor ( ) {
30
- const node = new EnvNode ( GLOBAL_FRAME ) ;
31
- this . _global = node ;
32
- this . _current = node ;
30
+ const node = new EnvNode ( GLOBAL_FRAME )
31
+ this . _global = node
32
+ this . _current = node
33
33
}
34
34
35
35
get global ( ) {
36
- return this . _global ;
36
+ return this . _global
37
37
}
38
38
39
39
get current ( ) {
40
- return this . _current ;
40
+ return this . _current
41
41
}
42
42
43
43
get objects ( ) {
44
- return this . _objects ;
44
+ return this . _objects
45
45
}
46
46
47
47
set current ( node : EnvNode ) {
48
- this . _current = node ;
48
+ this . _current = node
49
49
}
50
50
51
51
extendEnv ( fromEnv : EnvNode , name : string ) {
52
52
// Create new environemnt.
53
- const node = new EnvNode ( name ) ;
54
- node . parent = fromEnv ;
55
- fromEnv . addChild ( node ) ;
53
+ const node = new EnvNode ( name )
54
+ node . parent = fromEnv
55
+ fromEnv . addChild ( node )
56
56
57
57
// Set current environment.
58
- this . _current = node ;
58
+ this . _current = node
59
59
}
60
60
61
61
createObj ( c : Class ) : Object {
62
62
// Create new environment.
63
- const node = new EnvNode ( OBJECT_CLASS ) ;
63
+ const node = new EnvNode ( OBJECT_CLASS )
64
64
65
65
// Create new object.
66
- const obj = struct . objStruct ( node , c ) ;
66
+ const obj = struct . objStruct ( node , c )
67
67
68
68
// Add to objects arr.
69
- this . _objects . push ( obj ) ;
69
+ this . _objects . push ( obj )
70
70
71
71
// Set current environment.
72
- this . _current = node ;
72
+ this . _current = node
73
73
74
- return obj ;
74
+ return obj
75
75
}
76
76
77
77
restoreEnv ( toEnv : EnvNode ) {
78
- this . _current = toEnv ;
78
+ this . _current = toEnv
79
79
}
80
80
81
81
declareVariable ( name : Name , type : UnannType ) {
82
- const variable = struct . varStruct ( type , name , DECLARED_BUT_NOT_YET_ASSIGNED ) ;
83
- this . _current . setVariable ( name , variable ) ;
82
+ const variable = struct . varStruct ( type , name , DECLARED_BUT_NOT_YET_ASSIGNED )
83
+ this . _current . setVariable ( name , variable )
84
84
}
85
85
86
86
defineVariable ( name : Name , type : UnannType , value : VarValue ) {
87
- const variable = struct . varStruct ( type , name , value ) ;
88
- this . _current . setVariable ( name , variable ) ;
87
+ const variable = struct . varStruct ( type , name , value )
88
+ this . _current . setVariable ( name , variable )
89
89
}
90
90
91
91
getName ( name : Name ) : Variable | Class {
92
- return this . _current . getName ( name ) ;
92
+ return this . _current . getName ( name )
93
93
}
94
94
95
95
getVariable ( name : Name ) : Variable {
96
- return this . _current . getVariable ( name ) ;
96
+ return this . _current . getVariable ( name )
97
97
}
98
98
99
99
defineMtdOrCon ( name : Name , method : Closure ) {
100
- this . _current . setMtdOrCon ( name , method ) ;
100
+ this . _current . setMtdOrCon ( name , method )
101
101
}
102
102
103
103
defineClass ( name : Name , c : Class ) {
104
- this . _global . setClass ( name , c ) ;
104
+ this . _global . setClass ( name , c )
105
105
}
106
106
107
107
getClass ( name : Name ) : Class {
108
- return this . _global . getClass ( name ) ;
108
+ return this . _global . getClass ( name )
109
109
}
110
- } ;
110
+ }
111
111
112
112
// Frame with metadata, e.g., parent/children, name.
113
113
export class EnvNode {
114
- private _frame : Frame = new Frame ( ) ;
115
- private _parent : EnvNode ;
116
- private _children : EnvNode [ ] = [ ] ;
114
+ private _frame : Frame = new Frame ( )
115
+ private _parent : EnvNode
116
+ private _children : EnvNode [ ] = [ ]
117
117
118
118
constructor ( readonly name : string ) { }
119
119
120
120
get frame ( ) {
121
- return this . _frame ;
121
+ return this . _frame
122
122
}
123
123
124
124
get parent ( ) : EnvNode {
125
- return this . _parent ;
125
+ return this . _parent
126
126
}
127
127
128
128
set parent ( parent : EnvNode ) {
129
- this . _parent = parent ;
129
+ this . _parent = parent
130
130
}
131
131
132
132
get children ( ) {
133
- return this . _children ;
133
+ return this . _children
134
134
}
135
135
136
136
addChild ( child : EnvNode ) {
137
- this . _children . push ( child ) ;
137
+ this . _children . push ( child )
138
138
}
139
139
140
140
setVariable ( name : Name , value : Variable ) {
141
141
if ( this . _frame . has ( name ) ) {
142
- throw new errors . VariableRedeclarationError ( name ) ;
142
+ throw new errors . VariableRedeclarationError ( name )
143
143
}
144
- this . _frame . set ( name , value ) ;
144
+ this . _frame . set ( name , value )
145
145
}
146
146
147
147
getName ( name : Name ) : Variable | Class {
148
148
if ( this . _frame . has ( name ) ) {
149
- return this . _frame . get ( name ) as Variable | Class ;
149
+ return this . _frame . get ( name ) as Variable | Class
150
150
}
151
151
if ( this . _parent ) {
152
- return this . _parent . getName ( name ) ;
152
+ return this . _parent . getName ( name )
153
153
}
154
- throw new errors . UndeclaredNameError ( name ) ;
154
+ throw new errors . UndeclaredNameError ( name )
155
155
}
156
156
157
157
getVariable ( name : Name ) : Variable {
158
158
if ( this . _frame . has ( name ) ) {
159
- return this . _frame . get ( name ) as Variable ;
159
+ return this . _frame . get ( name ) as Variable
160
160
}
161
161
if ( this . _parent ) {
162
- return this . _parent . getVariable ( name ) ;
162
+ return this . _parent . getVariable ( name )
163
163
}
164
- throw new errors . UndeclaredVariableError ( name ) ;
164
+ throw new errors . UndeclaredVariableError ( name )
165
165
}
166
166
167
167
setMtdOrCon ( name : Name , value : Closure ) {
168
168
if ( this . _frame . has ( name ) ) {
169
- throw new errors . MtdOrConRedeclarationError ( name ) ;
169
+ throw new errors . MtdOrConRedeclarationError ( name )
170
170
}
171
- this . _frame . set ( name , value ) ;
171
+ this . _frame . set ( name , value )
172
172
}
173
173
174
174
setClass ( name : Name , value : Class ) {
175
175
if ( this . _frame . has ( name ) ) {
176
- throw new errors . ClassRedeclarationError ( name ) ;
176
+ throw new errors . ClassRedeclarationError ( name )
177
177
}
178
- this . _frame . set ( name , value ) ;
178
+ this . _frame . set ( name , value )
179
179
}
180
180
181
181
getClass ( name : Name ) : Class {
182
182
if ( this . _frame . has ( name ) ) {
183
- return this . _frame . get ( name ) as Class ;
183
+ return this . _frame . get ( name ) as Class
184
184
}
185
185
if ( this . _parent ) {
186
- return this . _parent . getClass ( name ) ;
186
+ return this . _parent . getClass ( name )
187
187
}
188
- throw new errors . UndeclaredClassError ( name ) ;
188
+ throw new errors . UndeclaredClassError ( name )
189
189
}
190
190
}
191
191
0 commit comments