|
Post by johnno56 on Nov 12, 2021 18:32:51 GMT -6
Aurel,
I know, I know, I'm not Ed...
I have never used ReDim() before, heard about it, but never used it.
Correct me if I am wrong. Does a ReDim reset the array arguments to zero? Why do you think some types of Basic do not use it?
|
|
|
Post by eddavis2 on Nov 13, 2021 9:58:51 GMT -6
Ed I still cannot figure where you alocate space for array ? it looks to me that is created "on the fly " here : index = num_arr_stor_max + 1 num_arr_stor_max = num_arr_stor_max + a_len redim _preserve numeric_arr_store(num_arr_stor_max)
do i have right ? so you use ReDim to do that ? I am asking because i want avoid that in microA anyway o2 dont have redim ..which is not quite safe way ..i think ? Arrays are created when they are dim'ed. And yes, you found the right place - it is in a routine called dimstmt, which is only called when the code encounters the dim keyword. All numeric arrays are stored in a single master array (numeric_arr_store), with another array (var_names) holding the array name, size, index into the master array, number of dimensions, and other bookkeeping information. Strings arrays mimic the numeric arrays, except of course they need have their own master array (string_arr_store). The code figures out the user requested dimensions, and then redim's the master array as appropriate. Each new array found is appended to the appropriate existing master array. Note that the code uses redim preserve, so no information is lost. In C, one would just use realloc(), and each array could have its own memory space - which gives one much more control, but of course it takes more code and effort to get correct. I'm not sure what isn't safe about redim - it seems to work fine for me. If you are not going to use redim, are you going to pre-allocate space for all arrays? I guess I could have done that, but it would have required at least a single first pass to implement (e.g., to find all the array declarations, and then allocate the needed space), and I didn't want to do that. redim works fine for allocating the arrays as they are discovered, so I went with that. As an aside, I can no longer use O2 at all. On my company provided laptop (I can't afford a second one), the multiple required virus detectors immediately delete O2 as soon as I try to unzip it. As a second aside, I'll leave you with very tiny numeric expression parser. Just add a routine called nextsym() - that returns the next symbol from the input, and away you go! It handles plus/minus/multiplication/division/exponentiation, unary plus and minus, parenthesis, and also uses standard order of precedence. Exponentiation is left to right (just like QBASIC), but can be changed by simply not inc'ing prec. And it is trivial to add new binary operators, variables, and calls to functions (e.g., sin()). As a third aside, I tried converting the below code to RCBasic, but I did not get very far. Anyone care to help? I can supply the complete code (another 50 lines) if you are willing to do so. Anyway, here it is: function expr(p as integer) as double dim n as double, n2 as double, op as string, prec as integer
' handle numeric operands - numbers and unary operators if sym = "-" then nextsym: n = -expr(3) ' precedence between power and multiplication elseif sym = "+" then nextsym: n = expr(3) ' same as unary minus elseif sym = "(" then nextsym: n = expr(0) if sym = ")" then nextsym else print "expecting ')'" elseif isnumeric(sym) then n = val(sym): nextsym else print "syntax error: expecting an operand, found: ", sym end if
' binary operators do ' while binary operator and precedence of sym >= p select case sym case "+", "-" : prec = 1 case "*", "/" : prec = 2 case "^" : prec = 4 case else : prec = 0 ' not a binary operator end select
if prec = 0 or prec < p then exit do
op = sym: nextsym: prec = prec + 1 ' if right associative, don't increment
n2 = expr(prec) ' recursive call select case op case "+" : n = n + n2 case "-" : n = n - n2 case "*" : n = n * n2 case "/" : n = n / n2 case "^" : n = n ^ n2 case else: print "syntax error: expecting a binary operator, found: ", sym end select loop
expr = n end function
|
|