From ThorstensHome
Home < Tutorials < How to program your own programming languageOK, although PHP already exists, we want to program our own programming language. This is an example for it, we call our programming language
stark. This article describes all about it.
Ideas
One idea of stark is that you can find every function definition very easy. For example, to find the definition of foo, search for "def foo".
Syntax
The following syntax shall be possible with our programming language:
write ("hello world");
a:=42;
b:=a;
def function1()
{
global b;
b:=b-1;
if (b>40) function1();
}
function1();
Implement it
Ok, first, we need a lexical analyzer. This will tell us what is a token (e.g. a command or the = sign), what a string literal and so on. This will be used by a compiler compiler to assign its parameters to every token (e.g. the command "print" can be followed by a number or by a string literal. This compiler compiler will build a command tree out of the source code, e.g. the above code will get:
type=command
name=write
type=string
content="hello world"
type=assignment
assignee=a
value=42
type=assignment
assignee=b
type=compute
content=a
type=function definition
name=function1
type=global
name=b
type=assignment
assignee=b
type=compute
content=b-1
type=if (requires three tokens, lvalue, rvalue and what-if-true)
operator="<"
type=compute
content=b
type=compute
content=40
type=functioncall
content=function1
Your lessons
See also