-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.fjs
More file actions
142 lines (109 loc) · 5.64 KB
/
test.fjs
File metadata and controls
142 lines (109 loc) · 5.64 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// -------- Examples from the docs --------
. "\nFrom the *Hello World* section" drop<
. 'Hello World'
. "\nFrom the *Comments* section" drop<
. + 1 1 // prints "2" on stdout
. * 1 1 // prints "1" on stdout
. "\nFrom the *Named Variables* section" drop<
x= 3 // assign 3 to variable x
. x // prints "3"
. "\nFrom the *Functions* section" drop<
( . "hello world" ) // creates and runs function, prints "hello world"
printGreeting= :( . "hello world" ) // creates function and assigns it to a variable
printGreeting // runs function, prints "hello world"
. "\nFrom the *Word Modifers* section" drop<
. x x= 1 // prints "1"
. :x x= 1 // prints "x"
. Math.min // prints "Infinity"
. :Math.min // prints "[Function: min]"
. ( + 1 2 ) // prints "3"
. :( + 1 2 ) // prints "[Function]"
. "\nFrom the *Word Modifers* section" drop<
. Math.max 1 2 3 // prints "3" (Math.max(1, 3, 2)) Leaves empty stack
. max. Math 1 2 3 // prints "3" (Math.max(1, 3, 2)) Leaves empty stack
. "\nFrom the *Word Modifers* section" drop<
. 1 2 3 // pops one item and prints "1", leaves two on stack
.< 1 2 3 drop< // empties stack and prints "1 2 3"
.<2 1 2 3 // pops two items and prints "1 2"
. Math.max 1 2 3 // empties stack and prints "3"
. Math.max<2 1 2 3 // pops two items and prints "2"
. "\nFrom the *Using Arguments* section" drop<
( . @ . @ . @ ) 1 2 3 // prints "1", then "2", then "3" and leaves stack empty
( . Math.max @2 ) 1 2 3 // prints "2" and leaves one item on stack
( .< @@ 4 )<2 1 2 3 // prints "1 2 4" and leaves one item on stack
. "\nFrom the *Function Results* section" drop<
.< ( 1 2 3 ) // prints "1 2 3"
.< ( + 1 2 3 ) // prints "3 3"
.< + ( + 1 2 3 ) // prints "6"
. "\nFrom the *Function Results* section" drop<
. + - 5 3 + 2 4 // prints "8"
. + ( - 5 3 ) ( + 2 4 ) // same but much more readable.
. "\nFrom the *Function Results* section" drop<
. Math.min // prints Infinity
.< ( split. "1 2 3" ' ' ) // prints "1 2 3"
. "\nFrom the *Named Variable Scopes* section" drop<
x= 1 // assign 1 to x
( y= 2 ) // anonymous function executed with var y in local scope
. typeof:y // prints "undefined"
( x= 3 ) // var x is from outer scope
. x // prints 3, not 1
. "\nFrom the *Async Execution* section" drop<
fs= require 'fs' // load the fs module and assing it to the fs variable.
.< wait ( . 'hello world' ) fs.readFile 'fjs.bat' 'utf8' cb
// prints "hello world" and then the error code and file contents
. "\nFrom the *Comparisons and Boolean Operators* section" drop<
. < 1 2 // prints "true"
. > 1 2 // prints "false"
. = 3 + 1 2 // prints "true"
. "\nFrom the *Comparisons and Boolean Operators* section" drop<
. not true // prints "false"
. and true false // prints "false'
. or < x 3 > x 5 x= 6 // prints "true'
. or ( < x 3 ) ( > x 5 ) x= 6 // same as last line but easier to read
. "\nFrom the *Special Operator Prefixes* section" drop<
obj= `{z:1}` // assign object to var obj
( . z with:obj ) // prints 1 (`with` goes to end of function
. typeof:z // prints "undefined"
. "\nFrom the *Conditionals and Looping* section" drop<
if ( = x 1 ) :( console.log 'x is 1' ) x= 1 // prints "x is 1"
if = x 1 :prt1 x= 1 prt1= :( . 'x is 1' ) // prints "x is 1"
. y if < x 3 :( y= 2 ) y= 1 x= 1 // prints "2"
. y doif :( y= 2 ) < x 3 y= 1 x= 1 // prints "2"
. "\nFrom the *Conditionals and Looping* section" drop<
while :( < x 3 ) :( x= + x 1 . x ) x= 0 // prints "0", "1" and "2"
. "\nFrom the *Conditionals and Looping* section" drop<
repeat :( < x 3 x= + x 1 . x ) x= 0 // prints "0", "1" and "2"
. "\nFrom the *Arrays and Objects* section" drop<
. [] // prints "[]"
. []< 1 2 // prints "[ 1, 2 ]"
. {}< :a 1 :b 2 // prints "{ a: 1, b: 2 }"
. "\nFrom the *Arrays and Objects* section" drop<
. x. `{ x: 'one', y: 'two' }` // prints "one", js: console.log({x:'one', y:'two'}.x)
. get `{ x: 'one', y: 'two' }` 'x' // prints "one", js: console.log({x:'one', y:'two'}['x'])
. get `[ 'zero', 'one' ]` 1 // prints "one", js: console.log(['zero', 'one'][1])
. "\nFrom the *Arrays and Objects* section" drop<
. x set x 0 'a' x= [] // prints "[ "a" ]", js: x=[]; x[0] = 'a'; console.log(x)
. x set x 'a' 1 x= {} // prints "{ a: 1 }", js: x={}; x['a'] = 1 ; console.log(x)
. "\nFrom the *Arrays and Objects* section" drop<
. pop. `[1, 2, 3]` // prints "3"
. x push. x 4 x= `[1, 2, 3]` // prints "[ 1, 2, 3, 4 ]"
. "\nFrom the *Arrays and Objects* section" drop<
. map :( * dup @ ) `[1,2,5]` // prints [1, 4, 25]
. "\nFrom the *Arrays and Objects* section" drop<
each :( . @ ) `[1, 2]` // prints "1" then "2"
. "\nFrom the *Embedding Javascript Code* section" drop<
. `x = 1 + 1` // assigns 2 to x and prints "2"
obj= `{a:1, b:2}` // assigns a constant object to the variable "obj"
. "\nFrom the *Http Server* section" drop<
// -------- Standard Javascript http server example from Node --------
// var http = require('http');
// http.createServer(function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Hello World\n');
// }).listen(1337, '127.0.0.1');
// console.log('Server running at http://127.0.0.1:1337/');
createServer. ( require 'http' ) cb
drop ( listen. @ 1337 '127.0.0.1' )
dup drop wait . 'Server running at http://127.0.0.1:1337/'
( writeHead. @ 200 `{'Content-Type':'text/plain'}` )
( end. @ 'Hello World\n' )