2021-08-08 11:42:26 -06:00
|
|
|
use std::cell::RefCell;
|
2021-04-18 08:51:21 -06:00
|
|
|
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct IdGenerator {
|
2021-08-08 11:42:26 -06:00
|
|
|
counter: RefCell<i64>,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IdGenerator {
|
|
|
|
|
pub fn new() -> Self {
|
2021-09-11 12:34:23 -06:00
|
|
|
IdGenerator {
|
|
|
|
|
counter: RefCell::new(0),
|
|
|
|
|
}
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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-08-08 11:42:26 -06:00
|
|
|
})
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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-08-08 11:42:26 -06:00
|
|
|
})
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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,
|
|
|
|
|
}
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct NamedTypeUsage {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub name: Identifier,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct UnknownTypeUsage {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub name: String,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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 {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub fn new_unknown(id_gen: &IdGenerator) -> TypeUsage {
|
2021-09-11 12:34:23 -06:00
|
|
|
return TypeUsage::Unknown(UnknownTypeUsage {
|
2021-08-07 20:20:11 -06:00
|
|
|
name: id_gen.next(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
pub fn new_named(identifier: Identifier) -> TypeUsage {
|
2021-09-11 12:34:23 -06:00
|
|
|
return TypeUsage::Named(NamedTypeUsage {
|
2021-08-07 20:20:11 -06:00
|
|
|
name: identifier.clone(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08 11:42:26 -06:00
|
|
|
})
|
2021-08-07 20:20:11 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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 {
|
|
|
|
|
arguments: (0..arg_count)
|
|
|
|
|
.map(|_| TypeUsage::new_unknown(&id_gen))
|
|
|
|
|
.collect(),
|
2021-08-08 11:42:26 -06:00
|
|
|
return_type: Box::new(TypeUsage::new_unknown(&id_gen)),
|
2021-08-07 20:20:11 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub enum Operator {
|
|
|
|
|
Mul,
|
|
|
|
|
Div,
|
|
|
|
|
Plus,
|
|
|
|
|
Minus,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub struct LiteralInt {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub value: Spanned<String>,
|
2021-08-07 20:20:11 -06:00
|
|
|
pub type_: TypeUsage,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct LiteralFloat {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub value: Spanned<String>,
|
2021-08-07 20:20:11 -06:00
|
|
|
pub type_: TypeUsage,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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,
|
2021-08-08 11:42:26 -06:00
|
|
|
pub fields: Vec<(Identifier, Expression)>,
|
2021-08-07 20:20:11 -06:00
|
|
|
pub type_: TypeUsage,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub struct Identifier {
|
2021-07-23 08:58:17 -06:00
|
|
|
pub name: Spanned<String>,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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-08-08 11:42:26 -06:00
|
|
|
#[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),
|
|
|
|
|
LiteralStruct(LiteralStruct),
|
2021-07-23 08:58:17 -06:00
|
|
|
FunctionCall(FunctionCall),
|
2021-08-08 11:42:26 -06:00
|
|
|
VariableUsage(VariableUsage),
|
|
|
|
|
StructGetter(StructGetter),
|
|
|
|
|
Block(Block),
|
2020-07-23 23:54:01 -06:00
|
|
|
Op(Operation),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct Expression {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub subexpression: Box<Subexpression>,
|
2021-08-07 20:20:11 -06:00
|
|
|
pub type_: TypeUsage,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct ReturnStatement {
|
|
|
|
|
pub source: Expression,
|
2021-08-08 11:42:26 -06:00
|
|
|
}
|
2021-07-23 08:58:17 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct LetStatement {
|
2021-08-08 11:42:26 -06:00
|
|
|
pub variable_name: Identifier,
|
|
|
|
|
pub expression: Expression,
|
|
|
|
|
pub type_: TypeUsage,
|
2021-07-23 08:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub enum AssignmentTarget {
|
|
|
|
|
Variable(VariableUsage),
|
|
|
|
|
StructAttr(StructGetter),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -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),
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub struct Block {
|
2021-08-07 20:20:11 -06:00
|
|
|
pub statements: Vec<Statement>,
|
|
|
|
|
pub type_: TypeUsage,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
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,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct FunctionDeclaration {
|
|
|
|
|
pub name: Identifier,
|
2020-04-08 00:04:36 -06:00
|
|
|
pub arguments: Vec<VariableDeclaration>,
|
2021-07-23 08:58:17 -06:00
|
|
|
pub return_type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct Function {
|
|
|
|
|
pub declaration: FunctionDeclaration,
|
2020-04-08 00:04:36 -06:00
|
|
|
pub block: Block,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-08-07 20:20:11 -06:00
|
|
|
pub struct StructField {
|
|
|
|
|
pub name: Identifier,
|
|
|
|
|
pub type_: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct AliasTypeDeclaration {
|
|
|
|
|
pub name: Identifier,
|
|
|
|
|
pub replaces: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub enum TypeDeclaration {
|
|
|
|
|
Struct(StructTypeDeclaration),
|
|
|
|
|
Primitive(PrimitiveTypeDeclaration),
|
|
|
|
|
Alias(AliasTypeDeclaration),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-08-07 20:20:11 -06:00
|
|
|
pub struct Impl {
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct_name: Identifier,
|
|
|
|
|
pub functions: Vec<Function>,
|
|
|
|
|
}
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2021-08-07 20:20:11 -06:00
|
|
|
pub enum ModuleItem {
|
|
|
|
|
Function(Function),
|
|
|
|
|
TypeDeclaration(TypeDeclaration),
|
|
|
|
|
Impl(Impl),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 11:42:26 -06:00
|
|
|
#[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>,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|