1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use clap::{Parser, Subcommand, ValueEnum};
use miette::{IntoDiagnostic, Result};
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "fastc")]
#[command(about = "FastC transpiler - compile FastC to C11", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
/// Project type for scaffolding
#[derive(Debug, Clone, Copy, ValueEnum)]
enum CliProjectType {
/// A binary application
Binary,
/// A library
Library,
/// An FFI wrapper library
FfiWrapper,
}
impl From<CliProjectType> for fastc::ProjectType {
fn from(t: CliProjectType) -> Self {
match t {
CliProjectType::Binary => fastc::ProjectType::Binary,
CliProjectType::Library => fastc::ProjectType::Library,
CliProjectType::FfiWrapper => fastc::ProjectType::FfiWrapper,
}
}
}
/// Build system template
#[derive(Debug, Clone, Copy, ValueEnum)]
enum CliBuildTemplate {
/// GNU Make
Make,
/// CMake
Cmake,
/// Meson
Meson,
}
impl From<CliBuildTemplate> for fastc::BuildTemplate {
fn from(t: CliBuildTemplate) -> Self {
match t {
CliBuildTemplate::Make => fastc::BuildTemplate::Make,
CliBuildTemplate::Cmake => fastc::BuildTemplate::CMake,
CliBuildTemplate::Meson => fastc::BuildTemplate::Meson,
}
}
}
#[derive(Subcommand)]
enum Commands {
/// Compile a FastC source file to C
Compile {
/// Input FastC source file
input: PathBuf,
/// Output C file (use - for stdout)
#[arg(short, long, default_value = "-")]
output: String,
/// Also emit a C header file
#[arg(long)]
emit_header: bool,
},
/// Type-check a FastC source file without emitting C
Check {
/// Input FastC source file
input: PathBuf,
},
/// Format a FastC source file
Fmt {
/// Input FastC source file
input: PathBuf,
/// Output file (use - for stdout, omit to format in place)
#[arg(short, long)]
output: Option<String>,
/// Check if the file is already formatted (exit with error if not)
#[arg(long)]
check: bool,
},
/// Create a new FastC project
New {
/// Project name
name: String,
/// Project type
#[arg(long, short = 't', value_enum, default_value = "binary")]
r#type: CliProjectType,
/// Build system template
#[arg(long, value_enum, default_value = "make")]
template: CliBuildTemplate,
},
/// Initialize a FastC project in the current directory
Init {
/// Directory to initialize (defaults to current directory)
#[arg(default_value = ".")]
path: PathBuf,
/// Project type
#[arg(long, short = 't', value_enum, default_value = "binary")]
r#type: CliProjectType,
/// Build system template
#[arg(long, value_enum, default_value = "make")]
template: CliBuildTemplate,
},
/// Build the project using fastc.toml configuration
Build {
/// Build in release mode (optimizations enabled)
#[arg(long)]
release: bool,
/// Output directory for generated C files
#[arg(short, long, default_value = "build")]
output: PathBuf,
/// Compile the generated C code with a C compiler
#[arg(long)]
cc: bool,
/// C compiler to use (default: cc)
#[arg(long, default_value = "cc")]
compiler: String,
/// Additional flags to pass to the C compiler
#[arg(long)]
cflags: Option<String>,
},
/// Build, compile, and run the project
Run {
/// Build in release mode (optimizations enabled)
#[arg(long)]
release: bool,
/// C compiler to use (default: cc)
#[arg(long, default_value = "cc")]
compiler: String,
/// Additional flags to pass to the C compiler
#[arg(long)]
cflags: Option<String>,
/// Arguments to pass to the program
#[arg(last = true)]
args: Vec<String>,
},
/// Fetch project dependencies without building
Fetch,
}
fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Compile {
input,
output,
emit_header,
} => {
let source = std::fs::read_to_string(&input).into_diagnostic()?;
let filename = input.display().to_string();
let (c_code, header) = fastc::compile_with_options(&source, &filename, emit_header)?;
if output == "-" {
println!("{}", c_code);
if let Some(h) = header {
eprintln!("\n--- Header ---\n{}", h);
}
} else {
std::fs::write(&output, &c_code).into_diagnostic()?;
if let Some(h) = header {
let header_path = output.replace(".c", ".h");
std::fs::write(&header_path, &h).into_diagnostic()?;
}
}
}
Commands::Check { input } => {
let source = std::fs::read_to_string(&input).into_diagnostic()?;
let filename = input.display().to_string();
fastc::check(&source, &filename)?;
eprintln!("No errors found.");
}
Commands::Fmt {
input,
output,
check,
} => {
let source = std::fs::read_to_string(&input).into_diagnostic()?;
let filename = input.display().to_string();
if check {
// Check mode: verify already formatted
if fastc::check_formatted(&source, &filename)? {
eprintln!("File is already formatted.");
} else {
eprintln!("File is not formatted. Run `fastc fmt {}` to format.", input.display());
std::process::exit(1);
}
} else {
// Format mode
let formatted = fastc::format(&source, &filename)?;
match output.as_deref() {
Some("-") => {
print!("{}", formatted);
}
Some(path) => {
std::fs::write(path, &formatted).into_diagnostic()?;
}
None => {
// In-place formatting
std::fs::write(&input, &formatted).into_diagnostic()?;
eprintln!("Formatted {}.", input.display());
}
}
}
}
Commands::New {
name,
r#type,
template,
} => {
let current_dir = std::env::current_dir().into_diagnostic()?;
fastc::create_project(&name, ¤t_dir, r#type.into(), template.into())?;
}
Commands::Init {
path,
r#type,
template,
} => {
let path = if path.is_absolute() {
path
} else {
std::env::current_dir().into_diagnostic()?.join(path)
};
fastc::init_project(&path, r#type.into(), template.into())?;
}
Commands::Build {
release,
output,
cc,
compiler,
cflags,
} => {
let current_dir = std::env::current_dir().into_diagnostic()?;
let mut ctx = fastc::BuildContext::new(¤t_dir)
.map_err(|e| miette::miette!("{}", e))?;
// Fetch dependencies first
ctx.fetch_dependencies()
.map_err(|e| miette::miette!("{}", e))?;
// Compile the project to C
let c_file = ctx
.compile(&output, release)
.map_err(|e| miette::miette!("{}", e))?;
// Optionally compile with C compiler
if cc {
let cflags_vec: Vec<&str> = cflags
.as_deref()
.map(|s| s.split_whitespace().collect())
.unwrap_or_default();
ctx.cc_compile(&c_file, &compiler, &cflags_vec, release)
.map_err(|e| miette::miette!("{}", e))?;
}
}
Commands::Run {
release,
compiler,
cflags,
args,
} => {
let current_dir = std::env::current_dir().into_diagnostic()?;
let mut ctx = fastc::BuildContext::new(¤t_dir)
.map_err(|e| miette::miette!("{}", e))?;
// Fetch dependencies first
ctx.fetch_dependencies()
.map_err(|e| miette::miette!("{}", e))?;
// Compile the project to C
let output = PathBuf::from("build");
let c_file = ctx
.compile(&output, release)
.map_err(|e| miette::miette!("{}", e))?;
// Compile with C compiler
let cflags_vec: Vec<&str> = cflags
.as_deref()
.map(|s| s.split_whitespace().collect())
.unwrap_or_default();
let executable = ctx
.cc_compile(&c_file, &compiler, &cflags_vec, release)
.map_err(|e| miette::miette!("{}", e))?;
// Run the program
ctx.run(&executable, &args)
.map_err(|e| miette::miette!("{}", e))?;
}
Commands::Fetch => {
let current_dir = std::env::current_dir().into_diagnostic()?;
let mut ctx = fastc::BuildContext::new(¤t_dir)
.map_err(|e| miette::miette!("{}", e))?;
ctx.fetch_dependencies()
.map_err(|e| miette::miette!("{}", e))?;
eprintln!("Dependencies fetched successfully.");
}
}
Ok(())
}