| This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet as we cannot guarantee the safety of other websites. |
- For more information, see Programming language on Wikipedia.
- This article is about programming languages in general. For the type of project that simulates a programming language, see Programming Language (project type).
A programming language is a set of rules and commands that let people program computers, cell phones, tablets, and almost every other device. Programming languages are designed to make it easy for humans to write complex instructions. They function a lot like human languages: they have explicit grammar and primitive vocabulary. Scratch is a programming language.
Theory
A programming language is a language for writing instructions for a machine. A programming language is defined by a grammar, or syntax.
Many programming paradigms, or ways of programming, exist which focus on different design aspects. Imperative languages focus on commands and how to accomplish tasks, declarative languages dictate the rules and conditions of a task and do not specify the process of completing it, and functional languages emphasize functions which do not change state, and are guaranteed to always produce the same output for a given input.
Languages may be designed to support specific paradigms.[1] Paradigms include but are not limited to:
- Imperative: Programs use statements
- Functional: Programs use functions and avoid state
- Declarative: Programs state rules of a problems
- Object-oriented programming: Messages pass between objects
- Procedural: Programs are separated into subroutines
- Logical: Programs express facts and rules based on formal logic
Languages usually have various primitive data types which can be expanded with object-oriented programming:
- Numbers, which are parsed as numbers.
- Many languages consider integers and floating-point numbers to be different datatypes.
- Strings, which are parsed as text.
- Booleans, which are parsed as true/false values.
- Arrays, which are lists of elements.
- Functions, which are pieces of code which can be executed with Arguments.
In essence, a programming language just provides a framework where a function can be executed with arguments—the rest can be worked around. Usually, the grammar of a language consists of "statements" and "expressions". Statements are are usually one of the following:
- Assignments: binding some value to an identifier (variable).
- Procedure calls.
- Special forms: certain specialized procedures which cannot be created using the language itself, for example, IF/ELSE. The inputs to special forms are not immediately evaluated.
Expressions usually consist of operands and operators. For example, 1 + 10, 100 / 5, 6 == 6, etc.
The syntax of a programming language gives rules about how to do each of the above. For example:
- Assignments:
- Squeak:
x _ val - JavaScript:
let x = val; - Scheme:
(SET! x val)
- Squeak:
- Procedure calls:
- Squeak:
obj proc: arg - JavaScript:
proc(arg); - Scheme:
(proc arg)
- Squeak:
- Special forms (if, in this case):
- Squeak:
bool ifTrue: [something] ifFalse: [something]. - JavaScript:
if (bool) {something;} else {something;} - Scheme:
(if bool something something)
- Squeak:
Scratch simplifies programming a lot by hiding all of this in blocks: all blocks are equal, there are no special forms. Of course, in reality, certain blocks are programmed completely differently as special cases. For example, the IF block needs special programming to execute the C block contents, and the WAIT UNTIL block needs special programming to escape the atomic loop. Assignment is just another block. The REPEAT UNTIL special form block repeatedly evaluates its Boolean input, then its block input, until the former input evaluates to true. This contrasts with custom blocks, whose inputs are evaluated prior to their execution.
Implementation
Programming languages are generally either interpreted or compiled, which means they are either executed directly, or translated into another language. For example, C and C# are compiled while PHP and JavaScript are interpreted. Java is compiled into bytecode, which is interpreted by the Java Virtual Machine. Python is both compiled and interpreted.
| Java and JavaScript are two completely different programming languages with different grammars, semantics, creation, and uses. |
| JavaScript was named after Java, to capitalize on it's popularity at the time. |
The steps of a compiler can be separated into the front end and the back end. The front end translates the source language into an intermediate representation and the back end of a compiler translates the intermediate representation into machine code.
Language implementations usually consist of the following parts:
Front end
- A preprocessor: This cleans up syntax and can add extra code.
- A lexer: This converts the program into tokens.
- A parser: This analyzes the tokens as per the context-free grammar of the language, then converts them into a parse tree that can easily be interpreted.
- A semantic analyzer: This walks through the parse tree and determines if the code is valid.
- An intermediate code generator: This translates the parse tree into an intermediate representation.
Back end
- A code optimizer: This improves the program by making it smaller or faster.
- An interpreter or compiler: The interpreter runs the program and the compiler generates an output executable.
- (Optional) A linker: This links different output files into one executable.
The lexer accepts the program as an input and tokenizes it, or splits it into substrings with semantic meanings. The tokens may be defined by regular expressions. The lexer's output has two parts: the lexemes, which are the different substrings of the program, and the tokens, which are lexemes' classifications. For example, the JavaScript code var x = 0; could be separated into the following tokens:
| Lexeme | Token |
|---|---|
| var | var |
| x | identifier |
| = | assignment_operator |
| 0 | integer_literal |
| ; | semicolon |
The parser analyzes the tokens and lexemes produced by the lexer program and creates an abstract syntax tree (AST). The parser utilizes a context-free grammar, the specification of the syntax. In a grammar, symbols called nonterminals are defined by productions, sequences of tokens and nonterminals. Algorithms such as LALR or Earley may be used, and the parser may be table-driven or handwritten.
Parsing algorithms are classified into top-bottom parsing, which starts with the root node and constructs the tree to the leaves, and bottom-up parsing, which start with the tokens and work up to the root node. Bottom-up parsing consists of shifts, where the next token may be pushed onto the parse stack, and reduction, where items on the stack are found to match a production, and replaced with it.[2] Lookahead tokens are used when whether to shift or reduce is undecided.[3]
After the parse tree is created, it may be simplified into an abstract syntax tree (AST). Then, the tree is traversed and its nodes are read. Here, the compiler may check that all the variables are declared and, if the language is statically-typed, check if all the datatypes match.
Afterwards, a lower-level intermediate representation of the program is created.
Scratch as a Programming Language
Scratch is a block-based imperative, event-driven, dynamically-typed and interpreted programming language.
Typing
Scratch is dynamically-typed, meaning that whether data types agree is checked during program execution.[4] Scratch's primitive data types are numbers, strings and Booleans.
Special Forms
Scratch's special forms are blocks which cannot be replicated using custom blocks. These blocks may reevaluate their inputs. For example, the repeat-until loop reevaluates its Boolean input before each iteration to see if the condition's value has changed.[5]
Implementation
Scratch is an interpreted programming language. Scratch 3.0 uses a virtual machine, which builds an abstract syntax tree.[6]
Programming Languages and Libraries Relevant to Scratch
JavaScript
JavaScript (commonly shortened to JS) is a simple programming language based on ECMAScript for web development. It is used to develop Scratch 3.0 and a large part of the Scratch Website. Snap! is also written almost entirely in JavaScript.
TypeScript
TypeScript is a version of JavaScript that introduces typed values. It provides no runtime difference but makes it easier to fix bugs. Its use in Scratch 3.0 is currently increasing.
React
React is a JavaScript library for building user interfaces. It is used for most pages on the Scratch Website, as well as for the Scratch 3.0 editor.
Python
- Main article: Python
Python is a simple interpreted and compiled scripting language that is used for a part of the back-end (server-side code) of the Scratch Website. The Django coding platform is used on a nginx server.[7] The forums run on DjangoBB, a Python library. Python is also used in many user-created Scratch tools, including Kurt.
SQL
SQL (Structured Query Language) is a database query language. This is used to store backend information on the website, such as users and forum data.
MongoDB
MongoDB is a database system that is used to store cloud variables.
GL Shader Language
GLSL is a programming language used in OpenGL and WebGL to process vertices and fragments. It is being used in the Scratch 3.0 renderer.[8]
PHP
PHP is a programming language which was used in the Scratch 1.x website's server-side code (ScratchR) and is still used on the Scratch Wiki. PHP stands for "PHP: Hypertext Preprocessor" (a recursive acronym).
Flash
Adobe Flash is a programming suite by Adobe, which was used to create Scratch 2.0. Flash programming is done in ActionScript, a language based on ECMAScript.
Squeak
- Main article: Smalltalk
Squeak was used to program Scratch 1.x. It is a simple language designed to be human-readable and concise. See Squeak Tutorial for a simple introduction.
Java
Java is an object oriented programming language. Java was used to write the Java Player for Scratch.
Markup Languages
HTML
HTML is a web-based markup language.
Cascading Style Sheets
Cascading Style Sheets (commonly shortened to CSS) is a method of styling HTML documents. It is used to add color and make websites more visually appealing.
See Also
External links
References
- ↑ http://cs.lmu.edu/~ray/notes/paradigms/
- ↑ http://sites.tufts.edu/comp181/2013/10/06/shift-reduce-parsing-bottom-up-parsing/
- ↑ http://www.cs.engr.uky.edu/~lewis/essays/compilers/td-parse.html
- ↑ https://docs.oracle.com/cd/E57471_01/bigData.100/extensions_bdd/src/cext_transform_typing.html
- ↑ https://snap.berkeley.edu/snap/help/SnapManual.pdf
- ↑ https://github.com/LLK/scratch-vm
- ↑ post:44464
- ↑ https://github.com/LLK/scratch-render/tree/b00f6b4/src/shaders