2021-07-23 08:58:17 -06:00
|
|
|
use std::collections::HashMap;
|
2021-04-18 08:51:21 -06:00
|
|
|
use crate::types;
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct IdGenerator {
|
|
|
|
|
counter: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IdGenerator {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
IdGenerator{counter: 0}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self) -> i64 {
|
|
|
|
|
self.counter += 1;
|
|
|
|
|
self.counter
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_named(name: String) -> {
|
|
|
|
|
ast::TypeUsage::Named(ast::NamedTypeUsage{
|
|
|
|
|
name: ast::Identifier{
|
|
|
|
|
name: ast::Spanned{
|
|
|
|
|
span: ast::Span{left: 0, right: 0}, //todo: figure out a sane value for these
|
|
|
|
|
value: name,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_unit() -> {
|
|
|
|
|
ast::TypeUsage::Named(ast::NamedTypeUsage{
|
|
|
|
|
name: ast::Identifier{
|
|
|
|
|
name: ast::Spanned{
|
|
|
|
|
span: ast::Span{left: 0, right: 0}, //todo: figure out a sane value for these
|
|
|
|
|
value: "()".to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_never() -> {
|
|
|
|
|
ast::TypeUsage::Named(ast::NamedTypeUsage{
|
|
|
|
|
name: ast::Identifier{
|
|
|
|
|
name: ast::Spanned{
|
|
|
|
|
span: ast::Span{left: 0, right: 0}, //todo: figure out a sane value for these
|
|
|
|
|
value: "!".to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-07-23 23:54:01 -06:00
|
|
|
pub struct Span {
|
|
|
|
|
pub left: usize,
|
|
|
|
|
pub right: usize
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(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-07-23 08:58:17 -06:00
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct FunctionTypeUsage {
|
|
|
|
|
pub arguments: Vec<TypeUsage>,
|
|
|
|
|
pub return_type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct NamedTypeUsage {
|
|
|
|
|
name: Identifier,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct UnknownTypeUsage {
|
|
|
|
|
name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum TypeUsage {
|
|
|
|
|
Function(FunctionTypeUsage),
|
|
|
|
|
Named(NamedTypeUsage),
|
|
|
|
|
Unknown(UnknownTypeUsage),
|
2020-07-23 23:54:01 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub enum Operator {
|
|
|
|
|
Mul,
|
|
|
|
|
Div,
|
|
|
|
|
Plus,
|
|
|
|
|
Minus,
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub struct LiteralInt {
|
2021-07-23 08:58:17 -06:00
|
|
|
pub value: Spanned<i64>,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct LiteralFloat {
|
|
|
|
|
pub value: Spanned<f64>,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct LiteralStruct {
|
|
|
|
|
pub fields: HashMap<Identifier, Expression>,
|
|
|
|
|
pub type: TypeUsage,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(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-04-18 08:51:21 -06:00
|
|
|
#[derive(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>,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct StructGetter {
|
|
|
|
|
pub source: Box<Expression>,
|
|
|
|
|
pub attribute: Identifier,
|
|
|
|
|
pub type: TypeUsage,
|
2020-04-13 23:59:01 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(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,
|
|
|
|
|
pub type: TypeUsage,
|
2020-07-23 23:54:01 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct VariableUsage {
|
|
|
|
|
pub name: Identifier,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum Subexpression {
|
|
|
|
|
LiteralInt(LiteralInt),
|
|
|
|
|
FunctionCall(FunctionCall),
|
|
|
|
|
Identifier(Identifier),
|
2020-07-23 23:54:01 -06:00
|
|
|
Op(Operation),
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2021-07-23 08:58:17 -06:00
|
|
|
pub struct Expression {
|
|
|
|
|
pub subexpression: Spanned<Box<Subexpression>>,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct ReturnStatement {
|
|
|
|
|
pub source: Expression,
|
|
|
|
|
pub type: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct LetStatement {
|
|
|
|
|
variable_name: Identifier,
|
|
|
|
|
type: VariableUsage,
|
|
|
|
|
expression: Expression,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum AssignmentTarget {
|
|
|
|
|
Variable(VariableUsage),
|
|
|
|
|
StructAttr(StructGetter),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct AssignmentStatement {
|
|
|
|
|
pub source: AssignmentTarget,
|
|
|
|
|
pub expression: Expression,
|
|
|
|
|
pub type: TypeUsage,
|
2020-07-23 23:54:01 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-07-23 23:54:01 -06:00
|
|
|
pub enum Statement {
|
2021-07-23 08:58:17 -06:00
|
|
|
Assignment(Assignment),
|
|
|
|
|
Expression(Expression),
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-04-08 00:04:36 -06:00
|
|
|
pub struct Block {
|
2020-07-23 23:54:01 -06:00
|
|
|
pub statements: Vec<Spanned<Statement>>,
|
2021-07-23 08:58:17 -06:00
|
|
|
pub type: TypeUsage,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(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,
|
|
|
|
|
pub type: TypeUsage,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct Function {
|
|
|
|
|
pub declaration: FunctionDeclaration,
|
2020-04-08 00:04:36 -06:00
|
|
|
pub block: Block,
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 08:58:17 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct PrimitiveTypeDeclaration {
|
|
|
|
|
pub name: String, // cannot be identifier as it's not declared anywhere specific, it's builtins
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct StructTypeDeclaration {
|
|
|
|
|
pub name: Identifier,
|
|
|
|
|
pub fields: HashMap<Identifier, TypeUsage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct AliasTypeDeclaration {
|
|
|
|
|
pub name: Identifier,
|
|
|
|
|
pub replaces: TypeUsage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum TypeDeclaration {
|
|
|
|
|
Struct(StructTypeDeclaration),
|
|
|
|
|
Primitive(PrimitiveTypeDeclaration),
|
|
|
|
|
Alias(AliasTypeDeclaration),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum Impl {
|
|
|
|
|
pub struct_name: Identifier,
|
|
|
|
|
pub functions: Vec<Function>,
|
|
|
|
|
}
|
2020-04-08 00:04:36 -06:00
|
|
|
|
2021-04-18 08:51:21 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2020-04-13 23:59:01 -06:00
|
|
|
pub struct Module {
|
2020-04-08 00:04:36 -06:00
|
|
|
pub functions: Vec<Function>,
|
2021-07-23 08:58:17 -06:00
|
|
|
pub types: Vec<TypeDeclaration>,
|
|
|
|
|
pub impls: Vec<Impls>,
|
2020-04-08 00:04:36 -06:00
|
|
|
}
|