/Develop/Projects/Vcc/

k1.spdns.de / Develop / Projects / Vcc /

Kio's Virtual Instruction Compiler.

intro | invocation | preprocessor | types | functions | operators | instructions  

compiler | memory  

Instructions and other Keywords

Declarations and Definitions

type

all custom data types are defined starting with this keyword:

type mytype = typespec

'typespec' can be an already defined type, combined with a modifier, an array or a struct definition.

type foo = int
type foo = const int
type foo = int[]
type foo = int[7]
type foo = { int a,b; str s }

If the name is not followed by the '=' sign, then this opens the scope of this type's name and subsequently member functions can be defined:

type foo
{
int fuzzy( foo&, int ) { return 4711 }
/* ... */
}

enum

The keyword 'enum' is used to enumerate const values or to define an enumerated data type:

enum { foo1=1, foo2, foo3 }     // just enumerate, dflt. starts at value 0
enum int { foo1, foo2, foo3 } // also define the data type to use, dflt is int
enum int Foo { foo1, foo2 } // define data type Foo
enum int Foo; // declare Foo as a data type.

const

The keyword 'const' starts a constant value definition. If the data type is numeric, then the compiler handles them like plain numbers and the symbolic names can be used to define array counts for fixed-size arrays or switch cases.
If allocated types, e.g. strings are defined const, then they are created like variables only the compiler does not allow to modify them.

const int pi=3.14
const int mini=1, maxi=9
const str hallo = "Hello!"
const {
int foo1=1
float foo2=2.2
}

extern

The keyword 'extern' introduces declarations of functions, constants and variables which are probably defined in another source file.
'extern' can be use with one statement or with a block which contains multiple statements.

extern int foo(int)
extern const int limit
extern str hallo
extern{
const int z
str zstr
int fuzzy()
}

scope

create and enter or re-open an already defined scope (namespace).
If you want to re-open a scope you must be currently in the same scope this scope was defined in. Else you create another one.

float a
scope S
{
int a
}
print(a) // prints the float
print(S.a) // prints the int
scope S // reopens scope
{
int b
}
if(a)
{ // <- enter new (unnamed) scope
scope S // different!
{
str a // possible, because this S.a != outer S.a
}
}

block instruction and unnamed scope { ... }

If the opening curly brace is seen at a position where an instruction is expected, then this starts a block instruction which is also an unnamed scope.
Inside the { .. } block all names can be reused for local variables and so on.
The block instruction is also most often used with if .. elif .. else ..

Flow Control

if .. elif .. else

if condition instruction
if condition instruction else instruction
if condition instruction elif condition instruction ... else ...

The 'instruction' can be a block instruction and must not be a variable definition. Putting a declaration in here seems also very useless.
Note that there need not be brackets around the condition.

do .. while .. until .. next .. exit

do { var_definitions instructions }

Instruction 'do' starts a loop, which is infinite by default.
Inside the loop starts with definition of variables, which are only initialized once with their initial values and destroyed after exiting the loop.
The instructions include the following special loop instructions:

while condition      // resume if condition true, else exit loop.
until condition // exit loop if condition true, else resume.
next // jump to start of loop (after the var defs)
exit // exit loop (jump to loop end)

switch .. case .. default .. break

switch value
{
case const_int_value: instructions
case const_int_value: instructions break
default: instructions
}

Based on the switch value the program jumps to one of the case values, or default if present, else it resumes behind the switch block.
One case may run into the next case. To prevent this use 'break' to jump to the end of the switch block.

return

return;             // return from void function
return value // return from non-void function

end

end value          // end program and return value

the instruction 'end' may be placed at any nesting level.
The return value to a unix system is limited to range 0 .. 125.
0 indicates 'no error', any other value a problem.

Misc

assert

assert( condition )

if 'condition' is true then resume, else abort program with error message.
The assert opcode is only compiled if the macro DEBUG is defined and not 0.
This macro is most commonly set as a commandline option:

vcc -DDEBUG my_source_file

Note, that in vicci/vcc DEBUG must be defined to include the assert code, in contrast to C/C++ where NDEBUG must be defined to exclude them!

Note, that it is very stupid to put code into an assertion which has required side effects.

dealloc

dealloc( T& )

deallcoates a struct or array variable and stores NULL into it's pointer.
the variable is not properly destructed, only deallocated: that means, kill(T¢) is not called.

swap

Swaps two variables which must be of the same type:

int a,b
swap(a,b)
int[5] s
swap(s[0],s[1])

Semicolon ';'

The semicolon is a delimiter which delimits an expression (and sometimes items in lists). In most cases it is not required. Just omitting it is ok.

special cases:

create an instruction by it's own:

if condition ;           
if condition ; else ...

monadic prefix operators at the start of an instruction.
required due to syntax ambiguity:

instruction value; *p++ = 1

'*' after a value (expression) will be interpreted as operator* and therefore the instructions must be separated.
This can happen after if, until, while etc. and in assignment instructions.
The c-style use of '&' is similar but it's hard to get the '&' at the start of an instruction.
Also prefix ++ or -- are hard to write at the start of an instruction.
I was thinking of using a different character for '*' but i'm so used to '*' for this purpose...

a const definition after a type definition may be mixed up:

type u32str = uint32[];
const int cfoo = 42

Here the ';' is required, else 'const' would be part of the type.

Archive

Name Letzte Änderung Länge 
lib-Z80-Rop/ 2020-11-30 15:50 21 
lib-Z80-Vss/ 2017-11-12 09:55 452 
lib/ 2019-10-30 17:02
Libraries/ 2019-10-30 17:11
OSX-Qt40/ 2019-10-30 17:02
OSX/ 2020-09-05 12:54
Source/ 2019-10-30 17:02 11 
Tests/ 2019-10-30 17:02
LICENSE 2019-10-30 17:25 1323 
README.md 2019-10-30 17:25 288 
TODO.txt 2020-10-18 12:42 340 
macspin.gif

powered by vipsi - your friendly VIP Script Interpreter

Valid HTML Valid CSS