View Full Version: Interpreter

C++ Learning Community > C++ Works in Progress > Interpreter


Title: Interpreter


dr voodoo - September 11, 2003 09:41 PM (GMT)
First the link:
http://www.geocities.com/voodoocpp/download.htm

It's not much but still. I tried a few times to make one and this is the first I get it to run :D. Anyway I plan on expanding this one. (Anyone have an idea where I could use it :unsure: )

shortys team - September 11, 2003 11:01 PM (GMT)
cool man if you made a built in text editor you could create a programming language

TheHawgMaster - September 13, 2003 02:47 AM (GMT)
Built-in text editors are not part of a language.

Also, I have some unfinished interpreter code that I'll probably post pretty soon...

MonkeyMan - September 15, 2003 12:18 AM (GMT)
Realy cool. im working on something similare but its a language already devoloped but only has 8 characters as commands :) ill post when i finish it :)

Dragon - September 15, 2003 12:32 AM (GMT)
QUOTE
cool man if you made a built in text editor you could create a programming language


I think he means to create a Win32 program as an IDE sort of thing and it will save the code into Code.txt and the program will run main.exe or whatever.

dr voodoo - September 15, 2003 08:19 AM (GMT)
QUOTE
I think he means to create a Win32 program as an IDE sort of thing and it will save the code into Code.txt and the program will run main.exe or whatever.

I know what he means but I don't think I'll do that.

shortys team - September 15, 2003 07:22 PM (GMT)
yea thats what i meant im still new to c++ so i wont be all technical

but it would be cool if ya did that
i meant to just make a win32 app like a plain text editor and when you press compile it saves whatever.txt into the exe then the interpreter reads it. the only problem you would have would be too make an exe out of it

does this sound retarted? :lol:

Incubator - September 15, 2003 10:24 PM (GMT)
you could do that and when the user presses teh button you could use system("gcc file.c -o main");
all you have to do then is to catch gcc´s output and print it to the console output of your ide (just like kdevelop does)
and yes there´s a win32 version of gcc :)

Kylevision - September 25, 2003 11:11 PM (GMT)
This is an AWESOME program, I have been trying to make an interperitor for months, but eventually got bored from it never working, then I learned that I would be usng Java in school for the next 3 years and completly dumped all my C++ stuff to learn Java, I thought about making a interperitor in Java but havent had the time, I think it will be much easyer in Java...Any way very nice work, is there any way I could view the source for this great program? Just to use as a referance when I try to make my Java one?

MonkeyMan - September 26, 2003 01:23 AM (GMT)
While on the topic... is it posable to get tokens or is there a better method?

TheHawgMaster - September 26, 2003 02:09 AM (GMT)
CODE
is it posable to get tokens or is there a better method?
I like the technique of bytecode compiling the file like Java. Much faster and cleaner in implementation.

dr voodoo - September 26, 2003 03:11 PM (GMT)
QUOTE
Any way very nice work, is there any way I could view the source for this great program? Just to use as a referance when I try to make my Java one?

Here it is:
CODE

#include <windows.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

char**stack;
char**stack_top;



void message(void)
{
     stack[1][strlen(stack[1])-1]=0;
     cout<<stack[1]+1<<endl;
}



