Skip to content

Commit 2c56fef

Browse files
committed
C Convert to AST - Test run success - create git ignore for ignore /target
1 parent c5c7fc6 commit 2c56fef

File tree

370 files changed

+371
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

370 files changed

+371
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

Cargo.lock

+26-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
2-
name = "Crown"
2+
name = "crown"
33
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
clang-sys = "1.8.1"

doc/issuse-2.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1. **کد C را به AST تبدیل می‌کنید**: شما توانستید از طریق استفاده از `clang` و `-ast-dump`، کد C ورودی را به یک درخت تجزیه (AST) تبدیل کنید.
2+
3+
2. **کد Rust برای تجزیه C آماده است**: کد Rust شما با استفاده از `Command` به درستی از Clang برای پردازش کد C و استخراج AST استفاده می‌کند.
4+
5+
3. **تست‌ها با موفقیت اجرا شده‌اند**: تمامی تست‌ها به درستی کار کرده و به خروجی مورد انتظار رسیده‌اند. این بدان معناست که کد شما قادر به شبیه‌سازی و استخراج کد C به AST است و تست‌ها هم به درستی جواب می‌دهند.

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod parser;
2+

src/parser/clang_parser.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use clang_sys::support::Clang;
2+
use std::path::Path;
3+
use std::ffi::CString;
4+
use std::process::{Command, Stdio};
5+
use std::io::Write; // وارد کردن Write برای استفاده از متد write_all
6+
7+
/// این تابع برای تجزیه کد C به صورت یک AST ساده استفاده می‌شود.
8+
pub fn parse_c_code(code: &str) -> Result<String, String> {
9+
let path = Path::new("/usr/bin/clang");
10+
11+
let mut child = Command::new(path)
12+
.arg("-cc1")
13+
.arg("-x")
14+
.arg("c")
15+
.arg("-ast-dump")
16+
.arg("-")
17+
.stdin(Stdio::piped())
18+
.stdout(Stdio::piped())
19+
.stderr(Stdio::piped()) // اضافه کردن stderr
20+
.spawn()
21+
.map_err(|e| e.to_string())?;
22+
23+
if let Some(mut stdin) = child.stdin.take() {
24+
stdin.write_all(code.as_bytes()).map_err(|e| e.to_string())?;
25+
}
26+
27+
let output = child.wait_with_output().map_err(|e| e.to_string())?;
28+
29+
if !output.status.success() {
30+
let error_output = String::from_utf8_lossy(&output.stderr);
31+
return Err(format!("Clang failed with status: {}\n{}", output.status, error_output));
32+
}
33+
34+
let ast = String::from_utf8(output.stdout).map_err(|e| e.to_string())?;
35+
36+
println!("AST Output: {}", ast); // نمایش کامل خروجی
37+
38+
Ok(ast)
39+
}
40+
41+
42+
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
48+
#[test]
49+
fn test_parse_function() {
50+
let code = r#"
51+
int main() {
52+
return 0;
53+
}
54+
"#;
55+
56+
let ast = parse_c_code(code).unwrap();
57+
assert!(ast.contains("main"));
58+
}
59+
60+
#[test]
61+
fn test_parse_variable() {
62+
let code = r#"
63+
int a = 10;
64+
"#;
65+
66+
let ast = parse_c_code(code).unwrap();
67+
println!("AST Output: {}", ast);
68+
assert!(ast.contains("a"));
69+
70+
}
71+
}
72+

