1
0
forked from Mirror/wren
Files
wren/example/syntax.wren

189 lines
3.5 KiB
Plaintext
Raw Normal View History

2015-04-25 18:10:17 +02:00
// This file provides examples of syntactic constructs in wren, which is mainly
// interesting for testing syntax highlighters.
// This is a comment.
/* This is /* a nested */ comment. */
// Class definition with a toplevel name.
class SyntaxExample {
// Constructor
construct new() {
// Top-level name `IO`
System.print("I am a constructor!")
// Method calls
2015-04-25 18:10:17 +02:00
variables
fields()
// Block arguments
fields { block }
fields {|a, b| block }
fields(argument) { block }
fields(argument) {|a, b| block }
// Static method call
2015-04-25 18:10:17 +02:00
SyntaxExample.fields(1)
}
// Constructor with arguments
construct constructor(a, b) {
2015-04-25 18:10:17 +02:00
print(a, b)
field = a
}
// Method without arguments
variables {
// Valid local variable names.
var hi
var camelCase
var PascalCase
var abc123
var ALL_CAPS
}
// Method with empty argument list
fields() {
// Fields
_under_score = 1
_field = 2
}
// Static method with single argument
static fields(a) {
// Static field
__a = a
}
// Setter
2015-04-25 18:10:17 +02:00
field=(value) { _field = value }
// Method with arguments
print(a, b) { System.print(a + b) }
// Operators
+(other) { "infix + %(other)" }
-(other) { "infix - %(other)" }
*(other) { "infix * %(other)" }
/(other) { "infix / %(other)" }
%(other) { "infix \% %(other)" }
<(other) { "infix < %(other)" }
>(other) { "infix > %(other)" }
<=(other) { "infix <= %(other)" }
>=(other) { "infix >= %(other)" }
==(other) { "infix == %(other)" }
!=(other) { "infix != %(other)" }
&(other) { "infix & %(other)" }
|(other) { "infix | %(other)" }
! { "prefix !" }
~ { "prefix ~" }
- { "prefix -" }
2015-04-25 18:10:17 +02:00
}
// `class`, `is`
class ReservedWords is SyntaxExample {
reserved {
2015-04-25 18:10:17 +02:00
// `super`, `true`, `false`
super(true, false)
// `this`
this.foo
}
foo {
// `var`
var n = 27
// `while`, `if`, `else`
while (n != 1) if (n % 2 == 0) n = n / 2 else n = 3 * n + 1
// `for`, `in`
for (beatle in ["george", "john", "paul", "ringo"]) {
System.print(beatle)
2015-04-25 18:10:17 +02:00
// `break`
break
}
// `return`, `null`
return null
}
imports {
// `import`
import "hello"
// `import`, `for`
import "set" for Set
}
// `foreign`, `static`
// foreign static bar
// foreign baz(string)
2015-04-25 18:10:17 +02:00
// (Remove lines above to make this file compile)
}
class Literals is SyntaxExample {
2015-04-25 18:10:17 +02:00
booleans { true || false }
2015-04-25 18:10:17 +02:00
numbers {
0
1234
-5678
3.14159
1.0
-12.34
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
0x1000000
0xdeadbeef
0x1234567890ABCDEF
2015-04-25 18:10:17 +02:00
}
2015-04-25 18:10:17 +02:00
strings {
"hi there"
// Escapes:
"\0" // The NUL byte: 0.
"\"" // A double quote character.
"\\" // A backslash.
"\a" // Alarm beep. (Who uses this?)
"\b" // Backspace.
"\f" // Formfeed.
"\n" // Newline.
"\r" // Carriage return.
"\t" // Tab.
"\v" // Vertical tab.
// Unicode code points
System.print("\u0041fgdg\u0b83\u00DE") // "AஃÞ"
2015-04-25 18:10:17 +02:00
// Unencoded bytes
System.print("\x48\x69\x2e") // "Hi."
2015-04-25 18:10:17 +02:00
}
2015-04-25 18:10:17 +02:00
ranges {
3..8 // inclusive
4...6 // half-inclusive
}
2015-04-25 18:10:17 +02:00
nothing { null }
lists {
var list = [1, "banana", true]
list[0] = 5
list[1..2]
}
2015-04-25 18:10:17 +02:00
maps {
var stringMap = {
"George": "Harrison",
"John": "Lennon",
"Paul": "McCartney",
"Ringo": "Starr"
}
var a = 1
var weirdMap = {
true: 1,
false: 0,
null: -1,
"str": "abc",
(1..5): 10,
a: 2,
_a: 3,
__a: 4
}
}
}