Refactor AST

This commit is contained in:
d0cd 2022-11-15 16:15:23 -08:00
parent 9f5be95f59
commit a4d76c644a
3 changed files with 15 additions and 17 deletions

View File

@ -22,9 +22,7 @@ pub use program_id::*;
pub mod program_scope;
pub use program_scope::*;
use crate::Identifier;
use leo_span::Span;
use leo_span::{Span, Symbol};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
@ -34,7 +32,7 @@ use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Program {
/// 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.
pub program_scopes: IndexMap<ProgramId, ProgramScope>,
}

View File

@ -16,10 +16,10 @@
//! 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 leo_span::Span;
use leo_span::{Span, Symbol};
use serde::{Deserialize, Serialize};
use std::fmt;
@ -29,11 +29,11 @@ pub struct ProgramScope {
/// The program id of the program scope.
pub program_id: ProgramId,
/// 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.
pub mappings: IndexMap<Identifier, Mapping>,
pub mappings: IndexMap<Symbol, Mapping>,
/// 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.
pub span: Span,
}

View File

@ -73,7 +73,7 @@ impl ParserContext<'_> {
// TODO: remove import resolution from parser.
/// 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`.
let start = self.expect(&Token::Import)?;
@ -115,7 +115,7 @@ impl ParserContext<'_> {
// 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)?;
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 { ... }`.
@ -238,7 +238,7 @@ impl ParserContext<'_> {
}
/// 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 start = self.expect_any(&[Token::Struct, Token::Record])?;
let struct_name = self.expect_identifier()?;
@ -247,7 +247,7 @@ impl ParserContext<'_> {
let (members, end) = self.parse_struct_members()?;
Ok((
struct_name,
struct_name.name,
Struct {
identifier: struct_name,
members,
@ -258,7 +258,7 @@ impl ParserContext<'_> {
}
/// 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 identifier = self.expect_identifier()?;
self.expect(&Token::Colon)?;
@ -267,7 +267,7 @@ impl ParserContext<'_> {
let (value_type, _) = self.parse_type()?;
let end = self.expect(&Token::Semicolon)?;
Ok((
identifier,
identifier.name,
Mapping {
identifier,
key_type,
@ -416,7 +416,7 @@ impl ParserContext<'_> {
/// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name
/// and function definition.
fn parse_function(&mut self) -> Result<(Identifier, Function)> {
fn parse_function(&mut self) -> Result<(Symbol, Function)> {
// TODO: Handle dangling annotations.
// Parse annotations, if they exist.
let mut annotations = Vec::new();
@ -488,7 +488,7 @@ impl ParserContext<'_> {
let span = start + block.span;
Ok((
name,
name.name,
Function::new(annotations, call_type, name, inputs, output, block, finalize, span),
))
}