conlang: deprecate the AST

This commit is contained in:
John 2024-01-21 01:34:40 -06:00
parent 6bb2f3774f
commit 5e2f365f45
4 changed files with 390 additions and 331 deletions

View File

@ -7,7 +7,7 @@
//! [`Identifier`]` := `[`IDENTIFIER`](crate::token::token_type::Type::Identifier) //! [`Identifier`]` := `[`IDENTIFIER`](crate::token::token_type::Type::Identifier)
//! //!
//! See [statement], [literal], and [expression] for more information. //! See [statement], [literal], and [expression] for more information.
#![deprecated]
pub mod preamble { pub mod preamble {
#![allow(deprecated)] #![allow(deprecated)]
//! Common imports for working with the [ast](super) //! Common imports for working with the [ast](super)
@ -123,12 +123,30 @@ pub mod statement {
/// # Syntax /// # Syntax
/// [`Fn`](Stmt::Fn) := `"fn"` [`Identifier`] `'('` `Args...` `')'` [`Block`] /// [`Fn`](Stmt::Fn) := `"fn"` [`Identifier`] `'('` `Args...` `')'` [`Block`]
Fn(FnDecl), Fn(FnDecl),
/// Contains a module declaration
/// # Syntax
/// [`Mod`](Stmt::Mod) := `"mod"` [`Identifier`] `'{'`
///
/// `'}'`
/// Contains an expression statement /// Contains an expression statement
/// # Syntax /// # Syntax
/// [`Expr`](Stmt::Expr) := [`Expr`] `;` /// [`Expr`](Stmt::Expr) := [`Expr`] `;`
Expr(Expr), Expr(Expr),
} }
/// Contains the declarations allowed in a module
///
/// # Syntax
/// [Mod](Module::Mod) := "mod" [Identifier] '{' [Module] '}'
/// [`Let`](Module::Let) := `"let"` [`Identifier`] (`:` `Type`)? (`=` [`Expr`])? `;`
#[derive(Clone, Debug)]
pub enum Module {
Struct(StructDecl),
Mod(ModuleDecl),
Let(Let),
Fn(FnDecl),
}
/// Contains a variable declaration /// Contains a variable declaration
/// # Syntax /// # Syntax
/// [`Let`] := `let` [`Identifier`] (`:`) `Type`)? (`=` [`Expr`])? `;` /// [`Let`] := `let` [`Identifier`] (`:`) `Type`)? (`=` [`Expr`])? `;`
@ -160,7 +178,22 @@ pub mod statement {
pub ty: Option<TypeExpr>, pub ty: Option<TypeExpr>,
} }
/// Contains the name and declaration
#[derive(Clone, Debug)]
pub struct ModuleDecl {}
// TODO: Create closure, transmute fndecl into a name and closure // TODO: Create closure, transmute fndecl into a name and closure
/// Contains the name and field information for a struct
///
/// # Syntax
/// [`StructDecl`]` := "struct" `[`Identifier`]` '{'
/// (`[`Identifier`]` ':' `[`TypeExpr`]`),*
/// '}'`
#[derive(Clone, Debug)]
pub struct StructDecl {
pub name: Identifier,
pub data: Vec<(Identifier, TypeExpr)>,
}
} }
pub mod path { pub mod path {
@ -324,6 +357,10 @@ pub mod expression {
pub callee: Box<Primary>, pub callee: Box<Primary>,
pub args: Vec<Tuple>, pub args: Vec<Tuple>,
} }
#[allow(non_snake_case)]
pub fn FnCall(callee: Box<Primary>, args: Vec<Tuple>) -> FnCall {
FnCall { callee, args }
}
} }
pub mod tuple { pub mod tuple {
@ -762,6 +799,16 @@ pub mod todo {
//! traits, modules, etc. //! traits, modules, etc.
} }
pub mod module {
//! Module support
//! - [ ] Add Module Declaration type : ModDecl = "mod" Identifier '{' Module '}' ;
//! - [ ] Change Program to Module : Module = (ModDecl | FnDecl | Let)*
//! - [ ] Implementer's note: Modules must be traversed breadth-first, with no
//! alpha-renaming
//! - [ ] Blocks should probably also be traversed breadth-first, and Let declarations
//! hoisted up, leaving initialization assignments in-place
}
pub mod structure { pub mod structure {
//! Struct support //! Struct support
//! - [ ] Add struct declaration expression (returns a struct declaration) //! - [ ] Add struct declaration expression (returns a struct declaration)

View File

@ -1,5 +1,6 @@
//! Parses [tokens](super::token) into an [AST](super::ast) //! Parses [tokens](super::token) into an [AST](super::ast)
#![deprecated]
#![allow(deprecated)]
use super::{ast::preamble::*, lexer::Lexer, token::preamble::*}; use super::{ast::preamble::*, lexer::Lexer, token::preamble::*};
use error::{Error, *}; use error::{Error, *};

View File

@ -1,5 +1,6 @@
//! A [Printer] pretty-prints a Conlang [syntax tree](crate::ast) //! A [Printer] pretty-prints a Conlang [syntax tree](crate::ast)
#![deprecated]
#![allow(deprecated)]
use super::ast::preamble::*; use super::ast::preamble::*;
use std::{ use std::{
fmt::Display, fmt::Display,

View File

@ -3,8 +3,6 @@
//! This will hopefully become a fully fledged static resolution pass in the future //! This will hopefully become a fully fledged static resolution pass in the future
use std::collections::HashMap; use std::collections::HashMap;
use crate::ast::preamble::*;
use scopeguard::Scoped; use scopeguard::Scoped;
pub mod scopeguard { pub mod scopeguard {
//! Implements a generic RAII scope-guard //! Implements a generic RAII scope-guard
@ -488,14 +486,17 @@ pub trait Resolve {
Ok(Type::Empty) Ok(Type::Empty)
} }
} }
mod ast1 {
impl Resolve for Start { #![allow(deprecated)]
use super::*;
use crate::ast::preamble::*;
impl Resolve for Start {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Self(program) = self; let Self(program) = self;
program.resolve(resolver) program.resolve(resolver)
} }
} }
impl Resolve for Program { impl Resolve for Program {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Self(module) = self; let Self(module) = self;
for decl in module { for decl in module {
@ -504,8 +505,8 @@ impl Resolve for Program {
// TODO: record the number of module-level assignments into the AST // TODO: record the number of module-level assignments into the AST
Ok(Type::Empty) Ok(Type::Empty)
} }
} }
impl Resolve for Stmt { impl Resolve for Stmt {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
match self { match self {
Stmt::Let(value) => value.resolve(resolver), Stmt::Let(value) => value.resolve(resolver),
@ -513,10 +514,11 @@ impl Resolve for Stmt {
Stmt::Expr(value) => value.resolve(resolver), Stmt::Expr(value) => value.resolve(resolver),
} }
} }
} }
impl Resolve for Let { impl Resolve for Let {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Let { name: Name { symbol: Identifier { name, index }, mutable, ty: _ }, init } = self; let Let { name: Name { symbol: Identifier { name, index }, mutable, ty: _ }, init } =
self;
debugln!("ty> let {name} ..."); debugln!("ty> let {name} ...");
if let Some(init) = init { if let Some(init) = init {
let ty = init.resolve(resolver)?; let ty = init.resolve(resolver)?;
@ -527,8 +529,8 @@ impl Resolve for Let {
} }
Ok(Type::Empty) Ok(Type::Empty)
} }
} }
impl Resolve for FnDecl { impl Resolve for FnDecl {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let FnDecl { name: Name { symbol: Identifier { name, index }, .. }, args, body } = self; let FnDecl { name: Name { symbol: Identifier { name, index }, .. }, args, body } = self;
debugln!("ty> fn {name} ..."); debugln!("ty> fn {name} ...");
@ -550,13 +552,13 @@ impl Resolve for FnDecl {
let _ = std::mem::replace(&mut resolver.scopes, scopes); let _ = std::mem::replace(&mut resolver.scopes, scopes);
out out
} }
} }
impl Resolve for Name { impl Resolve for Name {
fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> {
Ok(Type::Empty) Ok(Type::Empty)
} }
} }
impl Resolve for Block { impl Resolve for Block {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Block { let_count: _, statements, expr } = self; let Block { let_count: _, statements, expr } = self;
let mut resolver = resolver.frame(); let mut resolver = resolver.frame();
@ -565,15 +567,15 @@ impl Resolve for Block {
} }
expr.resolve(&mut resolver) expr.resolve(&mut resolver)
} }
} }
impl Resolve for Expr { impl Resolve for Expr {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Expr(expr) = self; let Expr(expr) = self;
expr.resolve(resolver) expr.resolve(resolver)
} }
} }
impl Resolve for Operation { impl Resolve for Operation {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
match self { match self {
Operation::Assign(value) => value.resolve(resolver), Operation::Assign(value) => value.resolve(resolver),
@ -582,8 +584,8 @@ impl Resolve for Operation {
Operation::Call(value) => value.resolve(resolver), Operation::Call(value) => value.resolve(resolver),
} }
} }
} }
impl Resolve for Assign { impl Resolve for Assign {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Assign { target, operator, init } = self; let Assign { target, operator, init } = self;
// Evaluate the initializer expression // Evaluate the initializer expression
@ -600,9 +602,9 @@ impl Resolve for Assign {
.map(|_| Type::Empty), .map(|_| Type::Empty),
} }
} }
} }
impl Resolve for Binary { impl Resolve for Binary {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Binary { first, other } = self; let Binary { first, other } = self;
@ -613,8 +615,8 @@ impl Resolve for Binary {
} }
Ok(first) Ok(first)
} }
} }
impl Resolve for Unary { impl Resolve for Unary {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Unary { operators, operand } = self; let Unary { operators, operand } = self;
let mut operand = operand.resolve(resolver)?; let mut operand = operand.resolve(resolver)?;
@ -623,9 +625,9 @@ impl Resolve for Unary {
} }
Ok(operand) Ok(operand)
} }
} }
/// Resolve [operator]s /// Resolve [operator]s
impl Resolver { impl Resolver {
fn resolve_binary_operator( fn resolve_binary_operator(
&mut self, &mut self,
first: Type, first: Type,
@ -641,20 +643,24 @@ impl Resolver {
Ok(first) Ok(first)
} }
} }
fn resolve_unary_operator(&mut self, operand: Type, op: &operator::Unary) -> TyResult<Type> { fn resolve_unary_operator(
&mut self,
operand: Type,
op: &operator::Unary,
) -> TyResult<Type> {
// TODO: Allow more expressive unary operator type conversions // TODO: Allow more expressive unary operator type conversions
todo!("Resolve unary operators {op:?} {operand}") todo!("Resolve unary operators {op:?} {operand}")
} }
} }
impl Resolve for Call { impl Resolve for Call {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
match self { match self {
Call::FnCall(value) => value.resolve(resolver), Call::FnCall(value) => value.resolve(resolver),
Call::Primary(value) => value.resolve(resolver), Call::Primary(value) => value.resolve(resolver),
} }
} }
} }
impl Resolve for FnCall { impl Resolve for FnCall {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let FnCall { callee, args } = self; let FnCall { callee, args } = self;
let mut callee = callee.resolve(resolver)?; let mut callee = callee.resolve(resolver)?;
@ -691,8 +697,8 @@ impl Resolve for FnCall {
} }
Ok(callee) Ok(callee)
} }
} }
impl Resolve for Primary { impl Resolve for Primary {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
match self { match self {
Primary::Identifier(value) => value.resolve(resolver), Primary::Identifier(value) => value.resolve(resolver),
@ -702,9 +708,9 @@ impl Resolve for Primary {
Primary::Branch(value) => value.resolve(resolver), Primary::Branch(value) => value.resolve(resolver),
} }
} }
} }
impl Resolve for Group { impl Resolve for Group {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
match self { match self {
Group::Tuple(tuple) => tuple.resolve(resolver), Group::Tuple(tuple) => tuple.resolve(resolver),
@ -712,9 +718,9 @@ impl Resolve for Group {
Group::Empty => Ok(Type::Empty), Group::Empty => Ok(Type::Empty),
} }
} }
} }
impl Resolve for Tuple { impl Resolve for Tuple {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Tuple { elements } = self; let Tuple { elements } = self;
let mut types = vec![]; let mut types = vec![];
@ -723,9 +729,9 @@ impl Resolve for Tuple {
} }
Ok(Type::Tuple(types)) Ok(Type::Tuple(types))
} }
} }
impl Resolve for Identifier { impl Resolve for Identifier {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Identifier { name, index: id_index } = self; let Identifier { name, index: id_index } = self;
let Variable { index, status, .. } = resolver.get(name)?; let Variable { index, status, .. } = resolver.get(name)?;
@ -737,8 +743,8 @@ impl Resolve for Identifier {
debugln!("ty> Resolved {} #{index}: {ty}", name); debugln!("ty> Resolved {} #{index}: {ty}", name);
Ok(ty.to_owned()) Ok(ty.to_owned())
} }
} }
impl Resolve for Literal { impl Resolve for Literal {
fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> {
Ok(match self { Ok(match self {
Literal::String(_) => Type::String, Literal::String(_) => Type::String,
@ -748,9 +754,9 @@ impl Resolve for Literal {
Literal::Int(_) => Type::Int, Literal::Int(_) => Type::Int,
}) })
} }
} }
impl Resolve for Flow { impl Resolve for Flow {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
// TODO: Finish this // TODO: Finish this
match self { match self {
@ -762,8 +768,8 @@ impl Resolve for Flow {
Flow::Break(value) => value.resolve(resolver), Flow::Break(value) => value.resolve(resolver),
} }
} }
} }
impl Resolve for While { impl Resolve for While {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
// TODO: Finish this // TODO: Finish this
// Visit else first, save that to a break-pattern stack in the Resolver, // Visit else first, save that to a break-pattern stack in the Resolver,
@ -773,8 +779,8 @@ impl Resolve for While {
body.resolve(resolver)?; // discard body.resolve(resolver)?; // discard
else_.resolve(resolver) // compare with returns inside body else_.resolve(resolver) // compare with returns inside body
} }
} }
impl Resolve for If { impl Resolve for If {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let If { cond, body, else_ } = self; let If { cond, body, else_ } = self;
let cond = cond.resolve(resolver)?; let cond = cond.resolve(resolver)?;
@ -789,9 +795,9 @@ impl Resolve for If {
Err(Error::TypeMismatch { want: body_ty, got: else_ty }) Err(Error::TypeMismatch { want: body_ty, got: else_ty })
} }
} }
} }
impl Resolve for For { impl Resolve for For {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let For { var: Identifier { name, index }, iter, body, else_ } = self; let For { var: Identifier { name, index }, iter, body, else_ } = self;
debugln!("> for {name} in ..."); debugln!("> for {name} in ...");
@ -816,34 +822,38 @@ impl Resolve for For {
Ok(body_ty) Ok(body_ty)
} }
} }
} }
impl Resolve for Else { impl Resolve for Else {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
let Else { expr } = self; let Else { expr } = self;
expr.resolve(resolver) expr.resolve(resolver)
} }
} }
impl Resolve for Continue { impl Resolve for Continue {
fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, _resolver: &mut Resolver) -> TyResult<Type> {
// TODO: Finish control flow // TODO: Finish control flow
Ok(Type::Never) Ok(Type::Never)
} }
} }
impl Resolve for Break { impl Resolve for Break {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
// TODO: Finish control flow // TODO: Finish control flow
let Break { expr } = self; let Break { expr } = self;
expr.resolve(resolver) expr.resolve(resolver)
} }
} }
impl Resolve for Return { impl Resolve for Return {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {
// TODO: Finish control flow // TODO: Finish control flow
let Return { expr } = self; let Return { expr } = self;
expr.resolve(resolver) expr.resolve(resolver)
} }
}
} }
mod ast2 {}
// heakc yea man, generics // heakc yea man, generics
impl<T: Resolve> Resolve for Option<T> { impl<T: Resolve> Resolve for Option<T> {
fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> { fn resolve(&mut self, resolver: &mut Resolver) -> TyResult<Type> {