Skip to content

Commit

Permalink
Support optional else branch (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
stonechoe authored Jul 23, 2024
1 parent e3b173f commit 236ffa8
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/main/resources/manuals/funcs/BigInt::exponentiate.ir
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ def <NUM>:BigInt::exponentiate(
): Normal[BigInt] | Abrupt[throw] = {
if (< exponent 0n) {
return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%RangeError.prototype%"], "ErrorData" -> undefined)))
} else {}
}
if (&& (= base 0n) (= exponent 0n)) {
return 1n
} else {}
}
return (** base exponent)
// return ([bigInt] (** ([math] base) ([math] exponent)))
}
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/BigInt::leftShift.ir
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ def <NUM>:BigInt::leftShift(
return (<< x y)
// if (< y 0n) {
// return ([bigInt] (/ ([math] x) (** 2 (- ([math] y)))))
// } else {}
// }
// return ([bigInt] (* ([math] x) (** 2 ([math] y))))
}
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/INTRINSICS.EvalError.ir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.EvalError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ def <BUILTIN>:INTRINSICS.Function.prototype.toString(
// 1. Return ! CodePointsToString(_func_.[[SourceText]]).
if (&& (= (typeof func) @Object) (&& (! (= func.SourceText absent)) (= (typeof func.SourceText) @String))) {
return func.SourceText
} else {}
}

// 1. If _func_ is a <emu-xref href=\"#sec-built-in-function-objects\">built-in function object</emu-xref>, return an implementation-defined String source code representation of _func_. The representation must have the syntax of a |NativeFunction|. Additionally, if _func_ has an [[InitialName]] internal slot and _func_.[[InitialName]] is a String, the portion of the returned String that would be matched by |NativeFunctionAccessor?| |PropertyName| must be the value of _func_.[[InitialName]].
if (? func: "BuiltinFunctionObject") {
return (concat "function " func.InitialName "() { [native code] }")
} else {}
}

// 1. If Type(_func_) is Object and IsCallable(_func_) is *true*, return an implementation-defined String source code representation of _func_. The representation must have the syntax of a |NativeFunction|.
call %0 = clo<IsCallable>(func)
if (&& (= (typeof func) @Object) (= %0 true)) {
return "function () { [native code] }"
} else {}
}

return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
}
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/INTRINSICS.RangeError.ir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.RangeError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.ReferenceError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/INTRINSICS.SyntaxError.ir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.SyntaxError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/INTRINSICS.TypeError.ir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.TypeError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/manuals/funcs/INTRINSICS.URIError.ir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def <BUILTIN>:INTRINSICS.URIError(
let msg = [? %3]
call %4 = clo<CreateNonEnumerableDataPropertyOrThrow>(O, "message", msg)
[! %4]
} else {}
}
call %5 = clo<InstallErrorCause>(O, options)
[? %5]
return O
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/manuals/funcs/IsInTailPosition.ir
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ def IsInTailPosition(
call: Ast[CallExpression, MemberExpression, OptionalChain],
): Boolean = {
// If the source text matched by _call_ is non-strict code, return *false*.
if false return false else {}
if false return false

// If _call_ is not contained within a |FunctionBody|, |ConciseBody|, or |AsyncConciseBody|, return *false*.
// Let _body_ be the |FunctionBody|, |ConciseBody|, or |AsyncConciseBody| that most closely contains _call_.
%0 = false
%1 = call
while (&& (! %0) (! (= %1 absent))) {
%0 = (|| (|| (|| %0 (? %1: "FunctionBody")) (? %1: "ConciseBody")) (? %1: "AsyncConciseBody"))
if (! %0) %1 = %1.parent else {}
if (! %0) %1 = %1.parent
}
if (! %0) return false else {}
if (! %0) return false
let body = %1

// If _body_ is the |FunctionBody| of a |GeneratorBody|, return *false*.
// If _body_ is the |FunctionBody| of an |AsyncFunctionBody|, return *false*.
// If _body_ is the |FunctionBody| of an |AsyncGeneratorBody|, return *false*.
%2 = body.parent
if (! (= %2 absent)) {
if (&& (? body: "FunctionBody") (? %2: "GeneratorBody")) return false else {}
if (&& (? body: "FunctionBody") (? %2: "AsyncFunctionBody")) return false else {}
if (&& (? body: "FunctionBody") (? %2: "AsyncGeneratorBody")) return false else {}
} else {}
if (&& (? body: "FunctionBody") (? %2: "GeneratorBody")) return false
if (&& (? body: "FunctionBody") (? %2: "AsyncFunctionBody")) return false
if (&& (? body: "FunctionBody") (? %2: "AsyncGeneratorBody")) return false
}

// If body is an |AsyncConciseBody|, return false.
if (? body: "AsyncConciseBody") return false else {}
if (? body: "AsyncConciseBody") return false

