In code terms, Script Editor creates an instance of AppleScript in the host, passing the code as text. It then tells the script to compile, and assuming it does, it then asks for its styled source, which is returned as an attributed string. The compiled script is essentially what's saved to disk. In code:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "OSAKit"
-- create a new AppleScript instance
set anOSALanguageInstance to current application's OSALanguageInstance's languageInstanceWithLanguage:(current application's OSALanguage's defaultLanguage())
-- make script and run it from the new instance
set theSource to "display dialog \"Hello world\""
set theScript to current application's OSAScript's alloc()'s initWithSource:theSource fromURL:(missing value) languageInstance:anOSALanguageInstance usingStorageOptions:(current application's OSANull)
set {theResult, theError} to theScript's compileAndReturnError:(reference)
if theResult as boolean is false then
-- handle error
else
set sourceText to theScript's source()
set styledSourceText to theScript's richTextSource()
end if
Run that in Script Debugger and you will see what styledSourceText looks like.
Other editors may use the original Carbon APIs; OSAScript is just an Objective-C wrapper around them.
The important point, from your perspective, is that compilation and styling a completely opaque process.