<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Creating_a_Programming_Language</id>
	<title>Creating a Programming Language - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Creating_a_Programming_Language"/>
	<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Creating_a_Programming_Language&amp;action=history"/>
	<updated>2026-05-20T02:33:07Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Creating_a_Programming_Language&amp;diff=1824&amp;oldid=prev</id>
		<title>Patarakin: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Creating_a_Programming_Language&amp;diff=1824&amp;oldid=prev"/>
		<updated>2022-07-21T08:33:17Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 11:33, 21 июля 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Patarakin</name></author>
	</entry>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Creating_a_Programming_Language&amp;diff=1823&amp;oldid=prev</id>
		<title>scratch&gt;NFlex23: /* Tokenizer */ Added a comment for clarification</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Creating_a_Programming_Language&amp;diff=1823&amp;oldid=prev"/>
		<updated>2022-07-08T19:30:20Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Tokenizer: &lt;/span&gt; Added a comment for clarification&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This tutorial teaches how to create a simple [[Programming Language|programming language]] that uses [[wikipedia:S-Expression|s-expressions]] for its instructions. Here is an example snippet of the language:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
(say (+ (* 4 7) 2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[projects:664143277|This project]] contains the completed code if you don&amp;#039;t want to go through the whole tutorial.&lt;br /&gt;
&lt;br /&gt;
==Prerequisites==&lt;br /&gt;
You will need to create these [[Variables]].&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(i)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(accumulator)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(token)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also create these [[Lists]].&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(tokens :: list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(instructions :: list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(operator stack :: list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
* &amp;lt;scratchblocks&amp;gt;(stack :: list)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tokenizer==&lt;br /&gt;
The [[Programming Language (project type)#Lexer|tokenizer]] (also known as a lexer) converts a program into a list of [[wikipedia:Lexical Analysis#Token|tokens]], or bits of the original program like words, numbers, and parentheses.&lt;br /&gt;
{{note|All of the following custom blocks should have &amp;quot;Run without screen refresh&amp;quot; checked.}}&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
define tokenize (program)&lt;br /&gt;
set [i v] to [1]&lt;br /&gt;
set [accumulator v] to []&lt;br /&gt;
delete all of [tokens v]&lt;br /&gt;
repeat until &amp;lt;(i) &amp;gt; (length of (program))&amp;gt;&lt;br /&gt;
  set [token v] to (letter (i) of (program))&lt;br /&gt;
  if &amp;lt;[() ] contains (token)&amp;gt; then // Make sure to include a space after &amp;quot;()&amp;quot; or it won&amp;#039;t work correctly&lt;br /&gt;
    if &amp;lt;not &amp;lt;(accumulator) = []&amp;gt;&amp;gt; then&lt;br /&gt;
	  add (accumulator) to [tokens v]&lt;br /&gt;
	  set [accumulator v] to []&lt;br /&gt;
	end&lt;br /&gt;
	if &amp;lt;[()] contains (token)&amp;gt; then&lt;br /&gt;
	  add (token) to [tokens v]&lt;br /&gt;
	end&lt;br /&gt;
  else&lt;br /&gt;
    set [accumulator v] to (join (accumulator) (token))&lt;br /&gt;
  end&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;not &amp;lt;(accumulator) = []&amp;gt;&amp;gt; then&lt;br /&gt;
  add (accumulator) to [tokens v]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Parser==&lt;br /&gt;
The [[Programming Language (project type)#Parser|parser]] takes in the tokens and builds a [[wikipedia:Parse tree|parse tree]], or in the case of this language, a simple form of [[wikipedia:Assembly language|assembly]].&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
define parse&lt;br /&gt;
delete all of [instructions v]&lt;br /&gt;
delete all of [operator stack v]&lt;br /&gt;
repeat until &amp;lt;(length of [tokens v]) = [0]&amp;gt;&lt;br /&gt;
  set [token v] to (item [1] of [tokens v])&lt;br /&gt;
  delete [1] of [tokens v]&lt;br /&gt;
  if &amp;lt;(token) = [(]&amp;gt; then&lt;br /&gt;
    add (item [1] of [tokens v]) to [operator stack v]&lt;br /&gt;
	delete [1] of [tokens v]&lt;br /&gt;
  else&lt;br /&gt;
    if &amp;lt;(token) = [)]&amp;gt; then&lt;br /&gt;
	  add (item (length of [operator stack v]) of [operator stack v]) to [instructions v]&lt;br /&gt;
	  delete (length of [operator stack v]) of [operator stack v]&lt;br /&gt;
	else&lt;br /&gt;
	  add [push] to [instructions v]&lt;br /&gt;
	  add (token) to [instructions v]&lt;br /&gt;
	end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(length of [operator stack v]) &amp;gt; [0]&amp;gt; then&lt;br /&gt;
  say [Unclosed parenthesis] for [2] seconds&lt;br /&gt;
  stop [all v]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Evaluator==&lt;br /&gt;
The last step is evaluating (also known as [[Programming Language (project type)#Interpretation|interpreting]]) the parsed instructions. Without this step, all we would have is a list of instructions which is pretty useless on its own.&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
define evaluate&lt;br /&gt;
set [i v] to [1]&lt;br /&gt;
delete all of [stack v]&lt;br /&gt;
repeat until &amp;lt;(i) &amp;gt; (length of [instructions v])&amp;gt;&lt;br /&gt;
  run instruction (item (i) of [instructions v])&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
define run instruction (operator)&lt;br /&gt;
if &amp;lt;(operator) = [push]&amp;gt; then&lt;br /&gt;
  add (item ((i) + [1]) of [instructions v]) to [stack v]&lt;br /&gt;
  change [i v] by [2]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(operator) = [say]&amp;gt; then&lt;br /&gt;
  say (item (length of [stack v]) of [stack v]) for [2] seconds&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(operator) = [+]&amp;gt; then&lt;br /&gt;
  replace item ((length of [stack v]) - [1]) of [stack v] with ((item ((length of [stack v]) - [1]) of [stack v]) + (item (length of [stack v]) of [stack v]))&lt;br /&gt;
  delete (length of [stack v]) of [stack v]&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(operator) = [-]&amp;gt; then&lt;br /&gt;
  replace item ((length of [stack v]) - [1]) of [stack v] with ((item ((length of [stack v]) - [1]) of [stack v]) - (item (length of [stack v]) of [stack v]))&lt;br /&gt;
  delete (length of [stack v]) of [stack v]&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(operator) = [*]&amp;gt; then&lt;br /&gt;
  replace item ((length of [stack v]) - [1]) of [stack v] with ((item ((length of [stack v]) - [1]) of [stack v]) * (item (length of [stack v]) of [stack v]))&lt;br /&gt;
  delete (length of [stack v]) of [stack v]&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
if &amp;lt;(operator) = [/]&amp;gt; then&lt;br /&gt;
  replace item ((length of [stack v]) - [1]) of [stack v] with ((item ((length of [stack v]) - [1]) of [stack v]) / (item (length of [stack v]) of [stack v]))&lt;br /&gt;
  delete (length of [stack v]) of [stack v]&lt;br /&gt;
  change [i v] by [1]&lt;br /&gt;
  stop [this script v]&lt;br /&gt;
end&lt;br /&gt;
... // You can add your own instructions to make the language even better!&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Putting It All Together==&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
tokenize [(say (+ (* 4 7) 2))] :: custom&lt;br /&gt;
parse :: custom&lt;br /&gt;
evaluate :: custom&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
When you run the program, the sprite should say &amp;quot;30&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Final Thoughts==&lt;br /&gt;
While the language works, it could be greatly improved. Here are some things you could do to improve it:&lt;br /&gt;
* More functions, like &amp;lt;code&amp;gt;ask&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;say-for-seconds&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Better [[wikipedia:Exception handling|error handling]]. Currently, the language doesn&amp;#039;t care if you do something wrong, like &amp;lt;code&amp;gt;(say 1 2 3)&amp;lt;/code&amp;gt;.&lt;br /&gt;
* [[wikipedia:Control flow|Control flow]] functions, like &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;. To implement these you would need to create some sort of [[wikipedia:Control flow#Labels|jumping system]] to move around the program.&lt;br /&gt;
A project with all of the features above can be viewed [[projects:656513485|here]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Math Tutorials]] [[Category:Scripting Tutorials]]&lt;/div&gt;</summary>
		<author><name>scratch&gt;NFlex23</name></author>
	</entry>
</feed>