a simple basic interpreter written in c

Upload: cooperante-anonimo

Post on 08-Mar-2016

125 views

Category:

Documents


10 download

DESCRIPTION

A Simple BASIC Interpreter Written in C

TRANSCRIPT

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 1/11

    Search

    News:InstructionsforcreatingAndroidApps:

    http://www.qb64.net/forum/index.php?topic=13162.0

    QB64Community Development Development(Moderators:Galleon,OlDosLover,SMcNeill,Kobolt)AsimpleBASICInterpreterwritteninC

    PRINTPages:[1]

    AsimpleBASICInterpreterwritteninC

    on:June22,2013,04:42:24am

    OlDosLoverModeratorHeroMember

    Posts:5329

    OlDosLover

    Hiall,InmytravelsicameacrossaverysmallCBasicIntrepetor.WhatwouldbereallygoodwouldbeifsomeoneknowledgableinCcouldconvertthistoQB64.HeretheoriginalCcode.Code:[Select]

    - Line editor using line re-entry. - A line number with nothing following it, deletes the line. - Line number range 1-10000

    Input format:

    - ALL INPUT MUST BE UPPERCASE. - No space is allowed before the line number. - Exactly one space is needed between the OLD or SAVE command and the filename.

    No error checking is performed. The message 'core dumped' or 'bus error' signifies a syntax or semantic error.

    */

    Hereisanotherbugcorrectionversionofabove.Code:[Select]

    previousnext

    Author Topic:AsimpleBASICInterpreterwritteninC(Read6253times)

    Home Help Search Login Register

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 2/11

    RUN

    Be sure not to run out of fuel!

    Selected notes from the author:

    This is the DDS-BASIC Interpreter (Version 1.00).

    Immediate commands:

    RUN LIST NEW OLD filename BYE SAVE filename

    Program commands:

    variable names a to z variables initialized to 0 on RUN

    Thiscameaboutfromthispost:http://forum.basicprogramming.org/index.php/topic,2954.0.htmlOlDosLover.

    Logged

    Re:AsimpleBASICInterpreterwritteninC

    Reply#1on:June22,2013,06:56:08am

    AmaysimJr.Member

    Posts:54

    WhatsaBasicIntrepetorandwhatdoesitdo?Ama

    Logged

    Re:AsimpleBASICInterpreterwritteninC

    Reply#2on:June22,2013,12:01:26pm

    DSMan195276HeroMember

    Posts:2113

    Yes

    SorryOlDos,

    Quote

    thisisa1990IOCCC(obfuscatedCcode)entry

    There'snexttonohopeofconvertingthisunlessthereisanunobfuscatedversionofthecodesomewhere.

    Matt

    Logged

    "CastyourcaresontheLordandhewillsustainyouhewillneverlettherighteousbeshaken"Psalm55:22QB64LinuxInstaller

    Re:AsimpleBASICInterpreterwritteninC

    Reply#3on:June22,2013,05:19:12pm

    GalleonAdministratorHeroMember

    Posts:5464

    Thetroubleisthattheinterpreterwouldbesolimitedthatyou'dbebetteroffrollingyourownanyway.InfactI'veseenaninterpreterwritteninQBcode,itmayevenbeintheexamplessomewhere.

    hereitis:

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 3/11

    QBForeverCode:[Select]IF ACCEPT("-") THEN FACTOR% = -FACTOR% EXIT FUNCTIONEND IFIF ACCEPT("(") THEN FACTOR% = EXPRESSION CALL EXPECT(")") EXIT FUNCTIONEND IFIF LEFT$(TOK$, 1) >= "0" AND LEFT$(TOK$, 1) = "A" AND LEFT$(TOK$, 1)

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 4/11

    int*gosub_stackp,gosub_stack[999],line[999],line_off[999],lim[999],var[999],linenum,i,inquoteint*gosub_foop,gosub_foo[999],at[999]charbuff[999],two[2]CHARPm[12*999],p,q,s,d//pusedasexprptrFILE*f

    #if!defined(randomize)#definerandomize()#endif

    #if!defined(random)staticintrandom_seed=0

    staticintrandom(intn){random_seed=random_seed*1103515245+12345return((((random_seed/65536)%32768)&0x7fffffff)%n)}#endif

    intfindop(charop){switch(op){case'=':return1case'#':return1//,notequalcase'':return2case'$':return2//=case'+':return3case'':return3case'*':return4case'/':return4}return1}

    intevalbinary(charop,intl1,intl2){switch(op){case'=':returnl1==l2case'#':returnl1!=l2//,notequalcase'':returnl1>l2case'$':returnl1=case'+':returnl1+l2case'':returnl1l2case'*':returnl1*l2case'/':returnl1/l2}return0}

    intexpr(intprec){into,this_prec

    if(*p==''){p++o=expr(999)}elseif(isdigit(*p)){o=strtol(p,&p,10)//can'tuse0forbase,becauseofhexpbm.}elseif(*p=='('){++po=expr(0)++p}elseif(*p=='@'){//@(exp)++po=at[expr(999)]}elseif(memcmp(p,"rnd",3)==0){//rnd(exp)p+=3o=expr(999)o=random(o)+1}elseif(memcmp(p,"abs",3)==0){//abs(exp)p+=3o=abs(expr(999))}elseif(memcmp(p,"asc",3)==0){//asc("x")p+=5o=*pp+=3}else

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 5/11

    o=var[*p++]

    while((this_prec=findop(*p))>0&&this_prec>=prec){charop=*p++o=evalbinary(op,o,expr(this_prec+1))}returno}

    voidprint_string(void){intwidth++pwidth=strchr(p,'"')pprintf("%*.*s",width,width,p)p+=width+1}

    //'print'[[#num',']expr{','[#num',']expr}][',']{':'stmt}eol//exprcanalsobealiteralstringvoidprint(void){intprint_nl

    print_nl=truefor( {intwidth=0if(*p==':'||*p=='\0')breakprint_nl=trueif(*p=='#'){++pwidth=expr(0)if(*p==',')++p}

    if(*p=='"')print_string()elseprintf("%*d",width,expr(0))

    if(*p==','||*p==''){++pprint_nl=false}elsebreak}if(print_nl)printf("\n")}

    voidenterline(void){char*s=buffwhile(*s&&isspace(*s))s++linenum=atoi(s)if(m[linenum])free(m[linenum])if((p=strstr(s,""))!=NULL)strcpy(m[linenum]=malloc(strlen(p)),p+1)elsem[linenum]=0}

    voidload(char*fn){f=fopen(fn,"r")while(fgets(buff,999,f))(*strstr(buff,"\n")=0,enterline())fclose(f)}

    voidrun(void){intoffset=0gosub_stackp=gosub_stackgosub_foop=gosub_foolinenum=1for(i=0i

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 6/11

    intif_contwhile((s=m[linenum])==0)linenum++if(!strstr(s,"\"")){while((p=strstr(s,""))!=0)*p++='#',*p=''while((p=strstr(s,"="))!=0)*p++='!',*p=''}//removeextraspaces,linecopiedtobuffd=buffwhile((*two=*s)!='\0'){if(*s=='"')inquote++if(inquote&1||!strstr("\t",two))*d++=*ss++}inquote=*d=0s=buffline_processed:p=(s+=offset)offset=if_cont=0

    if(s[1]=='='){//assignmenta=expp=s+2var[*s]=expr(0)}elseif(s[0]=='@'){//assignment:@(exp)=expintndxp=s+1ndx=expr(999)//usehighprectoforceendat')'++pat[ndx]=expr(0)}elseswitch(*s){case'e'://endcase's'://stoplinenum=1breakcase'r'://remandreturnif(s[2]!='m'){linenum=*gosub_stackp//returnoffset=*gosub_foop}breakcase'i'://input[constant_string,]varandifif(s[1]=='n'){//inputinttmpcharin_buff[20]d=p=&s[5]if(*p=='"'){print_string()d=++p//skip','}tmp=*dp=fgets(in_buff,sizeof(in_buff)2,stdin)var[tmp]=isdigit(*p)?expr(0):*pp=++d}else{//ifp=s+2if(expr(0)){pif_cont=true}elsep=0}breakcase'p'://printstringandexprp=&s[5]print()breakcase'g'://goto,gosubp=s+4if(s[2]=='s'){//gosub*gosub_stackp++=linenump++}linenum=expr(0)1if(s[2]=='s')//gosub*gosub_foop++=(*p==':')?pbuff+1:0

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 7/11

    p=0breakcase'f'://for*(q=strstr(s,"to"))=0p=s+5var[i=s[3]]=expr(0)p=q+2lim=expr(0)line=linenumline_off=(*p==':')?pbuff+1:0breakcase'n'://nextd=s+4p=d+1if(++var[*d]1){load(argv[1])buff[0]='r'loaded=true}while(loaded||(puts("Ok"),gets(buff))){loaded=falseswitch(*buff){case'r'://runrun()breakcase'l'://listfor(i=0i

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 8/11

    Code:5y=2999:input"Doyouwantadifficultgame?(yorn):",a10print"Stardate3200:yourmissionis",:ifa=asc("y")y=99915k=0:b=0:d=30:fori=0to63:j=rnd(99)

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 9/11

    355print#1,e,"unitsofenergyleft.":ifrnd(e/4)>treturn360if@(70)=0@(70)=rnd(t/50+1):j=7:goto375365j=rnd(6):@(j+63)=rnd(t/99+1)+@(j+63):i=rnd( +1:c=c+i370print"McCoy:'Sickbaytobridge,wesuffered",#2,i,"casualties.'"375i=@(j+63):ifj=1print"Shortrangesensor",380ifj=2print"Computerdisplay",385ifj=3print"Longrangesensor",390ifj=4print"Phaser",395ifj=5print"Warpengine",400ifj=6print"Photontorpedotubes",405ifj=7print"Shield",410ifi=0return415print"damaged,",#1,i,"stardatesestimatedforrepair":return420print"Statusreport:":print"Stardate",#10,3230d:print"timeleft",#7,d425print"Condition",:ifoprint"Docked":goto445430ifnprint"Red":goto445435ife91w=91:print"Spock:'Areyousure,Captain?'"485ife0):nextm505p=45*x+22:g=45*y+22:w=45*w:form=1to8:w=wr:ifw

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 10/11

    Reply#6on:June25,2013,11:00:12pm

    ModeratorHeroMember

    Posts:5329

    OlDosLover

    Hiall,Cameacrossaninterestinglink:https://sites.google.com/site/smallbasicinterpreters/OlDosLover.

    Logged

    Re:AsimpleBASICInterpreterwritteninC

    Reply#7on:June26,2013,12:17:11am

    OlDosLoverModeratorHeroMember

    Posts:5329

    OlDosLoverHiall,Here'sareferencebook.https://dl.dropboxusercontent.com/u/10291175/SICPIntrerpretor%20Book1.7zOlDosLover.

    Logged

    Re:AsimpleBASICInterpreterwritteninC

    Reply#8on:June26,2013,12:24:33am

    OlDosLoverModeratorHeroMember

    Posts:5329

    OlDosLoverHiall,Anotherknowledgeableresourcegroup.http://tech.groups.yahoo.com/group/QDepartment/OlDosLover.

    Logged

    Re:AsimpleBASICInterpreterwritteninC

    Reply#9on:July01,2013,12:02:30pm

    qbguySr.Member

    Posts:263

    Quotefrom:OlDosLoveronJune26,2013,12:17:11am

    Hiall,Here'sareferencebook.https://dl.dropboxusercontent.com/u/10291175/SICPIntrerpretor%20Book1.7zOlDosLover.

    ThebookStructureandInterpretationofComputerProgramshasaPDFversionathttp://sicpebook.files.wordpress.com/2012/11/sicp.pdfandisavailableonlineathttp://mitpress.mit.edu/sicp/fulltext/book/book.html

    Therearealsosomevideostogowithit.ItdescribesaninterpreterandacompilerforScheme.

    Logged

    PRINTPages:[1]

    QB64Community Development Development(Moderators:Galleon,OlDosLover,SMcNeill,Kobolt)AsimpleBASICInterpreterwritteninC

    previousnext

    Jumpto: =>Development go

  • 15/12/2015 A simple BASIC Interpreter written in C

    http://www.qb64.net/forum/index.php?topic=11134.0 11/11

    SMF2.0.3|SMF2011,SimpleMachinesXHTML RSS WAP2