1. questions about Eu and C

Hello
I've some questions about Eu and C/C++, so there is :
in C, you can define 2 functions like that :

void f1();
void f2();

void f1()
{ ...
  f2();
  ...
}

void f2()
{ ...
  f1();
  ...
}

--> f1 can call f2 and vice-versa. Is there a way to do that in Euphoria ??

Another things : in Eu we have a type called sequence, and we can change
the length of a sequence just by adding an element
by example. But in C, vectors have fixed length, I just don't know how to
do a thing like that in C :

sequence s
s={10, 2, 7, 9}

s=s[1..2] & s[4] -- s={10, 2, 9}
s=append(s, 5)   -- s={10, 2, 9, 5}
s=append(s, 8)   -- s={10, 2, 9, 5, 8}

Do you have any idea ? Maybe in c++ (I know nothings about OOP)  ?

Gwen

new topic     » topic index » view message » categorize

2. Re: questions about Eu and C

Hi Gwen,

Just quickly:

To allow routines to call each other look at routine_id(), call_func() and
call_proc().

The doco for call_proc() has an example exactly the same as what you
requested.

One way to emulate sequences in C would be to use linked lists, the standard
(simple)
linked list will only use one data type and you'll have to write your own
functions
for length, append, etc etc.

Ray Smith

----- Original Message -----
From: Feta <mb11363 at CHELLO.BE>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Wednesday, November 08, 2000 5:40 AM
Subject: questions about Eu and C


> Hello
> I've some questions about Eu and C/C++, so there is :
> in C, you can define 2 functions like that :
>
> void f1();
> void f2();
>
> void f1()
> { ...
>   f2();
>   ...
> }
>
> void f2()
> { ...
>   f1();
>   ...
> }
>
> --> f1 can call f2 and vice-versa. Is there a way to do that in Euphoria
??
>
> Another things : in Eu we have a type called sequence, and we can change
> the length of a sequence just by adding an element
> by example. But in C, vectors have fixed length, I just don't know how to
> do a thing like that in C :
>
> sequence s
> s={10, 2, 7, 9}
>
> s=s[1..2] & s[4] -- s={10, 2, 9}
> s=append(s, 5)   -- s={10, 2, 9, 5}
> s=append(s, 8)   -- s={10, 2, 9, 5, 8}
>
> Do you have any idea ? Maybe in c++ (I know nothings about OOP)  ?
>
> Gwen

new topic     » goto parent     » topic index » view message » categorize

3. Re: questions about Eu and C

Hi, Gwen.

> But in C, vectors have fixed length, I just
> don't know how to do a thing like that in C

It would take a bit of work to emulate sequences in C. Here's the internal
format for a sequence:

   typedef int object;
   typedef int *object_ptr;

   struct s1 {
       object_ptr first;
       long length;
       long ref;
       long postfill;
   };

s1.first points to the actual data in the sequence. s1.length refers to the
length of the data in the sequence. s1.ref is the number of items
referencing the sequence. sequences are allotted with some 'extra' space;
s.postfill refers to the amount of remaining free space in the sequence.

The actual data portion of a sequence (pointed to by s1.first) is just a
malloc'ed chunk of memory - basically an array of integers. For each integer
in that array, the upper bits of the integers tells Euphoria how to
interpret the number.

Euphoria uses the high bit of to flag if a number is an integer, so it can
store integers up to 31 bits of precision. Larger values and floats are
stored as a double (atom). Since atoms can't be stored directly in sequences
(you lose the top bits), Euphoria needs to allocate a seperate structure for
atoms:

   struct d {
       double dbl;
       long ref;
   };

and then stores the pointer to the atom in the sequence (same as pointers to
sequences).

Euphoria frees up the top bits of pointers by... urg... let's just say
'magic'. blink

So much for the easy part.

As sequences grow past their limits, you need to reallocate memory, copy the
old data to the new location, change s1.first to point to the new data,
deallocate the old sequence.

You have to maintain reference counts on sequences, so you know when it's
safe to alter a sequence, or when you need to create a copy first. When a
reference count falls to zero, you can free a sequence from memory, garbage
collecting it. You need to do the same thing with atoms, since they maintain
a reference count as well.

Then you have to write support routines so that you can fetch, store, slice,
append, prepend, concatonate, compare, and handle assignments to slices.

Since you're working with C, you also need support routines to create
sequence from C strings, convert sequences into C strings, and convert
sequences to and from various C structures.

As you can imagine, it's not entirely trivial, but it can be done. If you
want to take a look at how, take a look at Pete's Peuphoria, a clone of
Euphoria.

-- David Cuny

new topic     » goto parent     » topic index » view message » categorize

4. Re: questions about Eu and C

I dont think you can allocate variable length arrays in C but in C++ the
'new' command works for that as well as other things too. The first
dimension must still be constant however.

C++ Example code:

  //dim1 and dim2 are dimensions of array
  //makes a dim1 x dim2 array of type 'char'

  char** lpArray;
  ulong jarray;
  ulong MakeArray(void)
    {
      lpArray=(char**)new ulong[dim1];
       for(jarray=0;jarray<dim1;jarray++)
           lpArray[jarray]=new char[dim2];
      return ulong(lpArray);
    }


  //cleanup:

  ulong DistroyArray(void)
    {
      for(jarray=0;jarray<dim1;jarray++)
          delete[](lpArray[jarray]);
      delete[]lpArray;
      return 1;
    }


--AL

new topic     » goto parent     » topic index » view message » categorize

5. Re: questions about Eu and C

Errata for my last post to this thread:

  I wrote:

  "The first dimension still has to be a constant"

  but really it doesnt have to be constant either:

  ulong dim1=32,dim2=64

  is fine, and allows the program to specify the dimensions on the fly.

Sorry.

--Al

new topic     » goto parent     » topic index » view message » categorize

6. Re: questions about Eu and C

Just a little a word to say thanks for your help !

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu