fancy 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORS +7 -0
- data/LICENSE +19 -0
- data/README +173 -0
- data/Rakefile +255 -0
- data/bin/fancy +40 -0
- data/bin/fdoc +23 -0
- data/bin/fyi +22 -0
- data/bin/ifancy +46 -0
- data/boot/README +12 -0
- data/boot/code_loader.rb +165 -0
- data/boot/compile.fy +3 -0
- data/boot/fancy_ext.rb +13 -0
- data/boot/fancy_ext/block_env.rb +29 -0
- data/boot/fancy_ext/class.rb +26 -0
- data/boot/fancy_ext/kernel.rb +12 -0
- data/boot/fancy_ext/module.rb +89 -0
- data/boot/fancy_ext/object.rb +34 -0
- data/boot/fancy_ext/string_helper.rb +10 -0
- data/boot/load.rb +72 -0
- data/boot/rbx-compiler/README +12 -0
- data/boot/rbx-compiler/compiler.rb +24 -0
- data/boot/rbx-compiler/compiler/ast.rb +23 -0
- data/boot/rbx-compiler/compiler/ast/README +11 -0
- data/boot/rbx-compiler/compiler/ast/array_literal.rb +13 -0
- data/boot/rbx-compiler/compiler/ast/assign.rb +57 -0
- data/boot/rbx-compiler/compiler/ast/block.rb +70 -0
- data/boot/rbx-compiler/compiler/ast/class_def.rb +35 -0
- data/boot/rbx-compiler/compiler/ast/expression_list.rb +57 -0
- data/boot/rbx-compiler/compiler/ast/hash_literal.rb +11 -0
- data/boot/rbx-compiler/compiler/ast/identifier.rb +120 -0
- data/boot/rbx-compiler/compiler/ast/match.rb +81 -0
- data/boot/rbx-compiler/compiler/ast/message_send.rb +71 -0
- data/boot/rbx-compiler/compiler/ast/method_def.rb +116 -0
- data/boot/rbx-compiler/compiler/ast/node.rb +6 -0
- data/boot/rbx-compiler/compiler/ast/range_literal.rb +22 -0
- data/boot/rbx-compiler/compiler/ast/require.rb +20 -0
- data/boot/rbx-compiler/compiler/ast/return.rb +29 -0
- data/boot/rbx-compiler/compiler/ast/ruby_args.rb +35 -0
- data/boot/rbx-compiler/compiler/ast/script.rb +56 -0
- data/boot/rbx-compiler/compiler/ast/singleton_method_def.rb +39 -0
- data/boot/rbx-compiler/compiler/ast/string_literal.rb +14 -0
- data/boot/rbx-compiler/compiler/ast/super.rb +25 -0
- data/boot/rbx-compiler/compiler/ast/try_catch_block.rb +220 -0
- data/boot/rbx-compiler/compiler/ast/tuple_literal.rb +33 -0
- data/boot/rbx-compiler/compiler/command.rb +39 -0
- data/boot/rbx-compiler/compiler/compiler.rb +83 -0
- data/boot/rbx-compiler/compiler/stages.rb +99 -0
- data/boot/rbx-compiler/parser.rb +2 -0
- data/boot/rbx-compiler/parser/README +15 -0
- data/boot/rbx-compiler/parser/Rakefile +54 -0
- data/boot/rbx-compiler/parser/extconf.rb +3 -0
- data/boot/rbx-compiler/parser/fancy_parser.bundle +0 -0
- data/boot/rbx-compiler/parser/fancy_parser.c +46 -0
- data/boot/rbx-compiler/parser/fancy_parser.h +8 -0
- data/boot/rbx-compiler/parser/lexer.lex +180 -0
- data/boot/rbx-compiler/parser/parser.rb +356 -0
- data/boot/rbx-compiler/parser/parser.y +711 -0
- data/boot/rsexp_pretty_printer.rb +76 -0
- data/doc/api/fancy.css +93 -0
- data/doc/api/fancy.jsonp +1 -0
- data/doc/api/fdoc.js +187 -0
- data/doc/api/index.html +57 -0
- data/doc/api/underscore-min.js +18 -0
- data/doc/features.md +228 -0
- data/examples/argv.fy +8 -0
- data/examples/arithmetic.fy +7 -0
- data/examples/armstrong_numbers.fy +33 -0
- data/examples/array.fy +52 -0
- data/examples/blocks.fy +15 -0
- data/examples/boolean.fy +24 -0
- data/examples/call_with_receiver.fy +9 -0
- data/examples/class.fy +68 -0
- data/examples/closures.fy +24 -0
- data/examples/constant_access.fy +15 -0
- data/examples/default_args.fy +17 -0
- data/examples/define_methods.fy +15 -0
- data/examples/documentation.fy +57 -0
- data/examples/documentation_formatters.fy +25 -0
- data/examples/echo.fy +16 -0
- data/examples/empty_catch.fy +4 -0
- data/examples/exception.fy +9 -0
- data/examples/factorial.fy +12 -0
- data/examples/fibonacci.fy +16 -0
- data/examples/files.fy +23 -0
- data/examples/finally.fy +5 -0
- data/examples/game_of_life.fy +148 -0
- data/examples/hashes.fy +7 -0
- data/examples/hello_world.fy +6 -0
- data/examples/html_generator.fy +54 -0
- data/examples/implicit_return.fy +3 -0
- data/examples/matchers.fy +6 -0
- data/examples/methods.fy +29 -0
- data/examples/nested_classes.fy +27 -0
- data/examples/nested_try.fy +9 -0
- data/examples/numbers.fy +12 -0
- data/examples/pattern_matching.fy +40 -0
- data/examples/person.fy +65 -0
- data/examples/project-euler/01.fy +8 -0
- data/examples/project-euler/02.fy +21 -0
- data/examples/project-euler/28.fy +33 -0
- data/examples/rbx/and_or.fy +7 -0
- data/examples/rbx/blocks.fy +22 -0
- data/examples/rbx/classes.fy +32 -0
- data/examples/rbx/hello.fy +8 -0
- data/examples/rbx/include.fy +12 -0
- data/examples/rbx/inherit.fy +11 -0
- data/examples/rbx/methods.fy +15 -0
- data/examples/rbx/nested_classes.fy +9 -0
- data/examples/rbx/require.fy +3 -0
- data/examples/rbx/strings.fy +5 -0
- data/examples/regex.fy +7 -0
- data/examples/require.fy +7 -0
- data/examples/retry.fy +12 -0
- data/examples/return.fy +13 -0
- data/examples/ruby_require.fy +7 -0
- data/examples/ruby_send.fy +3 -0
- data/examples/singleton_methods.fy +21 -0
- data/examples/stupid_quicksort.fy +12 -0
- data/examples/threads.fy +18 -0
- data/examples/tuple.fy +8 -0
- data/examples/webserver/webserver.fy +18 -0
- data/lib/argv.fy +36 -0
- data/lib/array.fy +207 -0
- data/lib/block.fy +88 -0
- data/lib/boot.fy +41 -0
- data/lib/class.fy +106 -0
- data/lib/compiler.fy +14 -0
- data/lib/compiler/ast.fy +40 -0
- data/lib/compiler/ast/assign.fy +96 -0
- data/lib/compiler/ast/block.fy +84 -0
- data/lib/compiler/ast/class_def.fy +33 -0
- data/lib/compiler/ast/expression_list.fy +47 -0
- data/lib/compiler/ast/identifier.fy +113 -0
- data/lib/compiler/ast/literals.fy +122 -0
- data/lib/compiler/ast/match.fy +88 -0
- data/lib/compiler/ast/message_send.fy +110 -0
- data/lib/compiler/ast/method_def.fy +90 -0
- data/lib/compiler/ast/node.fy +7 -0
- data/lib/compiler/ast/range.fy +16 -0
- data/lib/compiler/ast/require.fy +15 -0
- data/lib/compiler/ast/return.fy +23 -0
- data/lib/compiler/ast/script.fy +52 -0
- data/lib/compiler/ast/singleton_method_def.fy +35 -0
- data/lib/compiler/ast/super.fy +17 -0
- data/lib/compiler/ast/try_catch.fy +176 -0
- data/lib/compiler/ast/tuple_literal.fy +34 -0
- data/lib/compiler/command.fy +51 -0
- data/lib/compiler/compiler.fy +73 -0
- data/lib/compiler/stages.fy +81 -0
- data/lib/directory.fy +17 -0
- data/lib/documentation.fy +115 -0
- data/lib/enumerable.fy +269 -0
- data/lib/eval.fy +31 -0
- data/lib/fancy_spec.fy +202 -0
- data/lib/fdoc.fy +359 -0
- data/lib/fdoc_hook.fy +10 -0
- data/lib/file.fy +54 -0
- data/lib/hash.fy +56 -0
- data/lib/main.fy +80 -0
- data/lib/method.fy +22 -0
- data/lib/nil_class.fy +56 -0
- data/lib/number.fy +87 -0
- data/lib/object.fy +170 -0
- data/lib/package.fy +61 -0
- data/lib/package/dependency.fy +24 -0
- data/lib/package/installer.fy +180 -0
- data/lib/package/specification.fy +55 -0
- data/lib/package/uninstaller.fy +15 -0
- data/lib/parser.fy +4 -0
- data/lib/parser/ext/README +15 -0
- data/lib/parser/ext/ext.c +42 -0
- data/lib/parser/ext/ext.h +8 -0
- data/lib/parser/ext/extconf.rb +3 -0
- data/lib/parser/ext/lexer.lex +187 -0
- data/lib/parser/ext/parser.y +744 -0
- data/lib/parser/methods.fy +297 -0
- data/lib/rbx.fy +37 -0
- data/lib/rbx/array.fy +237 -0
- data/lib/rbx/bignum.fy +23 -0
- data/lib/rbx/block.fy +9 -0
- data/lib/rbx/class.fy +129 -0
- data/lib/rbx/code_loader.fy +192 -0
- data/lib/rbx/console.fy +63 -0
- data/lib/rbx/directory.fy +46 -0
- data/lib/rbx/documentation.fy +64 -0
- data/lib/rbx/environment_variables.fy +3 -0
- data/lib/rbx/exception.fy +30 -0
- data/lib/rbx/false_class.fy +58 -0
- data/lib/rbx/fiber.fy +25 -0
- data/lib/rbx/file.fy +191 -0
- data/lib/rbx/fixnum.fy +25 -0
- data/lib/rbx/float.fy +14 -0
- data/lib/rbx/hash.fy +38 -0
- data/lib/rbx/integer.fy +15 -0
- data/lib/rbx/io.fy +30 -0
- data/lib/rbx/match_data.fy +9 -0
- data/lib/rbx/method.fy +22 -0
- data/lib/rbx/name_error.fy +3 -0
- data/lib/rbx/no_method_error.fy +15 -0
- data/lib/rbx/object.fy +117 -0
- data/lib/rbx/range.fy +15 -0
- data/lib/rbx/regexp.fy +9 -0
- data/lib/rbx/string.fy +63 -0
- data/lib/rbx/symbol.fy +12 -0
- data/lib/rbx/system.fy +37 -0
- data/lib/rbx/tcp_server.fy +6 -0
- data/lib/rbx/tcp_socket.fy +7 -0
- data/lib/rbx/thread.fy +75 -0
- data/lib/rbx/tuple.fy +37 -0
- data/lib/set.fy +61 -0
- data/lib/stack.fy +51 -0
- data/lib/string.fy +58 -0
- data/lib/struct.fy +13 -0
- data/lib/symbol.fy +23 -0
- data/lib/true_class.fy +43 -0
- data/lib/tuple.fy +68 -0
- data/lib/version.fy +6 -0
- data/tests/argv.fy +13 -0
- data/tests/array.fy +343 -0
- data/tests/assignment.fy +53 -0
- data/tests/block.fy +103 -0
- data/tests/class.fy +409 -0
- data/tests/control_flow.fy +79 -0
- data/tests/documentation.fy +24 -0
- data/tests/exception.fy +115 -0
- data/tests/file.fy +86 -0
- data/tests/hash.fy +101 -0
- data/tests/method.fy +131 -0
- data/tests/nil_class.fy +55 -0
- data/tests/number.fy +128 -0
- data/tests/object.fy +125 -0
- data/tests/parsing/sexp.fy +50 -0
- data/tests/pattern_matching.fy +82 -0
- data/tests/range.fy +11 -0
- data/tests/set.fy +10 -0
- data/tests/stack.fy +22 -0
- data/tests/string.fy +102 -0
- data/tests/symbol.fy +17 -0
- data/tests/true_class.fy +63 -0
- data/tests/tuple.fy +21 -0
- data/tools/fancy-mode.el +63 -0
- metadata +321 -0
data/lib/fdoc_hook.fy
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
class Fancy FDoc {
|
2
|
+
# A hash to keep all objects to extract documentation from.
|
3
|
+
# Having this hash only for FDoc is fine IMHO. As we dont need
|
4
|
+
# to use ObjectSpace or anything like that. Just to produce docs.
|
5
|
+
@documented_objects = <[]>
|
6
|
+
Fancy Documentation on_documentation_set: |object, documentation| {
|
7
|
+
@documented_objects store(object, documentation)
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
data/lib/file.fy
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
class File {
|
2
|
+
"""
|
3
|
+
Instances of File represent files in the filesystem of the operating
|
4
|
+
system on which Fancy is running.
|
5
|
+
"""
|
6
|
+
|
7
|
+
# def self exists?: filename {
|
8
|
+
# "Indicates, if a File exists with a given filename."
|
9
|
+
# try {
|
10
|
+
# f = File open: filename modes: ['read]
|
11
|
+
# f close
|
12
|
+
# true
|
13
|
+
# } catch IOError => e {
|
14
|
+
# nil
|
15
|
+
# }
|
16
|
+
# }
|
17
|
+
|
18
|
+
def self read: filename {
|
19
|
+
"""
|
20
|
+
Reads all the contens (in ASCII mode) of a given file and returns
|
21
|
+
them as an Array of lines being read.
|
22
|
+
"""
|
23
|
+
|
24
|
+
lines = []
|
25
|
+
File open: filename modes: ['read] with: |f| {
|
26
|
+
{ f eof? } while_false: {
|
27
|
+
lines << (f readln)
|
28
|
+
}
|
29
|
+
}
|
30
|
+
lines join: "\n"
|
31
|
+
}
|
32
|
+
|
33
|
+
def writeln: x {
|
34
|
+
"""
|
35
|
+
Writes a given argument as a String followed by a newline into the
|
36
|
+
File.
|
37
|
+
"""
|
38
|
+
|
39
|
+
write: x
|
40
|
+
self newline
|
41
|
+
}
|
42
|
+
|
43
|
+
def print: x {
|
44
|
+
"Same as File#write:."
|
45
|
+
|
46
|
+
write: x
|
47
|
+
}
|
48
|
+
|
49
|
+
def println: x {
|
50
|
+
"Same as File#writeln:."
|
51
|
+
|
52
|
+
writeln: x
|
53
|
+
}
|
54
|
+
}
|
data/lib/hash.fy
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
class Hash {
|
2
|
+
"""
|
3
|
+
Class for Hashes (HashMaps / Dictionaries).
|
4
|
+
Maps a key to a value.
|
5
|
+
"""
|
6
|
+
|
7
|
+
include: FancyEnumerable
|
8
|
+
|
9
|
+
def [] key {
|
10
|
+
"Returns the value for a given key."
|
11
|
+
|
12
|
+
at: key
|
13
|
+
}
|
14
|
+
|
15
|
+
def each: block {
|
16
|
+
"Calls a given Block with each key and value."
|
17
|
+
|
18
|
+
if: (block argcount == 1) then: {
|
19
|
+
self keys each: |key| {
|
20
|
+
block call: [[key, at: key]]
|
21
|
+
}
|
22
|
+
} else: {
|
23
|
+
self keys each: |key| {
|
24
|
+
block call: [key, at: key]
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
def each_key: block {
|
30
|
+
"Calls a given Block with each key."
|
31
|
+
|
32
|
+
self keys each: |key| {
|
33
|
+
block call: [key]
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
def each_value: block {
|
38
|
+
"Calls a given Block with each value."
|
39
|
+
|
40
|
+
self values each: |val| {
|
41
|
+
block call: [val]
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
def to_a {
|
46
|
+
"Returns an Array of the key-value pairs in a Hash."
|
47
|
+
|
48
|
+
map: |pair| { pair }
|
49
|
+
}
|
50
|
+
|
51
|
+
def to_s {
|
52
|
+
"Returns a string representation of a Hash."
|
53
|
+
|
54
|
+
self to_a to_s
|
55
|
+
}
|
56
|
+
}
|
data/lib/main.fy
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# main.fy
|
2
|
+
# This file gets run directly from bin/fancy.
|
3
|
+
# It loads up all the necessary auto-load files by loading lib/boot.fy
|
4
|
+
# and handles any given ARGV options.
|
5
|
+
# Finally, if any .fy source filename is passed in via ARGV, it is
|
6
|
+
# loaded and executed.
|
7
|
+
|
8
|
+
ARGV for_options: ["-v", "--version"] do: {
|
9
|
+
"Fancy " ++ FANCY_VERSION println
|
10
|
+
"(C) 2010, 2011 Christopher Bertels <chris@fancy-lang.org>" println
|
11
|
+
System exit
|
12
|
+
}
|
13
|
+
|
14
|
+
ARGV for_options: ["--help", "-h"] do: {
|
15
|
+
["Usage: fancy [option] [programfile] [arguments]",
|
16
|
+
" --help Print this output",
|
17
|
+
" -h Print this output",
|
18
|
+
" --version Print Fancy's version number",
|
19
|
+
" -v Print Fancy's version number",
|
20
|
+
" -I directory Add directory to Fancy's LOAD_PATH",
|
21
|
+
" -e 'command' One line of Fancy code that gets evaluated immediately",
|
22
|
+
" -c [filenames] Compile given files to Rubinius bytecode",
|
23
|
+
" -cv [filenames] Compile given files to Rubinius bytecode verbosely (outputting the generated bytecode).",
|
24
|
+
"",
|
25
|
+
"Fancy package management:",
|
26
|
+
" install [packagename] Install a Fancy package with a given name to $FANCYPACK_DIR",
|
27
|
+
" uninstall [packagename] Uninstall a Fancy package with a given name from $FANCYPACK_DIR"] println
|
28
|
+
System exit # quit when running --help
|
29
|
+
}
|
30
|
+
|
31
|
+
ARGV for_option: "-I" do: |path| {
|
32
|
+
Fancy CodeLoader push_loadpath: path
|
33
|
+
}
|
34
|
+
|
35
|
+
ARGV for_option: "-e" do: |eval_string| {
|
36
|
+
eval_string eval
|
37
|
+
System exit # quit when running with -e
|
38
|
+
}
|
39
|
+
|
40
|
+
ARGV for_option: "-c" do: {
|
41
|
+
ARGV index: "-c" . if_do: |idx| {
|
42
|
+
ARGV[[idx + 1, -1]] each: |filename| {
|
43
|
+
"Compiling " ++ filename println
|
44
|
+
Fancy Compiler compile_file: filename to: nil line: 1 print: false
|
45
|
+
}
|
46
|
+
}
|
47
|
+
System exit
|
48
|
+
}
|
49
|
+
|
50
|
+
ARGV for_option: "-cv" do: {
|
51
|
+
ARGV index: "-cv" . if_do: |idx| {
|
52
|
+
ARGV[[idx + 1, -1]] each: |filename| {
|
53
|
+
"Compiling " ++ filename println
|
54
|
+
Fancy Compiler compile_file: filename to: nil line: 1 print: true
|
55
|
+
}
|
56
|
+
}
|
57
|
+
System exit
|
58
|
+
}
|
59
|
+
|
60
|
+
ARGV for_option: "install" do: |package_name| {
|
61
|
+
Fancy Package install: package_name
|
62
|
+
System exit
|
63
|
+
}
|
64
|
+
|
65
|
+
ARGV for_option: "uninstall" do: |package_name| {
|
66
|
+
Fancy Package uninstall: package_name
|
67
|
+
System exit
|
68
|
+
}
|
69
|
+
|
70
|
+
# push package install dir to load_path
|
71
|
+
Fancy Package add_to_loadpath
|
72
|
+
|
73
|
+
# Load a source file, if any given:
|
74
|
+
ARGV first if_do: |file| {
|
75
|
+
Fancy CodeLoader load_compiled_file: file
|
76
|
+
}
|
77
|
+
|
78
|
+
ARGV empty? if_do: {
|
79
|
+
require: "../bin/ifancy"
|
80
|
+
}
|
data/lib/method.fy
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
class Method {
|
2
|
+
"""
|
3
|
+
An instance of Method represents a method on a Class.
|
4
|
+
Every method in Fancy is an instance of the Method class.
|
5
|
+
"""
|
6
|
+
|
7
|
+
def tests {
|
8
|
+
"""
|
9
|
+
Returns an Array of all the FancySpec SpecTests defined for a
|
10
|
+
Method.
|
11
|
+
"""
|
12
|
+
|
13
|
+
@tests if_nil: { @tests = [] }
|
14
|
+
@tests
|
15
|
+
}
|
16
|
+
|
17
|
+
def test: test_block {
|
18
|
+
it = FancySpec new: self
|
19
|
+
test_block call: [it]
|
20
|
+
self tests << it
|
21
|
+
}
|
22
|
+
}
|
data/lib/nil_class.fy
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
class NilClass {
|
2
|
+
"NilClass. The class of the singleton nil value."
|
3
|
+
|
4
|
+
def NilClass new {
|
5
|
+
# always return nil singleton object when trying to create a new
|
6
|
+
# NilClass instance
|
7
|
+
nil
|
8
|
+
}
|
9
|
+
|
10
|
+
def if_true: then_block else: else_block {
|
11
|
+
"Calls else_block."
|
12
|
+
else_block call
|
13
|
+
}
|
14
|
+
|
15
|
+
def if_true: block {
|
16
|
+
"Returns nil."
|
17
|
+
nil
|
18
|
+
}
|
19
|
+
|
20
|
+
def if_false: block {
|
21
|
+
"Calls the block."
|
22
|
+
block call
|
23
|
+
}
|
24
|
+
|
25
|
+
def if_nil: block {
|
26
|
+
"Calls the block."
|
27
|
+
block call
|
28
|
+
}
|
29
|
+
|
30
|
+
def nil? {
|
31
|
+
"Returns true."
|
32
|
+
true
|
33
|
+
}
|
34
|
+
|
35
|
+
def false? {
|
36
|
+
"Returns true."
|
37
|
+
true
|
38
|
+
}
|
39
|
+
|
40
|
+
def true? {
|
41
|
+
"Returns nil."
|
42
|
+
false
|
43
|
+
}
|
44
|
+
|
45
|
+
def to_s {
|
46
|
+
""
|
47
|
+
}
|
48
|
+
|
49
|
+
def to_a {
|
50
|
+
[]
|
51
|
+
}
|
52
|
+
|
53
|
+
def not {
|
54
|
+
true
|
55
|
+
}
|
56
|
+
}
|
data/lib/number.fy
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
class Number {
|
2
|
+
"""
|
3
|
+
Number is a mixin-class for all number values (integer & floats for
|
4
|
+
now).
|
5
|
+
"""
|
6
|
+
|
7
|
+
def upto: num {
|
8
|
+
i = self
|
9
|
+
arr = []
|
10
|
+
while: { i <= num } do: {
|
11
|
+
arr << i
|
12
|
+
i = i + 1
|
13
|
+
}
|
14
|
+
arr
|
15
|
+
}
|
16
|
+
|
17
|
+
def downto: num {
|
18
|
+
i = self
|
19
|
+
arr = []
|
20
|
+
while: { i >= num } do: {
|
21
|
+
arr << i
|
22
|
+
i = i - 1
|
23
|
+
}
|
24
|
+
arr
|
25
|
+
}
|
26
|
+
|
27
|
+
def squared {
|
28
|
+
"Returns the square of a Number."
|
29
|
+
|
30
|
+
self * self
|
31
|
+
}
|
32
|
+
|
33
|
+
def doubled {
|
34
|
+
"Returns the double value of a Number."
|
35
|
+
|
36
|
+
self + self
|
37
|
+
}
|
38
|
+
|
39
|
+
def abs {
|
40
|
+
"Returns the absolute (positive) value of a Number."
|
41
|
+
|
42
|
+
if: (self < 0) then: {
|
43
|
+
self * -1
|
44
|
+
} else: {
|
45
|
+
self
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
def negate {
|
50
|
+
"Negates a Number (-1 becomes 1 and vice versa)."
|
51
|
+
|
52
|
+
self * -1
|
53
|
+
}
|
54
|
+
|
55
|
+
def even? {
|
56
|
+
"Indicates, if a Number is even."
|
57
|
+
|
58
|
+
modulo: 2 . == 0
|
59
|
+
}
|
60
|
+
|
61
|
+
def odd? {
|
62
|
+
"Indicates, if a Number is odd."
|
63
|
+
|
64
|
+
self even? not
|
65
|
+
}
|
66
|
+
|
67
|
+
def to_num {
|
68
|
+
self
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
class FancyEnumerable {
|
73
|
+
def sum {
|
74
|
+
"""Calculates the sum of all the elements in the @Enumerable
|
75
|
+
(assuming them to be @Number@s (implementing '+' & '*'))."""
|
76
|
+
|
77
|
+
reduce: |x y| { x + y } init_val: 0
|
78
|
+
}
|
79
|
+
|
80
|
+
def product {
|
81
|
+
"""Calculates the product of all the elements in the @Enumerable
|
82
|
+
(assuming them to be @Number@s (implementing @+ & @*))."""
|
83
|
+
|
84
|
+
reduce: |x y| { x * y } init_val: 1
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
data/lib/object.fy
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
class Object {
|
2
|
+
"""
|
3
|
+
Root class of Fancy's class hierarchy.
|
4
|
+
All classes inherit from Object.
|
5
|
+
"""
|
6
|
+
|
7
|
+
def loop: block {
|
8
|
+
"Infinitely calls the block (loops)."
|
9
|
+
{ true } while_true: {
|
10
|
+
block call
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
def println {
|
15
|
+
"Same as Console println: self. Prints the object on STDOUT, followed by a newline."
|
16
|
+
Console println: $ self to_s
|
17
|
+
}
|
18
|
+
|
19
|
+
def print {
|
20
|
+
"Same as Console print: self. Prints the object on STDOUT."
|
21
|
+
Console print: $ self to_s
|
22
|
+
}
|
23
|
+
|
24
|
+
def != other {
|
25
|
+
"Indicates, if two objects are unequal."
|
26
|
+
self == other not
|
27
|
+
}
|
28
|
+
|
29
|
+
def if_false: block {
|
30
|
+
"Calls the block."
|
31
|
+
nil
|
32
|
+
}
|
33
|
+
|
34
|
+
def if_nil: block {
|
35
|
+
"Returns nil."
|
36
|
+
nil
|
37
|
+
}
|
38
|
+
|
39
|
+
def nil? {
|
40
|
+
"Returns nil."
|
41
|
+
nil
|
42
|
+
}
|
43
|
+
|
44
|
+
def false? {
|
45
|
+
"Returns nil."
|
46
|
+
nil
|
47
|
+
}
|
48
|
+
|
49
|
+
def true? {
|
50
|
+
"Returns nil."
|
51
|
+
nil
|
52
|
+
}
|
53
|
+
|
54
|
+
def if_do: block {
|
55
|
+
"If the object is non-nil, it calls the given block with itself as argument."
|
56
|
+
|
57
|
+
match self -> {
|
58
|
+
case nil -> nil
|
59
|
+
case false -> nil
|
60
|
+
case _ -> block call: [self]
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
def if_do: then_block else: else_block {
|
65
|
+
"""If the object is non-nil, it calls the given then_block with itself as argument.
|
66
|
+
Otherwise it calls the given else_block."""
|
67
|
+
|
68
|
+
match self -> {
|
69
|
+
case nil -> else_block call: [self]
|
70
|
+
case false -> else_block call: [self]
|
71
|
+
case _ -> then_block call: [self]
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
def or_take: other {
|
76
|
+
"Returns self if it's non-nil, otherwise returns the given object."
|
77
|
+
|
78
|
+
if: (self nil?) then: {
|
79
|
+
other
|
80
|
+
} else: {
|
81
|
+
self
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
def to_num {
|
86
|
+
0
|
87
|
+
}
|
88
|
+
|
89
|
+
def to_a {
|
90
|
+
[self]
|
91
|
+
}
|
92
|
+
|
93
|
+
def to_i {
|
94
|
+
0
|
95
|
+
}
|
96
|
+
|
97
|
+
def || other {
|
98
|
+
"Same as Object#or:"
|
99
|
+
or: other
|
100
|
+
}
|
101
|
+
|
102
|
+
def && other {
|
103
|
+
"Same as Object#and:"
|
104
|
+
and: other
|
105
|
+
}
|
106
|
+
|
107
|
+
def if: cond then: block {
|
108
|
+
"""
|
109
|
+
Same as:
|
110
|
+
cond if_do: block
|
111
|
+
"""
|
112
|
+
cond if_do: block
|
113
|
+
}
|
114
|
+
|
115
|
+
def if: cond then: then_block else: else_block {
|
116
|
+
"""
|
117
|
+
Same as:
|
118
|
+
cond if_do: then_block else: else_block
|
119
|
+
"""
|
120
|
+
cond if_do: then_block else: else_block
|
121
|
+
}
|
122
|
+
|
123
|
+
def while: cond_block do: body_block {
|
124
|
+
"""
|
125
|
+
Same as:
|
126
|
+
cond_block while_do: body_block
|
127
|
+
"""
|
128
|
+
|
129
|
+
cond_block while_do: body_block
|
130
|
+
}
|
131
|
+
|
132
|
+
def until: cond_block do: body_block {
|
133
|
+
"""
|
134
|
+
Same as:
|
135
|
+
cond_block until_do: body_block
|
136
|
+
"""
|
137
|
+
|
138
|
+
cond_block until_do: body_block
|
139
|
+
}
|
140
|
+
|
141
|
+
def unless: cond do: block {
|
142
|
+
"""
|
143
|
+
Same as:
|
144
|
+
cond if_do: { nil } else: block
|
145
|
+
"""
|
146
|
+
|
147
|
+
cond if_do: { nil } else: block
|
148
|
+
}
|
149
|
+
|
150
|
+
def method: method_name {
|
151
|
+
"Returns the method with a given name for self, if defined."
|
152
|
+
|
153
|
+
method(message_name: method_name)
|
154
|
+
}
|
155
|
+
|
156
|
+
def documentation {
|
157
|
+
"Returns the documentation string for an Object."
|
158
|
+
Fancy Documentation for: self . to_s
|
159
|
+
}
|
160
|
+
|
161
|
+
def documentation: str {
|
162
|
+
"Sets the documentation string for an Object."
|
163
|
+
Fancy Documentation for: self is: str
|
164
|
+
}
|
165
|
+
|
166
|
+
def identity {
|
167
|
+
"The identity method simply returns self."
|
168
|
+
self
|
169
|
+
}
|
170
|
+
}
|