src/parser/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod clang_parser;
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"$message_type":"diagnostic","message":"can't find crate for `clang_sys`","code":{"code":"E0463","explanation":"A crate was declared but cannot be found.\n\nErroneous code example:\n\n```compile_fail,E0463\nextern crate foo; // error: can't find crate\n```\n\nYou need to link your code to the relevant crate in order to be able to use it\n(through Cargo or the `-L` option of rustc, for example).\n\n## Common causes\n\n- The crate is not present at all. If using Cargo, add it to `[dependencies]`\n in Cargo.toml.\n- The crate is present, but under a different name. If using Cargo, look for\n `package = ` under `[dependencies]` in Cargo.toml.\n\n## Common causes for missing `std` or `core`\n\n- You are cross-compiling for a target which doesn't have `std` prepackaged.\n Consider one of the following:\n + Adding a pre-compiled version of std with `rustup target add`\n + Building std from source with `cargo build -Z build-std`\n + Using `#![no_std]` at the crate root, so you won't need `std` in the first\n place.\n- You are developing the compiler itself and haven't built libstd from source.\n You can usually build it with `x.py build library/std`. More information\n about x.py is available in the [rustc-dev-guide].\n\n[rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html#building-the-compiler\n"},"level":"error","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":0,"byte_end":23,"line_start":1,"line_end":1,"column_start":1,"column_end":24,"is_primary":true,"text":[{"text":"extern crate clang_sys;","highlight_start":1,"highlight_end":24}],"label":"can't find crate","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror[E0463]\u001b[0m\u001b[0m\u001b[1m: can't find crate for `clang_sys`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/parser/clang_parser.rs:1:1\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m1\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0mextern crate clang_sys;\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mcan't find crate\u001b[0m\n\n"}
2+
{"$message_type":"diagnostic","message":"aborting due to 1 previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: aborting due to 1 previous error\u001b[0m\n\n"}
3+
{"$message_type":"diagnostic","message":"For more information about this error, try `rustc --explain E0463`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1mFor more information about this error, try `rustc --explain E0463`.\u001b[0m\n"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ed5fa1088f637008
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":13207435774680941178,"features":"[]","declared_features":"[]","target":1984032727470466438,"profile":11597332650809196192,"path":10602529704205407992,"deps":[[6895127599370038612,"clang_sys",false,6859782510580660348],[14806868352350619280,"Crown",false,12353783466517598881]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/Crown-3dffbff173e5bce9/dep-bin-Crown","checksum":false}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"$message_type":"diagnostic","message":"unused import: `clang_sys::support::Clang`","code":{"code":"unused_imports","explanation":null},"level":"warning","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":4,"byte_end":29,"line_start":1,"line_end":1,"column_start":5,"column_end":30,"is_primary":true,"text":[{"text":"use clang_sys::support::Clang;","highlight_start":5,"highlight_end":30}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`#[warn(unused_imports)]` on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"remove the whole `use` item","code":null,"level":"help","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":0,"byte_end":31,"line_start":1,"line_end":2,"column_start":1,"column_end":1,"is_primary":true,"text":[{"text":"use clang_sys::support::Clang;","highlight_start":1,"highlight_end":31},{"text":"use std::path::Path;","highlight_start":1,"highlight_end":1}],"label":null,"suggested_replacement":"","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: unused import: `clang_sys::support::Clang`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/parser/clang_parser.rs:1:5\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m1\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0muse clang_sys::support::Clang;\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: `#[warn(unused_imports)]` on by default\u001b[0m\n\n"}
2+
{"$message_type":"diagnostic","message":"unused variable: `args`","code":{"code":"unused_variables","explanation":null},"level":"warning","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":608,"byte_end":612,"line_start":13,"line_end":13,"column_start":9,"column_end":13,"is_primary":true,"text":[{"text":" let args = vec![","highlight_start":9,"highlight_end":13}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`#[warn(unused_variables)]` on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"if this is intentional, prefix it with an underscore","code":null,"level":"help","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":608,"byte_end":612,"line_start":13,"line_end":13,"column_start":9,"column_end":13,"is_primary":true,"text":[{"text":" let args = vec![","highlight_start":9,"highlight_end":13}],"label":null,"suggested_replacement":"_args","suggestion_applicability":"MaybeIncorrect","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: unused variable: `args`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/parser/clang_parser.rs:13:9\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m13\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m let args = vec![\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33m^^^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33mhelp: if this is intentional, prefix it with an underscore: `_args`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: `#[warn(unused_variables)]` on by default\u001b[0m\n\n"}
3+
{"$message_type":"diagnostic","message":"2 warnings emitted","code":null,"level":"warning","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: 2 warnings emitted\u001b[0m\n\n"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4b2cd65154a232fb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":13207435774680941178,"features":"[]","declared_features":"[]","target":9212279850505161265,"profile":15632368228915330634,"path":17777289886553719987,"deps":[[6895127599370038612,"clang_sys",false,6859782510580660348]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/Crown-4771282e33be8aaf/dep-test-lib-Crown","checksum":false}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"$message_type":"diagnostic","message":"can't find crate for `clang_sys`","code":{"code":"E0463","explanation":"A crate was declared but cannot be found.\n\nErroneous code example:\n\n```compile_fail,E0463\nextern crate foo; // error: can't find crate\n```\n\nYou need to link your code to the relevant crate in order to be able to use it\n(through Cargo or the `-L` option of rustc, for example).\n\n## Common causes\n\n- The crate is not present at all. If using Cargo, add it to `[dependencies]`\n in Cargo.toml.\n- The crate is present, but under a different name. If using Cargo, look for\n `package = ` under `[dependencies]` in Cargo.toml.\n\n## Common causes for missing `std` or `core`\n\n- You are cross-compiling for a target which doesn't have `std` prepackaged.\n Consider one of the following:\n + Adding a pre-compiled version of std with `rustup target add`\n + Building std from source with `cargo build -Z build-std`\n + Using `#![no_std]` at the crate root, so you won't need `std` in the first\n place.\n- You are developing the compiler itself and haven't built libstd from source.\n You can usually build it with `x.py build library/std`. More information\n about x.py is available in the [rustc-dev-guide].\n\n[rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html#building-the-compiler\n"},"level":"error","spans":[{"file_name":"src/parser/clang_parser.rs","byte_start":0,"byte_end":23,"line_start":1,"line_end":1,"column_start":1,"column_end":24,"is_primary":true,"text":[{"text":"extern crate clang_sys;","highlight_start":1,"highlight_end":24}],"label":"can't find crate","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror[E0463]\u001b[0m\u001b[0m\u001b[1m: can't find crate for `clang_sys`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/parser/clang_parser.rs:1:1\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m1\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0mextern crate clang_sys;\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^^^^^^^^^^^^^^^^^^^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mcan't find crate\u001b[0m\n\n"}
2+
{"$message_type":"diagnostic","message":"aborting due to 1 previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: aborting due to 1 previous error\u001b[0m\n\n"}
3+
{"$message_type":"diagnostic","message":"For more information about this error, try `rustc --explain E0463`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1mFor more information about this error, try `rustc --explain E0463`.\u001b[0m\n"}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file has an mtime of when this was started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a112e0119c7471ab
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rustc":13207435774680941178,"features":"[]","declared_features":"[]","target":9212279850505161265,"profile":11597332650809196192,"path":17777289886553719987,"deps":[[6895127599370038612,"clang_sys",false,6859782510580660348]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/Crown-a700c5b1cf4385cb/dep-lib-Crown","checksum":false}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}

0 commit comments

Comments
 (0)