Move parse::operator into module::operator

This commit is contained in:
Richard Feldman 2020-05-01 21:57:33 -04:00
parent f47d96e1a4
commit 101dc80ae5
13 changed files with 18 additions and 19 deletions

View file

@ -1,10 +1,9 @@
use crate::header::ModuleName;
use crate::ident::Ident;
use crate::operator::CalledVia;
use crate::operator::{BinOp, UnaryOp};
use bumpalo::collections::String;
use bumpalo::collections::Vec;
use bumpalo::Bump;
use roc_module::operator::{BinOp, CalledVia, UnaryOp};
use roc_region::all::{Loc, Region};
#[derive(Clone, Debug, PartialEq)]

View file

@ -7,7 +7,6 @@ use crate::blankspace::{
use crate::ident::{global_tag_or_ident, ident, lowercase_ident, Ident};
use crate::keyword;
use crate::number_literal::number_literal;
use crate::operator::{BinOp, CalledVia, UnaryOp};
use crate::parser::{
self, allocated, char, fail, not, not_followed_by, optional, sep_by1, string, then, unexpected,
unexpected_eof, Either, Fail, FailReason, ParseResult, Parser, State,
@ -16,6 +15,7 @@ use crate::type_annotation;
use bumpalo::collections::string::String;
use bumpalo::collections::Vec;
use bumpalo::Bump;
use roc_module::operator::{BinOp, CalledVia, UnaryOp};
use roc_region::all::{Located, Region};
pub fn expr<'a>(min_indent: u16) -> impl Parser<'a, Expr<'a>> {

View file

@ -21,7 +21,6 @@ pub mod ident;
pub mod keyword;
pub mod module;
pub mod number_literal;
pub mod operator;
pub mod pattern;
pub mod problems;
pub mod string_literal;

View file

@ -1,138 +0,0 @@
use self::BinOp::*;
use std::cmp::Ordering;
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CalledVia {
/// Calling with space, e.g. (foo bar)
Space,
/// Calling with an operator, e.g. (bar |> foo) or (1 + 2)
BinOp(BinOp),
/// Calling with a unary operator, e.g. (!foo bar baz) or (-foo bar baz)
UnaryOp(UnaryOp),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnaryOp {
/// (-), e.g. (-x)
Negate,
/// (!), e.g. (!x)
Not,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinOp {
// highest precedence
Caret,
Star,
Slash,
DoubleSlash,
Percent,
DoublePercent,
Plus,
Minus,
Equals,
NotEquals,
LessThan,
GreaterThan,
LessThanOrEq,
GreaterThanOrEq,
And,
Or,
Pizza, // lowest precedence
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ArgSide {
Left,
Right,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Associativity {
/// left-associative operators:
///
/// arithmetic: * / // % + -
/// application: |>
LeftAssociative,
/// right-associative operators:
///
/// exponentiation: ^
/// boolean: && ||
/// application: <|
RightAssociative,
/// non-associative operators:
///
/// comparison: == > >= < <=
NonAssociative,
}
impl BinOp {
pub fn associativity(self) -> Associativity {
use self::Associativity::*;
match self {
Pizza | Star | Slash | DoubleSlash | DoublePercent | Percent | Plus | Minus => {
LeftAssociative
}
And | Or | Caret => RightAssociative,
Equals | NotEquals | LessThan | GreaterThan | LessThanOrEq | GreaterThanOrEq => {
NonAssociative
}
}
}
fn precedence(self) -> u8 {
match self {
Caret => 7,
Star | Slash | DoubleSlash | DoublePercent | Percent => 6,
Plus | Minus => 5,
Equals | NotEquals | LessThan | GreaterThan | LessThanOrEq | GreaterThanOrEq => 4,
And => 3,
Or => 2,
Pizza => 1,
}
}
}
impl PartialOrd for BinOp {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BinOp {
fn cmp(&self, other: &Self) -> Ordering {
self.precedence().cmp(&other.precedence())
}
}
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let as_str = match self {
Caret => "^",
Star => "*",
Slash => "/",
DoubleSlash => "//",
Percent => "%",
DoublePercent => "%%",
Plus => "+",
Minus => "-",
Equals => "==",
NotEquals => "!=",
LessThan => "<",
GreaterThan => ">",
LessThanOrEq => "<=",
GreaterThanOrEq => ">=",
And => "&&",
Or => "||",
Pizza => "|>",
};
write!(f, "{}", as_str)
}
}