// Return the result of HasCallInTailPosition of _body_ with argument _call_.
sdo-call %3 = body->HasCallInTailPosition(call)
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/manuals/funcs/RequireObjectCoercible.ir
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ def RequireObjectCoercible(
): Normal[ESValue] | Abrupt[throw] = {
if (= (typeof argument) @Undefined) {
return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
} else {}
}
if (= (typeof argument) @Null) {
return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
} else {}
if (= (typeof argument) @Boolean) return argument else {}
if (= (typeof argument) @Number) return argument else {}
if (= (typeof argument) @String) return argument else {}
if (= (typeof argument) @Symbol) return argument else {}
if (= (typeof argument) @BigInt) return argument else {}
if (= (typeof argument) @Object) return argument else {}
}
if (= (typeof argument) @Boolean) return argument
if (= (typeof argument) @Number) return argument
if (= (typeof argument) @String) return argument
if (= (typeof argument) @Symbol) return argument
if (= (typeof argument) @BigInt) return argument
if (= (typeof argument) @Object) return argument
assert false
}
16 changes: 8 additions & 8 deletions src/main/resources/manuals/funcs/ToBigInt.ir
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ def ToBigInt(
): Normal[BigInt] | Abrupt[throw] = {
call %0 = clo<ToPrimitive>(argument, ~number~)
let prim = [? %0]
if (= (typeof prim) @Undefined) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined))) else {}
if (= (typeof prim) @Null) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined))) else {}
if (= (typeof prim) @Undefined) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
if (= (typeof prim) @Null) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
if (= (typeof prim) @Boolean) {
if prim return 1n else return 0n
} else {}
if (= (typeof prim) @BigInt) return prim else {}
if (= (typeof prim) @Number) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined))) else {}
}
if (= (typeof prim) @BigInt) return prim
if (= (typeof prim) @Number) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
if (= (typeof prim) @String) {
call %1 = clo<StringToBigInt>(prim)
let n = %1
if (= n undefined) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%SyntaxError.prototype%"], "ErrorData" -> undefined))) else {}
if (= n undefined) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%SyntaxError.prototype%"], "ErrorData" -> undefined)))
return n
} else {}
if (= (typeof prim) @Symbol) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined))) else {}
}
if (= (typeof prim) @Symbol) return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
assert false
}
16 changes: 8 additions & 8 deletions src/main/resources/manuals/funcs/ToBoolean.ir
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
def ToBoolean(
argument: ESValue,
): Boolean = {
if (= (typeof argument) @Undefined) return false else {}
if (= (typeof argument) @Null) return false else {}
if (= (typeof argument) @Boolean) return argument else {}
if (= (typeof argument) @Undefined) return false
if (= (typeof argument) @Null) return false
if (= (typeof argument) @Boolean) return argument
if (= (typeof argument) @BigInt) {
if (= argument 0n) return false else return true
} else {}
}
if (= (typeof argument) @Number) {
if (|| (|| (= argument 0.0f) (= argument -0.0f)) (= argument NaN)) return false else return true
} else {}
}
if (= (typeof argument) @String) {
if (= argument "") return false else return true
} else {}
if (= (typeof argument) @Symbol) return true else {}
if (= (typeof argument) @Object) return true else {}
}
if (= (typeof argument) @Symbol) return true
if (= (typeof argument) @Object) return true
assert false
}
16 changes: 8 additions & 8 deletions src/main/resources/manuals/funcs/ToObject.ir
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ def ToObject(
): Normal[Object] | Abrupt[throw] = {
if (= (typeof argument) @Undefined) {
return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
} else {}
}
if (= (typeof argument) @Null) {
return comp[~throw~/~empty~]((new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%TypeError.prototype%"], "ErrorData" -> undefined)))
} else {}
}
if (= (typeof argument) @Boolean) {
return (new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%Boolean.prototype%"], "BooleanData" -> argument, "Extensible" -> true))
} else {}
}
if (= (typeof argument) @Number) {
return (new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%Number.prototype%"], "NumberData" -> argument, "Extensible" -> true))
} else {}
}
if (= (typeof argument) @String) {
let obj = (new StringExoticObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%String.prototype%"], "StringData" -> argument, "Extensible" -> true))
obj.SubMap.length = (new PropertyDescriptor("Value" -> ([number] argument.length), "Writable" -> false, "Enumerable" -> false, "Configurable" -> false))
return obj
} else {}
}
if (= (typeof argument) @Symbol) {
let obj = (new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%Symbol.prototype%"], "SymbolData" -> argument, "Extensible" -> true))
return obj
} else {}
}
if (= (typeof argument) @BigInt) {
let obj = (new OrdinaryObject("Prototype" -> @EXECUTION_STACK[0].Realm.Intrinsics["%BigInt.prototype%"], "BigIntData" -> argument, "Extensible" -> true))
return obj
} else {}
if (= (typeof argument) @Object) return argument else {}
}
if (= (typeof argument) @Object) return argument
assert false
}
Loading

0 comments on commit 236ffa8

Please sign in to comment.