Skip to content

Commit 2a582bb

Browse files
committed
Fix bugs
1 parent 4bb793d commit 2a582bb

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

src/types/checker/__tests__/assignStatements.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const testcases: {
2525
int test = 0;
2626
test = 1;
2727
`,
28-
result: { type: null, errors: [] }
28+
result: { type: null, errors: [] },
29+
only: true
2930
},
3031
{
3132
input: `

src/types/checker/environment.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,16 @@ export class Frame {
4242
private constructor() {}
4343

4444
public getMethod(name: string): Method | Error {
45-
let frame: Frame | null = this
46-
while (frame) {
47-
const method = frame._methods.get(name)
48-
if (method) return method
49-
frame = frame._parentFrame
50-
}
45+
const method = this._methods.get(name)
46+
if (method) return method
47+
if (this._parentFrame) return this._parentFrame.getMethod(name)
5148
return new CannotFindSymbolError()
5249
}
5350

5451
public getReturn(): Type | Error {
55-
let frame: Frame | null = this
56-
while (frame) {
57-
const type = frame._returnType
58-
if (type) return type
59-
frame = frame._parentFrame
60-
}
61-
return new Error('Cannot find return type.')
52+
if (this._returnType) return this._returnType
53+
if (this._parentFrame) return this._parentFrame.getReturn()
54+
return new Error('cannot find return type')
6255
}
6356

6457
public getType(name: string): Type | Error {
@@ -69,22 +62,16 @@ export class Frame {
6962
return new Array(prefixType)
7063
}
7164

72-
let frame: Frame | null = this
73-
while (frame) {
74-
const type = frame._types.get(name)
75-
if (type) return type
76-
frame = frame._parentFrame
77-
}
65+
const type = this._types.get(name)
66+
if (type) return type
67+
if (this._parentFrame) return this._parentFrame.getType(name)
7868
return new CannotFindSymbolError()
7969
}
8070

8171
public getVariable(name: string): Type | Error {
82-
let frame: Frame | null = this
83-
while (frame) {
84-
const type = frame._variables.get(name)
85-
if (type) return type
86-
frame = frame._parentFrame
87-
}
72+
const variable = this._variables.get(name)
73+
if (variable) return variable
74+
if (this._parentFrame) return this._parentFrame.getVariable(name)
8875
return new CannotFindSymbolError()
8976
}
9077

src/types/checker/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,14 @@ export const check = (node: Node, frame: Frame = Frame.globalFrame()): Result =>
233233
case 'StringLiteral':
234234
return newResult(String.from(value))
235235
default:
236+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
236237
throw new Error(`Unrecgonized literal type ${kind} found in literal.`)
237238
}
238239
}
239240
case 'LocalVariableDeclarationStatement': {
240241
if (!node.variableDeclaratorList) throw new Error('Variable declarator list is undefined.')
241-
const errors: Error[] = []
242242
const results = node.variableDeclaratorList.map(variableDeclarator => {
243+
const errors: Error[] = []
243244
const declaredType = frame.getType(node.localVariableType)
244245
if (declaredType instanceof Error) return newResult(null, [declaredType])
245246
const { variableInitializer } = variableDeclarator
@@ -262,7 +263,6 @@ export const check = (node: Node, frame: Frame = Frame.globalFrame()): Result =>
262263
if (checkResult.currentType == null)
263264
throw new Error('Variable initializer should return a type.')
264265
if (!declaredType.canBeAssigned(checkResult.currentType)) {
265-
console.log('HELLO!')
266266
errors.push(new IncompatibleTypesError())
267267
}
268268
return

0 commit comments

Comments
 (0)