Formal JavaScript Semantics
Grammar
|
|
Wednesday, February 28, 2001
Types
Semantics
type Value
=
oneof {
undefinedValue;
nullValue;
booleanValue: Boolean;
numberValue: Float64;
stringValue: String;
objectValue: Object}
type ObjectOrNull =
oneof {nullObjectOrNull; objectObjectOrNull: Object}
type DefaultValueHint =
oneof {noHint; numberHint; stringHint}
type Property
=
tuple {
name: String;
readOnly: Boolean;
enumerable: Boolean;
permanent: Boolean;
value: Value}
type Reference
=
oneof {valueReference: Value; placeReference: Place; virtualReference: PropName}
type IntegerOrException =
oneof {normal: Integer; abrupt: Exception}
type VoidOrException =
oneof {normal; abrupt: Exception}
type BooleanOrException =
oneof {normal: Boolean; abrupt: Exception}
type Float64OrException =
oneof {normal: Float64; abrupt: Exception}
type StringOrException =
oneof {normal: String; abrupt: Exception}
type ValueListOrException =
oneof {normal: Value[]; abrupt: Exception}
Helper Functions
Semantics
Exceptions
Semantics
type Exception =
oneof {exception: Value; error: Error}
type Error
=
oneof {
coerceToPrimitiveError;
coerceToObjectError;
getValueError;
putValueError;
deleteError}
Objects
Conversions
Semantics
Coercions
Semantics
coerceBooleanToFloat64(
b:
Boolean) :
Float64 =
if b then 1.0
else +0.0
float64ToUint32(
x:
Float64) :
Integer
=
if float64IsNaN(
x)
or float64IsInfinite(
x)
then 0
else truncateFloat64(
x)
mod4294967296
uint32ToInt32(
ui:
Integer) :
Integer =
if ui < 2147483648
then ui else ui - 4294967296
Environments
Semantics
Terminal Actions
Semantics
action EvalIdentifier[Identifier] : String
action EvalNumber[Number] : Float64
action EvalString[String] : String
Primary Expressions
Syntax
this
| null
| true
| false
| Number
| String
Semantics
Left-Side Expressions
Syntax
ExprKind {anyValue, noLValue}
MemberExprKind {call, noCall}
MemberExpressionnoCall,noLValue
MemberExpressionnoCall,anyValue
Semantics
Postfix Expressions
Syntax
PostfixExpressionanyValue
PostfixExpressionnoLValue
Semantics
Unary Operators
Syntax
Semantics
Multiplicative Operators
Syntax
MultiplicativeExpressionExprKind
Semantics
Additive Operators
Syntax
AdditiveExpressionExprKind
Semantics
Bitwise Shift Operators
Syntax
Semantics
Relational Operators
Syntax
RelationalExpressionExprKind
Semantics
orderValues(
leftValue:
Value,
rightValue:
Value,
less:
Boolean,
greaterOrEqual:
Boolean)
:
ValueOrException
=
letexc leftPrimitive:
Value =
coerceToPrimitive(
leftValue,
numberHint)
in letexc rightPrimitive:
Value =
coerceToPrimitive(
rightValue,
numberHint)
in if leftPrimitive is stringValue and rightPrimitive is stringValue
then booleanResult(
compareStrings(
leftPrimitive.
stringValue,
rightPrimitive.
stringValue,
less,
greaterOrEqual,
greaterOrEqual))
else letexc leftNumber:
Float64 =
coerceToFloat64(
leftPrimitive)
in letexc rightNumber:
Float64 =
coerceToFloat64(
rightPrimitive)
in booleanResult(
float64Compare(
leftNumber,
rightNumber,
less,
greaterOrEqual,
greaterOrEqual,
false))
compareStrings(
left:
String,
right:
String,
less:
Boolean,
equal:
Boolean,
greater:
Boolean) :
Boolean
=
if |
left| = 0
and |
right| = 0
then equal
else if |
left| = 0
then less
else if |
right| = 0
then greater
else let leftCharCode:
Integer =
characterToCode(
left[0]);
rightCharCode:
Integer =
characterToCode(
right[0])
in if leftCharCode <
rightCharCode
then less
else if leftCharCode >
rightCharCode
then greater
else compareStrings(
left[1 ...],
right[1 ...],
less,
equal,
greater)
Equality Operators
Syntax
EqualityExpressionExprKind
Semantics
compareValues(
leftValue:
Value,
rightValue:
Value) :
BooleanOrException
=
case leftValue of
undefinedValue,
nullValue:
case rightValue of
undefinedValue,
nullValue:
normal true;
booleanValue,
numberValue,
stringValue,
objectValue:
normal false
end;
booleanValue(
leftBool:
Boolean):
case rightValue of
undefinedValue,
nullValue:
normal false;
booleanValue(
rightBool:
Boolean):
normal (
not (
leftBool xor rightBool));
numberValue,
stringValue,
objectValue:
compareFloat64ToValue(
coerceBooleanToFloat64(
leftBool),
rightValue)
end;
numberValue(
leftNumber:
Float64):
compareFloat64ToValue(
leftNumber,
rightValue);
stringValue(
leftStr:
String):
case rightValue of
undefinedValue,
nullValue:
normal false;
booleanValue(
rightBool:
Boolean):
letexc leftNumber:
Float64 =
coerceToFloat64(
leftValue)
in normal float64Equal(
leftNumber,
coerceBooleanToFloat64(
rightBool));
numberValue(
rightNumber:
Float64):
letexc leftNumber:
Float64 =
coerceToFloat64(
leftValue)
in normal float64Equal(
leftNumber,
rightNumber);
stringValue(
rightStr:
String):
normal compareStrings(
leftStr,
rightStr,
false,
true,
false);
objectValue:
letexc rightPrimitive:
Value =
coerceToPrimitive(
rightValue,
noHint)
in compareValues(
leftValue,
rightPrimitive)
end;
objectValue(
leftObj:
Object):
case rightValue of
undefinedValue,
nullValue:
normal false;
booleanValue(
rightBool:
Boolean):
letexc leftPrimitive:
Value =
coerceToPrimitive(
leftValue,
noHint)
in compareValues(
leftPrimitive,
numberValue coerceBooleanToFloat64(
rightBool));
numberValue,
stringValue:
letexc leftPrimitive:
Value =
coerceToPrimitive(
leftValue,
noHint)
in compareValues(
leftPrimitive,
rightValue);
objectValue(
rightObj:
Object):
normal (
leftObj.
properties rightObj.
properties)
end
end
float64Equal(
x:
Float64,
y:
Float64) :
Boolean
=
float64Compare(
x,
y,
false,
true,
false,
false)
strictCompareValues(
leftValue:
Value,
rightValue:
Value) :
Boolean
=
case leftValue of
undefinedValue:
rightValue is undefinedValue;
nullValue:
rightValue is nullValue;
booleanValue(
leftBool:
Boolean):
case rightValue of
booleanValue(
rightBool:
Boolean):
not (
leftBool xor rightBool);
undefinedValue,
nullValue,
numberValue,
stringValue,
objectValue:
false
end;
numberValue(
leftNumber:
Float64):
case rightValue of
numberValue(
rightNumber:
Float64):
float64Equal(
leftNumber,
rightNumber);
undefinedValue,
nullValue,
booleanValue,
stringValue,
objectValue:
false
end;
stringValue(
leftStr:
String):
case rightValue of
stringValue(
rightStr:
String):
compareStrings(
leftStr,
rightStr,
false,
true,
false);
undefinedValue,
nullValue,
booleanValue,
numberValue,
objectValue:
false
end;
objectValue(
leftObj:
Object):
case rightValue of
objectValue(
rightObj:
Object):
leftObj.
properties rightObj.
properties;
undefinedValue,
nullValue,
booleanValue,
numberValue,
stringValue:
false
end
end
Binary Bitwise Operators
Syntax
BitwiseAndExpressionExprKind
BitwiseXorExpressionExprKind
BitwiseOrExpressionExprKind
Semantics
Binary Logical Operators
Syntax
LogicalAndExpressionExprKind
LogicalOrExpressionExprKind
Semantics
Conditional Operator
Syntax
ConditionalExpressionExprKind
Semantics
Assignment Operators
Syntax
AssignmentExpressionExprKind
Expressions
Syntax
Semantics
Syntax
Semantics
Programs
Syntax
Semantics