duskphantom_frontend/parse/
oprt.rs

1// Copyright 2024 Duskphantom Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17use super::*;
18
19pub fn unary_op(input: &mut &str) -> PResult<UnaryOp> {
20    dispatch! { peek(any);
21        '!' => token("!").value(UnaryOp::Not),
22        '-' => token("-").value(UnaryOp::Neg),
23        '+' => token("+").value(UnaryOp::Pos),
24        _ => fail,
25    }
26    .parse_next(input)
27}
28
29/// Level 0 operators, left to right
30pub fn binary_op_lv0(input: &mut &str) -> PResult<BinaryOp> {
31    dispatch! { peek(any);
32        '*' => token("*").value(BinaryOp::Mul),
33        '/' => token("/").value(BinaryOp::Div),
34        '%' => token("%").value(BinaryOp::Mod),
35        _ => fail,
36    }
37    .parse_next(input)
38}
39
40/// Level 1 operators, left to right
41pub fn binary_op_lv1(input: &mut &str) -> PResult<BinaryOp> {
42    alt((
43        token("+").value(BinaryOp::Add),
44        token("-").value(BinaryOp::Sub),
45    ))
46    .parse_next(input)
47}
48
49/// Level 2 operators, left to right
50pub fn binary_op_lv2(input: &mut &str) -> PResult<BinaryOp> {
51    alt((
52        token(">>").value(BinaryOp::Shr),
53        token("<<").value(BinaryOp::Shl),
54    ))
55    .parse_next(input)
56}
57
58/// Level 3 operators, left to right
59pub fn binary_op_lv3(input: &mut &str) -> PResult<BinaryOp> {
60    dispatch! { peek(any);
61        '>' => alt((
62            token(">=").value(BinaryOp::Ge),
63            token(">").value(BinaryOp::Gt),
64        )),
65        '<' => alt((
66            token("<=").value(BinaryOp::Le),
67            token("<").value(BinaryOp::Lt),
68        )),
69        _ => fail,
70    }
71    .parse_next(input)
72}
73
74/// Level 4 operators, left to right
75pub fn binary_op_lv4(input: &mut &str) -> PResult<BinaryOp> {
76    alt((
77        token("==").value(BinaryOp::Eq),
78        token("!=").value(BinaryOp::Ne),
79    ))
80    .parse_next(input)
81}
82
83/// Level 5 operators, left to right
84pub fn binary_op_lv5(input: &mut &str) -> PResult<BinaryOp> {
85    token("&").value(BinaryOp::BitAnd).parse_next(input)
86}
87
88/// Level 6 operators, left to right
89pub fn binary_op_lv6(input: &mut &str) -> PResult<BinaryOp> {
90    token("^").value(BinaryOp::BitXor).parse_next(input)
91}
92
93/// Level 7 operators, left to right
94pub fn binary_op_lv7(input: &mut &str) -> PResult<BinaryOp> {
95    token("|").value(BinaryOp::BitOr).parse_next(input)
96}
97
98/// Level 8 operators, left to right
99pub fn binary_op_lv8(input: &mut &str) -> PResult<BinaryOp> {
100    token("&&").value(BinaryOp::And).parse_next(input)
101}
102
103/// Level 9 operators, left to right
104pub fn binary_op_lv9(input: &mut &str) -> PResult<BinaryOp> {
105    token("||").value(BinaryOp::Or).parse_next(input)
106}