Skip to content

Commit d745a44

Browse files
committed
refactor: bug fix
1 parent 1f327e9 commit d745a44

File tree

6 files changed

+49
-28
lines changed

6 files changed

+49
-28
lines changed

crates/cli/src/commands.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ use resolver::{
1414
};
1515
use static_analyzer::context::AnalysisContext;
1616
use std::{
17-
cell::RefCell, env, io::{self, Write}, path::Path, process::exit, rc::Rc, sync::{Arc, Mutex}
17+
cell::RefCell,
18+
env,
19+
io::{self, Write},
20+
path::Path,
21+
process::exit,
22+
rc::Rc,
23+
sync::{Arc, Mutex},
1824
};
1925
use typed_ast::{ModuleID, TypedProgramTree};
2026
use utils::fs::{ensure_output_dir, get_directory_of_file};
@@ -64,8 +70,13 @@ fn get_program_trees(
6470
{
6571
for (_, _, module_id, typed_program_tree) in program_trees.iter() {
6672
{
67-
let mut analyzer =
68-
AnalysisContext::new(&resolver, *module_id, typed_program_tree.clone(), entry_points.clone(), options.disable_warnings);
73+
let mut analyzer = AnalysisContext::new(
74+
&resolver,
75+
*module_id,
76+
typed_program_tree.clone(),
77+
entry_points.clone(),
78+
options.disable_warnings,
79+
);
6980
analyzer.analyze();
7081
DiagReporter::display(&analyzer.reporter);
7182
if analyzer.reporter.has_errors() {
@@ -92,7 +103,10 @@ fn prepare_compilation(
92103
Vec<(String, ModuleFilePath, ModuleID, Rc<RefCell<TypedProgramTree>>)>,
93104
Rc<Resolver>,
94105
) {
95-
let opts = options.to_compiler_options();
106+
let mut opts = options.to_compiler_options();
107+
if file_path.is_some() {
108+
opts.disable_modulefs_cache = true;
109+
}
96110
let file_path = get_entry_source_code_path(options.base_path.clone(), file_path);
97111
let final_build_dir = get_final_build_directory_path(options.base_path.clone(), opts.build_dir.clone());
98112
ensure_build_dir_subs(options.base_path.clone(), final_build_dir.clone());
@@ -103,9 +117,7 @@ fn prepare_compilation(
103117
}
104118

105119
pub(crate) fn command_run(mut options: CompilerOptions, file_path: Option<String>) {
106-
let (mut opts, file_path, final_build_dir, program_trees, resolver_rc) =
107-
prepare_compilation(&mut options, file_path);
108-
opts.disable_modulefs_cache = true;
120+
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
109121

110122
let mut temp = env::temp_dir();
111123
temp.push("path");
@@ -131,7 +143,6 @@ pub(crate) fn command_run(mut options: CompilerOptions, file_path: Option<String
131143
}
132144

133145
pub(crate) fn command_emit_llvm(mut options: CompilerOptions, file_path: Option<String>, output_path: Option<String>) {
134-
options.disable_modulefs_cache = true;
135146
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
136147

137148
let output_path = output_path.unwrap_or_else(|| {
@@ -154,7 +165,6 @@ pub(crate) fn command_emit_bytecode(
154165
file_path: Option<String>,
155166
output_path: Option<String>,
156167
) {
157-
options.disable_modulefs_cache = true;
158168
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
159169

160170
let output_path = output_path.unwrap_or_else(|| {
@@ -177,7 +187,6 @@ pub(crate) fn command_emit_bytecode(
177187
}
178188

179189
pub(crate) fn command_emit_asm(mut options: CompilerOptions, file_path: Option<String>, output_path: Option<String>) {
180-
options.disable_modulefs_cache = true;
181190
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
182191

183192
let output_path = output_path.unwrap_or_else(|| {
@@ -217,7 +226,6 @@ pub(crate) fn command_build(mut options: CompilerOptions, file_path: Option<Stri
217226
}
218227

219228
pub(crate) fn command_object(mut options: CompilerOptions, file_path: Option<String>, output_path: Option<String>) {
220-
options.disable_modulefs_cache = true;
221229
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
222230

223231
let output_path = output_path.unwrap_or_else(|| {
@@ -236,7 +244,6 @@ pub(crate) fn command_object(mut options: CompilerOptions, file_path: Option<Str
236244
}
237245

238246
pub(crate) fn command_dylib(mut options: CompilerOptions, file_path: Option<String>, output_path: Option<String>) {
239-
options.disable_modulefs_cache = true;
240247
let (opts, file_path, final_build_dir, program_trees, resolver_rc) = prepare_compilation(&mut options, file_path);
241248

242249
let output_path = output_path.unwrap_or_else(|| {

crates/codegen/src/context/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use resolver::{Resolver, moduleloader::ModuleFilePath};
99
use std::{
1010
cell::RefCell,
1111
path::{Path, PathBuf},
12-
process::{exit, Command},
12+
process::{Command, exit},
1313
rc::Rc,
1414
sync::{Arc, Mutex},
1515
};
@@ -103,6 +103,13 @@ impl CodeGenContext {
103103
linker_command.arg("-rdynamic");
104104
}
105105

106+
linker_command.arg("-funroll-loops");
107+
linker_command.arg("-flto");
108+
109+
if let Some(opt_level) = self.opts.opt_level {
110+
linker_command.arg(format!("-O{}", opt_level));
111+
}
112+
106113
linker_command.arg("-o").arg(output_path);
107114
linker_command.args(object_files_str_list);
108115

crates/static_analyzer/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'a> AnalysisContext<'a> {
178178
TypedStatement::BlockStatement(typed_block_statement) => {
179179
self.analyze_block_statement(typed_block_statement)
180180
}
181-
TypedStatement::If(typed_if) => self.analyze_if_stmt(typed_if),
181+
TypedStatement::If(typed_if) => self.analyze_if_stmt(block_stmt.scope_id, typed_if, None),
182182
TypedStatement::Return(typed_return) => self.analyze_return(block_stmt.scope_id, typed_return),
183183
TypedStatement::Break(typed_break) => {
184184
self.analyze_break(typed_break);
@@ -277,9 +277,11 @@ impl<'a> AnalysisContext<'a> {
277277
}
278278
}
279279

280-
fn analyze_if_stmt(&mut self, typed_if: &mut TypedIf) -> FlowState {
280+
fn analyze_if_stmt(&mut self, scope_id: ScopeID, typed_if: &mut TypedIf, expected_type:Option<ConcreteType>) -> FlowState {
281281
let consequent_state = self.analyze_block_statement(&mut typed_if.consequent);
282282

283+
self.analyze_typed_expr_type(Some(scope_id), &mut typed_if.condition, expected_type.clone());
284+
283285
let alternate_state = {
284286
if let Some(block_stmt) = &mut typed_if.alternate {
285287
self.analyze_block_statement(&mut *block_stmt)
@@ -289,7 +291,7 @@ impl<'a> AnalysisContext<'a> {
289291
};
290292

291293
typed_if.branches.iter_mut().for_each(|branch| {
292-
self.analyze_if_stmt(branch);
294+
self.analyze_if_stmt(scope_id, branch, expected_type.clone());
293295
});
294296

295297
self.merge_flow_state(consequent_state, alternate_state)

examples/fibonacci.cyr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public func fibonacci(n: int) int {
1515

1616
func main() {
1717
for (#i = 0; i < 30; i++) {
18-
printf("%d\n", foo(i));
18+
printf("%d\n", fibonacci(i));
1919
}
2020
}

examples/main.cyr

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
extern func printf(format: const char*, ...);
2-
extern func malloc(size: uint) void*;
3-
extern func free(pointer: void*);
2+
extern func exit(status: int);
43

5-
func main() {
6-
#int_ptr: int* = malloc(sizeof(int));
7-
8-
*int_ptr = 10;
9-
10-
printf("value: %d\n", *int_ptr);
4+
inline func fibonacci(n: int) int {
5+
if (n == 0) {
6+
return 0;
7+
}
8+
else if (n == 1) {
9+
return 1;
10+
}
11+
else {
12+
return fibonacci(n - 1) + fibonacci(n - 2);
13+
}
14+
}
1115

12-
free(int_ptr);
16+
func main() {
17+
for (#i = 0; i < 40; i = i + 1) {
18+
printf("%d\n", fibonacci(i));
19+
}
1320
}

scripts/benchmark.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/bin/bash
2-
31
# benchmark.sh
42
# A simple script to measure the execution time of a given program.
53

0 commit comments

Comments
 (0)