//! This is the module that contains relevant code to parsing Reid, that is to //! say transforming a Vec of FullTokens into a loose parsed AST that can be //! used for unwrapping syntax sugar, and then be transformed into Reid MIR. use std::path::PathBuf; use token_stream::TokenRange; use crate::lexer::FullToken; pub mod lexer; pub mod parse; pub mod process; pub mod token_stream; #[derive(Debug, Clone)] pub struct Type(pub TypeKind, pub TokenRange); #[derive(Debug, Clone)] pub enum TypeKind { Bool, I8, I16, I32, I64, I128, U8, U16, U32, U64, U128, F16, F32B, F32, F64, F128, F80, F128PPC, Char, Array(Box, u64), Custom(String), Borrow(Box, bool), Ptr(Box), Unknown, } #[derive(Debug, Clone)] pub enum Literal { Integer(u128), Decimal(f64), Bool(bool), String(String), Char(char), Specific(SpecificLiteral), } #[derive(Debug, Clone)] pub enum SpecificLiteral { I8(i8), I16(i16), I32(i32), I64(i64), I128(i128), U8(u8), U16(u16), U32(u32), U64(u64), U128(u128), F16(f32), F32(f32), F32B(f32), F64(f64), F80(f64), F128(f64), F128PPC(f64), } #[derive(Debug, Clone)] pub struct Expression(pub ExpressionKind, pub TokenRange); #[derive(Debug, Clone)] pub enum ExpressionKind { VariableName(String), Borrow(String, bool), Deref(String), Literal(Literal), Array(Vec), ArrayShort(Box, u64), /// Array-indexed, e.g. [] Indexed(Box, Box), /// Struct-accessed, e.g. . Accessed(Box, String), /// Associated function call, but with a shorthand AccessCall(Box, Box), Binop(BinaryOperator, Box, Box), FunctionCall(Box), AssociatedFunctionCall(Type, Box), BlockExpr(Box), IfExpr(Box), StructExpression(StructExpression), UnaryOperation(UnaryOperator, Box), CastTo(Box, Type), } #[derive(Debug, Clone)] pub enum UnaryOperator { Plus, Minus, Not, } #[derive(Debug, Clone, Copy)] pub enum BinaryOperator { Add, Minus, Mult, Div, Mod, And, LT, LE, GT, GE, EQ, NE, } impl BinaryOperator { pub fn get_precedence(&self) -> i8 { use BinaryOperator::*; match &self { Minus => 5, Add => 10, Mult => 15, Div => 20, Mod => 20, And => 100, LT => 100, LE => 100, GT => 100, GE => 100, EQ => 100, NE => 100, } } } #[derive(Debug, Clone)] pub struct FunctionCallExpression(pub String, pub Vec, pub TokenRange); #[derive(Debug, Clone)] pub struct IfExpression( pub Expression, pub Expression, pub Option, #[allow(dead_code)] pub TokenRange, ); #[derive(Debug, Clone)] pub struct LetStatement { pub name: String, pub ty: Option, pub mutable: bool, pub value: Expression, pub name_range: TokenRange, } #[derive(Debug, Clone)] pub struct ImportStatement(pub Vec, pub TokenRange); #[derive(Debug)] pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub TokenRange); #[derive(Debug, Clone)] pub struct FunctionSignature { pub name: String, pub self_kind: SelfKind, pub params: Vec<(String, Type)>, pub return_type: Option, #[allow(dead_code)] pub range: TokenRange, } #[derive(Debug, Clone)] pub enum SelfKind { Owned(TypeKind), Borrow(TypeKind), MutBorrow(TypeKind), None, } #[derive(Debug, Clone, Copy)] pub enum ReturnType { Soft, Hard, } #[derive(Debug, Clone)] pub struct StructExpression { name: String, fields: Vec<(String, Expression)>, range: TokenRange, } #[derive(Debug, Clone)] pub struct Block( pub Vec, pub Option<(ReturnType, Option)>, pub TokenRange, ); #[derive(Debug, Clone)] pub enum BlockLevelStatement { Let(LetStatement), /// Try to set a variable to a specified expression value Set(Expression, Expression, TokenRange), Import { _i: ImportStatement, }, Expression(Expression), Return(ReturnType, Option), ForLoop(String, TokenRange, Expression, Expression, Block), WhileLoop(Expression, Block), } #[derive(Debug, Clone)] pub struct TypeDefinition { name: String, kind: TypeDefinitionKind, range: TokenRange, } #[derive(Debug, Clone)] pub enum TypeDefinitionKind { Struct(Vec), } #[derive(Debug, Clone)] pub struct StructDefinitionField { name: String, ty: Type, range: TokenRange, } #[derive(Debug)] pub enum TopLevelStatement { Import(ImportStatement), ExternFunction(FunctionSignature), FunctionDefinition(FunctionDefinition), TypeDefinition(TypeDefinition), BinopDefinition(BinopDefinition), AssociatedFunction(Type, Vec), } #[derive(Debug)] pub struct BinopDefinition { pub lhs: (String, Type), pub op: BinaryOperator, pub rhs: (String, Type), pub return_ty: Type, pub block: Block, pub signature_range: TokenRange, } #[derive(Debug)] pub struct Module { pub name: String, pub tokens: Vec, pub top_level_statements: Vec, pub path: Option, pub is_main: bool, }