Skip to content

Commit

Permalink
tried to add another internal iterator, but ferris doesn't like this …
Browse files Browse the repository at this point in the history
…one.
  • Loading branch information
Lokathor committed Dec 30, 2024
1 parent 310a896 commit 3504261
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 25 deletions.
49 changes: 48 additions & 1 deletion src/ast/data/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Function {
// where:
struct ExpressionsMut<'r>(&'r mut Function);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_guts! {}
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
Expand All @@ -33,6 +33,53 @@ impl Function {
}
}

#[cfg(target_os = "none")]
pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r Function);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut Function);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn generate_code(&self) -> Vec<Asm> {
let mut out = Vec::new();
let mut loop_stack = Vec::new();
Expand Down
54 changes: 53 additions & 1 deletion src/ast/data/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl IfElse {
// where:
struct ExpressionsMut<'r>(&'r mut IfElse);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_guts! {}
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
Expand All @@ -56,6 +56,58 @@ impl IfElse {
}
}

pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r IfElse);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.if_body.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
}
for stmt in self.0.else_body.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut IfElse);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.if_body.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
}
for stmt in self.0.else_body.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn write_code(
&self, loop_stack: &mut Vec<(Option<StrID>, StrID)>,
out: &mut impl Extend<Asm>,
Expand Down
48 changes: 47 additions & 1 deletion src/ast/data/loop_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Loop {
// where:
struct ExpressionsMut<'r>(&'r mut Loop);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_guts! {}
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
Expand All @@ -64,6 +64,52 @@ impl Loop {
}
}

pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r Loop);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter() {
stmt.calls_ref().try_for_each(&mut f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut Loop);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
for stmt in self.0.statements.iter_mut() {
stmt.calls_mut().try_for_each_mut(&mut *f)?;
}
ControlFlow::Continue(())
}
}
}

pub fn write_code(
&self, loop_stack: &mut Vec<(Option<StrID>, StrID)>,
out: &mut impl Extend<Asm>,
Expand Down
64 changes: 63 additions & 1 deletion src/ast/data/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Statement {
// where:
struct ExpressionsMut<'r>(&'r mut Statement);
impl<'r> InternalIterator for ExpressionsMut<'r> {
internal_iterator_guts! {}
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for ExpressionsMut<'r> {
Expand Down Expand Up @@ -49,6 +49,68 @@ impl Statement {
}
}

pub fn calls_ref(
&self,
) -> impl '_ + InternalIterator<Item = &'_ FileSpanned<Call>> {
return CallsRef(self);
// where:
struct CallsRef<'r>(&'r Statement);
impl<'r> InternalIterator for CallsRef<'r> {
type Item = &'r FileSpanned<Call>;

fn try_for_each<R, F>(self, mut f: F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
match self.0 {
Statement::Call(c) => f(c)?,
Statement::IfElse(if_else) => if_else.calls_ref().try_for_each(f)?,
Statement::Loop(loop_) => loop_.calls_ref().try_for_each(f)?,
Statement::Break(_)
| Statement::Continue(_)
| Statement::Expression(_)
| Statement::Return
| Statement::StatementError => {}
}
ControlFlow::Continue(())
}
}
}

pub fn calls_mut(
&mut self,
) -> impl '_ + InternalIteratorMut<ItemMut = &'_ mut FileSpanned<Call>> {
return CallsMut(self);
// where:
struct CallsMut<'r>(&'r mut Statement);
impl<'r> InternalIterator for CallsMut<'r> {
internal_iterator_mut_guts! {}
}

impl<'r> InternalIteratorMut for CallsMut<'r> {
type ItemMut = &'r mut FileSpanned<Call>;

fn try_for_each_mut<R, F>(self, f: &mut F) -> ControlFlow<R>
where
F: FnMut(Self::Item) -> ControlFlow<R>,
{
match self.0 {
Statement::Call(c) => f(c)?,
Statement::IfElse(if_else) => {
if_else.calls_mut().try_for_each_mut(f)?
}
Statement::Loop(loop_) => loop_.calls_mut().try_for_each_mut(f)?,
Statement::Break(_)
| Statement::Continue(_)
| Statement::Expression(_)
| Statement::Return
| Statement::StatementError => {}
}
ControlFlow::Continue(())
}
}
}

pub fn write_code(
&self, loop_stack: &mut Vec<(Option<StrID>, StrID)>,
out: &mut impl Extend<Asm>,
Expand Down
15 changes: 15 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ impl Ast {
}
}

#[cfg(target_os = "none")]
pub fn check_all_calls_valid(&mut self) {
for function in self.functions.values() {
function.calls_ref().for_each(|c| {
if !self.functions.contains_key(&c.target) {
if self.statics.contains_key(&c.target) {
self.err_bucket.push(YagError::CalledAStatic(c.clone()))
} else {
self.err_bucket.push(YagError::CalledAMissingFunction(c.clone()))
}
}
});
}
}

pub fn resolve_size_of_static(&mut self) {
for func in self.functions.values_mut() {
func.expressions_mut().for_each(|xpr| {
Expand Down
45 changes: 25 additions & 20 deletions src/bin/yagbas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub fn do_ast(args: AstArgs) {
every_item.extend(items);
}
let mut ast = Ast::from_items(every_item);
//ast.check_all_calls_valid();
ast.run_const_eval();
ast.run_static_eval();
ast.resolve_size_of_static();
Expand All @@ -228,36 +229,40 @@ pub fn do_codegen(args: CodegenArgs) {
every_item.extend(items);
}
let mut ast = Ast::from_items(every_item);
//ast.check_all_calls_valid();
ast.run_const_eval();
ast.run_static_eval();
ast.resolve_size_of_static();
ast.resolve_numeric_literals();
ast.resolve_identifiers();
ast.resolve_ref();
ast.simplify_constant_values();
let assembly_items = ast.generate_assembly_items();
let mut buffer = String::new();
for (i, (name, assembly)) in assembly_items.iter().enumerate() {
if i > 0 {
println!();
}
for asm in assembly.iter() {
use core::fmt::Write;
write!(buffer, "{asm}").ok();
if buffer.ends_with(":") {
// a label
if buffer.starts_with('.') {
// local label
println!(" {buffer}")
err_bucket.append(&mut ast.err_bucket);
if err_bucket.is_empty() {
let assembly_items = ast.generate_assembly_items();
let mut buffer = String::new();
for (i, (name, assembly)) in assembly_items.iter().enumerate() {
if i > 0 {
println!();
}
for asm in assembly.iter() {
use core::fmt::Write;
write!(buffer, "{asm}").ok();
if buffer.ends_with(":") {
// a label
if buffer.starts_with('.') {
// local label
println!(" {buffer}")
} else {
println!("{buffer}");
}
} else {
println!("{buffer}");
println!(" {buffer}");
}
} else {
println!(" {buffer}");
buffer.clear();
}
buffer.clear();
}
} else {
report_all_the_errors(src_files, err_bucket, args.message_size);
}
err_bucket.append(&mut ast.err_bucket);
report_all_the_errors(src_files, err_bucket, args.message_size);
}
Loading

0 comments on commit 3504261

Please sign in to comment.