Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logical shift operator #249

Merged
merged 4 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ print "Hello World!" // Hello, World!

各中置演算子の性質は次の表に示したとおりです。

|演算子|優先順位|結合性|呼び方|
|:----|:------|:----|:----|
|`*` |1 |左 |乗算 |
|`/` |1 |左 |除算 |
|`+` |2 |左 |加算 |
|`-` |2 |左 |減算 |
|`<=` |3 |左 |以下 |
|`<` |3 |左 |未満 |
|`>=` |3 |左 |以上 |
|`>` |3 |左 |大きい|
|`<=>`|3 |左 |宇宙船|
|`==` |4 |左 |等価 |
|`!=` |4 |左 |非等価 |
| 演算子 | 優先順位 |結合性|呼び方|
|:------|:-----|:----|:----|
| `*` | 1 |左 |乗算 |
| `/` | 1 |左 |除算 |
| `+` | 2 |左 |加算 |
| `-` | 2 |左 |減算 |
| `<<` | 3 |左 | |
| `>>` | 3 |左 | |
| `<=` | 4 |左 |以下 |
| `<` | 4 |左 |未満 |
| `>=` | 4 |左 |以上 |
| `>` | 4 |左 |大きい|
| `<=>` | 4 |左 |宇宙船|
| `==` | 5 |左 |等価 |
| `!=` | 5 |左 |非等価 |

各中置演算子のオペランドは同じ型でなければなりません。これに従わなかった場合、コンパイル時にエラーが発生します。

Expand Down
4 changes: 4 additions & 0 deletions package/origlang-ast/src/after_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ pub enum BinaryOperatorKind {
Equal,
#[display(fmt = "!=")]
NotEqual,
#[display(fmt = "<<")]
ShiftLeft,
#[display(fmt = ">>")]
ShiftRight,
}
45 changes: 44 additions & 1 deletion package/origlang-cli/src/task/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use origlang_compiler::parser::{ParserError, SimpleErrorWithPos};
use origlang_runtime::{Runtime, TypeBox, Accumulate, DisplayTupleValue};
use crate::task::Task;
use origlang_ast::{AtomicPattern, Comment, Identifier, RootAst, Statement, TypeSignature};
use origlang_ast::after_parse::Expression;
use origlang_ast::after_parse::{BinaryOperatorKind, Expression};
use origlang_compiler::type_check::error::TypeCheckError;
use origlang_compiler::type_check::TypeChecker;
use origlang_ir::IntoVerbatimSequencedIR;
Expand Down Expand Up @@ -192,6 +192,7 @@ impl Test {
Self::test_exit().expect("exit");
Self::test_underscore_discard().expect("underscore_discard");
Self::test_type_alias().expect("type_alias");
Self::test_shift().expect("shift");

Ok(())
}
Expand Down Expand Up @@ -492,6 +493,48 @@ print 1
assert_eq!(Self::evaluated_expressions("type Ik = Int32\nvar t: Ik = 0i32\nprint t\n")?, type_boxes![0 => Int32]);
Ok(())
}

fn test_shift() -> Result<(), Err> {
assert_eq!(Self::ast("var a = 1 << 2\n").expect("fail").statement, [
Statement::VariableDeclaration {
pattern: AtomicPattern::Bind(Identifier::new("a".to_owned())),
expression: Expression::BinaryOperator {
lhs: Box::new(Expression::IntLiteral {
value: 1,
suffix: None,
}),
rhs: Box::new(Expression::IntLiteral {
value: 2,
suffix: None,
}),
operator: BinaryOperatorKind::ShiftLeft,
},
type_annotation: None,
}
]);

assert_eq!(Self::evaluated_expressions("var t = 1i32 << 2i32\nprint t\n")?, type_boxes![4 => Int32]);
assert_eq!(Self::ast("var a = 4 >> 2\n").expect("fail").statement, [
Statement::VariableDeclaration {
pattern: AtomicPattern::Bind(Identifier::new("a".to_owned())),
expression: Expression::BinaryOperator {
lhs: Box::new(Expression::IntLiteral {
value: 4,
suffix: None,
}),
rhs: Box::new(Expression::IntLiteral {
value: 2,
suffix: None,
}),
operator: BinaryOperatorKind::ShiftRight,
},
type_annotation: None,
}
]);

assert_eq!(Self::evaluated_expressions("var t = 4i32 >> 2i32\nprint t\n")?, type_boxes![1 => Int32]);
Ok(())
}
}

