Page 1 of 1

Compiler or Interpeter?

Posted: Thu Dec 22, 2005 2:10 pm
by josku_x
Hello!

I've heard it is possible to make either a compiler or an interpeter in PB. But the idiotic thing is, I don't know what an interpeter is!!

I know what a compiler is, a compiler translates programming code into an executable or dll whatever. But what is an interpeter?

That is killing my head, I searched through google, but without luck :cry:

help me please!

EDIT: forgotted to tell you the point of this thread :wink: So, now I am working on my own language (don't be afraid Fred, I won't wrap any functions.) What should I begin to make? A compiler or a interpeter?
because, after I know what's a interpeter, I maybe want to make that and not a compiler. :P .

thanks!

Posted: Thu Dec 22, 2005 2:29 pm
by Ypser
An interpreter doesnt translate the Code to machine code, like a compiler does, but
executes the commands from source code. Or at least translates every line at runtime, not the
whole program at once before program start.
Thats why an interpreter usually is slower than a comiled exe.

Posted: Thu Dec 22, 2005 3:03 pm
by Trond

Posted: Thu Dec 22, 2005 4:01 pm
by srod
Interpreters are still widely used.

Most scripting engines run as interpreters (can't think of any which compile!) Programs which run under the original JAVA schemes, do so by being compiled to 'byte-code' and then being interpreted at runtime by the Java runtime engine (what else? :D )

Not sure where the latest Java or .Net fits into the traditional compile/interpret model though, what with the advent of 'just in time' compilation? Seems a strange way of doing things if you ask me!

'Ibasic standard' is an interpreter. Liberty basic runs by compiling to byte-code and then interpreting etc. I think code written for the original Darkbasic was interpreted (not sure about the latest version!)

Posted: Thu Dec 22, 2005 6:53 pm
by josku_x
Hehe, I have Darkbasic, it is full bullsh*it compared to Pure :lol:

Well, thank you for telling me what an interpeter is, well I am still confused of what I'll start to make, a compiler or interpeter.

Making an interpeter sounds harder than making a compiler, or how do you guys see it :? ?

Thanks!

Posted: Thu Dec 22, 2005 7:57 pm
by Nik
Making an interpreter is most often a lot easier because you don't need to be able to write good asm code.
bye Nik

Posted: Thu Dec 22, 2005 8:11 pm
by thefool
Nik wrote:Making an interpreter is most often a lot easier because you don't need to be able to write good asm code.
bye Nik
True. An interpretter is much much easier than a compiler.

Posted: Thu Dec 22, 2005 8:44 pm
by josku_x
Okay, I understand, well, it was clear for me as I discovered that I have to translate the code to machine code and then I was K. O. :lol:

I think an interpeter wouldn't be a big challenge for me, you know, I make unusual things :wink:

Posted: Thu Dec 22, 2005 9:41 pm
by Trond
If you know asm I'd say an compiler is just as easy as an interpreter if you don't care that the code you output isn't very optimized.

Posted: Thu Dec 22, 2005 11:00 pm
by Killswitch
It sure as hell is possible to write an interpreter in PB, and in fact I'm working on a compiler in PB right now:

viewtopic.php?t=18289

Posted: Thu Dec 22, 2005 11:06 pm
by srod
Well, thank you for telling me what an interpeter is, well I am still confused of what I'll start to make, a compiler or interpeter.
Josku_x, no offence, but if you're asking for the differences between a compiler and an interpreter, then perhaps it might be a little early to begin such a project yourself! :lol:

Start with an interpreter whilst you learn assembly through and through. Maybe start with a basic scripting engine and go from there. Build it up. As you learn the basic of parsing, then perhaps think of a full blown interpreter etc. There are some very good interpreted languages. And the best thing is, write it in PB and you can leverage all of Fred's hard work -hehe. :lol:

Posted: Fri Dec 23, 2005 6:00 am
by Amiga5k
Another way to put it is that an interpreter does not create an executable, per se, although you may be able to save your work as some sort of 'tokenized' program. The program is converted, or interpreted, into executable code in real time. The interpreter or some sort of runtime engine must be present in order for the created program to run.

One of the few advantages of an interpreted program is that it runs immediately since no compilation needs to take place. In the old 8 bit days, most of the BASICs that were part of a computer's OS were interpreted and many companies made compilers that actually did compile to machine code (for speed) or P-Code (for small size). After compilation, most programs enjoyed a 2 - 10x improvement in speed with no additional changes. There were actually a fair number of commercial programs sold that were compiled BASIC programs, rather than made-from-scratch asm.

My experience is for the Commodore 64, but I'm pretty sure that the Atari 400/600/800/XL and Apple ][, ][e 8 bit systems were similar in that respect.

Russell

Posted: Fri Dec 23, 2005 8:30 am
by blueznl
if you can dig it up somewhere, gfabasic was an interpreter, and a very fast one at that

what it actually did was 'process' the entered code at the moment you, well, entered it, it would quickly do a syntax check and you could not 'leave' the line unless the syntax was correct

that way, it accomplished two things:

1. no syntax checking necessary before running the program (as that would have been done on coding time)

2. fast execution (as code did not have to be tokenized anymore, and all error handling could be limited to catching value errors and such)

for an interpreter gfa was lightning fast, compiled (yes, there was a compiler later on) it was even faster

alas, gfa is no more, but there is pure these days :-)

