Skip to content

Commit

Permalink
Added null to all user types
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jul 28, 2024
1 parent 111296d commit b0f9ba0
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 89 deletions.

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

59 changes: 25 additions & 34 deletions .idea/workspace.xml

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

76 changes: 36 additions & 40 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,48 @@ section .text
main:
push rbp
mov rbp, rsp
; Heap Alloc
mov qword [rbp-24], 3
mov qword [rbp-16], 4
mov rdi, 16
sub rsp, 24
extern malloc
call malloc
sub rsp, 24
mov qword [rbp-32], rax
mov rdx, qword [rbp-32]
mov rax, qword [rbp-24]
mov qword [rdx+0], rax
mov rax, qword [rbp-16]
mov qword [rdx+8], rax
mov rax, qword [rbp-32]
mov qword [rbp-8], rax
; Unheap
mov rdx, qword [rbp-8]
mov rax, qword [rdx+0]
mov qword [rbp-48], rax
mov rax, qword [rdx+8]
mov qword [rbp-40], rax
; Get member
sub rsp, 25
call _4
add rsp, 25
mov rax, qword [rbp-25]
mov qword [rbp-9], rax
mov al, byte [rbp-17]
mov byte [rbp-1], al
mov rax, rbp
add rax, -48
mov qword [rbp-56], rax
; Deref member
mov rdx, qword [rbp-56]
mov rax, qword [rdx+0]
mov qword [rbp-64], rax
; Print
mov rax, qword [rbp-64]
mov qword [rbp-72], rax
mov rdi, __8_fstr
mov rsi, [rbp-72]
add rax, -9
mov qword [rbp-33], rax
mov rdx, qword [rbp-33]
mov al, byte [rdx+0]
mov byte [rbp-34], al
mov al, byte [rbp-34]
cmp al, 0
jz __23_0
mov rdi, __23_t_fstr
jmp __23_1
__23_0:
mov rdi, __23_f_fstr
__23_1:
mov rsi, 0
mov al, 0
sub rsp, 72
sub rsp, 34
extern printf
call printf
add rsp, 72
; Return
mov qword [rbp-80], 7
mov rax, qword [rbp-80]
add rsp, 34
mov qword [rbp-42], 0
mov rax, qword [rbp-42]
leave
ret


_4:
push rbp
mov rbp, rsp
mov byte [rbp+16], 1
mov qword [rbp+17], 0
leave
ret


section .data_readonly
__8_fstr db `Integer: %ld\n`,0
__23_f_fstr db `Boolean: False`,0
__23_t_fstr db `Boolean: True`,0
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
22 changes: 21 additions & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ struct LL {
first: &Node
}

impl LL {
fn new() -> LL {
return LL {
has_node: false,
first: Node::null()
};
}

fn add(&self, val: int) {
let x: &bool = self.has_node;
if (*x) {
self.first.add(val);
} else {
self.first = Node::new(val);
};
}
}

struct Node {
val: int,
Expand All @@ -11,5 +28,8 @@ struct Node {
}

fn main() -> int {
let r: LL = LL { has_node: false, first: Node::null() };
let r: LL = LL::new();
printb(*(r.has_node));

return 0;
}
4 changes: 3 additions & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;
#[cfg(debug_assertions)]
pub const DEBUG_ON_ERROR: bool = false;
pub const DEBUG_ON_ERROR: bool = true;

// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
Expand Down Expand Up @@ -77,6 +77,8 @@ pub fn main_args(args: Args) -> Result<(), WErr> {
let parsed = parse(PathBuf::from(&args.input))?;
);

println!("{:#?}", parsed);

print!("Resolving Names... ");
time!(
let (global_table, unprocessed_functions) = resolve(parsed)?;
Expand Down
3 changes: 2 additions & 1 deletion src/root/compiler/assembly/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod utils;
pub mod heap;
pub mod heap;
pub mod null;
41 changes: 41 additions & 0 deletions src/root/compiler/assembly/null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator};
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::parser::parse_parameters::SelfType;
use crate::root::shared::common::{FunctionID, LocalAddress, TypeID};

pub struct NullFunction {
id: FunctionID,
parent_type: TypeID
}

impl BuiltinInlineFunction for NullFunction {
fn id(&self) -> FunctionID {
self.id
}

fn name(&self) -> &'static str {
"null"
}

fn signature(&self) -> FunctionSignature {
FunctionSignature::new(SelfType::None, vec![], Some(self.parent_type.with_indirection(1)))
}

fn inline(&self) -> InlineFunctionGenerator {
|_, return_into: Option<LocalAddress>, _, _| -> String {
let return_into = return_into.unwrap();
format!(" mov qword {return_into}, 0\n")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(self.parent_type)
}
}

pub fn null_function(t: TypeID, f: FunctionID) -> NullFunction {
NullFunction {
id: f,
parent_type: t,
}
}
13 changes: 8 additions & 5 deletions src/root/compiler/evaluation/type_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ pub fn compile_evaluable_type_only(
name.location().clone(),
)
}
NameResult::Type(_) => {
return WErr::ne(
EvalErrs::CannotEvalStandaloneType(name.name().clone()),
name.location().clone(),
)
NameResult::Type(t) => {
t.immediate()
// println!("> {}", name.name());
// std::process::exit(123);
// return WErr::ne(
// EvalErrs::CannotEvalStandaloneType(name.name().clone()),
// name.location().clone(),
// )
}
NameResult::Variable(address) => address.type_ref().clone(),
}
Expand Down
17 changes: 11 additions & 6 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::root::POINTER_SIZE;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use crate::root::compiler::assembly::null::null_function;

#[derive(Debug)]
enum NameTreeEntry {
Expand Down Expand Up @@ -197,8 +198,12 @@ impl GlobalDefinitionTable {
}

/// Adds a type definition for a previously given `TypeID`
pub fn add_type(&mut self, given_id: TypeID, definition: Box<dyn Type>) {
pub fn add_user_type(&mut self, given_id: TypeID, definition: Box<dyn Type>) {
self.type_definitions.insert(given_id, definition);
let fid = self.id_counter;
self.id_counter += 1;
let null_function = null_function(given_id, FunctionID(fid));
self.register_inline_function(&null_function);
}

/// Takes a name and resolves it to a type (or error)
Expand Down Expand Up @@ -321,11 +326,6 @@ impl GlobalDefinitionTable {
Ok(address)
}

/// Returns the `FunctionSignature` of a function
pub fn get_function_signature(&self, function_id: FunctionID) -> &FunctionSignature {
self.function_signatures.get(&function_id).as_ref().unwrap()
}

/// Returns whether a main function has been defined
pub fn has_main(&self) -> bool {
self.function_signatures.contains_key(&FunctionID(0))
Expand Down Expand Up @@ -442,6 +442,11 @@ impl GlobalDefinitionTable {
}
}

/// Returns the `FunctionSignature` of a function
pub fn get_function_signature(&self, function_id: FunctionID) -> &FunctionSignature {
self.function_signatures.get(&function_id).as_ref().unwrap()
}

/// Returns a function specified by the `FunctionID`
pub fn get_function(
&self,
Expand Down
2 changes: 1 addition & 1 deletion src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn resolve_names(
}

for (id, user_type) in final_types {
global_table.add_type(id, Box::new(user_type));
global_table.add_user_type(id, Box::new(user_type));
}

// (final_types, type_names, unprocessed_functions)
Expand Down

0 comments on commit b0f9ba0

Please sign in to comment.