代码之家  ›  专栏  ›  技术社区  ›  Roman A. Taycher

语言文字支持

  •  2
  • Roman A. Taycher  · 技术社区  · 15 年前

    Are there any languages that support easily extending the compiler to support new literals example such as for json/xml/yaml/different string encodings/extra number types. I understand that you can always fork the compiler, write a dsl but that's not quite what I'm asking about. I understand that some languages such as smalltalk and lisp are incredibly flexible but I'm not looking for something that can be done fairly simple by one team, but something which is an attempt to bring this to the whole language and allow it as common practice. You can also share any research on similar ideas.

    或者,是否有任何语言通过带有字符串参数alla的对象中的特殊方法支持文本(在本例中,“”表示要传递给xml.newFromTripleString(字符串a)的字符串的开始和结束)。

    xml exapmlexml=“” {{No}} {标题} “” 我知道很多语言都支持这种类型的事情,比如 xml exapmlexml=xml.newFromTripleString(“”+“\n” “+”\“+” “名称+”\n“+ “标题+”\n“+ “” but do any language try to make this easier with something like implicit conversion? Any research on these sort of techniques?

    对于如何在语言中引入更灵活的字面或类似字面的支持,任何与其他想法的链接和解释都是不错的。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Jörg W Mittag    15 年前

    Ioke

    IOKE允许重写文本,但不允许定义新的文本。它的工作方式是将文本简单地转换为消息发送,然后您可以像其他方法一样重写相应的方法。

    例如,这是 Dict 有两个条目,一个映射 Symbol 到A Text ,另一个映射a 符号 到A Number :

    { :name => "Jörg", :age => 31 }
    

    对于名为 {} (btw:列表的工作方式相同,它们对应的消息是 [] )它完全等同于(如果你想这样写的话)来:

    {}(:name => "Jörg", :age => 31)
    

    现在, => 实际上只是一个为几乎所有对象定义的运算符,它只返回 Pair 其中键(第一个元素)是接收器,值是参数。现在,运营商 只发送消息,所以这相当于:

    {}(:name =>("Jörg"), :age =>(31))
    

    这个 : 表示文字符号的sigil也被转换为消息发送:

    {}(:("name") =>("Jörg"), :("age") =>(31))
    

    文本文本被转换为发送 internal:createText 信息:

    {}(:("name") =>(internal:createText("Jörg")), :("age") =>(31))
    

    [Note: obviously, the way it is written here will lead to an infinite recursion. The truth is that the argument to 内部:CreateText 显然不是一个样子 文本 而是一个平台字符串。即 ikj ,IOKE的JVM实现,实际上是 java.lang.String 为了 ikc CIL实现,它是 System.String . 我在这里用三个引号表示了这一点。]

    {}(:("name") =>(internal:createText("""Jörg""")), :("age") =>(31))
    

    这只会给我们留下一个数字,你猜对了,它也是一个信息发送:

    {}(:("name") =>(internal:createText("""Jörg""")),
       :("age") =>(internal:createNumber("""31""")))
    

    因为每件事都是一个消息发送,所以您可以通过实现相应的方法,随意定制文本的行为。这是一份来自 iik ,交互式loke repl:

    iik> "Hello"
    +> "Hello"
    
    iik> internal:createText = method(raw, super(raw) upper)
    
    iik> "Hello"
    +> "HELLO"
    

    Converge

    Converge允许强大的编译时元编程,包括一个名为 DSL块 . DSL块是不使用聚合语法的代码块。DSL块如下所示:

    $<<xml>>:
        <xml>
          <literal>here</literal>
        </xml>
    

    其工作方式是 $<< >> 是被调用的函数的名称 编译时 并以字符串的形式传递整个DSL块(以及一些源代码元数据,如行号、文件名等),并返回Converge抽象语法树的一个片段。因此,在这个特殊的例子中,会有这样一个函数:

    func xml(dsl_block, src_infos):
        // implement an XML parser here ...
        return ast
    

    Factor

    因子允许定义 解析词 这些词会影响同一范围内其他词的解析方式。因素实际上有一个 XML library implementation 它使用解析词来获取与scala的XML文本非常相似的语法,但只是普通的因子代码:

    : feed>xml ( feed -- xml )
        [ title>> ]
        [ url>> present ]
        [ entries>> [ entry>xml ] map ] tri
        <XML
            <feed xmlns="http://www.w3.org/2005/Atom">
                <title><-></title>
                <link href=<-> />
                <->
            </feed> 
        XML> ;
    

    [快速介绍因素: : 定义一个新单词,即第一行定义一个名为 feed>xml 它接受一个参数并产生一个结果。单词的前三行从提要对象中提取标题、URI和条目,并将它们放在堆栈上。这个 <XML 是打开XML模式和 XML> 再次关闭。在XML代码中, <-> 从堆栈中获取值并将其插入XML。]

    普通LISP

    Common Lisp Reader Macros 允许您钩住阅读阶段,即获取字符串并生成嵌套列表的阶段,然后将它们交给编译器/计算器。它们要求您选择唯一的一个或两个字符前缀,并且它们是全局的。第一个问题不算什么,因为我们可以简单地选择 < character as our prefix to make it look natural.

    Perl 6

    Perl6应该允许您在程序运行时更改语法。Perl6有一个动态可变语法,这意味着代码在执行时会被解析,并且可以更改语法,以便使用新语法解析文件中的其他代码。

    奥美塔/可乐

    Alessandro Warth's OMeta language 在…之上运行 Ian Piumarta's COLA system allows for what they call "mood-specific languages". I.e. languages whose specification and implementation is so lightweight that you can use them for just one line in the middle of your program and then switch to a different syntax again.

    它用于 Inventing Fundamental New Computing Technologies 在Alan Kay Viewpoint Research Institute . One example usage is the implementation of an entire TCP/IP networking stack in just 200 lines of code by designing a language whose syntax is identical to the ASCII art diagrams used in IETF RfCs and another language for writing networking protocol state machines. Then, the implementation of the networking stack simply consists of copy&pasting the ASCII diagrams from the RfCs and transiterating the English state machine descriptions from the RfCs into the state machine language.

    (Oh, in case you are wondering: the 200 lines is not just for the ASCII diagrams and the state machines. It also includes the parsers and compilers for the two languages.)

    π

    编程语言可能也很有趣。

        2
  •  2
  •   Jerry Coffin    15 年前

    听起来像是用户定义的文字(_§2.14.8) C++ 0x (警告:大型PDF)非常接近(或可能正是)您正在寻找的内容。

        3
  •  1
  •   BCS    15 年前

    这个 D programming language 具有所需的构造, at compile time ,转换包含JSON的字符串文本 into the corresponding object/struct/arrays . 它甚至可以 load the string at compile time from a external file . 我不知道要写什么代码,但写起来并不难。

    如果你在运行时想要同样的东西,D有 Associative arrays dynamic arrays 以及标准的OO特性集,因此构建一个遗传JSONDOM模型并不难。

    我不知道其他编码,但如果它们比JSON更麻烦的话,我会很惊讶的。

    简短回答: D不支持它的本土化,但它不会很难让它工作。

    推荐文章