Files
boring-lang/src/ast.rs

393 lines
9.7 KiB
Rust
Raw Normal View History

use std::cell::RefCell;
2021-04-18 08:51:21 -06:00
2022-03-12 04:35:48 -07:00
#[derive(Debug, Clone, PartialEq, Eq)]
2021-07-23 08:58:17 -06:00
pub struct IdGenerator {
2022-03-12 04:35:48 -07:00
id_key: String,
counter: RefCell<i64>,
2021-07-23 08:58:17 -06:00
}
impl IdGenerator {
2022-03-12 04:35:48 -07:00
pub fn new(key: &str) -> Self {
IdGenerator { id_key: key.to_string(), counter: RefCell::new(0) }
2021-07-23 08:58:17 -06:00
}
pub fn next(&self) -> String {
*self.counter.borrow_mut() += 1;
2022-03-12 04:35:48 -07:00
(self.id_key.to_owned() + &self.counter.borrow().to_string()).to_string()
2021-07-23 08:58:17 -06:00
}
}
pub fn new_unit() -> TypeUsage {
2021-09-11 12:34:23 -06:00
TypeUsage::Named(NamedTypeUsage {
type_parameters: GenericUsage::Known(GenericInstantiation { parameters: vec![] }),
2021-09-11 12:34:23 -06:00
name: Identifier {
name: Spanned {
span: Span { left: 0, right: 0 }, //todo: figure out a sane value for these
2021-09-11 12:32:08 -06:00
value: "unit".to_string(),
2021-09-11 12:34:23 -06:00
},
},
})
2021-07-23 08:58:17 -06:00
}
pub fn new_never() -> TypeUsage {
2021-09-11 12:34:23 -06:00
TypeUsage::Named(NamedTypeUsage {
type_parameters: GenericUsage::Known(GenericInstantiation { parameters: vec![] }),
2021-09-11 12:34:23 -06:00
name: Identifier {
name: Spanned {
span: Span { left: 0, right: 0 }, //todo: figure out a sane value for these
2021-07-23 08:58:17 -06:00
value: "!".to_string(),
2021-09-11 12:34:23 -06:00
},
},
})
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-07-23 23:54:01 -06:00
pub struct Span {
pub left: usize,
2021-09-11 12:34:23 -06:00
pub right: usize,
2020-07-23 23:54:01 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-07-23 23:54:01 -06:00
pub struct Spanned<T> {
pub span: Span,
pub value: T,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct FunctionTypeUsage {
pub arguments: Vec<TypeUsage>,
2021-08-07 20:20:11 -06:00
pub return_type: Box<TypeUsage>,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct NamedTypeUsage {
2021-10-08 19:17:07 -06:00
pub type_parameters: GenericUsage,
pub name: Identifier,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct UnknownTypeUsage {
pub name: String,
2021-07-23 08:58:17 -06:00
}
2022-03-12 04:35:48 -07:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NamespaceTypeUsage {
Type(NamedTypeUsage)
// Module
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub enum TypeUsage {
Function(FunctionTypeUsage),
Named(NamedTypeUsage),
Unknown(UnknownTypeUsage),
2022-03-12 04:35:48 -07:00
Namespace(NamespaceTypeUsage)
2020-07-23 23:54:01 -06:00
}
2021-08-07 20:20:11 -06:00
impl TypeUsage {
pub fn new_unknown(id_gen: &IdGenerator) -> TypeUsage {
2021-09-25 11:45:31 -06:00
return TypeUsage::Unknown(UnknownTypeUsage { name: id_gen.next() });
2021-08-07 20:20:11 -06:00
}
2021-10-08 19:17:07 -06:00
pub fn new_named(identifier: &Identifier, generic_usage: &GenericUsage) -> TypeUsage {
return TypeUsage::Named(NamedTypeUsage {
type_parameters: generic_usage.clone(),
name: identifier.clone(),
});
2021-08-07 20:20:11 -06:00
}
pub fn new_builtin(name: String) -> TypeUsage {
2021-09-11 12:34:23 -06:00
TypeUsage::Named(NamedTypeUsage {
type_parameters: GenericUsage::Known(GenericInstantiation { parameters: vec![] }),
2021-09-11 12:34:23 -06:00
name: Identifier {
name: Spanned {
span: Span { left: 0, right: 0 }, //todo: figure out a sane value for these
2021-08-07 20:20:11 -06:00
value: name,
2021-09-11 12:34:23 -06:00
},
},
})
2021-08-07 20:20:11 -06:00
}
pub fn new_function(arg_count: usize, id_gen: &IdGenerator) -> TypeUsage {
2021-09-11 12:34:23 -06:00
return TypeUsage::Function(FunctionTypeUsage {
2021-09-25 11:45:31 -06:00
arguments: (0..arg_count).map(|_| TypeUsage::new_unknown(&id_gen)).collect(),
return_type: Box::new(TypeUsage::new_unknown(&id_gen)),
2021-08-07 20:20:11 -06:00
});
}
}
2021-10-08 19:17:07 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericParameter {
pub name: Identifier,
pub bounds: Vec<Identifier>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Generic {
pub parameters: Vec<GenericParameter>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericInstantiation {
pub parameters: Vec<TypeUsage>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericUsage {
Known(GenericInstantiation),
Unknown,
}
impl GenericUsage {
pub fn new(type_parameters: &[TypeUsage]) -> Self {
GenericUsage::Known(GenericInstantiation {
parameters: type_parameters.iter().map(|tp| tp.clone()).collect(),
2021-10-08 19:17:07 -06:00
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Operator {
Mul,
Div,
Plus,
Minus,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LiteralInt {
pub value: Spanned<String>,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct LiteralFloat {
pub value: Spanned<String>,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
2021-09-14 21:15:39 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LiteralBool {
pub value: Spanned<String>,
pub type_: TypeUsage,
}
2022-10-12 11:06:58 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LiteralString {
pub value: Spanned<String>,
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct LiteralStruct {
2021-10-08 19:17:07 -06:00
pub type_parameters: GenericUsage,
2021-08-07 20:20:11 -06:00
pub name: Identifier,
pub fields: Vec<(Identifier, Expression)>,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Identifier {
2021-07-23 08:58:17 -06:00
pub name: Spanned<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-04-13 23:59:01 -06:00
pub struct FunctionCall {
2021-07-23 08:58:17 -06:00
pub source: Expression,
pub arguments: Vec<Expression>,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct StructGetter {
2021-10-08 19:17:07 -06:00
pub type_parameters: GenericUsage,
2021-08-07 20:20:11 -06:00
pub source: Expression,
2021-07-23 08:58:17 -06:00
pub attribute: Identifier,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2020-04-13 23:59:01 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-07-23 23:54:01 -06:00
pub struct Operation {
2021-07-23 08:58:17 -06:00
pub left: Expression,
2020-07-23 23:54:01 -06:00
pub op: Operator,
2021-07-23 08:58:17 -06:00
pub right: Expression,
2020-07-23 23:54:01 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct VariableUsage {
pub name: Identifier,
2022-03-12 04:35:48 -07:00
pub type_parameters: GenericUsage,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
2021-09-14 21:15:39 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IfExpression {
pub condition: Expression,
pub block: Block,
pub else_: Option<Block>,
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub enum Subexpression {
LiteralInt(LiteralInt),
2021-08-07 20:20:11 -06:00
LiteralFloat(LiteralFloat),
2021-09-14 21:15:39 -06:00
LiteralBool(LiteralBool),
2022-10-12 11:06:58 -06:00
LiteralString(LiteralString),
2021-08-07 20:20:11 -06:00
LiteralStruct(LiteralStruct),
2021-07-23 08:58:17 -06:00
FunctionCall(FunctionCall),
VariableUsage(VariableUsage),
2021-09-14 21:15:39 -06:00
If(IfExpression),
StructGetter(StructGetter),
Block(Block),
2020-07-23 23:54:01 -06:00
Op(Operation),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct Expression {
pub subexpression: Box<Subexpression>,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct ReturnStatement {
pub source: Expression,
}
2021-07-23 08:58:17 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct LetStatement {
pub variable_name: Identifier,
pub expression: Expression,
pub type_: TypeUsage,
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub enum AssignmentTarget {
Variable(VariableUsage),
StructAttr(StructGetter),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct AssignmentStatement {
pub source: AssignmentTarget,
pub expression: Expression,
2020-07-23 23:54:01 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-07-23 23:54:01 -06:00
pub enum Statement {
2021-08-07 20:20:11 -06:00
Return(ReturnStatement),
Let(LetStatement),
Assignment(AssignmentStatement),
2021-07-23 08:58:17 -06:00
Expression(Expression),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Block {
2021-08-07 20:20:11 -06:00
pub statements: Vec<Statement>,
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VariableDeclaration {
2021-07-23 08:58:17 -06:00
pub name: Identifier,
2021-08-07 20:20:11 -06:00
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct FunctionDeclaration {
2021-10-08 19:17:07 -06:00
pub generic: Generic,
2021-07-23 08:58:17 -06:00
pub name: Identifier,
pub arguments: Vec<VariableDeclaration>,
2021-07-23 08:58:17 -06:00
pub return_type: TypeUsage,
}
2021-09-25 11:45:31 -06:00
impl FunctionDeclaration {
pub fn to_type(&self) -> TypeUsage {
TypeUsage::Function(FunctionTypeUsage {
arguments: self.arguments.iter().map(|arg| arg.type_.clone()).collect(),
return_type: Box::new(self.return_type.clone()),
})
}
pub fn to_method_type(&self) -> TypeUsage {
TypeUsage::Function(FunctionTypeUsage {
arguments: self.arguments[1..self.arguments.len()]
.iter()
.map(|arg| arg.type_.clone())
.collect(),
return_type: Box::new(self.return_type.clone()),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct Function {
pub declaration: FunctionDeclaration,
pub block: Block,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct PrimitiveTypeDeclaration {
pub name: String, // cannot be identifier as it's not declared anywhere specific, it's builtins
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-08-07 20:20:11 -06:00
pub struct StructField {
pub name: Identifier,
pub type_: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct StructTypeDeclaration {
2021-10-08 19:17:07 -06:00
pub generic: Generic,
2021-07-23 08:58:17 -06:00
pub name: Identifier,
2021-08-07 20:20:11 -06:00
pub fields: Vec<StructField>,
2021-07-23 08:58:17 -06:00
}
2021-09-25 11:45:31 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TraitItem {
FunctionDeclaration(FunctionDeclaration),
Function(Function),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TraitTypeDeclaration {
2021-10-08 19:17:07 -06:00
pub generic: Generic,
2021-09-25 11:45:31 -06:00
pub name: Identifier,
pub functions: Vec<TraitItem>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct AliasTypeDeclaration {
pub name: Identifier,
pub replaces: TypeUsage,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub enum TypeDeclaration {
Struct(StructTypeDeclaration),
Primitive(PrimitiveTypeDeclaration),
Alias(AliasTypeDeclaration),
2021-09-25 11:45:31 -06:00
Trait(TraitTypeDeclaration),
2021-07-23 08:58:17 -06:00
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-08-07 20:20:11 -06:00
pub struct Impl {
2021-10-08 19:17:07 -06:00
pub generic: Generic,
pub struct_: NamedTypeUsage,
pub trait_: Option<NamedTypeUsage>,
2021-07-23 08:58:17 -06:00
pub functions: Vec<Function>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-08-07 20:20:11 -06:00
pub enum ModuleItem {
Function(Function),
TypeDeclaration(TypeDeclaration),
Impl(Impl),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2020-04-13 23:59:01 -06:00
pub struct Module {
2021-08-07 20:20:11 -06:00
pub items: Vec<ModuleItem>,
}