Skip to content

Commit

Permalink
Merge branch 'feature/dictforeach'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 15, 2023
2 parents 044ff23 + 604cd74 commit 1195cb8
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 60 deletions.
66 changes: 31 additions & 35 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "fift"
description = "Rust implementation of the Fift esoteric language"
repository = "https://github.com/broxus/fift"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
rust-version = "1.70"
include = ["src/**/*.rs", "src/**/*.fif", "LICENSE-*", "README.md"]
Expand Down Expand Up @@ -30,6 +30,6 @@ sha2 = "0.10"
thiserror = "1.0"
unicode-segmentation = "1.0"

everscale-types = "0.1.0-rc.2"
everscale-types = "0.1.0-rc.3"

fift-proc = { path = "./proc", version = "=0.1.7" }
4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "fift-cli"
description = "A CLI for the Fift esoteric language interpreter"
repository = "https://github.com/broxus/fift"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
rust-version = "1.70"
include = ["src/**/*.rs", "src/**/*.fif", "LICENSE", "README.md"]
Expand All @@ -18,4 +18,4 @@ argh = "0.1"
console = "0.15"
rustyline = { version = "11.0", default-features = false }

fift = { path = "..", version = "0.1.8" }
fift = { path = "..", version = "=0.1.9" }
103 changes: 100 additions & 3 deletions src/core/cont.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,105 @@ impl ContImpl for WhileCont {
}
}

pub struct LoopCont<T> {
inner: T,
state: LoopContState,
func: Cont,
after: Option<Cont>,
}

impl<T> LoopCont<T> {
pub fn new(inner: T, func: Cont, after: Option<Cont>) -> Self {
Self {
inner,
state: LoopContState::Init,
func,
after,
}
}
}

impl<T: LoopContImpl + 'static> ContImpl for LoopCont<T> {
fn run(mut self: Rc<Self>, ctx: &mut Context) -> Result<Option<Cont>> {
let Some(this) = Rc::get_mut(&mut self) else {
return Ok(Some(Rc::new(Self {
inner: self.inner.clone(),
state: self.state,
func: self.func.clone(),
after: self.after.clone(),
})));
};

ctx.insert_before_next(&mut this.after);
Ok(loop {
match this.state {
LoopContState::Init => {
if !this.inner.init(ctx)? {
break this.after.take();
}
this.state = LoopContState::PreExec;
}
LoopContState::PreExec => {
if !this.inner.pre_exec(ctx)? {
this.state = LoopContState::Finalize;
continue;
}
this.state = LoopContState::PostExec;
let res = self.func.clone();
ctx.next = Some(self);
break Some(res);
}
LoopContState::PostExec => {
if !this.inner.post_exec(ctx)? {
this.state = LoopContState::Finalize;
continue;
}
this.state = LoopContState::PreExec;
break Some(self);
}
LoopContState::Finalize => {
break if this.inner.finalize(ctx)? {
this.after.take()
} else {
None
};
}
}
})
}

fn fmt_name(&self, _: &Dictionary, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<generic loop continuation state {:?}>", self.state)
}
}

pub trait LoopContImpl: Clone {
fn init(&mut self, ctx: &mut Context) -> Result<bool> {
_ = ctx;
Ok(true)
}
fn pre_exec(&mut self, ctx: &mut Context) -> Result<bool> {
_ = ctx;
Ok(true)
}
fn post_exec(&mut self, ctx: &mut Context) -> Result<bool> {
_ = ctx;
Ok(true)
}
fn finalize(&mut self, ctx: &mut Context) -> Result<bool> {
_ = ctx;
Ok(true)
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum LoopContState {
Init,
PreExec,
PostExec,
Finalize,
}

pub struct IntLitCont(BigInt);

impl<T> From<T> for IntLitCont
Expand Down Expand Up @@ -584,9 +683,7 @@ impl ContImpl for MultiLitCont {
fn fmt_name(&self, d: &Dictionary, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut first = true;
for item in &self.0 {
if first {
first = false;
} else {
if !std::mem::take(&mut first) {
f.write_str(" ")?;
}
write_lit_cont_name(item.as_ref(), d, f)?;
Expand Down
Loading

0 comments on commit 1195cb8

Please sign in to comment.