Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization and Deserialization for parser Expression #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ impl TryFrom<&str> for Program {
}
}

impl From<Expression> for Program {
fn from(expression: Expression) -> Self {
Program { expression }
}
}

#[cfg(test)]
mod tests {
use crate::context::Context;
Expand Down
5 changes: 5 additions & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ edition = "2021"
license = "MIT"
categories = ["parsing"]

[features]
default = []
serde = ["dep:serde"]

[dependencies]
lalrpop-util = { version = "0.22.0", features = ["lexer"] }
regex = "1.4.2"
serde = { version = "1.0.215", features = ["derive", "rc"], optional = true }
thiserror = "1.0.40"

[dev-dependencies]
Expand Down
9 changes: 9 additions & 0 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::collections::HashSet;
use std::sync::Arc;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum RelationOp {
LessThan,
Expand All @@ -12,6 +16,7 @@ pub enum RelationOp {
In,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ArithmeticOp {
Add,
Expand All @@ -21,6 +26,7 @@ pub enum ArithmeticOp {
Modulus,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum UnaryOp {
Not,
Expand All @@ -29,6 +35,7 @@ pub enum UnaryOp {
DoubleMinus,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Arithmetic(Box<Expression>, ArithmeticOp, Box<Expression>),
Expand All @@ -49,13 +56,15 @@ pub enum Expression {
Ident(Arc<String>),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub enum Member {
Attribute(Arc<String>),
Index(Box<Expression>),
Fields(Vec<(Arc<String>, Expression)>),
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub enum Atom {
Int(i64),
Expand Down