Skip to content

Add number literal type #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions samples/numberlit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare module numberlit {

export type HttpStatuscode = 200 | 404 | 503 ;

// 1 to Int, 1.0 (which is valid-int) to Double
export function floating(prob: 0.1 | 0.5 | 1.0): 0.0 | 1

export interface Machine {
state?: 0 | 1;

setState(flag: 0 | 1 | boolean): 0 | 1;
}
}
25 changes: 25 additions & 0 deletions samples/numberlit.ts.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import scala.scalajs.js
import js.annotation._
import js.|

package numberlit {

package numberlit {

@js.native
trait Machine extends js.Object {
var state: Int = js.native
def setState(flag: Int | Boolean): Int = js.native
}

@js.native
@JSGlobal("numberlit")
object Numberlit extends js.Object {
type HttpStatuscode = Int
def floating(prob: Double): Double | Int = js.native
}

}

}
6 changes: 6 additions & 0 deletions src/main/scala/org/scalajs/tools/tsimporter/Importer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ class Importer(val output: java.io.PrintWriter) {
case ConstantType(StringLiteral(_)) =>
TypeRef.String

case ConstantType(IntLiteral(i)) =>
TypeRef.Int

case ConstantType(DoubleLiteral(d)) =>
TypeRef.Double

case ObjectType(List(IndexMember(_, TypeRefTree(CoreType("string"), _), valueType))) =>
val valueTpe = typeToScala(valueType)
TypeRef(QualifiedName.Dictionary, List(valueTpe))
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/org/scalajs/tools/tsimporter/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ object Trees {

case class BooleanLiteral(value: Boolean) extends Literal

case class NumberLiteral(value: Double) extends Literal
sealed trait NumberLiteral extends Literal

case class IntLiteral(value: Int) extends NumberLiteral

case class DoubleLiteral(value: Double) extends NumberLiteral

case class StringLiteral(value: String) extends Literal with PropertyName {
override def name = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class TSDefLexical extends Lexical with StdTokens with ImplicitConversions {
// not standard, but I guess it could happen nevertheless
digits => digits.foldLeft(0L)(_ * 8 + _).toString
}
| success("0")
)
| stringOf1(digit) ~ opt(stringOf1('.', digit)) ^^ {
case part1 ~ part2 => part1 + (part2.getOrElse(""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
lazy val paramType: Parser[TypeTree] = (
typeDesc
| stringLiteral ^^ ConstantType
| numberLiteral ^^ ConstantType
)

lazy val optResultType =
Expand Down Expand Up @@ -216,6 +217,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
| objectType
| functionType
| stringType
| numberType
| typeQuery
| tupleType
| thisType
Expand Down Expand Up @@ -247,6 +249,9 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
lazy val stringType: Parser[TypeTree] =
stringLiteral ^^ ConstantType

lazy val numberType: Parser[TypeTree] =
numberLiteral ^^ ConstantType

lazy val thisType: Parser[TypeTree] =
"this" ^^^ PolymorphicThisType

Expand Down Expand Up @@ -319,6 +324,16 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
lazy val stringLiteral: Parser[StringLiteral] =
stringLit ^^ StringLiteral

lazy val numberLiteral: Parser[NumberLiteral] =
numericLit ^^ { s =>
val d = s.toDouble
if (!s.contains(".") && d.isValidInt) {
IntLiteral(d.toInt)
} else {
DoubleLiteral(d)
}
}

private val isCoreTypeName =
Set("any", "void", "number", "bool", "boolean", "string", "null", "undefined", "never")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ object TypeRef {
val Any = TypeRef(scala_js dot Name("Any"))
val Dynamic = TypeRef(scala_js dot Name("Dynamic"))
val Double = TypeRef(scala dot Name("Double"))
val Int = TypeRef(scala dot Name("Int"))
val Boolean = TypeRef(scala dot Name("Boolean"))
val String = TypeRef(java_lang dot Name("String"))
val Object = TypeRef(scala_js dot Name("Object"))
Expand Down