Skip to content

Commit

Permalink
Some compile refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBB committed Mar 31, 2024
1 parent f18dc94 commit 43cb422
Show file tree
Hide file tree
Showing 7 changed files with 658 additions and 433 deletions.
76 changes: 21 additions & 55 deletions gold/src/ast/high.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::fmt::Display;
use std::rc::Rc;

use crate::{builtins::BUILTINS, error::{Reason, Span, Syntax}};
use crate::{builtins::BUILTINS, error::{Reason, Span, Syntax}, types::LogicOp};
use crate::formatting::FormatSpec;

use crate::error::{Action, Error, Tagged, Taggable};
use crate::Object;
use crate::types::Key;
use super::low;
use super::scope::{SubScope, Scope, LocalScope};
use crate::types::{UnOp, BinOp, Res};
use crate::types::{UnOp, BinOp, Res, EagerOp};

// Utility
// ----------------------------------------------------------------
Expand Down Expand Up @@ -438,7 +437,7 @@ impl Lower for ArgElement {
#[derive(Debug, Clone, PartialEq)]
pub enum Transform {
/// Unary operator
UnOp(Tagged<UnOp>),
UnOp(Tagged<Option<UnOp>>),

/// Binary operator with right operand
BinOp(Tagged<BinOp>, Box<Tagged<Expr>>),
Expand All @@ -455,7 +454,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Index.tag(loc), Box::new(subscript))
Transform::BinOp(BinOp::Eager(EagerOp::Index).tag(loc), Box::new(subscript))
}

/// Construct an exponentiation transform.
Expand All @@ -465,7 +464,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Power.tag(loc), Box::new(exponent))
Transform::BinOp(BinOp::Eager(EagerOp::Power).tag(loc), Box::new(exponent))
}

/// Construct a multiplication transform.
Expand All @@ -475,7 +474,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Multiply.tag(loc), Box::new(multiplicand))
Transform::BinOp(BinOp::Eager(EagerOp::Multiply).tag(loc), Box::new(multiplicand))
}

/// Construct an integer division transform.
Expand All @@ -485,7 +484,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::IntegerDivide.tag(loc), Box::new(divisor))
Transform::BinOp(BinOp::Eager(EagerOp::IntegerDivide).tag(loc), Box::new(divisor))
}

/// Construct a mathematical division transform.
Expand All @@ -495,7 +494,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Divide.tag(loc), Box::new(divisor))
Transform::BinOp(BinOp::Eager(EagerOp::Divide).tag(loc), Box::new(divisor))
}

/// Construct an addition transform.
Expand All @@ -505,7 +504,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Add.tag(loc), Box::new(addend))
Transform::BinOp(BinOp::Eager(EagerOp::Add).tag(loc), Box::new(addend))
}

/// Construct a subtraction transform.
Expand All @@ -515,7 +514,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Subtract.tag(loc), Box::new(subtrahend))
Transform::BinOp(BinOp::Eager(EagerOp::Subtract).tag(loc), Box::new(subtrahend))
}

/// Construct a less-than transform.
Expand All @@ -525,7 +524,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Less.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::Less).tag(loc), Box::new(rhs))
}

/// Construct a greater-than transform.
Expand All @@ -535,7 +534,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Greater.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::Greater).tag(loc), Box::new(rhs))
}

/// Construct a less-than-or-equal transform.
Expand All @@ -545,7 +544,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::LessEqual.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::LessEqual).tag(loc), Box::new(rhs))
}

/// Construct a greater-than-or-equal transform.
Expand All @@ -555,7 +554,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::GreaterEqual.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::GreaterEqual).tag(loc), Box::new(rhs))
}

/// Construct an equality check transform.
Expand All @@ -565,7 +564,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Equal.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::Equal).tag(loc), Box::new(rhs))
}

/// Construct an inequality check transform.
Expand All @@ -575,7 +574,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::NotEqual.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::NotEqual).tag(loc), Box::new(rhs))
}

/// Construct a containment check transform.
Expand All @@ -585,7 +584,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Contains.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Eager(EagerOp::Contains).tag(loc), Box::new(rhs))
}

/// Construct a logical conjunction transform.
Expand All @@ -595,7 +594,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::And.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Logic(LogicOp::And).tag(loc), Box::new(rhs))
}

/// Construct a logical disjunction transform.
Expand All @@ -605,7 +604,7 @@ impl Transform {
where
Span: From<U>,
{
Transform::BinOp(BinOp::Or.tag(loc), Box::new(rhs))
Transform::BinOp(BinOp::Logic(LogicOp::Or).tag(loc), Box::new(rhs))
}
}

Expand All @@ -628,39 +627,6 @@ impl Lower for Transform {
}
}

impl Display for UnOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Passthrough => f.write_str(""),
Self::ArithmeticalNegate => f.write_str("-"),
Self::LogicalNegate => f.write_str("not"),
}
}
}

impl Display for BinOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Index => f.write_str("subscript"),
Self::Power => f.write_str("^"),
Self::Multiply => f.write_str("*"),
Self::IntegerDivide => f.write_str("//"),
Self::Divide => f.write_str("/"),
Self::Add => f.write_str("+"),
Self::Subtract => f.write_str("-"),
Self::Less => f.write_str("<"),
Self::Greater => f.write_str(">"),
Self::LessEqual => f.write_str("<="),
Self::GreaterEqual => f.write_str(">="),
Self::Equal => f.write_str("=="),
Self::NotEqual => f.write_str("!="),
Self::Contains => f.write_str("in"),
Self::And => f.write_str("and"),
Self::Or => f.write_str("or"),
}
}
}

// Expr
// ----------------------------------------------------------------

Expand Down Expand Up @@ -882,7 +848,7 @@ impl Tagged<Expr> {
where
Span: From<U>,
{
self.transform(Transform::UnOp(UnOp::ArithmeticalNegate.tag(loc)))
self.transform(Transform::UnOp(Some(UnOp::ArithmeticalNegate).tag(loc)))
}

/// Logically negate this expression.
Expand All @@ -892,7 +858,7 @@ impl Tagged<Expr> {
where
Span: From<U>,
{
self.transform(Transform::UnOp(UnOp::LogicalNegate.tag(loc)))
self.transform(Transform::UnOp(Some(UnOp::LogicalNegate).tag(loc)))
}

/// Form a function call expression from by calling this function with a
Expand Down
6 changes: 3 additions & 3 deletions gold/src/ast/low.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct MapBinding {

#[derive(Debug, Clone)]
pub enum Transform {
UnOp(Tagged<UnOp>),
UnOp(Tagged<Option<UnOp>>),
BinOp(Tagged<BinOp>, Box<Tagged<Expr>>),
FunCall(Tagged<Vec<Tagged<ArgElement>>>, bool),
}
Expand Down Expand Up @@ -139,7 +139,7 @@ pub struct Function {
pub keywords: Option<Tagged<MapBinding>>,
pub expression: Box<Tagged<Expr>>,
pub slots: SlotCatalog,
pub requires: Vec<BindingLoc>,
pub requires: Option<Vec<BindingLoc>>,
}

impl Function {
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'a> FunctionBuilder<'a> {
keywords: self.keywords,
expression: Box::new(self.expression.unwrap()),
slots,
requires,
requires: Some(requires),
}
}
}
Expand Down
Loading

0 comments on commit 43cb422

Please sign in to comment.