Add support for positive integer ranges

Including support for variables and subexpressions as range bounds.
This commit is contained in:
Jakub Žádník
2021-09-05 00:52:57 +03:00
parent 2794556eaa
commit 0b412cd6b3
9 changed files with 277 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
use super::{Call, Expression, Operator};
use super::{Call, Expression, Operator, RangeOperator};
use crate::{BlockId, Signature, Span, VarId};
#[derive(Debug, Clone)]
@@ -6,6 +6,11 @@ pub enum Expr {
Bool(bool),
Int(i64),
Float(f64),
Range(
Option<Box<Expression>>,
Option<Box<Expression>>,
RangeOperator,
),
Var(VarId),
Call(Box<Call>),
ExternalCall(Vec<u8>, Vec<Vec<u8>>),

View File

@@ -7,6 +7,7 @@ pub struct Expression {
pub span: Span,
pub ty: Type,
}
impl Expression {
pub fn garbage(span: Span) -> Expression {
Expression {
@@ -15,6 +16,7 @@ impl Expression {
ty: Type::Unknown,
}
}
pub fn precedence(&self) -> usize {
match &self.expr {
Expr::Operator(operator) => {

View File

@@ -1,3 +1,5 @@
use crate::Span;
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -46,3 +48,24 @@ impl Display for Operator {
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum RangeInclusion {
Inclusive,
RightExclusive,
}
#[derive(Debug, Copy, Clone)]
pub struct RangeOperator {
pub inclusion: RangeInclusion,
pub span: Span,
}
impl Display for RangeOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.inclusion {
RangeInclusion::Inclusive => write!(f, ".."),
RangeInclusion::RightExclusive => write!(f, "..<"),
}
}
}