mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Add support for not equals (!=) infix operation.
This commit is contained in:
parent
0b3700667d
commit
95fe6e9abb
@ -2,12 +2,15 @@
|
|||||||
main = { in
|
main = { in
|
||||||
three = 3
|
three = 3
|
||||||
two = 2
|
two = 2
|
||||||
|
|
||||||
calculateSimple = { in
|
calculateSimple = { in
|
||||||
(50 + three) * two
|
(50 + three) * two
|
||||||
}
|
}
|
||||||
|
|
||||||
calculateComplex = { in
|
calculateComplex = { in
|
||||||
three + two + 50
|
three + two + 50
|
||||||
}
|
}
|
||||||
|
|
||||||
multiply = { a, b in
|
multiply = { a, b in
|
||||||
a * b
|
a * b
|
||||||
}
|
}
|
||||||
@ -25,6 +28,14 @@ main = { in
|
|||||||
!value
|
!value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notEqual = { a, b in
|
||||||
|
a != b
|
||||||
|
}
|
||||||
|
|
||||||
|
equal = { a, b in
|
||||||
|
a == b
|
||||||
|
}
|
||||||
|
|
||||||
[
|
[
|
||||||
calculateSimpleResult,
|
calculateSimpleResult,
|
||||||
calculateComplexResult,
|
calculateComplexResult,
|
||||||
@ -33,6 +44,10 @@ main = { in
|
|||||||
trueValue,
|
trueValue,
|
||||||
falseValue,
|
falseValue,
|
||||||
invert(true),
|
invert(true),
|
||||||
invert(false)
|
invert(false),
|
||||||
|
equal(5, 5),
|
||||||
|
equal(5, 6),
|
||||||
|
notEqual(5, 5),
|
||||||
|
notEqual(5, 6)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,6 @@ enum class InfixOperator(val token: String) {
|
|||||||
Minus("-"),
|
Minus("-"),
|
||||||
Multiply("*"),
|
Multiply("*"),
|
||||||
Divide("/"),
|
Divide("/"),
|
||||||
Equals("==")
|
Equals("=="),
|
||||||
|
NotEquals("!=")
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,9 @@ class PorkEvaluator(root: Scope) : NodeVisitor<Any> {
|
|||||||
InfixOperator.Equals -> {
|
InfixOperator.Equals -> {
|
||||||
return left == right
|
return left == right
|
||||||
}
|
}
|
||||||
|
InfixOperator.NotEquals -> {
|
||||||
|
return left != right
|
||||||
|
}
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +117,8 @@ class PorkParser(source: PeekableSource<Token>) {
|
|||||||
TokenType.Minus,
|
TokenType.Minus,
|
||||||
TokenType.Multiply,
|
TokenType.Multiply,
|
||||||
TokenType.Divide,
|
TokenType.Divide,
|
||||||
TokenType.Equality)) {
|
TokenType.Equality,
|
||||||
|
TokenType.Inequality)) {
|
||||||
val infixToken = next()
|
val infixToken = next()
|
||||||
val infixOperator = convertInfixOperator(infixToken)
|
val infixOperator = convertInfixOperator(infixToken)
|
||||||
return InfixOperation(expression, infixOperator, readExpression())
|
return InfixOperation(expression, infixOperator, readExpression())
|
||||||
@ -133,6 +134,7 @@ class PorkParser(source: PeekableSource<Token>) {
|
|||||||
TokenType.Multiply -> InfixOperator.Multiply
|
TokenType.Multiply -> InfixOperator.Multiply
|
||||||
TokenType.Divide -> InfixOperator.Divide
|
TokenType.Divide -> InfixOperator.Divide
|
||||||
TokenType.Equality -> InfixOperator.Equals
|
TokenType.Equality -> InfixOperator.Equals
|
||||||
|
TokenType.Inequality -> InfixOperator.NotEquals
|
||||||
else -> throw RuntimeException("Unknown Infix Operator")
|
else -> throw RuntimeException("Unknown Infix Operator")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
|||||||
Symbol,
|
Symbol,
|
||||||
IntLiteral,
|
IntLiteral,
|
||||||
Equality,
|
Equality,
|
||||||
|
Inequality,
|
||||||
Equals(SingleChar('='), Promotion('=', Equality)),
|
Equals(SingleChar('='), Promotion('=', Equality)),
|
||||||
Plus(SingleChar('+')),
|
Plus(SingleChar('+')),
|
||||||
Minus(SingleChar('-')),
|
Minus(SingleChar('-')),
|
||||||
@ -17,7 +18,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
|||||||
RightBracket(SingleChar(']')),
|
RightBracket(SingleChar(']')),
|
||||||
LeftParentheses(SingleChar('(')),
|
LeftParentheses(SingleChar('(')),
|
||||||
RightParentheses(SingleChar(')')),
|
RightParentheses(SingleChar(')')),
|
||||||
Negation(SingleChar('!')),
|
Negation(SingleChar('!'), Promotion('=', Inequality)),
|
||||||
Comma(SingleChar(',')),
|
Comma(SingleChar(',')),
|
||||||
False(Keyword("false")),
|
False(Keyword("false")),
|
||||||
True(Keyword("true")),
|
True(Keyword("true")),
|
||||||
|
Loading…
Reference in New Issue
Block a user