Skip to content

Commit

Permalink
Began adding assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Lucas committed Jun 5, 2024
1 parent 341f14f commit fdf1740
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 89 deletions.
4 changes: 2 additions & 2 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ main:
mov rbp, rsp
sub rsp, 16
mov qword [rbp-8], 12
mov rax, qword [rbp-8]
mov qword [rbp-16], 12
mov rax, qword [rbp-16]
leave
ret
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
3 changes: 2 additions & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ struct A {
}

fn main() -> int {
return 12;
let a: int = 13;
return a;
}
8 changes: 6 additions & 2 deletions src/root/builtin/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ use crate::root::shared::types::Type;
#[UniqueTypeIdType = "u16"]
pub struct IntType {}

impl Type for IntType {
fn id(&self) -> TypeID {
impl IntType {
pub const fn id() -> TypeID {
TypeID(-(IntType::unique_type_id().0 as isize) - 1)
}
}

impl Type for IntType {
fn id(&self) -> TypeID { Self::id() }

fn size(&self) -> ByteSize {
ByteSize(8)
Expand Down
23 changes: 10 additions & 13 deletions src/root/compiler/compile_evaluable.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use std::any::Any;
use std::collections::HashSet;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens};
use crate::root::shared::common::{FunctionID, TypeRef};
use crate::root::shared::common::AddressedTypeRef;

pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option<AddressedTypeRef>, local_variables: &mut LocalVariableTable, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet<FunctionID>) -> (String, Option<AddressedTypeRef>) {
pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option<AddressedTypeRef>, local_variables: &mut LocalVariableTable, global_table: &mut GlobalDefinitionTable, function_calls: &mut HashSet<FunctionID>) -> (String, Option<AddressedTypeRef>) {
let et = et.token();

match et {
EvaluableTokens::Name(_, _) => todo!(),
EvaluableTokens::Literal(literal) => {
let (address, t, tid) = if let Some(target) = target {
let (address, tid) = target.dissolve();
if tid.indirection().has_indirection() {
let (address, t) = if let Some(target) = target {
if target.type_ref().indirection().has_indirection() {
todo!()
}
let t = global_table.type_definitions().get(tid.type_id()).unwrap();
(address, t, tid)
let t = global_table.get_type(*target.type_ref().type_id());
(target, t)
}
else {
let tid = literal.literal().default_type();
if tid.indirection().has_indirection() {
todo!()
}
let t = global_table.type_definitions().get(tid.type_id()).unwrap();
let address = local_variables.add_new_unnamed(t.size());
(address, t, tid)
let address = global_table.add_local_variable_unnamed_base(tid.clone(), local_variables);
let t = global_table.get_type(*tid.type_id());
(address, t)
};

(t.instantiate_from_literal(&address, literal), Some(AddressedTypeRef::new(address, tid)))
(t.instantiate_from_literal(address.local_address(), literal), Some(address))
}
EvaluableTokens::InfixOperator(_, _, _) => todo!(),
EvaluableTokens::PrefixOperator(_, _) => todo!(),
Expand Down
20 changes: 11 additions & 9 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
let mut param_address = LocalAddress(8);

for (param_name, param_type) in parameters {
let type_ref = global_table.resolve_to_type_ref(&param_type)?;
let t = global_table.resolve_to_type_ref(&param_type)?;
global_table.add_local_variable_named(param_name.name().clone(), &param_type, &mut local_variables)?;

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

param_address += LocalAddress(size.0 as isize);
param_address += LocalAddress(global_table.get_size(&t).0 as isize);
}

let return_variable = return_type.and_then(
Expand All @@ -42,7 +40,7 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
);

let mut function_calls = HashSet::new();
let (full_contents, local_variables) = recursively_compile_lines(fid, &lines, &return_variable, local_variables, global_table, &mut function_calls);
let (full_contents, local_variables) = recursively_compile_lines(fid, &lines, &return_variable, local_variables, global_table, &mut function_calls)?;

let stack_size = local_variables.stack_size();

Expand All @@ -66,13 +64,17 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
Ok((final_contents, function_calls))
}

fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_variable: &Option<AddressedTypeRef>, local_variables: Box<LocalVariableTable>, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet<FunctionID>) -> (String, Box<LocalVariableTable>) {
fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_variable: &Option<AddressedTypeRef>, local_variables: Box<LocalVariableTable>, global_table: &mut GlobalDefinitionTable, function_calls: &mut HashSet<FunctionID>) -> Result<(String, Box<LocalVariableTable>), WError> {
let mut local_variables = local_variables.enter_block();
let mut contents = String::new();

for line in lines {
match line {
LineTokens::Initialisation(_) => todo!(),
LineTokens::Initialisation(it) => {
let (name, type_name, value) = (it.name(), it.type_name(), it.value());
let address = global_table.add_local_variable_named(name.name().clone(), type_name, &mut local_variables)?;
compile_evaluable(fid, value, Some(address), &mut local_variables, global_table, function_calls);
},
LineTokens::Assignment(_) => todo!(),
LineTokens::If(_) => todo!(),
LineTokens::While(_) => todo!(),
Expand All @@ -96,5 +98,5 @@ fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_varia
}
}

(contents, local_variables.leave_block())
Ok((contents, local_variables.leave_block()))
}
14 changes: 1 addition & 13 deletions src/root/compiler/local_variable_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID, TypeRef};
use crate::root::shared::types::Type;

/// Function-local table of defined variables. Only used within function processing
Expand Down Expand Up @@ -48,16 +48,4 @@ impl LocalVariableTable {
None
}
}

pub fn get_ref_and_type<'a>(&self, name: &str, type_defs: &'a HashMap<TypeID, Box<dyn Type>>) -> Option<(AddressedTypeRef, &'a dyn Type)> {
if let Some(r) = self.table.get(name) {
if let Some(t) = type_defs.get(r.type_ref().type_id()) {
return Some((r.clone(), t.as_ref()));
}
panic!("Type in VariableTable but no corresponding definition found!");
}
else {
None
}
}
}
45 changes: 43 additions & 2 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_function::parse_evaluable::{FullNameToken, FullNameTokens, FullNameWithIndirectionToken};
use crate::root::parser::parse_name::SimpleNameToken;
use crate::root::parser::parse_struct::StructToken;
use crate::root::shared::common::{AddressedTypeRef, FunctionID, TypeID, TypeRef};
use crate::root::POINTER_SIZE;
use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, TypeID, TypeRef};

