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. Ͱ͖Δ͜ͱ 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ϝιουݺͼग़͠ ߴ֊ؔ਺
  2. Ͱ͖Δ͜ͱ 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 ;; } ੍ޚߏจ ϩʔΧϧม਺ ࠶ؼ
  3. Ͱ͖Δ͜ͱ 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 ;; } ύϥϝʔλଟ૬ ܕਪ࿦ ଟ૬ͳ୅਺తσʔλܕ ύλʔϯϚον
  4. ίϯύΠϥͷߏ੒ Source Raw AST Named AST Typed AST JAST Parser

    Namer Typer Javalizer • Parser: ιʔείʔυ͔ΒAST΁ͷม׵ • Namer: Ϋϥε໊΍ม਺໊ͷղܾ • Typer: ܕ෇͚ • Javalizer: JavaόΠτίʔυʹ͍ۙதؒදݱʹม׵ • Emitter: ΫϥεϑΝΠϧͷग़ྗ .class Emitter
  5. ΫϥεϑΝΠϧ΁ͷม׵ • ؔ਺͸ΦϒδΣΫτͱͯ͠දݱ͞ΕΔ • ͢΂ͯͷؔ਺͸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; }
  6. ΫϥεϑΝΠϧ΁ͷม׵ • ෳ਺Ҿ਺ؔ਺͸ؔ਺Λฦؔ͢਺Ͱදݱ͞ΕΔ • ؔ਺ΦϒδΣΫτ͸֎ଆͷؔ਺΁ͷࢀরΛ௨ͯ͡Ҿ਺ʹΞ ΫηεͰ͖Δ 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 } }
  7. ΫϥεϑΝΠϧ΁ͷม׵ • ࠶ؼతࢀরΛ࣮ݱ͢ΔͨΊɺࣄલʹՄม഑ྻΛڞ༗͓ͯ͠ ͍ͯޙ͔ΒࢀরΛઃఆ 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
  8. ΫϥεϑΝΠϧ΁ͷม׵ • ୅਺తσʔλܕ͸Ϋϥε֊૚ͱͯ͠දݱ data Option a = Some a |

    None abstract class Option extends Data class Option$Some(val v0: Object) extends Option class Option$None() extends Option
  9. ΫϥεϑΝΠϧ΁ͷม׵ • ୅਺తσʔλܕͷύλʔϯϚον͸νΣοΫؔ਺+஋ͷऔ Γग़ؔ͠਺Ͱ࣮ݱ 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