4tH to the rescue!

14 views
Skip to first unread message

The Beez

unread,
Mar 12, 2012, 2:58:05 PM3/12/12
to 4tH-compiler
Hi 4tH-ers!

Another 4tH story today! In my work as Configuration Managers I have a
lot to do with datasets. That's why you'll find so many spreadsheet
and CSV utilities in 4tH.

Today I met one of my Configuration Administrators who complained that
an export to another repository was not possible, because the guys
only took XML files in a certain format, so an ordinary spreadsheet
export in XML would not do.

I set down and wrote an CSV2XML converter in an hour or two. It takes
an ordinary CSV file (as you would save in Excel) and punches out the
data in XML and XSD (column names in first row). Yes, the field names
are fully qualified, no, it doesn't punch out empty tags, yes, it
takes care of quoted fields. You can even determine the delimiter
yourself and several checks are in place.

Lines spent (yes, I'm an Edsger Dijkstra groupie): about one hundred.
Code in SVN (it will work with v3.61.3).

Hans Bezemer

田明

unread,
Mar 12, 2012, 9:02:18 PM3/12/12
to 4th-co...@googlegroups.com
 Beez:

 in compileword function ,I have a problem,is the var Errline meaning ErrorLine?
can you explain it for me ,and ErrNo!
thank you !
BEST REGARDS


--
You received this message because you are subscribed to the Google Groups "4tH-compiler" group.
To post to this group, send email to 4th-co...@googlegroups.com.
To unsubscribe from this group, send email to 4th-compiler...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/4th-compiler?hl=en.


Hans Bezemer

unread,
Mar 13, 2012, 3:21:49 AM3/13/12
to 4th-co...@googlegroups.com
On Tuesday 13 March 2012, 田明 wrote:
> in compileword function ,I have a problem,is the var Errline meaning
> ErrorLine?
> can you explain it for me ,and ErrNo!
It's the program counter at runtime and the "next available word" at compile
time. From the manual:

When exiting this function ErrLine will contain the address of
the word in the Code Segment where the error occured.

The member "ErrLine" of an Hcode header always points to the
next available word in the Code Segment. That is the place where
we will compile our 'BRANCH' instruction. When the instruction
pointer is set to that location, it will be automatically
incremented and get inside the definition.

24.19 Useful variables

We've already seen that dump_4th() can provide you with a lot of
information about Hcode. If you need this information, you don't
have to call dump_4th(). The dump_4th() function simply uses the
information that is already available. This small program shows
you how to obtain it:

Hcode *Program;

Program = comp_4th (strdup (".\" Hello world\" cr"));

if (Program == NULL)
printf ("Not enough memory\n");
else {
printf ("Error# : %u\n", Program->ErrNo);
printf ("Error at word: %d\n", Program->ErrLine);
printf ("Object size : %d\n", Program->CodeSiz);
printf ("String size : %u\n", Program->StringSiz);
printf ("Var. offset : %u\n", Program->Offset);
printf ("Variables : %u\n", Program->Variables);
printf ("Strings : %u\n", Program->Strings);
printf ("Reliable : %s\n", Program->Reliable ? "Yes" :
"No");
}

The labels are kept the same as in dump_4th(), so if you need
more information, read that section again.

25.18 <sec:Adding-words-with>Adding words with arguments

The very first thing you have to do is to make sure that your
code can be saved and loaded again. Words that only consist of a
token are saved without the argument. That reduces the size of
the HX file. If you want to save the argument you have to add a
line to both load_4th() and save_4th():

case (LITERAL):

case (PRINT):

case (BRANCH):

case (BRANCH0):

Now we have to add code to exec_4th() in order to execute '."'.
The first problem we encounter is: how do we access the argument?
Accessing an argument is quite an expression:

Object->CodeSeg [Object->ErrLine].Value

In which:

Object = Hcode pointer

CodeSeg = Member of Hcode, pointing to the Code Segment

ErrLine = Member of Hcode, pointing to the current word

Value = Member of word, holding the argument

In plain English it means: give me the argument of the currently
executed word in the Code Segment.

If you want to create your own branch instructions, you'll have
to define their behaviour in exec_4th(). If the argument of the
token contains the address you want to jump to in the end, you'll
have to define it like this:

JUMP (OPERAND);

That is pretty easy. This macro changes the Program Counter,
which is part of the Hcode header:

Object->ErrLine

Of course, we've defined a macro for that:

PROGCOUNT

If the address you want to jump to is issued by the user, you
probably want to check whether it is a valid execution token.

The member "ErrLine" of an Hcode header always points to the
next available word in the Code Segment. That is the place where
we will compile our 'BRANCH' instruction. When the instruction
pointer is set to that location, it will be automatically
incremented and get inside the definition.

Hans Bezemer

--
Absolutely no trees were killed to produce this sig. Well, OK, we had to tie
one up and torture it. That's it.

Visit our website! http://thebeez.home.xs4all.nl/4tH/

*** Home of the 4tH compiler! ***

The Beez

unread,
Mar 15, 2012, 3:19:42 AM3/15/12
to 4tH-compiler
On Mar 13, 8:21 am, Hans Bezemer <theb...@xs4all.nl> wrote:
BTW, ErrNo is much like the C "errno". It's a message number and can
be used as an index to the array in errs_4th.c. It is used in both
compilation and runtime. In 4tH, it is usually manipulated using
#define constants called "mnemonics". Like all header members, it is
better not manipulated from a host C program.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages