September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,2 +0,0 @@
int var;
int* ptr = &var;

View file

@ -1,9 +0,0 @@
struct S{}
class C{}
void foo(S s){} // pass by value
void foo(C c){} // pass by reference
void foo(int i){} // pass by value
void foo(int[4] i){} // pass by value
void foo(int[] i){} // pass by reference
void foo(ref T t){} // pass by reference regardless of what type T really is

View file

@ -0,0 +1,39 @@
// Kotlin Native v0.3
import kotlinx.cinterop.*
fun main(args: Array<String>) {
// allocate space for an 'int' on the native heap and wrap a pointer to it in an IntVar object
val intVar: IntVar = nativeHeap.alloc<IntVar>()
intVar.value = 3 // set its value
println(intVar.value) // print it
println(intVar.ptr) // corresponding CPointer object
println(intVar.rawPtr) // the actual address wrapped by the CPointer
// change the value and print that
intVar.value = 333
println()
println(intVar.value)
println(intVar.ptr) // same as before, of course
// implicitly convert to an opaque pointer which is the supertype of all pointer types
val op: COpaquePointer = intVar.ptr
// cast opaque pointer to a pointer to ByteVar
println()
var bytePtr: CPointer<ByteVar> = op.reinterpret<ByteVar>()
println(bytePtr.pointed.value) // value of first byte i.e. 333 - 256 = 77 on Linux
bytePtr = (bytePtr + 1)!! // increment pointer
println(bytePtr.pointed.value) // value of second byte i.e. 1 on Linux
println(bytePtr) // one byte more than before
bytePtr = (bytePtr + (-1))!! // decrement pointer
println(bytePtr) // back to original value
nativeHeap.free(intVar) // free native memory
// allocate space for an array of 3 'int's on the native heap
println()
var intArray: CPointer<IntVar> = nativeHeap.allocArray<IntVar>(3)
for (i in 0..2) intArray[i] = i // set them
println(intArray[2]) // print the last element
nativeHeap.free(intArray) // free native memory
}

View file

@ -0,0 +1,4 @@
local table1 = {1,2,3}
local table2 = table1
table2[3] = 4
print(unpack(table1))

View file

@ -0,0 +1,11 @@
::class Foo
::method init
expose x
x = 0
::attribute x
::routine somefunction
a = .Foo~new -- assigns a to point to a new Foo object
b = a -- b and a now point to the same object
a~x = 5 -- modifies the X variable inside the object pointer to by a
say b~x -- displays "5" because b points to the same object as a

View file

@ -0,0 +1,20 @@
a.='not set'
a.3=3
Say 'Before Call sub: a.3='a.3
Call sub a.
Say ' After Call sub: a.3='a.3
Call sub2 a.
Say ' After Call sub2: a.3='a.3
Exit
sub: Procedure
Use Arg a. -- this established access to the caller's object
a.3=27
Return
sub2: Procedure
Parse Arg a. -- this gets the value of the caller's object
a.3=9
Say 'in sub2: a.='a.
Say 'in sub2: a.3='a.3 -- this changes the local a.
Return

View file

@ -0,0 +1,22 @@
dcl i fixed bin(31);
dcl p pointer;
dcl j fixed bin(31) based;
i=5;
p=addr(i);
p->j=p->j+1; /an other way to say i=i+1 */
put skip edit(i)(F(5)); /* -> 6 */
/* second form */
dcl i fixed bin(31);
dcl j fixed bin(31) based(p);
i=5;
p=addr(i);
j=j+1; /* an other way to say i=i+1 */
put skip edit(i)(F(5)); /* -> 6 */
/* cascading pointers */
dcl (p,q,s,t) pointer;
dcl (j,k) fixed bin(31) based;
dcl (i1,i2) fixed bin(31);
p=addr(i1); t=addr(i2), q=addr(p); s=addr(t);
q->p->j = s->t->k + 3; /* to say i1=i2+3 */

View file

@ -0,0 +1,5 @@
atom addr = allocate(8) -- (assumes 32 bit)
poke4(addr,{NULL,SOME_CONSTANT})
c_proc(xSome_External_Routine,{addr,addr+4})
?peek4s({addr,2}) -- prints {x,y}
free(addr)

View file

@ -0,0 +1,9 @@
atom mypi
#ilASM{
fldpi
[32]
lea edi,[mypi]
[64]
lea rdi,[mypi]
[]
call :%pStoreFlt }

View file

@ -0,0 +1,10 @@
string mystring = "mystring"
#ilASM{
[32]
mov esi,[mystring]
lea esi,[ebx+esi*4] -- byte[esi] is 'm'
[64]
mov rsi,[mystring]
lea rsi,[rbx+rsi*4] -- byte[rsi] is 'm'
[]
}

View file

@ -0,0 +1,2 @@
sequence s
s = myfunc(s)

View file

@ -0,0 +1 @@
fcn f(r){r.inc()} r:= Ref(1); f(r); r.value; //-->2

View file

@ -0,0 +1 @@
fcn f(lst){lst.append(5)} f(L()); //-->L(5)