add CONJ and OR

This commit is contained in:
Masato Imai
2022-08-23 10:41:11 +09:00
parent c1ffb262ef
commit e3efd57b09
6 changed files with 31 additions and 5 deletions

View File

@ -13,6 +13,7 @@ MUL: '*' ;
DIV: '/' ;
CONJ: '&&' ;
AND: '&' ;
OR: '||' ;
EQ: '=' ;
EQEQ: '==' ;
NE: '!=' ;

View File

@ -30,7 +30,7 @@ permission: DOLLER LPAREN ( IDENT LBRACKET ( IDENT COMMA? )* RBRACKET COMMA? )*
expr: assign ;
assign: equality (EQ assign)? ;
equality: relational (EQEQ relational | NE relational | CONJ)* ;
equality: relational (EQEQ relational | NE relational | OR relational | CONJ relational)* ;
relational: add (LE add | LT add | BE add | BT add)* ;
add: mul (ADD mul | SUB mul | SUB_ASSIGNMENT mul | ADD_ASSIGNMENT mul)* ;
mul: unary (MUL unary | DIV unary | DIV_ASSIGNMENT unary | MUL_ASSIGNMENT unary)* ;

View File

@ -9,10 +9,12 @@ pub enum NodeKind {
SUB,
MUL,
DIV,
EQ, // ==
NE, // !=
LT, // <
LE, // <=
CONJ, // &&
OR, // ||
EQ, // ==
NE, // !=
LT, // <
LE, // <=
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

View File

@ -426,6 +426,10 @@ impl Parser {
node = Node::new_node(NodeKind::EQ, node, self.relational()?);
} else if self.tokenizer.consume(String::from("!=")) {
node = Node::new_node(NodeKind::NE, node, self.relational()?);
} else if self.tokenizer.consume(String::from("&&")) {
node = Node::new_node(NodeKind::CONJ, node, self.relational()?);
} else if self.tokenizer.consume(String::from("||")) {
node = Node::new_node(NodeKind::OR, node, self.relational()?);
} else {
return Ok(node);
}

View File

@ -132,6 +132,7 @@ impl Tokenizer {
String::from(")"),
String::from("["),
String::from("]"),
String::from("||"),
String::from("=="),
String::from("!="),
String::from(">="),

View File

@ -465,6 +465,24 @@ impl GPSL {
},
_ => Err("Cannot subtract non-number.".to_string()),
},
NodeKind::CONJ => {
if lhs.extract_number() == Some(1)
&& rhs.extract_number() == Some(1)
{
Ok(Some(Variable::Number { value: 1 }))
} else {
Ok(Some(Variable::Number { value: 0 }))
}
}
NodeKind::OR => {
if lhs.extract_number() == Some(1)
|| rhs.extract_number() == Some(1)
{
Ok(Some(Variable::Number { value: 1 }))
} else {
Ok(Some(Variable::Number { value: 0 }))
}
}
NodeKind::EQ => {
if lhs == rhs {
Ok(Some(Variable::Number { value: 1 }))