diff --git a/examples/syntax.pork b/examples/syntax.pork index 54368fe..bc70427 100644 --- a/examples/syntax.pork +++ b/examples/syntax.pork @@ -2,12 +2,15 @@ main = { in three = 3 two = 2 + calculateSimple = { in (50 + three) * two } + calculateComplex = { in three + two + 50 } + multiply = { a, b in a * b } @@ -25,6 +28,14 @@ main = { in !value } + notEqual = { a, b in + a != b + } + + equal = { a, b in + a == b + } + [ calculateSimpleResult, calculateComplexResult, @@ -33,6 +44,10 @@ main = { in trueValue, falseValue, invert(true), - invert(false) + invert(false), + equal(5, 5), + equal(5, 6), + notEqual(5, 5), + notEqual(5, 6) ] } diff --git a/src/main/kotlin/gay/pizza/pork/ast/nodes/InfixOperator.kt b/src/main/kotlin/gay/pizza/pork/ast/nodes/InfixOperator.kt index 776b636..d311697 100644 --- a/src/main/kotlin/gay/pizza/pork/ast/nodes/InfixOperator.kt +++ b/src/main/kotlin/gay/pizza/pork/ast/nodes/InfixOperator.kt @@ -5,5 +5,6 @@ enum class InfixOperator(val token: String) { Minus("-"), Multiply("*"), Divide("/"), - Equals("==") + Equals("=="), + NotEquals("!=") } diff --git a/src/main/kotlin/gay/pizza/pork/eval/PorkEvaluator.kt b/src/main/kotlin/gay/pizza/pork/eval/PorkEvaluator.kt index 2837db0..b00a893 100644 --- a/src/main/kotlin/gay/pizza/pork/eval/PorkEvaluator.kt +++ b/src/main/kotlin/gay/pizza/pork/eval/PorkEvaluator.kt @@ -81,6 +81,9 @@ class PorkEvaluator(root: Scope) : NodeVisitor { InfixOperator.Equals -> { return left == right } + InfixOperator.NotEquals -> { + return left != right + } else -> {} } diff --git a/src/main/kotlin/gay/pizza/pork/parse/PorkParser.kt b/src/main/kotlin/gay/pizza/pork/parse/PorkParser.kt index cf2c2ab..2844a34 100644 --- a/src/main/kotlin/gay/pizza/pork/parse/PorkParser.kt +++ b/src/main/kotlin/gay/pizza/pork/parse/PorkParser.kt @@ -117,7 +117,8 @@ class PorkParser(source: PeekableSource) { TokenType.Minus, TokenType.Multiply, TokenType.Divide, - TokenType.Equality)) { + TokenType.Equality, + TokenType.Inequality)) { val infixToken = next() val infixOperator = convertInfixOperator(infixToken) return InfixOperation(expression, infixOperator, readExpression()) @@ -133,6 +134,7 @@ class PorkParser(source: PeekableSource) { TokenType.Multiply -> InfixOperator.Multiply TokenType.Divide -> InfixOperator.Divide TokenType.Equality -> InfixOperator.Equals + TokenType.Inequality -> InfixOperator.NotEquals else -> throw RuntimeException("Unknown Infix Operator") } diff --git a/src/main/kotlin/gay/pizza/pork/parse/TokenType.kt b/src/main/kotlin/gay/pizza/pork/parse/TokenType.kt index 6e0ccd6..0fcb721 100644 --- a/src/main/kotlin/gay/pizza/pork/parse/TokenType.kt +++ b/src/main/kotlin/gay/pizza/pork/parse/TokenType.kt @@ -6,6 +6,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) { Symbol, IntLiteral, Equality, + Inequality, Equals(SingleChar('='), Promotion('=', Equality)), Plus(SingleChar('+')), Minus(SingleChar('-')), @@ -17,7 +18,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) { RightBracket(SingleChar(']')), LeftParentheses(SingleChar('(')), RightParentheses(SingleChar(')')), - Negation(SingleChar('!')), + Negation(SingleChar('!'), Promotion('=', Inequality)), Comma(SingleChar(',')), False(Keyword("false")), True(Keyword("true")),