Skip to content

Commit 26e09ef

Browse files
committed
feat: add support for line and block comments
1 parent 40b98e5 commit 26e09ef

13 files changed

Lines changed: 133 additions & 7 deletions

examples/block_comments.kit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include "stdio.h";
2+
3+
/* Block comment at start of file */
4+
function main() {
5+
/* Block comment with indentation */
6+
var x: int = 42; /* End of line block comment */
7+
8+
/*
9+
* Multi-line block comment
10+
* with multiple lines
11+
*/
12+
printf("%d", x);
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

examples/line_comments.kit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include "stdio.h";
2+
3+
// Line comment at start of file
4+
function main() {
5+
// Line comment with indentation
6+
var x: int = 42; // End of line comment
7+
8+
// Another line comment
9+
printf("%d", x);
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

examples/mixed_comments.kit

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include "stdio.h";
2+
3+
// Mix of line and block comments
4+
function main() {
5+
// Start with line comment
6+
var x: int = 1;
7+
8+
/* Block comment in middle */
9+
var y: int = 2; // End of line comment
10+
11+
/*
12+
* Multi-line block comment
13+
* followed by line comment
14+
*/
15+
// After multi-line
16+
var z: int = x + y;
17+
printf("%d", z); /* Block comment at end */
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

examples/nested_comments.kit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include "stdio.h";
2+
3+
// Test that nested block comments fail appropriately (this should not compile)
4+
function main() {
5+
/* This has /* nested */ comment */
6+
var x = 1;
7+
printf("%d\n", x);
8+
}

kitc/tests/examples.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,47 @@ fn test_range_negative() -> Result<(), Box<dyn std::error::Error>> {
159159
fn test_simple_range() -> Result<(), Box<dyn std::error::Error>> {
160160
run_example_test("simple_range", None)
161161
}
162+
163+
#[test]
164+
fn test_line_comments() -> Result<(), Box<dyn std::error::Error>> {
165+
run_example_test("line_comments", None)
166+
}
167+
168+
#[test]
169+
fn test_block_comments() -> Result<(), Box<dyn std::error::Error>> {
170+
run_example_test("block_comments", None)
171+
}
172+
173+
#[test]
174+
fn test_mixed_comments() -> Result<(), Box<dyn std::error::Error>> {
175+
run_example_test("mixed_comments", None)
176+
}
177+
178+
#[test]
179+
#[test]
180+
fn test_nested_comments() -> Result<(), Box<dyn std::error::Error>> {
181+
let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
182+
.parent()
183+
.ok_or("couldn't get workspace root")?;
184+
185+
let examples_dir = workspace_root.join("examples");
186+
let example_file = examples_dir.join("nested_comments.kit");
187+
188+
assert!(
189+
example_file.exists(),
190+
"example file {} does not exist",
191+
example_file.display()
192+
);
193+
194+
// This test should fail to compile due to nested block comments
195+
let kitc = cargo_bin!("kitc");
196+
let mut cmd = assert_cmd::Command::from_std(std::process::Command::new(kitc));
197+
cmd.current_dir(workspace_root);
198+
cmd.arg("compile").arg(&example_file);
199+
200+
let result = cmd.assert();
201+
// Should fail to compile
202+
assert!(result.try_success().is_err());
203+
204+
Ok(())
205+
}

kitlang/src/codegen/frontend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::collections::HashSet;
55
use std::path::{Path, PathBuf};
66
use std::process::Command;
77

8-
use crate::codegen::ast::{Include, Program, Function, Block, Stmt, Expr, Literal};
8+
use crate::codegen::ast::*;
99
use crate::codegen::compiler::{CompilerMeta, CompilerOptions, Toolchain};
10-
use crate::codegen::types::{Type, ToCRepr};
1110
use crate::codegen::parser::Parser as CodeParser;
11+
use crate::codegen::types::{ToCRepr, Type};
1212

1313
pub type CompileResult<T> = Result<T, CompilationError>;
1414

@@ -29,7 +29,7 @@ impl Compiler {
2929
c_output: output.as_ref().with_extension("c"),
3030
includes: Vec::new(),
3131
libs,
32-
parser: CodeParser,
32+
parser: CodeParser::new(),
3333
}
3434
}
3535

kitlang/src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
pub mod ast;
88
pub mod compiler;
99
pub mod parser;
10+
pub use ast::{Block, Expr, Function, Include, Literal, Param, Program, Stmt};
1011
pub use compiler::Toolchain;
11-
pub use ast::{Include, Program, Function, Param, Block, Stmt, Expr, Literal};
1212

1313
/// Handles the initial parsing of Kitlang source files, constructs the
1414
/// Abstract Syntax Tree (AST), and orchestrates the generation of C code

0 commit comments

Comments
 (0)