Posted: Fri Dec 23, 2005 7:52 pm
by josku_x
:lol:

You all think I can't do it, and that's right :D

I am just learning ASM, and so on, but hey! don't underestimate me, or tomorrow everyone would buy my products on the market :twisted:


Well, nothing is impossible and if I trust myself, I can do everything (well, I need some practice with pb :? )

:wink:

speaking of interpreters

Posted: Sat Dec 24, 2005 3:40 am
by kake26
Well, people speaking of interpreters I've written a small one. Its based on a very strange language called Brainfuck. Mine is not a direct implementation of Brainfuck, but it is similar. I call it Brainfreeze, cause trying to comprehend it will make you go bat wacky crazy. Currently it can run most Brainfuck code , however Brainfreeze has two types of loops. One is a normal loop the other is threaded, yes it threads loops to allow async execution of code. It is a very simple,small and most importantly very fast interpreter. Just because I can I'm posting it here for anyone to enjoy or rip their hair out over, hehe. Enjoy :wink:.

Code: Select all

; brainfuck based interpreter
; This lang is brilliant since its so simple

; though I've taken it upon myself to do it more efficently

NewList stack.l();we store by ascii 
; pointers? we have no pointers only a linked list 
; This way hopefully we can prevent such large memory usage

Global program$
program$ = "+>:+..]>:++++++]<<." ; fun or try ,.[,.] 
program$ = program$ + "E" ; E is just added char for making sure it executes
count.l = 1

OpenConsole()

Procedure tloop(count)
Debug("start thread")
search = count 
track = stack()
Repeat
loops:
scc$ = Mid(program$, search, 1)
scc$ = Trim(scc$)
Debug "scc = " + scc$
Select scc$ 
Case "<" : Debug("Decrease Pointer"):PreviousElement(stack()) 
Case ">" : Debug("Increse Pointer"):AddElement(stack());NextElement(stack()) 
Case "+" : Debug("Add one"):stack() = stack() + 1
Case "-" : Debug("Subtract one"):stack() = stack() - 1
Case "." : Debug("Print thread "):PrintN(Chr(stack()))
Case "," : stack() = Asc(Input())
Case "]" : Debug("Thread LOOP END") : 
If stack <> track And stack() < 256 And stack() > 0 
search = count 
Goto loops 
Else 
Break
EndIf
Default : Debug("Default Threaded Loop");Debug stack(); do jack shit other than count
EndSelect
search = search + 1 
Until stack() = 0
Debug("end thread")
EndProcedure

PrintN("Pure BrainFreeze v0.1") ; main loop
AddElement(stack())
Repeat 
cc$ = Mid(program$, count, 1)
cc$ = Trim(cc$)
Debug cc$
Select cc$ 
Case "<" : Debug("Decrease Pointer"):PreviousElement(stack()) 
Case ">" : Debug("Increse Pointer"):AddElement(stack());NextElement(stack()) 
Case "+" : Debug("Add one"):stack() = stack() + 1
Case "-" : Debug("Subtract one"):stack() = stack() - 1
Case "." : Debug("Print  "):PrintN(Chr(stack()))
Case "," : stack() = Asc(Input())
Case ":" : Debug("Threaded Loop Start") : CreateThread(@tloop,count) : Debug(FindString(program$,"]",count)) ; threaded loop
Case "[" : Debug("Start Loop"): 
; start normal loop
search = count 
track = stack()
Repeat
tloops:
scc$ = Mid(program$, search, 1)
scc$ = Trim(scc$)
Debug "scc = " + scc$
Select scc$ 
Case "<" : Debug("Decrease Pointer"):PreviousElement(stack()) 
Case ">" : Debug("Increse Pointer"):AddElement(stack());NextElement(stack()) 
Case "+" : Debug("Add one"):stack() = stack() + 1
Case "-" : Debug("Subtract one"):stack() = stack() - 1
Case "." : Debug("Print  "):PrintN(Chr(stack()))
Case "," : stack() = Asc(Input())
Case "]" : Debug("LOOP END") : 
If stack <> track And stack() < 256 And stack() > 0 
search = count 
Goto tloops 
Else 
Break
EndIf
Default : Debug("Default Loop");Debug stack(); do jack shit other than count
EndSelect
search = search + 1 
Until stack() = 0
; end normal loop

If search+1 < Len(program$)
Debug(Len(program$))
Debug(count)
Debug(search)
count = search
Else
Debug(Len(program$))
Debug(count)
Debug(search)
Break
EndIf

Default :Debug("Default main"): Debug(count);Debug stack(); do jack shit other than count
EndSelect
;If count = Len(program$)
;count = count - 1
;Else
count = count + 1
;EndIf
Until count = Len(program$) And count < 256
;MessageRequester("Information",a$,#PB_MessageRequester_Ok)
Yeah crazy eh? But its fast and tiny. I've just proved I have to much time on my hands. Actually, thats not the case this code is just a expirament and no where near totally perfect yet, but it basically works so I decided to create one.