bool LoadIndex(void)
{
    char*index;

    HANDLE file=CreateFile("code.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(file==INVALID_HANDLE_VALUE)
    {
          cout<<"Could not open code.txt"<<endl;
          system("pause");
          return false;
    }
    int size=GetFileSize(file,0);
    index=new char[size+2];
    index[size]=0;
    DWORD b;
    ReadFile(file,index,size,&b,NULL);
    CloseHandle(file);
    //Remove comment & new lines & taps
    bool string=false;
    bool comment=false;
    for(register char*i=index;*i!=0;i++)
    {
          if(string&&*i=='"')
               string=false;
          else if(comment)
          {
               if(*i==13)
                    comment=false;
               *i=' ';
          }
          else
          switch(*i)
          {
                case '"':
                string=true;
                break;
                case '#':
                if(string)
                    break;
                comment=true;
                case 9:
                case 13:
                if(string)
                    break;
                *i=' ';
                case 10:
                if(string)
                    break;
                *i='|';
          }
    }
    //Remove mutiple Spaces (but not in ""s) and spaces before |
    string=false;
    for(register char*i=index;*i!=0;i++)
    {
          if(string&&*i=='"')
               string=false;
          else if(!string)
          switch(*i)
          {
                case '"':
                string=true;
                break;
                case ' ':
                if(*(i+1)==' '||*(i+1)=='|'||(i!=index&&*(i-1)=='|'))
                {
                    memmove(i,i+1,strlen(i+1));
                    index[strlen(index)-1]=0;
                    i--;
                }
          }
    }
    //Remove multiple |s (but not in ""s)
    string=false;
    for(register char*i=index;*i!=0;i++)
    {
          if(string&&*i=='"')
               string=false;
          else if(!string)
          switch(*i)
          {
                case '"':
                string=true;
                break;
                case '|':
                if(*(i+1)=='|'||(i==index&&*i=='|'))
                {
                    memmove(i,i+1,strlen(i+1));
                    index[strlen(index)-1]=0;
                    i--;
                }
          }
    }
    int last=strlen(index)-1;
    if(index[last]!='|')
    {
         index[last]='|';
         index[last+1]=0;
    }

    //start interpreter
    //First we create the stack (best is to make it bigger than needed)
    stack=new char*[10];
    stack_top=stack;
    //We need to conserve the original index to free the memory
    char*old_index=index;

    string=false;
    for(register char*i=index;*i!=0;i++)
    {
          if(string&&*i=='"')
               string=false;
          else if(!string)
          switch(*i)
          {
                case '"':
                string=true;
                break;
                //If we need to push data on the stack:
                case '|':
                case ' ':
                //we push the data on the stack
                char temp=*i;
                *i=0;
                *stack_top=index;
                stack_top++;
                //We delete what we have handled
                index=i+1;

                //we check if we not are supposed to execute a command
                if(temp==' ')
                     break;

                if(strcmp(stack[0],"Message")==0)
                      message();
                else if(strcmp(stack[0],"Cls")==0)
                      system("cls");
                else if(strcmp(stack[0],"Pause")==0)
                      system("Pause");
                else
                      cout<<"Unknown command for '"<<stack[0]<<"'"<<endl;

                stack_top=stack;


          }
    }
    delete[]index;
    delete[]stack;
}
main()
{
    char*code;
    LoadIndex();
}

Kylevision - September 26, 2003 10:07 PM (GMT)
Thanks a lot :)

Jossh - September 28, 2003 09:35 PM (GMT)
I get this error:
CODE

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\Documents and Settings\Familt\Desktop\test.cpp:
Warning W8070 C:\Documents and Settings\Familt\Desktop\test.cpp 168: Function should return a value in function LoadIndex()
Warning W8004 C:\Documents and Settings\Familt\Desktop\test.cpp 168: 'old_index' is assigned a value that is never used in function LoadIndex()
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'WinMain' referenced from C:\BORLAND\BCC55\LIB\C0W32.OBJ

Dragon - September 28, 2003 10:59 PM (GMT)
QUOTE
Unresolved external 'WinMain' referenced from C:\BORLAND\BCC55\LIB\C0W32.OBJ


*Sigh*. Compile as a Win32 GUI application.

Jossh - September 28, 2003 11:02 PM (GMT)
>.> i did using Drvoodoo's bat maker

Dragon - September 29, 2003 12:18 AM (GMT)
Well, you obviously did something wrong. I compiled it a while back and it worked fine.

dr voodoo - September 29, 2003 02:50 PM (GMT)
QUOTE
QUOTE
Unresolved external 'WinMain' referenced from C:\BORLAND\BCC55\LIB\C0W32.OBJ

*Sigh*. Compile as a Win32 GUI application.

He is compiling as GUI but the program is a console application.

Dragon - September 29, 2003 10:51 PM (GMT)
Oh, it is. I didn't remember what I compiled it as.

Jossh - September 29, 2003 10:53 PM (GMT)
hmmm.... i compiled it as a console app, no errors happend this time but when i open the .exe it just shows a blank msdos screen and then closes >.>

