Menu

Language Syntax

Kalle

The tag

:::script
<h1>Outside of the code</h1>
<?v1 
    /* All code is within the v1 tags. */ 
?>
<p>All outside text will be displayed. You can define multiple <?v1 print ("V1"); ?> elements.</p>
<p>You also allowed to mix <?v1 for (i=0;i<10;i++) { print ("V1");  ?>and outside code<?v1 } ?> in loops.</p>

Operations

Arithmetic operations:

:::php
<?v1
a = 10;
b = 5.5;
c = a+b; // Addition
c = a-b; // Subtraction
c = a*b; // Multiplication
c = a/b; // Division (floating point result)
c = a%b; // Modulo
c = a**3; // Power, pow (a,3)

a++;  // Increment, a=a+1
a+=10; // Increment with offset, a=a+10
a--; // Decrement, a=a-1
a-=10; // Decrement with offset, a=a-10
++a==10;--a==10; // Increment / Decrement before comparison, a++; a==10;
a*=3; // Multiplication, a=a*3
a**=3; // Power, a=a**3
a/=3; // Division, a=a/3
?>

Comparison:

:::php
<?v1
a = 10;
a==10; // Comparison, if a is equal 10 then the expression will be true
a!=10; // Unequal, if a is not equal 10 then the expression will be true
a<10; // Lower, if a is lower 10 then the expression will be true
a>10; // Greater, if a is greater 10 then the expression will be true
a<=10; // Lower or equal, if a is lower or equal 10 then the expression will be true
a>=10; // Greater or equal, if a is greater or equal 10 then the expression will be true
?>

Comparison with strict Datatypes:

V1 has two operators for strict comparison (same as in PHP).
If you need to check if the value and the datatype are equal or unequal, use the === operator or !== operator.

See Datatypes

Logical operations:

:::php
<?v1
a = true;
b = false;
a && b; // Logical AND, if a and b are true or not 0 then the expression will be true
a || b; // Logical OR, if a or b is true or not 0 then the expression will be true
!a; // Negation, if a is false or 0 then the expression will be true
?>

Binary operations:

:::php
<?v1
a = 0x80;
b = a & 0x80; // Binary AND, b will be 0x80
b = a | 0x01; // Binary OR, b will be 0x00
b = a ^ 0x81;  // Binary XOR, b will be 0x01
b = ~a; // Binary NEGATION, b will be 0x7F
b = a >> 1; // Binary SHIFT RIGHT, a will be shifted 1 bit to right, b will be 0x40
b = a << 2; // Binary SHIFT LEFT, a will be shifted 2 bits to left, b will be 0x200
// All binary operations can be written in assignment mode like:
a&=0x80;
?>

String concatenate:

:::php
<?v1
a = "String 1";
b = "String 2";
c = a." ".b; // To concatenate two or more strings use the '.' operator. Dont use the '+' operator!
c.=" Another String"; 
?>

Kewords

The Language Syntax is derivied from C, PHP and Javascript.
That means, every Statement is finished with a semicolon ; But you can append multiple Expressions with comma within a Statement.

:::php
<?v1
// Statement with semicolon on the end
print ("Hello World");

// Multiple Expressions in one Statement separated with comma 
a = 10, b=11, c=12, print (c);

// Statements can be summarized with { } brackets.
{ a=10; b=11; c=12; }
?>

The 'if' and 'else' keyword (decisions):

:::php
<?v1
a = 10;
// Simple decision
if (a==10) 
    print ('a is 10');

// Decision with else
if (a==10) {  print ('a is 10'); } else {  print ('a is not 10'); }

// Combine multiple if and else
if (a==10)
    print ('a is 10');  
else 
if (a==11)
    print ('a is 11'); 
else 
    print ('a is not 10 and not 11'); 
?>

The '?' and ':' keyword (assignment decisions):

:::php
<?v1
a = 10;
// b will get the value 11 if a is 10, otherwise 12
b = a==10 ? 11 : 12;
?>

The 'for' keyword (loops):

:::php
<?v1
for (a=1;a<10;i++) print ("a is ".a); 
?>

The 'while' keyword (loops):

:::php
<?v1
a=10;
while (a>0) { print ("a is greater than 0"); a--; }
?>

The 'do' and 'while' keyword (loops):

:::php
<?v1
a=10;
do {
    print ("a is greater than 0");
} while (a-->0);
?>

The 'break' keyword (loops):

:::php
<?v1
a=10;
do {
    print ("a is greater than 0");
    if (a>5)
        break; // Break the current loop
} while (a-->0);
?>

The 'switch' keyword (decisions):

:::php
<?v1
a=10;
switch (a) {
    case 10:
        print ('a is 10');
        break;

    case 11:
        print ('a is 11');
        break;

    case 12:;
    case 13:
        print ('a is 12 or 13');
        break;

    default:
        print ('a is not 10, 11, 12, 13');
        break;
}
?>

The 'foreach' keyword (loop through Arrays):

Note! foreach will duplicate the array internally for thread safty, so it is not the performantest way to loop through verry big arrays.

:::php
<?v1
// Iterate a simple array (list)
a=array [10.2, true, "String", array ["Sub Array"]];
foreach (a as value) {
    print_r (value);
}

// Iterate a key/value array
a=array ("KEY"=>"VALUE", "KEY2"=>"VALUE2", "KEY3"=>"VALUE3");
foreach (a as key => value) {
    print ('Key is '.key);
    print_r (value);
}
?>

Functions

V1 is a functional language. No object orientated programming and no classes are possible.

The 'function' keyword (define Functions):

:::php
<?v1
function myFunction (param1, param2=11) {
    localVar = 10;
    print ('myFunction was called with param1=', param1, 'and param2=', param2, ', localVar=', localVar);
}
// Call function with first parameter, second paramater has default value!
myFunction (10);
?>

** Parameters as References:**

Parameters can be marked with ** &** as references to variables (like in PHP). Otherwise the variable will be duplicated into the parameter.

:::php
<?v1
globalVar = 10;
function myFunction (&param) {
   param = 11;
}
myFunction (globalVar); // globalVar is given as reference
print (globalVar); // will output 11
?>

** The 'global' keyword (Scope of Variables):**

Global variables are not visible for functions.
Functions can make global variables visible with keyword global.

:::php
<?v1
globalVar = 1;

function myFunction () {
    globalVar = 3; // Global variable will not be overwritten
}

function myFunction2 () {
    global globalVar; 
    globalVar = 2; // Global variable will be overwritten
    localVar = 3; // Local variable
}
myFunction ();
print (globalVar); // will output 1

myFunction2 ();
print (globalVar); // will output 2
?>

Constants

Constants can be defined with keyword const. Constants are visible in every scope and cannot be overwritten after definition.

:::php
<?v1
const PI  = 3.14159265359;
const DEG = PI/180.0;
const RAD = 180.0/PI;
const PI2 = 2*PI;

function myFunction () {
    print ("PI=", PI);
}

myFunction (); // Will output 3.14159265359
?>

** Special Constants **

__FILE__ = Current V1 file
__LINE__ = Current line number
_ALIGN = System default byte align (4 on 32 Bit versions, 8 on 64 Bit versions of V1)

back to Home