Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interpreter write-a-C-interpreter/xc.c - from Github
01-11-2019, 05:20 AM
Post: #1
Interpreter write-a-C-interpreter/xc.c - from Github
I just stop over github and forund this one .
I think that will not compile under Falcon C++ 3.3 IDE
so i just copy paste code into new file and run F(9)

and voila it WORK Smile
I am not sure what this interpretr processing probably
some sort of source built-in into source i run ?
Do you know Ed?

link:
https://github.com/lotabout/write-a-C-in...aster/xc.c

and here is screenshot:


Attached File(s) Image(s)
   
Find all posts by this user
Quote this message in a reply
01-11-2019, 05:22 AM
Post: #2
RE: Interpreter write-a-C-interpreter/xc.c - from Github
ED
Did you ever tried to use this Falcon C++ IDE v3.3 ?
It seems that working well and report each errors or problems?
right?
Find all posts by this user
Quote this message in a reply
01-11-2019, 09:16 PM
Post: #3
Hello Ed !- write-a-C-interpreter/xc.c - from Github
Hi Ed..
do you can explain to me in short:
Is this small interpreter byetcode.?
From what i see and understand there is no real tokenizer?
And do you know how this nterpreter work Huh
Find all posts by this user
Quote this message in a reply
01-11-2019, 10:28 PM (This post was last modified: 01-11-2019 10:33 PM by Ed Davis.)
Post: #4
RE: Interpreter write-a-C-interpreter/xc.c - from Github
XC - this is just a cleaned up version of C4: https://github.com/rswier/c4

c4 is a wonderful feat of engineering - it is a compiler for a subset of C that can compile itself! From the authors webpage:

Quote:c4 - C in four functions

An exercise in minimalism.

Try the following:

gcc -o c4 c4.c (you may need the -m32 option on 64bit machines)
./c4 hello.c
./c4 -s hello.c

./c4 c4.c hello.c
./c4 c4.c c4.c hello.c

That last example is c4 compiling running c4 compiling running c4 compiling and running hello. Cool!

(01-11-2019 09:16 PM)Aurel Wrote:  Hi Ed..
do you can explain to me in short:
Is this small interpreter byetcode.?

Yes, it generates bytecode. See the definitions of the opcodes at line 11.

Bytecode interpreter is in function eval(), starting at line 1213.

Quote:From what i see and understand there is no real tokenizer?

No, you missed it. There is a _real_ tokenizer. In fact a very good one - able to tokenize most of c. Which is often much harder than BASIC. It is in functio next(), starting at line 64.

Quote:And do you know how this nterpreter work Huh

Yep. I've studied XC and c4 for many hours. I wish I was half as smart as the author - Robert Swierczek. He is definitely an elite programmer.

Once you have read it for a while, you'll realize that conceptually, it is pretty simple.

next() returns each token in turn.

Parsing starts at program() which in turns parses declarations and functions and then statements. Recursive descent is used for parsing.

Expressions are parsed in function expression(). Precedence climbing is used for parsing expressions.

Code is generated as soon as it is identified. No AST is built.

This is a classic compiler. This is the way many smaller ones are built. PL/0 is pretty much like this, except PL/0 uses recursive descent for everything, even expressions, where as c4 uses precedence climbing for expressions. Otherwise, they are very, very similar.

You can learn a lot from studying this code.

Quote:This is a classic compiler. This is the way many smaller ones are built. PL/0 is pretty much like this, except PL/0 uses recursive descent for everything, even expressions, where as c4 uses precedence climbing for expressions. Otherwise, they are very, very similar.

I should have added that this and the little Toy compilers I wrote (in C, Pascal, FreeBasic, Euphoria and vbscript) follow this exact same model. Well, they are more like PL/0, as they use recursive descent throughout. I wrote Toy after studying PL/0.

Again, XC (c4) is a really good example. If you can understand each line, you'll no only have a good understanding how compilers work (tokenizing, parsing, code generation, interpreting) you'll also have a good understanding of C.
Find all posts by this user
Quote this message in a reply
01-12-2019, 03:25 AM
Post: #5
RE: Interpreter write-a-C-interpreter/xc.c - from Github
Thanks Ed on explanation and observation:

Quote:you'll also have a good understanding of C.

hmmm .. that is really interesting conclusion
well I need lot of time for study this interpreter
that's for sure.
See i undestand some part but this selfcompiling is really weird for me.
Code is clear in some things but in some is not to me.
I am confused with resulting console window with
just "press any key "
ok i will try ... Smile
Find all posts by this user
Quote this message in a reply
01-12-2019, 03:45 AM
Post: #6
RE: Interpreter write-a-C-interpreter/xc.c - from Github
(01-12-2019 03:25 AM)Aurel Wrote:  See i undestand some part but this selfcompiling is really weird for me.

Self-compiling just means that it can compile itself.

Run this:

c4 hello.c

You'll get something like:

"hello, world".

You've told c4 to compile and run hello.c

Now run this:

c4 c4.c hello.c

You'll get:

"hello, world"

Now you've told c4 to compile and run c4.c, and you've instructed this second copy of c4 to run hello.c.

We could do the same thing with PL/0. In fact, I did. I kept adding the minimal number of features, until it could compile and run itself.

It is kind of a "right of passage" of programmers, writing a compile that can compile itself.

(01-12-2019 03:25 AM)Aurel Wrote:  Code is clear in some things but in some is not to me.

Same here - it took me a while to decipher must of it.

(01-12-2019 03:25 AM)Aurel Wrote:  I am confused with resulting console window with
just "press any key "
ok i will try ... Smile

Console programs work best if you run them from a console. Real programmers use the console, so you need to get used to it :-)

Click on start, then run, and type "cmd", and press enter. Presto, you have a console to play with!
Find all posts by this user
Quote this message in a reply
01-12-2019, 07:33 AM
Post: #7
RE: Interpreter write-a-C-interpreter/xc.c - from Github
Real programmers use the console,

This sound silly Ed and you know that Big Grin

OK Sir !
Smile
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: