use crate::symtab::Identifier;
use serde_derive::Serialize;
#[derive(Debug, Serialize)]
pub struct Array {
data: Vec<Primitive>,
}
#[derive(Debug, Serialize)]
pub enum ScopeType {
Fn,
Class,
Data,
Trait,
Mod,
Anonymous,
If,
Loop,
}
#[derive(Debug, Serialize)]
pub enum Primitive {
Int32(i32),
Int64(i64),
Int128(i128),
UInt32(u32),
UInt64(u64),
UInt128(i128),
Float32(f32),
Float64(f64),
Bool(bool),
Char(u8),
Byte(u8),
}
#[derive(Debug, Serialize)]
pub enum ReiType {
Primitive(Primitive),
Array(Array),
}
#[derive(Debug)]
pub struct Content {
pub expressions: Vec<Expr>,
}
impl Content {
pub fn new(expressions: Vec<Expr>) -> Self {
Self { expressions }
}
}
impl Default for Content {
fn default() -> Self {
Self {
expressions: Default::default(),
}
}
}
#[derive(Debug)]
pub enum Expr {
Literal(Primitive),
Identifier(Identifier),
VarDef(Box<VarDef>),
VarRedef(Box<VarRedef>),
UnaryOp(Box<UnaryOp>),
BinaryOp(Box<BinaryOp>),
UseExpr(Box<UseExpr>),
If(Box<If>),
Loop(Box<Loop>),
Fn(Box<Fn>),
ParamList(Box<ParamList>),
ArgList(Box<ArgList>),
CallArg(Box<CallArg>),
FnCall(Box<FnCall>),
Class(Box<Class>),
Data(Box<Data>),
DataFieldDef(Box<DataFieldDef>),
Trait(Box<Trait>),
AnonScope(Box<AnonScope>),
ExportExpr(Box<ExportExpr>),
ModExpr(Box<ModExpr>),
AnnotationExpr(Box<AnnotationExpr>),
Empty,
}
macro_rules! into_expr {
($id:ident) => {
impl Into<Expr> for $id {
fn into(self) -> Expr {
Expr::$id(Box::new(self))
}
}
};
}
#[derive(Debug)]
pub struct AnnotationExpr {
pub annotated_expr: Expr,
}
#[derive(Debug)]
pub struct ExportExpr {
pub item: Expr,
}
#[derive(Debug)]
pub struct ModExpr {
pub body: Expr,
}
into_expr!(ModExpr);
#[derive(Debug, Clone, Copy)]
pub enum TypeModifier {
Let,
Mut,
Const,
Static,
}
#[derive(Debug)]
pub struct VarDef {
pub var_type_modifier: TypeModifier,
pub lhs: Expr,
pub rhs: Expr,
}
#[derive(Debug)]
pub struct VarRedef {
pub lhs: Expr,
pub rhs: Expr,
}
into_expr!(VarRedef);
#[derive(Debug)]
pub struct PartialExpr {
pub expr: Expr,
}
#[derive(Debug)]
pub struct AnonScope {
pub expr: Expr,
}
into_expr!(AnonScope);
#[derive(Debug, Clone, Copy)]
pub enum ReservedOperator {
DollarSign,
Annotation,
Hash,
Eq,
Dot,
Comma,
QuestionMark,
Colon,
Semicolon,
DoubleColon,
}
#[derive(Debug, Clone, Copy)]
pub enum OverloableUnaryOperator {
PlusPlus,
MinusMinus,
Exclamation,
}
#[derive(Debug, Clone, Copy)]
pub enum OverloableBinaryOperator {
Equiv,
Lt,
Gt,
Gte,
Lte,
And,
Or,
Mult,
Div,
Add,
Sub,
Modulo,
PlusEq,
MinusEq,
MultEq,
DivEq,
DoubleMult,
DoubleDiv,
LeftShift,
RightShift,
BitwiseAnd,
BitwiseOr,
BitwiseNot,
BitwiseXor,
}
#[derive(Debug)]
pub struct UnaryOp {
pub kind: OverloableUnaryOperator,
pub lhs: Expr,
}
into_expr!(UnaryOp);
#[derive(Debug)]
pub struct BinaryOp {
pub kind: OverloableBinaryOperator,
pub lhs: Expr,
pub rhs: Expr,
}
into_expr!(BinaryOp);
#[derive(Debug)]
pub struct Fn {
pub export: bool,
pub fn_name: Identifier,
pub fn_return_type: ReiType,
pub args: ParamList,
pub body: Expr,
}
pub type DefaultParam = Option<Identifier>;
pub type ParamName = Identifier;
pub type ParamType = Identifier;
#[derive(Debug)]
pub struct ParamList {
pub params: Vec<(ParamName, ParamType, DefaultParam)>,
}
#[derive(Debug)]
pub struct ArgList {
pub args: Vec<Identifier>,
}
into_expr!(ArgList);
into_expr!(ParamList);
into_expr!(Fn);
#[derive(Debug)]
pub struct Type {
pub export: bool,
pub ident: Identifier,
pub rhs: Expr,
}
#[derive(Debug)]
pub struct If {
pub condition: Expr,
pub true_body: Expr,
pub false_body: Expr,
}
into_expr!(If);
#[derive(Debug)]
pub struct Class {
pub export: bool,
pub class_name: Identifier,
pub body: Expr,
pub implements: Vec<*mut Trait>,
pub inherits: Vec<*mut Class>,
}
into_expr!(Class);
pub type Namespace = String;
pub type UseItems = String;
#[derive(Debug)]
pub struct UseExpr {
pub namespace: Namespace,
pub items: Vec<UseItems>,
}
into_expr!(UseExpr);
#[derive(Debug)]
pub struct CallArg {
pub arg: Identifier,
pub keyword_param: Option<Identifier>,
}
into_expr!(CallArg);
#[derive(Debug)]
pub struct FnCall {
pub fn_name: Identifier,
pub args: ArgList,
}
into_expr!(FnCall);
#[derive(Debug)]
pub struct Data {
pub export: bool,
pub data_name: Identifier,
pub fields: Vec<DataFieldDef>,
}
#[derive(Debug)]
pub struct DataFieldDef {
pub ident: Identifier,
pub def: Expr,
}
into_expr!(Data);
into_expr!(DataFieldDef);
#[derive(Debug)]
pub struct Trait {
pub export: bool,
pub trait_name: Identifier,
pub fields: Vec<TraitExpr>,
}
#[derive(Debug)]
pub enum TraitExpr {
TraitFnDec(Fn),
Type(Type),
}
#[derive(Debug)]
pub struct Loop {
pub condition: BoolExpr,
pub body: Expr,
}
#[derive(Debug)]
pub struct BoolExpr {
pub boolean_expr: Expr,
}
into_expr!(Loop);