mirror of
https://github.com/fluencelabs/aquavm
synced 2025-06-24 12:11:33 +00:00
Use custom logic for lambda instead of json path (#154)
This commit is contained in:
20
crates/air-lib/lambda/ast/Cargo.toml
Normal file
20
crates/air-lib/lambda/ast/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "air-lambda-ast"
|
||||
description = "Parser of AIR value algebra values"
|
||||
version = "0.1.0"
|
||||
authors = ["Fluence Labs"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
publish = false
|
||||
keywords = ["fluence", "air", "lambda", "ast"]
|
||||
categories = ["wasm"]
|
||||
|
||||
[lib]
|
||||
name = "air_lambda_ast"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
non-empty-vec = { version = "0.2.0", features = ["serde"] }
|
||||
|
||||
serde = { version = "1.0.118", features = ["rc", "derive"] }
|
||||
serde_json = "1.0.61"
|
3
crates/air-lib/lambda/ast/README.md
Normal file
3
crates/air-lib/lambda/ast/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
## AIR lambda AST
|
||||
|
||||
AIR supports lambda expressions that could be applied both to scalars and streams. This crate defines an AST of such expressions, it has a array-like structure, because such a structure is easier to handle.
|
36
crates/air-lib/lambda/ast/src/ast.rs
Normal file
36
crates/air-lib/lambda/ast/src/ast.rs
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2021 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.
|
||||
*/
|
||||
|
||||
mod traits;
|
||||
|
||||
use non_empty_vec::NonEmpty;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
pub type LambdaAST<'input> = NonEmpty<ValueAccessor<'input>>;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum ValueAccessor<'input> {
|
||||
// (.)?[$idx]
|
||||
ArrayAccess { idx: u32 },
|
||||
|
||||
// .field
|
||||
FieldAccess { field_name: &'input str },
|
||||
|
||||
// needed to allow parser catch all errors from a lambda expression without stopping
|
||||
// on the very first one. Although, this variant is guaranteed not to be present in a lambda.
|
||||
Error,
|
||||
}
|
31
crates/air-lib/lambda/ast/src/ast/traits.rs
Normal file
31
crates/air-lib/lambda/ast/src/ast/traits.rs
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2021 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::*;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
impl fmt::Display for ValueAccessor<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use ValueAccessor::*;
|
||||
|
||||
match self {
|
||||
ArrayAccess { idx } => write!(f, ".[{}]", idx),
|
||||
FieldAccess { field_name } => write!(f, ".{}", field_name),
|
||||
Error => write!(f, "a parser error occurred while parsing lambda expression"),
|
||||
}
|
||||
}
|
||||
}
|
38
crates/air-lib/lambda/ast/src/lib.rs
Normal file
38
crates/air-lib/lambda/ast/src/lib.rs
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2021 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.
|
||||
*/
|
||||
|
||||
#![deny(
|
||||
dead_code,
|
||||
nonstandard_style,
|
||||
unused_imports,
|
||||
unused_mut,
|
||||
unused_variables,
|
||||
unused_unsafe,
|
||||
unreachable_patterns
|
||||
)]
|
||||
|
||||
mod ast;
|
||||
|
||||
pub use ast::*;
|
||||
|
||||
pub fn format_ast(lambda_ast: &LambdaAST<'_>) -> String {
|
||||
let mut formatted_ast = String::new();
|
||||
for algebra in lambda_ast.iter() {
|
||||
formatted_ast.push_str(&algebra.to_string());
|
||||
}
|
||||
|
||||
formatted_ast
|
||||
}
|
Reference in New Issue
Block a user