mirror of
https://github.com/fluencelabs/aquavm
synced 2025-04-24 23:02:16 +00:00
WIP fold
This commit is contained in:
parent
0f4ca1523b
commit
fc025ffe93
31
air/src/execution_step/instructions/fold_canon_stream_map.rs
Normal file
31
air/src/execution_step/instructions/fold_canon_stream_map.rs
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2023 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use super::ExecutableInstruction;
|
||||
use super::ExecutionCtx;
|
||||
use super::ExecutionResult;
|
||||
use super::TraceHandler;
|
||||
// use crate::execution_step::instructions::fold_stream::stream_execute_helpers::execute_with_stream;
|
||||
// use crate::execution_step::Stream;
|
||||
// use crate::log_instruction;
|
||||
|
||||
use air_parser::ast::FoldCanonStreamMap;
|
||||
|
||||
impl<'i> ExecutableInstruction<'i> for FoldCanonStreamMap<'i> {
|
||||
fn execute(&self, _exec_ctx: &mut ExecutionCtx<'i>, _trace_ctx: &mut TraceHandler) -> ExecutionResult<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ mod canon_stream_map_scalar;
|
||||
mod compare_matchable;
|
||||
mod fail;
|
||||
mod fold;
|
||||
mod fold_canon_stream_map;
|
||||
mod fold_scalar;
|
||||
mod fold_stream;
|
||||
mod fold_stream_map;
|
||||
@ -87,6 +88,7 @@ impl<'i> ExecutableInstruction<'i> for Instruction<'i> {
|
||||
Instruction::Fail(fail) => execute!(self, fail, exec_ctx, trace_ctx),
|
||||
Instruction::FoldScalar(fold) => execute!(self, fold, exec_ctx, trace_ctx),
|
||||
Instruction::FoldStream(fold) => execute!(self, fold, exec_ctx, trace_ctx),
|
||||
Instruction::FoldCanonStreamMap(fold) => execute!(self, fold, exec_ctx, trace_ctx),
|
||||
Instruction::FoldStreamMap(fold) => execute!(self, fold, exec_ctx, trace_ctx),
|
||||
Instruction::Never(never) => execute!(self, never, exec_ctx, trace_ctx),
|
||||
Instruction::New(new) => execute!(self, new, exec_ctx, trace_ctx),
|
||||
|
@ -42,6 +42,7 @@ pub enum Instruction<'i> {
|
||||
FoldScalar(FoldScalar<'i>),
|
||||
FoldStream(FoldStream<'i>),
|
||||
FoldStreamMap(FoldStreamMap<'i>),
|
||||
FoldCanonStreamMap(FoldCanonStreamMap<'i>),
|
||||
Never(Never),
|
||||
New(New<'i>),
|
||||
Next(Next<'i>),
|
||||
@ -164,7 +165,7 @@ pub struct FoldStream<'i> {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// (fold stream_iterable iterator instruction)
|
||||
/// (fold stream_map_iterable iterator instruction)
|
||||
#[derive(Serialize, Debug, PartialEq)]
|
||||
pub struct FoldStreamMap<'i> {
|
||||
pub iterable: StreamMap<'i>,
|
||||
@ -176,6 +177,18 @@ pub struct FoldStreamMap<'i> {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// (fold canon_stream_map_iterable iterator instruction)
|
||||
#[derive(Serialize, Debug, PartialEq)]
|
||||
pub struct FoldCanonStreamMap<'i> {
|
||||
pub iterable: CanonStreamMap<'i>,
|
||||
#[serde(borrow)]
|
||||
pub iterator: Scalar<'i>,
|
||||
pub instruction: Rc<Instruction<'i>>,
|
||||
// option is needed to provide a graceful period of adoption
|
||||
pub last_instruction: Option<Rc<Instruction<'i>>>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// (fold stream_iterable iterator instruction)
|
||||
#[derive(Serialize, Debug, PartialEq, Eq)]
|
||||
pub struct Next<'i> {
|
||||
|
@ -193,6 +193,24 @@ impl<'i> FoldStreamMap<'i> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'i> FoldCanonStreamMap<'i> {
|
||||
pub fn new(
|
||||
iterable: CanonStreamMap<'i>,
|
||||
iterator: Scalar<'i>,
|
||||
instruction: Instruction<'i>,
|
||||
last_instruction: Option<Instruction<'i>>,
|
||||
span: Span,
|
||||
) -> Self {
|
||||
Self {
|
||||
iterable,
|
||||
iterator,
|
||||
instruction: Rc::new(instruction),
|
||||
last_instruction: last_instruction.map(Rc::new),
|
||||
span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'i> Next<'i> {
|
||||
pub fn new(iterator: Scalar<'i>) -> Self {
|
||||
Self { iterator }
|
||||
|
@ -43,6 +43,7 @@ impl fmt::Display for Instruction<'_> {
|
||||
New(new) => write!(f, "{new}"),
|
||||
Null(null) => write!(f, "{null}"),
|
||||
Error => write!(f, "error"),
|
||||
FoldCanonStreamMap(fold) => write!(f, "{fold}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -133,6 +134,12 @@ impl fmt::Display for FoldStreamMap<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FoldCanonStreamMap<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "fold {} {}", self.iterable, self.iterator)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Seq<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "seq")
|
||||
|
@ -112,16 +112,23 @@ Instr: Box<Instruction<'input>> = {
|
||||
},
|
||||
|
||||
<left: @L> "(" fold <stream_map:StreamMap> <iterator:Scalar> <instruction:Instr> <last_instruction:Instr?> ")" <right: @R> => {
|
||||
let iterable = StreamMap::new(stream_map.0, stream_map.1);
|
||||
let iterator = Scalar::new(iterator.0, iterator.1);
|
||||
let span = Span::new(left, right);
|
||||
let iterable = StreamMap::new(stream_map.0, stream_map.1);
|
||||
let fold = FoldStreamMap::new(iterable, iterator, *instruction, last_instruction.map(|v| *v), span);
|
||||
|
||||
validator.meet_fold_stream_map(&fold, span);
|
||||
|
||||
Box::new(Instruction::FoldStreamMap(fold))
|
||||
},
|
||||
|
||||
<left: @L> "(" fold <canon_stream_map:CanonStreamMap> <iterator:Scalar> <instruction:Instr> <last_instruction:Instr?> ")" <right: @R> => {
|
||||
let iterable = CanonStreamMap::new(canon_stream_map.0, canon_stream_map.1);
|
||||
let iterator = Scalar::new(iterator.0, iterator.1);
|
||||
let span = Span::new(left, right);
|
||||
let fold = FoldCanonStreamMap::new(iterable, iterator, *instruction, last_instruction.map(|v| *v), span);
|
||||
validator.meet_canon_fold_stream_map(&fold, span);
|
||||
Box::new(Instruction::FoldCanonStreamMap(fold))
|
||||
},
|
||||
|
||||
<left: @L> "(" next <iterator:Scalar> ")" <right: @R> => {
|
||||
let iterator = Scalar::new(iterator.0, iterator.1);
|
||||
let next = Next::new(iterator);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -133,6 +133,11 @@ impl<'i> VariableValidator<'i> {
|
||||
self.met_iterator_definition(&fold.iterator, span);
|
||||
}
|
||||
|
||||
pub(super) fn meet_canon_fold_stream_map(&mut self, fold: &FoldCanonStreamMap<'i>, span: Span) {
|
||||
self.met_variable_name(fold.iterable.name, span);
|
||||
self.met_iterator_definition(&fold.iterator, span);
|
||||
}
|
||||
|
||||
pub(super) fn met_new(&mut self, new: &New<'i>, span: Span) {
|
||||
self.not_iterators_candidates
|
||||
.push((new.argument.name(), span));
|
||||
|
@ -143,6 +143,9 @@ impl<W: io::Write> Beautifier<W> {
|
||||
ast::Instruction::FoldStreamMap(fold_stream_map) => {
|
||||
self.beautify_fold_stream_map(fold_stream_map, indent)
|
||||
}
|
||||
ast::Instruction::FoldCanonStreamMap(fold_canon_stream_map) => {
|
||||
self.beautify_fold_canon_stream_map(fold_canon_stream_map, indent)
|
||||
}
|
||||
ast::Instruction::Never(never) => self.beautify_simple(never, indent),
|
||||
ast::Instruction::New(new) => self.beautify_new(new, indent),
|
||||
ast::Instruction::Next(next) => self.beautify_simple(next, indent),
|
||||
@ -255,6 +258,22 @@ impl<W: io::Write> Beautifier<W> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn beautify_fold_canon_stream_map(
|
||||
&mut self,
|
||||
fold: &ast::FoldCanonStreamMap<'_>,
|
||||
indent: usize,
|
||||
) -> io::Result<()> {
|
||||
compound!(self, indent, fold);
|
||||
if let Some(last_instruction) = &fold.last_instruction {
|
||||
multiline!(
|
||||
self, indent;
|
||||
"last:";
|
||||
last_instruction
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn beautify_new(&mut self, new: &ast::New<'_>, indent: usize) -> io::Result<()> {
|
||||
compound!(self, indent, new);
|
||||
Ok(())
|
||||
|
Loading…
x
Reference in New Issue
Block a user