Skip to content

Commit 2a30bb1

Browse files
committed
feat: implement calculate_pi_with_n_iterations algorithm
using trigonometric formula
1 parent 07cc63f commit 2a30bb1

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

examples/pi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use auto_rust::auto_implement;
22

33
#[auto_implement]
4+
/// This algorithm works by calculating the area of a circle using the trigonometric formula for the area of a triangle.
5+
/// Don't use powi, use powf instead. Don't use the sqrt function, use the hypot function instead.
46
fn calculate_pi_with_n_iterations(n: u64) -> f64 {
57
todo!()
68
}

examples/quine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn calculate_rust_quine() -> String {
88
}
99

1010
fn main() {
11-
let quine = calculate_rust_quine().to_string();
11+
let quine = calculate_rust_quine();
1212

1313
print!("{}", quine)
1414
}

src/generator.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,26 @@ pub fn generate_body_function_from_head(
77
extra_context: Option<String>,
88
) -> Result<String, Box<dyn Error>> {
99
let system_message = "
10-
You are an advanced AI trained on the GPT-4 architecture, specializing in Rust programming. Your primary role is to generate Rust function bodies based on provided function signatures. Here's how you'll approach each task:
10+
You are an advanced AI, trained on the GPT-4 architecture, with expertise in Rust programming. Your task is to generate the body of a Rust function based on its signature. Please adhere to these guidelines:
1111
12-
1. Understand the Function Signature: Analyze the provided function signature to determine the function's purpose and expected behavior.
13-
2. Plan the Implementation: Conceptualize the necessary steps and logic required to implement the function.
14-
3. Write the Code: Generate the Rust code for the function body that fulfills the requirements of the function signature.
15-
4. Ensure Clarity and Efficiency: Write code that is clear, concise, and efficient, avoiding unnecessary complexity.
16-
5. Compliance with Constraints: Do not include triple backticks, the original function signature, or extraneous explanations in your response. Stick to plain Rust code for the function body.
12+
1. Receive the Function Signature: The signature will be provided in a standard Rust format, e.g., 'fn calculate_pi_with_n_iterations(n: u64) -> f64'. Focus on understanding the function's name, parameters, and return type.
13+
2. Generate Only the Function Body: You are required to write Rust code that fulfills the requirements of the function signature. This code should be the function body only, without including the function signature or any other wrapping code.
14+
3. Exclude Non-Essential Content: Your response must strictly contain valid Rust code applicable within the function's curly braces. Do not include comments, attributes, nested functions, or any redundant repetitions of the function signature.
15+
4. Maintain Simplicity and Clarity: Avoid external crates, unnecessary imports, or extra features like feature flags. Use standard Rust libraries and functionalities. The code should be clear, maintainable, and compile-ready.
16+
5. Adhere to Rust Best Practices: Ensure that the generated code is idiomatic, efficient, and adheres to Rust standards and best practices.
1717
18-
Respond with only the function body as plain Rust code. Each response must be a direct implementation of the given function signature, tailored to its specific requirements.
18+
Your response should consist solely of the plain Rust code for the function body, fitting perfectly within the provided function signature, and ready for seamless integration into a Rust program.
1919
20-
Example 1:
21-
INPUT: /implement fn my_ip() -> String
22-
OUTPUT:
23-
use std::net::UdpSocket;
24-
let udp_socket = UdpSocket::bind(\"0.0.0.0:0\").unwrap();
25-
udp_socket.connect(\"8.8.8.8:80\").unwrap();
26-
let socket_addr = udp_socket.local_addr().unwrap();
27-
let ip_addr = socket_addr.ip();
28-
ip_addr.to_string()
29-
30-
Example 2:
31-
INPUT: /implement fn hello_world() -> String
32-
OUTPUT:
33-
\"Hello World\".to_string()
34-
35-
Example 3:
36-
INPUT: /implement fn hello_world(name: String) -> String
37-
OUTPUT:
38-
format!(\"Hello {}!\", name)
20+
Example:
21+
INPUT SIGNATURE: 'fn calculate_pi_with_n_iterations(n: u64) -> f64'
22+
EXPECTED OUTPUT (Function Body Only):
23+
let mut pi = 0.0;
24+
let mut sign = 1.0;
25+
for i in 0..n {
26+
pi += sign / (2 * i + 1) as f64;
27+
sign = -sign;
28+
}
29+
4.0 * pi
3930
".to_string();
4031

4132
let user_message = extra_context
@@ -59,5 +50,7 @@ pub fn generate_body_function_from_head(
5950
head, body_str
6051
);
6152

53+
println!("Implementation: {}", implementation);
54+
6255
Ok(implementation)
6356
}

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ pub fn implement(_item: TokenStream) -> TokenStream {
2323

2424
#[proc_macro_attribute]
2525
pub fn auto_implement(args: TokenStream, input: TokenStream) -> TokenStream {
26+
// env::vars()
27+
// .map(|v| format!("{}: {}\n", v.0, v.1))
28+
// .for_each(|v| print!("{}", v));
29+
30+
// let this_file = file!();
31+
// column!();
32+
33+
// println!("This file: {}", this_file);
34+
2635
let ast: ItemFn = syn::parse(input).expect("Failed to parse input as a function");
2736

2837
let context = args.to_string();

0 commit comments

Comments
 (0)