Παρασκευή 29 Μαρτίου 2019

Revision 14 Version 9.8

Some bug fixes.
1) Fix for passing array items by reference. Before works if we make a call from a module. The problem was the call from a function.
This work on previous revision 13
module b {
 dim a(10)=-1
 inc=lambda ->{
  stack
  read &x
  x++
 }
 call inc(&a(3))
 Print a(3)
}
call b
b

but not this (work nice in revision 14):
function b {
 dim a(10)=-5
 inc=lambda ->{
  stack
  read &x
  ? x
  x++
 }
 call inc(&a(3))
 Print a(3)
}
call b()
n=b()
Also the above programs show the stack (as we call) before the reading (and linking to a variable). An Array item passed by reference with a copy in copy out method. First a temporary variable hold a copy of array item, then the reference of that variable send to stack, so the read &x make x as a link to temporary variable (so x has no own data, it is an alias of the temporary variable). Also for this call interpreter save to a special collection the data which need to make the copy out at the end of the call.
The stack statement, inside function, show the strings (as references) of the temporary variables which interpreter produce. If array for some reason lost the item which we have to copy out, no error produced, the interpreter just do nothing for store back the value from temporary variable.

global z=2
function k (&n, &x) {
 x++
 n++
 Print x
}
dim a(3,3)
a(z,1)=100
call k(&z, &a(z,1))
Print a(2,1)=101, z=3
z<=2   ' need <= to assign a value to a global, otherwise we get a local one
\\ change the k (also change the signature too)
function k  {
 z++
 read &x
 x++
 Print x
}
\\ indexes calculated before the call
call k(&a(z,1))
Print a(2,1)=102, z=3


2) Another fix is in Each() function which return an iterator object. There are two forms, one using comma and another without, using a descriptive text.

Here is the program which make for Rosettacode.org (Four bit adder), which found the errors, and immediately fix them.


ha means half adder, fa is a full adder. We simulate bits using just values 0 and 1 (these are doubles, the default type for numbers). For this reason we don't use And operator or Binary.And() for binary And, and the other gates, but we have three arrays, as functions, for not(), and(), or(). Also task want to have a function for xor, so xor isn't an array.
Then we make lambda with closures whatever need each one. Lambda add4 need fa as a closure.
The add4 may work from one or more bits (so name is not exactly a match for this implementation)
We have a lambda called source which get a number of values, in a private stack, then reverse the order and return an array of these values. We do that to send the left most value as the last item of array. The output just written in the res()  array, which we pass by reference (this is not a copy in-copy out process, but a simple alias, the two names. res() and out() are local on different scopes, on same data). So to print the result first we have to print the carry, and next the top value res(3) until the res(0). So we make k as an iterator (which hold a pointer to array res(), plus other data, and the cursor, which we can access using k^), which work in a While {} or in a Print statement. Here in a print statement we get all values each on one column, and may wrap to next line if no column left in line.

\\ when we use Call we pass the same stack in the call, so we need to clear the stack before
\\ or we can use a block for a new stack: Stack New  {}
\\ when the block exit the old stack return as current stack.
\\ so here Flush empty the current stack
Flush
dim not(0 to 1),and(0 to 1, 0 to 1),or(0 to 1, 0 to 1)
not(0)=1,0
and(0,0)=0,0,0,1
or(0,0)=0,1,1,1
xor=lambda not(),and(),or() (a,b)->or(and(a,not(b)), and(b, not(a)))
ha=lambda xor, and() (a,b, &s, &c)->{
 s=xor(a,b)
 c=and(a,b)
}
fa=lambda ha, or() (a,b,c0, &s,&c1)->{
 def sa,ca,cb
 call ha(a,c0,&sa,&ca)
 call ha(sa, b, &s,&cb)
 c1=or(ca,cb)
}
add4=lambda fa (inpA(),inpB(),&v, &out()) ->{
 dim carry(0 to len(InpA()))=0
 for i=0 to len(InpA())-1
  mm=fa(InpA(i),inpB(i),carry(i),&out(i),&carry(i+1))
 next
 v=carry(i)
}
dim res(0 to 3)=-1
source=lambda->{
 shift 1, -stack.size  ' reverse stack items
 =array([])  ' convert current stack to array, empty current stack
}
def v
call add4(source(1,0,1,0),source(1,0,0,1),&v, &res())
'k=each(res(), -1, 1) ' another way to do the same as the later
k=each(res() end to start)  ' k is an iterator, now configure to read items in reverse
Print v, k

3) Fix at EditBox when it is in a M2000 mode (for displaying M2000 code), for the F2 and F3 when we mark an identifier (any type), works nice (before a flag make the F2 and F3  to work for words only). This problem not exist if we pass a list of identifiers for syntax color.

4) Added Hex statement in Help File.(there is a Hex$() but for no reason I never make a record for Hex statement, a special Print for results in Hexadecimal)








Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.