Add function to return AST as json (#35)

This commit is contained in:
folex 2020-12-02 18:47:14 +03:00 committed by GitHub
parent 221743ecc2
commit 42d2b825e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 83 additions and 27 deletions

31
Cargo.lock generated
View File

@ -20,6 +20,7 @@ dependencies = [
"lalrpop",
"lalrpop-util",
"regex",
"serde",
]
[[package]]
@ -710,14 +711,6 @@ dependencies = [
"wasmer-wasi-fl",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
dependencies = [
"fluence-sdk-wit 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.9"
@ -728,13 +721,11 @@ dependencies = [
]
[[package]]
name = "fluence-sdk-main"
name = "fluence-sdk-macro"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
dependencies = [
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
"fluence-sdk-wit 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
@ -749,9 +740,20 @@ dependencies = [
]
[[package]]
name = "fluence-sdk-wit"
name = "fluence-sdk-main"
version = "0.2.9"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
dependencies = [
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9c68c4d07e821e1be23b01c278acdae4e825d03c46879f453426ea3160b3e25"
dependencies = [
"proc-macro2",
"quote",
@ -764,8 +766,7 @@ dependencies = [
[[package]]
name = "fluence-sdk-wit"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9c68c4d07e821e1be23b01c278acdae4e825d03c46879f453426ea3160b3e25"
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
dependencies = [
"proc-macro2",
"quote",

View File

@ -13,6 +13,9 @@ regex = "1.4.1"
codespan = "0.9.5"
codespan-reporting = "0.9.5"
# TODO: hide serde behind feature
serde = "1.0.117"
[dev-dependencies]
fstrings = "0.2.3"
criterion = "0.3.3"

View File

@ -14,9 +14,10 @@
* limitations under the License.
*/
use serde::Serialize;
use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum Instruction<'i> {
Null(Null),
Call(Call<'i>),
@ -28,19 +29,19 @@ pub enum Instruction<'i> {
Error,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum PeerPart<'i> {
PeerPk(Value<'i>),
PeerPkWithServiceId(Value<'i>, Value<'i>),
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum FunctionPart<'i> {
FuncName(Value<'i>),
ServiceIdWithFuncName(Value<'i>, Value<'i>),
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Call<'i> {
pub peer_part: PeerPart<'i>,
pub function_part: FunctionPart<'i>,
@ -48,7 +49,7 @@ pub struct Call<'i> {
pub output: CallOutput<'i>,
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Debug, PartialEq, Eq, Clone)]
pub enum Value<'i> {
Variable(&'i str),
Literal(&'i str),
@ -57,31 +58,31 @@ pub enum Value<'i> {
InitPeerId,
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Serialize, Debug, PartialEq, Eq, Clone)]
pub enum CallOutput<'i> {
Scalar(&'i str),
Accumulator(&'i str),
None,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Seq<'i>(pub Box<Instruction<'i>>, pub Box<Instruction<'i>>);
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Par<'i>(pub Box<Instruction<'i>>, pub Box<Instruction<'i>>);
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Xor<'i>(pub Box<Instruction<'i>>, pub Box<Instruction<'i>>);
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Fold<'i> {
pub iterable: Value<'i>,
pub iterator: &'i str,
pub instruction: Rc<Instruction<'i>>,
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Next<'i>(pub &'i str);
#[derive(Debug, PartialEq, Eq)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Null;

View File

@ -27,6 +27,8 @@ use crate::Result;
use crate::StepperOutcome;
use crate::STEPPER_SUCCESS;
pub use prolog::parse;
pub fn execute_aqua(init_peer_id: String, aqua: String, prev_data: String, data: String) -> StepperOutcome {
log::trace!(
"aquamarine version is {}, init user id is {}",

View File

@ -77,3 +77,9 @@ pub(super) fn make_contexts(
Ok((exec_ctx, call_evidence_ctx))
}
/// Parse an AIR script to AST
pub fn parse(script: &str) -> Result<Instruction<'_>> {
let ast = air_parser::parse(script).map_err(|msg| AquamarineError::AIRParseError(msg))?;
Ok(*ast)
}

View File

@ -40,7 +40,9 @@ pub use crate::call_evidence::EvidenceState;
pub use crate::errors::AquamarineError;
pub use crate::stepper_outcome::StepperOutcome;
pub use crate::stepper_outcome::STEPPER_SUCCESS;
pub use air_parser::ast::Instruction;
pub use execution::execute_aqua;
pub use execution::parse;
pub(crate) type Result<T> = std::result::Result<T, AquamarineError>;
pub(crate) type JValue = serde_json::Value;

29
stepper/src/ast.rs Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright 2020 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 stepper_lib::parse;
/// Parse AIR script and return it as minified JSON
pub fn ast(script: String) -> String {
let do_parse = || -> std::result::Result<_, Box<dyn std::error::Error>> {
let ast = parse(&script)?;
serde_json::to_string(&ast).map_err(Into::into)
};
match do_parse() {
Ok(json) => json,
Err(err) => err.to_string(),
}
}

View File

@ -26,6 +26,7 @@
unreachable_patterns
)]
mod ast;
mod logger;
use fluence::fce;
@ -40,3 +41,8 @@ pub fn main() {
pub fn invoke(init_peer_id: String, aqua: String, prev_data: String, data: String) -> StepperOutcome {
execute_aqua(init_peer_id, aqua, prev_data, data)
}
#[fce]
pub fn ast(script: String) -> String {
ast::ast(script)
}

View File

@ -26,6 +26,7 @@
unreachable_patterns
)]
mod ast;
mod logger;
use stepper_lib::execute_aqua;
@ -41,3 +42,8 @@ pub fn invoke(init_peer_id: String, aqua: String, prev_data: String, data: Strin
let outcome = execute_aqua(init_peer_id, aqua, prev_data, data);
serde_json::to_string(&outcome).expect("Cannot parse StepperOutcome")
}
#[wasm_bindgen]
pub fn ast(script: String) -> String {
ast::ast(script)
}