Skip to content

Commit

Permalink
Fix warning and remove logs
Browse files Browse the repository at this point in the history
  • Loading branch information
danimal141 committed Nov 30, 2024
1 parent dd918ee commit 7831cbc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
4 changes: 1 addition & 3 deletions .summaryignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
.vscode
deno.lock
Dockerfile
docker-compose.yml
**/*/doc.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
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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 Down
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 -o poor-go main.ts",
"compile": "deno compile --allow-read --allow-write -o dist/poor-go 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
18 changes: 13 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LLVMGenerator } from "@/codegen/generator.ts";
import { CompilerPipeline } from "@/compiler/pipeline.ts";

async function main() {
// Parse command line arguments using parseArgs
// Type-safe command line arguments handling
const flags = parseArgs(Deno.args, {
string: ["output", "o"],
boolean: ["help", "emit-llvm", "verbose"],
Expand All @@ -31,13 +31,18 @@ async function main() {
}

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

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

if (flags["verbose"]) {
console.log(`Compiling ${sourceFile}...`);
Expand All @@ -60,7 +65,6 @@ async function main() {

// Compile to executable
const compiler = new CompilerPipeline();
// デフォルト値が設定されているので必ず string 型
const outFile = flags["output"] || "a.out";

const result = await compiler.compile(llvmIR, {
Expand All @@ -85,6 +89,10 @@ async function main() {
}
}

// Explicitly check if this is the main module
if (import.meta.main) {
main();
main().catch((error) => {
console.error("Fatal error:", error);
Deno.exit(1);
});
}
28 changes: 8 additions & 20 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,26 +235,21 @@ export class Parser {
}

private parseCallExpression(): CallExpression {
this.logToken("parseCallExpression - start");

const func: Identifier = {
type: "Identifier",
value: this.currentToken.literal,
location: this.currentLocation(),
};

this.nextToken(); // consume function name
this.logToken("parseCallExpression - after function name");

if (!this.checkToken(this.currentToken, TokenType.LPAREN)) {
this.throwError("Expected '(' after function name");
}

this.nextToken(); // consume '('
this.logToken("parseCallExpression - after LPAREN");

const args = this.parseCallArguments();
this.logToken("parseCallExpression - after arguments");

return {
type: "CallExpression",
Expand All @@ -266,17 +261,13 @@ export class Parser {

private parseCallArguments(): Expression[] {
const args: Expression[] = [];
this.logToken("parseCallArguments - start");

if (this.checkToken(this.currentToken, TokenType.RPAREN)) {
this.logToken("parseCallArguments - found immediate RPAREN");
this.nextToken(); // consume ')'
return args;
}

if (this.checkToken(this.currentToken, TokenType.STRING)) {
this.logToken("parseCallArguments - found STRING");

const strLit: StringLiteral = {
type: "StringLiteral",
value: this.currentToken.literal,
Expand All @@ -285,31 +276,28 @@ export class Parser {
args.push(strLit);

this.nextToken(); // consume string literal
this.logToken("parseCallArguments - after string literal");
} else {
this.throwError(`Unexpected argument token ${this.currentToken.type}`);
}

if (!this.checkToken(this.currentToken, TokenType.RPAREN)) {
this.logToken("parseCallArguments - missing RPAREN");
this.throwError(
`Expected ')' after argument, got ${this.currentToken.type}`,
);
}
this.nextToken(); // consume ')'
this.logToken("parseCallArguments - end");

return args;
}

private logToken(prefix: string) {
console.log(`${prefix}: {
type: ${this.currentToken.type},
literal: "${this.currentToken.literal}",
line: ${this.currentToken.line},
column: ${this.currentToken.column}
}`);
}
// private logToken(prefix: string) {
// console.log(`${prefix}: {
// type: ${this.currentToken.type},
// literal: "${this.currentToken.literal}",
// line: ${this.currentToken.line},
// column: ${this.currentToken.column}
// }`);
// }

// private parseCallArgument(): Expression {
// switch (this.currentToken.type) {
Expand Down

0 comments on commit 7831cbc

Please sign in to comment.