2012-03-27 3 views
17

क्या कोई यह जानता है कि वास्तव में आरईपीएल (या कंपाइलर) में संकलन करने की कोशिश करने से पहले किसी के लिए (स्कैला भाग केवल) के लिए एक समझ/अभिव्यक्ति अभिव्यक्ति का अनुवाद कैसे किया जाए?/स्कष्टीकरण के लिए एक स्केल के विलुप्त हिस्से को प्राप्त करना?

केवल एक चीज मैं अब तक मिल गया है संकलक "प्रिंट" झंडा है, लेकिन है कि आप पूर्ण स्काला अनुवाद ...

उत्तर

21

यह प्रतीत नहीं होता है किसी भी संभावनाओं मौजूद है आरईपीएल के भीतर सीधे "अभिव्यक्ति के लिए" अभिव्यक्ति के लिए desugar। लेकिन एक विकल्प के एक "प्रिंट" की तरह या सरल भाव के लिए कुछ स्काला संकलक विकल्पों का उपयोग कर सकते हैं के रूप में "Xprint: टाइपकर्ता -e"

उदाहरण:

एक फ़ाइल से desugard उत्पादन प्राप्त करने के लिए "प्रिंट का उपयोग "झंडा:

# scala -print file.scala 

एक सरल एक लाइनर अभिव्यक्ति desugar करने के लिए, का उपयोग करें" -Xprint: टाइपकर्ता -e "झंडा:

# scala -Xprint:typer -e "for (i <- 0 to 100) yield i" 
+2

सबसे पहले, आपको केवल desugaring देखने के लिए '-Xprint: पार्सर' का उपयोग करना चाहिए। इसके अलावा, आप इसे 'स्कैला-एक्सप्रिंट: पार्सर' जैसे आरईपीएल के साथ उपयोग कर सकते हैं। हालांकि, यह सभी रैपर कोड भी प्रकट करेगा जो आरईपीएल आपके कोड के चारों ओर रखता है। – mgd

30

जैसा कि मैंने पहले से ही अन्य विषय में कहा, scalac -print प्रिंट बाहर स्केला कोड, नहीं देता है जावा। यह उन सभी स्कैला कीवर्ड का अनुवाद करता है जो जावा के साथ सामान्य स्कैला कोड से सीधे संगत नहीं होते हैं। संकलक को केवल कुछ हिस्सों का अनुवाद करने देना संभव नहीं है। लेकिन मूल रूप से एक समझ के लिए हमेशा एक ही तरीके से अनुवाद किया जाता है।

के लिए/इस

for(x <- List(1,2,3)) yield x*x 

तरह उपज एक साधारण

for(x <- List(1,2,3)) println(x) 

List(1,2,3).foreach{x => println(x)} 
को

List(1,2,3).map {x => x*x} 

और करने के लिए अनुवाद किया जाएगा उपज के बिना

नेस्टेड fors नेस्टेड flatMap लिए अनुवाद किया जाएगा/मानचित्र बनाता

for(x <- List(1,2,3); y <- List(4,5,6)) yield x*y 

List(1,2,3).flatMap { x => 
    List(4,5,6).map { y => 
    x*y 
    } 
} 

लिए अनुवाद किया जाएगा तो वहाँ बिल्कुल नहीं जादू

+6

मेरा बुरा, आप बिल्कुल सही हैं! मुझे सरल अभिव्यक्तियों के लिए कंपाइलर-ध्वज भी मिला: स्कैला-एक्सप्रिंट: टाइपर -e "वैल i = 1" – IODEV

+0

+1 मैं डिस्क्लेड कोड की खोज कर रहा था, झंडे नहीं। – ziggystar

14

परिणाम देखने के लिए के बाद सरल desugaring ०१२३०३२४०२८ का उपयोगविकल्प।

इस सरल इनपुट फ़ाइल test.scala नामित आप है:

$ scalac -Xprint:parser test.scala 
[[syntax trees at end of     parser]] // test.scala 
package <empty> { 
    object Test extends scala.AnyRef { 
    def <init>() = { 
     super.<init>(); 
    () 
    }; 
    List(1, 2, 3).flatMap(((x) => List(4, 5, 6).map(((y) => x.$times(y))))) 
    } 
} 

-Xprint:<phase> के लिए लागू संकलक चरणों की एक पूरी सूची प्राप्त करने के लिए ऐसा करते हैं:

object Test { 
    for(x <- List(1,2,3); y <- List(4,5,6)) yield x*y 
} 

फिर scalac -Xprint:parser प्रिंट बाहर का उपयोग कर इसे संकलन:

$ scalac -Xshow-phases 
      phase name id description 
      ---------- -- ----------- 
       parser 1 parse source into ASTs, perform simple desugaring 
        namer 2 resolve names, attach symbols to named trees 
     packageobjects 3 load package objects 
        typer 4 the meat and potatoes: type the trees 
       patmat 5 translate match expressions 
     superaccessors 6 add super accessors in traits and nested classes 
      extmethods 7 add extension methods for inline classes 
       pickler 8 serialize symbol tables 
       refchecks 9 reference/override checking, translate nested objects 
      selectiveanf 10 
      selectivecps 11 
       uncurry 12 uncurry, translate function values to anonymous classes 
       tailcalls 13 replace tail calls by jumps 
      specialize 14 @specialized-driven class and method specialization 
      explicitouter 15 this refs to outer pointers, translate patterns 
       erasure 16 erase types, add interfaces for traits 
      posterasure 17 clean up erased inline classes 
       lazyvals 18 allocate bitmaps, translate lazy vals into lazified defs 
      lambdalift 19 move nested functions to top level 
      constructors 20 move field definitions into constructors 
       flatten 21 eliminate inner classes 
        mixin 22 mixin composition 
       cleanup 23 platform-specific cleanups, generate reflective calls 
        icode 24 generate portable intermediate code 
       inliner 25 optimization: do inlining 
