Run rustfmt on ast module

This commit is contained in:
Greg Shuflin 2021-10-28 02:00:37 -07:00
parent 765a0bec58
commit 5d04a020dc
3 changed files with 210 additions and 282 deletions

View File

@ -1,18 +1,22 @@
#![allow(clippy::upper_case_acronyms)] #![allow(clippy::upper_case_acronyms)]
#![allow(clippy::enum_variant_names)] #![allow(clippy::enum_variant_names)]
use std::rc::Rc; use std::{
use std::convert::{AsRef, From}; convert::{AsRef, From},
rc::Rc,
};
mod visitor;
mod operators; mod operators;
mod visitor;
pub use operators::{PrefixOp, BinOp}; pub use operators::{BinOp, PrefixOp};
pub use visitor::*; pub use visitor::*;
use crate::derivative::Derivative; use crate::{
use crate::tokenizing::Location; derivative::Derivative,
use crate::identifier::{Id, define_id_kind}; identifier::{define_id_kind, Id},
tokenizing::Location,
};
define_id_kind!(ASTItem); define_id_kind!(ASTItem);
@ -29,7 +33,7 @@ pub type ItemId = Id<ASTItem>;
#[derive(Derivative, Debug)] #[derive(Derivative, Debug)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct AST { pub struct AST {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
pub statements: Block, pub statements: Block,
} }
@ -37,9 +41,9 @@ pub struct AST {
#[derive(Derivative, Debug, Clone)] #[derive(Derivative, Debug, Clone)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct Statement { pub struct Statement {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub location: Location, pub location: Location,
pub kind: StatementKind, pub kind: StatementKind,
} }
@ -54,7 +58,7 @@ pub enum StatementKind {
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
pub struct Block { pub struct Block {
pub statements: Vec<Statement> pub statements: Vec<Statement>,
} }
impl From<Vec<Statement>> for Block { impl From<Vec<Statement>> for Block {
@ -80,7 +84,7 @@ pub type ParamName = Rc<String>;
#[derive(Debug, Derivative, Clone)] #[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct QualifiedName { pub struct QualifiedName {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
pub components: Vec<Rc<String>>, pub components: Vec<Rc<String>>,
} }
@ -89,42 +93,20 @@ pub struct QualifiedName {
pub struct FormalParam { pub struct FormalParam {
pub name: ParamName, pub name: ParamName,
pub default: Option<Expression>, pub default: Option<Expression>,
pub anno: Option<TypeIdentifier> pub anno: Option<TypeIdentifier>,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Declaration { pub enum Declaration {
FuncSig(Signature), FuncSig(Signature),
FuncDecl(Signature, Block), FuncDecl(Signature, Block),
TypeDecl { TypeDecl { name: TypeSingletonName, body: TypeBody, mutable: bool },
name: TypeSingletonName,
body: TypeBody,
mutable: bool
},
//TODO this needs to be more sophisticated //TODO this needs to be more sophisticated
TypeAlias { TypeAlias { alias: Rc<String>, original: Rc<String> },
alias: Rc<String>, Binding { name: Rc<String>, constant: bool, type_anno: Option<TypeIdentifier>, expr: Expression },
original: Rc<String>, Impl { type_name: TypeIdentifier, interface_name: Option<TypeSingletonName>, block: Vec<Declaration> },
}, Interface { name: Rc<String>, signatures: Vec<Signature> },
Binding { Annotation { name: Rc<String>, arguments: Vec<Expression> },
name: Rc<String>,
constant: bool,
type_anno: Option<TypeIdentifier>,
expr: Expression,
},
Impl {
type_name: TypeIdentifier,
interface_name: Option<TypeSingletonName>,
block: Vec<Declaration>,
},
Interface {
name: Rc<String>,
signatures: Vec<Signature>
},
Annotation {
name: Rc<String>,
arguments: Vec<Expression>
}
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@ -141,26 +123,26 @@ pub struct TypeBody(pub Vec<Variant>);
#[derive(Debug, Derivative, Clone)] #[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct Variant { pub struct Variant {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
pub name: Rc<String>, pub name: Rc<String>,
pub kind: VariantKind, pub kind: VariantKind,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum VariantKind { pub enum VariantKind {
UnitStruct, UnitStruct,
TupleStruct(Vec<TypeIdentifier>), TupleStruct(Vec<TypeIdentifier>),
Record(Vec<(Rc<String>, TypeIdentifier)>), Record(Vec<(Rc<String>, TypeIdentifier)>),
} }
#[derive(Debug, Derivative, Clone)] #[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct Expression { pub struct Expression {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
pub kind: ExpressionKind, pub kind: ExpressionKind,
pub type_anno: Option<TypeIdentifier> pub type_anno: Option<TypeIdentifier>,
} }
impl Expression { impl Expression {
@ -177,7 +159,7 @@ impl Expression {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum TypeIdentifier { pub enum TypeIdentifier {
Tuple(Vec<TypeIdentifier>), Tuple(Vec<TypeIdentifier>),
Singleton(TypeSingletonName) Singleton(TypeSingletonName),
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@ -196,60 +178,28 @@ pub enum ExpressionKind {
PrefixExp(PrefixOp, Box<Expression>), PrefixExp(PrefixOp, Box<Expression>),
TupleLiteral(Vec<Expression>), TupleLiteral(Vec<Expression>),
Value(QualifiedName), Value(QualifiedName),
NamedStruct { NamedStruct { name: QualifiedName, fields: Vec<(Rc<String>, Expression)> },
name: QualifiedName, Call { f: Box<Expression>, arguments: Vec<InvocationArgument> },
fields: Vec<(Rc<String>, Expression)>, Index { indexee: Box<Expression>, indexers: Vec<Expression> },
}, IfExpression { discriminator: Option<Box<Expression>>, body: Box<IfExpressionBody> },
Call { WhileExpression { condition: Option<Box<Expression>>, body: Block },
f: Box<Expression>, ForExpression { enumerators: Vec<Enumerator>, body: Box<ForBody> },
arguments: Vec<InvocationArgument>, Lambda { params: Vec<FormalParam>, type_anno: Option<TypeIdentifier>, body: Block },
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
},
IfExpression {
discriminator: Option<Box<Expression>>,
body: Box<IfExpressionBody>,
},
WhileExpression {
condition: Option<Box<Expression>>,
body: Block,
},
ForExpression {
enumerators: Vec<Enumerator>,
body: Box<ForBody>,
},
Lambda {
params: Vec<FormalParam>,
type_anno: Option<TypeIdentifier>,
body: Block,
},
ListLiteral(Vec<Expression>), ListLiteral(Vec<Expression>),
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum InvocationArgument { pub enum InvocationArgument {
Positional(Expression), Positional(Expression),
Keyword { Keyword { name: Rc<String>, expr: Expression },
name: Rc<String>, Ignored,
expr: Expression,
},
Ignored
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum IfExpressionBody { pub enum IfExpressionBody {
SimpleConditional { SimpleConditional { then_case: Block, else_case: Option<Block> },
then_case: Block, SimplePatternMatch { pattern: Pattern, then_case: Block, else_case: Option<Block> },
else_case: Option<Block> CondList(Vec<ConditionArm>),
},
SimplePatternMatch {
pattern: Pattern,
then_case: Block,
else_case: Option<Block>
},
CondList(Vec<ConditionArm>)
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@ -279,10 +229,7 @@ pub enum Pattern {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum PatternLiteral { pub enum PatternLiteral {
NumPattern { NumPattern { neg: bool, num: ExpressionKind },
neg: bool,
num: ExpressionKind,
},
StringPattern(Rc<String>), StringPattern(Rc<String>),
BoolPattern(bool), BoolPattern(bool),
} }
@ -302,23 +249,21 @@ pub enum ForBody {
#[derive(Debug, Derivative, Clone)] #[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)] #[derivative(PartialEq)]
pub struct ImportSpecifier { pub struct ImportSpecifier {
#[derivative(PartialEq="ignore")] #[derivative(PartialEq = "ignore")]
pub id: ItemId, pub id: ItemId,
pub path_components: Vec<Rc<String>>, pub path_components: Vec<Rc<String>>,
pub imported_names: ImportedNames pub imported_names: ImportedNames,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum ImportedNames { pub enum ImportedNames {
All, All,
LastOfPath, LastOfPath,
List(Vec<Rc<String>>) List(Vec<Rc<String>>),
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct ModuleSpecifier { pub struct ModuleSpecifier {
pub name: Rc<String>, pub name: Rc<String>,
pub contents: Block, pub contents: Block,
} }

View File

@ -49,7 +49,7 @@ impl BinOp {
} }
} }
fn token_kind_to_sigil(tok: & TokenKind) -> Option<&str> { fn token_kind_to_sigil(tok: &TokenKind) -> Option<&str> {
use self::TokenKind::*; use self::TokenKind::*;
Some(match tok { Some(match tok {
Operator(op) => op.as_str(), Operator(op) => op.as_str(),
@ -59,7 +59,7 @@ fn token_kind_to_sigil(tok: & TokenKind) -> Option<&str> {
LAngleBracket => "<", LAngleBracket => "<",
RAngleBracket => ">", RAngleBracket => ">",
Equals => "=", Equals => "=",
_ => return None _ => return None,
}) })
} }

View File

@ -3,17 +3,27 @@ use crate::ast::*;
#[derive(Debug)] #[derive(Debug)]
pub enum Recursion { pub enum Recursion {
Continue, Continue,
Stop Stop,
} }
pub trait ASTVisitor: Sized { pub trait ASTVisitor: Sized {
fn expression(&mut self, _expression: &Expression) -> Recursion { Recursion::Continue } fn expression(&mut self, _expression: &Expression) -> Recursion {
fn declaration(&mut self, _declaration: &Declaration, _id: &ItemId) -> Recursion { Recursion::Continue } Recursion::Continue
}
fn declaration(&mut self, _declaration: &Declaration, _id: &ItemId) -> Recursion {
Recursion::Continue
}
fn import(&mut self, _import: &ImportSpecifier) -> Recursion { Recursion::Continue } fn import(&mut self, _import: &ImportSpecifier) -> Recursion {
fn module(&mut self, _module: &ModuleSpecifier) -> Recursion { Recursion::Continue } Recursion::Continue
}
fn module(&mut self, _module: &ModuleSpecifier) -> Recursion {
Recursion::Continue
}
fn pattern(&mut self, _pat: &Pattern) -> Recursion { Recursion::Continue } fn pattern(&mut self, _pat: &Pattern) -> Recursion {
Recursion::Continue
}
} }
pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) { pub fn walk_ast<V: ASTVisitor>(v: &mut V, ast: &AST) {
@ -32,12 +42,11 @@ pub fn walk_block<V: ASTVisitor>(v: &mut V, block: &Block) {
} }
Import(ref import_spec) => { Import(ref import_spec) => {
v.import(import_spec); v.import(import_spec);
}, }
Module(ref module_spec) => { Module(ref module_spec) =>
if let Recursion::Continue = v.module(module_spec) { if let Recursion::Continue = v.module(module_spec) {
walk_block(v, &module_spec.contents); walk_block(v, &module_spec.contents);
} },
}
} }
} }
} }
@ -50,12 +59,7 @@ pub fn walk_declaration<V: ASTVisitor>(v: &mut V, decl: &Declaration, id: &ItemI
FuncDecl(_sig, block) => { FuncDecl(_sig, block) => {
walk_block(v, block); walk_block(v, block);
} }
Binding { Binding { name: _, constant: _, type_anno: _, expr } => {
name: _,
constant: _,
type_anno: _,
expr,
} => {
walk_expression(v, expr); walk_expression(v, expr);
} }
_ => (), _ => (),
@ -76,16 +80,14 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
PrefixExp(_, arg) => { PrefixExp(_, arg) => {
walk_expression(v, arg); walk_expression(v, arg);
} }
TupleLiteral(exprs) => { TupleLiteral(exprs) =>
for expr in exprs { for expr in exprs {
walk_expression(v, expr); walk_expression(v, expr);
} },
} NamedStruct { name: _, fields } =>
NamedStruct { name: _, fields } => {
for (_, expr) in fields.iter() { for (_, expr) in fields.iter() {
walk_expression(v, expr); walk_expression(v, expr);
} },
}
Call { f, arguments } => { Call { f, arguments } => {
walk_expression(v, f); walk_expression(v, f);
for arg in arguments.iter() { for arg in arguments.iter() {
@ -102,10 +104,7 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
walk_expression(v, indexer); walk_expression(v, indexer);
} }
} }
IfExpression { IfExpression { discriminator, body } => {
discriminator,
body,
} => {
if let Some(d) = discriminator.as_ref() { if let Some(d) = discriminator.as_ref() {
walk_expression(v, d); walk_expression(v, d);
} }
@ -126,18 +125,13 @@ pub fn walk_expression<V: ASTVisitor>(v: &mut V, expr: &Expression) {
ForBody::StatementBlock(block) => walk_block(v, block), ForBody::StatementBlock(block) => walk_block(v, block),
}; };
} }
Lambda { Lambda { params: _, type_anno: _, body } => {
params: _,
type_anno: _,
body,
} => {
walk_block(v, body); walk_block(v, body);
} }
ListLiteral(exprs) => { ListLiteral(exprs) =>
for expr in exprs { for expr in exprs {
walk_expression(v, expr); walk_expression(v, expr);
} },
}
}; };
} }
} }
@ -146,27 +140,20 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
use IfExpressionBody::*; use IfExpressionBody::*;
match body { match body {
SimpleConditional { SimpleConditional { then_case, else_case } => {
then_case,
else_case,
} => {
walk_block(v, then_case); walk_block(v, then_case);
if let Some(block) = else_case.as_ref() { if let Some(block) = else_case.as_ref() {
walk_block(v, block) walk_block(v, block)
} }
} }
SimplePatternMatch { SimplePatternMatch { pattern, then_case, else_case } => {
pattern,
then_case,
else_case,
} => {
walk_pattern(v, pattern); walk_pattern(v, pattern);
walk_block(v, then_case); walk_block(v, then_case);
if let Some(block) = else_case.as_ref() { if let Some(block) = else_case.as_ref() {
walk_block(v, block) walk_block(v, block)
} }
} }
CondList(arms) => { CondList(arms) =>
for arm in arms { for arm in arms {
match arm.condition { match arm.condition {
Condition::Pattern(ref pat) => { Condition::Pattern(ref pat) => {
@ -184,8 +171,7 @@ pub fn walk_if_expr_body<V: ASTVisitor>(v: &mut V, body: &IfExpressionBody) {
walk_expression(v, guard); walk_expression(v, guard);
} }
walk_block(v, &arm.body); walk_block(v, &arm.body);
} },
}
} }
} }
@ -194,21 +180,18 @@ pub fn walk_pattern<V: ASTVisitor>(v: &mut V, pat: &Pattern) {
if let Recursion::Continue = v.pattern(pat) { if let Recursion::Continue = v.pattern(pat) {
match pat { match pat {
TuplePattern(patterns) => { TuplePattern(patterns) =>
for pat in patterns { for pat in patterns {
walk_pattern(v, pat); walk_pattern(v, pat);
} },
} TupleStruct(_, patterns) =>
TupleStruct(_, patterns) => {
for pat in patterns { for pat in patterns {
walk_pattern(v, pat); walk_pattern(v, pat);
} },
} Record(_, name_and_patterns) =>
Record(_, name_and_patterns) => {
for (_, pat) in name_and_patterns { for (_, pat) in name_and_patterns {
walk_pattern(v, pat); walk_pattern(v, pat);
} },
}
_ => (), _ => (),
}; };
} }