impl Task for Test {
Expand Down
46 changes: 25 additions & 21 deletions package/origlang-compiler/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,36 @@ impl Lexer {
)
.or_else(|| self.try_char('(').expect("huh?").map(|_| Token::SymLeftPar))
.or_else(|| self.try_char(')').expect("huh?").map(|_| Token::SymRightPar))
.or_else(||
fold!(
self.try_char('<').expect("huh?").map(|_| Token::SymPlus),
fold!(
self.try_char('=').expect("huh?"),
fold!(
self.try_char('>').expect("huh?"),
Some(Token::PartLessEqMore),
.or_else(|| {
if let Some(_) = self.try_char('<').expect("huh?") {
if let Some(_) = self.try_char('=').expect("huh?") {
if let Some(_) = self.try_char('>').expect("huh?") {
Some(Token::PartLessEqMore)
} else {
Some(Token::PartLessEq)
),
}
} else if let Some(_) = self.try_char('<').expect("huh?") {
Some(Token::PartLessLess)
} else {
Some(Token::SymLess)
),
}
} else {
None
)
)
.or_else(||
fold!(
self.try_char('>').expect("huh?"),
fold!(
self.try_char('=').expect("huh?"),
Some(Token::PartMoreEq),
}
})
.or_else(|| {
if let Some(_) = self.try_char('>').expect("huh?") {
if let Some(_) = self.try_char('=').expect("huh?") {
Some(Token::PartMoreEq)
} else if let Some(_) = self.try_char('>').expect("huh?") {
Some(Token::PartMoreMore)
} else {
Some(Token::SymMore)
),
}
} else {
None
)
)
}
})
.or_else(||
fold!(
self.try_char('!').expect("huh?"),
Expand Down
6 changes: 6 additions & 0 deletions package/origlang-compiler/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pub enum Token {
SymAsterisk,
/// `/`
SymSlash,
/// `<<`
PartLessLess,
/// `>>`
PartMoreMore,
/// `"("`
SymLeftPar,
/// `")"`
Expand Down Expand Up @@ -151,6 +155,8 @@ impl Token {
Self::Reserved { .. } => "reserved_token",
Self::SymUnderscore => "sym:underscore",
Self::KeywordType => "keyword:type",
Self::PartLessLess => "part:less_less",
Self::PartMoreMore => "part:more_more",
}
}

Expand Down
49 changes: 47 additions & 2 deletions package/origlang-compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub enum TokenKind {
ComparisonOps,
#[display(fmt = "`==` or `!=`")]
EqualityOps,
#[display(fmt = "`<<` or `>>`")]
ShiftOps,
#[display(fmt = "int literal")]
IntLiteral,
#[display(fmt = "identifier")]
Expand Down Expand Up @@ -478,10 +480,53 @@ impl Parser {
}
}

fn parse_shift_expression(&self) -> Result<Expression, SimpleErrorWithPos> {
debug!("expr:shift");
let first_term = self.parse_additive()?;
let next_token = self.lexer.peek();
let is_relation_operator = |token: &Token| {
matches!(token, Token::PartLessLess | Token::PartMoreMore)
};

if is_relation_operator(&next_token.data) {
self.lexer.next();
let operator_token = next_token;
let lhs = first_term;
let rhs = self.parse_relation_expression()?;
let get_operator_from_token = |token: &WithPosition<Token>| {
match &token.data {
Token::PartLessLess => Ok(BinaryOperatorKind::ShiftLeft),
Token::PartMoreMore => Ok(BinaryOperatorKind::ShiftRight),
e => Err(SimpleErrorWithPos {
kind: ParserError::UnexpectedToken {
pat: TokenKind::ShiftOps,
unmatch: e.clone(),
},
position: token.position,
}),
}
};

let mut acc = Expression::binary(get_operator_from_token(&operator_token)?, lhs, rhs);
let mut operator_token = self.lexer.peek();
while is_relation_operator(&operator_token.data) {
self.lexer.next();
let new_rhs = self.parse_relation_expression()?;
// 左結合になるように詰め替える
acc = Expression::binary(get_operator_from_token(&operator_token)?, acc, new_rhs);
operator_token = self.lexer.peek();
}

Ok(acc)
} else {
Ok(first_term)
}
}

/// 現在の位置から比較演算式をパースしようと試みる
fn parse_relation_expression(&self) -> Result<Expression, SimpleErrorWithPos> {
debug!("expr:rel");
let first_term = self.parse_additive()?;
let first_term = self.parse_shift_expression()?;
let next_token = self.lexer.peek();
let is_relation_operator = |token: &Token| {
matches!(token, Token::PartLessEq | Token::PartMoreEq | Token::SymLess | Token::SymMore | Token::PartLessEqMore)
Expand All @@ -491,7 +536,7 @@ impl Parser {
self.lexer.next();
let operator_token = next_token;
let lhs = first_term;
let rhs = self.parse_additive()?;
let rhs = self.parse_shift_expression()?;
let get_operator_from_token = |token: &WithPosition<Token>| {
match &token.data {
Token::PartLessEq => Ok(BinaryOperatorKind::LessEqual),
Expand Down
16 changes: 16 additions & 0 deletions package/origlang-compiler/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ impl TryIntoTypeCheckedForm for Expression {
})
}
}
BinaryOperatorKind::ShiftLeft | BinaryOperatorKind::ShiftRight => {
if lhs_type == rhs_type {
Ok(TypedExpression::BinaryOperator {
lhs: Box::new(lhs_expr),
rhs: Box::new(rhs_expr),
operator,
return_type: lhs_type.clone()
})
} else {
Err(TypeCheckError::UnableToUnifyEqualityQuery {
operator,
got_lhs: lhs_type,
got_rhs: rhs_type,
})
}
}
}
}
Self::If { condition, then_clause_value, else_clause_value } => {
Expand Down
4 changes: 4 additions & 0 deletions package/origlang-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ macro_rules! f {
BinaryOperatorKind::MoreEqual => (lhs >= rhs).into(),
BinaryOperatorKind::Less => (lhs < rhs).into(),
BinaryOperatorKind::LessEqual => (lhs <= rhs).into(),
BinaryOperatorKind::ShiftLeft => (lhs << rhs).into(),
BinaryOperatorKind::ShiftRight => (lhs >> rhs).into(),
BinaryOperatorKind::ThreeWay => {
match lhs.cmp(&rhs) {
::std::cmp::Ordering::Less => -1,
Expand All @@ -315,6 +317,8 @@ macro_rules! f {
BinaryOperatorKind::MoreEqual => (lhs >= rhs).into(),
BinaryOperatorKind::Less => (lhs < rhs).into(),
BinaryOperatorKind::LessEqual => (lhs <= rhs).into(),
BinaryOperatorKind::ShiftLeft => (lhs << rhs).conv::<$intermediate>().into(),
BinaryOperatorKind::ShiftRight => (lhs >> rhs).conv::<$intermediate>().into(),
BinaryOperatorKind::ThreeWay => {
match lhs.cmp(&rhs) {
::std::cmp::Ordering::Less => -1,
Expand Down