Dragon - September 29, 2003 10:56 PM (GMT)
Did you take a look at how to use it?

dr voodoo - September 30, 2003 10:50 AM (GMT)
Make sure you have a file called "code.txt".

It is in the zip and containes the code to be interpreted:
If you don't want to download:
CODE
#This is my interpreter v0.1 not much for the moment
#so I am quickly finished:

#everything after a # is comment

#everything between "s is a string and will not
#be interpreted (althought it can be passed as
#argument)

#a | is an instruction seperator

#All functions are used in this example

Message "Hello
Read code.txt for detail"
Pause|Cls
Message "Cls clears all lines"|
Message "Because # and | are in quotes they
are not interpreted althought an escap char
for a quote is still missing :-/"
Pause
Message ""|Message "Anyway hope you like it
Oh and don't tell my code.txt is bad style coding
it is supposed to be like that to show all
syntax possiblieties and I know that there is no
AAA function"
Pause|Cls
AAA|
Pause

Jossh - September 30, 2003 10:59 AM (GMT)
Ya i do... owell, i just wanted to see how this thing worked..... but i doesnt for me.. Thanks anyways :D

TheHawgMaster - September 30, 2003 01:19 PM (GMT)
QUOTE
:-/
What's that? Drooling?

dr voodoo - October 1, 2003 03:53 PM (GMT)
QUOTE
Ya i do... owell, i just wanted to see how this thing worked..... but i doesnt for me.. Thanks anyways

Try downloading the compiled version (see first post of this topic) if it works it's some compilation error else it's your computer. If you used my batmaker could you please post the contend of the produced *.bat (Open with Notepad and copy paste). I just want to see if there's something wrong.

Jossh - October 1, 2003 07:46 PM (GMT)
Your compiled version works, but when i compile it doesnt, and yes i used your batmaker here is the .bat
CODE

:batmaker file
:::::::::::::::::::::::::::::::::::::::::::::
:This file was made with Batmaker v0.5a     :
:This file is not ment to be change by hand!:
:::::::::::::::::::::::::::::::::::::::::::::
:SFC:\Documents and Settings\Familt\Desktop\Josh\test.cpp
:PEMyProg.exe
:BCC:\Borland\BCC55\Bin\bcc32.exe
:LOMyProg.log
:CO
@echo off
color F0
path "C:\Borland\BCC55\Bin\"%PATH%
del "MyProg.exe"
del "MyProg.log"
cls
echo Batmaker v0.5a
echo Compiling C:\Documents and Settings\Familt\Desktop\Josh\test.cpp...
"C:\Borland\BCC55\Bin\bcc32.exe" -c -tWC "C:\Documents and Settings\Familt\Desktop\Josh\test.cpp">>"MyProg.log"
cls
echo Batmaker v0.5a
echo Linking MyProg.exe...
"C:\Borland\BCC55\Bin\ilink32.exe" -c -Gn -x -ap c0x32.obj "C:\Documents and Settings\Familt\Desktop\Josh\test.obj","MyProg.exe",,import32.lib cw32.lib,,>>"MyProg.log"
cls
del "C:\Documents and Settings\Familt\Desktop\Josh\test.obj"
del "MyProg.tds"
cls
echo Finished!
echo If this window doesn't close automaticaly hit the X in the upper right corner of this window.
exit

dr voodoo - October 1, 2003 08:32 PM (GMT)
Strange... :blink:
I can't reproduce the error even if I use the same folder paths and *.bat.

Can you get some other console apps to work?

Because the linker is linking the GUI startup module (c0w32.obj) instead of the Console one (c0x32.obj) but that bat clearly stats c0x32.obj.

Jossh - October 1, 2003 08:38 PM (GMT)
Yes.. i have compiled other console apps(like the hello example on your site)

AcidGame - November 26, 2003 03:42 AM (GMT)
Awesome Dr Voodoo! Just a quick question.. is it possible to make an interpreter that can do the same stuff as the php interpreter? What I mean by that is.. when you view a php page, it tells the server to run the file through a Console based program, called php.exe located on the server. But what php does is, it returns the final information to you for viewing, but keeps the same exact stuff un-interpreted. How would you mock this feature?




* Hosted for free by InvisionFree