-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from yazaldefilimone/feat/array
Feat/array
- Loading branch information
Showing
24 changed files
with
386 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Token } from "token/token"; | ||
import { Expression, ExpressionKind } from "./base"; | ||
import { Maybe } from "utils"; | ||
|
||
export class ArrayLiteral implements Expression { | ||
token: Token; | ||
kind: ExpressionKind; | ||
public elements: Maybe<Expression[]>; | ||
constructor(token: Token, elements: Maybe<Expression[]> = null) { | ||
this.token = token; | ||
this.kind = ExpressionKind.ARRAY; | ||
this.elements = elements || null; | ||
} | ||
expressionNode(): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
tokenLiteral(): string { | ||
return this.token.literal; | ||
} | ||
toString(): string { | ||
if (this.elements === null) { | ||
return ""; | ||
} | ||
return `[${this.elements.map((e) => e.toString()).join(", ")}]`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Token } from "token"; | ||
import { Expression, ExpressionKind } from "./base"; | ||
import { Maybe } from "utils"; | ||
|
||
export class IndexExpression implements Expression { | ||
token: Token; | ||
left: Maybe<Expression>; | ||
index: Maybe<Expression>; | ||
kind: ExpressionKind; | ||
constructor(token: Token, left: Maybe<Expression> = null, index: Maybe<Expression> = null) { | ||
this.token = token; | ||
this.left = left; | ||
this.index = index; | ||
this.kind = ExpressionKind.INDEX; | ||
} | ||
|
||
expressionNode() {} | ||
tokenLiteral() { | ||
return this.token.literal; | ||
} | ||
toString() { | ||
return `(${this.left?.toString()}[${this.index?.toString()}])`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BaseObject, EBaseObject } from "evaluator/object"; | ||
|
||
export class InternalArray implements BaseObject<BaseObject[]> { | ||
private _elements: BaseObject[]; | ||
constructor(elements: BaseObject[]) { | ||
this._elements = elements; | ||
} | ||
type(): EBaseObject { | ||
return EBaseObject.ARRAY; | ||
} | ||
inspect(): string { | ||
return this._elements.map((el) => el.inspect()).join(", "); | ||
} | ||
get value(): BaseObject[] { | ||
return this._elements; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { it, expect, describe } from "vitest"; | ||
import { makeSut } from "./sut"; | ||
import { InternalArray, Integer } from "evaluator/object"; | ||
|
||
describe("Evaluator", () => { | ||
describe("Array", () => { | ||
it("should create array and access by index", () => { | ||
const tests = [ | ||
{ | ||
input: ` | ||
[1, 2, 3][0]; | ||
`, | ||
expected: 1, | ||
}, | ||
{ | ||
input: ` | ||
[1, 2, 3][1]; | ||
`, | ||
expected: 2, | ||
}, | ||
{ | ||
input: ` | ||
[1, 2, 3][2]; | ||
`, | ||
expected: 3, | ||
}, | ||
{ | ||
input: ` | ||
let i = 0; | ||
[1][i]; | ||
`, | ||
expected: 1, | ||
}, | ||
{ | ||
input: ` | ||
[1, 2, 3][1 + 1]; | ||
`, | ||
expected: 3, | ||
}, | ||
{ | ||
input: ` | ||
let myArray = [1, 2, 3]; | ||
myArray[2]; | ||
`, | ||
expected: 3, | ||
}, | ||
{ | ||
input: ` | ||
let myArray = [1, 2, 3]; | ||
myArray[0] + myArray[1] + myArray[2]; | ||
`, | ||
expected: 6, | ||
}, | ||
{ | ||
input: ` | ||
let myArray = [1, 2, 3]; | ||
let i = myArray[0]; | ||
myArray[i]; | ||
`, | ||
expected: 2, | ||
}, | ||
{ | ||
input: ` | ||
[1, 2, 3][3]; | ||
`, | ||
expected: null, | ||
}, | ||
{ | ||
input: ` | ||
[1, 2, 3][-1]; | ||
`, | ||
expected: null, | ||
}, | ||
]; | ||
for (const tt of tests) { | ||
const sut = makeSut(tt.input); | ||
expect(sut).not.toBeNull(); | ||
expect(sut?.value).toBe(tt.expected); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { it, expect, describe } from "vitest"; | ||
import { makeSut } from "./sut"; | ||
import { InternalArray, Integer } from "evaluator/object"; | ||
|
||
describe("Evaluator", () => { | ||
describe("Array", () => { | ||
it("should create array", () => { | ||
const arrayTest = [1, 2, 3]; | ||
const tt = { | ||
input: ` | ||
let myArray = [${arrayTest.join(", ")}]; | ||
myArray; | ||
`, | ||
expected: new InternalArray(arrayTest.map((num) => new Integer(num))), | ||
}; | ||
const sut = makeSut(tt.input); | ||
expect(sut).not.toBeNull(); | ||
expect(sut?.value).toEqual(tt.expected.value); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.