1 // This file provides examples of syntactic constructs in wren, which is mainly
  2 // interesting for testing syntax highlighters.
  3 
  4 // This is a comment.
  5 /* This is /* a nested */ comment. */
  6 
  7 // Class definition with a toplevel name.
  8 class SyntaxExample {
  9   // Constructor
 10   new {
 11     // toplevel name `IO`
 12     IO.print("I am a constructor!")
 13     // method calls
 14     variables
 15     fields()
 16     // static method call
 17     SyntaxExample.fields(1)
 18   }
 19 
 20   // Constructor with arguments
 21   new(a, b) {
 22     print(a, b)
 23     field = a
 24   }
 25 
 26   // Method without arguments
 27   variables {
 28     // Valid local variable names.
 29     var hi
 30     var camelCase
 31     var PascalCase
 32     var abc123
 33     var ALL_CAPS
 34   }
 35 
 36   // Method with empty argument list
 37   fields() {
 38     // Fields
 39     _under_score = 1
 40     _field = 2
 41   }
 42 
 43   // Static method with single argument
 44   static fields(a) {
 45     // Static field
 46     __a = a
 47   }
 48 
 49   // setter
 50   field=(value) { _field = value }
 51 
 52   // Method with arguments
 53   print(a, b) { IO.print(a + b) }
 54 }
 55 
 56 // `class`, `is`
 57 class ReservedWords is SyntaxExample {
 58   // `new`
 59   new {
 60     // `super`, `true`, `false`
 61     super(true, false)
 62     // `this`
 63     this.foo
 64     // `new`
 65     new SyntaxExample
 66   }
 67 
 68   foo {
 69     // `var`
 70     var n = 27
 71     // `while`, `if`, `else`
 72     while (n != 1) if (n % 2 == 0) n = n / 2 else n = 3 * n + 1
 73 
 74     // `for`, `in`
 75     for (beatle in ["george", "john", "paul", "ringo"]) {
 76       IO.print(beatle)
 77       // `break`
 78       break
 79     }
 80 
 81     // `return`, `null`
 82     return null
 83   }
 84 
 85   imports {
 86     // `import`
 87     import "hello"
 88     // `import`, `for`
 89     import "set" for Set
 90   }
 91 
 92   // `foreign`, `static`
 93   foreign static bar
 94   foreign baz(string)
 95   // (Remove lines above to make this file compile)
 96 }
 97 
 98 class literals is SyntaxExample {
 99   booleans { true || false }
100   numbers {
101     0
102     1234
103     -5678
104     3.14159
105     1.0
106     -12.34
107     0xdeadbeef
108     0x1234567890ABCDEF
109   }
110   strings {
111     "hi there"
112     // Escapes:
113     "\0" // The NUL byte: 0.
114     "\"" // A double quote character.
115     "\\" // A backslash.
116     "\a" // Alarm beep. (Who uses this?)
117     "\b" // Backspace.
118     "\f" // Formfeed.
119     "\n" // Newline.
120     "\r" // Carriage return.
121     "\t" // Tab.
122     "\v" // Vertical tab.
123     // Unicode code points
124     IO.print("\u0041\u0b83\u00DE") // "AஃÞ"
125     // Unencoded bytes
126     IO.print("\x48\x69\x2e") // "Hi."
127   }
128   ranges {
129     3..8  // inclusive
130     4...6 // half-inclusive
131   }
132   nothing { null }
133 
134   lists {
135     var list = [1, "banana", true]
136     list[0] = 5
137     list[1..2]
138   }
139   maps {
140     var stringMap = {
141       "George": "Harrison",
142       "John": "Lennon",
143       "Paul": "McCartney",
144       "Ringo": "Starr"
145     }
146     var a = 1
147     var weirdMap = {
148       true: 1,
149       false: 0,
150       null: -1,
151       "str": "abc",
152       (1..5): 10,
153       a: 2,
154       _a: 3,
155       __a: 4
156     }
157   }
158 }