#[derive(Debug)]
enum NameTreeEntry {
Expand Down Expand Up @@ -54,7 +55,6 @@ impl TopLevelNameTree {
}
}

#[derive(Getters)]
pub struct GlobalDefinitionTable {
id_counter: isize,
type_definitions: HashMap<TypeID, Box<dyn Type>>,
Expand Down Expand Up @@ -190,4 +190,45 @@ impl GlobalDefinitionTable {

Err(WError::n(NRErrors::TypeNotFound(name.name().clone()), full_name.location().clone()))
}

pub fn get_size(&mut self, t: &TypeRef) -> ByteSize {
if t.indirection().has_indirection() {
POINTER_SIZE
}
else {
self.type_definitions.get(t.type_id()).unwrap().size()
}
}

pub fn add_local_variable_unnamed_base(&mut self, t: TypeRef, local_variable_table: &mut LocalVariableTable) -> AddressedTypeRef {
let size = self.get_size(&t);
let address = local_variable_table.add_new_unnamed(size);
AddressedTypeRef::new(address, t)
}

pub fn add_local_variable_unnamed(&mut self, t: &FullNameWithIndirectionToken, local_variable_table: &mut LocalVariableTable) -> Result<AddressedTypeRef, WError> {
let t = self.resolve_to_type_ref(t)?;
Ok(self.add_local_variable_unnamed_base(t, local_variable_table))
}

pub fn add_local_variable_named(&mut self, name: String, t: &FullNameWithIndirectionToken, local_variable_table: &mut LocalVariableTable) -> Result<AddressedTypeRef, WError> {
let t = self.resolve_to_type_ref(t)?;
let size = self.get_size(&t);
let address = local_variable_table.add_new_unnamed(size);
let address = AddressedTypeRef::new(address, t);
local_variable_table.add_existing(name, address.clone());
Ok(address)
}

pub fn get_function_signature(&self, function_id: FunctionID) -> &FunctionSignature {
self.function_signatures.get(&function_id).as_ref().unwrap()
}

pub fn has_main(&self) -> bool {
self.function_signatures.contains_key(&FunctionID(0))
}

pub fn get_type(&self, type_id: TypeID) -> &Box<dyn Type> {
self.type_definitions.get(&type_id).as_ref().unwrap()
}
}
2 changes: 1 addition & 1 deletion src/root/name_resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn resolve(ast: Vec<TopLevelTokens>) -> Result<(GlobalDefinitionTable, HashM
register_builtin(&mut global_table);
let unprocessed_functions = resolve_names(ast, &mut global_table)?;

if !global_table.function_signatures().contains_key(&FunctionID(0)) {
if !global_table.has_main() {
return Err(WError::locationless(NRErrors::NoMain))
}

Expand Down
38 changes: 0 additions & 38 deletions src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,6 @@ use crate::root::shared::common::{LocalAddress, TypeRef};
use crate::root::shared::common::{ByteSize, FunctionID, TypeID};
use crate::root::shared::types::Type;

// #[derive(Hash)]
// pub struct TypeName {
// name: String,
// location: Location,
// }
//
// impl TypeName {
// pub fn from_struct_token(st: &StructToken) -> TypeName {
// TypeName {
// name: st.name().clone(),
// location: st.location().clone(),
// }
// }
//
// pub fn from_impl_token(it: &ImplToken) -> TypeName {
// TypeName {
// name: it.name().clone(),
// location: it.location().clone(),
// }
// }

// pub fn from_name_token(name: UnresolvedNameToken) -> TypeName {
// let name: (Location, Rc<PathBuf>, Option<String>, usize, String, Vec<(NameConnectors, String)>, Option<Vec<EvaluableToken>>) = name.dissolve();
// TypeName {
// name: name.4,
// location: name.0,
// }
// }
// }

// impl PartialEq for TypeName {
// fn eq(&self, other: &Self) -> bool {
// self.name == other.name
// }
// }
//
// impl Eq for TypeName {}


/// A whython-code-defined type
#[derive(Getters)]
Expand Down
7 changes: 1 addition & 6 deletions src/root/name_resolver/resolve_type_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ pub fn resolve_type_sizes(
size += resolve_type_sizes(unsized_type, final_types, unsized_types, global_table, path);
}
else {
if let Some(builtin_type) = global_table.type_definitions().get(&attribute_type.type_id()) {
size += builtin_type.size();
}
else {
todo!()
}
size += global_table.get_type(*attribute_type.type_id()).size();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/root/parser/parse_function/parse_initialisation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use derive_getters::Getters;
use crate::root::parser::parse::{Location, ParseResult, Span};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name};
use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens};
Expand All @@ -7,7 +8,7 @@ use nom_supreme::tag::complete::tag;
use crate::root::parser::parse_name::{SimpleNameToken, parse_simple_name};
use crate::root::parser::parse_util::{discard_ignored, require_ignored};

#[derive(Debug)]
#[derive(Debug, Getters)]
pub struct InitialisationToken {
location: Location,
name: SimpleNameToken,
Expand Down
2 changes: 1 addition & 1 deletion src/root/parser/parse_function/parse_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl LiteralTokens {
todo!()
}
LiteralTokens::Int(_) => {
TypeRef::new(IntType{}.id(), Indirection(0))
TypeRef::new(IntType::id(), Indirection(0))
}
}
}
Expand Down

0 comments on commit fdf1740

Please sign in to comment.