mirror of
https://github.com/alecthomas/chroma.git
synced 2026-06-20 11:35:42 +02:00
Add Arturo lexer (#1232)
This is a port of `pygments.lexers.arturo` from Pygments.
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
|
||||
<lexer>
|
||||
<config>
|
||||
<name>Arturo</name>
|
||||
<alias>arturo</alias>
|
||||
<alias>art</alias>
|
||||
<filename>*.art</filename>
|
||||
</config>
|
||||
<rules>
|
||||
<state name="root">
|
||||
<rule pattern=";.*?$"><token type="CommentSingle"/></rule>
|
||||
<rule pattern="^((\s#!)|(#!)).*?$"><token type="CommentHashbang"/></rule>
|
||||
<rule pattern="(false|true|maybe)\b"><token type="NameConstant"/></rule>
|
||||
<rule pattern="\b(this|init)\b\??:?"><token type="NameBuiltinPseudo"/></rule>
|
||||
<rule pattern="`.`"><token type="LiteralStringChar"/></rule>
|
||||
<rule pattern="\\\w+\b\??:?"><token type="NameProperty"/></rule>
|
||||
<rule pattern="#\w+"><token type="NameConstant"/></rule>
|
||||
<rule pattern="\b[0-9]+\.[0-9]+"><token type="LiteralNumberFloat"/></rule>
|
||||
<rule pattern="\b[0-9]+"><token type="LiteralNumberInteger"/></rule>
|
||||
<rule pattern="\w+\b\??:"><token type="NameLabel"/></rule>
|
||||
<rule pattern="\'(?:\w+\b\??:?)"><token type="KeywordDeclaration"/></rule>
|
||||
<rule pattern="\:\w+"><token type="KeywordType"/></rule>
|
||||
<rule pattern="\.\w+\??:?"><token type="NameAttribute"/></rule>
|
||||
<rule pattern="(\()(.*?)(\)\?)"><bygroups><token type="Punctuation"/><usingself state="root"/><token type="Punctuation"/></bygroups></rule>
|
||||
<rule pattern="""><token type="LiteralStringDouble"/><push state="inside-simple-string"/></rule>
|
||||
<rule pattern="»"><token type="LiteralStringSingle"/><push state="inside-smart-string"/></rule>
|
||||
<rule pattern="«««"><token type="LiteralStringDouble"/><push state="inside-safe-string"/></rule>
|
||||
<rule pattern="\{\/"><token type="LiteralStringSingle"/><push state="inside-regex-string"/></rule>
|
||||
<rule pattern="\{\:"><token type="LiteralStringDouble"/><push state="inside-curly-verb-string"/></rule>
|
||||
<rule pattern="(\{)(\!)(\w+)(\s|\n)([\w\W]*?)(^\})">
|
||||
<usingbygroup>
|
||||
<sublexer_name_group>3</sublexer_name_group>
|
||||
<code_group>5</code_group>
|
||||
<emitters>
|
||||
<token type="LiteralStringDouble"/>
|
||||
<token type="LiteralStringInterpol"/>
|
||||
<token type="LiteralStringInterpol"/>
|
||||
<token type="TextWhitespace"/>
|
||||
<token type="LiteralString"/>
|
||||
<token type="LiteralStringDouble"/>
|
||||
</emitters>
|
||||
</usingbygroup>
|
||||
</rule>
|
||||
<rule pattern="\{"><token type="LiteralStringSingle"/><push state="inside-curly-string"/></rule>
|
||||
<rule pattern="\-{3,}"><token type="LiteralStringSingle"/><push state="inside-eof-string"/></rule>
|
||||
<rule><include state="builtin-functions"/></rule>
|
||||
<rule pattern="[()[\],]"><token type="Punctuation"/></rule>
|
||||
<rule pattern="(\->|==>|\||::|@|\#|\$|\&|!|!!|\./)"><token type="NameDecorator"/></rule>
|
||||
<rule pattern="(<:|:>|:<|>:|<\\|<>|<|>|ø|∞|\+|\-|\*|\~|=|\^|%|/|//|==>|<=>|<==>|=>>|<<=>>|<<==>>|\-\->|<\->|<\-\->|=\||\|=|\-:|:\-|_|\.|\.\.|\\)"><token type="Operator"/></rule>
|
||||
<rule pattern="\b\w+"><token type="Name"/></rule>
|
||||
<rule pattern="\s+"><token type="TextWhitespace"/></rule>
|
||||
<rule pattern=".+$"><token type="Error"/></rule>
|
||||
</state>
|
||||
<state name="inside-interpol">
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|]+"><usingself state="root"/></rule>
|
||||
</state>
|
||||
<state name="inside-template">
|
||||
<rule pattern="\|\|\>"><token type="LiteralStringInterpol"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|]+"><usingself state="root"/></rule>
|
||||
</state>
|
||||
<state name="string-escape">
|
||||
<rule pattern="(\\\\|\\n|\\t|\\")"><token type="LiteralStringEscape"/></rule>
|
||||
</state>
|
||||
<state name="inside-simple-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="""><token type="LiteralStringDouble"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|"]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="inside-smart-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="\n"><token type="LiteralStringSingle"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|\n]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="inside-safe-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="»»»"><token type="LiteralStringDouble"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|»]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="inside-regex-string">
|
||||
<rule pattern="\\[sSwWdDbBZApPxucItnvfr0]+"><token type="LiteralStringEscape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="\/\}"><token type="LiteralStringSingle"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|\/]+"><token type="LiteralStringRegex"/></rule>
|
||||
</state>
|
||||
<state name="inside-curly-verb-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="\:\}"><token type="LiteralStringDouble"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|<:]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="inside-curly-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="\}"><token type="LiteralStringSingle"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|<}]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="inside-eof-string">
|
||||
<rule><include state="string-escape"/></rule>
|
||||
<rule pattern="\|"><token type="LiteralStringInterpol"/><push state="inside-interpol"/></rule>
|
||||
<rule pattern="\<\|\|"><token type="LiteralStringInterpol"/><push state="inside-template"/></rule>
|
||||
<rule pattern="\Z"><token type="LiteralStringSingle"/><pop depth="1"/></rule>
|
||||
<rule pattern="[^|<]+"><token type="LiteralString"/></rule>
|
||||
</state>
|
||||
<state name="builtin-functions">
|
||||
<rule pattern="\b(all|and|any|ascii|attr|attribute|attributeLabel|binary|blockchar|contains|database|date|dictionary|empty|equal|even|every|exists|false|floatin|function|greater|greaterOrEqual|if|in|inline|integer|is|key|label|leap|less|lessOrEqual|literal|logical|lower|nand|negative|nor|not|notEqual|null|numeric|odd|or|path|pathLabel|positive|prefix|prime|set|some|sorted|standalone|string|subset|suffix|superset|ymbol|true|try|type|unless|upper|when|whitespace|word|xnor|xor|zero)\b\?"><token type="NameBuiltin"/></rule>
|
||||
<rule pattern="\b(abs|acos|acosh|acsec|acsech|actan|actanh|add|after|alphabet|and|angle|append|arg|args|arity|array|as|asec|asech|asin|asinh|atan|atan2|atanh|attr|attrs|average|before|benchmark|blend|break|builtins1|builtins2|call|capitalize|case|ceil|chop|chunk|clear|close|cluster|color|combine|conj|continue|copy|cos|cosh|couple|csec|csech|ctan|ctanh|cursor|darken|dec|decode|decouple|define|delete|desaturate|deviation|dictionary|difference|digest|digits|div|do|download|drop|dup|e|else|empty|encode|ensure|env|epsilon|escape|execute|exit|exp|extend|extract|factors|false|fdiv|filter|first|flatten|floor|fold|from|function|gamma|gcd|get|goto|hash|help|hypot|if|in|inc|indent|index|infinity|info|input|insert|inspect|intersection|invert|join|keys|kurtosis|last|let|levenshtein|lighten|list|ln|log|loop|lower|mail|map|match|max|maybe|median|min|mod|module|mul|nand|neg|new|nor|normalize|not|now|null|open|or|outdent|pad|panic|path|pause|permissions|permutate|pi|pop|pow|powerset|powmod|prefix|print|prints|process|product|query|random|range|read|relative|remove|rename|render|repeat|replace|request|return|reverse|round|sample|saturate|script|sec|sech|select|serve|set|shl|shr|shuffle|sin|sinh|size|skewness|slice|sort|split|sqrt|squeeze|stack|strip|sub|suffix|sum|switch|symbols|symlink|sys|take|tan|tanh|terminal|to|true|truncate|try|type|union|unique|unless|until|unzip|upper|values|var|variance|volume|webview|while|with|wordwrap|write|xnor|xor|zip)\b"><token type="NameBuiltin"/></rule>
|
||||
</state>
|
||||
</rules>
|
||||
</lexer>
|
||||
Vendored
+457
@@ -0,0 +1,457 @@
|
||||
#! home/.arturo/bin
|
||||
|
||||
; this is a comment
|
||||
; this is another comment
|
||||
|
||||
;---------------------------------
|
||||
; VARIABLES & VALUES
|
||||
;---------------------------------
|
||||
|
||||
; numbers
|
||||
a1: 2
|
||||
a2: 3.14
|
||||
a3: to :complex [1 2.0] ; 1.0+2.0i
|
||||
|
||||
; strings
|
||||
c1: "this is a string"
|
||||
c2: {
|
||||
this is a multiline string
|
||||
that is indentation-agnostic
|
||||
}
|
||||
|
||||
c3: {:
|
||||
this is
|
||||
a verbatim
|
||||
multiline string
|
||||
which will remain exactly
|
||||
as the original
|
||||
:}
|
||||
|
||||
c4: {
|
||||
this is a multiline string
|
||||
with |a1| lines.
|
||||
}
|
||||
|
||||
annotatedStrings: {!md
|
||||
# This is a markdown example
|
||||
|
||||
I'm a paragraph
|
||||
|
||||
> I'm a blockquote
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
char * name = "World";
|
||||
printf("Hello, %s!", name);
|
||||
printf( "I'm a highlighted C code"
|
||||
"inside Markdown highlighted code,"
|
||||
"inside Arturo. Awesome!"
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
}
|
||||
|
||||
smartString: »I'm a string until the end of line ; I'm not a comment
|
||||
safeString: «««I'm a safe string. 🙂»»» ; I'm a comment
|
||||
|
||||
; characters
|
||||
ch: `c`
|
||||
|
||||
; blocks/arrays
|
||||
d: [1 2 3]
|
||||
|
||||
; dictionaries
|
||||
e: #[
|
||||
name: "John"
|
||||
surname: "Doe"
|
||||
age: 34
|
||||
likes: [pizza spaghetti]
|
||||
]
|
||||
|
||||
; yes, functions are values too
|
||||
f: function [x][
|
||||
2 * x
|
||||
]
|
||||
|
||||
; colors - right, you can directly define them as well!
|
||||
g1: #red
|
||||
g2: #0077BF
|
||||
|
||||
; dates
|
||||
h: now ; 2021-05-03T17:10:48+02:00
|
||||
|
||||
; logical values
|
||||
i1: true
|
||||
i2: false
|
||||
i3: maybe
|
||||
|
||||
;---------------------------------
|
||||
; BASIC OPERATORS
|
||||
;---------------------------------
|
||||
|
||||
; simple arithmetic
|
||||
1 + 1 ; => 2
|
||||
8 - 1 ; => 7
|
||||
4.2 - 1.1 ; => 3.1
|
||||
10 * 2 ; => 20
|
||||
35 / 4 ; => 8
|
||||
35 // 4 ; => 8.75
|
||||
2 ^ 5 ; => 32
|
||||
5 % 3 ; => 2
|
||||
|
||||
; bitwise operators
|
||||
and 3 5 ; => 1
|
||||
or 3 5 ; => 7
|
||||
xor 3 5 ; => 6
|
||||
|
||||
; pre-defined constants
|
||||
pi ; => 3.141592653589793
|
||||
epsilon ; => 2.718281828459045
|
||||
null ; => null
|
||||
true ; => true
|
||||
false ; => false
|
||||
|
||||
;---------------------------------
|
||||
; COMPARISON OPERATORS
|
||||
;---------------------------------
|
||||
|
||||
; equality
|
||||
1 = 1 ; => true
|
||||
2 = 1 ; => false
|
||||
|
||||
; inequality
|
||||
1 <> 1 ; => false
|
||||
2 <> 1 ; => true
|
||||
|
||||
; more comparisons
|
||||
1 < 10 ; => true
|
||||
1 =< 10 ; => true
|
||||
10 =< 10 ; => true
|
||||
1 > 10 ; => false
|
||||
1 >= 10 ; => false
|
||||
11 >= 10 ; => true
|
||||
|
||||
;---------------------------------
|
||||
; CONDITIONALS
|
||||
;---------------------------------
|
||||
|
||||
; logical operators
|
||||
and? true true ; => true
|
||||
and? true false ; => false
|
||||
or? true false ; => true
|
||||
or? false false ; => false
|
||||
|
||||
and? [1=2][2<3] ; => false
|
||||
; (the second block will not be evaluated)
|
||||
|
||||
; simple if statements
|
||||
if 2 > 1 [ print "yes!"] ; yes!
|
||||
if 3 <> 2 -> print "true!" ; true!
|
||||
|
||||
; if/else statements
|
||||
if? 2 > 3 -> print "2 is greater than 3"
|
||||
else -> print "2 is not greater than 3" ; 2 is not greater than 3
|
||||
|
||||
; switch statements
|
||||
switch 2 > 3 -> print "2 is greater than 3"
|
||||
-> print "2 is not greater than 3" ; 2 is not greater than 3
|
||||
|
||||
a: (2 > 3)["yes"]["no"] ; a: "no"
|
||||
a: (2 > 3)? -> "yes" -> "no" ; a: "no" (exactly the same as above)
|
||||
|
||||
; case/when statements
|
||||
case [1]
|
||||
when? [>2] -> print "1 is greater than 2. what?!"
|
||||
when? [<0] -> print "1 is less than 0. nope..."
|
||||
else -> print "here we are!" ; here we are!
|
||||
|
||||
;---------------------------------
|
||||
; LOOPS
|
||||
;---------------------------------
|
||||
|
||||
; with `loop`
|
||||
arr: [1 4 5 3]
|
||||
loop arr 'x [
|
||||
print ["x =" x]
|
||||
]
|
||||
; x = 1
|
||||
; x = 4
|
||||
; x = 5
|
||||
; x = 3
|
||||
|
||||
; with loop and custom index
|
||||
loop.with:'i arr 'x [
|
||||
print ["item at position" i "=>" x]
|
||||
]
|
||||
; item at position 0 => 1
|
||||
; item at position 1 => 4
|
||||
; item at position 2 => 5
|
||||
; item at position 3 => 3
|
||||
|
||||
; using ranges
|
||||
loop 1..3 'x -> ; since it's a single statement
|
||||
print x ; there's no need for [block] notation
|
||||
; we can wrap it up using the `->` syntactic sugar
|
||||
|
||||
loop `a`..`c` 'ch ->
|
||||
print ch
|
||||
; a
|
||||
; b
|
||||
; c
|
||||
|
||||
; picking multiple items
|
||||
loop 1..10 [x y] ->
|
||||
print ["x =" x ", y =" y]
|
||||
; x = 1 , y = 2
|
||||
; x = 3 , y = 4
|
||||
; x = 5 , y = 6
|
||||
; x = 7 , y = 8
|
||||
; x = 9 , y = 10
|
||||
|
||||
; looping through a dictionary
|
||||
dict: #[name: "John", surname: "Doe", age: 34]
|
||||
loop dict [key value][
|
||||
print [key "->" value]
|
||||
]
|
||||
; name -> John
|
||||
; surname -> Doe
|
||||
; age -> 34
|
||||
|
||||
; while loops
|
||||
i: new 0
|
||||
while [i<3][
|
||||
print ["i =" i]
|
||||
inc 'i
|
||||
]
|
||||
; i = 0
|
||||
; i = 1
|
||||
; i = 2
|
||||
|
||||
;---------------------------------
|
||||
; STRINGS
|
||||
;---------------------------------
|
||||
|
||||
; case
|
||||
a: "tHis Is a stRinG"
|
||||
print upper a ; THIS IS A STRING
|
||||
print lower a ; this is a string
|
||||
print capitalize a ; THis Is a stRinG
|
||||
|
||||
; concatenation
|
||||
a: "Hello " ++ "World!" ; a: "Hello World!"
|
||||
|
||||
; strings as an array
|
||||
split "hello" ; => [h e l l o]
|
||||
split.words "hello world" ; => [hello world]
|
||||
|
||||
print first "hello" ; h
|
||||
print last "hello" ; o
|
||||
|
||||
; conversion
|
||||
to :string 123 ; => "123"
|
||||
to :integer "123" ; => 123
|
||||
|
||||
; joining strings together
|
||||
join ["hello" "world"] ; => "helloworld"
|
||||
join.with:"-" ["hello" "world"] ; => "hello-world"
|
||||
|
||||
; string interpolation
|
||||
x: 2
|
||||
print ~"x = |x|" ; x = 2
|
||||
|
||||
; interpolation with `print`
|
||||
print ["x =" x] ; x = 2
|
||||
; (`print` works by calculating the given block
|
||||
; and joining the different values as strings
|
||||
; with a single space between them)
|
||||
|
||||
; templates
|
||||
print render.template {
|
||||
<||= switch x=2 [ ||>
|
||||
Yes, x = 2
|
||||
<||][||>
|
||||
No, x is not 2
|
||||
<||]||>
|
||||
} ; Yes, x = 2
|
||||
|
||||
; matching
|
||||
prefix? "hello" "he" ; => true
|
||||
suffix? "hello" "he" ; => false
|
||||
|
||||
contains? "hello" "ll" ; => true
|
||||
contains? "hello" "he" ; => true
|
||||
contains? "hello" "x" ; => false
|
||||
|
||||
in? "ll" "hello" ; => true
|
||||
in? "x" "hello" ; => false
|
||||
|
||||
;---------------------------------
|
||||
; BLOCKS
|
||||
;---------------------------------
|
||||
|
||||
; calculate a block
|
||||
arr: [1 1+1 1+1+1]
|
||||
@arr ; => [1 2 3]
|
||||
|
||||
; execute a block
|
||||
sth: [print "Hello world"] ; this is perfectly valid,
|
||||
; could contain *anything*
|
||||
; and will not be executed...
|
||||
|
||||
do sth ; Hello world
|
||||
; (...until we tell it to)
|
||||
|
||||
; array indexing
|
||||
arr: ["zero" "one" "two" "three"]
|
||||
print first arr ; zero
|
||||
print arr\0 ; zero
|
||||
print last arr ; three
|
||||
print arr\3 ; three
|
||||
|
||||
x: 2
|
||||
print get arr x ; two
|
||||
print arr\[x] ; two
|
||||
|
||||
; setting an array element
|
||||
arr\0: "nada"
|
||||
set arr 2 "dos"
|
||||
print arr ; nada one dos three
|
||||
|
||||
; adding elements to an array
|
||||
arr: new []
|
||||
'arr ++ "one"
|
||||
'arr ++ "two"
|
||||
print arr ; one two
|
||||
|
||||
; remove elements from an array
|
||||
arr: new ["one" "two" "three" "four"]
|
||||
'arr -- "two" ; arr: ["one" "three" "four"]
|
||||
remove 'arr .index 0 ; arr: ["three" "four"]
|
||||
|
||||
; getting the size of an array
|
||||
arr: ["one" 2 "three" 4]
|
||||
print size arr ; 4
|
||||
|
||||
; getting a slice of an array
|
||||
print slice ["one" "two" "three" "four"] 0 1 ; one two
|
||||
|
||||
; check if array contains a specific element
|
||||
print contains? arr "one" ; true
|
||||
print contains? arr "five" ; false
|
||||
|
||||
; sorting array
|
||||
arr: [1 5 3 2 4]
|
||||
sort arr ; => [1 2 3 4 5]
|
||||
sort.descending arr ; => [5 4 3 2 1]
|
||||
|
||||
; mapping values
|
||||
map 1..10 [x][2*x] ; => [2 4 6 8 10 12 14 16 18 20]
|
||||
map 1..10 'x -> 2*x ; same as above
|
||||
map 1..10 => [2*&] ; same as above
|
||||
map 1..10 => [2*] ; same as above
|
||||
|
||||
; selecting/filtering array values
|
||||
select 1..10 [x][odd? x] ; => [1 3 5 7 9]
|
||||
select 1..10 => odd? ; same as above
|
||||
|
||||
filter 1..10 => odd? ; => [2 4 6 8 10]
|
||||
; (now, we leave out all odd numbers -
|
||||
; while select keeps them)
|
||||
|
||||
; misc operations
|
||||
arr: ["one" 2 "three" 4]
|
||||
reverse arr ; => [4 "three" 2 "one"]
|
||||
shuffle arr ; => [2 4 "three" "one"]
|
||||
unique [1 2 3 2 3 1] ; => [1 2 3]
|
||||
permutate [1 2 3] ; => [[1 2 3] [1 3 2] [3 1 2] [2 1 3] [2 3 1] [3 2 1]]
|
||||
take 1..10 3 ; => [1 2 3]
|
||||
repeat [1 2] 3 ; => [1 2 1 2 1 2]
|
||||
|
||||
;---------------------------------
|
||||
; FUNCTIONS
|
||||
;---------------------------------
|
||||
|
||||
; declaring a function
|
||||
f: function [x][ 2*x ]
|
||||
f: function [x]-> 2*x ; same as above
|
||||
f: $[x]->2*x ; same as above (only using the `$` alias
|
||||
; for the `function`... function)
|
||||
|
||||
; calling a function
|
||||
f 10 ; => 20
|
||||
|
||||
; returning a value
|
||||
g: function [x][
|
||||
if x < 2 -> return 0
|
||||
|
||||
res: 0
|
||||
loop 0..x 'z [
|
||||
res: res + z
|
||||
]
|
||||
return res
|
||||
]
|
||||
|
||||
;---------------------------------
|
||||
; CUSTOM TYPES
|
||||
;---------------------------------
|
||||
|
||||
; defining a custom type
|
||||
define :person [ ; define a new custom type "Person"
|
||||
name ; with fields: name, surname, age
|
||||
surname
|
||||
age
|
||||
][
|
||||
; with custom post-construction initializer
|
||||
init: [
|
||||
this\name: capitalize this\name
|
||||
]
|
||||
|
||||
; custom print function
|
||||
print: [
|
||||
render "NAME: |this\name|, SURNAME: |this\surname|, AGE: |this\age|"
|
||||
]
|
||||
|
||||
; custom comparison operator
|
||||
compare: 'age
|
||||
]
|
||||
|
||||
; create a method for our custom type
|
||||
sayHello: function [this][
|
||||
ensure -> is? :person this
|
||||
|
||||
print ["Hello" this\name]
|
||||
]
|
||||
|
||||
; create new objects of our custom type
|
||||
a: to :person ["John" "Doe" 34] ; let's create 2 "Person"s
|
||||
b: to :person ["jane" "Doe" 33] ; and another one
|
||||
|
||||
; call pseudo-inner method
|
||||
sayHello a ; Hello John
|
||||
sayHello b ; Hello Jane
|
||||
|
||||
; access object fields
|
||||
print ["The first person's name is:" a\name] ; The first person's name is: John
|
||||
print ["The second person's name is:" b\name] ; The second person's name is: Jane
|
||||
|
||||
; changing object fields
|
||||
a\name: "Bob"
|
||||
sayHello a ; Hello Bob
|
||||
|
||||
; verifying object type
|
||||
print type a ; :person
|
||||
print is? :person a ; true
|
||||
|
||||
; printing objects
|
||||
print a ; NAME: John, SURNAME: Doe, AGE: 34
|
||||
|
||||
; sorting user objects (using custom comparator)
|
||||
sort @[a b] ; Jane..., John...
|
||||
sort.descending @[a b] ; John..., Jane...
|
||||
|
||||
eofString:
|
||||
---
|
||||
I'm an end of file
|
||||
Multiline string.
|
||||
Vendored
+2137
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user