Skip to content

Commit

Permalink
Reuse getBuiltin
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Dec 4, 2024
1 parent af42e40 commit 182237f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
4 changes: 3 additions & 1 deletion hkmc2/jvm/src/test/scala/hkmc2/BbmlDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ abstract class BbmlDiffMaker extends JSBackendDiffMaker:
if isGlobal then typeCheck.disable.isGlobal = true
typeCheck.disable.setCurrentValue(())
if file =/= bbPreludeFile then
curCtx = Elaborator.State.init
importFile(bbPreludeFile, verbose = false)

curCtx = curCtx.nest(N)

override def init(): Unit =
if bbmlOpt.isSet then
import syntax.*
Expand Down
9 changes: 3 additions & 6 deletions hkmc2/shared/src/main/scala/hkmc2/bbml/bbML.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ final case class BbCtx(
env: HashMap[Uid[Symbol], GeneralType]
):
def +=(p: Symbol -> GeneralType): Unit = env += p._1.uid -> p._2
def get(sym: Symbol): Option[GeneralType] =
if BbCtx.builtinOps(sym.nme) then ctx.get(s"#${sym.nme}") match
case S(Ctx.SelElem(_, _, symOpt)) => symOpt.flatMap(getImpl(_))
case _ => N
else getImpl(sym)
private def getImpl(sym: Symbol): Option[GeneralType] = env.get(sym.uid) orElse parent.dlof(_.getImpl(sym))(None)
def get(sym: Symbol): Option[GeneralType] = env.get(sym.uid) orElse parent.dlof(_.get(sym))(None)
def getCls(name: Str): Option[TypeSymbol] =
for
elem <- ctx.get(name)
Expand Down Expand Up @@ -443,6 +438,8 @@ class BBTyper(using elState: Elaborator.State, tl: TL):
goStats(stats)
case (clsDef: ClassDef) :: stats =>
goStats(stats)
case (modDef: ModuleDef) :: stats =>
goStats(stats)
case Import(sym, pth) :: stats =>
goStats(stats) // TODO:
goStats(stats)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class JSBuilder(using Elaborator.State, Elaborator.Ctx) extends CodeBuilder:
case Call(Value.Ref(l: BuiltinSymbol), args) =>
err(msg"Illeal arity for builtin symbol '${l.nme}'")
case Call(s @ Select(_, id), lhs :: rhs :: Nil) =>
Elaborator.ctx.Builtins.tryMapOp(id.name) match
Elaborator.ctx.Builtins.getBuiltinOp(id.name) match
case S(jsOp) =>
val res = doc"${result(lhs)} ${jsOp} ${result(rhs)}"
if needsParens(jsOp) then doc"(${res})" else res
Expand Down
7 changes: 5 additions & 2 deletions hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ object Elaborator:
";" -> ",",
"+." -> "+",
"-." -> "-",
"*." -> "*")
"*." -> "*",
"/." -> "/")
private val builtinBinOps = aliasOps ++ (binaryOps.map: op =>
op -> op).toMap

val reservedNames = binaryOps.toSet ++ aliasOps.keySet + "NaN" + "Infinity"

Expand Down Expand Up @@ -91,7 +94,7 @@ object Elaborator:
val Num = assumeBuiltinCls("Num")
val Str = assumeBuiltinCls("Str")
val Predef = assumeBuiltinMod("Predef")
def tryMapOp(op: Str): Opt[Str] = aliasOps.get(op)
def getBuiltinOp(op: Str): Opt[Str] = if getBuiltin(op).isDefined then builtinBinOps.get(op) else N

object Ctx:
abstract class Elem:
Expand Down
34 changes: 22 additions & 12 deletions hkmc2/shared/src/test/mlscript/bbml/bbPrelude.mls
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// :bbml


class Any
class Nothing

class Alloc

class Bool
class Int
class Num

class CodeBase[T, C, S]
class Region[T]
class Ref[T, S](reg: Region[T], value: S)
Expand All @@ -10,26 +18,28 @@ class Str(length: Int, concat: Str -> Str)

class Error(msg: Str)

declare module Predef

fun run: [T] -> CodeBase[out T, out Nothing, out Any] -> T
fun log: Str -> Any
fun error: Nothing

fun (#+): (Int, Int) -> Int
fun (#-): (Int, Int) -> Int
fun (#*): (Int, Int) -> Int
fun (#/): (Int, Int) -> Num
fun (+): (Int, Int) -> Int
fun (-): (Int, Int) -> Int
fun (*): (Int, Int) -> Int
fun (/): (Int, Int) -> Num
fun (+.): (Num, Num) -> Num
fun (-.): (Num, Num) -> Num
fun (*.): (Num, Num) -> Num
fun (/.): (Num, Num) -> Num

fun (#<): (Int, Int) -> Bool
fun (#>): (Int, Int) -> Bool
fun (#<=): (Int, Int) -> Bool
fun (#>=): (Int, Int) -> Bool
fun (#==): [T] -> (T, T) -> Bool
fun (#!=): [T] -> (T, T) -> Bool
fun (<): (Int, Int) -> Bool
fun (>): (Int, Int) -> Bool
fun (<=): (Int, Int) -> Bool
fun (>=): (Int, Int) -> Bool
fun (==): [T] -> (T, T) -> Bool
fun (!=): [T] -> (T, T) -> Bool

fun (#&&): (Bool, Bool) -> Bool
fun (#||): (Bool, Bool) -> Bool
fun (&&): (Bool, Bool) -> Bool
fun (||): (Bool, Bool) -> Bool

0 comments on commit 182237f

Please sign in to comment.