mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 21:21:33 +00:00
language: add boolean and/or operators, change negation syntax (#7)
* language: add boolean and/or operators, change negation syntax * examples: simplify row builder using arrays
This commit is contained in:
@ -83,7 +83,7 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||
}
|
||||
|
||||
private fun readPrefixOperation(): PrefixOperation = within {
|
||||
expect(TokenType.Negation, TokenType.Plus, TokenType.Minus, TokenType.Tilde) {
|
||||
expect(TokenType.Not, TokenType.Plus, TokenType.Minus, TokenType.Tilde) {
|
||||
PrefixOperation(convertPrefixOperator(it), readExpression())
|
||||
}
|
||||
}
|
||||
@ -157,7 +157,7 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||
readParentheses()
|
||||
}
|
||||
|
||||
TokenType.Negation, TokenType.Plus, TokenType.Minus, TokenType.Tilde -> {
|
||||
TokenType.Not, TokenType.Plus, TokenType.Minus, TokenType.Tilde -> {
|
||||
readPrefixOperation()
|
||||
}
|
||||
|
||||
@ -215,7 +215,9 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||
TokenType.Lesser,
|
||||
TokenType.Greater,
|
||||
TokenType.LesserEqual,
|
||||
TokenType.GreaterEqual
|
||||
TokenType.GreaterEqual,
|
||||
TokenType.And,
|
||||
TokenType.Or
|
||||
)
|
||||
) {
|
||||
within {
|
||||
@ -338,10 +340,13 @@ class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||
TokenType.Greater -> InfixOperator.Greater
|
||||
TokenType.LesserEqual -> InfixOperator.LesserEqual
|
||||
TokenType.GreaterEqual -> InfixOperator.GreaterEqual
|
||||
TokenType.And -> InfixOperator.BooleanAnd
|
||||
TokenType.Or -> InfixOperator.BooleanOr
|
||||
else -> throw RuntimeException("Unknown Infix Operator")
|
||||
}
|
||||
|
||||
private fun convertPrefixOperator(token: Token): PrefixOperator = when (token.type) {
|
||||
TokenType.Not -> PrefixOperator.BooleanNot
|
||||
TokenType.Plus -> PrefixOperator.UnaryPlus
|
||||
TokenType.Minus -> PrefixOperator.UnaryMinus
|
||||
TokenType.Tilde -> PrefixOperator.BinaryNot
|
||||
|
@ -13,7 +13,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
(it in '0' .. '9')}, KeywordUpgrader),
|
||||
StringLiteral(StringLiteralFamily),
|
||||
Equality(OperatorFamily),
|
||||
Inequality(OperatorFamily),
|
||||
Inequality(ManyChars("!="), OperatorFamily),
|
||||
ExclaimationPoint(SingleChar('!'), Promotion('=', Inequality)),
|
||||
Equals(SingleChar('='), Promotion('=', Equality)),
|
||||
PlusPlus(ManyChars("++"), OperatorFamily),
|
||||
MinusMinus(ManyChars("--"), OperatorFamily),
|
||||
@ -21,6 +22,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
Minus(SingleChar('-'), OperatorFamily, Promotion('-', MinusMinus)),
|
||||
Multiply(SingleChar('*'), OperatorFamily),
|
||||
Divide(SingleChar('/'), OperatorFamily),
|
||||
And(ManyChars("and"), OperatorFamily),
|
||||
Or(ManyChars("or"), OperatorFamily),
|
||||
Tilde(SingleChar('~'), OperatorFamily),
|
||||
Ampersand(SingleChar('&'), OperatorFamily),
|
||||
Pipe(SingleChar('|'), OperatorFamily),
|
||||
@ -35,7 +38,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
RightBracket(SingleChar(']')),
|
||||
LeftParentheses(SingleChar('(')),
|
||||
RightParentheses(SingleChar(')')),
|
||||
Negation(SingleChar('!'), Promotion('=', Inequality), OperatorFamily),
|
||||
Not(ManyChars("not"), OperatorFamily),
|
||||
Mod(ManyChars("mod"), OperatorFamily),
|
||||
Rem(ManyChars("rem"), OperatorFamily),
|
||||
Comma(SingleChar(',')),
|
||||
|
Reference in New Issue
Block a user