Skip to content

Commit

Permalink
HUUGE CHANGE: Semicolons are history - or at least completely optiona…
Browse files Browse the repository at this point in the history
…l now! A newline will do it, except for literal strings, hashes, arrays & blocks, which _can_ have linebreaks in them. Much nicer syntax for Fancy. Yay!
  • Loading branch information
bakkdoor committed Sep 9, 2010
1 parent f708b6c commit 0e81073
Show file tree
Hide file tree
Showing 68 changed files with 1,309 additions and 1,292 deletions.
2 changes: 1 addition & 1 deletion bin/ifancy
@@ -1,7 +1,7 @@
#!/usr/bin/env fancy

["Welcome to the (still very simple) Fancy REPL",
"Fancy " ++ FANCY_VERSION] println;
"Fancy " ++ FANCY_VERSION] println

ARGV rest each: |file| {
"LOADING: " ++ file println;
Expand Down
2 changes: 1 addition & 1 deletion examples/argv.fnc
@@ -1,4 +1,4 @@
"This will always get printed, even when required from another file" println;
"This will always get printed, even when required from another file" println

ARGV[0] == __FILE__ if_true: {
"This will get printed, if this file is directly run with fancy" println
Expand Down
6 changes: 3 additions & 3 deletions examples/arithmetic.fnc
@@ -1,4 +1,4 @@
23 + 32432.32 println;
324432.432 - 32432.4324 println;
32432 * 432.32 println;
23 + 32432.32 println
324432.432 - 32432.4324 println
32432 * 432.32 println
432 / 12319 println
20 changes: 10 additions & 10 deletions examples/armstrong_numbers.fnc
Expand Up @@ -5,26 +5,26 @@
def class Number {
def decimals {
"""Returns all decimals of a Number as an Array.
E.g. 123 decimals # => [1,2,3]""";
E.g. 123 decimals # => [1,2,3]"""

decimals = [];
tmp = self;
decimals = []
tmp = self
{ tmp >= 10 } while_true: {
decimals << (tmp modulo: 10);
decimals << (tmp modulo: 10)
tmp = (tmp div: 10)
};
decimals << tmp;
}
decimals << tmp
decimals
}

def armstrong? {
"Indicates, if a Number is a Armstrong Number.";
"Indicates, if a Number is a Armstrong Number."

decimals = self decimals;
n_decimals = decimals size;
decimals = self decimals
n_decimals = decimals size
decimals map: |x| { x ** n_decimals } . sum == self
}
};
}

# output alls Armstrong Numbers between 0 and 10000
0 upto: 10000 do_each: |i| {
Expand Down
42 changes: 21 additions & 21 deletions examples/array.fnc
@@ -1,51 +1,51 @@
# some array example code

# create an array
arr = [1,2,3,4,5,6];
arr = [1,2,3,4,5,6]

# print each element squared
arr each: |x| {
x squared println
};
}

# display each element with its index in the array
arr each_with_index: |x i| {
"Index " ++ i ++ " -> " ++ x println
};
}

# print the array of squared elements
arr map: 'squared . inspect println;
arr map: 'squared . inspect println

# print the array of doubled elements
arr map: 'doubled . inspect println;
arr map: 'doubled . inspect println

# print array of all elements smaller than 4
arr select: |x| { x < 4 } . inspect println;
arr select: |x| { x < 4 } . inspect println

# print array of all elements that are not smaller than 4
arr reject: |x| { x < 4 } . inspect println;
arr reject: |x| { x < 4 } . inspect println

# prints: [5, 6]
arr take_while: |x| { x < 5 } . inspect println;
arr take_while: |x| { x < 5 } . inspect println

"testing reduce:with: " print;
arr reduce: |acc x| { acc * x } with: 1 . println; # same as: 1*1*2*3*4*5*6
"testing reduce:with: " print
arr reduce: |acc x| { acc * x } with: 1 . println # same as: 1*1*2*3*4*5*6

"testing any?: " print;
arr any?: |x| { x > 3 } . println; # prints: true
"testing any?: " print
arr any?: |x| { x > 3 } . println # prints: true

"testing all?: " print;
arr all?: |x| { x < 7 } . println; # prints: true
"testing all?: " print
arr all?: |x| { x < 7 } . println # prints: true

"testing from:to: " print;
arr [[3,5]] . inspect println; # prints: [4, 5, 6]
"testing from:to: " print
arr [[3,5]] . inspect println # prints: [4, 5, 6]

# some other handy methods
"testing size: " print;
arr size println; # prints: 6
"testing size: " print
arr size println # prints: 6

"testing to_s: " print;
arr to_s println; # prints: 123456
"testing to_s: " print
arr to_s println # prints: 123456

"testing inspect: " print;
"testing inspect: " print
arr inspect println # prints: [1, 2, 3, 4, 5, 6] : Array
12 changes: 6 additions & 6 deletions examples/blocks.fnc
@@ -1,12 +1,12 @@
x = { Console println: "Println from within block!" };
x call; # calls x and prints: "Println from within block!"
x = { Console println: "Println from within block!" }
x call # calls x and prints: "Println from within block!"

