Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自作言語進捗 2020 Mar / ojaml-2020-mar

自作言語進捗 2020 Mar / ojaml-2020-mar

todesking

March 24, 2020
Tweet

More Decks by todesking

Other Decks in Technology

Transcript

  1. ࣗ࡞ݴޠਐḿ
    2020 Mar
    @todesking

    View full-size slide

  2. લճ
    • Q: ࠓ·ͰԿΛ?

    • A: ৸ͯ·ͨ͠……

    • ࠷ۙى͖ͨͷͰ։ൃΛ࠶։ͨ͠

    View full-size slide

  3. ֓ཁ
    • JVMͰಈ͘ίϯύΠϥݴޠΛ࡞͍ͬͯ·͢

    • MLܥؔ਺ܕݴޠ

    • https://github.com/todesking/ojaml/

    View full-size slide

  4. Ͱ͖Δ͜ͱ
    module A {
    let add x y = x + y ;;
    let x = add 1 2 ;;
    let add1 = add 1 ;;
    let y = add1 10 ;;
    let app f x = f x ;;
    let z = app (fun x => x + 1) 1 ;;
    let num_to_str n: int =
    java.lang.Integer##toString(n) ;;
    }
    ؔ਺ఆٛͱݺͼग़͠
    ෦෼ద༻
    Javaϝιουݺͼग़͠
    ߴ֊ؔ਺

    View full-size slide

  5. Ͱ͖Δ͜ͱ
    module A {
    let s =
    if x == y then "eq"
    else "neq"
    let f x =
    let tmp = x + 1 in
    x * tmp ;;
    let x =
    let rec
    f x = g x
    g x = f x in
    f 0 ;;
    }
    ੍ޚߏจ
    ϩʔΧϧม਺
    ࠶ؼ

    View full-size slide

  6. Ͱ͖Δ͜ͱ
    module A {
    let id x = x ;; // id: [?1] ?1 -> ?1
    data Option a = Some a | None ;;
    let get_or_else o x =
    match o
    | Some a => a
    | None => x ;;
    let option_value = Some 1 ;;
    let value =
    get_or_else option_value 100 ;;
    }
    ύϥϝʔλଟ૬
    ܕਪ࿦
    ଟ૬ͳ୅਺తσʔλܕ
    ύλʔϯϚον

    View full-size slide

  7. ίϯύΠϥͷߏ੒
    Source Raw AST Named AST Typed AST JAST
    Parser Namer Typer Javalizer
    • Parser: ιʔείʔυ͔ΒAST΁ͷม׵

    • Namer: Ϋϥε໊΍ม਺໊ͷղܾ

    • Typer: ܕ෇͚

    • Javalizer: JavaόΠτίʔυʹ͍ۙதؒදݱʹม׵

    • Emitter: ΫϥεϑΝΠϧͷग़ྗ
    .class
    Emitter

    View full-size slide

  8. ΫϥεϑΝΠϧ΁ͷม׵
    • ιʔείʔυΛίϯύΠϧͯ͠.classϑΝΠϧͱͯ͠ग़ྗ
    ͍ͯ͠Δ

    • Ϟδϡʔϧ(ScaleͰݴ͏object૬౰)͕JavaͷΫϥεʹରԠ

    • letͰఆٛ͞Εͨม਺͸staticϑΟʔϧυͱͳΔ

    • ม਺ͷ஋͸static initializerͰॳظԽ͞ΕΔ
    module A {
    let n = 10 ;;
    }
    class A {
    static int n;
    static {
    A.n = 10;
    }
    }

    View full-size slide

  9. ΫϥεϑΝΠϧ΁ͷม׵
    • ؔ਺͸ΦϒδΣΫτͱͯ͠දݱ͞ΕΔ

    • ͢΂ͯͷؔ਺͸1Ҿ਺
    module A {
    let f x = x + 1 ;;
    }
    class A {
    static Fun f;
    static {
    A.f = new A$0();
    }
    }
    class A$0 extends Fun {
    override def app(x: Object) =
    (int)x + 1;
    }

    View full-size slide

  10. ΫϥεϑΝΠϧ΁ͷม׵
    • ෳ਺Ҿ਺ؔ਺͸ؔ਺Λฦؔ͢਺Ͱදݱ͞ΕΔ

    • ؔ਺ΦϒδΣΫτ͸֎ଆͷؔ਺΁ͷࢀরΛ௨ͯ͡Ҿ਺ʹΞ
    ΫηεͰ͖Δ
    fun x y = x + y
    class A$0 extends Fun {
    def app(x: Object) =
    return new A$1(
    local=x, parent=this)
    }
    class A$1 extends Fun {
    def app(arg: Object) = {
    val x = (int)parent.local;
    val y = (int)arg;
    x + y
    }
    }

    View full-size slide

  11. ΫϥεϑΝΠϧ΁ͷม׵
    • ࠶ؼతࢀরΛ࣮ݱ͢ΔͨΊɺࣄલʹՄม഑ྻΛڞ༗͓ͯ͠
    ͍ͯޙ͔ΒࢀরΛઃఆ
    let rec
    f x = g x
    g x = f x in
    f
    val env = Array[Object](null, null)
    val f = A$0(env, null)
    val g = A$1(env, null)
    env(0) = f
    env(1) = g
    return f
    // f.local(0) == f, f.local(1) == g

    View full-size slide

  12. ΫϥεϑΝΠϧ΁ͷม׵
    • ୅਺తσʔλܕ͸Ϋϥε֊૚ͱͯ͠දݱ
    data Option a =
    Some a | None
    abstract class Option extends Data
    class Option$Some(val v0: Object)
    extends Option
    class Option$None()
    extends Option

    View full-size slide

  13. ΫϥεϑΝΠϧ΁ͷม׵
    • ୅਺తσʔλܕͷύλʔϯϚον͸νΣοΫؔ਺+஋ͷऔ
    Γग़ؔ͠਺Ͱ࣮ݱ
    match value
    | Some a => a
    | None => x
    if(some_check(value))
    return some_get0(value)
    else if(none_check(value))
    return x
    def some_check(value) =
    value.isInstanceOf[Option$Some]
    def some_get0(value) =
    value.asInstanceOf[Option$Some]
    .v0

    View full-size slide

  14. ࠓޙͷ༧ఆ
    • ܕΫϥε͕΄͍͠

    • ηϧϑϗεςΟϯάʹ޲͚ͯ৭ʑ΍͍͖͍ͬͯͨͰ͢Ͷ

    View full-size slide