Files
boring-lang/src/ast.rs

333 lines
7.9 KiB
Rust
Raw Normal View History

use std::cell::RefCell;
2021-04-18 08:51:21 -06:00
2021-07-23 08:58:17 -06:00
pub struct IdGenerator {
counter: RefCell<i64>,
2021-07-23 08:58:17 -06:00
}
impl IdGenerator {
pub fn new() -> Self {
2021-09-25 11:45:31 -06:00
IdGenerator { counter: RefCell::new(0) }
2021-07-23 08:58:17 -06:00
}
pub fn next(&self) -> String {
*self.counter.borrow_mut() += 1;
("S".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 {
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 {
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 {
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
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub enum TypeUsage {
Function(FunctionTypeUsage),
Named(NamedTypeUsage),
Unknown(UnknownTypeUsage),
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
}
pub fn new_named(identifier: Identifier) -> TypeUsage {
2021-09-25 11:45:31 -06:00
return TypeUsage::Named(NamedTypeUsage { 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 {
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
});
}
}
#[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,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2021-07-23 08:58:17 -06:00
pub struct LiteralStruct {
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-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,
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),
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 {
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 {
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 {
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-09-25 11:45:31 -06:00
pub trait_: Option<Identifier>,
2021-07-23 08:58:17 -06:00
pub struct_name: Identifier,
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>,
}