separate typed and untyped ASTs

This commit is contained in:
Romain Paquet 2023-10-16 22:22:10 +02:00
parent 91148b1a20
commit 86d4f7fffb
14 changed files with 512 additions and 262 deletions

View file

@ -1,66 +1,67 @@
pub mod expr;
pub mod module;
use std::path::Path;
pub use crate::ast::expr::{BinaryOperator, Expr};
use crate::ast::module::*;
use crate::typing::Type;
pub mod typed;
pub mod untyped;
#[derive(Debug, PartialEq, Clone)]
pub enum BinaryOperator {
Add,
Sub,
Mul,
Div,
Modulo,
Equal,
NotEqual,
}
#[derive(Debug, PartialEq, Clone)]
pub enum UnaryOperator {
}
pub type Identifier = String;
// XXX: Is this enum actually useful? Is 3:30 AM btw
#[derive(Debug, PartialEq)]
pub enum Ast {
Module(Module),
#[derive(Debug, PartialEq, Clone)]
pub struct ModulePath {
components: Vec<String>,
}
#[derive(Debug, PartialEq)]
pub enum Definition {
FunctionDefinition(FunctionDefinition),
//StructDefinition(StructDefinition),
impl std::fmt::Display for ModulePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.components.join("::")))
}
}
#[derive(Debug, PartialEq)]
pub struct Location {
pub file: Box<Path>,
impl From<&Path> for ModulePath {
fn from(path: &Path) -> Self {
let meta = std::fs::metadata(path).unwrap();
ModulePath {
components: path
.components()
.map(|component| match component {
std::path::Component::Normal(n) => {
if meta.is_file() {
n.to_str().unwrap().split(".").nth(0).unwrap().to_string()
} else if meta.is_dir() {
n.to_str().unwrap().to_string()
} else {
// XXX: symlinks?
unreachable!()
}
}
_ => unreachable!(),
})
.collect(),
}
}
}
#[derive(Debug, PartialEq)]
pub struct FunctionDefinition {
pub name: Identifier,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
pub body: Box<Block>,
pub line_col: (usize, usize),
}
#[derive(Debug, PartialEq)]
pub struct Block {
pub statements: Vec<Statement>,
pub value: Option<Expr>,
}
#[derive(Debug, PartialEq)]
pub enum Statement {
DeclareStatement(Identifier, Expr),
AssignStatement(Identifier, Expr),
ReturnStatement(Option<Expr>),
CallStatement(Call),
UseStatement(ModulePath),
IfStatement(Expr, Block),
WhileStatement(Box<Expr>, Box<Block>),
}
#[derive(Debug, PartialEq)]
pub struct Call {
pub callee: Expr,
pub args: Vec<Expr>,
}
#[derive(Debug, PartialEq)]
pub struct Parameter {
pub name: Identifier,
pub typ: Type,
impl From<&str> for ModulePath {
fn from(string: &str) -> Self {
ModulePath {
components: string.split("::").map(|c| c.to_string()).collect(),
}
}
}
#[derive(Eq, PartialEq, Debug)]
pub struct Import(pub String);