Author Topic: The Best Interpreter I've ever made!  (Read 128 times)

Offline Prithak

  • Make it work, make it right, make it fast.
    • The History Of Video Games(My Latest Website)
The Best Interpreter I've ever made!
« on: March 16, 2019, 09:45:35 PM »
I made an interpreter.... again. And it's limited this time as well!! Anyways... Here's the code for the interpreter:

Code: [Select]
CLS
PRINT "Welcome to PLanguage Compiler! Enter your file name to see the results."
PRINT "Extra commands:"
PRINT "/cls - clear the screen"
PRINT "/edit - edit a file using notepad"
PRINT "/exit - to exit this program"
PRINT STRING$(80, "-")

menu:
INPUT ">>", file$
IF INSTR(file$, "/edit") THEN
    SHELL "notepad " + RIGHT$(file$, LEN(file$) - 6)
ELSEIF file$ = "/exit" THEN
    END
ELSEIF file$ = "/cls" THEN
    CLS
    GOTO menu
END IF
OPEN file$ FOR INPUT AS #1
OPEN "p1.bas" FOR OUTPUT AS #2
DO WHILE NOT EOF(1)
    LINE INPUT #1, l$
    l$ = replace$(l$, "[", "(")
    l$ = replace$(l$, "]", ")")
    x = x + 1
    IF INSTR(l$, "println") THEN
        PRINT #2, "PRINT " + RIGHT$(l$, LEN(l$) - 7)
    ELSEIF INSTR(l$, "input") THEN
        l$ = replace$(l$, "(", "")
        l$ = replace$(l$, ")", "")
        PRINT #2, l$
    ELSEIF INSTR(l$, "}") THEN
        IF e$ = "for" THEN
            PRINT #2, "NEXT"
        ELSEIF e$ = "if" THEN
            PRINT #2, "END IF"
        ELSEIF e$ = "funct" THEN
            PRINT #2, "END FUNCTION"
        ELSEIF e$ = "while" THEN
            PRINT #2, "WEND"
        END IF
        e$ = ""
    ELSEIF INSTR(l$, "for") THEN
        l$ = replace$(l$, "{", "")
        PRINT #2, l$
        e$ = "for"
    ELSEIF INSTR(l$, "elif") THEN
        l$ = replace$(l$, "{", "")
        PRINT #2, "ELSEIF " + RIGHT$(l$, LEN(l$) - 4) + " THEN"
    ELSEIF INSTR(l$, "else") THEN
        l$ = replace$(l$, "{", "")
        PRINT #2, l$

    ELSEIF INSTR(l$, "if") THEN
        l$ = replace$(l$, "{", "") + " THEN"
        PRINT #2, l$
        e$ = "if"
    ELSEIF INSTR(l$, "funct") THEN
        l$ = replace$(l$, "{", "")
        PRINT #2, "FUNCTION " + RIGHT$(l$, LEN(l$) - 5)
        e$ = "funct"
    ELSEIF INSTR(l$, "clear()") THEN
        PRINT #2, "CLS"
    ELSEIF INSTR(l$, "terminate()") THEN
        PRINT #2, "END"
    ELSEIF INSTR(l$, "wait") THEN
        PRINT #2, "SLEEP"
    ELSEIF INSTR(l$, "delay") THEN
        PRINT #2, "_DELAY " + RIGHT$(l$, LEN(l$) - 5)
    ELSEIF INSTR(l$, "terminate.instant()") THEN
        PRINT #2, "SYSTEM"
    ELSEIF INSTR(l$, "color") THEN
        l$ = replace$(l$, "(", "")
        l$ = replace$(l$, ")", "")
        PRINT #2, l$
    ELSEIF INSTR(l$, "while") THEN
        l$ = replace$(l$, "{", "")
        PRINT #2, l$
        e$ = "while"
    ELSEIF INSTR(l$, "createVector") THEN
        PRINT #2, "TYPE " + RIGHT$(l$, LEN(l$) - 12)
        PRINT #2, "X AS INTEGER"
        PRINT #2, "Y AS INTEGER"
        PRINT #2, "END TYPE"
    ELSEIF INSTR(l$, "setScreen") THEN
        PRINT #2, "SCREEN _NEWIMAGE" + RIGHT$(l$, LEN(l$) - 9)
    ELSEIF INSTR(l$, "declare") THEN
        l$ = replace$(l$, "[", "(")
        l$ = replace$(l$, "]", ")")
        PRINT #2, "DIM " + RIGHT$(l$, LEN(l$) - 7)
    ELSE
        IF INSTR(l$, "=") OR INSTR(l$, "}") OR LEN(l$) = 0 THEN
            l$ = replace$(l$, "[", "(")
            l$ = replace$(l$, "]", ")")
            PRINT #2, l$
        ELSE
            PRINT "ERROR ON LINE"; x
            PRINT l$
            PRINT "Please try revising the code..."
            END
        END IF
    END IF
