Skip to content

Commit

Permalink
Printnl
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Aug 6, 2024
1 parent 69475ab commit b21f378
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 94 deletions.
393 changes: 355 additions & 38 deletions build/out.asm

Large diffs are not rendered by default.

Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
54 changes: 0 additions & 54 deletions linked_list.why

This file was deleted.

16 changes: 14 additions & 2 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use linked_list;
use std/linked_list;


fn main() -> int {
let x: LL = LL::new();
x.add(12);
x.push(12);
x.push(7);
x.push(8);
x.print();
printnl();

printi(x.get(1));
printnl();

printi(x.pop());
printnl();

x.print();

return 8;
}

3 changes: 3 additions & 0 deletions src/root/builtin/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod exit;
mod printnl;

use crate::root::builtin::functions::exit::ExitFunction;
use crate::root::builtin::functions::printnl::PrintNL;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;

pub fn register_functions(global_table: &mut GlobalDefinitionTable) {
global_table.register_inline_function(&ExitFunction);
global_table.register_inline_function(&PrintNL);
}
56 changes: 56 additions & 0 deletions src/root/builtin/functions/printnl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::root::builtin::types::int::IntType;
use crate::root::builtin::{f_id, 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};
use unique_type_id::UniqueTypeId;

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct PrintNL;

impl PrintNL {
pub const fn id() -> FunctionID {
f_id(PrintNL::unique_type_id().0)
}
}

impl BuiltinInlineFunction for PrintNL {
fn id(&self) -> FunctionID {
Self::id()
}

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
SelfType::None,
&[],
None,
)
}

fn inline(&self) -> InlineFunctionGenerator {
|_, _, gt, sz| -> String {
let id = format!("{}_fstr", Self::id().string_id());
let data = format!("{id} db `\\n`,0");
gt.add_readonly_data(&id, &data);

format!(
" mov rdi, {id}
mov al, 0
sub rsp, {sz}
extern printf
call printf
add rsp, {sz}
"
)
}
}

fn parent_type(&self) -> Option<TypeID> {
None
}
}
88 changes: 88 additions & 0 deletions std/linked_list.why
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
struct LL {
first: &Node
}

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

fn push(&self, val: int) {
if (Node::is_null(*self.first)) {
self.first = Node::new(val);
} else {
self.first.push(val);
};
}

fn get(&self, idx: int) -> int {
return self.first.get(idx);
}

fn pop(&self) -> int {
if (Node::is_null(*(*self.first).next)) {
let val: int = *(*self.first).val;
Node::free(*self.first);
self.first = Node::null();
return val;
}

return self.first.pop();
}

fn print(&self) {
if (!Node::is_null(*self.first)) {
self.first.print();
};
}
}

struct Node {
val: int,
next: &Node
}

impl Node {
fn new(val: int) -> &Node {
return new Node {
val: val,
next: Node::null()
};
}

fn push(&self, val: int) {
if (Node::is_null(*self.next)) {
self.next = Node::new(val);
} else {
self.next.push(val);
};
}

fn get(&self, idx: int) -> int {
if (idx == 0) {
return *self.val;
}

return self.next.get(idx - 1);
}

fn pop(&self) -> int {
if (Node::is_null(*(*self.next).next)) {
let val: int = *(*self.next).val;
Node::free(*self.next);
self.next = Node::null();
return val;
}

return self.next.pop();
}

fn print(&self) {
printi(*self.val);
if (!Node::is_null(*self.next)) {
self.next.print();
};
}
}
6 changes: 6 additions & 0 deletions types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ BoolEq=32
BoolNE=33
IntType=34
IsNullFunction=35
P=36
Pr=37
Pri=38
Prin=39
Print=40
PrintNL=41

0 comments on commit b21f378

Please sign in to comment.