Skip to content

Commit

Permalink
Merge pull request #6 from danimal141/calc-support
Browse files Browse the repository at this point in the history
Calculation Support
  • Loading branch information
danimal141 authored Dec 3, 2024
2 parents 9edf410 + a141f1e commit 97f8d8f
Show file tree
Hide file tree
Showing 16 changed files with 719 additions and 742 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ This will create a summary that can be easily shared with others or used for doc

Create a file `hello.pgo`:
```go
package main {
package main

func main() {
print("hello")
}
```
Expand Down
6 changes: 6 additions & 0 deletions examples/calc.pgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
print(4 + 3 * 2)
print(10 - 6 / 2)
}
127 changes: 0 additions & 127 deletions src/codegen/doc.ja.md

This file was deleted.

70 changes: 66 additions & 4 deletions src/codegen/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
"declare i32 @printf",
"@.str.fmt = private unnamed_addr constant [3 x i8]",
"define i32 @main()",
"entry:",
"call i32 (i8*, ...) @printf",
"ret i32 0",
];
Expand All @@ -47,15 +48,76 @@ func main() {
print("hello\\nworld")
}`;
const ir = generateIR(input);

// Debug output
console.log("Generated IR:", ir);

assertEquals(
ir.includes("\\0A"),
true,
`IR should contain "\\0A" for newline.\nGenerated IR:\n${ir}`,
);
});
});

describe("arithmetic expressions", () => {
it("should generate correct IR for integer expressions", () => {
const input = `package main
func main() {
print(42)
}`;
const ir = generateIR(input);

const requiredParts = [
'@.str.int.fmt = private unnamed_addr constant [4 x i8] c"%d\\0A\\00"',
"call i32 (i8*, ...) @printf",
];

for (const part of requiredParts) {
assertEquals(
ir.includes(part),
true,
`Expected IR to contain "${part}"\nActual IR:\n${ir}`,
);
}
});

it("should generate correct IR for complex arithmetic expressions", () => {
const input = `package main
func main() {
print(4 + 3 * 2)
print(10 - 6 / 2)
}`;
const ir = generateIR(input);

// Check instruction ordering and numbering
const instructions = [
"%1 = mul nsw i32 3, 2", // First multiplication: 3 * 2
"%2 = add nsw i32 4, %1", // Then addition: 4 + result
"%3 = call i32 (i8*, ...)", // First print call
"%4 = sdiv i32 6, 2", // Division: 6 / 2
"%5 = sub nsw i32 10, %4", // Then subtraction: 10 - result
"%6 = call i32 (i8*, ...)", // Second print call
];

for (const instruction of instructions) {
assertEquals(
ir.includes(instruction),
true,
`Expected IR to contain instruction: "${instruction}"\nActual IR:\n${ir}`,
);
}

// Check instruction order
const irLines = ir.split("\n");
let lastIndex = -1;
for (const instruction of instructions) {
const currentIndex = irLines.findIndex((line) =>
line.trim().startsWith(instruction)
);
assertEquals(
currentIndex > lastIndex,
true,
`Instruction "${instruction}" is not in the correct order`,
);
lastIndex = currentIndex;
}
});
});
});
Loading

0 comments on commit 97f8d8f

Please sign in to comment.