Ast: remove ast.rs file
All checks were successful
ci/woodpecker/push/build_rust Pipeline was successful

This commit is contained in:
Akemi Izuko 2023-11-17 14:09:16 -07:00
parent 2abb04f4bb
commit 1176c5149d
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

View file

@ -1,243 +0,0 @@
use std::string::ToString;
pub trait AstNode: ToString {}
pub trait Expr: AstNode {
fn get_base(&self) -> BaseType;
}
pub trait Statement: AstNode {}
#[derive(Copy, Clone)]
pub enum Quantifier {
Const,
Var,
Unset,
}
impl Default for Quantifier {
fn default() -> Self {
Quantifier::Unset
}
}
impl ToString for Quantifier {
fn to_string(&self) -> String {
match *self {
Quantifier::Const => "const",
Quantifier::Var => "var",
Quantifier::Unset => panic!("Found unset quantifier"),
}.to_string()
}
}
#[derive(Copy, Clone)]
pub enum BaseType {
Int,
Real,
Never,
Unset,
}
impl Default for BaseType {
fn default() -> Self {
BaseType::Unset
}
}
impl ToString for BaseType {
fn to_string(&self) -> String {
// TODO: Use typedefs when possible. Might fit better on a high level node
match *self {
BaseType::Int => "integer",
BaseType::Real => "real",
BaseType::Never => panic!("Attempting to get string of never type"),
BaseType::Unset => panic!("Found unset basetype"),
}.to_string()
}
}
#[derive(Default)]
pub struct GlobalBlock {
statements: Vec<Box<dyn Statement>>,
}
impl GlobalBlock {
pub fn push(&mut self, node: Box<dyn Statement>) {
self.statements.push(node);
}
}
impl ToString for GlobalBlock {
fn to_string(&self) -> String {
let mut s = String::new();
for stat in &self.statements {
s.push_str(&stat.to_string());
}
s.push('\n');
s
}
}
pub struct Block {
statements: Vec<Box<dyn Statement>>,
}
impl Block {
pub fn push(&mut self, node: Box<dyn Statement>) {
self.statements.push(node);
}
}
impl ToString for Block {
fn to_string(&self) -> String {
let mut s = String::from("{ ");
for stat in &self.statements {
s.push_str(&stat.to_string());
s.push(' ');
}
s.push('}');
s
}
}
impl AstNode for Block {}
impl Statement for Block {}
#[derive(Debug, Clone)]
pub enum Literal {
Int(i32),
Real(f32),
}
impl ToString for Literal {
fn to_string(&self) -> String {
match *self {
Literal::Int(x) => x.to_string(),
Literal::Real(x) => x.to_string(),
}
}
}
impl AstNode for Literal {}
impl Expr for Literal {
fn get_base(&self) -> BaseType {
match *self {
Literal::Int(_) => BaseType::Int,
Literal::Real(_) => BaseType::Real,
}
}
}
#[derive(Default, Builder)]
pub struct Declaration <T: Expr> {
variable: Variable,
assn: T,
}
impl<T: Expr> ToString for Declaration<T> {
fn to_string(&self) -> String {
let mut s = String::new();
s.push_str(&self.variable.quantifer.to_string());
s.push(' ');
s.push_str(&self.variable.get_base().to_string());
s.push(' ');
s.push_str(&self.variable.to_string());
s.push_str(" = ");
s.push_str(&self.assn.to_string());
s.push(';');
s
}
}
impl<T: Expr> AstNode for Declaration<T> {}
impl<T: Expr> Statement for Declaration<T> {}
#[derive(Clone, Default, Builder)]
#[builder(setter(into))]
pub struct Variable {
type_: BaseType,
quantifer: Quantifier,
name: String,
}
impl ToString for Variable {
fn to_string(&self) -> String {
self.name.clone()
}
}
impl AstNode for Variable {}
impl Expr for Variable {
fn get_base(&self) -> BaseType {
self.type_
}
}
#[derive(Debug, Clone)]
pub enum BinaryOperator <L: Expr, R: Expr>
{
Add(L, R),
Subtract(L, R),
Multiply(L, R),
Divide(L, R),
}
impl<L: Expr, R: Expr> ToString for BinaryOperator<L, R> {
fn to_string(&self) -> String {
let mut s = String::new();
s.push('(');
match self {
BinaryOperator::Add(l, _) => s.push_str(&l.to_string()),
BinaryOperator::Subtract(l, _) => s.push_str(&l.to_string()),
BinaryOperator::Multiply(l, _) => s.push_str(&l.to_string()),
BinaryOperator::Divide(l, _) => s.push_str(&l.to_string()),
}
s.push(')');
s.push(
match self {
&BinaryOperator::Add(_, _) => '+',
&BinaryOperator::Subtract(_, _) => '-',
&BinaryOperator::Multiply(_, _) => '*',
&BinaryOperator::Divide(_, _) => '/',
}
);
s.push('(');
match self {
BinaryOperator::Add(_, r) => s.push_str(&r.to_string()),
BinaryOperator::Subtract(_, r) => s.push_str(&r.to_string()),
BinaryOperator::Multiply(_, r) => s.push_str(&r.to_string()),
BinaryOperator::Divide(_, r) => s.push_str(&r.to_string()),
}
s.push(')');
s
}
}
impl<L: Expr, R: Expr> AstNode for BinaryOperator<L, R> {}
impl<L: Expr, R: Expr> Expr for BinaryOperator<L, R> {
fn get_base(&self) -> BaseType {
// TODO: This get_base is clearly wrong
match self {
BinaryOperator::Add(l, _) => l.get_base(),
BinaryOperator::Subtract(l, _) => l.get_base(),
BinaryOperator::Multiply(l, _) => l.get_base(),
BinaryOperator::Divide(l, _) => l.get_base(),
}
}
}