diff --git a/build/out.asm b/build/out.asm index ace0deb..6ce2918 100644 --- a/build/out.asm +++ b/build/out.asm @@ -5,10 +5,281 @@ section .text main: push rbp mov rbp, rsp - mov qword [rbp-8], 3 - mov qword [rbp-16], 8 + sub rsp, 24 + call _10 + add rsp, 24 + mov rax, qword [rbp-24] + mov qword [rbp-8], rax + mov rax, rbp + add rax, -8 + mov qword [rbp-32], rax + mov qword [rbp-40], 12 + mov rax, qword [rbp-40] + mov qword [rbp-64], rax + mov rax, qword [rbp-32] + mov qword [rbp-72], rax + sub rsp, 72 + call _11 + add rsp, 72 + mov rax, rbp + add rax, -8 + mov qword [rbp-64], rax + mov rax, qword [rbp-64] + mov qword [rbp-72], rax + sub rsp, 72 + call _12 + add rsp, 72 + mov qword [rbp-72], 8 + mov rax, qword [rbp-72] + leave + ret + + +_10: + push rbp + mov rbp, rsp + mov qword [rbp+16], 0 + leave + ret + + +_11: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-17], rax + mov rdx, qword [rbp-17] + mov rax, qword [rdx+0] + mov qword [rbp-9], rax + mov rax, qword [rbp-9] + cmp rax, 0 + jz __36_12 + mov byte [rbp-1], 0 + jmp __36_13 + __36_12: + mov byte [rbp-1], 1 + __36_13: + cmp byte [rbp-1], 0 + jz _11_15 + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-25], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-57], rax + sub rsp, 57 + call _13 + add rsp, 57 + mov rax, qword [rbp-49] + mov qword [rbp-33], rax + mov rdx, qword [rbp-25] + mov rax, qword [rbp-33] + mov qword [rdx+0], rax + jmp _11_14 + _11_15: + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-25], rax + mov rdx, qword [rbp-25] + mov rax, qword [rdx+0] + mov qword [rbp-33], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-65], rax + mov rax, qword [rbp-33] + mov qword [rbp-73], rax + sub rsp, 73 + call _14 + add rsp, 73 + _11_16: + _11_14: + +leave +ret + +_12: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-18], rax + mov rdx, qword [rbp-18] + mov rax, qword [rdx+0] + mov qword [rbp-10], rax + mov rax, qword [rbp-10] + cmp rax, 0 + jz __36_0 + mov byte [rbp-2], 0 + jmp __36_1 + __36_0: + mov byte [rbp-2], 1 + __36_1: + mov al, byte [rbp-2] + cmp al, 0 + jz __23_2 + mov byte [rbp-1], 0 + jmp __23_3 + __23_2: + mov byte [rbp-1], 1 + __23_3: + cmp byte [rbp-1], 0 + jz _12_4 + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-26], rax + mov rdx, qword [rbp-26] + mov rax, qword [rdx+0] + mov qword [rbp-34], rax + mov rax, qword [rbp-34] + mov qword [rbp-42], rax + sub rsp, 42 + call _15 + add rsp, 42 + _12_5: + _12_4: + +leave +ret + +_13: + push rbp + mov rbp, rsp + mov rax, qword [rbp+16] + mov qword [rbp-16], rax + mov qword [rbp-8], 0 + mov rdi, 16 + sub rsp, 16 + extern malloc + call malloc + add rsp, 16 + mov qword [rbp-24], rax + mov rdx, qword [rbp-24] mov rax, qword [rbp-16] + mov qword [rdx+0], rax + mov rax, qword [rbp-8] + mov qword [rdx+8], rax + mov rax, qword [rbp-24] + mov qword [rbp+24], rax leave ret +_14: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-17], rax + mov rdx, qword [rbp-17] + mov rax, qword [rdx+0] + mov qword [rbp-9], rax + mov rax, qword [rbp-9] + cmp rax, 0 + jz __36_17 + mov byte [rbp-1], 0 + jmp __36_18 + __36_17: + mov byte [rbp-1], 1 + __36_18: + cmp byte [rbp-1], 0 + jz _14_20 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-25], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-57], rax + sub rsp, 57 + call _13 + add rsp, 57 + mov rax, qword [rbp-49] + mov qword [rbp-33], rax + mov rdx, qword [rbp-25] + mov rax, qword [rbp-33] + mov qword [rdx+0], rax + jmp _14_19 + _14_20: + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-25], rax + mov rdx, qword [rbp-25] + mov rax, qword [rdx+0] + mov qword [rbp-33], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-65], rax + mov rax, qword [rbp-33] + mov qword [rbp-73], rax + sub rsp, 73 + call _14 + add rsp, 73 + _14_21: + _14_19: + +leave +ret + +_15: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-16], rax + mov rdx, qword [rbp-16] + mov rax, qword [rdx+0] + mov qword [rbp-8], rax + mov rdi, __8_fstr + mov rsi, [rbp-8] + mov al, 0 + sub rsp, 16 + extern printf + call printf + add rsp, 16 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-34], rax + mov rdx, qword [rbp-34] + mov rax, qword [rdx+0] + mov qword [rbp-26], rax + mov rax, qword [rbp-26] + cmp rax, 0 + jz __36_6 + mov byte [rbp-18], 0 + jmp __36_7 + __36_6: + mov byte [rbp-18], 1 + __36_7: + mov al, byte [rbp-18] + cmp al, 0 + jz __23_8 + mov byte [rbp-17], 0 + jmp __23_9 + __23_8: + mov byte [rbp-17], 1 + __23_9: + cmp byte [rbp-17], 0 + jz _15_10 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-42], rax + mov rdx, qword [rbp-42] + mov rax, qword [rdx+0] + mov qword [rbp-50], rax + mov rax, qword [rbp-50] + mov qword [rbp-58], rax + sub rsp, 58 + call _15 + add rsp, 58 + _15_11: + _15_10: + +leave +ret + +section .data_readonly + __8_fstr db `Integer: %ld\n`,0 \ No newline at end of file diff --git a/build/out.o b/build/out.o index 3add5ba..279baff 100644 Binary files a/build/out.o and b/build/out.o differ diff --git a/build/out.out b/build/out.out index 329ee4f..9b29bb0 100755 Binary files a/build/out.out and b/build/out.out differ diff --git a/main.why b/main.why index c328c4b..44fb610 100644 --- a/main.why +++ b/main.why @@ -1,17 +1,10 @@ use linked_list; -struct Test { - a: int -} - -impl Test { - fn static() { - - } -} fn main() -> int { - let x: Test = Test { a: 3 }; + let x: LL = LL::new(); + x.add(12); + x.print(); return 8; } diff --git a/src/root/errors/parser_errors.rs b/src/root/errors/parser_errors.rs index 3e76768..b631090 100644 --- a/src/root/errors/parser_errors.rs +++ b/src/root/errors/parser_errors.rs @@ -13,6 +13,8 @@ pub enum ParseError { Expected(String), #[error("Failed parsing {0}")] NomErrorKind(String), + #[error("Failed to open file [{0}]")] + FailedToOpenFile(String) } pub fn create_custom_error(e: String, l: Span) -> nom::Err { diff --git a/src/root/parser/handle_errors.rs b/src/root/parser/handle_errors.rs index 8c54119..6dbde53 100644 --- a/src/root/parser/handle_errors.rs +++ b/src/root/parser/handle_errors.rs @@ -6,9 +6,9 @@ use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_toplevel::TopLevelTokens; -pub fn handle_error<'a>( - res: ParseResult<'a, Span<'a>, Vec>, -) -> Result<(Span<'a>, Vec), WErr> { +pub fn handle_error( + res: ParseResult, +) -> Result<(A, B), WErr> { match res { Ok(v) => Ok(v), Err(e) => match &e { diff --git a/src/root/parser/location.rs b/src/root/parser/location.rs index dedadd9..7add73e 100644 --- a/src/root/parser/location.rs +++ b/src/root/parser/location.rs @@ -8,6 +8,28 @@ use color_print::cformat; use nom::InputTake; use crate::root::parser::parse::Span; +pub enum ToLocation<'a> { + Location(Location), + Span(Span<'a>) +} + +impl<'a> ToLocation<'a> { + pub fn from_location(location: Location) -> ToLocation<'a> { + ToLocation::Location(location) + } + + pub fn from_span(span: Span<'a>) -> ToLocation<'a> { + ToLocation::Span(span) + } + + pub fn to_location(self) -> Location { + match self { + ToLocation::Location(location) => location, + ToLocation::Span(s) => Location::from_span(&s) + } + } +} + #[derive(Debug, Clone, Hash)] struct InnerLocation { /// Path of file diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index 438ea89..48b7203 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -5,11 +5,13 @@ use std::rc::Rc; use nom::IResult; use nom_locate::LocatedSpan; use nom_supreme::error::GenericErrorTree; - +use crate::root::errors::parser_errors::ParseError; use crate::root::errors::WErr; use crate::root::parser::handle_errors::handle_error; +use crate::root::parser::location::{Location, ToLocation}; use crate::root::parser::parse_toplevel; use crate::root::parser::parse_toplevel::TopLevelTokens; +use crate::root::parser::use_parser::parse_uses; pub type Span<'a> = LocatedSpan<&'a str, &'a Rc>; @@ -17,14 +19,30 @@ pub type ParseResult<'a, I = Span<'a>, O = Span<'a>, E = ErrorTree<'a>> = IResul pub type ErrorTree<'a> = GenericErrorTree, &'static str, &'static str, String>; pub fn parse(path: PathBuf) -> Result, WErr> { - let text = fs::read_to_string(&path).unwrap(); - let path = Rc::new(path); - let base = Span::new_extra(&text, &path); - - let res = parse_toplevel::parse_toplevel(base); - let (remaining, output) = handle_error(res)?; - - debug_assert!(remaining.is_empty()); + let mut path_queue = vec![(path, Location::builtin())]; + let mut output = Vec::new(); + + while let Some((path, location)) = path_queue.pop() { + print!("\n - {}", path.display()); + let Ok(text) = fs::read_to_string(path.as_path()) else { + return WErr::ne( + ParseError::FailedToOpenFile(format!("{}", path.display())), + location + ); + }; + + let path = Rc::new(path); + let base = Span::new_extra(&text, &path); + + let (after_use, found_paths) = handle_error(parse_uses(base))?; + path_queue.extend(found_paths); + + let res = parse_toplevel::parse_toplevel(after_use); + let (remaining, new_output) = handle_error(res)?; + debug_assert!(remaining.is_empty()); + output.extend(new_output); + } + println!(); Ok(output) } diff --git a/src/root/parser/use_parser.rs b/src/root/parser/use_parser.rs index e69de29..e28a964 100644 --- a/src/root/parser/use_parser.rs +++ b/src/root/parser/use_parser.rs @@ -0,0 +1,41 @@ +use std::fmt::format; +use std::path::PathBuf; +use nom::bytes::complete::{tag, take_till}; +use nom::character::complete::anychar; +use nom::complete::take; +use crate::root::errors::parser_errors::create_custom_error; +use crate::root::parser::location::{Location, ToLocation}; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; +use crate::root::parser::parse_util::discard_ignored; + +pub fn parse_uses(s: Span) -> ParseResult> { + let mut s = s; + let mut found_paths = Vec::new(); + loop { + let (ns, _) = discard_ignored(s)?; + let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns) else { + return Ok((ns, found_paths)); + }; + + let (ns, _) = discard_ignored(ns)?; + let Ok((pre_s, path)) = take_till::<_, _, ErrorTree>(|c| c == ';' || c =='\n' || c=='\r')(ns) else { + return Err(create_custom_error( + "Did not find ending ';' when parsing path".to_string(), + ns, + )); + }; + + let (ns, next) = anychar::<_, ErrorTree>(pre_s).unwrap(); + if next != ';' { + return Err(create_custom_error( + "Use path cannot be broken by newline".to_string(), + pre_s, + )); + } + + let path_buf = PathBuf::from(format!("{}.why", path)); + found_paths.push((path_buf, Location::from_span(&path))); + + s = ns; + } +} \ No newline at end of file diff --git a/todo.md b/todo.md index 959d0d6..8c7865f 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,3 @@ - Add array support -- Add support for multiple files +- Namespacing