inlineExceptionHandlers 26 optimization: inline exception handlers 
       closelim 27 optimization: eliminate uncalled closures 
        dce 28 optimization: eliminate dead code 
        jvm 29 generate JVM bytecode 
       terminal 30 The last phase in the compiler chain 

-Xprint:<phase> विकल्प scala पर और इस प्रकार आरईपीएल पर भी लागू होता है।हालांकि, आप सभी रैपर कोड आरईपीएल आवेषण भी देखेंगे।

$ scala -Xprint:parser 
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25). 
Type in expressions to have them evaluated. 
Type :help for more information. 

<..a lot of initialisation code printed..> 

scala> object Test { 
    | for(x <- List(1,2,3); y <- List(4,5,6)) yield x*y 
    | } 
[[syntax trees at end of     parser]] // <console> 
package $line3 { 
    object $read extends scala.AnyRef { 
    def <init>() = { 
     super.<init>(); 
    () 
    }; 
    object $iw extends scala.AnyRef { 
     def <init>() = { 
     super.<init>(); 
     () 
     }; 
     object $iw extends scala.AnyRef { 
     def <init>() = { 
      super.<init>(); 
     () 
     }; 
     object Test extends scala.AnyRef { 
      def <init>() = { 
      super.<init>(); 
      () 
      }; 
      List(1, 2, 3).flatMap(((x) => List(4, 5, 6).map(((y) => x.$times(y))))) 
     } 
     } 
    } 
    } 
} 

[[syntax trees at end of     parser]] // <console> 
package $line3 { 
    object $eval extends scala.AnyRef { 
    def <init>() = { 
     super.<init>(); 
    () 
    }; 
    lazy val $result = $line3.$read.$iw.$iw.Test; 
    val $print: String = { 
     $read.$iw.$iw; 
     "".$plus("defined module ").$plus("Test").$plus("\n") 
    } 
    } 
} 

defined module Test 

scala> 
17

मैक्रो के बारे में कैसे?

import scala.reflect.macros.Context 
import scala.reflect.runtime.universe._ 
import scala.language.experimental.macros 

def _desugar(c : Context)(expr : c.Expr[Any]): c.Expr[Unit] = { 
    import c.universe._ 
    println(show(expr.tree)) 
    reify {} 
} 

def desugar(expr : Any) = macro _desugar 

यह आपके अनुरोध के अनुसार, आरईपीएल में सीधे इस्तेमाल किया जा सकता है:

scala> desugar { for(i <- List(1,2,3,4,5)) yield i } 
immutable.this.List.apply[Int](1, 2, 3, 4, 5).map[Int, Any](((i: Int) => 
i))(immutable.this.List.canBuildFrom[Int]) 

scala> desguar { for(i <- (0 to 10) if (i > 5)) yield i } 
scala.this.Predef.intWrapper(0).to(10).withFilter(((i: Int) => i.>(5))).map[Int, 
Any](((i: Int) => i))(immutable.this.IndexedSeq.canBuildFrom[Int]) 

यह भी अन्य मनमाना भाव पर काम करता है।

scala> desugar { 
    | val x = 20 
    | val y = 10 
    | println(x + y) 
    | } 
{ 
    val x: Int = 20; 
    val y: Int = 10; 
    scala.this.Predef.println(x.+(y)) 
} 

यह शायद सबसे करीब है कि तुम क्या आप कभी भी संकलन या किसी भी बिंदु पर एक फाइल करने के लिए बाहर डेटा डंप किए बिना पूछ रहे हैं करने के लिए मिलेगा। आप सीधे REPL में मैक्रो को परिभाषित कर सकते हैं, या :load कमांड से लोड की गई बाहरी फ़ाइल में।

2

स्केला 2.11 में, यह भी उपयोग करना संभव है quasiquotes:

val universe: scala.reflect.runtime.universe.type = scala.reflect.runtime.universe 
import universe._ 
val tree = q""" 
    val x = 20 
    val y = 10 
    println(x + y) 
""" 
println(tree) 
2

Intellij एक सुविधा है, जिसे "Explain Scala" है कि सीधे नक्शा/flatMap/फिल्टर में comprehensions के लिए विस्तार हो रहा सहित desugaring की एक बहुत करता है फ़ाइल जिसे आप संपादित कर रहे हैं।

कृपया ध्यान दें कि इंटेलिजे 2017.1 के बाद से इसे अब "देसुगर स्कैला कोड" कहा जाता है और "कोड" मेनू में (जानकारी के लिए धन्यवाद Mikaël) है।

IntelliJ Desugar Scala

+0

मेरे मामले में काम नहीं करता है। –

+0

विवरण कृपया? – ChoppyTheLumberjack

+0

इंटेलिजे के अंतिम संस्करण के बाद से यह फिर से काम कर रहा है। लेकिन ध्यान दें कि यह "स्केल समझाओ" लिखा नहीं गया है, लेकिन "देसुगर स्कैला कोड" –

संबंधित मुद्दे