|
0. Syntax And Functionality | Syntax of Yjasl | |
1. Variables And Arrays | Syntax of variables and arrays | |
2. Operators | List of all operators | |
3. Conditions And Loops | Syntax of loops and IF |
|
4. Functions And Subs | Syntax of function and sub declaration | |
5. Reserved Words | List of reserved words and brief explanation | |
6. Standard Library Functions | List of all standard library functions and subs | |
7. YjaslRun.exe | Usage of YjaslRun.exe | |
8. YjaslGRE.exe | Usage of YjaslGRE.exe |
0.0 Rems |
0.1 The Semicolon |
0.2 Variables |
0.3 Expressions |
0.4 Global And Local Variables |
0.5 Namespaces |
0.6 Limitations, Maximum And Minimum Values |
Yjasl accepts C style remarks.
// This is a one line rem
/* This
is more than
one line ! */
Yjasl accepts C use of semicolons, but doesn't need them except in the case of function prototypes. I added the semicolon just to make some parts of my sources more readable (and because it's quite confusing switching from C# with semicolons to Yjasl without semicolons ... this solution produces less 'syntax error' exceptions. *g*)
// The next line looks nicer with semicolons also they're not needed
fnum = 6; x_min = -0.274188704403; x_max = -0.274188253684; y_min = 0.841196557584; y_max = 0.841196897085
sub proto_sub(a, b, c); // Sub prototype
function proto_func(a, b, c); // Function prototype
Variables do not have to be defined with a type, like in C. The type of a variable is determined by its last assign.
var = 3.1415926536 // var is a number
var = "Hello world!" // var is now a string
var = {.hello = "Hello", .world = "world!"} // var is now an array
var = sin // var is now a function
Expressions allways evaluate to a value. If you use logical operators, the expression will evaluate to
true
or false
.
bool = 0 == 1
a = 10
b = a * 5 + 2
term = pow(a, 2) + 2 * a * b + pow(b, 2)
term2 = (a + (b + (a * b) * 100) / 10) - 3.33333333
In functions or subs all variables are assigned as follows:
var = 10
1. Check if local variable 'var' exists, if so: local 'var' = 10 -> done
2. Check if global variable 'var' exists, if so: global 'var' = 10 -> done
3. Create local variable 'var', local 'var' = 10 -> done
Remark: Local variables are faster than global variables.
A namespace holds functions, subs and globals.
Example:
namespace test
function testfunc(a)
return a * 2
end
_::println("Hello from namespace 'test'!") // _ is the standard namespace
var = 100
end
println(test::testfunc(10))
println(test::var)
test::var = 100
test::new_var = 2000 // will define a new variable 'new_var' in namespace 'test'
The above could also be done like this:
function test::testfunc(a)
return a * 2
end
println("No other namespace!")
test::var = 100
println(test::testfunc(10))
println(test::var)
test::var = 100
test::new_var = 2000 // will define a new variable 'new_var' in namespace 'test'
|
Variable names may start with _
or a letter, may contain letters, digits and _
and
are case-insensitive.
Example:
x = 10
str = "Hello world!"
You can assign strings, numbers, arrays and functions to variables. The type of a variable is specified
by its content. You can use typeof()
to get the type of a variable.
(See Variables Library)
Yjasl does only have two variable scope types: global and local. All variables assigned in main code are global variables and can be accessed by all subs and functions. Variables declared in functions or sub are only available in this function or sub.
Yjasl's arrays are very flexible. Instead of explaining them, I'll just give some examples:
// Example #0
arr = {}
arr[0] = "Test"
arr[10] = "Unordered!"
arr[2] = 10
// Example #1
arr = {1, 2, "three"}
println(arr[0].." + "..arr[1].." = "..arr[2])
// Example #2
arr = {{0, 1, 2, 3}, {4, 5, 6, 7}}
println(arr[0][2]..", "..arr[1][3])
// Example #3
arr = {.name = "Foo", .age = 99}
println(arr.name.." is "..arr.age.." years old")
// Example #4
arr = {{.a = 10, .b = 20}, {.a = 30, .b = 40}}
println(arr[0].a)
// Example #5
arr = array(1000, 0) // creates an ordered array with size 1000, filled with zeros
// Example #6
arr = {}
arr.test = "Hello"
arr.worx = " world!"
println(arr.test..arr.worx)
// Example #7
arr = {sin, cos, timetick}
temp = arr[0]
println(temp(pi / 3))
println(arr[2]())
// Example #8
arr = {}
arr[] = 10 // arr[0] = 10
arr[] = 12 // arr[1] = 12
arr[] = pi // arr[2] = pi
// Example #9
function garr(a);
function func(a);
arr = {}
arr.println = findsub("println", 1)
arr.garr = findfunction("garr", 1)
arr.func = findfunction("func", 1)
arr.println("Hello world!")
arr.garr(findsub("println", 1)).ret("Foo")
arr.garr(arr.func(100))[0]("Bar")
function garr(a)
local arr = {}
arr[0] = a
arr.ret = a
return arr
end
function func(a)
return findsub("println", 1)
end
Remarks: Whenever possible, use array()
to initialize arrays. Due to runtime table
access performance, resizing gets very slow.
arr0 = array(65536)
arr1 = {}
t0 = timetick()
for x = 0, 65535 do
arr0[x] = x
end
t1 = timetick()
println((t1 - t0).." ms")
t0 = timetick()
for x = 0, 65535 do
arr1[x] = x
end
t1 = timetick()
println((t1 - t0).." ms")
/* on my AMD Athlon 1000MHz, 512MB RAM, WinXPpro this outputs:
130 ms
5999 ms
*/
Remarks: If your array uses number indexes only, make sure you add values in order. You can
also use array()
as seen in Example #5. This ensures that your array is an ordered
array and will give a great advantage in performance. (See Example #0 for an unordered array.)
Remarks: Arrays are references:
arr0 = {1, 2, 3, 4}
arr1 = arr0
arr1[2] = 10
println(arr0[2]) // will output 10
If you want a copy of an array, use arrclone()
. (See Variables Library)
Here is a list of all available operators and what they'll do:
|
3.0 IF Blocks |
3.1 FOR Blocks |
3.2 FOREACH Blocks |
3.3 DO ... UNTIL Blocks |
3.4 WHILE Blocks |
3.5 CFOR Blocks |
3.6 SWITCH Blocks |
IF
blocks contain at least one IF
clause, zero or more ELSEIF
clauses
and zero or one ELSE
clause:
if cond then
...
[elseif cond then]
...
[elseif cond then]
...
[else]
...
end
cond
is a term which evaluates to true
or false
.
true
is defined as -1.0, but may be any value except 0.0.
false
is defined as 0.0.
for var = start, end[, step] do
...
[continue]
...
[break]
...
end
var
is a variable name.
start
is an expression which sets the initial value of var
.
end
is an expression which sets the destination value.
step
is optional (default is 1.0), and (when given) must be a number, no expression.
continue
= forced continue of the loop (following code gets skipped)
break
= exits the loop.
Examples:
for i = 0, 9 do
println(i) // prints numbers 0 ... 9
end
for i = 9, 0, -1 do
println(i) // prints numbers 9 ... 0
end
Remark: The 'end' expression is evaluated only once and its value gets stored for later compares, so
4 - 10 + 100
is as fast as 94
during loop execution.
foreach var in array do
...
[continue]
...
[break]
...
end
This iterates through all elements in table 'array'.
var
is a variable name.
array
is an array.
Example:
arr = {.hello = "Help!", .world = 100, .foo = "lalala", .bar = sin}
foreach var in arr do
println("arr."..getindex(var).." = "..var)
end
/* This will print:
arr.hello = Help!
arr.world = 100
arr.foo = lalala
arr.bar = FUNCTION[...]
*/
do
...
[continue]
...
[break]
...
until cond
Executes the loop until cond
evaluates to true
.
while cond do
...
[continue]
...
[break]
...
end
Executes the loop while cond
evaluates to true
.
The cfor
block is an implementation of the C-style for loops.
cfor (init; cond; change) do
...
[continue]
...
[break]
...
end
init
is one or more optional initial assign(s) or function call(s)
cond
is an optional single condition
change
is one or more optional change assign(s) or function call(s)
Examples:
cfor (x = 0; x < 10; x += 1) do
println(x)
end
cfor (x = 0, y = 0; x < 10 && y < 12; x += 1, y += 2) do
println(x..", "..y)
end
// The above could also be written like this:
x = 0
y = 0
while x < 10 && y < 12 do
println(x..", "..y)
x += 1
y += 2
end
cfor (;;) do // This is the fastest endless loop in Yjasl
println("endless ")
end
switch exp do
[case exp2:]
...
[break]
...
[default:]
...
end
Remark: The 'exp' expression is evaluated only once and its value gets stored for later compares.
Function/sub names may start with _
or a letter, may contain letters, digits and _
and
are case-insensitive.
The only difference between funcions and sub is that functions allways return a value and subs allways don't.
Functions and subs need to be defined before their first use. You may use protypes to define the them.
function name([parameter-list]); // Prototype definition
function name([parameter-list])
...
return value
end
sub name([parameter-list])
...
[exit]
...
end
Parameter-list may be empty or contain names. The values passed to a function/sub are stored in local variables.
You do not need to place the return
statement at the end of the function, but make sure
you return a value somewhere in your function. The Yjasl VM checks the stack after a function call and
if neccessary just pushes a NULL onto the stack when a return value is missing.
function test(a, b)
c = a + b
end
println(test(1, 2)) // will output NULL
Here are some more examples to give you an impression of how functions work:
sub add_print(p0, p1)
println(p0.." + "..p1.." = "..(p0 + p1))
end
sub add_print(p0, p1, p2)
println(p0.." + "..p1.." + "..p2.." = "..(p0 + p1 + p2))
end
function my_add(a, b)
return a + b
end
add_print(1, 2)
add_print(1, 2, 3)
println(my_add(2, 5))
arr = {my_add}
println(arr[0](2, 5))
Remark: Direct assigning of an overloaded function/sub to a variable doesn't work correctly.
Use the return value of findfunction()
and findsub()
(See Variables Library) instead.
Remark: If you call a function/sub by a variable, Yjasl doesn't check for parameter count
or return values (except that functions still allways return a value). So make sure you call your
functions/sub in the right way or stack underruns or overflows may occur. callsub()
and
callfunc()
(See Jasl Library) do these calls in a safe way
by throwing exceptions when parameters don't match.
Remark: You can use the keyword override
to replace an existing function or sub by another
(See Reserved Words for an example).
|
6.0 Yjasl Library |
6.1 Variables Library |
6.2 String Library |
6.3 System Library |
6.4 IO Library |
6.5 Timer Library |
6.6 Math Library |
6.7 Console Library |
6.8 GZip Library |
6.9 Socket Library |
|
|
|
|
|
|
|
|
|
|
© by René 'Neotec' Jeschke in 2006, 2007 Last updated: 14 JUN 2007 (under construction) |
|