Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
mahkoh committed Jun 28, 2020
1 parent 5806fca commit 426e04a
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 211 deletions.
2 changes: 1 addition & 1 deletion src/diag.rs
Expand Up @@ -329,7 +329,7 @@ impl Diagnostic {
ErrorContext::EvalArithmetic(eid) => (e(eid), q("arithmetic expression")),
ErrorContext::EvalBool(eid) => (e(eid), q("boolean expression")),
ErrorContext::EvalOverlay(eid) => (e(eid), q("overlay expression")),
ErrorContext::EvalConcat(eid) => (e(eid), q("concat expression")),
ErrorContext::EvalAdd(eid) => (e(eid), q("add expression")),
ErrorContext::EvalCond(eid) => (e(eid), q("conditional expression")),
ErrorContext::EvalStringify(eid) => (e(eid), q("stringify expression")),
ErrorContext::EvalApl(eid) => (e(eid), q("function application")),
Expand Down
42 changes: 25 additions & 17 deletions src/eval/force.rs
Expand Up @@ -54,8 +54,11 @@ impl Elang {
Ok(())
}
ExprType::Resolved { dest, .. } => self.force(dest),
ExprType::Add { .. }
| ExprType::Sub { .. }
ExprType::Add { .. } => {
drop(borrow);
self.force_add(&expr)
}
ExprType::Sub { .. }
| ExprType::Mul { .. }
| ExprType::Div { .. }
| ExprType::Mod { .. }
Expand All @@ -76,10 +79,6 @@ impl Elang {
drop(borrow);
self.force_bool(&expr)
}
ExprType::Concat { .. } => {
drop(borrow);
self.force_concat(&expr)
}
ExprType::Overlay { .. } => {
drop(borrow);
self.force_overlay(&expr)
Expand Down Expand Up @@ -117,7 +116,7 @@ impl Elang {
fn force_int(&mut self, expr: &Expr) -> Result {
let ctx = ErrorContext::EvalArithmetic(expr.id);

let num = |slf: &mut Self, v| match slf.get_int_(v) {
let num = |slf: &mut Self, v| match slf.get_number_(v) {
Ok(v) => Ok(v),
Err(mut e) => {
e.context.push(ctx);
Expand All @@ -126,7 +125,6 @@ impl Elang {
};

let new = match *expr.val.borrow() {
ExprType::Add { lhs, rhs } => &*num(self, lhs)? + &*num(self, rhs)?,
ExprType::Sub { lhs, rhs } => &*num(self, lhs)? - &*num(self, rhs)?,
ExprType::Mul { lhs, rhs } => &*num(self, lhs)? * &*num(self, rhs)?,
ExprType::Div { numer, denom, int } => {
Expand Down Expand Up @@ -172,7 +170,7 @@ impl Elang {
}
}
}
let int = |slf: &mut Self, v| get(slf.get_int_(v), ctx);
let int = |slf: &mut Self, v| get(slf.get_number_(v), ctx);
let bol = |slf: &mut Self, v| get(slf.get_bool_(v), ctx);

let new = match *val {
Expand Down Expand Up @@ -233,29 +231,40 @@ impl Elang {
Ok(())
}

fn force_concat(&mut self, expr: &Expr) -> Result {
fn force_add(&mut self, expr: &Expr) -> Result {
let mut val = expr.val.borrow_mut();
let ctx = ErrorContext::EvalConcat(expr.id);
let ctx = ErrorContext::EvalAdd(expr.id);

let new = if let ExprType::Concat { lhs, rhs } = *val {
let new = if let ExprType::Add { lhs, rhs } = *val {
let left = self.resolve_(lhs)?;
let left = left.val.borrow();
match *left {
let leftb = left.val.borrow();
match *leftb {
ExprType::Number { val: ref left } => {
let left = left.clone();
drop(leftb);
let ctx2 = ErrorContext::EvalOtherExprType(lhs, ExprKind::Number);
let right = self.get_number_(rhs).ctx(ctx2).ctx(ctx)?;
let new = &*left + &*right;
ExprType::Number { val: Rc::new(new) }
}
ExprType::String { content: left } => {
drop(leftb);
let ctx2 = ErrorContext::EvalOtherExprType(lhs, ExprKind::String);
let right = self.get_string_(rhs).ctx(ctx2).ctx(ctx)?;
let new = self.store.concat(left, right);
ExprType::String { content: new }
}
ExprType::List { elements: ref left } => {
let left = left.clone();
drop(leftb);
let ctx2 = ErrorContext::EvalOtherExprType(lhs, ExprKind::List);
let right = self.get_list_(rhs).ctx(ctx2).ctx(ctx)?;
if right.len() == 0 {
ExprType::List {
elements: left.clone(),
}
} else {
let mut left = (**left).to_vec();
let mut left = (*left).to_vec();
left.reserve(right.len());
for &el in right.iter() {
left.push(el);
Expand All @@ -271,7 +280,7 @@ impl Elang {
lhs,
ErrorType::UnexpectedExprType(
&[ExprKind::String, ExprKind::List],
left.kind(),
leftb.kind(),
),
)
.ctx(ctx);
Expand Down Expand Up @@ -494,7 +503,6 @@ impl Elang {
lower: lhs,
upper: rhs,
}
| ExprType::Concat { lhs, rhs }
| ExprType::Apl {
func: lhs,
arg: rhs,
Expand Down
2 changes: 1 addition & 1 deletion src/eval/get.rs
Expand Up @@ -38,7 +38,7 @@ impl Elang {
}
}

pub(crate) fn get_int_(&mut self, expr: ExprId) -> Result<Rc<BigRational>> {
pub(crate) fn get_number_(&mut self, expr: ExprId) -> Result<Rc<BigRational>> {
let res = self.resolve_(expr)?;
let val = res.val.borrow();
match *val {
Expand Down
4 changes: 0 additions & 4 deletions src/eval/mod.rs
Expand Up @@ -183,9 +183,6 @@ impl Elang {
lower,
upper
),
ExprType::Concat { lhs, rhs } => {
bin!(|lhs, rhs| ExprType::Concat { lhs, rhs }, lhs, rhs)
}
ExprType::Apl { func, arg } => {
bin!(|func, arg| ExprType::Apl { func, arg }, func, arg)
}
Expand Down Expand Up @@ -242,7 +239,6 @@ impl Elang {
| ExprType::Eq { .. }
| ExprType::Ne { .. }
| ExprType::Overlay { .. }
| ExprType::Concat { .. }
| ExprType::Apl { .. }
| ExprType::Test { .. }
| ExprType::Cond { .. } => {
Expand Down
11 changes: 5 additions & 6 deletions src/lexer.rs
Expand Up @@ -3,7 +3,7 @@ use crate::types::{
result::{Result, ResultUtil},
span::{Span, Spanned},
store::Store,
token::{SToken, Token, TokenType},
token::{SToken, Token, TokenKind},
token_stream::TokenStream,
};

Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'a, 'b> Lexer<'a, 'b> {
//
}
if let Some(t) = self.braces.pop() {
return self.error(t.span, ErrorType::UnmatchedToken(TokenType::LeftBrace));
return self.error(t.span, ErrorType::UnmatchedToken(TokenKind::LeftBrace));
}
Ok(())
}
Expand Down Expand Up @@ -165,6 +165,8 @@ impl<'a, 'b> Lexer<'a, 'b> {
b'?' => one!(QuestionMark),
b'@' => one!(At),
b'%' => one!(Percent),
b'+' => one!(Plus),
b'-' => one!(Minus),
_ => {}
}

Expand All @@ -179,7 +181,7 @@ impl<'a, 'b> Lexer<'a, 'b> {
Some(t) => t,
_ => {
return self
.error(res.span, ErrorType::UnmatchedToken(t.ty()))
.error(res.span, ErrorType::UnmatchedToken(t.kind()))
}
};
if *ty == BraceType::String {
Expand Down Expand Up @@ -212,9 +214,6 @@ impl<'a, 'b> Lexer<'a, 'b> {
(b'<', _) => one!(LeftAngle),
(b'>', Some(b'=')) => two!(RightAngleEquals),
(b'>', _) => one!(RightAngle),
(b'+', Some(b'+')) => two!(PlusPlus),
(b'+', _) => one!(Plus),
(b'-', _) => one!(Minus),
_ => {}
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Expand Up @@ -9,7 +9,7 @@ pub use crate::{
result::Result,
span::{Span, Spanned},
store::StrId,
token::TokenType,
token::TokenKind,
tree::{BuiltInFn, Expr, ExprId, ExprKind, ExprType, FnParam, FnType},
value::Value,
},
Expand Down Expand Up @@ -142,8 +142,8 @@ impl Elang {
self.get_bool_(expr_id)
}

pub fn get_int(&mut self, expr_id: ExprId) -> Result<Rc<BigRational>> {
self.get_int_(expr_id)
pub fn get_number(&mut self, expr_id: ExprId) -> Result<Rc<BigRational>> {
self.get_number_(expr_id)
}

pub fn get_list(&mut self, expr_id: ExprId) -> Result<Rc<[ExprId]>> {
Expand Down
63 changes: 31 additions & 32 deletions src/parser.rs
Expand Up @@ -5,7 +5,7 @@ use crate::types::{
span::{Span, Spanned},
stack::Stack,
store::{Store, StrId},
token::{Token, TokenType},
token::{Token, TokenKind},
token_stream::TokenStream,
tree::{ExprId, ExprType, FnParam, FnType, SExpr},
};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl<'a> Parser<'a> {
if let Some(t) = self.tokens.try_next() {
return self.error(
t.span,
ErrorType::UnexpectedToken(TokenAlternative::EndOfInput, t.ty()),
ErrorType::UnexpectedToken(TokenAlternative::EndOfInput, t.kind()),
);
}
Ok(expr.val)
Expand Down Expand Up @@ -105,7 +105,6 @@ impl<'a> Parser<'a> {
Token::Slash => Op::Div(false),
Token::IntSlash => Op::Div(true),
Token::Percent => Op::Mod,
Token::PlusPlus => Op::Concat,
Token::QuestionMark => Op::Test,
Token::Dot => Op::Select,
t if t.starts_expr() => Op::Apl,
Expand Down Expand Up @@ -214,7 +213,7 @@ impl<'a> Parser<'a> {
token.span,
ErrorType::UnexpectedToken(
TokenAlternative::StartOfExpression,
token.ty(),
token.kind(),
),
);
}
Expand Down Expand Up @@ -306,7 +305,7 @@ impl<'a> Parser<'a> {
expr = Some(match expr {
Some(expr) => self.spanned(
Span::new(expr.span.lo, rhs.span.hi),
ExprType::Concat {
ExprType::Add {
lhs: expr.val,
rhs: rhs.val,
},
Expand Down Expand Up @@ -347,11 +346,11 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Number,
TokenType::Ident,
TokenType::LeftParen,
TokenKind::Number,
TokenKind::Ident,
TokenKind::LeftParen,
]),
next.ty(),
next.kind(),
),
);
}
Expand Down Expand Up @@ -528,10 +527,10 @@ impl<'a> Parser<'a> {
ident.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Ident,
TokenType::DotDot,
TokenKind::Ident,
TokenKind::DotDot,
]),
ident.ty(),
ident.kind(),
),
)
.ctx(ctx);
Expand Down Expand Up @@ -570,10 +569,10 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Comma,
TokenType::RightBrace,
TokenKind::Comma,
TokenKind::RightBrace,
]),
next.ty(),
next.kind(),
),
)
.ctx(ctx);
Expand Down Expand Up @@ -653,10 +652,10 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::In,
TokenType::Comma,
TokenKind::In,
TokenKind::Comma,
]),
next.ty(),
next.kind(),
),
)
.ctx(ctx);
Expand Down Expand Up @@ -752,10 +751,10 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Comma,
TokenType::RightBracket,
TokenKind::Comma,
TokenKind::RightBracket,
]),
next.ty(),
next.kind(),
),
)
.ctx(ctx);
Expand Down Expand Up @@ -854,11 +853,11 @@ impl<'a> Parser<'a> {
el.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Ident,
TokenType::Comma,
TokenType::RightBrace,
TokenKind::Ident,
TokenKind::Comma,
TokenKind::RightBrace,
]),
el.ty(),
el.kind(),
),
)
.ctx(ErrorContext::ParseInherit(next.span.lo));
Expand All @@ -871,11 +870,11 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Ident,
TokenType::Inherit,
TokenType::RightBrace,
TokenKind::Ident,
TokenKind::Inherit,
TokenKind::RightBrace,
]),
next.ty(),
next.kind(),
),
)
.ctx(ctx);
Expand All @@ -892,10 +891,10 @@ impl<'a> Parser<'a> {
next.span,
ErrorType::UnexpectedToken(
TokenAlternative::List(&[
TokenType::Comma,
TokenType::RightBrace,
TokenKind::Comma,
TokenKind::RightBrace,
]),
next.ty(),
next.kind(),
),
)
.ctx(ctx);
Expand Down

0 comments on commit 426e04a

Please sign in to comment.