Skip to content

Commit

Permalink
Add CST data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBuilds committed Sep 15, 2024
1 parent 0698352 commit dab2851
Show file tree
Hide file tree
Showing 69 changed files with 2,616 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ edition = "2021"
members = [
"compiler/ast",
"compiler/compiler",
"compiler/cst",
"compiler/parser",
"compiler/passes",
"compiler/span",
Expand Down
56 changes: 56 additions & 0 deletions compiler/cst/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[package]
name = "leo-cst"
version = "2.1.0"
authors = [ "The Aleo Team <[email protected]>" ]
description = "Concrete syntax tree (CST) for the Leo programming language"
homepage = "https://aleo.org"
repository = "https://github.com/AleoHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "compilers", "cryptography", "web-programming" ]
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2021"
rust-version = "1.69"

[dependencies.snarkvm]
workspace = true

[dependencies.leo-errors]
path = "../../errors"
version = "2.1.0"

[dependencies.leo-span]
path = "../span"
version = "2.1.0"

[dependencies.indexmap]
version = "1.9"
features = [ "serde-1" ]

[dependencies.itertools]
version = "0.13.0"

[dependencies.serde]
version = "1.0"
features = [ "derive", "rc" ]

[dependencies.serde_json]
version = "1.0"
features = [ "preserve_order" ]

[dependencies.smallvec]
version = "1.13.1"
features = [ "serde" ]

[dev-dependencies.criterion]
version = "0.5"

[features]
default = [ ]
ci_skip = [ ]
596 changes: 596 additions & 0 deletions compiler/cst/LICENSE.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions compiler/cst/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# leo-cst

[![Crates.io](https://img.shields.io/crates/v/leo-cst.svg?color=neon)](https://crates.io/crates/leo-cst)
[![Authors](https://img.shields.io/badge/authors-Aleo-orange.svg)](../AUTHORS)
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](./LICENSE.md)

22 changes: 22 additions & 0 deletions compiler/cst/src/access/array_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


use crate::{Expression, Node, NodeID};
use leo_span::Span;

use serde::{Deserialize, Serialize};

/// An array access expression, e.g., `foo[index]`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArrayAccess {
/// An expression evaluating to some array type, e.g., `[false, true]`.
pub array: Box<Expression>,
/// The index to access in the array expression. E.g., `0` for `[false, true]` would yield `false`.
pub index: Box<Expression>,
/// The span for the entire expression `foo[index]`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}


crate::simple_node_impl!(ArrayAccess);
21 changes: 21 additions & 0 deletions compiler/cst/src/access/associated_constant_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

use crate::{Identifier, Node, NodeID, Type};
use leo_span::Span;

use serde::{Deserialize, Serialize};

/// An access expression to an struct constant., e.g. `u8::MAX`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssociatedConstant {
/// The inner struct type.
pub ty: Type,
/// The struct constant that is being accessed.
pub name: Identifier,
/// The span for the entire expression `Foo::bar()`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}


crate::simple_node_impl!(AssociatedConstant);
24 changes: 24 additions & 0 deletions compiler/cst/src/access/associated_function_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


use crate::{Expression, Identifier, Node, NodeID};
use leo_span::Span;

use serde::{Deserialize, Serialize};

/// An access expression to an associated function in a struct, e.g.`Pedersen64::hash()`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssociatedFunction {
/// The inner struct variant.
pub variant: Identifier,
/// The static struct member function that is being accessed.
pub name: Identifier,
/// The arguments passed to the function `name`.
pub arguments: Vec<Expression>,
/// The span for the entire expression `Foo::bar()`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}


crate::simple_node_impl!(AssociatedFunction);
22 changes: 22 additions & 0 deletions compiler/cst/src/access/member_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

use crate::{Expression, Identifier, Node, NodeID};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use std::fmt;

/// A struct member access expression `inner.name` to some structure with *named members*.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MemberAccess {
/// The inner struct that is being accessed.
pub inner: Box<Expression>,
/// The name of the struct member to access.
pub name: Identifier,
/// The span covering all of `inner.name`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}


crate::simple_node_impl!(MemberAccess);
16 changes: 16 additions & 0 deletions compiler/cst/src/access/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


mod array_access;
pub use array_access::*;

mod associated_constant_access;
pub use associated_constant_access::*;

mod associated_function_access;
pub use associated_function_access::*;

mod member_access;
pub use member_access::*;

mod tuple_access;
pub use tuple_access::*;
22 changes: 22 additions & 0 deletions compiler/cst/src/access/tuple_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


use crate::{Expression, Node, NodeID, NonNegativeNumber};
use leo_span::Span;

use serde::{Deserialize, Serialize};

/// A tuple access expression, e.g., `tuple.index`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TupleAccess {
/// An expression evaluating to some tuple type, e.g., `(5, 2)`.
pub tuple: Box<Expression>,
/// The index to access in the tuple expression. E.g., `0` for `(5, 2)` would yield `5`.
pub index: NonNegativeNumber,
/// The span for the entire expression `tuple.index`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}


crate::simple_node_impl!(TupleAccess);
8 changes: 8 additions & 0 deletions compiler/cst/src/common/comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]

pub enum Comment {
Line(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
Block(String, #[serde(with = "leo_span::span_json")] Span, NodeID),
}
19 changes: 19 additions & 0 deletions compiler/cst/src/common/identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{simple_node_impl, Node, NodeID};
use leo_span::{Span, Symbol};

use super::Space;

#[derive(Clone, Copy)]
pub struct Identifier {
/// The symbol that the user wrote, e.g., `foo`.
pub pl_name: Space,
pub name: Symbol,
pub pr_name: Space,

/// A span locating where the identifier occurred in the source.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}

simple_node_impl!(Identifier);
8 changes: 8 additions & 0 deletions compiler/cst/src/common/import_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::Identifier;

#[derive(Debug, Clone)]
pub struct ImportModule {
pub module: Identifier,
pub span: Span,
pub id: NodeID,
}
5 changes: 5 additions & 0 deletions compiler/cst/src/common/location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Location {
pub program: Option<Symbol>,
pub name: Symbol,
}
22 changes: 22 additions & 0 deletions compiler/cst/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use leo_span::Span;

pub mod comment;
pub use comment::*;

pub mod import_module;
pub use import_module::*;

pub mod node;
pub use node::*;

pub mod positive_number;
pub use positive_number::*;

pub mod location;
pub use location::*;

pub mod identifier;
pub use identifier::*;

pub mod space;
pub use space::*;
62 changes: 62 additions & 0 deletions compiler/cst/src/common/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_span::Span;

/// A node ID.
// Development Note:
// A `NodeID` must implement: `Copy`, `Default`, among others.
pub type NodeID = usize;

/// A node in the AST.
pub trait Node:
std::fmt::Debug + std::fmt::Display + Clone + PartialEq + Eq + serde::Serialize + serde::de::DeserializeOwned
{
/// Returns the span of the node.
fn span(&self) -> Span;

/// Sets the span of the node.
fn set_span(&mut self, span: Span);

/// Returns the ID of the node.
fn id(&self) -> NodeID;

/// Sets the ID of the node.
fn set_id(&mut self, id: NodeID);
}

#[macro_export]
macro_rules! simple_node_impl {
($ty:ty) => {
impl Node for $ty {
fn span(&self) -> Span {
self.span
}

fn set_span(&mut self, span: Span) {
self.span = span;
}

fn id(&self) -> NodeID {
self.id
}

fn set_id(&mut self, id: NodeID) {
self.id = id;
}
}
};
}
10 changes: 10 additions & 0 deletions compiler/cst/src/common/positive_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

/// A number string guaranteed to be non-negative.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub struct NonNegativeNumber {
/// The string representation of the non-negative number.
string: String,
/// The numeric value of the non-negative number.
value: usize,
}
Loading

0 comments on commit dab2851

Please sign in to comment.