Skip to content

An idea of using Cirru syntax as an alternative of text format #617

Closed

Description

@tiye

...I'm just thinking from the syntax side, but not being experienced in compiler. I will just discuss on the syntax side.

I made a video to demonstrate the main ideas behind Cirru:
https://www.youtube.com/watch?v=1ShYO_A8g-0 (14mins)

Short version:

  • Cirru is trying to build a syntax tree that we can edit directly
  • that syntax tree has a text syntax, with parser and formatter
  • that text syntax is based on indentations
  • I've been thinking about it for several years and have been using it for months

For Cirru code like this:

defn add2 (x y) (+ x y)

it is equivalent to a piece of data in JSON:

["defn", "add2", ["x", "y"], ["+", "x", "y"]]

once you get JSON you are free to make an DOM editor or simply render to HTML/CSS:
image
and also it can be turned back to Cirru code as you want.

Check these links if you need details.
http://cirru.org/
https://github.com/Cirru/
https://twitter.com/cirrulang
https://github.com/Cirru/cirru-wasm-ast (mostly based on @indutny 's work)
https://github.com/Cirru/cirru-wasm-cli

From my side I got some experience using Cirru:

Pros

  • Preciously we are programming by manipulating text files, we all say it's about AST, which is a tree, but we need the help of text syntax to do it. Some languages pushed syntax even further and it becomes hard for newcomers to pick up. Cirru is giving more possibilities. I mean we can manipulate the syntax tree directly now.
  • Cirru syntax is simple. It's based on indentations and reduced the usage of parentheses a lot.
  • Cirru syntax is flexible. When recovered from binary files, it can be turned into serveral styles of text form, such as "smart folding look like human wrote it", "expression unfolded for the purpose of add break points", "tree view for a better user interface", etc.
  • I've been generating my JavaScript code and ClojureScript code in Cirru for many months, the syntax is quite handy now.

Cons

  • People hate to learn one more syntax, what's worse, it's different from C, Java or Python.
  • String syntax in Cirru can be confusing according to my experience introducing Cirru to others. Because Cirru was originally designed as a tree editor and people does not quote marks in a tree editor.
  • After Cirru code is parsed into a tree, we need another step to turn that tree into an AST. I'm not quite clear about how compilers will handle this. So I'm not sure it's good or bad.
  • I think we don't have very much experience programming with a tree rather than a text file. Our toolchains are mostly based on text files.

Even if we don't want the graphical part of CIrru, we can still try its text syntax. It's basic Lisp in indentations(with string syntax a bit strange):
https://gist.github.com/jiyinyiyong/0fc58e2ed7c641973d9b

module
  export :even $even
  export "odd" $odd

  func $even (param $n i32) (result i32)
    if (i32.eq (get_local $n) (i32.const 0))
      i32.const 1
      call $odd (i32.sub (get_local $n) (i32.const 1))

  func $odd (param $n i32) (result i32)
    store_global $scratch (get_local $n)
    if (i32.eq (get_local $n) (i32.const 0))
      i32.const 0
      call $even (i32.sub (get_local $n) (i32.const 1))

  global $scratch i32

I'm a big fan of Lisp(mainly Clojure), but dislike the parentheses in Lisp. So this is my idea to fix it and that in WebAssembly.

