Expressions

This commit is contained in:
Andrey Breslav
2010-11-18 14:38:11 +03:00
parent 0bd6020f5a
commit 661931469b
4 changed files with 108 additions and 18 deletions
+29
View File
@@ -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<T : this> : IComparable<T> {
val bits : Int
[operator] fun plus(other : T) : T
[operator] fun shl(bits : Int) : T
...
}
+1 -1
View File
@@ -40,7 +40,7 @@ typeParameter
;
typeConstraint
: userType ":" userType
: userType ":" (userType | "this")
// TODO: other constraints, maybe
;
+74 -14
View File
@@ -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 "]"
;
+4 -3
View File
@@ -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+;