Skip to content

Commit

Permalink
Markers
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jul 25, 2024
1 parent 312c99c commit 33fd1f0
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 29 deletions.

Large diffs are not rendered by default.

31 changes: 21 additions & 10 deletions .idea/workspace.xml

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

11 changes: 8 additions & 3 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ section .text
main:
push rbp
mov rbp, rsp
mov qword [rbp-8], 3
mov qword [rbp-8], 13
; Test Start
mov qword [rbp-32], 3
mov qword [rbp-24], 5
mov qword [rbp-16], 4
; Test End
mov rax, qword [rbp-8]
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
mov qword [rbp-40], rax
mov rax, qword [rbp-40]
leave
ret

Expand Down
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
25 changes: 18 additions & 7 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
struct StructA {
a: StructB
a: int,
b: StructB
}

struct StructB {
a: StructC
}

struct StructC {
a: StructB
a: int,
b: int
}

fn main() -> int {
let y: int = 3;
let y: int = 13;

@ Test Start

let x: StructA = StructA {
a: 3,
b: StructB {
a: 5,
b: 4
}
};

@ Test End

return y;
}
3 changes: 3 additions & 0 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_varia
LineTokens::NoOp(et) => {
contents.other(&compile_evaluable_reference(fid, et, local_variables, global_table, global_tracker)?.0);
}
LineTokens::Marker(value) => {
contents.line(&format!(";{}", value.value()));
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/root/parser/parse_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use nom::bytes::complete::take_until;
use nom::InputTake;
use crate::root::parser::parse::{ErrorTree, ParseResult, Span};
use crate::root::parser::parse_blocks::take_until_discard_smart;
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens, parse_evaluable};
use crate::root::parser::parse_name::SimpleNameToken;

Expand All @@ -11,8 +12,8 @@ pub fn parse_arguments<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleN

loop {
// TODO: Account for brackets
let (ns, section) = if let Ok((ns, section)) = take_until::<_, _, ErrorTree>(",")(s) {
(ns.take_split(1).0, section)
let (ns, section) = if let Ok((ns, section)) = take_until_discard_smart(s, ",") {
(ns, section)
}
else {
last = true;
Expand Down
16 changes: 12 additions & 4 deletions src/root/parser/parse_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ pub fn parse_terminator<'a, 'b, 'c>(s: Span<'a>, terminator: &'b Terminator, all
Err(nom::Err::Error(ErrorTree::from_char(s, terminator.closing)))
}

pub fn take_until_or_end_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a> {
pub fn take_until_or_end_discard_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a> {
let original = s.clone();
let mut s = s;
let mut found = false;
while !s.is_empty() {
if let Ok((ns, _)) = tag::<&str, LocatedSpan<&str, &Rc<PathBuf>>, ErrorTree>(until)(s) {
found = true;
s = ns;
break;
}
Expand All @@ -127,11 +129,15 @@ pub fn take_until_or_end_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a>
}

let offset = original.offset(&s);
let (end, mut inner) = original.take_split(offset);
if found {
inner = inner.take(inner.len() - until.len());
}

Ok(original.take_split(offset))
Ok((end, inner))
}

pub fn take_until_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a> {
pub fn take_until_discard_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a> {
let original = s.clone();
let mut s = s;
loop {
Expand All @@ -157,6 +163,8 @@ pub fn take_until_smart<'a>(s: Span<'a>, until: &str) -> ParseResult<'a> {
}

let offset = original.offset(&s);
let (end, inner) = original.take_split(offset);
let inner = inner.take(inner.len() - until.len());

Ok(original.take_split(offset))
Ok((end, inner))
}
1 change: 1 addition & 0 deletions src/root/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod parse_operator;
pub mod parse_return;
pub mod parse_while;
mod parse_struct_init;
mod parse_marker;

#[derive(Debug, Getters, Dissolve)]
pub struct FunctionToken {
Expand Down
6 changes: 6 additions & 0 deletions src/root/parser/parse_function/parse_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::root::parser::parse_function::parse_return::{test_parse_return, Retur
use crate::root::parser::parse_function::parse_while::{test_parse_while, WhileToken};
use nom::branch::alt;
use nom::Parser;
#[cfg(debug_assertions)]
use crate::root::parser::parse_function::parse_marker::{MarkerToken, test_parse_marker};
use crate::root::parser::parse_name::SimpleNameToken;
use crate::root::parser::parse_util::discard_ignored;

Expand All @@ -20,6 +22,8 @@ pub enum LineTokens {
Return(ReturnToken),
Break(BreakToken),
NoOp(EvaluableToken),
#[cfg(debug_assertions)]
Marker(MarkerToken)
}

/// fn(line span, Option<class name>)
Expand Down Expand Up @@ -51,6 +55,8 @@ pub fn parse_line<'a, 'b>(s: Span<'a>, containing_class: Option<&'b SimpleNameTo
test_parse_initialisation,
test_parse_while,
test_parse_if,
#[cfg(debug_assertions)]
test_parse_marker
// test_parse_assignment,
))
.parse(s)
Expand Down
40 changes: 40 additions & 0 deletions src/root/parser/parse_function/parse_marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use derive_getters::Getters;
use nom::bytes::complete::take_till;
use nom::character::complete::char;
use nom::sequence::Tuple;
use nom_supreme::tag::complete::tag;
use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};
use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens};
use crate::root::parser::parse_name::SimpleNameToken;
use crate::root::parser::parse_util::{discard_ignored, require_ignored};

#[derive(Debug, Getters)]
pub struct MarkerToken {
value: String,
}

#[cfg(debug_assertions)]
pub fn test_parse_marker<'a, 'b>(s: Span<'a>) -> ParseResult<Span, LineTestFn<'a, 'b>> {
match (char('@'), require_ignored).parse(s) {
Ok(_) => Ok((s, |x, _| {
parse_marker(x).map(|(s, x)| (s, LineTokens::Marker(x)))
})),
Err(e) => Err(e),
}
}

#[cfg(debug_assertions)]
pub fn parse_marker(s: Span) -> ParseResult<Span, MarkerToken> {
let (s, _) = char('@')(s)?;
let (s, value) = take_till(|c| c == '\n' || c == '\r')(s)?;

let (s, _) = discard_ignored(s)?;

Ok((
s,
MarkerToken {
value: value.fragment().to_string()
}
))
}
6 changes: 3 additions & 3 deletions src/root/parser/parse_function/parse_struct_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::bytes::complete::{tag, take_till};
use nom::bytes::streaming::take_until;
use nom::character::streaming::char;
use crate::root::parser::parse::{Location, ParseResult, Span};
use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set};
use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set, take_until_or_end_discard_smart};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens, FullNameToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name};
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken};
Expand Down Expand Up @@ -40,8 +40,8 @@ pub fn parse_struct_init<'a, 'b>(s: Span<'a>, containing_class: Option<&'b Simpl
let (ns, _) = char(':')(ns)?;
let (ns, _) = discard_ignored(ns)?;

let (ns, to_eval) = take_till(|c| c == ',')(ns)?;
let ns = if ns.is_empty() { ns } else { char(',')(ns)?.0 };
let (ns, to_eval) = take_until_or_end_discard_smart(ns, ",")?;
println!("{:?} - {:?}", ns.fragment(), to_eval.fragment());
let (_, eval) = parse_evaluable(to_eval, containing_class, false)?;
contents.push((name, eval));
s = ns;
Expand Down

0 comments on commit 33fd1f0

Please sign in to comment.