mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 11:12:48 +03:00
Refactor AST
This commit is contained in:
parent
9f5be95f59
commit
a4d76c644a
@ -22,9 +22,7 @@ pub use program_id::*;
|
|||||||
pub mod program_scope;
|
pub mod program_scope;
|
||||||
pub use program_scope::*;
|
pub use program_scope::*;
|
||||||
|
|
||||||
use crate::Identifier;
|
use leo_span::{Span, Symbol};
|
||||||
|
|
||||||
use leo_span::Span;
|
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -34,7 +32,7 @@ use std::fmt;
|
|||||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
/// A map from import names to import definitions.
|
/// A map from import names to import definitions.
|
||||||
pub imports: IndexMap<Identifier, (Program, Span)>,
|
pub imports: IndexMap<Symbol, (Program, Span)>,
|
||||||
/// A map from program names to program scopes.
|
/// A map from program names to program scopes.
|
||||||
pub program_scopes: IndexMap<ProgramId, ProgramScope>,
|
pub program_scopes: IndexMap<ProgramId, ProgramScope>,
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,10 @@
|
|||||||
|
|
||||||
//! A Leo program scope consists of struct, function, and mapping definitions.
|
//! A Leo program scope consists of struct, function, and mapping definitions.
|
||||||
|
|
||||||
use crate::{Function, Identifier, Mapping, ProgramId, Struct};
|
use crate::{Function, Mapping, ProgramId, Struct};
|
||||||
|
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use leo_span::Span;
|
use leo_span::{Span, Symbol};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@ -29,11 +29,11 @@ pub struct ProgramScope {
|
|||||||
/// The program id of the program scope.
|
/// The program id of the program scope.
|
||||||
pub program_id: ProgramId,
|
pub program_id: ProgramId,
|
||||||
/// A map from struct names to struct definitions.
|
/// A map from struct names to struct definitions.
|
||||||
pub structs: IndexMap<Identifier, Struct>,
|
pub structs: IndexMap<Symbol, Struct>,
|
||||||
/// A map from mapping names to mapping definitions.
|
/// A map from mapping names to mapping definitions.
|
||||||
pub mappings: IndexMap<Identifier, Mapping>,
|
pub mappings: IndexMap<Symbol, Mapping>,
|
||||||
/// A map from function names to function definitions.
|
/// A map from function names to function definitions.
|
||||||
pub functions: IndexMap<Identifier, Function>,
|
pub functions: IndexMap<Symbol, Function>,
|
||||||
/// The span associated with the program scope.
|
/// The span associated with the program scope.
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ impl ParserContext<'_> {
|
|||||||
|
|
||||||
// TODO: remove import resolution from parser.
|
// TODO: remove import resolution from parser.
|
||||||
/// Parses an import statement `import foo.leo;`.
|
/// Parses an import statement `import foo.leo;`.
|
||||||
pub(super) fn parse_import(&mut self) -> Result<(Identifier, (Program, Span))> {
|
pub(super) fn parse_import(&mut self) -> Result<(Symbol, (Program, Span))> {
|
||||||
// Parse `import`.
|
// Parse `import`.
|
||||||
let start = self.expect(&Token::Import)?;
|
let start = self.expect(&Token::Import)?;
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ impl ParserContext<'_> {
|
|||||||
// Use the parser to construct the imported abstract syntax tree (ast).
|
// Use the parser to construct the imported abstract syntax tree (ast).
|
||||||
let program_ast = parse_ast(self.handler, &prg_sf.src, prg_sf.start_pos)?;
|
let program_ast = parse_ast(self.handler, &prg_sf.src, prg_sf.start_pos)?;
|
||||||
|
|
||||||
Ok((import_name, (program_ast.into_repr(), start + end)))
|
Ok((import_name.name, (program_ast.into_repr(), start + end)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parsers a program scope `program foo.aleo { ... }`.
|
/// Parsers a program scope `program foo.aleo { ... }`.
|
||||||
@ -238,7 +238,7 @@ impl ParserContext<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a struct or record definition, e.g., `struct Foo { ... }` or `record Foo { ... }`.
|
/// Parses a struct or record definition, e.g., `struct Foo { ... }` or `record Foo { ... }`.
|
||||||
pub(super) fn parse_struct(&mut self) -> Result<(Identifier, Struct)> {
|
pub(super) fn parse_struct(&mut self) -> Result<(Symbol, Struct)> {
|
||||||
let is_record = matches!(&self.token.token, Token::Record);
|
let is_record = matches!(&self.token.token, Token::Record);
|
||||||
let start = self.expect_any(&[Token::Struct, Token::Record])?;
|
let start = self.expect_any(&[Token::Struct, Token::Record])?;
|
||||||
let struct_name = self.expect_identifier()?;
|
let struct_name = self.expect_identifier()?;
|
||||||
@ -247,7 +247,7 @@ impl ParserContext<'_> {
|
|||||||
let (members, end) = self.parse_struct_members()?;
|
let (members, end) = self.parse_struct_members()?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
struct_name,
|
struct_name.name,
|
||||||
Struct {
|
Struct {
|
||||||
identifier: struct_name,
|
identifier: struct_name,
|
||||||
members,
|
members,
|
||||||
@ -258,7 +258,7 @@ impl ParserContext<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a mapping declaration, e.g. `mapping balances: address => u128`.
|
/// Parses a mapping declaration, e.g. `mapping balances: address => u128`.
|
||||||
pub(super) fn parse_mapping(&mut self) -> Result<(Identifier, Mapping)> {
|
pub(super) fn parse_mapping(&mut self) -> Result<(Symbol, Mapping)> {
|
||||||
let start = self.expect(&Token::Mapping)?;
|
let start = self.expect(&Token::Mapping)?;
|
||||||
let identifier = self.expect_identifier()?;
|
let identifier = self.expect_identifier()?;
|
||||||
self.expect(&Token::Colon)?;
|
self.expect(&Token::Colon)?;
|
||||||
@ -267,7 +267,7 @@ impl ParserContext<'_> {
|
|||||||
let (value_type, _) = self.parse_type()?;
|
let (value_type, _) = self.parse_type()?;
|
||||||
let end = self.expect(&Token::Semicolon)?;
|
let end = self.expect(&Token::Semicolon)?;
|
||||||
Ok((
|
Ok((
|
||||||
identifier,
|
identifier.name,
|
||||||
Mapping {
|
Mapping {
|
||||||
identifier,
|
identifier,
|
||||||
key_type,
|
key_type,
|
||||||
@ -416,7 +416,7 @@ impl ParserContext<'_> {
|
|||||||
|
|
||||||
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
|
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
|
||||||
/// and function definition.
|
/// and function definition.
|
||||||
fn parse_function(&mut self) -> Result<(Identifier, Function)> {
|
fn parse_function(&mut self) -> Result<(Symbol, Function)> {
|
||||||
// TODO: Handle dangling annotations.
|
// TODO: Handle dangling annotations.
|
||||||
// Parse annotations, if they exist.
|
// Parse annotations, if they exist.
|
||||||
let mut annotations = Vec::new();
|
let mut annotations = Vec::new();
|
||||||
@ -488,7 +488,7 @@ impl ParserContext<'_> {
|
|||||||
|
|
||||||
let span = start + block.span;
|
let span = start + block.span;
|
||||||
Ok((
|
Ok((
|
||||||
name,
|
name.name,
|
||||||
Function::new(annotations, call_type, name, inputs, output, block, finalize, span),
|
Function::new(annotations, call_type, name, inputs, output, block, finalize, span),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user