Lopps && logical ops

This commit is contained in:
Aleksey Kladov 2018-08-05 18:16:52 +03:00
parent b0291cd8c2
commit 720861bcff
5 changed files with 40 additions and 14 deletions

View File

@ -41,6 +41,8 @@ Grammar(
[">=", "GTEQ"], [">=", "GTEQ"],
["+=", "PLUSEQ"], ["+=", "PLUSEQ"],
["-=", "MINUSEQ"], ["-=", "MINUSEQ"],
["&&", "AMPERSANDAMPERSAND"],
["||", "PIPEPIPE"],
], ],
keywords: [ keywords: [
"use", "use",
@ -143,6 +145,7 @@ Grammar(
"LAMBDA_EXPR", "LAMBDA_EXPR",
"IF_EXPR", "IF_EXPR",
"WHILE_EXPR", "WHILE_EXPR",
"LOOP_EXPR",
"BLOCK_EXPR", "BLOCK_EXPR",
"RETURN_EXPR", "RETURN_EXPR",
"MATCH_EXPR", "MATCH_EXPR",

View File

@ -48,6 +48,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark
MOVE_KW if la == PIPE => lambda_expr(p), MOVE_KW if la == PIPE => lambda_expr(p),
IF_KW => if_expr(p), IF_KW => if_expr(p),
WHILE_KW => while_expr(p), WHILE_KW => while_expr(p),
LOOP_KW => loop_expr(p),
MATCH_KW => match_expr(p), MATCH_KW => match_expr(p),
UNSAFE_KW if la == L_CURLY => block_expr(p), UNSAFE_KW if la == L_CURLY => block_expr(p),
L_CURLY => block_expr(p), L_CURLY => block_expr(p),
@ -143,6 +144,18 @@ fn while_expr(p: &mut Parser) -> CompletedMarker {
m.complete(p, WHILE_EXPR) m.complete(p, WHILE_EXPR)
} }
// test loop_expr
// fn foo() {
// loop {};
// }
fn loop_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(LOOP_KW));
let m = p.start();
p.bump();
block(p);
m.complete(p, LOOP_EXPR)
}
// test cond // test cond
// fn foo() { if let Some(_) = None {} } // fn foo() { if let Some(_) = None {} }
fn cond(p: &mut Parser) { fn cond(p: &mut Parser) {

View File

@ -56,27 +56,34 @@ enum Op {
// x += 1; // x += 1;
// 1 + 1 <= 2 * 3; // 1 + 1 <= 2 * 3;
// z -= 3 >= 0; // z -= 3 >= 0;
// true || true && false;
// } // }
fn current_op(p: &Parser) -> (u8, Op) { fn current_op(p: &Parser) -> (u8, Op) {
if p.at_compound2(L_ANGLE, EQ) {
return (3, Op::Composite(LTEQ, 2));
}
if p.at_compound2(R_ANGLE, EQ) {
return (3, Op::Composite(GTEQ, 2));
}
if p.at_compound2(PLUS, EQ) { if p.at_compound2(PLUS, EQ) {
return (1, Op::Composite(PLUSEQ, 2)); return (1, Op::Composite(PLUSEQ, 2));
} }
if p.at_compound2(MINUS, EQ) { if p.at_compound2(MINUS, EQ) {
return (1, Op::Composite(MINUSEQ, 2)); return (1, Op::Composite(MINUSEQ, 2));
} }
if p.at_compound2(PIPE, PIPE) {
return (3, Op::Composite(PIPEPIPE, 2));
}
if p.at_compound2(AMPERSAND, AMPERSAND) {
return (4, Op::Composite(AMPERSANDAMPERSAND, 2));
}
if p.at_compound2(L_ANGLE, EQ) {
return (5, Op::Composite(LTEQ, 2));
}
if p.at_compound2(R_ANGLE, EQ) {
return (5, Op::Composite(GTEQ, 2));
}
let bp = match p.current() { let bp = match p.current() {
EQ => 1, EQ => 1,
DOTDOT => 2, DOTDOT => 2,
EQEQ | NEQ => 3, EQEQ | NEQ => 5,
MINUS | PLUS => 4, MINUS | PLUS => 6,
STAR | SLASH => 5, STAR | SLASH => 7,
_ => 0, _ => 0,
}; };
(bp, Op::Simple) (bp, Op::Simple)

View File

@ -44,6 +44,8 @@ pub enum SyntaxKind {
GTEQ, GTEQ,
PLUSEQ, PLUSEQ,
MINUSEQ, MINUSEQ,
AMPERSANDAMPERSAND,
PIPEPIPE,
USE_KW, USE_KW,
FN_KW, FN_KW,
STRUCT_KW, STRUCT_KW,
@ -133,6 +135,7 @@ pub enum SyntaxKind {
LAMBDA_EXPR, LAMBDA_EXPR,
IF_EXPR, IF_EXPR,
WHILE_EXPR, WHILE_EXPR,
LOOP_EXPR,
BLOCK_EXPR, BLOCK_EXPR,
RETURN_EXPR, RETURN_EXPR,
MATCH_EXPR, MATCH_EXPR,
@ -272,6 +275,8 @@ impl SyntaxKind {
GTEQ => &SyntaxInfo { name: "GTEQ" }, GTEQ => &SyntaxInfo { name: "GTEQ" },
PLUSEQ => &SyntaxInfo { name: "PLUSEQ" }, PLUSEQ => &SyntaxInfo { name: "PLUSEQ" },
MINUSEQ => &SyntaxInfo { name: "MINUSEQ" }, MINUSEQ => &SyntaxInfo { name: "MINUSEQ" },
AMPERSANDAMPERSAND => &SyntaxInfo { name: "AMPERSANDAMPERSAND" },
PIPEPIPE => &SyntaxInfo { name: "PIPEPIPE" },
USE_KW => &SyntaxInfo { name: "USE_KW" }, USE_KW => &SyntaxInfo { name: "USE_KW" },
FN_KW => &SyntaxInfo { name: "FN_KW" }, FN_KW => &SyntaxInfo { name: "FN_KW" },
STRUCT_KW => &SyntaxInfo { name: "STRUCT_KW" }, STRUCT_KW => &SyntaxInfo { name: "STRUCT_KW" },
@ -361,6 +366,7 @@ impl SyntaxKind {
LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" }, LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" },
IF_EXPR => &SyntaxInfo { name: "IF_EXPR" }, IF_EXPR => &SyntaxInfo { name: "IF_EXPR" },
WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" }, WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" },
LOOP_EXPR => &SyntaxInfo { name: "LOOP_EXPR" },
BLOCK_EXPR => &SyntaxInfo { name: "BLOCK_EXPR" }, BLOCK_EXPR => &SyntaxInfo { name: "BLOCK_EXPR" },
RETURN_EXPR => &SyntaxInfo { name: "RETURN_EXPR" }, RETURN_EXPR => &SyntaxInfo { name: "RETURN_EXPR" },
MATCH_EXPR => &SyntaxInfo { name: "MATCH_EXPR" }, MATCH_EXPR => &SyntaxInfo { name: "MATCH_EXPR" },
@ -520,6 +526,8 @@ impl SyntaxKind {
GTEQ => ">=", GTEQ => ">=",
PLUSEQ => "+=", PLUSEQ => "+=",
MINUSEQ => "-=", MINUSEQ => "-=",
AMPERSANDAMPERSAND => "&&",
PIPEPIPE => "||",
USE_KW => "use", USE_KW => "use",
FN_KW => "fn", FN_KW => "fn",

View File

@ -1,5 +0,0 @@
fn foo() {
x += 1;
1 + 1 <= 2 * 3;
z -= 3 >= 0;
}