Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support return statement and closure #40

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/evaluator/evaluated/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
makeEvaluatedBoolean,
makeEvaluatedFunction,
makeEvaluatedEmpty,
wrapReturnValue,
} from "./";
import type { Evaluated } from "./";
import type {
FunctionExpression
} from "../../parser/syntax-tree";
Expand Down Expand Up @@ -80,3 +82,13 @@ describe("makeEvaluatedEmpty()", () => {
expect(evaluated.representation).toBe("(비어있음)");
});
});

describe("wrapReturnValue()", () => {
it("wrap return value", () => {
const valueMock = {} as Evaluated;

const wrapped = wrapReturnValue(valueMock);

expect(wrapped.type).toBe("return value");
});
});
11 changes: 11 additions & 0 deletions src/evaluator/evaluated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export interface EvaluatedEmpty extends EvaluatedBase {
readonly type: "empty";
}

export interface ReturnValue {
readonly type: "return value";
readonly value: Evaluated;
}

export type Evaluated =
EvaluatedPrimitive |
EvaluatedFunction |
Expand Down Expand Up @@ -93,3 +98,9 @@ export const makeEvaluatedEmpty: MakeEvaluatedEmpty = () => ({
return "(비어있음)";
},
});

export type WrapReturnValue = (value: Evaluated) => ReturnValue;
export const wrapReturnValue: WrapReturnValue = value => ({
type: "return value",
value,
});
24 changes: 23 additions & 1 deletion src/evaluator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,31 @@ describe("evaluate()", () => {
const cases = [
{
name: "function call with function literal",
input: "함수(바나나) { 바나나 + 1 }(42)",
input: "함수(바나나) { 결과 바나나 + 1 }(42)",
expected: 43,
},
{
name: "function call with identifier",
input: "더하기 = 함수(숫자1, 숫자2) { 결과 숫자1 + 숫자2 } 더하기(3, 4)",
expected: 7,
},
];

it.each(cases)("evaluate $name", testEvaluatingPrimitive);
});

describe("complex statements with function and calls", () => {
const cases = [
{
name: "make and call function containing branch statement",
input: "과일 = 함수(색깔) { 만약 (색깔 == '빨강') { 결과 '사과' } 아니면 { '포도' } } 과일('빨강')",
expected: "사과",
},
{
name: "make and call closure",
input: "더하기 = 함수(숫자1) { 결과 함수(숫자2) { 결과 숫자1+숫자2 } } 하나더하기 = 더하기(1) 하나더하기(4)",
expected: 5,
},
];

it.each(cases)("evaluate $name", testEvaluatingPrimitive);
Expand Down
Loading