LOOP
CLOSE #2
CLOSE #1
OPEN "p1.bas" FOR INPUT AS #1
DO WHILE NOT EOF(1)
    LINE INPUT #1, l$
    PRINT l$
LOOP
CLOSE #1
PRINT "Compiling Your File..."
SHELL _HIDE "qb64 -x p1.bas"
SHELL "p1.exe"
GOTO menu
FUNCTION replace$ (a$, from$, to$)
    FOR i = 1 TO LEN(a$)
        c$ = MID$(a$, i, 1)
        IF c$ = from$ THEN
            replace$ = replace$ + to$
        ELSE
            replace$ = replace$ + c$
        END IF
    NEXT i
END FUNCTION

I also made a website regarding this so: PLanguage.github.io
ONE PROBLEM!
I can't seem to use nested loops in this. I am guessing because I am not using an array in "e$". Well, I'll figure it out soon enough! If you have some ideas about this, then please do not hesitate to leave a comment! :D
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Re: The Best Interpreter I've ever made!
« Reply #1 on: March 17, 2019, 09:33:01 AM »
Prithak, a small point, but I notice that you submit your Programs in the QB64 Discussion section.  The Programs section "This section is for (i) completed programs, or (ii) works in progress." would be the best place - it would save Odin having to move things.
« Last Edit: March 17, 2019, 09:42:32 AM by Qwerkey »

Re: The Best Interpreter I've ever made!
« Reply #2 on: March 17, 2019, 10:01:06 AM »
This is a curious thing that you call an interpreter.

From your link, I found this example for function:
Quote
clear()
input("Enter a", a)
input("Enter b", b)
sum = add(a,b)
println "The Sum of those number = "; sum

funct add(num1,num2) {
   add = num1 + num2
}
What language is this? Your PL?

It would be nice in your menu to be able to start a new file in Notepad eg prompt for a name and start Notepad with it.

Anyway to get the test1.txt file to load in Notepad, I had to figure to type "/edit " then the filename, not just the filename.
I suggest choose a menu number THEN prompt for the file name when that menu # is chosen.

Then to get the filename to Open I had to fix this line:
OPEN RIGHT$(file$, LEN(file$) - 6) FOR INPUT AS #1 '<<<<<<<<<<<<<<<<<<<<<<< to edit out "/edit"
again easier to just prompt for filename after edit menu # chosen

Then to get QB64 to compile the p1.bas translation, I had to fully path my QB64.exe file
Woops! Nope! It's still not compiling. I must of compiled it when looking at p1.bas through QB64 IDE.

Quote
ONE PROBLEM!
I can't seem to use nested loops in this. I am guessing because I am not using an array in "e$".
As is, your translator can go only one } deep with this code:
Code: [Select]
    ELSEIF INSTR(l$, "}") THEN
        IF e$ = "for" THEN
            PRINT #2, "NEXT"
        ELSEIF e$ = "if" THEN
            PRINT #2, "END IF"
        ELSEIF e$ = "funct" THEN
            PRINT #2, "END FUNCTION"
        ELSEIF e$ = "while" THEN
            PRINT #2, "WEND"
        END IF
        e$ = ""

You will need to count brackets deep { and find next equivalent } count for match up to same level of embedded code block for ENDing it.
« Last Edit: March 17, 2019, 10:24:39 AM by bplus »
B = B + ...


Re: The Best Interpreter I've ever made!
« Reply #4 on: March 19, 2019, 04:24:31 AM »
In fact
it is strange ..there is no any expression evaluator in your lang
and all looking like translator...am i right?
..and in just 126 lines of code ..require lot more subroutines to create minimal
interpreter.
What we have here is attempt for some sort of translator to qb64?
from the other side when i read your profile i figured that you are very young
and that is really refreshing for one BASIC forum ...just keep it !
I add comment to your github,io site..