From 661931469b96e1f645553709e3ed5a1529eb9fab Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 18 Nov 2010 14:38:11 +0300 Subject: [PATCH] Expressions --- examples/src/BitArith.jetl | 29 ++++++++++++ grammar/src/class.grm | 2 +- grammar/src/expressions.grm | 88 +++++++++++++++++++++++++++++++------ grammar/src/lexical.grm | 7 +-- 4 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 examples/src/BitArith.jetl diff --git a/examples/src/BitArith.jetl b/examples/src/BitArith.jetl new file mode 100644 index 00000000000..678554993d2 --- /dev/null +++ b/examples/src/BitArith.jetl @@ -0,0 +1,29 @@ +fun oneBit(index : Int) = 1 shl index + +fun setBit(x : Int, index : Int) = x or oneBit(index) +fun unsetBit(x : Int, index : Int) = x and not(oneBit(index)) + +fun getBit(x : Int, index : Int) = x and oneBit(index) != 0 +fun getBit(x : Int, index : Int) = (x shr index) shl 31 != 0 + +fun countOnes(x : INumber) { + var result = 0 + while (x != 0) { + result += x and 1 + x = x ushr 1 + } + result +} + +fun mostSignificantBit(x : INumber) = (x and oneBit(x.bits - 1) != 0) as Int + +fun countOnes(x : INumber) = if (x == 0) 0 else mostSignificantBit(x) + countOnes(x shl 1) + +extension fun Int.matchMask(mask : Int) = this and mask == mask + +interface class INumber : IComparable { + val bits : Int + [operator] fun plus(other : T) : T + [operator] fun shl(bits : Int) : T + ... +} \ No newline at end of file diff --git a/grammar/src/class.grm b/grammar/src/class.grm index f3ed4ce6b2c..c05779f0d22 100644 --- a/grammar/src/class.grm +++ b/grammar/src/class.grm @@ -40,7 +40,7 @@ typeParameter ; typeConstraint - : userType ":" userType + : userType ":" (userType | "this") // TODO: other constraints, maybe ; diff --git a/grammar/src/expressions.grm b/grammar/src/expressions.grm index 3bb9323f146..7a514975c10 100644 --- a/grammar/src/expressions.grm +++ b/grammar/src/expressions.grm @@ -1,40 +1,59 @@ expression : "(" expression ")" // see tupleLiteral - : "new" constructorInvocation : literalConstant : functionLiteral : tupleLiteral : listLiteral : mapLiteral - : typeLiteral : "null" + : "this" + : memberAccessExpression + : infixFunctionCall : binOpExpression + : assignment : unOpExpression - : functionCall - : arrayAccess + : castExpression : match - : FieldName : if + : "new" constructorInvocation + : anonymousInnerClassInstantiation // TODO // TODO: list comprehension : jump ; +castExpression + : expression "as" type + ; + +binOpExpression + : expression binaryOperation expression // see priorities + ; + +unOpExpression + : expression postfixUnaryOperation + : prefixUnaryOperation expression + ; + binaryOperation // Decreasing precedence : "*" : "/" // No % : "+" : "-" // No << >> >>> - : "<" : ">" : ">=" : "<=" : "is" : "isnot" : "in" : "as" // TODO: Check the precedence for in carefully - : "==" : "===" + : "<" : ">" : ">=" : "<=" : "is" : "isnot" : "in" // TODO: Check the precedence for in carefully + : "!=" : "==" : "===" // No | & ^ ~ : "&&" : "||" ; -assignments +assignmentOperator : "=" : "+=" : "-=" : "*=" : "/=" // TODO: |=, %= and <<= make more sense than |, % or << alone, and so for others ; +assignment + : memberAccessExpression assignmentOperator expression // Assignment to functions prohibited by a semantic check + ; + prefixUnaryOperation : "-" : "+" : "++" : "--" @@ -46,9 +65,22 @@ postfixUnaryOperation ; functionCall - : completeFunctionCall - : onlyTypeParameters - : infixFunctionCall + : SimpleName typeArguments valueArguments? functionLiteral? + : SimpleName valueArguments functionLiteral? + ; + +typeArguments + : "<" type{","} ">" + ; + +valueArguments + : "(" expression{","} ")" + ; + +infixFunctionCall + : expression SimpleName typeArguments expression? + : expression SimpleName expression + ; jump : "throw" expression @@ -58,9 +90,9 @@ jump // yield ? ; -tupleLiteral // No, (a) is not a tuple - : "(" ")" - : "(" expression ("," expression)+ ")" +tupleLiteral // Ambiguity when after a SimpleName (infix call). In this case (e) is treated as an expression in parentheses + // to put a tuple, write write ((e)) + : "(" expression{","} ")" ; functionLiteral @@ -84,4 +116,32 @@ mapLiteral mapEntryLiteral : expression ":" expression + ; + +literalConstant + : booleanLiteral + : IntegerLiteral + : HexadecimalLiteral + : CharacterLiteral + : SingleLineStringLiteral + : SingleLineStringTemplate + : MultiLineStringLiteral + : MultiLineStringTemplate + ; + +memberAccessExpression + : (expression ".")? memberAccess + ; + +memberAccess + : functionCall + : fieldOrPropertyAccess + ; + +fieldOrPropertyAccess + : (FieldName | SimpleName) arrayAccess? + ; + +arrayAccess + : "[" expression "]" ; \ No newline at end of file diff --git a/grammar/src/lexical.grm b/grammar/src/lexical.grm index 26e8e971526..0b11e17837d 100644 --- a/grammar/src/lexical.grm +++ b/grammar/src/lexical.grm @@ -1,5 +1,6 @@ - - SimpleName : /*java identifier*/; FieldName : "$" SimpleName; -name : /*possibly qualified name*/; +[helper] Digit : ["0".."9"]; +IntegerLiteral : Digit+ "L"? +[helper] HaxDigit : Digit | ["A"-"F", "a"-"f"]; +HexadecimaLiteral : "0x" HexDigit+; \ No newline at end of file