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

0 Members and 1 Guest are viewing this topic.

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
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: QB64: [Select]
  1. PRINT "Welcome to PLanguage Compiler! Enter your file name to see the results."
  2. PRINT "Extra commands:"
  3. PRINT "/cls - clear the screen"
  4. PRINT "/edit - edit a file using notepad"
  5. PRINT "/exit - to exit this program"
  6. PRINT STRING$(80, "-")
  7.  
  8. menu:
  9. INPUT ">>", file$
  10. IF INSTR(file$, "/edit") THEN
  11.     SHELL "notepad " + RIGHT$(file$, LEN(file$) - 6)
  12. ELSEIF file$ = "/exit" THEN
  13.     END
  14. ELSEIF file$ = "/cls" THEN
  15.     CLS
  16.     GOTO menu
  17. OPEN file$ FOR INPUT AS #1
  18. OPEN "p1.bas" FOR OUTPUT AS #2
  19.     LINE INPUT #1, l$
  20.     l$ = replace$(l$, "[", "(")
  21.     l$ = replace$(l$, "]", ")")
  22.     x = x + 1
  23.     IF INSTR(l$, "println") THEN
  24.         PRINT #2, "PRINT " + RIGHT$(l$, LEN(l$) - 7)
  25.     ELSEIF INSTR(l$, "input") THEN
  26.         l$ = replace$(l$, "(", "")
  27.         l$ = replace$(l$, ")", "")
  28.         PRINT #2, l$
  29.     ELSEIF INSTR(l$, "}") THEN
  30.         IF e$ = "for" THEN
  31.             PRINT #2, "NEXT"
  32.         ELSEIF e$ = "if" THEN
  33.             PRINT #2, "END IF"
  34.         ELSEIF e$ = "funct" THEN
  35.             PRINT #2, "END FUNCTION"
  36.         ELSEIF e$ = "while" THEN
  37.             PRINT #2, "WEND"
  38.         END IF
  39.         e$ = ""
  40.     ELSEIF INSTR(l$, "for") THEN
  41.         l$ = replace$(l$, "{", "")
  42.         PRINT #2, l$
  43.         e$ = "for"
  44.     ELSEIF INSTR(l$, "elif") THEN
  45.         l$ = replace$(l$, "{", "")
  46.         PRINT #2, "ELSEIF " + RIGHT$(l$, LEN(l$) - 4) + " THEN"
  47.     ELSEIF INSTR(l$, "else") THEN
  48.         l$ = replace$(l$, "{", "")
  49.         PRINT #2, l$
  50.  
  51.     ELSEIF INSTR(l$, "if") THEN
  52.         l$ = replace$(l$, "{", "") + " THEN"
  53.         PRINT #2, l$
  54.         e$ = "if"
  55.     ELSEIF INSTR(l$, "funct") THEN
  56.         l$ = replace$(l$, "{", "")
  57.         PRINT #2, "FUNCTION " + RIGHT$(l$, LEN(l$) - 5)
  58.         e$ = "funct"
  59.     ELSEIF INSTR(l$, "clear()") THEN
  60.         PRINT #2, "CLS"
  61.     ELSEIF INSTR(l$, "terminate()") THEN
  62.         PRINT #2, "END"
  63.     ELSEIF INSTR(l$, "wait") THEN
  64.         PRINT #2, "SLEEP"
  65.     ELSEIF INSTR(l$, "delay") THEN
  66.         PRINT #2, "_DELAY " + RIGHT$(l$, LEN(l$) - 5)
  67.     ELSEIF INSTR(l$, "terminate.instant()") THEN
  68.         PRINT #2, "SYSTEM"
  69.     ELSEIF INSTR(l$, "color") THEN
  70.         l$ = replace$(l$, "(", "")
  71.         l$ = replace$(l$, ")", "")
  72.         PRINT #2, l$
  73.     ELSEIF INSTR(l$, "while") THEN
  74.         l$ = replace$(l$, "{", "")
  75.         PRINT #2, l$
  76.         e$ = "while"
  77.     ELSEIF INSTR(l$, "createVector") THEN
  78.         PRINT #2, "TYPE " + RIGHT$(l$, LEN(l$) - 12)
  79.         PRINT #2, "X AS INTEGER"
  80.         PRINT #2, "Y AS INTEGER"
  81.         PRINT #2, "END TYPE"
  82.     ELSEIF INSTR(l$, "setScreen") THEN
  83.         PRINT #2, "SCREEN _NEWIMAGE" + RIGHT$(l$, LEN(l$) - 9)
  84.     ELSEIF INSTR(l$, "declare") THEN
  85.         l$ = replace$(l$, "[", "(")
  86.         l$ = replace$(l$, "]", ")")
  87.         PRINT #2, "DIM " + RIGHT$(l$, LEN(l$) - 7)
  88.     ELSE
  89.         IF INSTR(l$, "=") OR INSTR(l$, "}") OR LEN(l$) = 0 THEN
  90.             l$ = replace$(l$, "[", "(")
  91.             l$ = replace$(l$, "]", ")")
  92.             PRINT #2, l$
  93.         ELSE
  94.             PRINT "ERROR ON LINE"; x
  95.             PRINT l$
  96.             PRINT "Please try revising the code..."
  97.             END
  98.         END IF
  99.     END IF
  100. OPEN "p1.bas" FOR INPUT AS #1
  101.     LINE INPUT #1, l$
  102.     PRINT l$
  103. PRINT "Compiling Your File..."
  104. SHELL _HIDE "qb64 -x p1.bas"
  105. SHELL "p1.exe"
  106. GOTO menu
  107. FUNCTION replace$ (a$, from$, to$)
  108.     FOR i = 1 TO LEN(a$)
  109.         c$ = MID$(a$, i, 1)
  110.         IF c$ = from$ THEN
  111.             replace$ = replace$ + to$
  112.         ELSE
  113.             replace$ = replace$ + c$
  114.         END IF
  115.     NEXT i
  116.  

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

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
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 »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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: QB64: [Select]
  1.     ELSEIF INSTR(l$, "}") THEN
  2.         IF e$ = "for" THEN
  3.             PRINT #2, "NEXT"
  4.         ELSEIF e$ = "if" THEN
  5.             PRINT #2, "END IF"
  6.         ELSEIF e$ = "funct" THEN
  7.             PRINT #2, "END FUNCTION"
  8.         ELSEIF e$ = "while" THEN
  9.             PRINT #2, "WEND"
  10.         END IF
  11.         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 »

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: The Best Interpreter I've ever made!
« Reply #3 on: March 18, 2019, 04:26:53 am »
Using INSTR() to get command ...funny!
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
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..
« Last Edit: March 19, 2019, 06:44:24 am by Aurel »
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////