y = |x y| { Console println: $ x + y };
y call: [2, 3]; # calls y and prints: 5
y = |x y| { Console println: $ x + y }
y call: [2, 3] # calls y and prints: 5

# prints numbers 0 to 20
zahl = 0;
zahl = 0
{ zahl <= 20 } while_true: {
Console println: zahl;
Console println: zahl
zahl = zahl + 1
}
16 changes: 8 additions & 8 deletions examples/boolean.fnc
@@ -1,20 +1,20 @@
# some boolean expressions & logicial operations

true and: true . println; # true
true and: nil . println; # nil
nil and: nil . println; # nil
true and: true . println # true
true and: nil . println # nil
nil and: nil . println # nil

true or: true . println; # true
true or: nil . println; # true
nil or: nil . println; # nil
true or: true . println # true
true or: nil . println # true
nil or: nil . println # nil


"--------------" println;
"--------------" println

# won't print string
nil if_true: {
"this should _not_ be displayed" println
};
}

# will print string
nil if_false: {
Expand Down
48 changes: 24 additions & 24 deletions examples/class.fnc
@@ -1,64 +1,64 @@
def class Bar {
def initialize {
Console println: "In Bar constructor!"
};
}

def say_hello: name {
Console print: "Hello, ";
Console print: "Hello, "
Console println: name
}
};
}

def class Foo : Bar {
def initialize: name {
Console println: "gonna set @name";
Console println: "gonna set @name"
@name = name
};
}
def say_hello {
Console print: "Hello, ";
Console println: @name;
Console print: "Hello, "
Console println: @name
{@block call} if: @block
};
}
def on_hello_do: block {
@block = block
}
};
}

bar = Bar new;
bar say_hello: "Chris";
bar = Bar new
bar say_hello: "Chris"

foo = Foo new: "Chris from Constructor";
foo say_hello;
foo = Foo new: "Chris from Constructor"
foo say_hello
foo on_hello_do: {
Console println: "Call me when calling on_hello! :)"
};
foo say_hello;
}
foo say_hello

foo class println; # print the class of foo
foo class println # print the class of foo

# define a singleton method on foo object
foo define_singleton_method: "foo!" with: {
"In foo method :D" println
};
}

foo foo!;
foo foo!

# define a 'normal' method on Foo class
# (instance method for all instances of Foo)
foo class define_method: "foo_for_all:" with: |x| {
"In foo_for_all method (defined for all instances of Foo class)" println;
"In foo_for_all method (defined for all instances of Foo class)" println
"Got argument: " ++ x println
};
}

foo2 = Foo new;
foo2 foo_for_all: "hello, test! :)";
foo foo_for_all: "hello, test (again)! :)";
foo2 = Foo new
foo2 foo_for_all: "hello, test! :)"
foo foo_for_all: "hello, test (again)! :)"

# define a class method on Foo class
# it's the same as calling 'define_singleton_method:with:' on class
foo class define_class_method: "cool_class_method:" with: |arg| {
"In class method for Foo! Argument given: " ++ arg println
};
}

# the following is the same as:
# foo class cool_class_method: "Some argument string"
Expand Down
10 changes: 5 additions & 5 deletions examples/closures.fnc
Expand Up @@ -2,19 +2,19 @@
def create_counter: number {
closure = {
number = number + 1
};
}
closure
};
}

# create a counter from 100 upwards
closure = self create_counter: 100;
closure = self create_counter: 100
# this will print numbers 100 - 120
20 times: {
Console println: $ closure call
};
}

# create a counter from 500 upwards
closure = self create_counter: 500;
closure = self create_counter: 500
# this will print numbers 500 - 510
10 times: {
Console println: $ closure call
Expand Down
2 changes: 1 addition & 1 deletion examples/factorial.fnc
Expand Up @@ -2,7 +2,7 @@ def class Number {
def factorial {
1 upto: self . product
}
};
}

1 upto: 10 do_each: |i| {
i to_s ++ "! = " ++ (i factorial) println
Expand Down
2 changes: 1 addition & 1 deletion examples/fibonacci.fnc
Expand Up @@ -10,7 +10,7 @@ def class Number {
}
}
}
};
}

15 times: |x| {
x fib println
Expand Down
12 changes: 6 additions & 6 deletions examples/files.fnc
@@ -1,20 +1,20 @@
{
Directory create: "tmp/"
} unless: $ Directory exists?: "tmp/";
} unless: $ Directory exists?: "tmp/"

File open: "tmp/Hello-World.txt" modes: ['write] with: |f| {
f write: "Hello, world" . newline
};
}

File delete: "tmp/Hello-World.txt";
File delete: "tmp/Hello-World.txt"

arr = [1,2,3,4];
arr = [1,2,3,4]
File open: "tmp/Array-Test.txt" modes: ['write] with: |f| {
arr each: |x| {
f writeln: x
}
};
}

File delete: "tmp/Array-Test.txt";
File delete: "tmp/Array-Test.txt"

Directory delete: "tmp/"

0 comments on commit 0e81073

Please sign in to comment.