Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Feb 20, 2025
1 parent 098a5af commit 5970720
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 80 deletions.
38 changes: 9 additions & 29 deletions compiler/src/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ffi::CString;
use std::ptr::null_mut;
use std::rc::Rc;
use std::str::CharIndices;

use ast::ast::*;
use ast::token::*;
Expand All @@ -13,7 +12,7 @@ use crate::scope::ScopeRef;
use crate::Compiler;

impl Compiler {
fn access_identifier_values(
pub(crate) fn access_identifier_values(
&mut self,
scope: ScopeRef,
identifier: Identifier,
Expand Down Expand Up @@ -42,16 +41,8 @@ impl Compiler {
Expression::Identifier(identifier) => self.compile_identifier(scope, identifier),
Expression::Prefix(unary_expression) => self.compile_prefix_expression(scope, unary_expression),
Expression::Infix(binary_expression) => self.compile_infix_expression(scope, binary_expression),
Expression::FieldAccessOrMethodCall(mut chains) => {
if let Some(first_method_call) = chains[0].method_call.clone() {
let result: *mut gcc_jit_rvalue =
self.compile_func_call(Rc::clone(&scope), first_method_call.clone());

chains.remove(0);

return self.struct_field_access_or_method_call(Rc::clone(&scope), String::new(), chains, result);
}
null_mut()
Expression::FieldAccessOrMethodCall(chains) => {
self.struct_field_access_or_method_call(Rc::clone(&scope), chains)
}
Expression::UnaryOperator(unary_operator) => self.compile_unary_operator(scope, unary_operator),
Expression::Array(array) => self.compile_array(Rc::clone(&scope), array, null_mut()),
Expand All @@ -76,25 +67,13 @@ impl Compiler {
}
}

// fn compile_field_access_or_method_call_chain(
// &mut self,
// scope: ScopeRef,
// chains: Vec<FieldAccessOrMethodCall>,
// ) -> *mut gcc_jit_rvalue {
// todo!();
// }

fn compile_package_call(&mut self, scope: ScopeRef, package_call: PackageCall) -> *mut gcc_jit_rvalue {
let package_name =
package_path_as_string(package_call.sub_packages[0..package_call.sub_packages.len() - 1].to_vec());

if let Some(metadata) = self.imported_package_table.borrow_mut().get(&package_name.clone()) {
// ANCHOR
// Perform stuff like method_call or field_access
todo!();
} else {
compiler_error!(format!("Package '{}' not defined in this module.", package_name));
}
dbg!(package_call);

todo!();
}

fn compile_cast_as(&mut self, scope: ScopeRef, cast_as: CastAs) -> *mut gcc_jit_rvalue {
Expand Down Expand Up @@ -293,8 +272,9 @@ impl Compiler {
drop(block_func);

let new_rvalue = match expr.clone() {
// TODO
// Expression::FuncCall(func_call) => self.compile_func_call(Rc::clone(&scope), func_call),
Expression::FieldAccessOrMethodCall(chains) => {
return self.struct_field_access_or_method_call(scope, chains)
}
Expression::StructFieldAccess(struct_field_access) => {
self.compile_struct_field_access(Rc::clone(&scope), *struct_field_access.clone())
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct FuncMetadata {
pub(crate) ptr: *mut gcc_jit_function,
pub(crate) return_type: TokenKind,
pub(crate) params: FunctionParams,
pub(crate) import_from_package: Option<String>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -161,6 +162,7 @@ impl Compiler {
ptr: func,
params: declare_function.params,
return_type,
import_from_package: None
},
);
}
Expand Down
21 changes: 6 additions & 15 deletions compiler/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ use crate::funcs::FuncMetadata;
use crate::structs::StructMetadata;
use crate::Compiler;

#[derive(Debug, Clone)]
pub struct ImportedPackageMetadata {
func_table: RefCell<HashMap<String, FuncMetadata>>,
global_vars_table: RefCell<HashMap<String, *mut gcc_jit_lvalue>>,
}

impl Compiler {
pub(crate) fn compile_import(&mut self, import: Import) {
let file_path = self.file_path.clone();
Expand Down Expand Up @@ -75,23 +69,17 @@ impl Compiler {
compiler.compile();
compiler.make_object_file(output_library_path.clone());

self.imported_package_table.borrow_mut().insert(
package_path_as_string(import.sub_packages.clone()),
ImportedPackageMetadata {
func_table: compiler.func_table.borrow_mut().clone().into(),
global_vars_table: compiler.global_vars_table.borrow_mut().clone().into(),
},
);
let package_name = package_path_as_string(import.sub_packages.clone());

self.compiled_object_files.push(output_library_path.clone());

self.import_package(compiler, import);
self.import_package(compiler, package_name, import);

let optname = CString::new(output_library_path).unwrap();
unsafe { gcc_jit_context_add_driver_option(self.context, optname.as_ptr()) };
}

fn import_package(&mut self, compiler: Compiler, import: Import) {
fn import_package(&mut self, compiler: Compiler, package_name: String, import: Import) {
// Import functions
for (key, value) in compiler.func_table.borrow_mut().clone() {
if value.func_type == VisType::Pub && !self.func_table.borrow_mut().contains_key(&key) {
Expand All @@ -112,6 +100,7 @@ impl Compiler {
ptr: func_ptr,
params: value.params,
return_type: value.return_type,
import_from_package: Some(package_name.clone()),
},
);
}
Expand Down Expand Up @@ -141,6 +130,7 @@ impl Compiler {
field_ptrs: struct_field_ptrs.clone(),
methods: Vec::new(),
method_ptrs: Vec::new(),
import_from_package: Some(package_name.clone()),
},
);

Expand All @@ -164,6 +154,7 @@ impl Compiler {
field_ptrs: struct_field_ptrs,
methods: value.methods,
method_ptrs: methods_decl,
import_from_package: Some(package_name.clone()),
},
);
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ast::ast::*;
use control_flow::LoopBlockPair;
use funcs::{FuncMetadata, FuncParamsRecords};
use gccjit_sys::*;
use import::ImportedPackageMetadata;
use options::CompilerOptions;
use scope::{Scope, ScopeRef};
use std::{
Expand Down Expand Up @@ -41,7 +40,6 @@ pub struct Compiler {
file_path: String,
program: Program,
context: *mut gcc_jit_context,
imported_package_table: RefCell<HashMap<String, ImportedPackageMetadata>>,
func_table: RefCell<HashMap<String, FuncMetadata>>,
global_struct_table: RefCell<HashMap<String, StructMetadata>>,
#[allow(dead_code)] // FIXME
Expand Down Expand Up @@ -104,7 +102,6 @@ impl Compiler {
file_path,
compiled_object_files: Vec::new(),
opts: CompilerOptions::default(),
imported_package_table: RefCell::new(HashMap::new()),
}
}

Expand Down Expand Up @@ -135,6 +132,7 @@ impl Compiler {
ptr,
params: func_def.params,
return_type,
import_from_package: None
},
);
}
Expand Down
32 changes: 18 additions & 14 deletions compiler/src/structs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::panic;
use std::ffi::CString;
use std::ptr::null_mut;
use std::rc::Rc;
Expand Down Expand Up @@ -25,6 +26,7 @@ pub struct StructMetadata {
pub(crate) field_ptrs: Vec<*mut gcc_jit_field>,
pub(crate) methods: Vec<StructMethodMetadata>,
pub(crate) method_ptrs: Vec<*mut gcc_jit_function>,
pub(crate) import_from_package: Option<String>,
}

impl Compiler {
Expand Down Expand Up @@ -98,23 +100,28 @@ impl Compiler {
pub(crate) fn struct_field_access_or_method_call(
&mut self,
scope: ScopeRef,
first_chain_func_name: String,
chains: Vec<FieldAccessOrMethodCall>,
rvalue: *mut gcc_jit_rvalue,
mut chains: Vec<FieldAccessOrMethodCall>,
) -> *mut gcc_jit_rvalue {
let (func, block) = {
let guard = self.block_func_ref.lock().unwrap();
(guard.func, guard.block)
};

if let (Some(func), Some(block)) = (func, block) {
let mut result: *mut gcc_jit_rvalue = rvalue;
let mut result: *mut gcc_jit_rvalue = {
if let Some(method_call) = &chains[0].method_call {
self.compile_func_call(Rc::clone(&scope), method_call.clone())
} else if let Some(field_access) = &chains[0].field_access {
self.access_identifier_values(Rc::clone(&scope), field_access.identifier.clone())
.1
} else {
panic!();
}
};
chains.remove(0);

if result == null_mut() {
compiler_error!(format!(
"Func '{}' returns null value, hence chained func call is an undefined behaviour.",
first_chain_func_name
));
compiler_error!("Chained struct_field_access_or_method_call on null value.");
}

for item in chains {
Expand Down Expand Up @@ -277,12 +284,7 @@ impl Compiler {
compiler_error!("Unexpected behaviour in struct field access compilation.");
}

self.struct_field_access_or_method_call(
Rc::clone(&scope),
first_chain_func_name.to_string(),
statement.chains,
result,
)
self.struct_field_access_or_method_call(Rc::clone(&scope), statement.chains)
}

pub(crate) fn compile_struct_init(&mut self, scope: ScopeRef, struct_init: StructInit) -> *mut gcc_jit_rvalue {
Expand Down Expand Up @@ -364,6 +366,7 @@ impl Compiler {
field_ptrs: field_ptrs.clone(),
methods: Vec::new(),
method_ptrs: Vec::new(),
import_from_package: None
},
);

Expand All @@ -377,6 +380,7 @@ impl Compiler {
methods,
field_ptrs,
method_ptrs,
import_from_package: None
};

self.global_struct_table
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ impl Compiler {
if let Some(expr) = variable.expr {
rvalue = match expr {
Expression::Array(array) => self.compile_array(Rc::clone(&scope), array, var_type),
// TODO
// Expression::FuncCall(func_call) => self.compile_func_call(Rc::clone(&scope), func_call),
Expression::FieldAccessOrMethodCall(chains) => {
self.struct_field_access_or_method_call(Rc::clone(&scope), chains)
},
Expression::StructFieldAccess(struct_field_access) => {
self.compile_struct_field_access(Rc::clone(&scope), *struct_field_access.clone())
}
Expand Down
19 changes: 2 additions & 17 deletions examples/main.cyr
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
pub struct Person {
name: string;
age: i32;

pub fn sample() {
#a = 1 + 2;
}
}

fn kirkoskon(): Person {
return Person {
name: "Taha",
age: 17
};
}
import std::io;

pub fn main() {
#a = kirkoskon().name;
// akasasldad(std::io::printf().sample());

}
Binary file removed stdlib/io.o
Binary file not shown.

0 comments on commit 5970720

Please sign in to comment.