Announcement

Collapse
No announcement yet.

Need help with my Interpreter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with my Interpreter

    Hey there,
    merry Christmas to all of you. The last two weeks I tried to write a little
    and very simple interpreter in PowerBASIC 3.5. Well so far, everything works
    but there is a problem I don't know how to solve.

    At first take a look at the source (and yeah I think I know there are better
    ways to program but I am a just a beginner )

    ' aBASIC - Interpreter Version 0.1
    ' Copyright (c) 2003 by Christian Damhus
    '

    filename$ = command$

    ' ™ffne die angegebene Datei
    zeile% = 1

    ON ERROR GOTO serror
    if filename$ = "help" then goto helpsystem
    anz_stringvariable = 0

    open filename$ for input as #1
    cls
    do
    input #1, sbcode$

    lange% = len(sbcode$)
    befehl$ = left$(sbcode$, 3)
    if befehl$ = "pri" then goto printbefehl
    if befehl$ = "cls" then cls : goto parsenextline
    if befehl$ = "bee" then beep : goto parsenextline
    if befehl$ = "key" then goto waitkeybefehl
    if befehl$ = "del" then goto delaybefehl
    if befehl$ = "rem" then goto parsenextline
    If befehl$ = "kil" then goto killbefehl
    if befehl$ = "bee" then beep : goto parsenextline
    if befehl$ = "dir" then goto dirbefehl
    if befehl$ = "chd" then goto chdirbefehl
    if befehl$ = "inp" then goto inputbefehl
    if befehl$ = "she" then goto shellbefehl

    ' ----------------------------------------------------------------
    ' Befehle
    ' ----------------------------------------------------------------
    printbefehl:
    ' Beispiel : print Hallo World
    ausgabe$ = mid$(sbcode$, 7)
    variable$ = right$(sbcode$,1)
    if variable$ = chr$(47) then goto gib_aus
    print ausgabe$
    goto parsenextline
    gib_aus:
    nausgabe$ = REMOVE$(ausgabe$, "/")
    print nausgabe$ + stringvariable$
    goto parsenextline


    ' ----------------------------------------------------------------

    waitkeybefehl:
    ' key
    print "Beliebige Taste drcken zum Fortfahren..."
    while taste$="" : taste$ = inkey$ : wend
    goto parsenextline

    ' ----------------------------------------------------------------

    delaybefehl:
    ' delay 100
    zeit$ = mid$(sbcode$, 7)
    zeit = val(zeit$)
    if zeit$ = chr$(47) then goto delayvariable
    delay(zeit)
    goto parsenextline
    delayvariable:
    zeita = val(stringvariable$)
    delay(zeita)
    goto parsenextline

    ' ----------------------------------------------------------------

    killbefehl:
    ' kill filename
    finame$ = mid$(sbcode$, 6)
    if finame$ = chr$(47) then kill stringvariable$ : goto parsenextline
    kill finame$
    goto parsenextline

    ' ----------------------------------------------------------------

    dirbefehl:
    ' dir extension
    ext$ = mid$(sbcode$, 5)
    if ext$ = chr$(47) then goto dirvariable
    datei$ = dir$(ext$)
    print datei$
    do until datei$ = ""
    datei$ = dir$
    print datei$
    loop
    goto parsenextline
    dirvariable:
    datei$ = dir$(stringvariable$)
    print datei$
    do until datei$ = ""
    datei$ = dir$
    print datei$
    loop
    goto parsenextline

    ' ----------------------------------------------------------------

    chdirbefehl:
    ' chdir c:\pb
    verz$ = mid$(sbcode$, 7)
    if verz$ = chr$(47) then goto chdirvariable
    chdir verz$
    goto parsenextline
    chdirvariable:
    chdir stringvariable$
    goto parsenextline

    ' ----------------------------------------------------------------

    inputbefehl:
    ' input was eingeben
    inputtext$ = mid$(sbcode$, 7)
    print inputtext$
    line input stringvariable$
    goto parsenextline

    ' ----------------------------------------------------------------

    shellbefehl:
    ' shell befehl
    shellbe$ = mid$(sbcode$, 7)
    if shellbe$ = chr$(47) then goto shellvariable
    shell shellbe$
    goto parsenextline
    shellvariable:
    shell stringvariable$
    goto parsenextline

    ' ----------------------------------------------------------------

    parsenextline:
    zeile% = zeile% + 1
    LOOP UNTIL sbcode$ = "end"
    close #1
    color 1,0 : print "ABASIC Interpreter V0.1"
    print "Copyright (c) 2003 by Christian Damhus."
    end


    fehler:
    PRINT "Zeile "; zeile%;": Befehl unbekannt!"
    end

    helpsystem :
    cls
    color 1,0 : locate 1,29 : print "ABASIC Version 0.1"
    locate 2,17 : print"Copyright (c) 2003 by Christian Damhus"
    locate 3,17 : color 7,0 : print"--------------------------------------"
    locate 5,17 : print "Syntax : sb <filename.bas> OR sb help"
    locate 7,10 : print "ABASIC is a simple, primitive BASIC Interpret written"
    locate 8,10 : print "PowerBASIC V3.5. It supports only a few real BASIC commands"
    locate 9,10 : print "but it is growing."
    locate 11,10: print "ABASIC is freeware. You can use it to learn how to"
    locate 12,10: print "write your own interpreter but it comes without ANY guarranties"
    locate 13,10: print "For further information see : www.christian-damhus.tk/sb
    locate 15,10: print "Supported Commands :"
    locate 16,10: print "===================="
    locate 17,10: print "PRINT, INPUT, SHELL, CLS, KILL, DIR, CHDIR, REM, KEY, BEEP"
    end

    serror:
    cls
    color 1,0 : print "ABASIC Version 0.1"
    color 1,0 : print "Copyright (c) 2003 by Christian Damhus"
    print
    print "Usage : sb <filename.bas> OR sb help"
    print
    color 7,0 : print "Error : Can't find file " + filename$
    end
    ---------------------------------------------------------------
    When you use input, the value you unter will be stored in stringvariable$ .
    But it can only have one value. If you use the input command again, the
    new value will overwrite the first value and so it gets lost.
    So every abasic program can just save one variable. What I try to do now
    is, that an abasic program can take more than just one variable, e.g.
    input name1$
    input name2$ and so on
    ...
    print name1$
    print name2$ and so on

    I don't know how to realize this. I tried to create a type structure (like
    type stringvariable_table
    identifier AS STRING*4 'only 4 letters for the name of the variable
    value AS STRING*255
    end type

    DIM Tstringvariable_table AS stringvariable_table

    Every variable should be stored in this table. But well it did not really
    work. As I said before, I am just a beginner and have to learn a lot about
    PowerBASIC. So I'd like to ask for your help because you know way more than I do.

    Greets
    Christian

    PS. You don't have to type the source code. The link is www.abasic.privat.t-online.de/abasic01.zip



    ------------------
    Christian Damhus M.A.

  • #2
    Christian,
    I'm not real clear on what you're trying to do, but I believe your answer is with ARRAYS. If you create a String Array (e.g. DIM Cmd$(10)) then you could store 11 commands in this one "variable", like so:

    Cmd$(0) = "Command 1"
    Cmd$(1) = "Command 2"
    Cmd$(2) = "Command 3"
    Cmd$(3) = "Command 4"
    ...
    Cmd$(10) = "Command 11"

    Arrays are Zero based (unless you declare otherwise). There are a number of ARRAY commands in PB as well that let you sort the data in the array, delete or insert data very easily. I think if you play with the ARRAY concept awhile, you'll find the solution you seek.

    --Joe


    ------------------
    Joe Byrne
    mailto:[email protected]
    [email protected]
    </A>
    Software makes Hardware Happen

    Comment


    • #3
      Hey Joe,

      I will try that and see how it works. Thank you for your help.

      Greets
      Chris



      ------------------
      Christian Damhus M.A.

      Comment

      Working...
      X