Způsob provedení
Python,TCLandvariousShellprogramsaregenerallyexecutedusinganinterpreter.Microsoft'sQbasiclanguageisalsoaninterpretationmethod,itcannotgenerateexecutableprograms(butQuickBasicandVisualBasiccan);thewidelyusednetworkprogramminglanguagejavahasbothinterpretationandcompilationmethods.
Beforewestart,itisnecessarytoemphasizeagain:theinterpreterdescribedbelowisasourcecodeinterpreter.Inotherwords,whentheinterpreterisexecuting,itreadsonestatementatatime,andperformsaspecificoperationbasedonthisstatement;thenreadsthenextstatement,andsoon.Thisisdifferentfromthepseudo-codeinterpreter,suchastheearlyJavaruntimesystem.Thedifferencebetweenthetwois:thesourcecodeinterpreterdirectlyinterpretsandexecutesthesourcecodeoftheprogram;whilethepseudo-codeinterpreterfirstconvertsthesourcecodeoftheprogramintosomekindofintermediatecodethathasnothingtodowiththemachine,andthenexecutestheintermediatecode.Incontrast,asourcecodeinterpreteriseasiertocreateanddoesnotrequireaseparatecompilationprocess.
Subsystémy
SmallBASICinterpreterincludestwomainsubsystems:oneistheexpressionparser,whichisresponsibleforprocessingnumericexpressions;theotheristheinterpreter,whichisresponsiblefortheprogramActualimplementation.Fortheformer,theexpressionparserintroducedinChapter2ofthisbookcanbeused.Butsomeimprovementshavebeenmadeheresothattheparsercanparsenumericexpressionscontainedinprogramstatements,ratherthanonlyparseisolatedexpressions.
Theinterpretersubsystemandtheparsersubsystemarecontainedinthesameinterpreterclass,whichisnamedSBasic.Althoughtheoreticallyyoucanusetwoindependentclasses:onecontainstheinterpreterandtheothercontainstheexpressionparser;butthegenerationefficiencywillbehigherifthetwoareimplementedinthesameclass,becausetheexpressionparserandtheinterpretationThecodeofthedeviceisinseparable.Forexample,bothsubsystemsoperateonthesamecharacterarraythatholdstheprogramcode.Iftheyarearrangedintwocategories,itwillincreaseconsiderableadditionaloverhead,andcauseperformancelossandfunctionalduplication.Inaddition,sincethetaskofprograminterpretationisheavyandparsingexpressionsisonlypartofit,itmakessensetoincludetheentireinterpretationmechanisminasingleclass.
Whentheinterpreterexecutes,itreadsanidentifierfromthesourcecodeoftheprogrameachtime.Ifwhatisreadisakeyword,theinterpreterwillperformthespecifiedoperationinaccordancewiththerequirementsofthekeyword.Forexample,whentheinterpreterreadsaPRINT,itwillprintthecharactersafterthePRINT;whenitreadsaGOSUB,itwillexecutethespecifiedsubroutine.Thisprocesswillberepeatedbeforereachingtheendoftheprogram.Asyoucansee,theinterpretersimplyperformstheactionsspecifiedbytheprogram.
Interpretace a kompilace
Tlumočník spouští program:
1. Přímo spusťte programovací jazyk na vysoké úrovni (jako je vlastní tlumočník shellu)
2.Convertthehigh-levelprogramminglanguagecodetosomeefficientbytecode,andrunthesebytecodes
3.Compilethehigh-levellanguagewiththecompilerincludedintheinterpreter,Andinstructtheprocessortorunthecompiledprogram(forexample:JIT)
Perl,Python,MATLAB,andRubybelongtothesecondmethod,whileUCSDPascalbelongstothethirdmethod.Intheprocessoftranslation,theprogramswritteninthisgroupofhigh-levellanguagesarestillmaintainedinthesourcecodeformat(orsomerelaylanguageformat),andtheactionsorbehaviorsreferredtobytheprogramitselfarerepresentedbytheinterpreter.
Usinganinterpretertoruntheprogramwillbeslowerthanrunningthecompiledmachinecodedirectly,buttherelativebehaviorofthisliteraltranslationwillbefasterthancompilingandthenrunning.Thisisespeciallyimportantintheprototypestageofprogramdevelopmentandwhenwritingexperimentalcode,becausethis"edit-literal-debug"cycleisusuallymoretime-savingthanthe"edit-compile-run-debug"cycle.many.
Runningtheprogramontheinterpreterisslowerthanrunningthecompiledcodedirectly,becausetheinterpretermustanalyzeandinterprettheprogramlineitrunseverytime,andthecompiledprogramisjustRundirectly.Thisanalysisatruntimeiscalled"literalcost".Intheinterpreter,variableaccessisalsorelativelyslow,becauseeverytimeitwantstoaccessavariable,itmustfindoutwherethevariableisactuallystored,unlikethecompiledprogramthatdeterminesthevariablewhenitiscompiled.Location.
Therearemanycompromisesbetweenusinganinterpretertoachievefasterdevelopmentspeedandusingacompilertoachievefasterrunningprogress.Somesystems(suchassomeLISP)allowliteralandcompiledcodetocalleachotherandsharevariables.Thismeansthatonceasubroutinehasbeentestedintheinterpreterandthemissesareeliminated,itcanbecompiledtogetfasterexecutionprogress.Manyinterpretersdonotruntheoriginalcodeasthenamesuggests,butinsteadconverttheoriginalcodeintoamorecompressedinternalformat.Forexample,someBASICinterpretersreplacekeywordswithasinglebytesymbolthatcanbeusedtofindthecorrespondinginstructioninthejumptable.Theinterpretercanalsousealexicalanalyzerandaparserlikeacompiler,andthentranslatetheabstractsyntaxtreethatisgenerated.
Goodportability,literaltranslationprogramshavebetterportabilitythancompiledprograms,andcanbeeasilyrunondifferentsoftwareandhardwareplatforms.Thecompiledprogramisonlylimitedtorunningonthedevelopmentenvironmentplatform.
Byteinterpretace
Consideringthetimerequiredtoanalyzetheprogrambeforerunning,thereisapossibilitybetweenliteraltranslationandcompilation.Forexample,sourcecodewritteninEmacsLispwillbecompiledintoahighlycompressedandoptimizedLispsourcecodeformat.Thisisakindofbytecode,whichisnotmachinecode(soitwillnotbeTiedtospecifichardware).This"compiled"codewillthenbetranslatedbyabytecodeinterpreter(writteninC).Inthiscase,this"compiled"codecanbesaidtobethemachinecodeofavirtualmachine(notrealhardware,butabytecodeinterpreter).ThismethodisusedintheForthcodeusedbytheOpenFirmwaresystem:theoriginalprogramwillbecompiledinto"Fcode"(abytecode),andthenliterallytranslatedandrunbyavirtualmachineonaspecificplatform.