Activity

  1. qwertie commented on Mar 19, 2016

    @qwertie

    You have some competition.... from me.

    I made LES (LESv2), a language inspired by LISP but syntactically based on Javascript and C#. Its relationship to JSON is somewhat different - LES is a superset of JSON, rather than being convertible to JSON (one could define such a reverse mapping, of course, I just haven't done so, and if JSON code is treated as LES code and then converted with any reasonable mapping back to JSON, it would probably have to expand in size). Edit: LES is probably a more sophisticated language than Wasm needs, so I was going to propose a subset of LES to the Wasm CG, I just haven't got around to defining it yet. And I was considering other refinements.

    Hmm... given that, in Cirru, an ident becomes "ident" in JSON, what about a string like "ident" in Cirru? Are strings and identifiers exactly equivalent? And what about the dollar sign in the above code? The documentation says $ as a function to fold code, see Haskell, but I suppose that's not what's happening here.

  2. tiye commented on Mar 19, 2016

    @tiye
    Author

    I'm looking into the "Cirru problems" first,

    For ident, my video covered it. Yes I realise this problem as soon as I began to use it. There are already examples on solving this. It's like 'something-quoted in Scheme, and :a-keyword in Clojure. My solution is to prefix that with one character, for JavaScript "str" is written in :str, and in Clojure I use |str.

    For $, in Haskell it's an infix function which evaluates x before f is calling in:

    ($) :: (a -> b) -> (a -> b)
    f $ x = f x

    Yeah, it's internally different in Cirru since Cirru Parser is doing syntax tree transformation at the end of parsing. I want to say the meaning is learnt from Haskell. f $ a b is evaluated like f (a b).

    Back to LES... personally I don't like writing brackets, but I have to say LES is far more mature than Cirru is and Cirru is hardly a competitor as an idea at this moment(well, I suppose Scheme syntax is by now the best). But I'm still glad Scheme is not our only option to write WebAssembly :D

    By the way even people don't like Cirru, I can still compile Cirru to WebAssembly by some ways..

    • CirruScript -> JavaScript -> wasm
    • Cirru -> ClojureScript -> wasm
    • Cirru -> wasm AST -> wasm
  3. qwertie commented on Mar 19, 2016

    @qwertie

    How old is Cirru, by the way? It seems to me that if the CG decided they just wanted an s-expression parser, they could go with Sweet Expressions, which appear to be 2.5 years 3.5 years old. I heard about sweet-expressions (a.k.a. Readable Lisp S-expressions) a couple of years ago, and I don't even use LISP. So.... I wonder, did you know about sweet expressions already when you started Cirru? Personally I think infix operators are a key to readability; sweet exprs have them but I don't see them in Cirru. Heck, if it weren't for the fact that everybody still writes LISP in s-exprs instead of sweet-exprs, I'm sure I would use a language in the LISP family by now (the real problem is that I can't learn LISP with sweet exprs: no one is teaching it that way.)

    LES is far more mature than Cirru

    Uhh, did you really mean to say that? Cirru is older than LES LESv2 (LESv1 is 2.7 years old), it has its own domain name, and it has parsers written in a whole bunch of languages. LES is great if we want a Javascript-style syntax, but Cirru (and sweet expressions) appear to be mature.

    About the $, I was referring to the code you wrote earlier like (param $n i32) - this means (param (n 132))?

    Edit added corrections after you replied

  4. tiye commented on Mar 20, 2016

    @tiye
    Author

    Cirru was started in the middle 2012 but only as a tree editor, and later got a text syntax by the end of the year, according to my git commits and tweets at weibo. It was only a toy before I can actually compile it to JavaScript in 2015. I saw Sweet before, but I don't know how to use it or where to find its interpreter. So, I did it before Sweet.

    The way I came to Cirru, I was making an AST editor after learning http://norvig.com/lispy2.html but failed to make it handy back that time with raw JavaScript. There was not React at that time, I built my first version of tree editor in canvas and it's hardly an editor I can use. So I realised I need a text syntax which looks very similar to Cirru Editor.

    Clojure is OK for me, Polish notation, so prefix syntax, it feels good to me. Ever since I built my own "lispy" interpreter I found it very nice, which JavaScript syntax bothered me a lot. I mean I think Lisp is still readable comparing to C-style syntax since the later one has much more syntax rules.

    Yes, I think LES is more mature in the part of WebAssembly. Well, you got a spec. Cirru does not have a spec, it only contains some special functions to help transforming code to JavaScript or Clojure, also some WebAssembly. So it's more like a code generator at this moment. However, I would say Cirru is more advanced as "an AST tree exploring project". :)

  5. qwertie commented on Mar 20, 2016

    @qwertie

    Edit corrected my earlier post.

    LES has a spec, but it's not complete, and if the CG considers LES as the Wasm text format, I will pretty much change it according to the whims of the "important" members like @lukewagner and @jfbastien. Heck, I could even stop calling it "my" idea - if that's the price :)

    The most important thing is that Wasm should choose a parser that isn't solely dedicated to Wasm itself - like s-expressions, it should be a syntax that lends itself to many different use cases. Why? Look, none of the "friendly" languages for representing syntax trees - sweet expressions, LES, Cirru - are popular. People don't use them because they aren't popular, and they aren't popular because people don't use them. Chicken and egg. I just talked to a prominent member of the C# community; he said he wouldn't use the Enhanced C# language I created, not because of anything wrong with it, but simply because "hardly anyone else knows" it.

    That means WebAssembly CG has a huge opportunity that they shouldn't squander. Whatever they pick automatically becomes popular. I hope that they will choose a variation of LES, because Javacript/C syntax is by far the most popular syntactic style, and as far as I know, LES is the only language that parses a language resembling Javacript et al into a simple data structure.

    Fun fact: originally LES had a Python-style mode where it was indentation-sensitive. I took that out in favor of just C/JS-style, because I think people will warm up to that more than the Python style. I don't really care one way or the other - I quite like Python's indentation-as-structure, but the important thing is to bring those LISP ideas of "syntax independent of semantics" and "code is data" over to the mainstream.

    JavaScript syntax bothered me a lot. I mean I think Lisp is still readable comparing to C-style syntax since the later one has much more syntax rules.

    I have difficulty understanding this perspective. I mean, I've been coding for over 20 years, and in that time I have spent far, far more time learning about library APIs than syntax. More time learning semantics than syntax. Etc. C/C++ syntax is definitely confusing in some ways, but Javascript? It's a lot easier. Also, infix operators are present in natural language ("five plus four"), the order of operations is taught before high school ("1 + 2 × 3 < 9" vs "< (+ 1 (× 2 3)) 9"), and some other parts of the syntax are popular in mathematics as well as programming languages (e.g. (tuple, of, things) and [list, of, things]).

  6. tiye commented on Mar 20, 2016

    @tiye
    Author

    That's far more than I have thought. Popularity matters a lot in language choosing I think. WebAssembly is still new as a compiling target so choosing a popular syntax may benefit from existing toolchains. S-expression is nice since it's simple to built from scratch and people already explored it enough to bypass the potential problems. And in my point, Cirru is just a simple version of S-expression and better designed for the future, which is enabling graphical tool in programming.

    So in short, Cirru is not the best choice at this moment. But if people want 1) a cleaner version of S-expression(by using indentations), 2) build graphical tools for WebAssembly, then Cirru might be a good start to the future plans.

    I've been learning programming for about 4 years, maybe 5, only. And from the day I began to learn, CoffeeScript is becoming popular and lots of ideas are making to choose an efficient way of writing code, like making less mistakes, bringing less confusions. That's why I'm exploring a new one trying to get more by using a graphical way. JavaScript syntax, in my view is tedious to write and modify. I can bear using it in large projects that enhance stability and team work, but not in all my projects that I want to code fast.

  7. shaunlebron commented on Mar 20, 2016

    @shaunlebron

    For anyone wanting quick context on this discussion: WebAssembly will be a binary format but there are some cases in which a Text Format is also desired. An official text format hasn't been decided, but S-expressions are currently used for the sake of simplicity in the spec. Hence, the discussion here on what the official text format should be by looking at alternative syntaxes explicitly designed to represent ASTs.

    A way to evaluate

    I want to offer that choosing a syntax may be an optimization problem of the following things:

    simplicitynumber of syntax rules
    flexibilityability and ease of adding rules
    familiarity*popularity of syntax
    readabilitya subjective function of simplicity, familiarity, and preference
    writabilityhow simple it is for a human to produce it w/ or w/o tools

    * I think familiarity is set by community defaults, for which WebAssembly has carte blanche, as @qwertie said.

    Evaluating: S-expressions

    • simplicity - obviously good
    • flexibility - obviously good
    • familiarity - obviously bad
    • readability
    • writability
      • balancing parens is difficult without plugins
      • we are working on Parinfer to allow indentation to influence structure and vice versa

    Evaluating: Cirru and LES

    I like what I've seen from both, but I can't fully evaluate these for lack of experience, so I will leave it to their authors...

    My thoughts

    I would place less weight on familiarity and more weight on simplicity and
    flexibility because that will contribute the most to future-proofing how we
    express the AST of the web, which will probably be around for a while! ;)

  8. qwertie commented on Mar 21, 2016

    @qwertie

    @JSStats LES actually has an unlimited number of operators (sequences of punctuation marks), with precedence chosen by a set of rules based on operators from several languages, especially C/C++/Java/C#/Javascript. There is also a backquote pseudo-operator, which lets you treat any identifier as an operator. For instance, the s-expression

    (i32.add (get_local N) (i32.const 1))
    

    could be directly transliterated to LES in prefix notation...

    @i32.add(get_local(N), @i32.const(1));
    

    or with "superexpressions"

    @i32.add(get_local N, @i32.const 1);
    

    or with a i32.add and a superexpression...

    (get_local N) `i32.add` (@i32.const 1);
    

    or even (worst of all) a backquote fest:

    `get_local` N `i32.add` `i32.const` 1;
    

    These are all equivalent, due to the equivalence of operators and "calls()". They are also ugly and not recommended; I'm just showing the different ways LES allows you to express the same thing. (Note that the @ in @int32.add is needed because the identifier i32.add contains a dot character and we probably don't want . to be considered a binary operator.)

    Wasm does not have +, rather it has i32.add i64.add

    Quite right. However, I do think it would make a lot of sense to include operators like + in the text format, requiring the "assembler" to infer whether it's i32.add or i64.add. There are other things we can readily do to make the text format more friendly. Let's consider this adder:

    (func $add (param $x i32) (param $y i32) (result i32) 
        (i32.add (get_local $x) (get_local $y)))
    

    (Incidentally, one extra @ sign makes this into valid LES code.)

    It could look something like this in LES:

    func add(x: i32, y: i32) -> i32 { x + y };
    

    That's if we decide users can access locals unqualified (or we could use #x or $x or whatever); similarly we could use simply 123 for i32.const 123 and 123L for i64.const 123. . The structure of this expression (expressed as an LES-compatible s-expression) is

    (func (@-> (add (@: x i32) (@: y i32)) (i32)) (@`{}` (@+ #x #y)));
    

    Languages and data do not in general map to a fixed set of operators so a fixed set of precedence and parsing rules seems to have limited utility.

    The fixed set of rules is designed to reflect the existing consensus of many different programming languages. I think the existing rule set would serve Wasm quite well - but it's not a finalized spec, and if the CG wants changes, then changes there will be. Even better would be if eyeballs outside the Wasm community deliberated on it, but I'm not sure how to make that happen.

  9. qwertie commented on Mar 21, 2016

    @qwertie

    @JSStats The 'add' example doesn't seem familiar? It looks like Rust and Swift. Since the text format isn't used directly on the web, I was assuming a one-pass compiler wouldn't be necessary for it (and certainly one could do multiple passes over individual functions), so some conveniences can be added, so + could be used for adding even if there is no single opcode for it. Heck, even if one really wanted to optimize the parser, that feature is still possible (given forward declarations for all functions).

  10. qwertie commented on Mar 22, 2016

    @qwertie

    On the LES page, you'll LES code that looks like Javascript, but since Wasm is statically typed it couldn't/shouldn't look quite like JS, hence the Rust/Swift style. I haven't made a LES-to-Wasm (or vice versa) converter yet, and I've been puzzling over which language I should write a parser in... JS? C++? OCaml? Currently it's just C# which presumably doesn't interest folks here.

  11. added this to the Meta milestone on Apr 5, 2016
  12. data-ux commented on Apr 8, 2016

    @data-ux

    I have just published a WASM playground that can also render the code in different text formats. This way it is easy to compare the pros and cons of different formats:
    http://ast.run/

    I'm planning to add next rendering to Sweet-expression like format. Cirru would be interesting, too.

  13. 22 remaining items

  14. dead-claudia commented on Jul 23, 2016

    @dead-claudia

    @jiyinyiyong To be fair, I didn't actually verify the code samples. And I know Cirru is far from perfect (it can be pretty awkward IMHO). I just wanted to put out a more representative sample. 😄

    (Oh, and I fixed the error, BTW.)

  15. yurydelendik commented on Jul 25, 2016

    @yurydelendik

    From fac.wast:

    // LES
    function $fac-opt ($a:i64) : (i64) {
    

    @isiahmeadows can you remove or change// LES from your examples above? The experimental text format has no such abbreviation. I think you are mistaken the name with counterproposal's one.

  16. dead-claudia commented on Jul 26, 2016

    @dead-claudia

    @yurydelendik I was referring to the raw expression syntax itself used by the two proposals to differentiate them (LES-based and Cirru-based syntaxes), not the actual syntax of the text format (which there isn't actually any ATM beyond the s-expressions used by the WASM reference interpreter and most current experimental implementations). So if I were to be technically correct, I'd make the comments LES-based and Cirru-based instead, but I thought that LES and Cirru would be sufficient given the context.

    To be fair, someone could theoretically invent their own syntax for it for another completely proposal (which I've considered doing so myself, but just haven't had the time to sketch it out). The ideas are pretty much limitless right now.

  17. yurydelendik commented on Jul 26, 2016

    @yurydelendik

    The @sunfishcode's strawman proposal is not LES-based (at least not yet) that's why I don't really understand why this is mentioned in the examples comments.

  18. dead-claudia commented on Jul 26, 2016

    @dead-claudia

    @yurydelendik Well...you're correct in a sense:

    [From the strawman pull: sunfishcode#3]

    I propose that the text format be compatible with LES - as the PR text explains, not LES as it exists today, but as it will be when the MVP is launched.

    At this point I feel we're arguing semantics. I feel the intent was sufficient enough (although I'll add a note above explaining the technicality).

  19. qwertie commented on Jul 26, 2016

    @qwertie

    @isiahmeadows, my LES proposal is derived from @sunfishcode's and not the other way around (although LES itself was developed before WebAssembly, just as s-expressions were.)

  20. dead-claudia commented on Jul 26, 2016

    @dead-claudia

    @qwertie Where did I imply that? If I was, I'm sorry, and I'll fix it. (I tried to speak of theirs as purely independent, and I didn't think I referenced yours.)

  21. qwertie commented on Jul 26, 2016

    @qwertie

    @isiahmeadows your comment 3 days ago, which discusses "sunfishcode's LES-based proposal". (There's only one LES proposal - mine. Unless you count this second one which is also mine)

  22. dead-claudia commented on Jul 26, 2016

    @dead-claudia

    @qwertie That was part of the associated edit with this comment stemming from this discussion. I'll fix it.

  23. dead-claudia commented on Jul 26, 2016

    @dead-claudia

    @yurydelendik I'm sorry...I didn't fully differentiate the PR from the actual strawman. I apologize.

  24. flagxor commented on Jul 30, 2016

    @flagxor
    Member

    After some communication between implementers, we've decided to focus on having all browsers be able to display linear opcodes for the MVP timeframe. Moving to Discussion.

  25. modified the milestones: Discussion, MVP on Jul 30, 2016
  26. jfbastien commented on May 11, 2017

    @jfbastien
    Member

    Text format proposal. Closing this in favor of the proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions