Repository navigation
Expand file tree
/
Copy pathoperators.cm
More file actions
82 lines (68 loc) · 2.43 KB
/
Copy pathoperators.cm
File metadata and controls
82 lines (68 loc) · 2.43 KB
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
# Every operator and punctuation mark, with each two-character form written
# beside the one-character form it could be confused with.
#
# ignore CM0409 in file
# ignore CM0410 in file
# A catalog names each result so a reader can see what the operator gives back.
# Nothing goes on to read them, and reading them would say nothing more.
shared model Operators
function Arithmetic(integer a, integer b)
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
let negated = -a;
end function
function Comparison(integer a, integer b)
# Each pair puts the longer operator next to its shorter form, which is
# where a mistake in longest-match scanning would show up first.
let lessOrEqual = a <= b;
let less = a < b;
let greaterOrEqual = a >= b;
let greater = a > b;
let equal = a == b;
let notEqual = a != b;
end function
function Assignment(integer a)
integer target;
target = a;
end function
function Logical(boolean p, boolean q)
let both = p and q;
let either = p or q;
let negation = not p;
end function
function TypeOperators(Model value)
let isString = value is string;
let asString = value as string;
end function
function Punctuation(integer[] items, string text)
# Parentheses, brackets, braces, comma, semicolon, and dot.
integer[] literal = {1, 2, 3};
let indexed = items[0];
let member = text.Count;
# A call is punctuation too, but this one yields nothing, so there is no
# result to give a name to.
Operators.Arithmetic(1, 2);
end function
function OptionalAndLambda()
# The question mark is a type suffix; a lambda's body follows "yield".
integer? maybe;
integer delegate(integer, integer) add = (a, b) yield a + b;
let recovered = maybe.Or(0);
end function
function CaseLabels(integer code)
# The colon appears only at the end of a case label.
switch code
case 1:
Console.WriteLine("one");
default:
Console.WriteLine("other");
end switch
end function
function Fractions()
# The pipe separates a fraction's numerator and denominator.
constant fraction ratio = 3|4;
end function
end model