Skip to content

Commit

Permalink
Improved name evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 5, 2024
1 parent 0c481eb commit 2faf4af
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 225 deletions.
30 changes: 12 additions & 18 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ struct A {
b: int
}

fn main() !-> int {
fn main() -> int {
return 12;
}
2 changes: 1 addition & 1 deletion src/root/builtin/int.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use unique_type_id::UniqueTypeId;
use crate::root::compiler::assembly::utils::get_qword_stack_pointer;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;

#[derive(UniqueTypeId)]
Expand Down
10 changes: 5 additions & 5 deletions src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
pub mod int;

use crate::root::builtin::int::IntType;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, ImplNode};
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable};
use crate::root::shared::types::Type;

pub fn register_builtin(global_table: &mut GlobalDefinitionTable) {
let types: [(String, Box<dyn Type>, ImplNode); 1] = [
("int".to_string(), Box::new(IntType{}), ImplNode::default())
let types: [(String, Box<dyn Type>); 1] = [
("int".to_string(), Box::new(IntType{}))
];

for (n, t, i) in types {
global_table.register_builtin_type(n, t, i);
for (n, t) in types {
global_table.register_builtin_type(n, t);
}
}
4 changes: 2 additions & 2 deletions src/root/compiler/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::shared::common::FunctionID;

pub fn compile(global_table: GlobalDefinitionTable, unprocessed_functions: HashMap<FunctionID, FunctionToken>) -> Result<String, WError> {
pub fn compile(mut global_table: GlobalDefinitionTable, unprocessed_functions: HashMap<FunctionID, FunctionToken>) -> Result<String, WError> {
let mut unprocessed_functions = unprocessed_functions;
let mut compiled_functions = HashMap::new();
let mut compiled_len = 0usize;
Expand All @@ -18,7 +18,7 @@ pub fn compile(global_table: GlobalDefinitionTable, unprocessed_functions: HashM

let current_function_token = unprocessed_functions.remove(&current_function).unwrap();

let (compiled, called_functions) = compile_function(current_function, current_function_token, &global_table)?;
let (compiled, called_functions) = compile_function(current_function, current_function_token, &mut global_table)?;
compiled_len += compiled.len() + 10;
compiled_functions.insert(current_function, compiled);

Expand Down
7 changes: 5 additions & 2 deletions src/root/compiler/compile_evaluable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option<Ad
let et = et.token();

match et {
EvaluableTokens::Name(_) => todo!(),
EvaluableTokens::Name(_, _) => todo!(),
EvaluableTokens::Literal(literal) => {
let (address, t, tid) = if let Some(target) = target {
let (address, tid) = target.dissolve();
Expand All @@ -32,6 +32,9 @@ pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option<Ad
(t.instantiate_from_literal(&address, literal), Some(AddressedTypeRef::new(address, tid)))
}
EvaluableTokens::InfixOperator(_, _, _) => todo!(),
EvaluableTokens::PrefixOperator(_, _) => todo!()
EvaluableTokens::PrefixOperator(_, _) => todo!(),
EvaluableTokens::DynamicAccess(_, _) => todo!(),
EvaluableTokens::StaticAccess(_, _) => todo!(),
EvaluableTokens::FunctionCall(_, _, _) => todo!()
}
}
30 changes: 6 additions & 24 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,27 @@ use crate::root::parser::parse_function::parse_line::LineTokens;
use crate::root::shared::common::{FunctionID, LocalAddress};
use crate::root::shared::common::AddressedTypeRef;

pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table: &GlobalDefinitionTable) -> Result<(String, HashSet<FunctionID>), WError> {
pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table: &mut GlobalDefinitionTable) -> Result<(String, HashSet<FunctionID>), WError> {
let mut local_variables = Box::new(LocalVariableTable::default());

let (_location, _name, return_type, parameters, lines) = function.dissolve();

let return_type = if fid.is_main() { None } else { return_type };

let return_type = if let Some((t, loc)) = return_type {
Some(match global_table.resolve_global_name_to_id(&t, &loc)? {
NameResultId::Function(_) => todo!(),
NameResultId::Type(type_ref) => {
if type_ref.indirection().has_indirection() {
todo!()
}
type_ref
}
NameResultId::NotFound => todo!()
})
let return_type = if let Some(t) = return_type {
Some(global_table.resolve_to_type_ref(&t)?)
}
else {
None
};

let mut param_address = LocalAddress(8);

for ((param_name, param_name_loc), (param_type, param_type_loc)) in parameters {
let type_ref = match global_table.resolve_global_name_to_id(&param_type, &param_type_loc)? {
NameResultId::Function(_) => todo!(),
NameResultId::Type(type_ref) => {
if type_ref.indirection().has_indirection() {
todo!()
}
type_ref
}
NameResultId::NotFound => todo!()
};
for (param_name, param_type) in parameters {
let type_ref = global_table.resolve_to_type_ref(&param_type)?;

let size = global_table.type_definitions().get(type_ref.type_id()).unwrap().size();
local_variables.add_existing(param_name, AddressedTypeRef::new(param_address, type_ref));
local_variables.add_existing(param_name.name().clone(), AddressedTypeRef::new(param_address, type_ref));

param_address += LocalAddress(size.0 as isize);
}
Expand Down
8 changes: 7 additions & 1 deletion src/root/errors/name_resolver_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ pub enum NRErrors {
#[error("Function reference cannot have indirection here")]
FunctionIndirectionError,
#[error("Identifier ({0}) not found")]
IdentifierNotFound(String)
IdentifierNotFound(String),
#[error("Expected type ({0}), found function of same name")]
FoundFunctionNotType(String),
#[error("Type ({0}) not found")]
TypeNotFound(String),
#[error("Expected type, not method or attribute")]
ExpectedTypeNotMethodOrAttribute
}
Loading

0 comments on commit 2faf4af

Please sign in to comment.