Files
vulcan/forge_core/src/ast.rs
T

231 lines
4.2 KiB
Rust
Raw Normal View History

2023-07-08 20:54:23 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct Program(pub Vec<Declaration>);
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub enum Declaration {
Function(Function),
Global(Global),
Struct(Struct),
Const(Const),
}
#[derive(PartialEq, Clone, Debug, Default)]
2023-07-03 23:11:44 -05:00
pub struct Varinfo {
pub typename: Option<String>,
2023-08-02 21:56:26 -05:00
pub size: Option<Expr>,
2023-07-03 23:11:44 -05:00
}
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub struct Global {
pub name: String,
pub typename: Option<String>,
2023-08-02 21:56:26 -05:00
pub size: Option<Expr>,
2023-07-03 23:11:44 -05:00
}
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub struct Struct {
pub name: String,
pub members: Vec<Member>,
}
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub struct Member {
pub name: String,
pub typename: Option<String>,
2023-08-02 21:56:26 -05:00
pub size: Option<Expr>,
2023-07-03 23:11:44 -05:00
}
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub struct Const {
pub name: String,
2023-08-02 21:56:26 -05:00
pub value: Option<Expr>,
2023-07-03 23:11:44 -05:00
pub string: Option<String>,
}
2023-07-08 20:54:23 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct Block(pub Vec<Statement>);
#[derive(PartialEq, Clone, Debug)]
2023-07-03 23:11:44 -05:00
pub struct Function {
pub name: String,
pub args: Vec<Argname>,
2023-07-08 20:54:23 -05:00
pub body: Block,
2023-07-03 23:11:44 -05:00
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Argname {
pub name: String,
pub typename: Option<String>,
}
2023-07-04 02:49:29 -05:00
2023-07-08 19:09:32 -05:00
#[derive(PartialEq, Clone, Debug)]
2023-07-04 02:49:29 -05:00
pub enum Statement {
Return(Return),
Assignment(Assignment),
2023-08-02 21:56:26 -05:00
Expr(Expr),
2023-07-04 02:49:29 -05:00
VarDecl(VarDecl),
Conditional(Conditional),
WhileLoop(WhileLoop),
RepeatLoop(RepeatLoop),
}
2023-07-08 19:09:32 -05:00
#[derive(PartialEq, Clone, Debug)]
2023-08-02 21:56:26 -05:00
pub struct Return(pub Option<Expr>);
2023-07-04 02:49:29 -05:00
2023-07-08 19:09:32 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct Assignment {
pub lvalue: Lvalue,
pub rvalue: Rvalue,
}
2023-07-04 02:49:29 -05:00
2023-07-08 19:09:32 -05:00
#[derive(PartialEq, Clone, Debug)]
2023-08-04 17:27:28 -05:00
pub struct Lvalue {
pub name: String,
pub subscripts: Vec<Suffix>,
}
impl From<&str> for Lvalue {
fn from(name: &str) -> Self {
Self { name: String::from(name), subscripts: vec![] }
}
}
impl From<String> for Lvalue {
fn from(name: String) -> Self {
Self { name, subscripts: vec![] }
}
2023-07-08 19:09:32 -05:00
}
#[derive(PartialEq, Clone, Debug)]
pub enum Rvalue {
2023-08-02 21:56:26 -05:00
Expr(Expr),
2023-07-08 19:09:32 -05:00
String(String),
}
#[derive(PartialEq, Clone, Debug)]
pub struct VarDecl {
pub name: String,
pub typename: Option<String>,
2023-08-02 21:56:26 -05:00
pub size: Option<Expr>,
2023-08-04 17:27:28 -05:00
pub initial: Option<Rvalue>,
2023-07-08 19:09:32 -05:00
}
2023-07-04 02:49:29 -05:00
2023-07-08 20:54:23 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct Conditional {
2023-08-02 21:56:26 -05:00
pub condition: Expr,
2023-07-08 20:54:23 -05:00
pub body: Block,
pub alternative: Option<Block>,
}
2023-07-04 02:49:29 -05:00
2023-07-08 20:54:23 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct WhileLoop {
2023-08-02 21:56:26 -05:00
pub condition: Expr,
2023-07-08 20:54:23 -05:00
pub body: Block,
}
2023-07-04 02:49:29 -05:00
2023-07-08 20:54:23 -05:00
#[derive(PartialEq, Clone, Debug)]
pub struct RepeatLoop {
2023-08-02 21:56:26 -05:00
pub count: Expr,
2023-07-08 20:54:23 -05:00
pub name: Option<String>,
pub body: Block,
}
2023-07-04 02:49:29 -05:00
/// One of the five arithmetical operators
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Operator {
Add,
Sub,
Mul,
Div,
Mod,
2023-07-08 23:08:22 -05:00
And,
Or,
BitAnd,
BitOr,
Xor,
Lt,
Le,
Gt,
Ge,
Eq,
Ne,
Lshift,
Rshift,
2023-07-04 02:49:29 -05:00
}
2023-07-08 23:33:30 -05:00
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Prefix {
Neg,
Not,
2023-08-02 21:56:26 -05:00
Address
}
#[derive(Debug, PartialEq, Clone)]
pub enum Suffix {
Subscript(BoxExpr),
2023-08-02 21:56:26 -05:00
Arglist(Vec<Rvalue>),
Member(String),
2023-07-08 23:33:30 -05:00
}
#[derive(Debug, PartialEq, Clone)]
pub enum Expr {
Number(i32),
Name(String),
Expr(BoxExpr),
2023-08-04 17:27:28 -05:00
Prefix(Prefix, BoxExpr),
Suffix(BoxExpr, Suffix),
Infix(BoxExpr, Operator, BoxExpr)
2023-07-04 02:49:29 -05:00
}
2023-07-05 23:19:54 -05:00
#[repr(transparent)]
#[derive(PartialEq, Clone, Debug)]
2023-08-02 21:56:26 -05:00
pub struct BoxExpr(pub Box<Expr>);
impl From<i32> for BoxExpr {
2023-07-05 23:19:54 -05:00
fn from(val: i32) -> Self {
2023-08-04 17:27:28 -05:00
BoxExpr(Box::from(Expr::Number(val)))
2023-07-05 23:19:54 -05:00
}
}
2023-08-02 21:56:26 -05:00
impl From<Expr> for BoxExpr {
fn from(expr: Expr) -> Self {
BoxExpr(Box::from(expr))
}
}
impl From<&str> for BoxExpr {
fn from(value: &str) -> Self {
2023-08-04 17:27:28 -05:00
Expr::Name(String::from(value)).into()
2023-07-05 23:19:54 -05:00
}
}
2023-08-02 21:56:26 -05:00
impl From<BoxExpr> for Expr {
fn from(expr: BoxExpr) -> Self {
*(expr.0)
2023-07-05 23:19:54 -05:00
}
}
2023-08-02 21:56:26 -05:00
impl From<i32> for Expr {
fn from(value: i32) -> Self {
2023-08-04 17:27:28 -05:00
Self::Number(value)
2023-08-02 21:56:26 -05:00
}
}
impl From<&str> for Expr {
fn from(value: &str) -> Self {
2023-08-04 17:27:28 -05:00
Self::Name(String::from(value))
}
}
impl From<i32> for Rvalue {
fn from(value: i32) -> Self {
Self::Expr(value.into())
}
}
impl From<&str> for Rvalue {
fn from(value: &str) -> Self {
Self::String(value.into())
}
2023-07-05 23:45:04 -05:00
}