-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkitchen-sink.ni
117 lines (93 loc) · 3.45 KB
/
kitchen-sink.ni
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Comment types:
// Single line comment
# Single line comment but using a pound/hash
/*
* Multi-line comments are also supported
*/
// Primitive types:
println("Integer: ", 4)
println("Float: ", 3.14)
println("Boolean: ", true)
println("String: ", "This is a string", 'This is so a string with single quotes')
println("Function: ", fn(x, y) { x + y; x * y; })
// Here we define the variable "hello" and assign it a fntion
let hello = fn(place) {
return "Hello, " + place
}
// Here we assign the output of calling hello()
let helloWorld = hello("World!")
// And constants
const helloWorld2 = hello("Mars!")
// The following code would fail since a constant can't be reassigned
// helloWorld2 = "Something else"
// Also, constants must be an int, float, string, bool or null. Arrays, hashmaps or
// other types aren't allowed as constants.
/*
* Nitrogen features many builtin fntions that are implemented directly in the
* interpreter. These fntions generally deal with any type of I/O or
* manipulation operations.
*/
// "println" will print all arguments with a newline after each
// The similarly named fntion "print" will print all arguments without a newline.
println(helloWorld)
println(hello2("Earth!"))
println(helloWorld2)
/*
* Nitrogen supports simple if statements
*
* Like many scripting languages, the following are considered true:
* TRUE
* Integer or floats not equal to 0
* Non-empty strings
*
* All other values are considered false.
*
* Compound logical expressions can be evalualted using "and" and "or"
*
* If statements will short circuit when possible, so below will evaluate properly
* even though somethingElse isn't defined anywhere.
*/
if helloWorld == "Hello, World!" and true or somethingElse {
println("Yep, that's right")
} else {
println("That's not right...")
}
// Arrays may be arbitrarily long and contain any variable type
let places = ["America", "Africa", "Europe"]
// Like any proper language, Nitrogen is zero-based
println(hello(places[1])) // Africa
// Hash maps are also supported. Keys can be either strings or ints.
// Values can be of any type.
let placeMap = {
"America": ["USA", "Canada", "Mexico"],
"Europe": ["Germany", "France", "Spain"], // Comma required due to automatic semicolon insertion
}
println(placeMap["Europe"])
// Standard library manipulation fntions do not alter the actual array
// The push below does not alter the array, but rather returns a new array
// with the elements pf placeMap["Europe"] plus the new element "Denmark".
println(push(placeMap["Europe"], "Denmark"))
// Map keys and array indices can be reassigned
placeMap["Europe"] = push(placeMap["Europe"], "Norway")
println(placeMap["Europe"])
// We can add new values to a map
placeMap["Africa"] = ["Egypt", "South Africa", "Madagascar"]
println(placeMap["Africa"])
placeMap["Africa"][1] = "Ethiopia"
println(placeMap["Africa"])
println(placeMap)
// And nil (null) for all your null needs
let thisIsNull = nil
println(thisIsNull)
// Functions can take more arguments than declared, this can be used for optional args
const extra = fn(a) {
// The local variable "arguments" is an array that contains all parameters after those
// that were declared. So here, "arguments[0]" will be the SECOND parameter given since
// the first paramter is bound to "a".
println(arguments)
}
extra(1, 2)
// Simple loops are also possible. An infinite loop can be achieved by omitting the loop header
for i = 0; i < 5; i + 1 {
println(i)
}