# 脚本语言背后的软件抽象层

• 虚拟机（Virtual Machine）
• 解释器（Interpreter）
• 运行时环境 （Run-time environment）
• 扩展语言（Extension language）

## 脚本语言（Scripting language）

The term “scripting language” is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl, and Python

In the case of scripting an application, this is also known as an extension language.

• Bash, for the Unix or Unix-like operating systems
• ECMAScript (JavaScript), for web browsers
• Visual Basic for Applications, for Microsoft Office applications.

• Vimscript
• Emacs Lisp
• sed and AWK

## 运行时环境（Run-time environment）

Every programming language has some form of a runtime system, whether the language is a compiled language, interpreted language, embedded domain-specific language, or is invoked via an API as is pthreads.

• The runtime system of the C language is a particular set of instructions inserted into the executable image by the compiler.
• The OS kernel can be viewed as a runtime system, and that the set of OS calls that invoke OS behaviors may be viewed as an API invoked language.
• For assembly language, the physical CPU itself can be viewed as an implementation of the runtime system for a programming language.

runtime的具体实质其实非常宽泛，针对不同的计算机语言，它所代指的内涵差异非常大。 但对于每一种计算机语言，runtime其实都提供了最重要的一个功能——解释语言所描述的行为。

A run-time system, also called runtime system, or just runtime, exhibits the behavior of the constructs of a computer language.

In addition to the behavior of the language constructs, a runtime system may also perform support services such as type checking, debugging, or code generation and optimization.

A scripting language or script language is a programming language that supports scripts, programs written for a special run-time environment that can interpret (rather than compile) and automate the execution of tasks that could alternatively be executed one-by-one by a human operator.

Environments that can be automated through scripting include

• software applications,
• web pages within a web browser,
• the shells of operating systems (OS), and
• embedded systems.

• 游戏引擎：渲染游戏画面
• 浏览器：与服务器端通信并修改加载当前的HTML页面
• MS Office：执行文档的批处理操作，比如Excel

In the limit, the run-time system may provide services such as a P-code machine or virtual machine, that hide even the processor’s instruction set. This arrangement greatly simplifies the task of language implementation and its adaptation to different machines, and allows sophisticated language features such as reflection.

## 虚拟机（Virtual Machine）

In computing, a virtual machine (VM) is an emulation of a particular computer system.

• CPU的加减乘除移位操作：不就几个+ - * / >>运算符么……
• 寄存器：几个变量而已，你想要几位的呢？我可以用数组给你仿个1024位的！
• 内存：向kernel申请内存就是了
• I/O：调用现有的I/O访问模块

VM有一套与真实CPU相对应的执行流程。

    Real          Virtual

+--------+ | +------------+
|Assembly| | |Intermediate|
|language| | |  language  |
+--------+ | +------------+
|      |        |
+--------+ |   +--------+
|Machine | |   |Bytecode|
|language| |   +--------+
+--------+ |
|      |        |
+---+    |      +--+
|CPU|    |      |VM|
+---+    |      +--+


## 解释器（Interpreter）

In computer science, an interpreter is a computer program that directly executes, i.e. performs, instructions written in a programming or scripting language, without previously compiling them into a machine language program.

1. parse the source code and perform its behavior directly
2. translate source code into some efficient intermediate representation and immediately execute this
3. explicitly execute stored precompiled code made by a compiler which is part of the interpreter system

Both compilers and interpreters generally turn source code (text files) into tokens, both may (or may not) generate a parse tree, and both may generate immediate instructions (for a stack machine, quadruple code, or by other means).

The basic difference is that a compiler system, including a (built in or separate) linker, generates a stand alone machine code program, while an interpreter system instead performs the actions described by the high level program.