Skip to content
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

Add code generation for bbml #245

Merged
merged 36 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f654551
Map builtin operators
NeilKleistGao Nov 26, 2024
270ff3e
WIP: Fix num and sel code gen & add predef
NeilKleistGao Nov 26, 2024
6896564
WIP: Add throw
NeilKleistGao Nov 26, 2024
c25ebd5
WIP: Fix predef
NeilKleistGao Nov 26, 2024
b9e3782
Fix getter generation & split.end typing
NeilKleistGao Nov 27, 2024
dc58521
Generate code for region & ref
NeilKleistGao Nov 27, 2024
7c56b26
WIP: Add tests
NeilKleistGao Nov 29, 2024
a40e1cc
Merge from main branch
NeilKleistGao Nov 29, 2024
c2deec1
Fix class matching typing
NeilKleistGao Nov 30, 2024
ddeb04a
Merge from main branch
NeilKleistGao Nov 30, 2024
a81aae2
Fix num ops code gen
NeilKleistGao Nov 30, 2024
dae1d36
Fix getter generation
NeilKleistGao Dec 1, 2024
4afe46f
Clean
NeilKleistGao Dec 1, 2024
b0c3f38
Merge from mlscript
NeilKleistGao Dec 1, 2024
a683de6
Update bbml predef
NeilKleistGao Dec 1, 2024
af42e40
Minor
NeilKleistGao Dec 1, 2024
182237f
Reuse getBuiltin
NeilKleistGao Dec 4, 2024
5443f1f
Fix GetElem implementation
NeilKleistGao Dec 4, 2024
89f5c6c
Merge branch 'hkmc2' of https://github.com/hkust-taco/mlscript into b…
NeilKleistGao Dec 5, 2024
ebe9103
Rerun tests
NeilKleistGao Dec 5, 2024
8e63fae
Move defn check to symbol
NeilKleistGao Dec 6, 2024
0ff406f
Fix module getter generation and ctx.get use
NeilKleistGao Dec 6, 2024
ce6f2cf
Refactor getter typing
NeilKleistGao Dec 6, 2024
e0a22ad
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
NeilKleistGao Dec 8, 2024
313d680
Merge from main branch
NeilKleistGao Dec 8, 2024
0bb46f1
WIP: Fix shadowing
NeilKleistGao Dec 8, 2024
5557984
Forbid getters from being defined in non-module and non-function scopes
NeilKleistGao Dec 8, 2024
52e2bd2
Update hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala
NeilKleistGao Dec 12, 2024
ae7bfd5
Remove top-level getter selection check
NeilKleistGao Dec 12, 2024
bbc8f9f
Add empty lines
NeilKleistGao Dec 12, 2024
710831f
Fix diff
NeilKleistGao Dec 12, 2024
6e7a96a
Fix diff
NeilKleistGao Dec 12, 2024
7ca79b9
Fix getter logic
NeilKleistGao Dec 12, 2024
634748d
Merge from main branch
NeilKleistGao Dec 12, 2024
e0c2e5e
Minor changes
NeilKleistGao Dec 12, 2024
ea0c9c8
Remove the unused import and update the comment
NeilKleistGao Dec 12, 2024
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
9 changes: 2 additions & 7 deletions hkmc2/shared/src/main/scala/hkmc2/bbml/bbML.scala
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,8 @@ class BBTyper(using elState: Elaborator.State, tl: TL):
case _ => (error(msg"${field.name} is not a valid member in class ${clsSym.nme}" -> t.toLoc :: Nil), Bot)
case N =>
(error(msg"Not a valid class: ${cls.describe}" -> cls.toLoc :: Nil), Bot)
case Term.App(lhs @ Term.SynthSel(_, Ident(nme)), Term.Tup(Nil)) if ctx.ctx.get(nme).map(e => e match {
case _: Elaborator.Ctx.GetElem => true
case _ => false
}).getOrElse(lhs.sym.map(_.defn match {
case S(TermDefinition(_, Fun, _, Nil, _, S(_), _, _)) => true
case _ => false
}).getOrElse(false)) => typeCheck(lhs) // * Gettters
case Term.App(lhs: Term.SynthSel, Term.Tup(Nil)) if lhs.sym.map(_.isGetter).getOrElse(false) =>
typeCheck(lhs) // * Getter access will be elaborated to applications. But they cannot be typed as normal applications.
case t @ Term.App(lhs, Term.Tup(rhs)) =>
val (funTy, lhsEff) = typeCheck(lhs)
app((funTy, lhsEff), rhs, t)
Expand Down
4 changes: 4 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ class JSBuilder(using Elaborator.State, Elaborator.Ctx) extends CodeBuilder:
doc" # ${td.sym.nme}($params) { #{ # ${
bodyDoc
} #} # }"
case td @ FunDefn(_, Nil, bod) =>
doc" # ${td.sym.nme}() { #{ # ${
NeilKleistGao marked this conversation as resolved.
Show resolved Hide resolved
this.body(bod)
} #} # }"
.mkDocument(" ")
}${
if mtds.exists(_.sym.nme == "toString")
Expand Down
12 changes: 5 additions & 7 deletions hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ extends Importer:
def mkLetBinding(sym: LocalSymbol, rhs: Term): Ls[Statement] =
LetDecl(sym) :: DefineVar(sym, rhs) :: Nil

def resolveField(srcTree: Tree, base: Opt[Symbol], nme: Ident): Opt[FieldSymbol] =
def resolveField(srcTree: Tree, base: Opt[Symbol], nme: Ident)(using Ctx): Opt[FieldSymbol] =
base match
case S(psym: BlockMemberSymbol) =>
psym.modTree match
Expand All @@ -170,6 +170,9 @@ extends Importer:
N
case N =>
N
case S(_: TopLevelSymbol) => ctx.get(nme.name).flatMap(_.symbol) match
case S(sym: FieldSymbol) => S(sym)
NeilKleistGao marked this conversation as resolved.
Show resolved Hide resolved
case _ => N
case _ => N

def cls(tree: Tree, inAppPrefix: Bool): Ctxl[Term] = trace[Term](s"Elab class ${tree.showDbg}", r => s"~> $r"):
Expand Down Expand Up @@ -355,12 +358,7 @@ extends Importer:
val res = if inAppPrefix
then Term.SynthSel(preTrm, nme)(sym)
else Term.Sel(preTrm, nme)(sym)
val isGetter = ctx.get(nme.name) match // TODO: create a function for this logic
case S(_: Ctx.GetElem) => true
case _ => sym.flatMap(_.defn) match
case S(TermDefinition(_, Fun, _, Nil, _, S(_), _, _)) => true
case _ => false
if isGetter then
if sym.map(_.isGetter).getOrElse(false) then
NeilKleistGao marked this conversation as resolved.
Show resolved Hide resolved
val emptyTup: Tree.Tup = Tree.Tup(Nil)
Term.App(res, Term.Tup(Nil)(emptyTup))(Tree.App(t, emptyTup), FlowSymbol("‹get-res›"))
else res
Expand Down
3 changes: 3 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/semantics/Symbol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ end BlockMemberSymbol
sealed abstract class MemberSymbol[Defn <: Definition](using State) extends Symbol:
def nme: Str
var defn: Opt[Defn] = N
lazy val isGetter: Bool = defn match
case S(TermDefinition(_, Fun, _, Nil, _, S(_), _, _)) => true
case _ => false
NeilKleistGao marked this conversation as resolved.
Show resolved Hide resolved


class TermSymbol(val k: TermDefKind, val owner: Opt[InnerSymbol], val id: Tree.Ident)(using State)
Expand Down
36 changes: 36 additions & 0 deletions hkmc2/shared/src/test/mlscript/codegen/Getters.mls
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ globalThis.t
//│ = 42


this.t
//│ = 42


:sjs
module M with
fun t = 0
//│ JS:
//│ const M$class = class M {
//│ constructor() {
//│
//│ }
//│ t() {
//│ return 0;
//│ }
//│ toString() { return "M"; }
//│ };
//│ this.M = new M$class;
//│ this.M.class = M$class;
//│ undefined


:sjs
M.t
//│ JS:
//│ let selRes, tmp;
//│ selRes = this.M.t;
//│ if (selRes === undefined) {
//│ throw new this.Error("Access to required field 't' yielded 'undefined'");
//│ } else {
//│ tmp = selRes;
//│ }
//│ tmp() ?? null
//│ = 0


:sjs
fun test() =
fun whoops = 42
Expand Down
Loading