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

Implement Command and compile #4

Merged
merged 7 commits into from
Nov 30, 2024
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
5 changes: 1 addition & 4 deletions .summaryignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
bin
.vscode
deno.lock
Dockerfile
docker-compose.yml
README.ja.md
**/*/*.ja.md

node_modules
dist
8 changes: 5 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.importMap": "./deno.json",
"editor.defaultFormatter": "denoland.vscode-deno",
"deno.unstable": [],
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
},
"typescript.validate.enable": false,
"javascript.validate.enable": false
}
11 changes: 0 additions & 11 deletions Dockerfile

This file was deleted.

8 changes: 4 additions & 4 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ error(message) // エラーを生成
### 5.1 コンパイルコマンド
```bash
# 基本的なコンパイル
$ pogo source.sg
$ pogo source.pg

# 出力ファイル指定
$ pogo -o program source.sg
$ pogo -o program source.pg

# LLVM IR出力
$ pogo --emit-llvm source.sg
$ pogo --emit-llvm source.pg

# 最適化レベル指定
$ pogo -O2 source.sg
$ pogo -O2 source.pg
```

### 5.2 オプション
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ error(message) // Create an error
### 5.1 Compilation Commands
```bash
# Basic compilation
$ pogo source.sg
$ pogo source.pg

# Specify output file
$ pogo -o program source.sg
$ pogo -o program source.pg

# Output LLVM IR
$ pogo --emit-llvm source.sg
$ pogo --emit-llvm source.pg

# Specify optimization level
$ pogo -O2 source.sg
$ pogo -O2 source.pg
```

### 5.2 Options
Expand Down Expand Up @@ -269,6 +269,11 @@ poor-go/

## 11. Tool Integration

### Dependencies
* deno v2 or later
* managed by asdf
* llvm

### LLVM Tools
```bash
# Generate LLVM bitcode
Expand All @@ -284,7 +289,7 @@ $ clang output.bc -o program
### Debug Support
```bash
# Generate debug information
$ pogo -g source.sg
$ pogo -g source.pg

# Use LLVM debugging tools
$ lldb ./program
Expand Down
2 changes: 0 additions & 2 deletions bin/run

This file was deleted.

4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"start": "deno run --allow-read --allow-write main.ts",
"test": "deno test --allow-read",
"test:watch": "deno test --watch --allow-read",
"compile": "deno compile --allow-read --allow-write main.ts",
"compile": "deno compile --allow-read --allow-write -o dist/pogo main.ts",
"lint": "deno lint **/*.ts",
"fmt": "deno fmt **/*.ts",
"check": "deno check **/*.ts"
Expand All @@ -15,7 +15,7 @@
},
"compilerOptions": {
"strict": true,
"lib": ["deno.window"],
"lib": ["deno.ns", "deno.window", "deno.unstable"],
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions docker-compose.yml

This file was deleted.

16 changes: 16 additions & 0 deletions examples/test.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare i32 @printf(i8* nocapture readonly, ...)

@.str = private unnamed_addr constant [2 x i8] c"%s\00", align 1

@.str.0 = private unnamed_addr constant [12 x i8] c"hello world\00", align 1

define i32 @main() {

entry:

%1 = getelementptr [2 x i8], [2 x i8]* @.str, i64 0, i64 0
%0 = getelementptr [8 x i8], [8 x i8]* @.str.0, i64 0, i64 0
%2 = call i32 (i8*, ...) @printf(i8* %1, i8* %0)
ret i32 0

}
1 change: 1 addition & 0 deletions examples/test.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main { print("hello world") }
99 changes: 98 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
console.log("Hello World.");
import { parseArgs } from "std/cli/parse_args.ts";
import { Lexer } from "@/lexer/lexer.ts";
import { Parser } from "@/parser/parser.ts";
import { SemanticAnalyzer } from "@/semantic/analyzer.ts";
import { LLVMGenerator } from "@/codegen/generator.ts";
import { CompilerPipeline } from "@/compiler/pipeline.ts";

async function main() {
// Type-safe command line arguments handling
const flags = parseArgs(Deno.args, {
string: ["output", "o"],
boolean: ["help", "emit-llvm", "verbose"],
default: {
output: "a.out",
"emit-llvm": false,
verbose: false,
},
alias: {
o: "output"
},
});

if (flags["help"] || flags._.length === 0) {
console.log("Usage: poor-go [options] <source-file>");
console.log("Options:");
console.log(" -o, --output <file> Output file name");
console.log(" --emit-llvm Output LLVM IR");
console.log(" --verbose Show compilation process");
console.log(" --help Show this help");
Deno.exit(0);
}

try {
// Read input file with explicit error handling
let source: string;
const sourceFile = flags._[0]?.toString();
if (!sourceFile) {
throw new Error("No input file specified");
}

try {
source = await Deno.readTextFile(sourceFile);
} catch (_error) {
throw new Error(`Failed to read source file: ${sourceFile}`);
}

if (flags["verbose"]) {
console.log(`Compiling ${sourceFile}...`);
}

// Lexical analysis
const lexer = new Lexer(source);

// Syntax analysis
const parser = new Parser(lexer);
const ast = parser.parseProgram();

// Semantic analysis
const analyzer = new SemanticAnalyzer();
analyzer.analyze(ast);

// Generate LLVM IR
const generator = new LLVMGenerator();
const llvmIR = generator.generate(ast);

// Compile to executable
const compiler = new CompilerPipeline();
const outFile = flags["output"] || "a.out";

const result = await compiler.compile(llvmIR, {
outputPath: outFile,
emitLLVM: Boolean(flags["emit-llvm"]),
verbose: Boolean(flags["verbose"]),
});

if (!result.success) {
console.error("Compilation failed:", result.error);
Deno.exit(1);
}

if (flags["verbose"]) {
console.log(`Successfully compiled to ${outFile}`);
}

} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error("Compilation failed:", errorMessage);
Deno.exit(1);
}
}

// Explicitly check if this is the main module
if (import.meta.main) {
main().catch((error) => {
console.error("Fatal error:", error);
Deno.exit(1);
});
}
2 changes: 1 addition & 1 deletion src/codegen/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("LLVMGenerator", () => {
// Verify required components are present
const requiredParts = [
"declare i32 @printf",
"@.str = private unnamed_addr constant [2 x i8]",
"@.str.fmt = private unnamed_addr constant [3 x i8]", // Updated format string size
"define i32 @main()",
"call i32 (i8*, ...) @printf",
"ret i32 0",
Expand Down
Loading