-
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.
Add support for while loop blocks in code generator
- Introduce WhileLoopBlock type - Implement while block handler and TypeScript generation - Add unit tests for while loop blocks - Update block generator to handle while loops - Update imports
- Loading branch information
1 parent
f4b2fd0
commit 2f048fc
Showing
6 changed files
with
122 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { CodeBlock } from "../../types/blocks"; | ||
import { FunctionInfo } from "../../types/common"; | ||
import { CodeGeneratorState } from "../../types/generator"; | ||
import { createFunctionCallBlock } from "../blocks/function-call"; | ||
import { createWhileBlock } from "../blocks/while-block"; | ||
|
||
describe("While Block Generator", () => { | ||
let initialState: CodeGeneratorState; | ||
let dummyFunctionInfo: FunctionInfo; | ||
|
||
beforeEach(() => { | ||
initialState = { | ||
blocks: [], | ||
variables: [], | ||
isAsync: false, | ||
}; | ||
dummyFunctionInfo = { | ||
name: "dummyFunction", | ||
returnType: "void", | ||
}; | ||
}); | ||
|
||
test("generates simple while block successfully", () => { | ||
const condition = "x < 10"; | ||
const loopBlocks: CodeBlock[] = [ | ||
createFunctionCallBlock(dummyFunctionInfo, initialState), | ||
]; | ||
|
||
const result = createWhileBlock(condition, loopBlocks, initialState); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result.blockType).toBe("while"); | ||
expect(result.condition).toBe(condition); | ||
expect(result.loopBlocks).toEqual(loopBlocks); | ||
expect(result.index).toBe(1); // 0 is the dummy function block | ||
}); | ||
|
||
test("generates nested while block successfully", () => { | ||
const outerCondition = "x < 10"; | ||
const innerWhileBlock = createWhileBlock( | ||
"y < 5", | ||
[createFunctionCallBlock(dummyFunctionInfo, initialState)], | ||
initialState | ||
); | ||
|
||
const result = createWhileBlock( | ||
outerCondition, | ||
[innerWhileBlock], | ||
initialState | ||
); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result.blockType).toBe("while"); | ||
expect(result.condition).toBe(outerCondition); | ||
expect(result.loopBlocks).toHaveLength(1); | ||
expect(result.loopBlocks[0]).toBe(innerWhileBlock); | ||
expect(result.index).toBe(2); // 0 is the dummy function block, 1 is the inner while block | ||
}); | ||
|
||
test("updates state correctly after creating while block", () => { | ||
const condition = "x < 10"; | ||
const loopBlocks: CodeBlock[] = [ | ||
createFunctionCallBlock(dummyFunctionInfo, initialState), | ||
]; | ||
|
||
const result = createWhileBlock(condition, loopBlocks, initialState); | ||
|
||
expect(initialState.blocks).toHaveLength(2); | ||
expect(initialState.blocks[1]).toBe(result); | ||
}); | ||
}); |
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,37 @@ | ||
import { CodeBlock, WhileLoopBlock } from "../../types/blocks"; | ||
import { CodeGeneratorState } from "../../types/generator"; | ||
import { factory, Statement } from "typescript"; | ||
import { blockToTypeScript } from "../block-generator"; | ||
|
||
export function whileBlockToTypeScript( | ||
block: WhileLoopBlock, | ||
state: CodeGeneratorState | ||
): Statement { | ||
const loopBody = factory.createBlock( | ||
block.loopBlocks.map((b) => blockToTypeScript(b, state)) | ||
); | ||
|
||
return factory.createWhileStatement( | ||
factory.createIdentifier(block.condition), | ||
loopBody | ||
); | ||
} | ||
|
||
export function createWhileBlock( | ||
condition: string, | ||
loopBlocks: CodeBlock[], | ||
state: CodeGeneratorState | ||
): WhileLoopBlock { | ||
const index = state.blocks.length; | ||
|
||
const block: WhileLoopBlock = { | ||
condition, | ||
loopBlocks, | ||
index, | ||
blockType: "while", | ||
}; | ||
|
||
state.blocks.push(block); | ||
|
||
return block; | ||
} |
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